add mesh examples

This commit is contained in:
2025-09-17 14:20:46 +02:00
parent 7aa8e8e79c
commit e5451c10fc
3 changed files with 81 additions and 2 deletions
+1 -1
View File
@@ -61,7 +61,7 @@ void Direct3D::beginScene()
_pD3DDevice->Clear(
0, nullptr, // clear specific areas only
D3DCLEAR_TARGET, // what to clear
D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0xFFFFFFFF // how to clear
D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0xFFFFFFFF // how to clear
);
}
+78 -1
View File
@@ -21,6 +21,16 @@ void Mesh::update(FLOAT dt)
void Mesh::render(IDirect3DDevice9* pD3DDevice)
{
// 1. set mesh data
pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride);
pD3DDevice->SetFVF(FVF);
pD3DDevice->SetIndices(_pIndexBuffer);
// 2. draw call
//pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, _vertexCount / 3);
//pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, _vertexCount - 2);
//pD3DDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, _vertexCount - 2);
pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, _vertexCount, 0, _indexCount / 3);
}
void Mesh::deInit()
@@ -32,7 +42,7 @@ void Mesh::deInit()
UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
{
// 1. define mesh structure
_vertexCount = 3;
_vertexCount = 4;
_vertexStride = sizeof(Vertex);
// 2. create vertex buffer WITHOUT data
@@ -44,12 +54,79 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
&_pVertexBuffer,
nullptr // not used anymore
);
CheckFailed(hr, 300);
// 3. fill vertex buffer with data
Vertex* pVertices = nullptr;
hr = _pVertexBuffer->Lock(0, 0, reinterpret_cast<void**>(&pVertices), 0);
CheckFailed(hr, 310);
// triangle
//pVertices[0] = Vertex(0.0f, 0.5f, 0.0f);
//pVertices[1] = Vertex(0.5f, -0.5f, 0.0f);
//pVertices[2] = Vertex(-0.5f, -0.5f, 0.0f);
// quad with trianglelist WITHOUT index buffer
//// triangle 1
//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);
//// triangle 2
//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 trianglestrip
//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 trianglefan
// quad with trianglelist & 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();
CheckFailed(hr, 320);
pVertices = nullptr;
return 0;
}
UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice)
{
_indexCount = 6; // triangle * 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
pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
// triangle 2
pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;
hr = _pIndexBuffer->Unlock();
CheckFailed(hr, 350);
pIndices = nullptr;
return 0;
}
+2
View File
@@ -32,6 +32,8 @@ int WINAPI WinMain(
error = d3d.init(window.getWindowHandle(), width, height, isFullscreen);
CheckError(error);
d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
// 3. create a mesh
Mesh mesh = {};
error = mesh.init(d3d.getDevice());