PDA

View Full Version : How to change the point of rotation using OpenGL 3.2 / MVP matrixes?



perrya
07-12-2013, 02:42 PM
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;


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;



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;

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

Nowhere-01
07-12-2013, 02:55 PM
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.