[DirectX, C++] Problems drawing a cube on screen

Started by
4 comments, last by __Homer__ 12 years, 7 months ago
Hello gamedev,
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);
Advertisement
So what's the problem? Compiler error? Linker error? Crash?
Oh, I am sorry. The cube is simply not visible. I can move the camera, but I still cannot see the cube at position (0.0f, 0.0f, 0.0f).
Hi :)
I assume this is DX9 stuff (fixed function pipeline).
Where did you enable texturing?
Try to disable lighting, make sure it's not a material issue (ie no diffuse).
Other than that, I can't see how you are making your view matrix.
Suggest to comment it out and use D3DXMatrixLookAt to prove your code, then switch back.
In C++, friends have access to your privates.
In ObjAsm, your members are exposed!

Hi :)
I assume this is DX9 stuff (fixed function pipeline).
Where did you enable texturing?
Other than that, I can't see how you are making your view matrix.
Suggest to comment it out and use D3DXMatrixLookAt to prove your code, then switch back.


Yes, I am using DX9.
I do not enable texturing...My vertex only uses a position and a color.

mCamera = tbMatrixCamera(g_vCameraPos, g_vCameraPos + vCameraDir);
D3D.SetTransform(D3DTS_VIEW, mCamera);

(taken from above)
I am creating the view matrix with the the position of the camera and I look at "vCameraDir".
I think the camera code is fine. What dou you mean with D3DXMatrixLookAt? So I always look at the cube?

Edit: Tried enabling/disabling D3DRS_LIGHTING, no luck.
I can't see inside tbCameraMatrix, unfortunately.
In D3D9, if you are not using lighting / don't have a working Material , leave it off - otherwise everything will definitely be black (or white on very old cards).
Set the window flood color to something other than black.
Disable blending as well.
Disable depth testing.
Disable backface culling.
Check that your camera direction is Normalized - it will screw up your view matrix.
Are you certain that camera view code is not just asking for a position and a direction explicitly?
These are the kinds of things I'd be looking for.
Once you can see your thingy, you can start making decisions about what you want to turn back on.
In C++, friends have access to your privates.
In ObjAsm, your members are exposed!

This topic is closed to new replies.

Advertisement