PDA

View Full Version : about matrix usage in opengl



zydgyy
05-15-2012, 07:23 AM
Here is my test,


...

// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Save the matrix state and do the rotations
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
float b[16] =
{1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,-300,1};
glMultMatrixf(b);


// Move the light after we draw the sun!
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
// Draw the Earth
glColor3ub(0,0,255);
float c[16] =
{1,0,0,0,
0,1,0,0,
0,0,1,0,
105,0,0,1};
glMultMatrixf(c);
glutSolidSphere(15.0f, 30, 17);

// Restore the matrix state
glPopMatrix(); // Modelview matrix

}
...



and i change the code link this,


...

// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Save the matrix state and do the rotations
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
float b[16] =
{1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,-300,1};
//glMultMatrixf(b);

// Move the light after we draw the sun!
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
// Draw the Earth
glColor3ub(0,0,255);
float c[16] =
{1,0,0,0,
0,1,0,0,
0,0,1,0,
105,0,0,1};
//glMultMatrixf(c);

float product[16];
m3dMatrixMultiply44(product,b,c);
glMultMatrix(product);
glutSolidSphere(15.0f, 30, 17);

// Restore the matrix state
glPopMatrix(); // Modelview matrix
}
...




first i construct two matrix,use glMulMatrix to multiply the first,and then use it again to multiply the sencod.
And the second case is multiply the two matrices and then use glMultiMatrix to mutiply th product,
Do the both of two way get the same result???

BionicBytes
05-15-2012, 08:00 AM
The order of matrix multiplies is very important. In your case you have performed the following in case 1)
M' = (M * B) * C
For case 2)
P= B * C
M' = M * P
which is the same as case 1 - just written differently.
The lighting will be different though, because the call to glLightfv(GL_LIGHT0,GL_POSITION,lightPos) will be affected by the current value of the ModelView matrix.

thokra
05-15-2012, 08:03 AM
If I read correctly it should do the same. Your first way can be mathematically expressed as

M1 = ( A * B ) * C

whereas the second calculation would be

M2 = A * (B * C)

Since matrix multiplication is associative M1 == M2. Be aware that matrix multiplication is generally NOT commutative, thus

M1 * M2 != M2 * M1