Original Post
Lo all,
I'm stuck again.
Basically my world to camera function isn't working as I think it should. The objects don't rotate properly when the camera is away from the world origin (0,0,0). I think I should moving the camera and objects before I rotate everything, but when I try that the objects warp slightly.
It is only and extremely basic wireframe 3D app.
I'll post the code for the function below:
A link to a demo of what I mean is below, you rotate the camera using the up, down arrow keys, and move it side to side with the left and right arrow keys. If you move the camera quite far off center and then rotate, you will see what I mean. It is only about 60-70kb.
www.chizief.co.uk/test3d.exe
Thanks...
Coin
[edited by - Coin on September 6, 2003 9:19:59 PM]
void TriPoly3D_World_to_Camera(TriPoly3D *poly, EulerCam *cam, Point3Dh *wc)
{
int count;
TriPoly3D test;
Matrix3x3 matx, maty, matz;
Matrix3x3 temp;
Matrix3x3 rotation;
// Initialise the rotation matrix inverse X axis
Init_Matrix3x3(&matx, 1, 0, 0,
0, cosf((PI * cam->Direction.x) / 180), -sinf((PI * cam->Direction.x) / 180),
0, sinf((PI * cam->Direction.x) / 180), cosf((PI * cam->Direction.x) / 180));
// Initialise the rotation matrix inverse Z axis
Init_Matrix3x3(&matz, cosf((PI * cam->Direction.z) / 180), -sinf((PI * cam->Direction.z) / 180), 0,
sinf((PI * cam->Direction.z) / 180), cosf((PI * cam->Direction.z) / 180), 0,
0, 0, 1);
// Initialise the rotation matrix inverse Y axis
Init_Matrix3x3(&maty, cosf((PI * cam->Direction.y) / 180), 0, sinf((PI * cam->Direction.y) / 180),
0, 1, 0,
-sinf((PI * cam->Direction.y) / 180), 0, cosf((PI * cam->Direction.y) / 180));
// Multiply the rotation matrices together
MMul_3x3_3x3(&matz, &maty, &temp);
MMul_3x3_3x3(&temp, &matx, &rotation);
// Transform the vertices
for(count = 0; count < 3; count++)
{
// Rotate the points
MMul_3x3_Point3Dh(&rotation, &poly->TVList[count], &poly->TVList[count]);
// Transform the points to camera co-ords
poly->TVList[count].x -= cam->Position.x;
poly->TVList[count].y -= cam->Position.y;
poly->TVList[count].z -= cam->Position.z;
}
}
The code is a bit wasteful, but it is only until I get everything working properly.