Jump to content
  • Advertisement
Sign in to follow this  
doctorsixstring

OpenGL Matrix Creation & Storage

This topic is 4619 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!