My tetrahedron doesn't render completely depending on angle of rotation

Started by
2 comments, last by jefferytitan 11 years, 11 months ago
Depending on the angle of rotation, one or more edges of my wireframe tetrahedron that should be visible disappears from view. What's causing this?

Here is my code:

void TetrahedronDemo::updateScene(float dt)
{
mGfxStats->setVertexCount(4);
mGfxStats->setTriCount(4);
mGfxStats->update(dt);
gDInput->poll();
if( gDInput->keyDown(DIK_W) )
mCameraHeight += 25.0f * dt;
if( gDInput->keyDown(DIK_S) )
mCameraHeight -= 25.0f * dt;
mCameraRotationY += gDInput->mouseDX() / 100.0f;
mCameraRadius += gDInput->mouseDY() / 25.0f;
if( fabsf(mCameraRotationY) >= 2.0f * D3DX_PI )
mCameraRotationY = 0.0f;
if( mCameraRadius < 5.0f )
mCameraRadius = 5.0f;
buildViewMtx();
}
void TetrahedronDemo::drawScene()
{
HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));
HR(gd3dDevice->BeginScene());
HR(gd3dDevice->SetStreamSource(0, mVB, 0, sizeof(VertexPos)));
HR(gd3dDevice->SetIndices(mIB));
HR(gd3dDevice->SetVertexDeclaration(VertexPos::Decl));
D3DXMATRIX W;
D3DXMatrixIdentity(&W);
HR(gd3dDevice->SetTransform(D3DTS_WORLD, &W));
HR(gd3dDevice->SetTransform(D3DTS_VIEW, &mView));
HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &mProj));
HR(gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME));
HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 4));

mGfxStats->display();
HR(gd3dDevice->EndScene());
HR(gd3dDevice->Present(0, 0, 0, 0));
}
void TetrahedronDemo::buildVertexBuffer()
{
HR(gd3dDevice->CreateVertexBuffer(4 * sizeof(VertexPos), D3DUSAGE_WRITEONLY,
0, D3DPOOL_MANAGED, &mVB, 0));
VertexPos* v = 0;
HR(mVB->Lock(0, 0, (void**)&v, 0));

v[0] = VertexPos(-1.0f, 0.0f, -1.0f);
v[1] = VertexPos(0.0f, 1.0f, 0.0f);
v[2] = VertexPos(0.0f, 0.0f, 1.0f);
v[3] = VertexPos(1.0f, 0.0f, -01.0f);

HR(mVB->Unlock());
}
void TetrahedronDemo::buildIndexBuffer()
{
HR(gd3dDevice->CreateIndexBuffer(12 * sizeof(WORD), D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIB, 0));
WORD* k = 0;
HR(mIB->Lock(0, 0, (void**)&k, 0));

k[0] = 0; k[1] = 1; k[2] = 2;
k[3] = 0; k[4] = 1; k[5] = 3;
k[6] = 0; k[7] = 2; k[8] = 3;
k[9] = 1; k[10]= 2; k[11]= 3;

HR(mIB->Unlock());
}
void TetrahedronDemo::buildViewMtx()
{
float x = mCameraRadius * cosf(mCameraRotationY);
float z = mCameraRadius * sinf(mCameraRotationY);
D3DXVECTOR3 pos(x, mCameraHeight, z);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&mView, &pos, &target, &up);
}
void TetrahedronDemo::buildProjMtx()
{
float w = (float)md3dPP.BackBufferWidth;
float h = (float)md3dPP.BackBufferHeight;
D3DXMatrixPerspectiveFovLH(&mProj, D3DX_PI * 0.25f, w/h, 1.0f, 5000.0f);
}

Advertisement
Hi, not a D3D expert, but I have a few guesses depending upon how the lines disappear. My three general best bets (based on general 3D knowledge) would be:
1. Normals facing the wrong way. This would make a specific face always invisible from the wrong side.
2. Some downsampling/antialiasing effect making a thin line "go away". This would make lines pop in or out while rotating.
3. View frustrum too small, e.g. near or far clipping plane wrong. This would make lines get cut off at a particular point as it rotates.

Using filled in faces instead of wireframe may help to diagnose.
How do I handle the normals or the view frustrum for that matter? Also, would it help to post this question in the DirectX and XNA forum?
NOTE: This is all guesswork.
I'm guessing the normals would be based on the order you define the indices if you haven't explicitly set normals. It is likely the the API requires that you specify the indices in either clockwise or counterclockwise order to automatically generate normals. It's like the right hand rule (or left hand rule based upon implementation) in maths/physics. If you wrap your fingers around the polygon in the order the indices are defined, your thumb will point the direction of the normal. I'm not sure if they would base it upon the left or right hand, but it should be consistent.

PS - Yes, it may be worth asking in that forum if this doesn't help. Good luck!

This topic is closed to new replies.

Advertisement