Doomulation
?????????????????????????
Okay, this is a project I was messing around a little with some time ago...
The thing was, I couldn't get it working right. There are two cubes which I render, the second cube INSIDE the first one.
The problem is this:
- either the second cube is completly clipped (ie invisible, doesn't exist)
- or, the clipping starts to get all funny.
To see what I mean, see the screenies I attached.
In screenie, I've used pDevice->SetRenderState(D3DRS_ZENABLE,true);
In the second, pDevice->SetRenderState(D3DRS_ZENABLE,false);
I'm showing the rest of the d3d code (save for the vertices, to save space), in case you might figure out what I'm doing wrong.
This should be everything relevant for the d3d. Vertices, vertex buffer and texture creation has been omitted. Note that also the images I create textures from are transparent (the images!). Which is to make me able to see through the first cube.
While I'm at it... I'm not really good at calculating normals. I was going to try lighting, but I'm not really that good at computing the normals for the vertices. If some typical sample code could be provided I'd be eternally grateful.
The thing was, I couldn't get it working right. There are two cubes which I render, the second cube INSIDE the first one.
The problem is this:
- either the second cube is completly clipped (ie invisible, doesn't exist)
- or, the clipping starts to get all funny.
To see what I mean, see the screenies I attached.
In screenie, I've used pDevice->SetRenderState(D3DRS_ZENABLE,true);
In the second, pDevice->SetRenderState(D3DRS_ZENABLE,false);
I'm showing the rest of the d3d code (save for the vertices, to save space), in case you might figure out what I'm doing wrong.
Code:
bool CD3DTestDlg::InitD3D()
{
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
ZeroMemory(&m_PresentParameters,sizeof(m_PresentParameters));
m_PresentParameters.Windowed = true;
m_PresentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
m_PresentParameters.EnableAutoDepthStencil = true;
m_PresentParameters.AutoDepthStencilFormat = D3DFMT_D16;
m_PresentParameters.hDeviceWindow = m_hWnd;
m_PresentParameters.BackBufferHeight = m_Height;
m_PresentParameters.BackBufferWidth = m_Width;
m_PresentParameters.BackBufferFormat = D3DFMT_R5G6B5;
//m_PresentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
HRESULT hr;
if (FAILED(hr = pD3D->CreateDevice(0,D3DDEVTYPE_HAL,m_hWnd,
D3D_HWVP/*|D3DCREATE_MULTITHREADED*/,&m_PresentParameters,
&pDevice)))
{
ErrorBox(hr,"Error creating a D3D Device!\nReturned error: ");
return false;
}
return true;
}
bool CD3DTestDlg::InitScene()
{
HRESULT hr;
hr = pDevice->SetRenderState(D3DRS_LIGHTING,false);
hr = pDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
//hr = pDevice->SetRenderState(D3DRS_ZENABLE,true);
hr = pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
hr = pDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
hr = pDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
hr = pDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
// Some more code, which inits the vertices, copies them into vertex buffers and creates the textures.
return true;
}
bool CD3DTestDlg::RenderScene()
{
if ( CheckDevice() )
{
HRESULT hr;
hr = pDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0,1.0f,0);
hr = pDevice->BeginScene();
Transform();
hr = pDevice->SetTexture(0,pTextureFront);
hr = pDevice->SetStreamSource(0,pCube,0,sizeof(CUSTOMVERTEX));
pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);
pDevice->SetTexture(0,pTextureBack);
pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,6,2);
pDevice->SetTexture(0,pTextureLeft);
pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,12,2);
pDevice->SetTexture(0,pTextureRight);
pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,18,2);
pDevice->SetTexture(0,pTextureUp);
pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,24,2);
pDevice->SetTexture(0,pTextureDown);
pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,30,2);
pDevice->SetStreamSource(0,pCube2,0,sizeof(CUSTOMVERTEX));
pDevice->SetTexture(0,pTextureCube2);
pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,12);
hr = pDevice->EndScene();
hr = pDevice->Present(NULL,NULL,NULL,NULL);
}
//fRotation += 0.001f;
return true;
}
bool CD3DTestDlg::Transform()
{
D3DXMATRIX matProjection, matView, matWorld, matWorld2, matWorld3;
D3DXMatrixRotationX( &matWorld, timeGetTime()/1000.0f );
D3DXMatrixRotationY( &matWorld2, timeGetTime()/1000.0f );
pDevice->SetTransform(D3DTS_WORLD, &(matWorld2));
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f, 3.0f),
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
D3DXMatrixPerspectiveFovLH(&matProjection,D3DX_PI/4.0f,
(float)m_Width / (float)m_Height,1.0f,1000.0f);
pDevice->SetTransform(D3DTS_PROJECTION,&matProjection);
pDevice->SetTransform(D3DTS_VIEW, &matView);
return true;
}
This should be everything relevant for the d3d. Vertices, vertex buffer and texture creation has been omitted. Note that also the images I create textures from are transparent (the images!). Which is to make me able to see through the first cube.
While I'm at it... I'm not really good at calculating normals. I was going to try lighting, but I'm not really that good at computing the normals for the vertices. If some typical sample code could be provided I'd be eternally grateful.