Best Practice, matrix tracking or own matrix code


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?

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!


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).

sounds good :slight_smile: 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 :slight_smile:

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.

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.