Question

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).

Was it helpful?

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

OTHER TIPS

I suggest that you use the library OpenGL Mathematics instead of implementing everything yourself. OpenGL Mathematics contains all the math you need for OpenGL - and the syntax is inspired from GLSL. The library is found here:

http://glm.g-truc.net/

Alternative you could implement your own math library. In that case I suggest you take a look at the Angels math library (from the book "Interactive Computer Graphics: A Top-Down Approach with Shader-Based OpenGL, 6/E"). This library is very simple to understand:

http://www.cs.unm.edu/~angel/BOOK/INTERACTIVE_COMPUTER_GRAPHICS/SIXTH_EDITION/CODE/include/

(look in the mat.h and vec.h)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top