PDA

View Full Version : Rotation of an object by rotation matrix



MrFoil
03-02-2015, 10:30 AM
Hi all,

I'm trying to rotate an object around an axis (cube around Y), and I actually did it, but the thing is that it is rotating around one of its edges.
Using simple rotation matrix:
1651

Angle is changing in cycle, then I am just sending updated matrix to the vertex shader and multiplying on each vertex.
Vertex shader:


#version 330 core

layout(location = 0) in vec3 VertexPosition;

in vec2 texcoord;
uniform vec4 Translation;
uniform mat4 MVP;
uniform mat3 Rotation;

out vec2 fragTexcoord;


void main(){

gl_Position = MVP * ( ( vec4( VertexPosition * Rotation, 1 ) + Translation ));

fragTexcoord = texcoord;
}


Screenshots:
1652 1653


Can someone explain me how to rotate my cube around an axis that comes through centers of oposite sides?

tkausl
03-02-2015, 11:25 AM
The rotation-matrix is actually rotating around the models (or meshs) origin, so if your model has the origin at one edge, its rotating around this edge. What you should do is rearranging your mesh in your modeling-software. You could center it after loading but thats just a mess if you got more and more meshs with bad origins.

If thats a hardcoded cube with vertices between (0,0,0) and (1,1,1) then use vertices between (-.5, -.5, -.5) and (.5, .5, .5) to set its origin to the center.

GClements
03-02-2015, 04:41 PM
Can someone explain me how to rotate my cube around an axis that comes through centers of oposite sides?
If you want the cube to rotate about its centre, the cube's origin should probably be its centre rather than a corner.

Having said that, you can rotate about an arbitrary point (cx,cy,cz) by translating by (-cx,-cy,-cz), rotating about the origin, then translating by (cx,cy,cz).