Link to home
Start Free TrialLog in
Avatar of jaisor
jaisor

asked on

How can I use the Model Matrix to determine where and how was a vector rotated?

Here is my problem. I know the rotation (angleX, angleY) of a vector, and I can create a matrix that completes that rotation or I can use glRotatef() and let OpenGL make the matrix for me. Then I can retrieve that matrix with let say

glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);

After that I try to apply that matrix to a vector - and to retrieve the new vector coordinates and there comes the problem - IT IS NEVER ACCURATE. If I let opengl transform the vector everything works fine, but when I try to multiply my vector with the matrix - the new coordinates are off.

Here is the part of my code:

void Rotate(GLfloat angle, GLfloat x, GLfloat y, CVector3D &vView)
{
     GLfloat n[16];
     glPushMatrix();
     glRotatef(angle,x,y,0.0);
     glGetFloatv(GL_MODELVIEW_MATRIX, n);
     glPopMatrix();

     GLfloat     v1[4],v2[4];

     v1[0] = vView.X();
     v1[1] = vView.Y();
     v1[2] = vView.Z();
     v1[3] = 1;

     GLfloat     sum;
     int     index, alpha;
     for (index = 0; index < 4; index++)
     {
          sum= 0;

          for (alpha = 0; alpha < 4; alpha++)
               sum += n[index + alpha*4] * v1[alpha];

          v2[index] = (float)sum;
     }

     vView = CVector3D(v2[0], v2[1], v2[2]);    
}

This function is supposed to rotate the velocity vector for the particle from a rotation angle.

Please help
ASKER CERTIFIED SOLUTION
Avatar of Insolence
Insolence

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jaisor
jaisor

ASKER

Thank you master for your infinite wizdom. You were right I was messing up somewhere else and not taking the global matrix in consideration.