I want to a draw 3D cube using the vertex and index buffer (DX9!). But I cannot see the cube on screen.
This is my vertex:
struct SCubeVertex
{
tbVector3 vPosition;
DWORD dwColor;
static const DWORD dwFVF;
};
const DWORD SCubeVertex::dwFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
I am using an engine that makes it somewhat easier to use Direct3D. So I will include the declaration of some functions.
This is how initialize my buffers.
SCubeVertex Vertex;
vBuf = new tbVertexBuffer;
//tbResult Init(DWORD dwSize, DWORD dwVertexSize, DWORD dwFVF, DWORD dwUsage = D3DUSAGE_WRITEONLY, D3DPOOL Pool = D3DPOOL_DEFAULT);
if (vBuf->Init(8 * sizeof(SCubeVertex), sizeof(SCubeVertex), SVertex::dwFVF, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DPOOL_DEFAULT))
{
// error...
}
iBuf = new tbIndexBuffer;
//tbResult Init(DWORD dwSize, DWORD dwIndexSize, D3DFORMAT IndexFormat, DWORD dwUsage = D3DUSAGE_WRITEONLY, D3DPOOL Pool = D3DPOOL_DEFAULT);
if(iBuf->Init(36 * sizeof(WORD), sizeof(WORD), D3DFMT_INDEX16))
{
// error..
}
// Color is choosen randomly
Vertex.vPosition = pos + tbVector3(-1.0f, 1.0f, -1.0f);
Vertex.dwColor = tbColorRandom(1.0f) * 2.0f;
vBuf->SetVertex(0, &Vertex);
Vertex.vPosition = pos + tbVector3(-1.0f, 1.0f, 1.0f);
Vertex.dwColor = tbColorRandom(1.0f) * 2.0f;
vBuf->SetVertex(1, &Vertex);
Vertex.vPosition = pos + tbVector3( 1.0f, 1.0f, 1.0f);
Vertex.dwColor = tbColorRandom(1.0f) * 2.0f;
vBuf->SetVertex(2, &Vertex);
Vertex.vPosition = pos + tbVector3( 1.0f, 1.0f, -1.0f);
Vertex.dwColor = tbColorRandom(1.0f) * 2.0f;
vBuf->SetVertex(3, &Vertex);
Vertex.vPosition = pos + tbVector3(-1.0f, -1.0f, -1.0f);
Vertex.dwColor = tbColorRandom(1.0f) * 2.0f;
vBuf->SetVertex(4, &Vertex);
Vertex.vPosition = pos + tbVector3(-1.0f, -1.0f, 1.0f);
Vertex.dwColor = tbColorRandom(1.0f) * 2.0f;
vBuf->SetVertex(5, &Vertex);
Vertex.vPosition = pos + tbVector3( 1.0f, -1.0f, 1.0f);
Vertex.dwColor = tbColorRandom(1.0f) * 2.0f;
vBuf->SetVertex(6, &Vertex);
Vertex.vPosition = pos + tbVector3( 1.0f, -1.0f, -1.0f);
Vertex.dwColor = tbColorRandom(1.0f) * 2.0f;
vBuf->SetVertex(7, &Vertex);
// index order is correct!
int aiIndex[36] =
{0, 3, 7, 0, 7, 4, // Front
2, 1, 5, 2, 5, 6, // Back
1, 0, 4, 1, 4, 5, // Left
3, 2, 6, 3, 6, 7, // Right
0, 1, 2, 0, 2, 3, // Top
6, 5, 4, 6, 4, 7}; // Bottom
iBuf->AddIndices(36, aiIndex);
Here's the actual render function. I am using a sky box and a free flying camera.
tbResult RenderProc(float fNumSecsPassed)
{
tbMatrix mProjection;
tbMatrix mCamera;
tbMatrix mWorld;
tbVector3 vCameraDir;
int iNumPasses;
tbDirect3D& D3D = tbDirect3D::Instance();
D3D->BeginScene();
// camera stuff
// ------------------------------------------------------------------
// create projection matrix
mProjection = tbMatrixProjection(TB_DEG_TO_RAD(90.0f), D3D.GetAspect(), 0.1f, 100.0f);
D3D.SetTransform(D3DTS_PROJECTION, mProjection);
// create camera matrix (unimportant regarding the issue)
vCameraDir = tbVector3(sinf(g_fCameraRot) * cosf(g_fCameraUpDown),
sinf(g_fCameraUpDown),
cosf(g_fCameraRot) * cosf(g_fCameraUpDown));
mCamera = tbMatrixCamera(g_vCameraPos, g_vCameraPos + vCameraDir);
D3D.SetTransform(D3DTS_VIEW, mCamera);
// sky box stuff
// ------------------------------------------------------------------
D3D.SetRS(D3DRS_ZENABLE, FALSE);
D3D.SetRS(D3DRS_DITHERENABLE, TRUE);
D3D.SetTexture(0, g_pEnvMap);
D3D.SetTSS(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
// create worldmatrix, that translates the sky box the camera
mWorld = tbMatrixTranslation(g_vCameraPos);
D3D.SetTransform(D3DTS_WORLD, mWorld);
D3D->SetStreamSource(0, g_pSkyBoxVB->GetVB(), 0, sizeof(SSkyBoxVertex));
D3D->SetIndices(g_pSkyBoxIB->GetIB());
D3D.SetFVF(SSkyBoxVertex::dwFVF);
// Draw!
iNumPasses = g_pSkyBoxEffect->Begin();
for(int iPass = 0; iPass < iNumPasses; iPass++)
{
g_pSkyBoxEffect->Pass(iPass);
D3D->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
}
g_pSkyBoxEffect->End();
// cube stuff
// ------------------------------------------------------------------
D3D.SetRS(D3DRS_ZENABLE, TRUE);
D3D.SetTransform(D3DTS_WORLD, tbMatrixIdentity());
g_Cube->Update(D3D); //g_Cube is a initialized GameObject
D3D->EndScene();
What g_Cube->Update(D3D); calls:
D3D->SetStreamSource(0, vBuf->GetVB(), 0, sizeof(SCubeVertex));
D3D->SetIndices(iBuf->GetIB());
D3D->SetFVF(SCubeVertex::dwFVF);
D3D->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);