PDA

View Full Version : glRotate and glTranslate are messing up my lighting?!?


Jake
2003.10.14, 08:43 PM
I am trying to add a nice little scoreboard with your club info and hole to my game, so I decided to first blend a quad (90% opaque, just enough so you can see the terrain behind it), and the write some text. It turns out because I am blending, I need to draw the frame before I add the quad, so, what I am trying to do is just reverse my translate and rotate statements , so I can position the quad in front of all the polygons. Now everything is working just fine, the quad is in the correct part of the screen, with the correct texture, BUT the lighting is all screwed up (when I look out, its REALLY dark, when I look down its 100% bright). I am really confused, I pinpointed it to these lines...

glTranslatef( -cx, -cy, -cz );
glRotatef(-yrot,0.0f,1.0f,0.0f);
glRotatef(-xrot,1.0f,0.0f,0.0f);

If I comment those out, it works, if I leave them in, it screws up. Heres the rest of the relevant code in my drawRect method ( I deleted my own game code, and just left the drawling code)

- (void) drawRect:(NSRect)rect
{
glClear( GL_DEPTH_BUFFER_BIT );
glLoadIdentity(); // Reset the current modelview matrix

glTranslatef(0.0f,0.0f,-4.0f);
glBindTexture( GL_TEXTURE_2D, 0);
glColor3f( 1.0f,0.2f,0.1f );
glDisable( GL_LIGHTING );
glRasterPos2f( 0 , 0 );
// buffer = [clubs[currentClub].name cString];
[ self glPrint:@"Active OpenGL Text With NeHe - "];
glEnable( GL_LIGHTING );

glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glTranslatef( cx, cy, cz );

[golfer render: -time3 x: -clubx-3 y: -(cluby+0.18) z: -clubz xr: 0.0 yr: -yrot zr: zrot ];

[currentLevel renderLevel];

glTranslatef( -cx, -cy, -cz );
glRotatef(-yrot,0.0f,1.0f,0.0f);
glRotatef(-xrot,1.0f,0.0f,0.0f);

[ [ self openGLContext ] flushBuffer ];

}

Any ideas? I'm stumped.

OneSadCookie
2003.10.14, 08:58 PM
Read the red book... it has a good little section on what order you need to do lighting and transformation calls in to get the expected result...

Jake
2003.10.15, 10:01 PM
After reading through various chapters, I can't find it, do you have an idea of what chapter to look in?

OneSadCookie
2003.10.15, 10:58 PM
Chapter 5 "Lighting"; "Controlling a Light's Position and Direction"; page 191 in my copy...

Jake
2003.10.16, 07:45 PM
Ok, I see, I thought lights were constant, but it turns out they rotate with other polygons. MY light was out of position. Thanks for the help!

MacFiend
2003.10.17, 05:08 PM
glPushAttrib() and glPopAttrib() is your friend.