Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Matrix Creation & Storage

Started by doctorsixstring 5 replies 900+ views
Original Post
doctorsixstring
doctorsixstring
I've been playing around with Python and PyOpenGL for the last few weeks. Before that, I used C# and Managed D3D. When using D3D, I would store the transformation of a "chunk" of geometry in a Matrix structure. I could create and modify a Matrix using built-in methods, then apply it to the current scene (using the SetTransform method or a MatrixStack object). In OpenGL, it seems like I can only make changes to the matrix on the top of my stack. Instead of storing each chunk's local transformation in a single Matrix object, I somehow need to have each chunk make the appropriate glTranslate/glRotate/glScale calls to the top matrix in the stack. I could store each chunk's transform in a matrix array, but I would then have to either write my own matrix manipulation methods, or use the matrix stack for building and extracting the matrix. Neither option is particularly appealing. How do most OGL devs handle this?
Falken42
Falken42
You are correct, OpenGL doesn't have a built-in math library.

Either rebuild the matrix you need using glTranslate/glRotate/glScale as you mentioned, or roll your own matrix code. It's not that hard, and it's a good math learning experience -- although I've been doing it since the software rendering days of DOS and VGA... :)

Edit: NumPy looks like it might help out with the vector and matrix side of things if you don't want to write your own code.
Endar
Endar
Quote:
Original post by bpoint
It's not that hard, and it's a good math learning experience ....


If you have the time, definately write it yourself. If you end up going so far as to write functions to calculate the inverse matrix then you'll also get a nice small lesson on floating point accuracy.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
haegarr
haegarr
Quote:
Original post by doctorsixstring
In OpenGL, it seems like I can only make changes to the matrix on the top of my stack. Instead of storing each chunk's local transformation in a single Matrix object, I somehow need to have each chunk make the appropriate glTranslate/glRotate/glScale calls to the top matrix in the stack. I could store each chunk's transform in a matrix array, but I would then have to either write my own matrix manipulation methods, or use the matrix stack for building and extracting the matrix. Neither option is particularly appealing.

I strongly recommend not to use GL's matrix stack for your own matrix operations. First, the matrix stack is often implemented in software, so you have no performance win. Calling GL's matrix stack is not a null cost operation; some peoply here on GDnet reported its cost measureably above invoking a local routine. Furthurmore, the stack could only be used for some operations; e.g. matrix inversion must be done by yourself anyway, and the stack allows multiplication on the right (w.r.t. column vector definition) only.

Quote:
Original post by doctorsixstring
How do most OGL devs handle this?

I've written my own vector/matrix stuff. I suggest you to either look for an existing library (best especially if written for 3D gfx), or else to do it yourself.
Endar
Endar
haegarr: Have you written your own matrix stack, and then just use glLoadMatrix?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
haegarr
haegarr
Quote:
Original post by Endar
haegarr: Have you written your own matrix stack, and then just use glLoadMatrix?

Nope (own matrix stack) and yep (using glLoadMatrix). My geometric package's basics are (C++):
class Vector2; // a 2 component vector
class Vector3; // a 3 component vector (also used for translation)
class Vector4; // a 4 component vector
class Rotation3; // a rotation in 3D space (here a unit quaternion)
class AffineMap3; // an affine map in 3D space (in fact a matrix)
class Frame3; // a co-ordinate frame in 3D space

(Based on these classes there are also Ray, Plane, Transform, ... classes, but these are out of interest for this post, I think.)

The usual vector stuff is implemented by the vector classes, of course. A "stand alone" rotation is given by Rotation3; I'm using a unit quaternion inside that class, but it could be an axis/angle pair or a a matrix as well in principle (however, I personally would not use a matrix at that stage).

Matrix stuff is given only in AffineMap3. However, as the name suggests, the class handles matrix stuff as far as needed by affine transformations only. E.g. it allows to multiply left or right with a translation given by a Vector3, and also to multiply left or right with a rotation given by a Rotation3 object. Scaling is supported basically. The class is able to invert self and to return an inverse (it does that by looking at how it is build, and uses an effective method then, if implemented).

A Frame3 is a co-ordinate frame as is needed by a scene graph supporting forward kinematics. Say, a frame shows a local AffineMap3 and a pointer to the parent frame, and also a global AffineMap3 for efficiency reasons.

Now, if I have the need to push/multiply a matrix into GL, I have a read only access to the fields of AffineMap3. The fields are ordered so that they perfectly reflect the memory layout needed by both OpenGL and D3D, so their content could be used "as is".


If you're interested in specific details, some more explanations and also code could be posted.
doctorsixstring
doctorsixstring
Thanks, guys. Exactly the info I was looking for. I'll probably write my own matrix methods. I'm pretty familiar with the math, since I've been playing around with them for a few years with D3D.

Ratings++ for all

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.