Matrix stacks in OpenGL deprecated?

倖福魔咒の 提交于 2019-12-03 03:42:39

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!