Jump to content
  • Advertisement
Sign in to follow this  
scwizzo

glTranslatef is driving me nuts

This topic is 3542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...*/

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!