<div dir="ltr">Hi,<br><br>Some more remarks:<br><br>+#include &quot;d3d9.h&quot; is unneeded: already set in d3dx9.h.<br><br>why not use the more common syntax: pointer-&gt;field instead of (*pointer).field?<br><br> +#define module(vx,vy,vz) sqrt((vx)*(vx)+(vy)*(vy)+(vz)*<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
(vz)) can be avoided by calling d3dxVector3Length.<br></blockquote><br>&nbsp;/*Let&#39;s calculate rotation now*/<br>+&nbsp;&nbsp;&nbsp; /*If some of the scale factors is zero, it is impossible to obtain the rotation*/<br>+&nbsp;&nbsp;&nbsp; if (((*poutscale).x==0)||((*poutscale).y==0)||((*poutscale).z==0))<br>
+&nbsp;&nbsp;&nbsp; {<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return D3DERR_INVALIDCALL;<br>+&nbsp;&nbsp;&nbsp; }<br>+&nbsp;&nbsp;&nbsp; /*We calculate the different products with simple sums of the members of the 3x3 submatrix<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Quaternion (x,y,z,w) to Matrix:<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 1-2y2-2z2&nbsp; 2xy-2wz&nbsp;&nbsp;&nbsp; 2xz+2wy&nbsp;&nbsp; | <br>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 2xy+2wz&nbsp;&nbsp;&nbsp; 1-2x2-2z2&nbsp; 2yz-wx&nbsp;&nbsp;&nbsp; | <br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 2xz-2wy&nbsp;&nbsp;&nbsp; 2yz+2wx&nbsp;&nbsp;&nbsp; 1-2x2-2y2 |*/<br>+&nbsp;&nbsp;&nbsp; wx=-((*pM).m[2][1]/(*poutscale).z-(*pM).m[1][2]/(*poutscale).y)/4;<br>+&nbsp;&nbsp;&nbsp; wy=-((*pM).m[0][2]/(*poutscale).x-(*pM).m[2][0]/(*poutscale).z)/4;<br>
+&nbsp;&nbsp;&nbsp; wz=-((*pM).m[1][0]/(*poutscale).y-(*pM).m[0][1]/(*poutscale).x)/4;<br>+&nbsp;&nbsp;&nbsp; xy=((*pM).m[0][1]/(*poutscale).x+(*pM).m[1][0]/(*poutscale).y)/4;<br>+&nbsp;&nbsp;&nbsp; xz=((*pM).m[0][2]/(*poutscale).x+(*pM).m[2][0]/(*poutscale).z)/4;<br>
+&nbsp;&nbsp;&nbsp; yz=((*pM).m[1][2]/(*poutscale).y+(*pM).m[2][1]/(*poutscale).z)/4;<br>+&nbsp;&nbsp;&nbsp; xx=(1+(*pM).m[0][0]/(*poutscale).x-(*pM).m[1][1]/(*poutscale).y-(*pM).m[2][2]/(*poutscale).z)/4;<br>+&nbsp;&nbsp;&nbsp; yy=(1-(*pM).m[0][0]/(*poutscale).x+(*pM).m[1][1]/(*poutscale).y-(*pM).m[2][2]/(*poutscale).z)/4;<br>
+&nbsp;&nbsp;&nbsp; zz=(1-(*pM).m[0][0]/(*poutscale).x-(*pM).m[1][1]/(*poutscale).y+(*pM).m[2][2]/(*poutscale).z)/4;<br>+&nbsp;&nbsp;&nbsp; /*Then we go for the biggest component in order to minimise division error as commonly explained in the literacy.<br>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; We just calculate one component from the diagonal to avoid sign loss of the square root. The first sign is not important<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; because the rotations (x,y,z,w) and (-x,-y,-z,-w) are equivalent. But if we calc ulated the other two components we would have<br>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to choose and could be wrong.*/<br>+&nbsp;&nbsp;&nbsp; if ((xx&gt;yy) &amp;&amp; (xx&gt;zz))<br>+&nbsp;&nbsp;&nbsp; {<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (*poutrotation).x=sqrt(xx);<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (*poutrotation).y=xy/(*poutrotation).x;<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (*poutrotation).z=xz/(*poutrotation).x;<br>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (*poutrotation).w=wx/(*poutrotation).x; <br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return S_OK;<br>+&nbsp;&nbsp;&nbsp; }<br>+&nbsp;&nbsp;&nbsp; if (yy&gt;zz)<br>+&nbsp;&nbsp;&nbsp; {<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (*poutrotation).y=sqrt(yy);<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (*poutrotation).x=xy/(*poutrotation).y;<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (*poutrotation).z=yz/(*poutrotation).y;<br>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (*poutrotation).w=wy/(*poutrotation).y; <br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return S_OK;<br>+&nbsp;&nbsp;&nbsp; }<br>+&nbsp;&nbsp;&nbsp; (*poutrotation).z=sqrt(zz);<br>+&nbsp;&nbsp;&nbsp; (*poutrotation).x=xz/(*poutrotation).z;<br>+&nbsp;&nbsp;&nbsp; (*poutrotation).y=yz/(*poutrotation).z;<br>+&nbsp;&nbsp;&nbsp; (*poutrotation).w=wz/(*poutrotation).z; <br>
+&nbsp;&nbsp;&nbsp; return S_OK;<br>+}<br>+<br>can be avoided by calling D3DXQuaternionRotationMatrix<br><br><br>You could avoid useless explicit allocation of memory by writing<br>D3DXMATRIX pM; instead of D3DXMATRIX *pM;......<br>and then D3DXMatrixDecompose(&amp;poutscale, &amp;poutrotation, &amp;pouttranslation, &amp;pM); instead of D3DXMatrixDecompose(poutscale, poutrotation, pouttranslation, pM);<br>
<br>David<br><br><br><br><div class="gmail_quote">2008/8/4 H. Verbeet <span dir="ltr">&lt;<a href="mailto:hverbeet@gmail.com">hverbeet@gmail.com</a>&gt;</span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
2008/8/4 Luis Busquets &lt;<a href="mailto:luis.busquets@ilidium.com">luis.busquets@ilidium.com</a>&gt;:<br>
&gt; +#include &quot;d3d9.h&quot;<br>
...<br>
&gt; +#define module(vx,vy,vz) sqrt((vx)*(vx)+(vy)*(vy)+(vz)*(vz))<br>
...<br>
&gt; +#include &quot;stdio.h&quot;<br>
<br>
You don&#39;t really need those.</blockquote><br></div><br></div>