0

|实际上,我正在尝试在屏幕上同时显示两个或多个模型。我分别为顶点、颜色、法线生成三个不同的顶点缓冲区,这里是主要代码:

    //==================From here is my implementation ==========================//

        // Create and compile our GLSL program from the shaders. Todo: load shader
        GLuint programID = LoadShaders("simple_shading.vertexshader", "simple_shading.fragmentshader");

        // Get a handle for uniform variables such as matrices, lights, ....
        GLuint ModelMatrixID = glGetUniformLocation(programID, "ModelMatrix");
        GLuint ViewMatrixID = glGetUniformLocation(programID, "ViewMatrix");
        GLuint NormalMatrixID = glGetUniformLocation(programID, "NormalMatrix");
        GLuint lightPosition_worldspaceID = glGetUniformLocation(programID, "lightPosition_worldspace");
        GLuint lightColorID = glGetUniformLocation(programID, "lightColor");

        // Get a handle for vertex attribute arrays such as vertex positions, colors, normals, ....
        GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");
        GLuint vertexColorID = glGetAttribLocation(programID, "vertexColor");
        GLuint vertexNormal_modelspaceID = glGetAttribLocation(programID, "vertexNormal_modelspace");

        // Load your scene


        TRIModel model;
        TRIModel model2;
    model.loadFromFile("models/ball.tri");
        model2.loadFromFile("models/ball.tri");
        //vector<TRIModel> models;



        /*** Here shows an exmple which generates three different vertex buffers for vertices, colors, normals.***/
        //However, you can always incorporate them together in a single buffer.(Hint: use glBufferSubData)

        GLuint vertexbuffer;
        glGenBuffers(1, &vertexbuffer);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
        glBufferData(GL_ARRAY_BUFFER, model.vertices.size() * sizeof(vec3), &model.vertices[0], GL_STATIC_DRAW);

        GLuint colorbuffer;
        glGenBuffers(1, &colorbuffer);
        glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
        glBufferData(GL_ARRAY_BUFFER, model.vertices.size() * sizeof(vec3), &model.foreColors[0], GL_STATIC_DRAW);

        GLuint normalbuffer;
        glGenBuffers(1, &normalbuffer);
        glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
        glBufferData(GL_ARRAY_BUFFER, model.vertices.size() * sizeof(vec3), &model.normals[0], GL_STATIC_DRAW);

        ///*** **/

        GLfloat rotZ;
   while (!glfwWindowShouldClose(window))
    {
                glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        //      //Tell OpenGL to use the shader program
                glUseProgram(programID);

        //      //send matrix to shader: You can use the keyboard and mouse to close the window and control the transformation
        //      //                               (See computeMatricesFromKey(window) )

                mat4 ModelMatrix;
                mat4 ViewMatrix = lookAt( vec3(0.5, 0.5, 1), vec3(2, 3, 2), vec3(0, 1.0, 0.0) );

                //move object to the world origin
        ModelMatrix = translate(mat4(1.0), -vec3(model.center[0], model.center[1], model.center[2]));
                ModelMatrix = scale( ModelMatrix, vec3(0.002, 0.002, 0.002) );
        ModelMatrix = rotate( ModelMatrix, rotZ, vec3(0.0, 0.0, 1.0) );

                mat4 NormalMatrix = transpose( inverse(ViewMatrix * ModelMatrix) );
                glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, &ViewMatrix[0][0]);
                glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &ModelMatrix[0][0]);
                glUniformMatrix4fv(NormalMatrixID, 1, GL_FALSE, &NormalMatrix[0][0]);

        //      //send lights to shader
                vec3 lightPosition = vec3(4, 4, 4);
                vec3 lightColor = vec3(1.0, 1.0, 1.0);
                glUniform3f(lightPosition_worldspaceID, lightPosition.x, lightPosition.y, lightPosition.z);
                glUniform3f(lightColorID, lightColor.r, lightColor.g, lightColor.b);

                //define an vertex attribute array on the vertex buffer
        glEnableVertexAttribArray(vertexPosition_modelspaceID);
                glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
                glVertexAttribPointer(
                        vertexPosition_modelspaceID, // The attribute we want to configure
                        3,                  // size
                        GL_FLOAT,           // type
                        GL_FALSE,           // normalized?
                        0,                  // stride
                        (void*) 0           // array buffer offset
                );

        //      //define an vertex attribute array on the color buffer
                glEnableVertexAttribArray(vertexColorID);
                glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
                glVertexAttribPointer(
                        vertexColorID, // The attribute we want to configure
                        3,                  // size
                        GL_FLOAT,           // type
                        GL_FALSE,           // normalized?
                        0,                  // stride
                        (void*) 0           // array buffer offset
                );

        //      //define an vertex attribute array on the normal buffer
                glEnableVertexAttribArray(vertexNormal_modelspaceID);
                glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
                glVertexAttribPointer(
                        vertexNormal_modelspaceID, // The attribute we want to configure
                        3,                  // size
                        GL_FLOAT,           // type
                        GL_FALSE,           // normalized?
                        0,                  // stride
                        (void*) 0           // array buffer offset
                );


                //draw the scene by vertex attribute arrays
                glDrawArrays(GL_TRIANGLES, 0, model.vertices.size());
                glDisableVertexAttribArray(vertexPosition_modelspaceID);
                glDisableVertexAttribArray(vertexColorID);
                glDisableVertexAttribArray(vertexNormal_modelspaceID);
        glfwSwapBuffers(window);
        glfwPollEvents();
        //     
        //      //update rotate angle
                rotZ += 0.1;
   }
        //// Cleanup VBO and shade      //glDeleteBuffers(1, &vertexbuffer);
        glDeleteBuffers(1, &colorbuffer);
        glDeleteBuffers(1, &normalbuffer);
        glDeleteProgram(programID);



    //=================== implementation Ends here ================================// 

我读到一些资源,我可以使用 glBufferSubData 将它们合并到一个缓冲区中,但我不知道如何使用它。最终,我成功展示了一个模型,如图所示。现在,如果我需要显示另一个模型,假设我想在右侧显示另一个球,我应该怎么做?我试图生成另一组缓冲区,但它仍然无法正常工作。我能看到的只是模型一。有任何想法吗?

4

0 回答 0