Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: OpenGL glm::rotate question: How do I change the point of rotation using glm, matrixs

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2013
    Posts
    5

    OpenGL glm::rotate question: How do I change the point of rotation using glm, matrixs

    I have a problem where I need to change the point on which a rotate operation occurs using GLM. The standard response on the Internet is to do the following;

    Code :
    glPushMatrix();
    glTranslatef(x, y, z); 
    glRotatef(angle, rot_axis_x, rot_axis_y, rot_axis_z);  
    Draw();   
    glPopMatrix();

    However, I am using OpenGL and everything revolves around a matrix. So my code looks like this;

    Code :
        glm::vec3   position;
        float       angle, turrent, barrel;
        glm::vec3   direction;
        glm::vec3   scale = glm::vec3(1.0f);
        glm::mat4   MVP;
    ...
        MVP = glm::translate(MVP,position);
        MVP = glm::rotate(MVP,angle,direction);
        MVP = glm::scale(MVP,scale);
        setMVP(MVP);
        clazz.drawWheels();
        clazz.drawBody();
     
        MVP = glm::translate(MVP,glm::vec3(0,0,1.1));
        MVP = glm::rotate(MVP,turrent,direction);
        setMVP(MVP);
        clazz.drawTurrent();
     
        demoElevation();
     
        MVP = glm::rotate(MVP,elevation,glm::vec3(1.0f, 0.0f, 0.0f));
        setMVP(MVP);
        clazz.drawBarrel();
     
    ...
    It's a tank! And I have managed to draw the body nicely. And I have managed to turn the turrent nicely. One thing I can't seem to do is elevate the barrel properly. It doesn't seem to have the right rotation point. But this begs the question, how can I change the rotation point before I do a rotate using OpenGL 3.2 MVP (model view perception) matrixes?

    Doing a;
    Code :
        MVP = glm::translate(MVP,glm::vec3(0,0,.5));
        MVP = glm::rotate(MVP,elevation,glm::vec3(1.0f, 0.0f, 0.0f));
        setMVP(MVP);
        clazz.drawBarrel();
    only moves the barrel further out, it does not change the centre point at which a rotate is made from.

    New to OpenGL and wonders who out there can shed some light on this for me.

    Thanks,

    Perry

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    716
    Please don't double post (see also Forum Posting Guidelines), instead follow up on your other thread if the answer there is not sufficient.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •