Jump to content
• Advertisement

# OpenGL How can I apply a 3x3 transformation matrix in opengl?

This topic is 2792 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 was looking at examples of being able to represent transformations using a matrix instead of the built in functions. I want to learn more about the low level functions without using opengl's transform functions or matrix functions. I was always confused how this works.

Say I have a matrix class:
class Matrix
{
public:
float data [ 3 ] [ 3 ];

Matrix ( void )
{
int i, j;

for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
data [ i ] [ j ] = 0;
}sa
}
}
};

Example function:

Matrix scale ( Pt p, float alpha )
{
Matrix rvalue;

rvalue.data[0][0] = alpha;
rvalue.data[0][1] = 0;
rvalue.data[0][2] = (1-alpha)*p.x;

rvalue.data[1][0] = 0;
rvalue.data[1][1] = alpha;
rvalue.data[1][2] = (1-alpha)*p.y;

rvalue.data[2][0] = 0;
rvalue.data[2][1] = 0;
rvalue.data[2][2] = 1;

return rvalue;
}

How would I apply those transformations using opengl? I'm not where where to start. Is there a function that cane take a 3x3 matrix and load it in?

#### Share this post

##### Share on other sites
Advertisement
The math behind a scaling function such as is glScalef() is just matrix multiplication. For each vertex (x0, y0, z0) you multiply by some scaling matrix.

 x1 s 0 0 x0 y1 = 0 s 0 * y0 z1 0 0 s z0 

You will have to use your imagination a bit since there isn't an easy way to add equations in this post.

Your code might look something like this:
 x1 = s * x0; y1 = s * y0; z1 = s * z0; 

This might provide some better information on the math: http://en.wikipedia...._%28function%29

I'm not sure how you want to go about doing this in OpenGL without the built in functions. GLM might be useful to you for matrix math. It can be used with OpenGL easily.

#### Share this post

##### Share on other sites
Hidden
openGL uses 4x4 matrix.
First you put your 3x3 matrix into 4x4:

3x3 Identity matrix:

1 0 0
0 1 0
0 0 1

4x4 Identity matrix:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

Create 4x4 from 3x3

3x3 4x4

x x x x x x 0
x x x -> x x x 0
x x x x x x 0
0 0 0 1

after you have 4x4 matrix you set the modelview matrix state like this:

glMatrixMode(GL_MODELVIEW);
glLoadMatrixf( mat4x4Ptr );

I hope it helps

#### Share this post

##### Link to post
openGL uses 4x4 matrix.
First you put your 3x3 matrix into 4x4:

3x3 Identity matrix:
1 0 0
0 1 0
0 0 1

4x4 Identity matrix:
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

Create 4x4 from 3x3

3x3
x x x
x x x
x x x

to

4x4
x x x 0
x x x 0
x x x 0
0 0 0 1

after you have 4x4 matrix you set the modelview matrix state like this:

glMatrixMode(GL_MODELVIEW);
glLoadMatrixf( mat4x4Pointer );

I hope it helps

One more thing: problem is that you want to use 3x3 matrix for openGL, this means that you will be only able to rotate your geometry.
In order to have rotations and translations you will have to use 4x4 matrix. Foxfire92 suggested to use GLM, its not best, but for starters it will do very well.

#### Share this post

##### Share on other sites

• Advertisement
• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• Advertisement

• ### Popular Now

• 9
• 9
• 11
• 15
• 21
• 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!