Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matrix stacks in OpenGL deprecated?

Tags:

opengl

glsl

I just read this:

"OpenGL provided support for managing coordinate transformations and projections using the standard matrix stacks (GL_MODELVIEW and GL_PROJECTION). In core OpenGL 4.0, however, all of the functionality supporting the matrix stacks has been removed. Therefore, it is up to us to provide our own support for the usual transformation and projection matrices, and then to pass them into our shaders."

This is strange, so how do I set the modelview and projection matrices now? I should create them in the opengl app and then multiply the vertices in the vertex shader with the matrices?

like image 559
user1796942 Avatar asked Nov 30 '12 14:11

user1796942


2 Answers

This is strange

Nope. Fixed function was replaced by programmable pipeline that lets you design your transformations however you want.

I should create them in the opengl app and then multiply the vertices in the vertex shader with the matrices?

If you want to have something that would work just like the old OpenGL pair of matrix stacks, then you'd want to make your vertex shader look, for instance, like:

in vec4 vertexPosition; 
// ...

uniform mat4 ModelView, Projection;

void main() {
    gl_Position = Projection * ModelView * vertexPosition;
    // ...
}

(You can optimise that a bit, of course)

And the corresponding client-size code (shown as C++ here) would be like:

std::stack<Matrix4x4> modelViewStack;
std::stack<Matrix4x4> projectionStack;

// Initialize them by
modelViewStack.push(Matrix4x4.Identity());
projectionStack.push(Matrix4x4.Identity());

// glPushMatrix:
stack.push(stack.top());
    // `stack` is either one stack or the other;
    // in old OpenGL you switched the affected stack by glMatrixMode

// glPopMatrix:
stack.pop();

// glTranslate and family:
stack.top().translate(1,0,0);

// And in order to pass the topmost ModelView matrix to a shader program:
GLint modelViewLocation = glGetUniformLocation(aLinkedProgramObject,
                                               "ModelView");
glUniformMatrix4fv(modelViewLocation, 1, false, &modelViewStack.top());

I've assumed here that you have a Matrix4x4 class that supports operations like .translate(). A library like GLM can provide you with client-side implementations of matrices and vectors that behave like corresponding GLSL types, as well as implementations of functions like gluPerspective.


You can also keep using the OpenGL 1 functionality through the OpenGL compatibility profile, but that's not recommended (you won't be using OpenGL's full potential then).

OpenGL 3 (and 4)'s interface is more low level than OpenGL 1; If you consider the above to be too much code, then chances are you're better off with a rendering engine, like Irrlicht.

like image 182
Kos Avatar answered Oct 15 '22 11:10

Kos


The matrix stack is part of the fixed function pipeline which is deprecated. You still can access the old functionality over the compatibility extension but you should avoid to do this.

There are some good tutorials on matrices and cameras but I prefer this one. Send your matrix to the shader and multiply, as you said, the vertices with the matrix.

like image 7
Felix K. Avatar answered Oct 15 '22 11:10

Felix K.