Archived

This topic is now archived and is closed to further replies.

Software3d ---?---> OpenGl

This topic is 5749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone! In "Software 3d" there was a camera marix containing stuff for cam space,object matrices for every object... Wich matrices in OpenGl stand for these matrices?

Share this post


Link to post
Share on other sites
GL basically deals with two matrices: the modelview matrix and the projection matrix. You can ensure you are in modelmatrix mode, not projection matrix mode, by using
glMatrixMode(GL_MODELVIEW_MATRIX);

Often the projection matrix is used only once when converting from 3D to screen coordinates, the modelview matrix, however, is used repeatedly to describe 3D transformations of both objects and cameras Whenever a transformation command is sent to GL it affects the modelview matrix. glTranslate, glRotate and glScale all carry out operations on this matrix. When a draw command such as glTriangle is sent to GL the object is then drawn using the parameters of the modelview matrix. You can load your own matrix into the modelview matrix as long as it is in the same format as gl''s matrices i.e. an array of 16 floats (float m[16]) using the command
glLoadMatrixf(m);

Similarly you can download the current modelview matrix into your own array using
glGetFloatv(GL_MODELVIEW_MATRIX, m);

You can also save a copy of the current modelview matrix in gl using
glPushMatrix();

This makes a copy of the current matrix and stores it in a stack. Temporary operations can then be carried out on the modelview matrix and the copy can be restored to the current modelview matrix using
glPopMatrix();

When this is done the current modelview matrix is overwritten with the restored matrix. So to demonstrate quickly let us imagine you are storing your matrices as GL does and you want to make a translate function for your own matrix class:

class matrix {

Translate(float x, float y, float z);

float m[16];
};

matrix::Translate {
glPushMatrix();
glLoadMatrixf(m);
glTranslatef(x,y,z);
glGetFloatv(GL_MODELVIEW_MATRIX, m);
glPopMatrix();
}

Pushing the current matrix to the stack at the start and Popping it at the end ensures that when the function is called the current state of GL will remain unchanged. Our matrix is loaded into GL, translated by GL and then save back into our matrix structure. Elegant in its simplicity is it not? Similar functions can be drawn up for rotate and scale functions. Not only is this easier than writing all your own matrix maths routines but in my case at least it is almost certainly faster as GL routines are coded by experienced professionals and sometimes use assembler for speed boosts. I would probably do it this way even if it was slower just for simplicity. Anyway I hope that is clear enough. For more information on this sort of thing download the red book pdf - link available on this site.

_

Geocyte Has Committed Suicide.

Share this post


Link to post
Share on other sites