Wednesday, August 13, 2008

Matrix in OpenGL


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

4x4 matrix in memory.

[R0 R3 R6 Tx]
[R1 R4 R7 Ty]
[R2 R5 R8 Tz]
[ 0 0 0 1]

'R' represents rotations and scaling (and shearing)
'T' translation


First, I read the matrix and print it.


float x[16];
int i;

//read the 4x4 matrix and store in x
glGetFloatv (GL_MODELVIEW_MATRIX, (float*)x);

//print the matrix
for(i=0; i<4; i++){
printf("%f\t%f\t%f\t%f", x[i], x[i+4], x[i+8], x[i+12]);
printf("\n");
}

glTranslatef(2, 0.0, 0.0);


//print the matrix after translate

glGetFloatv (GL_MODELVIEW_MATRIX, (float*)x);

printf("\n-----------------------\n");

for(i=0; i<4; i++){
printf("%f\t%f\t%f\t%f", x[i], x[i+4], x[i+8], x[i+12]);
printf("\n");
}



Run the program.

After translate our matrix changes in "Tx" -2.000000

-1.000000   0.000000   0.000000   0.000000
0.000000  -0.196116   0.980581   0.000000
0.000000   0.980581   0.196116  -10.198039
0.000000   0.000000   0.000000   1.000000
-----------------------------------------
-1.000000   0.000000   0.000000  -2.000000
0.000000  -0.196116   0.980581   0.000000
0.000000   0.980581   0.196116  -10.198039
0.000000   0.000000   0.000000   1.000000



Instead of translate if we use rotation

glRotatef(45, 0.0, 1.0, 1.0);

After rotation our matrix changes in "R"

-1.000000   0.000000   0.000000   0.000000
0.000000  -0.196116   0.980581   0.000000
0.000000   0.980581   0.196116  -10.198039
0.000000   0.000000   0.000000   1.000000
-----------------------------------------
-0.707107   0.500000  -0.500000   0.000000
-0.588348  -0.023793   0.808257   0.000000
0.392232   0.865699   0.310998  -10.198039
0.000000   0.000000   0.000000   1.000000




Reference: www.gamedev.net Forum

No comments: