Matrix Math and Vector classes for OpenGL

Hi All…

I am new to Xojo, but have experience in Visual Basic. My forte is C++.

I took my Matrix and Vector classes I use for my OpenGL programs and ported them to be used in Xojo. I have tested them somewhat by creating an STL Viewer program.

The classes included are:
Vector2 (2D Vector Class)
Vector3 (3D Vector Class)
Vector4 (4D Vector Class)
Matrix2 (2x2 Matrix Class)
Matrix3 (3x3 Matrix Class)
Matrix4 (4x4 Matrix Class)
All matrices are row major. (OpenGL uses column-major matrix)

| 0 1 |    | 0 1 2 |    |  0  1  2  3 |
| 2 3 |    | 3 4 5 |    |  4  5  6  7 |
           | 6 7 8 |    |  8  9 10 11 |
                        | 12 13 14 15 |

Here is an example on how to use them with OpenGL.

  Dim cX as Single, cY as Single, cZ as Single
  Dim v As New Vector3(cX,cY,cZ)
  Dim matView As New Matrix4
  Dim matModel As New Matrix4

Matrix4::getTranspose() converts the matrix to Column-Major and places its contents into a MemoryBlock so it can be used in such OpenGL functions as glLoadMatrixf, glMultMatrixf, etc.

They work pretty much how you’d expect them to. They could probably be optimized a bit by the Xojo pros who understand the language better than I do.

Here is a screen cap of my program using them:

Here is a link to the classes if you are interested.

I am open for critiques or optimizations that could make it even better.

If you don’t mind me asking Andrew, how did you load the head model in your screen cap from disk? (e.g. what file format do you use for storing the model vectors?)

I am using the STL File Format for this project. It is the most basic of model formats. Only triangles are stored. No textures, no names, no skeletons, etc…

STL is what they use mainly for 3D Printing since you don’t need any other information but the solid.

Thanks for the info.

Hmmm… You can’t edit posts? I guess I will just add on to it.

Well, I have updated the classes. Optimized them a little bit and made it easier to use. Now you can string together transforms and whatnot:

[code] ’ Get the center point of the current model
Dim cX as Single, cY as Single, cZ as Single
Dim vecModelCenter As New Vector3(cX,cY,cZ)

’ Set up View matrix
’ (Center the model in the middle of the view)
Dim matView As New Matrix4
Call matView.translate(-vecModelCenter.x,-vecModelCenter.y,Self.Zoom)

’ Set up Model matrix
’ (Rotate the model based on model center point)
Dim matModel As New Matrix4
Call matModel. _
rotateAt(Self.RotateX,1,0,0,vecModelCenter.opNegate()). _
rotateAt(Self.RotateY,0,1,0,vecModelCenter.opNegate()). _

’ Create the Model-View matrix and apply it to current transform
Dim matModelView As New Matrix4(matView.opMultiply(matModel))

I am working on porting all my Quaternion and UnitQuaternion classes as well.

Here is a link to the updated classes.

If you want to play with some ideas Andrew, I’ve created a Quaternion class in this tutorial…

X3: Tutorial 12