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
jaisorAsked:
Who is Participating?
 
InsolenceConnect With a Mentor Commented:
That code works perfectly for what you are doing.  Your code is all messed up somewhere else!  If you rotating the velocity vector for the particle make sure that you are not rotating the entire world with it.  =P
0
 
jaisorAuthor Commented:
Thank you master for your infinite wizdom. You were right I was messing up somewhere else and not taking the global matrix in consideration.
0
All Courses

From novice to tech pro — start learning today.