10
« on: April 04, 2012, 09:36:46 pm »
Here is the code I am using. Rotate the top pink box to the left (using the up arrow key) until it is at the same height as the green bottom box (PI rotation on Z). Then (using the down arrow key) rotate it to the back (X rotation) reproduces the behaviour I described.
package com.cubetest.example;
import java.lang.reflect.Field;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.KeyEvent;
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.Matrix;
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.TextureManager;
import com.threed.jpct.World;
import com.threed.jpct.util.BitmapHelper;
import com.threed.jpct.util.MemoryHelper;
/**
*/
public class HelloWorld extends Activity {
// Used to handle pause and resume...
private static HelloWorld master = null;
private GLSurfaceView mGLView;
private MyRenderer renderer = null;
private FrameBuffer fb = null;
private World world = null;
private RGBColor back = new RGBColor(50, 50, 100);
private RGBColor c1 = new RGBColor(50, 100, 50);
private RGBColor c2 = new RGBColor(100, 50, 50);
private Object3D cb=null;
protected void onCreate(Bundle savedInstanceState) {
Logger.log("onCreate");
if (master != null) {
copy(master);
}
super.onCreate(savedInstanceState);
mGLView = new GLSurfaceView(getApplication());
mGLView.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() {
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
// Ensure that we get a 16bit framebuffer. Otherwise, we'll fall
// back to Pixelflinger on some device (read: Samsung I7500)
int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };
EGLConfig[] configs = new EGLConfig[1];
int[] result = new int[1];
egl.eglChooseConfig(display, attributes, configs, 1, result);
return configs[0];
}
});
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);
}
}
protected boolean isFullscreenOpaque() {
return true;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
SimpleVector sv;
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
sv=new SimpleVector(0,1,0);
cb.setRotationPivot(sv);
cb.rotateZ((float)Math.PI/10);
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
sv=new SimpleVector(0,1,1);
cb.setRotationPivot(sv);
cb.rotateX((float)Math.PI/10);
return true;
}
return false;
}
class MyRenderer implements GLSurfaceView.Renderer {
public MyRenderer() {
}
public Object3D createBox() {
Object3D cube=new Object3D(12);
cube.addTriangle( new SimpleVector( 0, 0, 0 ),
0, 0,
new SimpleVector( 0, 1, 0 ), 0, 1,
new SimpleVector( 1, 1, 0 ), 1, 1);
cube.addTriangle( new SimpleVector( 1, 1, 0 ),
1, 1,
new SimpleVector( 1, 0, 0 ), 1, 0,
new SimpleVector( 0, 0, 0 ), 0, 0);
cube.addTriangle( new SimpleVector( 0, 0, 1 ),
0, 0,
new SimpleVector( 0, 1, 1 ), 0, 1,
new SimpleVector( 0, 1, 0 ), 1, 1);
cube.addTriangle( new SimpleVector( 0, 1, 0 ),
1, 1,
new SimpleVector( 0, 0, 0 ), 1, 0,
new SimpleVector( 0, 0, 1 ), 0, 0);
cube.addTriangle( new SimpleVector( 1, 0, 0 ),
0, 0,
new SimpleVector( 1, 1, 0 ), 0, 1,
new SimpleVector( 1, 1, 1 ), 1, 1);
cube.addTriangle( new SimpleVector( 1, 1, 1 ),
1, 1,
new SimpleVector( 1, 0, 1 ), 1, 0,
new SimpleVector( 1, 0, 0 ), 0, 0);
cube.addTriangle( new SimpleVector( 1, 0, 1 ),
0, 0,
new SimpleVector( 1, 1, 1 ), 0, 1,
new SimpleVector( 0, 1, 1 ), 1, 1);
cube.addTriangle( new SimpleVector( 0, 1, 1 ),
1, 1,
new SimpleVector( 0, 0, 1 ), 1, 0,
new SimpleVector( 1, 0, 1 ), 0, 0);
cube.addTriangle( new SimpleVector( 0, 0, 1 ),
0, 0,
new SimpleVector( 0, 0, 0 ), 0, 1,
new SimpleVector( 1, 0, 0 ), 1, 1);
cube.addTriangle( new SimpleVector( 1, 0, 0 ),
1, 1,
new SimpleVector( 1, 0, 1 ), 1, 0,
new SimpleVector( 0, 0, 1 ), 0, 0);
cube.addTriangle( new SimpleVector( 0, 1, 0 ),
0, 0,
new SimpleVector( 0, 1, 1 ), 0, 1,
new SimpleVector( 1, 1, 1 ), 1, 1 );
cube.addTriangle( new SimpleVector( 1, 1, 1 ),
1, 1,
new SimpleVector( 1, 1, 0 ), 1, 0,
new SimpleVector( 0, 1, 0 ), 0, 0 );
cube.setCenter(new SimpleVector(0,0,0));
return cube;
}
public void buildObj() {
if (master == null) {
world = new World();
cb=createBox();
cb.translate(-1, 0, -1);
cb.setAdditionalColor(c1);
cb.build();
world.addObject(cb);
cb=createBox();
cb.translate(-1, -1, -1);
cb.setAdditionalColor(c2);
cb.build();
world.addObject(cb);
world.getCamera().setPosition(-0.5f, -0.5f, -5);
world.getCamera().lookAt(new SimpleVector(-0.5,-0.5,-0.5));
}
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
if (fb != null) {
fb.dispose();
}
fb = new FrameBuffer(gl, w, h);
buildObj();
world.buildAllObjects();
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
public void onDrawFrame(GL10 gl) {
fb.clear(back);
world.renderScene(fb);
world.draw(fb);
fb.display();
}
}
}