Wednesday, November 10, 2010

Unexpected Roadblock

Well this... was unexpected. I've been cracking my skull under the vice grips of scrutiny as I review code trying to discern a reason for its undesired behavior. I hate to say that the code is not working precisely because it is working. Code works. It's just not working in the manner that I expect or want.

The part about it that makes it so much fun is that this was supposed to be the easy part. Afterall, I've done it before.

If, when rendering, my camera is positioned like so:
Then it stands to complete reason that the following should retrieve the exact same matrix:
glMatrixMode(GL_MODELVIEW);
glPushmatrix();
glLoadIdentity();
glTranslatef(Camera->pos.x, Camera->pos.y, Camera->pos.z);
glRotatef(Camera->theta, 1, 0, 0);
glRtoatef(Camera,phi, 0, 1, 0);
Then it stands to complete reason that the following should return the same matrix:

GLfloat m[16];
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(Camera->pos.x, Camera->pos.y, Camera->pos.z);
glRotatef(Camera->theta, 1, 0, 0);
glRtoatef(Camera,phi, 0, 1, 0);
glGetFloatfv(GL_MODELVIEW_MATRIX, m);
glPop();
So when I declare a new object in the world and assign it a z coordinate of -4, it should have its coordinates transformed to be right in front of the camera when rotated by the above matrix m.
gameObject++;
gameObject=gameObject->getLastChild();
gameObject->pos.z=-4.
gameObject->pos = rotateVector(gameObject->pos);
Where
rotateVector(myVector temp)
{
     myVector tempmovement;
     tempmovememnt.x = temp->pos.x*m[0] + temp->pos.y*m[4] +
                        temp->pos.z*m[8] + m[12];
     tempmovememnt.y = temp->pos.x*m[1] + temp->pos.y*m[5] +
                        temp->pos.z*m[9] + m[13];
     tempmovememnt.z = temp->pos.x*m[2] + temp->pos.y*m[6] +
                        temp->pos.z*m[10] + m[14];

     return tempmovement;
}
But this isn't the case. When spawning a new object, it's placed in a wholly different location. I can't make sense of it. Unfortunately, due to my frustration I've taken liberties with good coding practice and logic have begun changing things on a whim, trying to get it right by sheer trial and error. This is, of course, bad.

 Tonight, I intend to sit down, have a beer (or four) and figure this out logically.

So irritated right now...

1 comment:

  1. 500 points to the first person to point out all the errors in the code snippets above!

    ReplyDelete