Camera/Projection restoration in Android

This commit is contained in:
codeanticode
2011-07-06 23:20:17 +00:00
parent 228572ebb4
commit aa01c085fd
@@ -144,6 +144,8 @@ public class PGraphicsAndroid3D extends PGraphics {
protected float[] pcamera;
protected float[] pcameraInv;
protected float[] pprojection;
protected float[] gltemp;
// PMatrix3D version for use in Processing.
@@ -154,6 +156,12 @@ public class PGraphicsAndroid3D extends PGraphics {
public PMatrix3D camera;
public PMatrix3D cameraInv;
/**
* Marks when changes to the size have occurred, so that the camera
* will be reset in beginDraw().
*/
protected boolean sizeChanged;
protected boolean modelviewUpdated;
protected boolean projectionUpdated;
@@ -546,6 +554,9 @@ public class PGraphicsAndroid3D extends PGraphics {
cameraNear = cameraZ / 10.0f;
cameraFar = cameraZ * 10.0f;
cameraAspect = (float) width / (float) height;
// set this flag so that beginDraw() will do an update to the camera.
sizeChanged = true;
}
@@ -558,6 +569,7 @@ public class PGraphicsAndroid3D extends PGraphics {
glmodelviewInv = new float[16];
pcamera = new float[16];
pcameraInv = new float[16];
pprojection = new float[16];
gltemp = new float[16];
projection = new PMatrix3D();
@@ -1034,13 +1046,24 @@ public class PGraphicsAndroid3D extends PGraphics {
gl.glGetIntegerv(GL11.GL_VIEWPORT, viewport, 0);
gl.glViewport(0, 0, width, height);
// set up the default camera and initializes modelview matrix.
camera();
if (sizeChanged) {
// set up the default camera and initializes modelview matrix.
camera();
// defaults to perspective, if the user has setup up their
// own projection, they'll need to fix it after resize anyway.
// this helps the people who haven't set up their own projection.
perspective();
// defaults to perspective, if the user has setup up their
// own projection, they'll need to fix it after resize anyway.
// this helps the people who haven't set up their own projection.
perspective();
// clear the flag
sizeChanged = false;
} else {
// The pcamera and pprojection arrays, saved when calling camera() and frustrum()
// are set as the current modelview and projection matrices. This is done to
// remove any additional modelview transformation (and less likely, projection
// transformations) applied by the user after setting the camera and/or projection
restoreCamera();
restoreProjection();
}
noLights();
lightFalloff(1, 0, 0);
@@ -4105,11 +4128,27 @@ public class PGraphicsAndroid3D extends PGraphics {
gl.glMatrixMode(GL10.GL_MODELVIEW);
}
if (usingGLMatrixStack) {
projection.set(glmodelview);
projectionStack.set(glprojection);
}
projectionUpdated = true;
}
public void restoreProjection() {
PApplet.arrayCopy(pprojection, glprojection);
copyGLArrayToPMatrix(pprojection, projection);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadMatrixf(glprojection, 0);
if (matrixMode == MODELVIEW) {
gl.glMatrixMode(GL10.GL_MODELVIEW);
}
if (usingGLMatrixStack) {
projectionStack.set(glprojection);
}
projectionUpdated = true;
}
//////////////////////////////////////////////////////////////
// CAMERA
@@ -4514,12 +4553,28 @@ public class PGraphicsAndroid3D extends PGraphics {
modelviewUpdated = true;
calculateModelviewInvNoScaling();
PApplet.arrayCopy(glmodelview, pcamera);
PApplet.arrayCopy(glmodelviewInv, pcameraInv);
copyGLArrayToPMatrix(pcamera, camera);
copyGLArrayToPMatrix(pcameraInv, cameraInv);
}
public void restoreCamera() {
PApplet.arrayCopy(pcamera, glmodelview);
PApplet.arrayCopy(pcameraInv, glmodelviewInv);
copyGLArrayToPMatrix(pcamera, camera);
copyGLArrayToPMatrix(pcameraInv, cameraInv);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadMatrixf(glmodelview, 0);
if (usingGLMatrixStack) {
modelviewStack.set(glmodelview);
}
copyGLArrayToPMatrix(glmodelview, modelview);
modelviewUpdated = true;
}
/**
* Print the current camera matrix.
*/
@@ -4678,6 +4733,8 @@ public class PGraphicsAndroid3D extends PGraphics {
copyGLArrayToPMatrix(glprojection, projection);
projectionUpdated = true;
PApplet.arrayCopy(glprojection, pprojection);
// The matrix mode is always MODELVIEW, because the user will be doing
// geometrical transformations all the time, projection transformations
// only a few times.