Search code examples
c++openglmatrixshadertransformation

How to translate and rotate a Triangle in OpenGL over time?


I am trying to rotate and translate my single triangle over time. I have already written the main.cpp and I have written separate files for my Vertex and Fragment shader source code.

Here is the code in my main.cpp file:

void framebuffer_size_callback(GFLWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// Shaders
const char *vertexShaderSource =
"#version 410\n"
"in vec3 vp;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(aPos, 1.0);\n"
"}\0";

const char *fragmentShaderSource =
"#version 410\n"
"out vec4 FragColor;\n"
"in vec3 myColor;\n"
"void main()\n"
"{\n"
"FragColor = vec4(myColor, 1.0f);\n"
"}\n\0";

int main ()
{
  // start GL context and O/S window using the GLFW helper library
  if (!glfwInit ())
  {
     fprintf (stderr, "ERROR: could not start GLFW3\n");
     return 1;
  }
 // uncomment these lines if on Apple OS X
 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

 GLFWwindow* window = glfwCreateWindow(640, 480, "LearnOpenGL", NULL, NULL);

    if (!window)
    {
        fprintf(stderr, "ERROR: could not open window with GLFW3\n");
        glfwTerminate();
        return 1;
    }
    glfwMakeContextCurrent(window);

    // start GLEW extension handler
    glewExperimental = GL_TRUE;
    glewInit();

    // get version info
    const GLubyte* renderer = glGetString(GL_RENDERER);
    const GLubyte* version = glGetString(GL_VERSION);

    printf("Renderer: %s\n", renderer);
    printf("OpenGL version supported %s\n", version);

    glEnable(GL_DEPTH_TEST); // enable depth-testing
    glDepthFunc(GL_LESS);

    // Draw a single triangle
    float points[] = {
    // positions       // colors
    0.0f, 0.5f, 0.0f,  1.0f, 0.0f, 0.0f,
    0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
    -0.5f, -0.5f, 0.0f, 0.0f 0.0f, 1.0f
    };

    GLuint VBO = 0;
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);

    // Generate a VAO.
    GLuint VAO = 0;
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glEnableVertexAttribArray(0);

    // Compile a Vertex Shader
    int vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    // Compile a fragment shader.
    int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);

    // Compile shaders into a executable shader program.
    int shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, fragmentShader);
    glAttachShader(shaderProgram, vertexShader);
    glLinkProgram(shaderProgram);

    // Create another float array to make my triangle fan.
    float points_5_triangles[] = {
    -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f,
    0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
    0.0f, 0.5f, 0.0f, 1.0f, 2.0f, 0.5f,
    // Another triangle made from point 1, 3, and 4
    -0.5f,
    }
    // Generate another VBO for my Triangle Fan
    GLuint VBO_5_triangles = 0;
    glGenBuffers(1, &VBO_5_triangles);
    glBindBuffer(GL_ARRAY_BUFFER, VBO_5_triangles);
    glBufferData(GL_ARRAY_BUFFER, sizeof(points_5_triangles), points_5_triangles, GL_STATIC_DRAW);

    // Generate another VAO for my Triangle Fan
    GLuint VAO_5_triangles = 0;
    glGenVertexArrays(1, &VAO_5_triangles);
    glBindVertexArray(VAO_5_triangles);
    glBindBuffer(GL_ARRAY_BUFFER, VBO_5_triangles);
    glBufferData(GL_ARRAY_BUFFER, sizeof(points_5_triangles), points_5_triangles, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glEnableVertexAttribArray(0);

    // Drawing the triangles aka render loop
    while (!glfwWindowShouldClose(window))
    {
        processInput(window);
        // wipe the drawing surface clear
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Draw Triangle
        glUseProgram(shaderProgram);
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        // Draw Triangle Fan
        glBindVertexArray(VAO_5_triangles);
        glDrawArrays(GL_TRIANGLE_FAN, 0, 7);

        // swap buffers and poll IO events
        glfwPollEvents();
        glfwSwapBuffers(window);
    }

    // close GL context and any other GLFW resources
    glfwTerminate();
    return 0;
}

The tutorial I am following does go over transformations but in the example it uses, the triangles have textures as well as shaders. For my purposes, I want to do this without textures added to my code.

Can someone walk me through how to add 2 transformations: translate and rotation to my "single triangle" as shown in this code?


Solution

  • Your triangle having a texture or not has nothing to do with transformation.

    You rotate your triangle simply by calculating a transformation matrix, passing it to your vertex shader and multiply it with your coordinates.

    Your Vertex Shader should look look something like this:

    const char *vertexShaderSource =
    "#version 410\n"
    "layout (location = 0) in vec3 vp;\n"
    "uniform mat4 transform;"
    "void main()\n"
    "{\n"
    "    gl_Position = transform * vec4(vp, 1.0);\n"
    "}\0";
    

    I recommend you to use the glm library for that. You calculate your matrix and pass it to your shader like this:

    auto transformMatrix = glm::rotate( /* your rotation calculation */ );
    
    auto transLoc = glGetUniformLocation(shaderProgram, "transform");
    glUniformMatrix4fv(transLoc , 1, GL_FALSE, glm::value_ptr(transformMatrix));