Matrix stacks in OpenGL deprecated?

后端 未结 2 1818
走了就别回头了
走了就别回头了 2021-02-06 14:34

I just read this:

\"OpenGL provided support for managing coordinate transformations and projections using the standard matrix stacks (GL_MODELVIEW and GL_PROJECTION). In

2条回答
  •  死守一世寂寞
    2021-02-06 15:27

    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 modelViewStack;
    std::stack 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.

提交回复
热议问题