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: How to change the point of rotation using OpenGL 3.2 / MVP matrixes?

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

    How to change the point of rotation using OpenGL 3.2 / MVP matrixes?

    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
    Last edited by perrya; 07-12-2013 at 01:47 PM.

  2. #2
    Member Regular Contributor Nowhere-01's Avatar
    Join Date
    Feb 2011
    Location
    Novosibirsk
    Posts
    251
    rotation about origin should work like this:

    MVP = glm::translate(MVP,glm::vec3(-Origin.x, -Origin.y, -Origin.z)); //subtract origin position
    MVP = glm::rotate(MVP,elevation,glm::vec3(1.0f, 0.0f, 0.0f)); //rotate
    MVP = glm::translate(MVP,glm::vec3(Origin.x, Origin.y, Origin.z)); //return object on it's place

    however, i'm not sure, if it's what you was looking for. it's a bit unclear.

Posting Permissions

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