0

I have the following code:

    void setupCamera() {

    // Clear the screen buffer
    glClear(GL_COLOR_BUFFER_BIT);

    // reset the projection matrix //
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // and set the perspective with the current field of view,
    // and the current height and width
    gluPerspective(fov, ratio, objDepth - objRadius * 2,
            objDepth + objRadius * 2);
    glViewport(0, 0, WINDOW_SIZE, WINDOW_SIZE);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    if(first){
        glMultMatrixf(rotMatrix);

    }
    first = true;

    //translate everything to be in objDepth//
    glTranslatef(initX * 0.01, initY * 0.01, (-1) * objDepth);

    glRotatef(200 * angle, rotationVector[0], rotationVector[1],
            rotationVector[2]);

    glutWireTeapot(1.5);


    glGetFloatv (GL_MODELVIEW_MATRIX, rotMatrix);


    }

The rotation vector holds the rotation axis,

translate is used for moving all to the correct position.

the thing is, I am using glMultMatrixf in order to use the last matrix saved,

do rotation and then translation, and then save the matrix again with glGetFloatv.

this function is constantly called with timer, but for some reason

I can't figure out, the matrix wont save anything and always be initialized over and over,

meaning rotation is always used with small angles(because matrix isn't saved).

the saved matrix is not being used anywhere else in the code.

Any idea's?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Itzik984
  • 15,968
  • 28
  • 69
  • 107
  • Wouldn't you want to use glLoadMatrix rather than multmatrix? Also, show how you construct the GLfloat array. Also why are you doing the getFloat and such? why aren't you using a math library to handle matrix multiplication? Pushing and getting things from OpenGL like this is going to cause slow down. Or even using GLpush/pop. – zero298 Nov 24 '12 at 23:42
  • it is being initialized for the first time after using glGetFloatv, and afterwards the `first` boolean variable is `true` and can load and multiply the matrix – Itzik984 Nov 24 '12 at 23:58

1 Answers1

0

Are you calling setupCamera() every tick? If you are, you may want to stop. You don't need to reset your projection matrix every frame and unless you NEED to, you don't need to reset your model view matrix either. Consider incrementing angle and your translation by itself rather than storing the whole matrix.

The steps you might want to take are:

1) Init matrices. Initialize projection to identity and use gluPerspective. Then switch to modelview and initialize it to identity as well. This should really only be done once.

2) In your update loop, increment your angle and translation on their representative variables.

3) In your draw loop, use glPushMatrix() to push the modelview matrix. Apply your matrix changes with gltranslate and glrotate then draw whatever it is you're drawing. After that, glPopMatrix() in order to return to identity.

I still don't really understand if you're trying to draw something and think that you need to read and store and restore the current glMatrix every frame or if you are actually trying to evaluate what OpenGL does to matrices and really DO need to read it out.

zero298
  • 25,467
  • 10
  • 75
  • 100
  • Thanks for the answer zero298, i agree with your comments, but still, i dont want to sum the angle all the time because it might change to a different one. i am trying to draw something (edited in the code),but i dont understand how to save the modelview matrix im working on, and then do all the transformations needed – Itzik984 Nov 25 '12 at 00:34