Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

c++ - Modern OpenGL: VBO, GLM and Matrix Stacks

After searching and reading about Modern OpenGL in order to upgrade my existing project, I'm a bit confused, since my 3D framework based on OpenGL 2.1.

so, as far as I learn...

  • We need to generate our Vertex-Buffer-Objects from vertices, indices, normals, colors, uvs, etc.

  • then we can use GLM for matrix transformation, and we only use VBO to create or manipulate meshes, finally we pass everything into GLSL vertex shader like this...

    glm::mat4 MVP = projection * view * model;
    glUniformMatrix4fv(glGetUniformLocation(shaderProgramID, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); //or &MVP[0][0]
    
    // uniform mat4 MVP;
    // in vec3 Vertex;
    // void main(void)
    // {
    //    gl_Position = MVP * vec4(Vertex, 1.0); //instead of ftransform();
    // }
    

QUESTION: How we do hierarchical transformations without pushMatrix/popMatrix? (or maybe we do hierarchical transformation by using our VBOs, is it possible?)

If not possible, then how to achieve same result as pushMatrix/popMatrix by using GLM and C++ < stack > library?

Lets say I need something like this:

> Set identity
> Translate to X, Y, Z
> Draw Mesh 1
> Rotate 0.5 by X axis
> Draw Mesh 2
> Scale down to 0.1
> Draw Mesh 3
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If your rendering already happens hierarchically using, for example, function recursion then you already have matrix stack!

void renderMesh(Matrix transform, Mesh mesh)
{
    // here call glDrawElements/glDrawArrays and send transform matrix to MVP uniform
    mesh->draw(transform);

    // now render all the sub-meshes, then will be transformed relative to current mesh
    for (int i=0; i<mesh->subMeshCount(); i++)
    {
        Matrix subMeshTransform = mesh->getSubMeshTransform(i);
        Mesh subMesh = mesh->getSubMesh();

        renderMesh(subMeshTransform * transform, subMesh);
    }
}

// somwhere in main function
...
Matrix projection = Matrix::perspective(...);
Matrix view = camera->getViewMatrix();

Matrix transform = view * projectIon;
renderMesh(transform, rootMesh);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...