How to set DirectX (C++) on a right-handed coordinate system by default?
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.
_____________________________________________________________________________
DEVELOMENT OF STEPS ACCORDING TO THE MSDN
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;
indices = indices;
indices = tmp;
}
p_mesh->UnlockIndexBuffer();
}
______________________________________________________________________________
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.
______________________________________________________________________________
D3DXVECTOR3 pos(3000,0,0);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 0.0f, 1.0f);
D3DXMatrixLookAtRH(&mView, &pos, &target, &up);
D3DXMATRIX transpose_mView,matrizViewFinal,Temp, toSeeMview;
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);
D3DXMatrixTranspose(&transpose_mView,&mView);
Temp=transpose_mView*Ineg3Fila;
D3DXMatrixTranspose(&matrizViewFinal,&Temp);
mView=matrizViewFinal;//Where _31, _32, _33, and _34 * (-1).
______________________________________________________________________________
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.
______________________________________________________________________________
…D3DXMatrixPerspectiveFovRH(&mProj, D3DX_PI * 0.25f, w/h, 1.0f, 5000.0f);…
______________________________________________________________________________
But I saw that the result is wrong. What is the error???.
I have a simple application to check the coordinate system. It consists in a beam. The beam is along of axes “y”(based on a right-handed coordinate system).
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 on a right-handed coordinate system).
If I use the next instruction the rotation is good for pi/2 rad.
______________________________________________________________________________
D3DXMatrixRotationX(&R3x,D3DX_PI/2.0f);
______________________________________________________________________________
But if I implement the following matrix,
______________________________________________________________________________
D3DXMATRIX R3(1,0,0,0,0,cosf(theta13),-sinf(theta13),0,0,sinf(theta13),cosf(theta13),0,0,0,0,1);
______________________________________________________________________________
It should Rotate the beam around the x-axis(based on 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.
Another thing is that If I move the position of the beam along the positive z-axis like the next instruction:
______________________________________________________________________________
mMeshBeam.pos = D3DXVECTOR3(0.0f, 0.0f,600.0f);
______________________________________________________________________________
it move to negative z-axis.
All This mean that the directX continue based on a left-handed coordinate system, despite of all changes That I did.
What’s wrong with that???
I don't believe you need to muck around with the view matrix if you use the *RH function to create it.
Also I'm not really sure why the documentation says you need to flip around the order of your vertices...you could always just use D3DCULL_CW instead of D3DCULL_CCW and everything will work fine.
Also I'm not really sure why the documentation says you need to flip around the order of your vertices...you could always just use D3DCULL_CW instead of D3DCULL_CCW and everything will work fine.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement