Hey guys,
I'm attempting to roll my own matrix and vector mathematics to do my transformations and creating MV and MVP matrices to send to my shaders. I've managed to get perspective working but I'm having a few problems with my view matrix.
What is working:
I can look around the world with the camera and everything looks and moves fine.
What's the problem:
As soon as I begin to translate my view matrix I get really weird behaviour which I find hard to explain. When I start moving around the world The camera seems to change the direction its looking at.
This is my create view code:
Matrix4x4 createView( Vector3* pos, Vector3* dir, Vector3* up )
{
Matrix4x4 mat = identity();
Matrix4x4 invView = identity();
Vector3 rowZ = sub(dir, pos);
Vector3 row4 = { pos->x, pos->y, pos->z };
Vector3 rowX = crossProd(up, &rowZ);
Vector3 rowY = {0};
normalise(&rowZ);
normalise(&rowX);
normalise(&rowY);
setEqual(&rowY, up);
setRowv(&mat, 0, &rowX, 0.0f);
setRowv(&mat, 1, &rowY, 0.0f);
setRowv(&mat, 2, &rowZ, 0.0f);
setRowv(&mat, 3, &row4, 1.0f);
invView = inverseMat4(&mat); // has to be inverse for camera transformations
return invView;
}
My matrix inverse code is from Mesa3D glu implementation (http://stackoverflow.com/questions/1148309/inverting-a-4x4-matrix)
And here is where the camera gets updated
void updateCam( Camera* cam )
{
Vector3 sum = addVec(&cam->dir, &cam->pos);
cam->view = createView(&cam->pos, &sum, &cam->up);
}
For translation I'm just adding directly to the cam.pos vector.
Rotation seems to work fine but here it is anyway:
void rotateView( Camera* cam, float yawAmt, float pitchAmt)
{
float cosPitch;
float cosYaw;
float sinPitch;
float sinYaw;
cam->yaw += yawAmt;
cam->pitch += pitchAmt;
sinYaw = sin(degToRad(cam->yaw));
sinPitch = sin(degToRad(cam->pitch));
cosYaw = cos(degToRad(cam->yaw));
cosPitch = cos(degToRad(cam->pitch));
cam->dir.x = cosPitch * sinYaw;
cam->dir.z = cosPitch * cosYaw;
cam->dir.y = sinPitch;
}
Thanks for the help, I'm sure the problem lies within the create view function.