29

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

qdii
  • 12,505
  • 10
  • 59
  • 116
Alex319
  • 3,818
  • 9
  • 34
  • 40

3 Answers3

61

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.

Kos
  • 70,399
  • 25
  • 169
  • 233
  • So with the matrix stack depreciated, I'm not sure what the new approach should be. In our old code we applied transformations to instanced objects and rendered each from a common function. So it seems like now to get that to work I'd need to update the modelView matrix manually after every transform. What is the new paradigm for doing this? – Nigel May 29 '13 at 18:23
  • 1
    If you want a matrix stack in GL3+, make a `std::vector` and pass the topmost matrix object with `glUniformMatrix4fv`. You can have several of them, and you're able to decide how to use each one in your shaders. – Kos Jun 02 '13 at 18:28
  • @Nigel. _Depreciated_ means a loss of value, usually in monetary terms. _Deprecated_ means obsolete. Two very different terms. Please use wisely :) – Engineer Mar 05 '15 at 17:32
  • 1
    Argh! What is wrong with the Khronos folks? Again and again they show that they do not understand the meaning of "deprecate." It's supposed to be *something you do when you have a better replacement for the functionality in question,* but they repeatedly use it to make things *worse* on us instead, taking things that used to "just work" and dumping it in our laps to re-implement manually, if that's even possible. (I still haven't seen a good replacement for `GL_QUADS`, for example!) – Mason Wheeler Jul 14 '15 at 17:48
11

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); 
ergosys
  • 47,835
  • 5
  • 49
  • 70
2
float modelview[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelview);
Andreas Brinck
  • 51,293
  • 14
  • 84
  • 114