PDA

View Full Version : OpenGL camera upside down problem



deshan
10-14-2011, 04:53 AM
Hi
I am writing a opengl camera and I have a problem with this.

I move my camera using the simple function glTranslatef.
The problem is I have to give minus values for the upvalue to move the camera up.
For example.


glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f, (float)width/(float)height, 0.1f, 5000.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// camera move
glTranslatef(x, y, z);

// rest of the drawing code comes here
// the transformation of all the game objects calculate using glMultMatrixf function


so if I give y as a minus value(-10) camera moves up and plus value (+10) camera moves down.
I do not know why.

Could you please assist me?

Thank you

Dan Bartlett
10-14-2011, 07:28 AM
You need to put the inverse of the camera transform onto the ModelView matrix stack before rendering. Since you have put glTranslatef(x,y,z); that is as though your camera is a (-x, -y, -z). You can use gluLookAt (or equivalent) to easily set up the inverse camera transformation.

http://www.opengl.org/resources/faq/technical/viewing.htm
http://www.opengl.org/wiki/GluLookAt_code

zeoverlord
10-14-2011, 08:44 AM
so if I give y as a minus value(-10) camera moves up and plus value (+10) camera moves down.
I do not know why.

The reason is that the "camera" in openGL is always at (0,0,0) and glTranslatef is actually moving the world around it.

So if you want to move the camera up relative to the world you must move everything else down.

deshan
10-14-2011, 09:55 AM
Understood.
Thought that was a wrong way.
Now I can undrstand wht really a camera is.
Thank you very much both of you.