theman
theman

Reputation: 345

Object rotating via world axis and not local axis OpenGL

So I have a function here

void rotateLocal(GLfloat deg, GLfloat x, GLfloat y, GLfloat z)

where x, y, z are the coordinates of a local axis you want to rotate around for this object. I am using (0,1,0) for testing purposes, but yet it still ONLY rotates around world y instead of local y. Here is the logic for one vertex of an object in this function:

ax = x;
ay = y;
az = z;

//normalize
length = sqrt((ax * ax) + (ay * ay) + (az * az));
ux = ax / length;
uy = ay / length;
uz = az / length;

//square these
uxS = ux * ux;
uyS = uy * uy;
uzS = uz * uz;

getx = vertex[0];
gety = vertex[1];
getz = vertex[2];

//find new vertex points using rotation matrix for local axis
vertex[0] = (getx * (uxS + cos(deg) * (1 - uxS))) + (gety * (ux * uy * (1 - cos(deg)) - uz * sin(deg))) + (getz * (uz * ux * (1 - cos(deg)) + uy * sin(deg)));
vertex[1] = (getx * (ux * uy * (1-cos(deg)) + uz * sin(deg))) + (gety * (uyS + cos(deg) * (1 - uyS))) + (getz * (uy * uz * (1 - cos(deg)) - ux * sin(deg)));
vertex[2] = (getx * (uz * ux * (1-cos(deg)) - uy * sin(deg))) + (gety * (uy * uz * (1-cos(deg)) + ux * sin(deg))) + (getz * (uzS + cos(deg) * (1-uzS)));

is there something wrong with my rotation matrix? Am I using incorrect variable somewhere?

NOTE: I don't want to use RotateGL or anything like that, I want to do the matrix math myself.

Upvotes: 0

Views: 946

Answers (1)

Jordi Cruzado
Jordi Cruzado

Reputation: 885

The problem is that you are expressing the axis in world coordinates. What you have to do is:

  • Get the local to world transformation matrix of the object you want to rotate.
  • Use the inverse of this matrix to convert the world axis (in your example (0,1,0)) to local coordinates.
  • Use the converted axis to compute the rotation as you are doing in the code.

I understand that the coordinates of the object are expressed in local space.

That's all.

Upvotes: 1

Related Questions