Jump to content
  • Advertisement
Sign in to follow this  
Stowelly

abstracting from Dx's D3DMATRIX functions

This topic is 4568 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

ok i recently had a job interview where they asked me if i was able to do the matrix calculations myself, i realised i couldnt do it and didnt get the job so ive set to learn the math for matrix calculations and found some very good doccumentations, however i have changed my rendering function to support my own matrix multiplications etc but ive ended up with some very strange results, i.e when the character rotates it seems to twist round and go really thin at certain points in the rotation, im sure its a simple enough problem but i seem to be blind to it heres my rendering loop, there ae still some of the DX funcs included as im still working on my own
 D3DXMatrixIdentity(&Total);

	D3DXMATRIX SC;
	D3DXMatrixIdentity(&SC);
	//D3DXMatrixScaling(&SC, Scale.x, Scale.y, Scale.z);
	d3d::ScaleMatrix(SC,Scale);
	D3DXMatrixRotationX(&RotX,  rotx);
	D3DXMatrixRotationY(&RotY,  roty);
	D3DXMatrixRotationZ(&RotZ,  rotz);

    
	D3DXMatrixTranslation(&Pos,_cpos.x,_cpos.y,_cpos.z);
	d3d::MultiplyMatrix(Total,SC,Total);
	//D3DXMatrixMultiply(&Total,&Total,&SC);
	//D3DXMatrixMultiply(&Total,&Total,&RotX);
	d3d::MultiplyMatrix(Total,RotX, Total);
	d3d::MultiplyMatrix(Total,RotY, Total);
	d3d::MultiplyMatrix(Total,RotZ, Total);
	//D3DXMatrixMultiply(&Total,&Total,&RotY);
	//D3DXMatrixMultiply(&Total,&Total,&RotZ);

	//D3DXMatrixMultiply(&Total,&Total,&Pos);
	d3d::MultiplyMatrix(Total,Pos, Total);
and here is the calculations
void d3d::MultiplyMatrix(D3DXMATRIX &m1,D3DXMATRIX &m2,D3DXMATRIX &m3)
{
	//D3DXMatrixIdentity(&m3);

m3._11 =  ((m1._11 * m2._11) + (m1._12 * m2._21) + (m1._13 * m2._31) + (m1._14 * m2._41));
m3._12 =  ((m1._11 * m2._12) + (m1._12 * m2._22) + (m1._13 * m2._32) + (m1._14 * m2._42));
m3._13 =  ((m1._11 * m2._13) + (m1._12 * m2._23) + (m1._13 * m2._33) + (m1._14 * m2._43));
m3._14 =  ((m1._11 * m2._14) + (m1._12 * m2._24) + (m1._13 * m2._34) + (m1._14 * m2._44));

m3._21 =  ((m1._21 * m2._11) + (m1._22 * m2._21) + (m1._23 * m2._31) + (m1._24 * m2._41));
m3._22 =  ((m1._21 * m2._12) + (m1._22 * m2._22) + (m1._23 * m2._32) + (m1._24 * m2._42));
m3._23 =  ((m1._21 * m2._13) + (m1._22 * m2._23) + (m1._23 * m2._33) + (m1._24 * m2._43));
m3._24 =  ((m1._21 * m2._14) + (m1._22 * m2._24) + (m1._23 * m2._34) + (m1._24 * m2._44));

m3._31 =  ((m1._31 * m2._11) + (m1._32 * m2._21) + (m1._33 * m2._31) + (m1._34 * m2._41));
m3._32 =  ((m1._31 * m2._12) + (m1._32 * m2._22) + (m1._33 * m2._32) + (m1._34 * m2._42));
m3._33 =  ((m1._31 * m2._13) + (m1._32 * m2._23) + (m1._33 * m2._33) + (m1._34 * m2._43));
m3._34 =  ((m1._31 * m2._14) + (m1._32 * m2._24) + (m1._33 * m2._34) + (m1._34 * m2._44));

m3._41 =  ((m1._41 * m2._11) + (m1._42 * m2._21) + (m1._43 * m2._31) + (m1._44 * m2._41));
m3._42 =  ((m1._41 * m2._12) + (m1._42 * m2._22) + (m1._43 * m2._32) + (m1._44 * m2._42));
m3._43 =  ((m1._41 * m2._13) + (m1._42 * m2._23) + (m1._43 * m2._33) + (m1._44 * m2._43));
m3._44 =  ((m1._41 * m2._14) + (m1._42 * m2._24) + (m1._43 * m2._34) + (m1._44 * m2._44));
 

Share this post


Link to post
Share on other sites
Advertisement
You're passing in matrices by reference (Note: 2 of them shoud really be const reference), but you pass in the same matrix for 2 parameters. That means that you're editing the matrix as you calculate values. m1 is the same matrix as m3, but the first line writes to m3._11 (which is m1._11). You then use m1._11 in subsequent lines, relying on it being the same as it was when you entered the function.
You could check the address of m1 and compare it to m2 and m3 in the function, or you could pass in 3 matrices instead of 2.

Share this post


Link to post
Share on other sites
excellent thanks for your help, just one more quick question regarding this problem

ive now made the matrixmultiply function locally create the variable and then return it in order to avoid the above problem like so


D3DXMATRIX d3d::MultiplyMatrix(const D3DXMATRIX &m1, const D3DXMATRIX &m2)
{
D3DXMATRIX m3;
D3DXMatrixIdentity(&m3);

//multiplication code

return m3;
}


this works for all the rotations etc, but when i have the call to multiply the position matrix by the end result matrix my mesh completely disapears

Total = d3d::MultiplyMatrix(Pos, Total);

but with the dx matrix multiply it works


D3DXMatrixTranslation(&Pos,_cpos.x,_cpos.y,_cpos.z);
Total = d3d::MultiplyMatrix(SC,Total);
//D3DXMatrixMultiply(&Total,&Total,&SC);
//D3DXMatrixMultiply(&Total,&Total,&RotX);
Total = d3d::MultiplyMatrix(RotX, Total);
Total = d3d::MultiplyMatrix(RotY, Total);
Total = d3d::MultiplyMatrix(RotZ, Total);
//D3DXMatrixMultiply(&Total,&Total,&RotY);
//D3DXMatrixMultiply(&Total,&Total,&RotZ);
D3DXMatrixMultiply(&Total,&Total,&Pos);
//Total = d3d::MultiplyMatrix(Pos, Total);

Share this post


Link to post
Share on other sites
I can't see anything wrong with that code. I'd recommend running it through the debugger, and outputting the value of the matrix at certain points, and compare that to the D3D version to see where it goes wrong.

Share this post


Link to post
Share on other sites
If you ever get the change to take a course in Linear Algebra, and havn't done so yet, catch the opportunity. It is always nice to have a solid mathematical background on these kinds of stuff, instead of only learning from computer source code.

Share this post


Link to post
Share on other sites
Books and courses definitely recommended.

Try changing this:
Total = d3d::MultiplyMatrix(RotX, Total);
Total = d3d::MultiplyMatrix(RotY, Total);
Total = d3d::MultiplyMatrix(RotZ, Total);


to this:
Total = d3d::MultiplyMatrix(Total, RotX);
Total = d3d::MultiplyMatrix(Total, RotY);
Total = d3d::MultiplyMatrix(Total, RotZ);


If that works, then you've just learnt about a property of matrix multiplication:

Generally, unlike scalar multiplication, the product of two matrices (A*B) is not commutative (A*B != B*A) unless one of the matrices is identity (C*I == I*C).

If that works, it also mean your interpretation of whether the matrix is row-major or column-major is out.


TIP: when working out whether to interpret a matrix as row-major or column-major or determining its memory layout to implement a matrix operation from a textbook/website/etc, find some code that applies a translation to the matrix and find the same in the text book. The translation will either be along the bottom or down the right side, make sure you interpret both consistently.

TIP: for getting to grips with maths stuff like vectors and matrices, it can be very useful to render those vectors and [the component vectors of] matrices as coloured lines to get a much more intuitive idea of what's going on (e.g. to see a scale matrix change the length of a basis vector - and even just seeing the basis vectors of an identity matrix can be eye opening).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!