Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the modelview and projection matrices in OpenGL?

Tags:

opengl

glsl

I am trying to use the OpenGL Shading Language (GLSL) version 1.5 to make vertex and geometry shaders.

I have learned that in GLSL version 1.5, the built-in variables like gl_ModelViewProjectionMatrix are deprecated so you have to pass them in manually. If I have already set the modelview and projection matrices (using gluLookAt and gluPerspective for example) then how do I get the matrices to pass into the vertex and geometry shaders? I've done some searching and some sites seem to mention a function glGetMatrix(), but I can't find that function in any official documentation, and it doesn't seem to exist in the implementation I am using (I get a compilation error unknown identifier: glGetMatrix when I try to compile it with that function).

like image 966
Alex319 Avatar asked Nov 17 '10 07:11

Alex319


People also ask

How do you find a projection matrix matrix?

A square matrix P is called an orthogonal projector (or projection matrix) if it is both idempotent and symmetric, that is, P2 = P and P′ = P (Rao and Yanai, 1979). For a given matrix X of order n × p (n ≥ p) where X′X is nonsingular, let PX = X(X′X)−1X′ and QX = I − PX.

What is ModelView OpenGL?

The ModelView matrix contains both modelling and viewing transformations that place the viewer at the origin with the view direction aligned with the negative Z axis. Clip Coordinates result from transforming Eye Coordinates by the Projection matrix.

What is projection matrix in OpenGL?

In all OpenGL books and references, the perspective projection matrix used in OpenGL is defined as: [2nr−l0r+lr−l002nt−bt+bt−b000−f+nf−n−2fnf−n00−10] What similarities does this matrix have with the matrix we studied in the previous chapter?

What is the ModelView matrix?

ModelView matrix is the concatenation of Model matrix and View Matrix. View Matrix defines the position(location and orientation) of the camera, while model matrix defines the frame's position of the primitives you are going to draw.


3 Answers

Hey, let's slow down a bit here :) Yes, that's true that you receive the matrix by glGetFloatv(GL_MODELVIEW_MATRIX, ptr)... But that's definitely not the thing you should do here!

Let me explain:

In GLSL, built-in variables like gl_ModelViewProjectionMatrix or functions like ftransform() are deprecated - that's right, but that's only because the whole matrix stack is deprecated in GL 3.x and you're supposed to use your own matrix stack (or use any other solution, a matrix stack is helpful but isn't obligatory!).

If you're still using the matrix stack, then you're relying on functionality from OpenGL 2.x or 1.x. That's okay since all of this is still supported on modern graphics cards because of the GL compatibility profile - it's good to switch to a new GL version, but you can stay with this for now.

But if you are using an older version of OpenGL (with matrix stack), also use an older version of GLSL. Try 1.2, because higher versions (including your 1.5) are designed to be compatible with OpenGL3, where things such as projection or modelview matrices no longer exist in OpenGL and are expected to be passed explicitly as custom, user-defined uniform variables if needed.

The correspondence between OpenGL and GLSL versions used to be a bit tricky (before they cleaned up the version numbering to match), but it should be more or less similar to:

GL    GLSL

4.1 - 4.1
4.0 - 4.0
3.3 - 3.3
3.2 - 1.5
3.1 - 1.4
3.0 - 1.3
2.x and lower - 1.2 and lower

So, long story short - the shader builtin uniforms are deprecated because the corresponding functionality in OpenGL is also deprecated; either go for a higher version of OpenGL or a lower version of GLSL.

like image 64
Kos Avatar answered Nov 09 '22 16:11

Kos


To get either matrix you use the constants GL_MODELVIEW_MATRIX or GL_PROJECTION_MATRIX with glGetxxxx:

GLfloat model[16]; 
glGetFloatv(GL_MODELVIEW_MATRIX, model); 
like image 33
ergosys Avatar answered Nov 09 '22 15:11

ergosys


float modelview[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelview);
like image 23
Andreas Brinck Avatar answered Nov 09 '22 16:11

Andreas Brinck