Author Topic: jPCT and real 3D  (Read 8023 times)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: jPCT and real 3D
« Reply #15 on: October 11, 2012, 07:23:53 am »
I'm not aware of any problem with blitting. I'll try to create a simple test case today and post the results and the code.

Offline guillaume

  • int
  • **
  • Posts: 67
    • View Profile
Re: jPCT and real 3D
« Reply #16 on: October 11, 2012, 01:14:16 pm »
I'm not aware of any problem with blitting. I'll try to create a simple test case today and post the results and the code.
thanks, Egon. I appreciate your help very much.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: jPCT and real 3D
« Reply #17 on: October 11, 2012, 05:28:30 pm »
Here's a simple example. It contains a little quirk...it renders into the render targets and then, after removing the render target, it renders into an empty world that shares its camera with the normal world. This doesn't cost any performance at all, but it's a quick way to set the viewport to the desired size. This doesn't happen correctly otherwise, because of...engine internals...(No, it's not a hack... ;) )

Hope this helps:

Code: [Select]
package com.example.helloworldsplitted;

import java.lang.reflect.Field;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;

import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Light;
import com.threed.jpct.Logger;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.RGBColor;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.World;
import com.threed.jpct.util.MemoryHelper;

/**
 *
 * @author EgonOlsen
 *
 */
public class HelloWorldSplitted extends Activity {

// Used to handle pause and resume...
private static HelloWorldSplitted master = null;

private GLSurfaceView mGLView;
private MyRenderer renderer = null;
private FrameBuffer fb = null;
private World world = null;
private World emptyWorld=null;

private Texture renderTarget = null;
private Texture renderTarget2 = null;

private float touchTurn = 0;
private float touchTurnUp = 0;

private float xpos = -1;
private float ypos = -1;

private Object3D cube = null;
private int fps = 0;

private Light sun = null;

protected void onCreate(Bundle savedInstanceState) {

Logger.log("onCreate");

if (master != null) {
copy(master);
}

super.onCreate(savedInstanceState);
mGLView = new GLSurfaceView(getApplication());
mGLView.setEGLContextClientVersion(2);

renderer = new MyRenderer();
mGLView.setRenderer(renderer);
setContentView(mGLView);
}

@Override
protected void onPause() {
super.onPause();
mGLView.onPause();
}

@Override
protected void onResume() {
super.onResume();
mGLView.onResume();
}

@Override
protected void onStop() {
super.onStop();
}

private void copy(Object src) {
try {
Logger.log("Copying data from master Activity!");
Field[] fs = src.getClass().getDeclaredFields();
for (Field f : fs) {
f.setAccessible(true);
f.set(this, f.get(src));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public boolean onTouchEvent(MotionEvent me) {

if (me.getAction() == MotionEvent.ACTION_DOWN) {
xpos = me.getX();
ypos = me.getY();
return true;
}

if (me.getAction() == MotionEvent.ACTION_UP) {
xpos = -1;
ypos = -1;
touchTurn = 0;
touchTurnUp = 0;
return true;
}

if (me.getAction() == MotionEvent.ACTION_MOVE) {
float xd = me.getX() - xpos;
float yd = me.getY() - ypos;

xpos = me.getX();
ypos = me.getY();

touchTurn = xd / -100f;
touchTurnUp = yd / -100f;
return true;
}

try {
Thread.sleep(15);
} catch (Exception e) {
// No need for this...
}

return super.onTouchEvent(me);
}

protected boolean isFullscreenOpaque() {
return true;
}

class MyRenderer implements GLSurfaceView.Renderer {

private long time = System.currentTimeMillis();

public MyRenderer() {
}

public void onSurfaceChanged(GL10 gl, int w, int h) {
if (fb != null) {
fb.dispose();
}
fb = new FrameBuffer( w, h);

if (master == null) {

emptyWorld = new World();

world = new World();
world.setAmbientLight(20, 20, 20);

sun = new Light(world);
sun.setIntensity(250, 250, 250);
cube = Primitives.getCube(10);
cube.strip();
cube.build();

world.addObject(cube);

Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
cam.lookAt(cube.getTransformedCenter());

SimpleVector sv = new SimpleVector();
sv.set(cube.getTransformedCenter());
sv.y -= 100;
sv.z -= 100;
sun.setPosition(sv);
MemoryHelper.compact();

renderTarget=new Texture(fb.getWidth(), fb.getHeight());
renderTarget2=new Texture(fb.getWidth(), fb.getHeight());

if (master == null) {
Logger.log("Saving master Activity!");
master = HelloWorldSplitted.this;
}
}
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}

public void onDrawFrame(GL10 gl) {
if (touchTurn != 0) {
cube.rotateY(touchTurn);
touchTurn = 0;
}

if (touchTurnUp != 0) {
cube.rotateX(touchTurnUp);
touchTurnUp = 0;
}

fb.setRenderTarget(renderTarget);
fb.clear(RGBColor.RED);
world.renderScene(fb);
world.draw(fb);
fb.setRenderTarget(null);

fb.setRenderTarget(renderTarget2);
fb.clear(RGBColor.GREEN);
world.renderScene(fb);
world.draw(fb);
fb.setRenderTarget(null);

fb.clear();
emptyWorld.setCameraTo(world.getCamera());
emptyWorld.renderScene(fb);
emptyWorld.draw(fb);

fb.blit(renderTarget, 0, 0, 0, 0, renderTarget.getWidth(), -renderTarget.getHeight(), fb.getWidth()/2, fb.getHeight(), -1, false, null);
fb.blit(renderTarget2, 0, 0, fb.getWidth()/2, 0, renderTarget2.getWidth(), -renderTarget2.getHeight(), fb.getWidth()/2, fb.getHeight(), -1, false, null);

fb.display();

if (System.currentTimeMillis() - time >= 1000) {
Logger.log(fps + "fps");
fps = 0;
time = System.currentTimeMillis();
}
fps++;
}
}
}

« Last Edit: October 12, 2012, 09:39:37 am by EgonOlsen »

Offline guillaume

  • int
  • **
  • Posts: 67
    • View Profile
Re: jPCT and real 3D
« Reply #18 on: October 20, 2012, 07:07:19 am »
thanks. this really helps. 8)