Search code examples
copenglmatrixmodel-viewprojection-matrix

Correctly Calculating Model and View Matrices for OpenGL Use?


I'm having a bit of trouble with matrices in GL 3.2. How, preferably in matrix notation, do I go about generating a model and view matrix? How do I setup my model/view matrix? I already have a projection matrix; it's defined as:

float right = 800.0f, left = 0.0f;
float top = 0.0f, bottom = 600.0f;
float far = 1.0f, near = -1.0f;

float ortho_mat[16] = {(2.0f / (right - left)), 0.0f, 0.0f, 0.0f,
                     0.0f, (2.0f / (top - bottom)), 0.0f, 0.0f,
                     0.0f, 0.0f, (-2.0f / (far - near)), 0.0f,
                     (-((right + left) / (right - left))),
                     (-((top + bottom) / (top - bottom))),
                     (-((far + near) / (far - near))), 1.0f};

I understand that this orthographic matrix has to be multiplied by the model and view matrices, and those have to be multiplied by the point. How do I setup those matrices?

Edit: I don't mind if they're concatenated into one (modelview).


Solution

  • If you just want to calculate the modelview matrix, you can refer to the reference pages for how it is implemented in GLU. (You can't actually use the GLU library in OpenGL 3, but the reference pages show how the matrix math is implemented internally.)

    For positioning a "camera": http://www.opengl.org/sdk/docs/man/xhtml/gluLookAt.xml

    For doing translations: http://www.opengl.org/sdk/docs/man/xhtml/glTranslate.xml

    For doing rotations: http://www.opengl.org/sdk/docs/man/xhtml/glRotate.xml