简体   繁体   中英

OpenGL 3.3 - Different rotation for 2 triangles

I am following the opengl-tutorial.org series and the tutorial 3 draws a triangle on the screen.The code uses a vertex shader to make the vertex transformations by feeding it a ModelViewProjection (MVP) matrix.

I can make the triangle rotate around it's origin on every loop. What I can't come around is when I add a second triangle, how can I make only one of them rotate while the other one remains static. This whole vertex transformations in the shader working together with the main loop is confusing me.

Here's part of the code directly from the tutorial:

static const GLfloat g_vertex_buffer_data[] = { 
    -1.0f, -1.0f, 0.0f,
     1.0f, -1.0f, 0.0f,
     0.0f,  1.0f, 0.0f,
};
static const GLushort g_element_buffer_data[] = { 0, 1, 2 };

GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);

glm::mat4 View       = glm::lookAt(
                            glm::vec3(0,0,3), 
                            glm::vec3(0,0,0), 
                            glm::vec3(0,1,0));

glm::mat4 Model      = glm::mat4(1.0f);

glm::mat4 MVP        = Projection * View * Model; 

do{

    // Clear the screen
    glClear( GL_COLOR_BUFFER_BIT );

    // Use our shader
    glUseProgram(programID);

    // Send our transformation to the currently bound shader, 
    // in the "MVP" uniform
    glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);

    // 1rst attribute buffer : vertices
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glVertexAttribPointer(
        0,                 
        3,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // normalized?
        0,                  // stride
        (void*)0            // array buffer offset
    );

    // Draw the triangle !
    glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle

    glDisableVertexAttribArray(0);

    // Swap buffers
    glfwSwapBuffers(window);
    glfwPollEvents();

}

To rotate, I'm using this:

glm::mat4 Model      = glm::mat4(1.0f);

rot=rot+0.01f;
glm::vec3 myRotationAxis( 0, 0, 1);
Model = glm::rotate( Model, rot, myRotationAxis );

glm::mat4 MVP        = Projection * View * Model; 

I'm drawing a second triangle by modifying the vertex buffer on top:

static const GLfloat g_vertex_buffer_data[] = { 
    -1.0f, -1.0f, 0.0f,
     1.0f, -1.0f, 0.0f,
     0.0f,  1.0f, 0.0f,
     1.5f, 0.0f, -5.0f,
     2.5f, 0.0f, -5.0f,
     2.0f, 1.0f, -5.0f
};

And then draw my 2 triangles:

glDrawArrays(GL_TRIANGLES, 0, 6); // 3 indices starting at 0 -> 1 triangle

The vertex shader is clearly transforming all 6 vertices. How would I make this program so that only the second triangle rotates?

You're sending both triangles through the same vertex shader with the same parameters. You could either send them through the shader separately, with different parameters, or you could send them through different shaders, depending on what you're trying to do. The easiest way is probably something like this:

// Draw the first triangle like you did above
glDrawArrays (GL_TRIANGLES, 0, 3);

// ... set up the matrix for the second triangle here ... 

// Tell the shader about the new matrix
glUniformMatrix (MatrixID, 1, GL_FALSE, &MVP[0][0]);

// And draw starting at the second triangle
glDrawArrays (GL_TRIANGLES, 3, 3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM