Frustum culling in Terrain rendering


Two friends and myself are working on a project, simulating terrain in 3D with possibilities of ‘flying’ around in the terrain.

I am trying to implement frustum culling into our engine, but I am having some problems with getting my hands on the Modelview- and Projection matrix.

// Get The Current PROJECTION Matrix From OpenGL
   glGetFloatv( GL_PROJECTION_MATRIX, proj );

// Get The Current MODELVIEW Matrix From OpenGL
   glGetFloatv( GL_MODELVIEW_MATRIX, modl ); 

As far as I have heard, “glGetFloatv” haven’t been implemented yet. Though, I have found a ‘work-around’, using “” and “MatrixTrackingGL” to get a hand on the modelview- and projectionmatrix, but I still don’t get it to work properly. It seems everytime i call ‘extractFrustum()’ the view seems to change drastically. It looks like it moves “into” the terrain, as the whole screen turns green/brown/blue depending where in the terrain I am looking. It looks like it happens when i use the following calls:

/* Get the current PROJECTION matrix from OpenGL */   
	    getMatrix(gl, GL10.GL_MODELVIEW, modl);
	   /* Get the current MODELVIEW matrix from OpenGL */
	    getMatrix(gl, GL10.GL_PROJECTION, proj);

This method looks like this:

private void getMatrix(GL10 gl, int mode, float[] mat) {
        MatrixTrackingGL gl2 = (MatrixTrackingGL) gl;
        gl2.getMatrix(mat, 0);      

I would really appreciate some help with this.
Many thanks in advance.

I suppose you are talking about OpenGL ES on Android? Please always mention the platform you are using.

It appears that the getMatrix method sets the GL matrix mode, but does not reset it to the previous state after obtaining the matrix. Thus subsequent matrix updates in GL might affect the wrong GL matrix stack.

Yes, OpenGL ES on Android. Sorry for not mentioning that.

I have noticed that, yes. That the method sets the matrix mode, but if that’s the problem, how can I fix it?
Can i simply just call gl.glLoadIdentity() ?

You need to save the current matrix mode in a local variable, then restore it at the end.

private void getMatrix(GL10 gl, int mode, float[] mat) {
        MatrixTrackingGL gl2 = (MatrixTrackingGL) gl;
        IntBuffer matrixMode = IntBuffer.allocate(1);
        gl2.glGetIntegerv(GL11.GL_MATRIX_MODE, matrixMode);
        gl2.getMatrix(mat, 0);     

Thanks. I will try it tomorrow when we’re back at work. Although, we got to test on the new HTC Desire today, and the calls I didn’t think were implemented actually worked, so I got that part working after all. We have tested on a HTC Magic till now, so I guess Desire fully supports OpenGL ES 1.1.

But thanks for fast replies, I’m sure that way would have done the job as well.

I will come back to you if I meet further problems :slight_smile: