View Full Version : Best Practice, matrix tracking or own matrix code

03-20-2008, 08:48 AM

I'm in the process of constructing a 3d engine of sorts and need to be able to track the ModelView matrix for each 'entity' that's rendered (for reasons i won't get into), an entity in my case is simply a vertex buffer object that's drawn at a certain location.

At the moment i am going down the route of using my own translation/rotation code to generate a matrix that's sent though glLoadMatrix, which means i can store the current matrix for each entity. I could however simply use glTranslate/Rotate/Scale and use glGet to grab the current matrix at the time of rendering the entity and store it somewhere.

I am wondering which is the 'best practice' for this kind of thing;
the advantage as i see it with my own matrix code is that i get to leave glGet alone (experience has taught me that quick code and glGet don't go hand in hand for obvious reasons) but there is always the very good chance that a lack of understanding of matrix code (from a lack of *good* resources dedicated to matrix code and opengl [although i am struggling by okay]) might lead to small errors that can quickly grow into large hard to track down errors.

Given that there might not be too many entities drawn per frame the glGet slowdown might be worth the 'correctness'

any thoughts?

03-20-2008, 09:57 AM
I recommend you roll your own matrices and leave glGet* alone, for the reasons that you mention above. There are many references out there on how to write simple, yet efficient matrix classes. Any computer graphics book will have a section on rotation/translation/scaling.

Happy coding!


03-20-2008, 10:02 AM
Use your own matrix code. To minimize the numerical error try to avoid error accumulation. For example, when rotating an object, adjust it rotation angle and compute the matrix each frame, instead of changing the matrix each frame (by multiplying with the rotation matrix).

03-20-2008, 06:22 PM
sounds good :) i am getting slight errors with my rotation matrices code but they are ever so slight, i figure if i stick to only creating them when they are needed it'll be okay.. "well it'll be okay" is starting to become the mantra of my code these days.

thanks for the tips :)

03-21-2008, 08:47 AM
every so slight will turn into "what the f*ck is up with my lighting?", and "why isn't it looking straight up anymore?" as the long axis arms of your transform matrices start to sprawl ever more outwards and skewards.

03-23-2008, 08:01 AM
I second using your own matrix generation code, and also I recommand not using the matrix stack at all as it will be deprecated in OGL3.