2
\$\begingroup\$

I'm not an opengl expert, and, as a novice, I prefer to practice a little bit with the old opengl just to be sure to understand correctly the basic concept of computer graphics before deal with shaders and modern opengl (3.x). I don't want to start a flame with this so I'll go through my question. I just know that what I'm using is deprecated.

What I wanto to render is this:

enter image description here

and I'm drawing it using this piece of code:

// draw grid
drawGrid(10, 1);

// draw a teapot
glPushMatrix();
glTranslatef(modelPosition[0], modelPosition[1], modelPosition[2]);
glRotatef(modelAngle[0], 1, 0, 0);
glRotatef(modelAngle[1], 0, 1, 0);
glRotatef(modelAngle[2], 0, 0, 1);
drawAxis(4);
drawTeapot();
glPopMatrix();

Now, I'd like to replace the last glTranslatef and glRotatef with matrixes, and I'm doing in this way:

Matrix4 _matrixModel;
_matrixModel.identity();
_matrixModel.translate(modelPosition[0], modelPosition[1], modelPosition[2]);
_matrixModel.rotateX(modelAngle[0]);
_matrixModel.rotateY(modelAngle[1]);
_matrixModel.rotateZ(modelAngle[2]);

glLoadMatrixf(_matrixModel.getTranspose());

And I don't see anymore the teapot. So I thought that this _matrixModel is not complete because it is only the model matrix and I need a modelview so I have to multiply it with the projection matrix but.. this is what I get:

enter image description here

where am I wrong?

\$\endgroup\$
2
\$\begingroup\$

So I thought that this _matrixModel is not complete because it is only the model matrix and I need a modelview so I have to multiply it with the projection matrix but.

Not quite. You also have to multiply it with the view matrix. The model matrix transforms coordinates from model (or object) space to world space. The view matrix then transforms them from world space to camera (or eye) space. Then the projection matrix is used to transform coordinates in camera space to projection space.

See also this question on stackoverflow. And also the article linked therein.

The view matrix is what for example gluLookAt produces. See here for documentation about that function and what the matrix looks like.

If you have a camera object, that has a model matrix like other objects, the view matrix would be just the inverse of that model matrix.

\$\endgroup\$
2
\$\begingroup\$

Translate after rotation. If you rotate while the object is still in object-space, it will rotate the object around its center.

If you rotate after it's been translated into world-space you're in for a real nasty surprise. More often than not, the object will rotate its way off-screen. In short, if you want to rotate an object around the object's center, then the object has to be centered at the origin.

This is a very common problem, I would suggest you lookup rotation around an arbitrary point for a more thorough understanding.

Also, why are you loading the transpose matrix? Is your matrix math library row-major for some reason? I'd suggest you use glm at that point...

\$\endgroup\$
  • \$\begingroup\$ yes it is row major \$\endgroup\$ – nkint Sep 5 '13 at 8:18
1
\$\begingroup\$

don't omit the pushMatrix and popMatrix and use glMultMatrix instead of glLoadMatrix. Otherwise you throw away all your camera information that was used to draw the grid.

http://www.opengl.org/sdk/docs/man2/xhtml/glMultMatrix.xml

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.