Reputation: 413
I am using the glRotate and glTranslate functions to rotate a model around itself.When I run my program though, my model is rotating around a point right next to it. Here is my model definition:
private static final float[] mesh = {
-0.5f, 0.5f,-0f,
-0.5f, -0.5f, -0,
0.5f, -0.5f, 0f,
0.5f, 0.5f, 0f,
-0.5f, 0.5f,-1f,
-0.5f, -0.5f, -1f,
0.5f, -0.5f, -1f,
0.5f, 0.5f, -1f,
};
private static final byte[] indices = {
0, 2, 1,
0, 3, 2,
1,2,6,
6,5,1,
4,5,6,
6,7,4,
2,3,6,
6,3,7,
0,7,3,
0,4,7,
0,1,5,
0,5,4
};
and here is my function for rotating the model:
glRotatef(rotation.x,1,0,0);
glRotatef(rotation.y,0,1,0);
glRotatef(rotation.z,0,0,1);`
I am using LWJGL but I can read C++ just fine.
Upvotes: 0
Views: 538
Reputation: 21993
As your code is not complete I have to guess a little but I suspect you did the following.
You did a translate of .5 in the z direction to get your object centerered oround the origin and then you applied the rotation. Expecting it to turn around the origin which is now located within the object. Makes sense but unfortunatly this is not how OpenGL works.
When you translate the object the whole coordinate system is translated with it. So when you rotate around the origin the origin is still a point beside your object.
You will have to reverse the order. So you first rotate the object and then translate the object. Note that when you rotate the object the coordinate system also rotates with it so the amount and direction you need to translate is still .5 in the z direction regardless of how much you rotated your object.
Upvotes: 3