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

Started by
1 comment, last by VladimirBondarev 12 years, 1 month ago
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?
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.
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
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.

This topic is closed to new replies.

Advertisement