add different meshes with different primitive types & without or with index buffer
refactor background color to main
This commit is contained in:
+62
-5
@@ -24,10 +24,16 @@ void Mesh::render(IDirect3DDevice9* pD3DDevice)
|
|||||||
// 1. set mesh data
|
// 1. set mesh data
|
||||||
pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride);
|
pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride);
|
||||||
pD3DDevice->SetFVF(FVF);
|
pD3DDevice->SetFVF(FVF);
|
||||||
|
pD3DDevice->SetIndices(_pIndexBuffer);
|
||||||
|
|
||||||
// 2. draw call
|
// 2. draw call
|
||||||
// vertex buffer only
|
// vertex buffer only
|
||||||
pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, _vertexCount / 3);
|
//pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, _vertexCount / 3);
|
||||||
|
//pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, _vertexCount - 2);
|
||||||
|
//pD3DDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, _vertexCount - 2);
|
||||||
|
|
||||||
|
// vertex & index buffer
|
||||||
|
pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, _vertexCount, 0, _indexCount / 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::deInit()
|
void Mesh::deInit()
|
||||||
@@ -39,7 +45,7 @@ void Mesh::deInit()
|
|||||||
UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||||
{
|
{
|
||||||
// 1. define mesh parameters
|
// 1. define mesh parameters
|
||||||
_vertexCount = 3;
|
_vertexCount = 4;
|
||||||
_vertexStride = sizeof(Vertex);
|
_vertexStride = sizeof(Vertex);
|
||||||
|
|
||||||
// 2. create vertex buffer object WITHOUT data
|
// 2. create vertex buffer object WITHOUT data
|
||||||
@@ -60,9 +66,31 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
|||||||
CheckFailed(hr, 310);
|
CheckFailed(hr, 310);
|
||||||
|
|
||||||
// fill vertex buffer with data
|
// fill vertex buffer with data
|
||||||
pVertices[0] = Vertex(0.0f, 0.5f, 0.0f);
|
//// triangle with triangle list
|
||||||
pVertices[1] = Vertex(0.5f, -0.5f, 0.0f);
|
//pVertices[0] = Vertex(0.0f, 0.5f, 0.0f);
|
||||||
pVertices[2] = Vertex(-0.5f, -0.5f, 0.0f);
|
//pVertices[1] = Vertex(0.5f, -0.5f, 0.0f);
|
||||||
|
//pVertices[2] = Vertex(-0.5f, -0.5f, 0.0f);
|
||||||
|
|
||||||
|
// quad with triangle list, vertex buffer only
|
||||||
|
//pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f);
|
||||||
|
//pVertices[1] = Vertex(0.5f, 0.5f, 0.0f);
|
||||||
|
//pVertices[2] = Vertex(0.5f, -0.5f, 0.0f);
|
||||||
|
//pVertices[3] = Vertex(-0.5f, 0.5f, 0.0f);
|
||||||
|
//pVertices[4] = Vertex(0.5f, -0.5f, 0.0f);
|
||||||
|
//pVertices[5] = Vertex(-0.5f, -0.5f, 0.0f);
|
||||||
|
|
||||||
|
//// quad with triangle strip, vertex buffer only
|
||||||
|
//pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f);
|
||||||
|
//pVertices[1] = Vertex(0.5f, 0.5f, 0.0f);
|
||||||
|
//pVertices[2] = Vertex(-0.5f, -0.5f, 0.0f);
|
||||||
|
//pVertices[3] = Vertex(0.5f, -0.5f, 0.0f);
|
||||||
|
|
||||||
|
// quad with triangle fan, vertex buffer only
|
||||||
|
// quad with triangle list & index buffer
|
||||||
|
pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f);
|
||||||
|
pVertices[1] = Vertex(0.5f, 0.5f, 0.0f);
|
||||||
|
pVertices[2] = Vertex(0.5f, -0.5f, 0.0f);
|
||||||
|
pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f);
|
||||||
|
|
||||||
hr = _pVertexBuffer->Unlock(); // transfer vertex buffer back from cpu memory to video memory
|
hr = _pVertexBuffer->Unlock(); // transfer vertex buffer back from cpu memory to video memory
|
||||||
CheckFailed(hr, 320);
|
CheckFailed(hr, 320);
|
||||||
@@ -74,5 +102,34 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
|||||||
|
|
||||||
UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice)
|
UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||||
{
|
{
|
||||||
|
_indexCount = 6; // triangle count * 3
|
||||||
|
|
||||||
|
HRESULT hr = pD3DDevice->CreateIndexBuffer(
|
||||||
|
_indexCount * sizeof(WORD),
|
||||||
|
D3DUSAGE_WRITEONLY,
|
||||||
|
D3DFMT_INDEX16,
|
||||||
|
D3DPOOL_MANAGED,
|
||||||
|
&_pIndexBuffer,
|
||||||
|
nullptr
|
||||||
|
);
|
||||||
|
CheckFailed(hr, 330);
|
||||||
|
|
||||||
|
WORD* pIndices = nullptr;
|
||||||
|
|
||||||
|
hr = _pIndexBuffer->Lock(0, 0, reinterpret_cast<void**>(&pIndices), 0);
|
||||||
|
CheckFailed(hr, 340);
|
||||||
|
|
||||||
|
// quad
|
||||||
|
// triangle 1 - 0, 1, 2
|
||||||
|
pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
|
||||||
|
|
||||||
|
// triangle 2 - 0, 2, 3
|
||||||
|
pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;
|
||||||
|
|
||||||
|
hr = _pIndexBuffer->Unlock();
|
||||||
|
CheckFailed(hr, 350);
|
||||||
|
|
||||||
|
pIndices = nullptr;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,9 @@ int WINAPI WinMain(
|
|||||||
error = d3d.init(window.getWindowHandle(), width, height, isFullscreen);
|
error = d3d.init(window.getWindowHandle(), width, height, isFullscreen);
|
||||||
CheckError(error);
|
CheckError(error);
|
||||||
|
|
||||||
|
// set render states
|
||||||
|
d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
|
||||||
|
|
||||||
// 3. create a mesh
|
// 3. create a mesh
|
||||||
Mesh mesh = {};
|
Mesh mesh = {};
|
||||||
error = mesh.init(d3d.getDevice());
|
error = mesh.init(d3d.getDevice());
|
||||||
|
|||||||
Reference in New Issue
Block a user