Home > Back-end >  How to Rotate a Quad
How to Rotate a Quad

Time:12-16

I'm trying to make a quad rotate around its center. I am using glm::rotate() and setting the quad to rotate on the z axis. However when I do this it gives this weird effect. The quad stretches and warps. It almost looks 3d but since I am rotating it around the z axis that shouldn't happen right?

Here's relevant code for context:

    float rotation = 0.0f;
    double prevTime = glfwGetTime();

    while (!glfwWindowShouldClose(window)) 
    {
        GLCall(glClearColor(0.0f, 0.0f, 0.0f, 1.0f));

        GLCall(glClear(GL_COLOR_BUFFER_BIT));

        updateInput(window);

        shader.Use();
        glUniform1f(xMov, x);
        glUniform1f(yMov, y);
        test.Bind();

        double crntTime = glfwGetTime();
        if (crntTime - prevTime >= 1 / 60) 
        {
            rotation  = 0.5f;
            prevTime = crntTime;
        }

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

        model = glm::rotate(model, glm::radians(rotation), glm::vec3(0.0f, 0.0f, 1.0f));

        int modelLoc = glGetUniformLocation(shader.id, "model");
        glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));

        vao.Bind();
        vBuffer1.Bind();
        iBuffer1.Bind();

        GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0));

        glfwSwapBuffers(window);

        glfwPollEvents();
    }

Shader:

#version 440 core

layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aColor;
layout(location = 2) in vec2 aTex;

out vec3 color;
out vec2 texCoord;

uniform float xMove;
uniform float yMove;
uniform mat4 model;

void main()
{
    gl_Position = model * vec4(aPos.x   xMove, aPos.y   yMove, aPos.z, 1.0);
    color = aColor;
    texCoord = aTex;
}

CodePudding user response:

Without you showing the graphical output it is hard to say.

Your first issue is, you are not rotating around the center, to rotate by the center you must, offset the quad so that its center is at 0,0. then rotate, then offset back to the original position, but you have this line:

gl_Position = model * vec4(aPos.x xMove, aPos.y yMove, aPos.z, 1.0);

Under the assumption that the quad as at the origin to begin with you are rotating it around the point (-xMove, -yMove).

  • Related