PDA

View Full Version : noob question - modelview and projection matrices



newsb
04-23-2012, 03:38 PM
Sorry, but I must know the answer to this and tried to google alot, without finding the exact answer...

Q: Why does this work?


glMatrixMode(GL_PROJECTION)
glLoadIdentity()
aspect = float(width)/float(height)
gluPerspective(40., aspect, 0.1, 40.)

glMatrixMode(GL_MODELVIEW)
gluLookAt(0,0,10, 0,0,0, 0,1,0)

When this doesn't work?


glMatrixMode(GL_PROJECTION)
glLoadIdentity()
aspect = float(width)/float(height)
gluPerspective(40., aspect, 0.1, 40.)

glMatrixMode(GL_MODELVIEW)
gluLookAt(0,0,10, 0,0,0, 0,1,0)

I thought the projection and the modelview matrices have absolutely nothing to do with each other, so why does the order of the operations/matrix stuff matter ???

Why doesn't it give the same result ???

Noob question, I know... But I don't understand this...

Rosario Leonardi
04-23-2012, 03:44 PM
they look like pretty the same to me.

carsten neumann
04-23-2012, 03:49 PM
Huh? I must be blind, but the two look like literally the same code, where is the difference hiding?
BTW, what does "it doesn't work" mean here? That is usually not a very useful problem description (i.e. one that makes a good answer possible), take a look at Forum Posting Guidelines (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=305969&an=2#Pos t305969) for suggestions how to improve it.

Assuming the second code block was meant to look like


glMatrixMode(GL_MODELVIEW)
gluLookAt(0,0,10, 0,0,0, 0,1,0)

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
aspect = float(width)/float(height)
gluPerspective(40., aspect, 0.1, 40.)


I'd then guess the problem is that you leave the projection matrix active, so any calls to glTranslate, glRotate, etc. affect your projection matrix, which is usually not what you want.

Dan Bartlett
04-23-2012, 04:06 PM
Also, you most likely want to reset the model-view matrix to identity before calling gluLookAt:


glMatrixMode(GL_PROJECTION)
glLoadIdentity()
aspect = float(width)/float(height)
gluPerspective(40., aspect, 0.1, 40.)

glMatrixMode(GL_MODELVIEW)
glLoadIdentity(); // <===== add this line
gluLookAt(0,0,10, 0,0,0, 0,1,0)

Since gluLookAt simply multiplies the current matrix by the "look at" matrix, and if you don't reset it, the transformation will stack each time you render.

newsb
04-23-2012, 04:19 PM
ARRGH!

Thanks a lot, both of you! You're both right... Ofcourse it was a really stupid copy/paste error I made, I had to remove a lot of comments so my mind was focused on removing comments instead of showing what I meant to show... And what I meant to show was:


glMatrixMode(GL_PROJECTION)
glLoadIdentity()
aspect = float(width)/float(height)
gluPerspective(40., aspect, 0.1, 40.)

glMatrixMode(GL_MODELVIEW)
gluLookAt(0,0,10, 0,0,0, 0,1,0)

When this doesn't work?


glMatrixMode(GL_MODELVIEW)
gluLookAt(0,0,10, 0,0,0, 0,1,0)

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
aspect = float(width)/float(height)
gluPerspective(40., aspect, 0.1, 40.)


You're both absolutely right:

1) Ofcourse it was stupid of me, not to end with an extra switch to the gl_modelview-matrix. This actually solved my main problem and confirms that I think I understand the difference between the two matrices, thanks!

2) Also thanks a lot for the glLoadIdentity-comment - I've now added that. Makes good sense...

Stupid of me - I actually knew the importance of ending with the modelview matrix, but somehow I've forgot...

Thanks all (especially for quick answers)!