How to set DirectX (C++) on a right-handed coordinate system by default?

Started by
1 comment, last by vacing 15 years, 7 months ago
According to the MSDN we need to do the follow: _____________________________________________________________________________ Direct3D uses a left-handed coordinate system. If you are porting an application that is based on a right-handed coordinate system, you must make two changes to the data passed to Direct3D. 1. Flip the order of triangle vertices so that the system traverses them clockwise from the front. In other words, if the vertices are v0, v1, v2, pass them to Direct3D as v0, v2, v1. 2. Use the view matrix to scale world space by -1 in the z-direction. To do this, flip the sign of the _31, _32, _33, and _34 member of the D3DMATRIX structure that you use for your view matrix. To obtain what amounts to a right-handed world, use the D3DXMatrixPerspectiveRH and D3DXMatrixOrthoRH functions to define the projection transform. However, be careful to use the corresponding D3DXMatrixLookAtRH function, reverse the backface-culling order, and lay out the cube maps accordingly. _____________________________________________________________________________ Well, I have a Mesh (X. file), now I need to implement the next code to do de (1) point: This code swap two of the three indices in the index buffer.This code is from the forum: Problem with triangle winding order by kovacsp. void ReverseCulling(ID3DXMesh* p_mesh) { Int nFaces = p_mesh->GetNumFaces(); IndexBuffer indices(p_mesh, 0); for (int i = 0; i < nFaces; i++) { DWORD tmp; tmp = indices; indices = indices; indices = tmp; } } But I do not how to read and write indices of my buffer that has the indices of my mesh(X files)to implement the previous function. I know that there are many functions(like Get_Set IndexBuffer, LockIndexBuffer) but they have caused me a lot of problems. That’s why I need any suggestion and if it is possible an example. In conclusion How to implement the function “IndexBuffer indices(p_mesh, 0)” and how to set the indices in the buffer again with C++? <!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - vacing on September 15, 2008 11:08:10 AM]<!–EDIT–></span><!–/EDIT–>
Advertisement
With LockIndexBuffer(). Example (Untested):
void ReverseCulling(ID3DXMesh* p_mesh){   int nFaces = p_mesh->GetNumFaces();   WORD* indices;   HRESULT hResult = p_mesh->LockIndexBuffer(0, (void**)&indices);   if(FAILED(hResult))   {      // Failed to lock index buffer      return;   }   for (int i = 0; i < nFaces; i++) {      DWORD tmp;      tmp = indices;<br>      indices = indices;<br>      indices = tmp;<br>   }<br><br>   p_mesh-&gt;UnlockIndexBuffer()<br>}<br><br><br></pre></div><!–ENDSCRIPT–><br><br>EDIT: Assuming a 16-bit index buffer. If it's 32-bit you'll need to change indices to a DWORD*. If you want to support 16 and 32-bit index buffers, you'll have to have code to check the index buffer format and act accordingly.
Evil Steve, Thanks a lot!! The function is so good, but I haven’t figured out the problem yet.

Review all that I have did until now,
1) Flip the order of triangle vertices so that the system traverses them clockwise from the front. In other words, if the vertices are v0, v1, v2, pass them to Direct3D as v0, v2, v1 implementing the function exposed previously.
______________________________________________________________________________
. . .LoadXFile("Beam.x", &mBeamMesh, mMtrl, mTex);. . .

. . .ReverseCulling(mBoneMesh);. . .


void RobotArmDemo::ReverseCulling(ID3DXMesh* p_mesh)
{
int nFaces = p_mesh->GetNumFaces();
WORD* indices;
HRESULT hResult = p_mesh->LockIndexBuffer(0, (void**)&indices);
if(FAILED(hResult))
{
// Failed to lock index buffer
return;
}

for (int i = 0; i < nFaces; i++) {
WORD tmp;
tmp = indices;<br> indices = indices;<br> indices = tmp;<br> }<br><br> p_mesh-&gt;UnlockIndexBuffer();<br>} <br>______________________________________________________________________________ <br>2.) Use the view matrix to scale world space by -1 in the z-direction. To do this, flip the sign of the _31, _32, _33, and _34 member of the D3DMATRIX structure that you use for your view matrix.<br><br>______________________________________________________________________________<br><br>D3DXVECTOR3 pos(3000,0,0);<br> D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);<br> D3DXVECTOR3 up(0.0f, 0.0f, 1.0f);<br> D3DXMatrixLookAtRH(&mView, &pos, &target, &up);<br><br> D3DXMATRIX transpose_mView,matrizViewFinal,Temp, toSeeMview;<br><br>D3DXMATRIX Ineg3File(1.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,0.0f,-1.0f,0.0f,0.0f,0.0f,0.0f,1.0f);<br><br> D3DXMatrixTranspose(&transpose_mView,&mView);<br> Temp=transpose_mView*Ineg3Fila;<br> D3DXMatrixTranspose(&matrizViewFinal,&Temp);<br><br><br> mView=matrizViewFinal;//Where _31, _32, _33, and _34 * (-1).<br><br>______________________________________________________________________________<br><br>3. )To obtain what amounts to a right-handed world, use the D3DXMatrixPerspectiveRH and D3DXMatrixOrthoRH functions to define the projection transform. However, be careful to use the corresponding D3DXMatrixLookAtRH function, reverse the backface-culling order, and lay out the cube maps accordingly.<br>______________________________________________________________________________<br><br>…D3DXMatrixPerspectiveFovRH(&mProj, D3DX_PI * 0.25f, w/h, 1.0f, 5000.0f);…<br>______________________________________________________________________________<br><br><br>But I saw that the result is wrong. What is the error???.<br><br>I have a simple application to check the coordinate system. It consists in a beam. The beam is along of axes “y”(based &#111;n a right-handed coordinate system).<br><br>The check consists in rotate the beam around the x-axis, producing the beam moves in the counterclockwise direction to the x-axis positive position. (based &#111;n a right-handed coordinate system).<br><br><br>If I use the next instruction the rotation is good for pi/2 rad.<br>______________________________________________________________________________<br>D3DXMatrixRotationX(&R3x,D3DX_PI/2.0f);<br>______________________________________________________________________________<br><br>But if I implement the following matrix,<br>______________________________________________________________________________<br>D3DXMATRIX R3(1,0,0,0,0,cosf(theta13),-sinf(theta13),0,0,sinf(theta13),cosf(theta13),0,0,0,0,1);<br>______________________________________________________________________________<br><br> It should Rotate the beam around the x-axis(based &#111;n a right-handed coordinate system), which it should be equal to the previous instruction of rotation, it produces the beam moves in the counterclockwise direction to the x-axis negative position. <br><br>Another thing is that If I move the position of the beam along the positive z-axis like the next instruction:<br>______________________________________________________________________________<br> mMeshBeam.pos = D3DXVECTOR3(0.0f, 0.0f,600.0f);<br>______________________________________________________________________________<br>it move to negative z-axis.<br><br>All This mean that the directX continue based &#111;n a left-handed coordinate system, despite of all changes That I did.<br><br><br><br>What’s wrong with that???

This topic is closed to new replies.

Advertisement