views:

44

answers:

3

Hello,

For my graphics class, our professor wants us to keep track of our current matrix on our own and apply rotations/translations/scaling matrices to it then load it using glMatrixMode(GL_MODELVIEW) and glLoadMatrix(current_matrix). All of this seems fine, but when I actually use it, I keep having an issue:

if I apply a rotation to something, it'll rotate properly, but if I try to rotate the world view around the y-axis, each object will only rotate around their y-axis.

If this was the original image: http://www.glowfoto.com/static_image/17-104501L/1324/jpg/10/2010/img5/glowfoto

This is what I get: Link in comment

This is what I want to get: Link in comment

What I do is, make a copy of my current_matrix. Apply a rotation to the current_matrix (multiply it). Then call glMatrixMode(GL_MODELVIEW) and glLoadMatrix(transformed_matrix). Draw something. Then copy the old matrix back into the current_matrix. Then call glMatrixMode(GL_MODELVIEW) and glLoadMatrix(current_matrix). Any help would be wonderful!

P.S. the point of the assignment is to not use glRotate glScale and other commands.

A: 

Well your biggest issue is that you are, evidently, rotating the right most object and then translating it. What you want to do is translate it THEN rotate it (about the origin). To do this you need to apply the translate and rotate in the opposite order ... Matrix maths is non-commutative.

Goz
actually i think the problem was the fact that i kept using GL_MODELVIEW and not GL_PROJECTION
hassaanm
Well you "could" modify the projection matrix, however, that answer highlights to me that you don't really understand how the transformation pipeline works ....
Goz
He shouldn't touch the projection matrix for the task at hand! Leave it as it's set up for orthogonal or projection transform.
Madman
Thats the point I was trying to make.
Goz
A: 

What I do is, (1) make a copy of my current_matrix. (2) Apply a rotation to the current_matrix (multiply it). Then (3) call glMatrixMode(GL_MODELVIEW) and (4) glLoadMatrix(transformed_matrix). ...

(numbers added by me).

That sounds like with step 4, you're overwriting the result of step 2. After step 2 do you save the result somewhere, e.g. into transformed_matrix?

(5) Draw something. Then (6) copy the old matrix back into the current_matrix. Then (7) call glMatrixMode(GL_MODELVIEW) and (8) glLoadMatrix(current_matrix).

Any matrix modifications that occur after drawing will have no effect on what appears on the screen (unless they affect the following pass through the drawing loop).

Also it's not clear what "old matrix" you're referring to in step 6.

P.S. Here is a good resource on understanding MODELVIEW and PROJECTION matrices in OpenGL: OpenGL Transformation. E.g.:

Note once again, OpenGL performs multiple transformations in reverse order, therefore, viewing transform comes first before modeling transform in your code. And, if you want to rotate then translate an object, put glTranslatef() first then glRotatef().

LarsH
A: 

Verify that the matrix is constructed properly (print it to file or that like), then remember that depending on whether you use post or pre multiply the result is completely different. If you still have problems, show us some code.

Madman