PDA

View Full Version : newbie lighting/matrice question



07-08-2002, 03:28 AM
hi,
I recently started tyring to learn OpenGL but i think I am a bit confused about the push and pop matrix commands. I want to turn an object that I have drawn, but when I use the glrotate command the light source turns with the object. I want to turn the object independantly of the light source. Am I right in thinking that I should be able to do this with appropriate use of the pushMatrix and popMatrix commands.

I hate to admit this - but I don't program in C or C++ and i'm using GL4Java (www.jausoft.com/products/gl4java). The code I'm using to draw the display looks like this -

//Clear The Screen And The Depth Buffer
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Reset The View
gl.glLoadIdentity();
//Move into the screen
gl.glTranslatef(0.0f, 0.0f, -8.0f);
//rotate on x-axis
gl.glRotatef(35,1,0,0);
//position light
gl.glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);

gl.glPushMatrix();
//rotate on y-axis
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
//draw lumpy bits
gl.glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
gl.glBegin(GL_QUADS);
// i draw the object in here
gl.glEnd();
gl.glPopMatrix();

yrot += 0.4f;
//Swap buffers
glj.gljSwap();
//glj.gljCheckGL();
glj.gljFree();

Can something like this do what I want or have I got the wrong idea about all this?

thanks,
colin

Jambolo
07-08-2002, 06:49 PM
Push and pop are just save and undo. Since transformations are cumulative, you need to be able to save and undo them.

So, do this each frame

LoadIdentity
Position the camera
Push
Position the light
Pop
For each object
....Push
....Position the object
....Draw the object
....Pop
Swap buffers

07-09-2002, 03:14 AM
i've changed the display method so it has the structure you suggested - the light source still turns with the object - i'm looking for help from the gl4java mailing list,

thanks for your help,
colin