When should one use the matrix stack functions?

Started by manji, October 17, 2010, 02:10:42

Previous topic - Next topic

manji

I understand how glPushMatrix() and glPopMatrix() work but I am not sure when it is best to use them. I have read that you should use them when you have hierarchical translations. But at which point is it usually more efficient to use these functions instead of just translating back manually? For example could one say for sure which is more efficient? :
a)
glTranslatef(x, y, z);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
render();
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
glTranslatef(-x, -y, -z);


b)
glPushmatrix();
glTranslatef(x, y, z);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
render();
glPopMatrix();
toyWars game blog: http://toywars.wordpress.com/

Rene

The first code example is a bad idea because roundoff errors can cause the matrix from before running this code to differ from the matrix after running the code.

Hierarchical translations, or to be more precise hierarchical transformations, are useful then you want to 'attach' a certain object to another object. An example is the door of a car. You want to move the door with the car, but you should also be able to 'add a rotation' if you want to open it. In this case you'll push the car transformation, add the rotation, render the door, and finally pop the matrix to continue drawing other objects of the car.

My advice is to forget the matrix stack altogether. If you're working with shaders you should pass the matrices as uniforms, if you're still working with the fixed function pipeline use LoadMatrix. In both cases you'll need a math library to do the calculations for you, I think LWJGL's math library should suit your needs. You can also roll your own, you'll learn a lot from doing so.

Why do it this way? The most important reason is because it will be easier in the long run, for example when you need to calculate physics, intersections, transform bounding boxes/spheres, etc. etc. It can also be useful now because it will improve performance. Calling native functions from Java is expensive.
When I am king, they shall not have bread and shelter only, but also teachings out of books, for a full belly is little worth where the mind is starved - Mark Twain

manji

Rene, thank you very much for your detailed answer. Up to now I have been only using these opengl functions to make my transformations. I do not work with shaders, so I guess I should go with the manual matrix transformation way.

Do you know any good tutorial or code on this? And when you say LWJGL math library, what are you referring to?
toyWars game blog: http://toywars.wordpress.com/

ryanm

On the efficiency front, popMatrix will just be shifting one pointer while applying an inverse transform will take a full matrix multiply.

Rene

Quote
And when you say LWJGL math library, what are you referring to?
Add the lwjgl_util jar in your classpath. The math classes can be found in the package org.lwjgl.util.vector. I've never used them myself, but I think they can do everything you need.

Quote
Do you know any good tutorial or code on this?
Hmm, not sure. Google around a bit for linear transformations. To start you can take a look at http://en.wikipedia.org/wiki/Transformation_matrix.

If you have any specific questions about certain calculations just ask and I'll do my best to answer them.

EDIT:

I've learned most of the 3d related math I know from books. Most books concerning computer graphics contain a section about transformations. The first edition of the OpenGL Red Book is available online, take a look at appendix G:
http://fly.cc.fer.hr/~unreal/theredbook/appendixg.htm

Math may seem boring at first, but once you get the hang of it it can actually be quite fun  ;)
When I am king, they shall not have bread and shelter only, but also teachings out of books, for a full belly is little worth where the mind is starved - Mark Twain

manji

Thank you, I guess I have study to do. Cheers!
toyWars game blog: http://toywars.wordpress.com/