0

|実は、2 つ以上のモデルを同時に画面に表示しようとしています。頂点、色、法線の 3 つの異なる頂点バッファーを個別に生成しています。メイン コードは次のとおりです。

    //==================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 を使用して単一のバッファに組み込むことができるいくつかのリソースを読みましたが、その使用方法がわかりません。最終的に、写真のように 1 つのモデルを表示することに成功しました。さて、別のモデルを表示する必要がある場合、右側に別のボールを表示したい場合、どうすればよいでしょうか? 別の buffers のセットを生成しようとしましたが、まだ機能していません。私が見ることができるのはモデル 1 だけです。何か案は?

4

0 に答える 0