# OpenGL 3x3 matrix to 4x4 matrix

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

## Recommended Posts

Hi, in a software 3D engine I do the following with 3D points p: First I scale it, by multiplying the x, y and z position of it with scale.x, scale.y and scale.z Then I rotate it around an axis by using p = rotateAroundArbitrary(p, axis_through_origin, angle) Then I do a translation by adding a position to p Then I do another translation by subtracting the camera position from it Then I multiply it with a 3x3 matrix (the camera matrix, this can be any arbitrary matrix, including one that's skewed or has undergone other weird transformations). Now, I want to let all this happen not by my own software Matrix and Camera class, but I want to generate a GL_MODELVIEW matrix of OpenGL that will let the hardware do all this instead. This GL_MODELVIEW matrix should include ALL the transformations I described above. I'm not entirely sure how to do this though, because I don't know exactly what things in OpenGL have to be inverted and which not, and I'm also not entirely sure how to go from my 3x3 matrix + position vector representation to a 4x4 matrix. And the multiplication with the 3x3 matrix ain't gonna work if the MODELVIEW matrix is a 4x4 one, so how to let it have the same effect? How do I do this?

##### Share on other sites
Quote:
 Hi,in a software 3D engine I do the following with 3D points p:First I scale it, by multiplying the x, y and z position of it with scale.x, scale.y and scale.zThen I rotate it around an axis by using p = rotateAroundArbitrary(p, axis_through_origin, angle)Then I do a translation by adding a position to pThen I do another translation by subtracting the camera position from itThen I multiply it with a 3x3 matrix (the camera matrix, this can be any arbitrary matrix, including one that's skewed or has undergone other weird transformations).Now, I want to let all this happen not by my own software Matrix and Camera class, but I want to generate a GL_MODELVIEW matrix of OpenGL that will let the hardware do all this instead. This GL_MODELVIEW matrix should include ALL the transformations I described above. I'm not entirely sure how to do this though, because I don't know exactly what things in OpenGL have to be inverted and which not, and I'm also not entirely sure how to go from my 3x3 matrix + position vector representation to a 4x4 matrix. And the multiplication with the 3x3 matrix ain't gonna work if the MODELVIEW matrix is a 4x4 one, so how to let it have the same effect?How do I do this?
We'll start by making a list of the transforms in order of application:

1. Scale
2. Axis-angle rotation
3. Translate by p
4. Translate by -cam.pos
5. Apply linear camera transform

Then translate them to OpenGL code:

1. Scale

glScale*(x, y, z);

2. Axis-angle rotation

glRotate*(angle, axis.x, axis.y, axis.z);

3. Translate by p

glTranslate*(p.x, p.y, p.z);

4. Translate by -cam.pos

glTranslate*(-cam.pos.x, -cam.pos.y, -cam.pos.z);

5. Apply linear camera transform

Here we'll have to load your matrix into an array and upload it to OpenGL. We must consider that OpenGL uses column vectors and column-major matrices. We'll assume your matrix uses column vectors; otherwise, you'll need to transpose the following example:

float m[16];

m[0] = m11;
m[4] = m12;
m[8] = m13;
m[12] = 0.0f;

m[1] = m21;
m[5] = m22;
m[9] = m23;
m[13] = 0.0f;

m[2] = m31;
m[6] = m32;
m[10] = m33;
m[14] = 0.0f;

m[3] = 0.0f;
m[7] = 0.0f;
m[11] = 0.0f;
m[15] = 1.0f;

glMatrixMode(GL_MODELVIEW);
glMultMatrixf(m);

Finally, due to the use of column vectors this will appear in reverse order in your code. In summary:

ApplyCameraMatrix(cam.matrix); // Just to avoid writing it all out again
glTranslate*(-cam.pos.x, -cam.pos.y, -cam.pos.z);
glTranslate*(p.x, p.y, p.z);
glRotate*(angle, axis.x, axis.y, axis.z);
glScale*(x, y, z);

And unless I made a mistake, that should do it. Let me know if you have any other questions.

##### Share on other sites
Hey, it's working except that I think the 4x4 matrix has to be inverted, because everything happens in reverse: when I go forward, the terrain goes backwards, when I look left, it goes to the right, and so on. When I roll the camera it rolls the correct way tho, and strafing left and right works too, weird that these do work. I'll now try if it works if I do all the transformations in the inverse way, instead of inverting the 4x4 matrix (because I only have an inverse 3x3 matrix function and I choose the easiest solution :))

Thanks for the help!

EDIT: now I did everything the opposite way, and now moving forward and backwards work fine, but now rolling and moving left and right are working the opposite way. I guess I'll have to use trial and error to find out which things must be inverted and which not :)

EDIT2: it works now! With your code except the transpose of the matrix, even though I thought I had column vectors.

[Edited by - Lode on August 28, 2005 1:59:04 PM]

##### Share on other sites
Hm. There are several reasons your rotations and/or movement could be reversed, but I couldn't tell you for sure what the problem was without knowing more about the simulation. Remember that the camera linear transform needs to be inverted before loading it into OpenGL (it sounds like you're already doing that), and that if you use row vectors you'll need to transpose as well.

Also, keep OpenGL's right-handed coordinate system and rotations in mind. It may just be that you're using the wrong sign in some cases when rotating or moving.

Also, at some point you have to take into consideration the fact that OpenGL is -z forward in camera space. Depending on how you're setting things up, that could also be throwing things off.

Sorry I can't give you a definite solution...

##### Share on other sites
It is likely that you simply need to reverse the order of your transformations. OpenGL functions operate on the local coordinate system, not any particular object or set of vertices. Generally, you will see in an OpenGL program that the view transformation is first -- that was my clue.

In math terms, the matrices are applied to the vertexes in the reverse order that they appear in the code. For example, the following code ...
    glTranslate( T1 );    glRotate( R );    glTranslate( T2 );    glScale( S );
... results in this:
    v' = T1RT2Sv

##### Share on other sites
Quote:
 EDIT2: it works now! With your code except the transpose of the matrix, even though I thought I had column vectors.
Good :-) I'm always wary, though, when a random transpose fixes a problem, 'cause that means you could now have two errors that are canceling each other out. If you're so inclined, you might show us how your 3x3 camera matrix is constructed, as that would probably eliminate any ambiguity.

@John: I'm pretty sure he has the operations in the right order for OpenGL, as we discussed that earlier in the thread.

##### Share on other sites
I think I can clear this up, the 3x3 camera matrix is already inverted after being constructed out of column vectors, which is why in my code the result has to be transposed compared to your code.

It looks like this in my code:

    m[0] = cam.getInvMatrix().a;    m[1] = cam.getInvMatrix().d;    m[2] = -cam.getInvMatrix().g;    m[3] = 0.0f;        m[4] = cam.getInvMatrix().b;    m[5] = cam.getInvMatrix().e;    m[6] = -cam.getInvMatrix().h;    m[7] = 0.0f;        m[8] = cam.getInvMatrix().c;    m[9] = cam.getInvMatrix().f;    m[10] = -cam.getInvMatrix().i;    m[11] = 0.0f;        m[12] = 0.0f;    m[13] = 0.0f;    m[14] = 0.0f;    m[15] = 1.0f;

##### Share on other sites
It could be because forward in OpenGL is normally down the negative z direction. So adding to the z would be going backwards. But if you fixed it, it doesn't really matter lol.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 45
• 22
• 27
• ### Forum Statistics

• Total Topics
634044
• Total Posts
3015211
×