Reputation: 137
I am working on a camera system in a 3d scene on Android. So far, I used this initialisation code to:
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.i(LOG_TAG, "onSurfaceCreated()");
TextureLibrary.loadTextures(m_context, gl);
//Set initial perspective and viewport
gl.glMatrixMode(GL10.GL_PROJECTION);
float fov = .01f * (float) Math.tan(Math.toRadians(45.0 / 2.0));
float aspectRatio = 1.9541f;
gl.glFrustumf(-fov, fov, -fov / aspectRatio, fov / aspectRatio, 0.01f, 100.0f);
gl.glViewport(0, 0, (int) _viewportWidth, (int) _viewportHeight);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glFrontFace(GL10.GL_CW);
gl.glCullFace(GL10.GL_BACK);
initialise();
}
But now, I would like to update the frustum in the render method, to perform zooms and/or image distortions. However, it seems like the the frustum update.
Here is the render code (trimmed of the some fat, which does not contain any GL State altering code, apart from the usual Push and PopMatrix):
private void render(GL10 gl)
{
//Frustrum update code
gl.glMatrixMode(GL10.GL_PROJECTION);
float fov = .01f * (float) Math.tan(Math.toRadians(45.0 / 2.0));
float aspectRatio = 1.9541f;
gl.glFrustumf(-fov, fov, -fov / aspectRatio, fov / aspectRatio, 0.01f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
gl.glLoadIdentity();
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
//Render all the things here!
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
As you can see, the snippet where the frustum is set is almost identical, and the values used are the same. However, when glFrustum is called in the render method, the program will not show anything being drawn. I find this strange, since the values used are exactly the same (when forcing the aspect ratio).
The viewport is still set/updated in the OnSurfaceChanged method, which is hit at least once.
public void onSurfaceChanged(GL10 gl, int width, int height) {
_viewportWidth = width;
_viewportHeight = height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glViewport(0, 0, (int) _viewportWidth, (int) _viewportHeight);
gl.glMatrixMode(GL10.GL_MODELVIEW);
}
I think this may have something to do with OpenGL States, but I haven't yet found a solution, out of everything I tried.
Upvotes: 1
Views: 170
Reputation: 16582
glFrustum()
concatenates with the current matrix. As you haven't done a glLoadIdentity()
, this means you're going to be multiplying with the previous frustum.
Upvotes: 1