Question

I'm working on an OpenGL application, specifically for Android with OpenGL ES (2.0 I think). I can currently draw objects independently and rotate the scene all at once. I need to be able to translate/rotate individual objects independently and then rotate/translate the whole scene together. How can I accomplish this? I've read several threads explaining how to push/pop matrices but I'm pretty sure this functionality was deprecated along with the fixed function pipeline of OpenGL 1.1.

To give some perspective, below is the onDrawFrame method for my renderer. Field, Background and Track are all classes I've made that encapsulate vertex data and the draw method draws the appropriate matrices to the supplied context, in this case GL10 'gl'.

        //clear the screen
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT |GL10.GL_DEPTH_BUFFER_BIT);
    // TODO Auto-generated method stub

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    GLU.gluLookAt(gl, 0, 0, 60, 0, 0, 0, 0, 2, 0);//setup camera

    //apply rotations
    long time = SystemClock.uptimeMillis();
    float angle =  ((int)time)/150.0f;
    gl.glRotatef(55.0f, -1, 0, 0);//rotates whole scene
    gl.glRotatef(angle, 0, 0, -1);//rotates whole scene

    MainActivity.Background.draw(gl);
    MainActivity.Track.draw(gl);
    MainActivity.Field.draw(gl);

*Update: As it turns out, I can push and pop matrices. Is there anything wrong with the pushing/popping method? It seems to be a very simple way of independently rotating and translating objects which is exactly what I need. *

Was it helpful?

Solution

There should be nothing wrong with using glPushMatrix and glPopMatrix. The Android implementation of ES 2.0, to my understanding, has default shaders which behave exactly like the default fixed-function pipeline, and you can use the fixed-function pipeline functions. They will behave identically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top