Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

acw83

Check my code...

This topic is 6606 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here''s my code for matrix multiplications and 3D rotation, tell me if you see any areas for improevment or flat out errors. Thanks! ---- void MatMul(MATRIX* first, MATRIX* second, MATRIX* dest) { int c, r; // column and row counters for( c = 0; c < 4; c++ ) { for( r = 0; r < 4; r++ ) { dest->data[c][r] = first->data[0][r] * second->data[c][0] + first->data[1][r] * second->data[c][1] + first->data[2][r] * second->data[c][2] + first->data[3][r] * second->data[c][3]; } } } /////////////// void VecRotate(VECTOR* srcVector, VECTOR* destVector, float x, float y, float z) { float xSin, xCos, ySin, yCos, zSin, zCos; xSin = (float)sin(x * PI / 180); xCos = (float)cos(x * PI / 180); ySin = (float)sin(y * PI / 180); yCos = (float)cos(y * PI / 180); zSin = (float)sin(z * PI / 180); zCos = (float)cos(z * PI / 180); MATRIX xRotMat = { 1, 0, 0, 0, 0, xCos, xSin, 0, 0, -xSin, xCos, 0, 0, 0, 0, 1 }; MATRIX yRotMat = { yCos, 0, -ySin, 0, 0, 1, 0, 0, ySin, 0, yCos, 0, 0, 0, 0, 1 }; MATRIX zRotMat = { zCos, zSin, 0, 0, -zSin, zCos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; MATRIX temp, final; MatMul(&xRotMat, &yRotMat, &temp); MatMul(&temp, &zRotMat, &final); destVector->data[0] = final.data[0][0] * srcVector->data[0] + final.data[1][0] * srcVector->data[1] + final.data[2][0] * srcVector->data[2] + final.data[3][0]; destVector->data[1] = final.data[0][1] * srcVector->data[0] + final.data[1][1] * srcVector->data[1] + final.data[2][1] * srcVector->data[2] + final.data[3][1]; destVector->data[2] = final.data[0][2] * srcVector->data[0] + final.data[1][2] * srcVector->data[1] + final.data[2][2] * srcVector->data[2] + final.data[3][2]; }

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
well, i assume VecRotate() is called once per vertex.. for large scenes, with a lot of vertices, it''s pretty crucial to make it run as fast as possible. first off, instead of creating three matrices and multiplying them, i''d make it a single matrix with all three rotations included (basically, take the X, Y and Z rotation matrices and multiply them out on paper). that eliminates unnecessary data access and transfer and two function calls. second, instead of passing two vector instances, make your vector class or struct contain original and transformed coordinates. third, i''m not sure how other compilers handle this, but MSVC actually makes function calls in order to calculate sin() and cos(). using the FSIN/FCOS/FSINCOS assembly instructions instead would allow you inline it, avoid having to cast from double to float and get a pretty nice speed increase overall.
well.. cant think of anythign else for now

-goltrpoat

Share this post


Link to post
Share on other sites
Thanks for the ideas. Before I even worry about optimizations though, I just need to know if I have this right. Are the matrices set up with the proper values in the right places? Am I missing anything? I''ve tested this code but get some really weird results(Vertices skipping a lot between degrees etc...). Any help appreciated.

Share this post


Link to post
Share on other sites
yup, looks right to me. make sure you''re not doing additive rotations, i.e. keep a copy of the original vertex and increase the rotation angle by a constant increment instead of rotating the same vertex over and over by a constant angle. can you elaborate a bit more on how exactly are the results "weird"?

-goltrpoat


--
Float like a butterfly, bite like a crocodile.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!