Search code examples
c++openglglm-math

glm rotation in ortho space


I set up my ortho projection like this :

transform = glm::ortho(0.0f, width, height, 0.0f);

this works pretty well, but when I want to use the glm::rotate function like this:

transform = glm::rotate(transform, glm::radians(45.0f), glm::vec3(0, 0, 1));

my object rotates around 0 : 0 : 0.

my vertices look like this:

GLfloat vertices[] = {
    600, 100, 0,
    612, 100, 0,
    612, 130, 0,
    600, 130, 0
};

how can I make my object rotate around its center ?


Solution

  • If you want to rotate around its center you have to:

    • Translate the object so that the center of the object is moved to (0, 0).
    • Rotate the object.
    • Move the object so that the center point moves in its original position.
    GLfloat center_x = 606.0f;
    GLfloat center_y = 115.0f;
    
    transform = glm::translate(transform, glm::vec3(center_x, center_y, 0));
    transform = glm::rotate(transform, glm::radians(45.0f), glm::vec3(0, 0, 1));
    transform = glm::translate(transform, glm::vec3(-center_x, -center_y, 0));
    

    See also How to use Pivot Point in Transformations