PDA

View Full Version : glLoadMatrix and normals



IanDJ
07-28-2010, 01:50 AM
Hi all,

I am trying to use glLoadMatrix to load the matrix which would do the same job as a glTranslate*() and glRotate*(). So I created 3 4x4 matrices. One for translation, one for rotation, and one for the product of the first two. Once implemented, and with the angle being incremented a little each frame, I have a triangle that spins, just as it does using glTranslate*() and glRotate*(). However, with the glTranslate*() and glRotate*() code, the triangle gets darker as it turns away from the front gradually. With my own matrix doing the translation and rotation, the darkening and lightening of the triangle appears to be wrong. It doesn't seem to change for much of the rotation, then suddenly it changes a lot.

Here is a snippet:

glLoadMatrixf(*rotation);
glNormal3f(0.0, 0.0, 1.0);
glLoadMatrixf(*transformation);
glColor3f (1.0f, 0.85f, 0.35f);

glBegin(GL_TRIANGLES);
{
glVertex3f( 0.0, 0.6, 0.0f);
glVertex3f( -0.2, -0.3, 0.0f);
glVertex3f( 0.2, -0.3, 0.0f);
}

And it looks much the same, if I have 'glNormal3f(0.0, 0.0, 1.0);' inside the glBegin() call and don't load the rotation matrix by itself.

Is it that to transform normals, you use the inverse transpose of the matrix? The inverse of a 4x4 matrix looks a bit complicated.

Or is there another simple answer as to why the normals aren't behaving correctly?

Thanks,

Ian.

Rosario Leonardi
07-28-2010, 04:55 AM
check the matrix, probably the result matrix is not an orthogonal matrix.

Also:
the matrix is applied to the whole object when you render the object, not when you specify the vertex attribute. So multiple consecutive LoadMatrix are not effective.

ps: where is glEnd()?

DmitryM
07-28-2010, 05:08 AM
If you pass your own matrix to GL as ModelView matrix, then it does inverse+transpose (only on the 3x3 sub-matrix) on it's own.

If you transform vertices on your own, you should transform normals as well, of course. In general case, you need the inversed-transposed 3x3 sub-matrix of the vertex matrix.

If your transformations are rotations + translations only, then the 3x3 ModelView sub-matrix is ortho-normal, and it equals the Normal matrix (inverse = transpose, so no need for calculations).

IanDJ
07-29-2010, 12:23 AM
Awesome, thanks guys. That's a big help.


check the matrix, probably the result matrix is not an orthogonal matrix.

Right this is probably it. I assumed it was OK, cause the vertices were transformed properly. I'll check it.


ps: where is glEnd()?

Ah - just missing from the copy and paste. I wasn't paying attention there.


If you pass your own matrix to GL as ModelView matrix, then it does inverse+transpose (only on the 3x3 sub-matrix) on it's own.

Thank you very much. This is what I should have asked in the first place.

Thanks,

Ian.