glTranslatef is driving me nuts

Started by
4 comments, last by Brother Bob 15 years, 3 months ago
I have an md2 loader (that works perfectly) and I want to add frustum culling to individual objects since not all objects are in the same place. I added a translate portion to the loader so I can translate the object while rendering it, but I don't understand what is happening. The first translatef that I commented will work, but when I comment it out and try the one inside the render function suddenly it doesn't work and nothing shows on the screen, but both use the exact same values. Is there a rule I don't about when using glTranslatef in other functions?

void RenderScene()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   // Clears the screen.
    glLoadIdentity();                   //Reset modelview matrix for new frame.
    
    heading = lookdir; //reset the heading

    //this translate function works
    //glTranslatef(xpos, -15.0f, zpos); //move the world about the camera

    glRotatef(heading, 0.0f, 1.0f, 0.0f);
    
    //render the models
    glRotatef(-90.0, 1.0, 0, 0); //rotate to right side up
    mdl.RenderMD2Objects(0,xpos,0,zpos); ///param: 1st = frame #, 2nd = x, 3rd = y, 4th = z
    
    SwapBuffers(g_HDC); // Display the new frame.
}

//now my md2 render function

bool MD2::RenderMD2Objects(int frame, float x, float y, float z)
{
    //this translate function does NOT work
    //glTranslatef(x, y, z); //translate
    
    //if there is a texture then bind it
    if(mdl.TexID != NO_TEXTURE)
        glBindTexture(GL_TEXTURE_2D,mdl.TexID);
    
    glBegin(GL_TRIANGLES);                                      //.md2 files only support triangles
    
    //draw all faces
    for(int i = 0; i < mdl.nFaces; i++){
        
        //this function checks if the next triangle should be displayed
        if(Display(mdl.v_x[ mdl.fip1 + mdl.nVertices*frame ] + x,
                   mdl.v_y[ mdl.fip1 + mdl.nVertices*frame ] + y,
                   mdl.v_z[ mdl.fip1 + mdl.nVertices*frame ] + z) )
        {
 /*rest of render function...*/
/ Visual Studios 2010 / Codeblocks 10.05 / Windows 7 / Ubuntu 10.10 / - I might be wrong
Advertisement
im betting its because you also rotate
translate ONLY first (comment out all rotated), this should work and be apparant.

then rotate and see what happens in the cases
translate / rotate and
rotate / translate

the effect should be obvious
Quote:Original post by scwizzoIs there a rule I don't about when using glTranslatef in other functions?


Yes. read all openGL scale/translate/rotate funcs in reverse order. You want to rotate, then translate your model. So translate first, and then rotate. That's why the second one goes a bit mad (the translation puts the object in the distance, and it then rotates around the origin which is very likely to put it outside of the fov)
Thanks, I don't know why I wasn't able to see that.
/ Visual Studios 2010 / Codeblocks 10.05 / Windows 7 / Ubuntu 10.10 / - I might be wrong
its called a stack.
the last (rotation, translate or scale) matrix in code is the first executed
Quote:Original post by UnIXbLueStar
its called a stack.
the last (rotation, translate or scale) matrix in code is the first executed

It has nothing to do with stacks. It's the result of how matrix operations are defined, and how you interpret the operations.

If you look at the operations from an outside coordinate system (world space), they are applied in reverse order as written in the code. If you view the operations from the object (object space), they are applied in forward order as written in the code.

Had OpenGL defined matrix multiplications as pre-multiplication instead of post-multiplications, it would have been the other way around.

This topic is closed to new replies.

Advertisement