Are OpenGL matrix stacks obsolete?

I just read this:

"OpenGL support for managing coordinate transformations and projections using standard matrix stacks (GL_MODELVIEW and GL_PROJECTION). However, basically OpenGL 4.0 all functions support matrix stacks has been removed. Therefore, we must provide our own support for conventional transformation and design matrices, and then transfer them into our shaders. "

This is strange, so how do you set up the model and projection matrix now? Should I create them in an opengl application and then multiply the vertices in the vertex shader using matrices?

+4
source share
2 answers

This is strange

Nope. The fixed function has been replaced by a programmable pipeline that allows you to design your conversions the way you want.

Should I create them in an opengl application and then multiply the vertices in the vertex shader using matrices?

If you want to have something that works just like the old pair of OpenGL matrix stacks, then you want your vertex shader to look like, for example:

in vec4 vertexPosition; // ... uniform mat4 ModelView, Projection; void main() { gl_Position = Projection * ModelView * vertexPosition; // ... } 

(Of course you can optimize a bit)

And the corresponding client size code (shown here with C ++ here) will look like this:

 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 assumed 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 as corresponding GLSL types, as well as implementations of functions like gluPerspective .


You can also use OpenGL 1 through the OpenGL compatibility profile, but this is not recommended (you will not use the full potential of OpenGL then).

The OpenGL 3 (and 4) interface is lower than OpenGL 1; If you think that this is too much code, then most likely you are better off working with a rendering engine such as Irrlicht.

+10
source

The matrix stack is part of a fixed function pipeline that is deprecated. You can still access old features over compatibility enhancements, but you should avoid this.

There are some good guides 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.

+4
source

All Articles