From e5451c10fc4ccbbadd659b0d8950b1f6f4917af8 Mon Sep 17 00:00:00 2001 From: David Hackbarth Date: Wed, 17 Sep 2025 14:20:46 +0200 Subject: [PATCH] add mesh examples --- GDP/D3D9/Direct3D.cpp | 2 +- GDP/D3D9/Mesh.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++- GDP/D3D9/main.cpp | 2 ++ 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/GDP/D3D9/Direct3D.cpp b/GDP/D3D9/Direct3D.cpp index abadb5a..c51f322 100644 --- a/GDP/D3D9/Direct3D.cpp +++ b/GDP/D3D9/Direct3D.cpp @@ -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 ); } diff --git a/GDP/D3D9/Mesh.cpp b/GDP/D3D9/Mesh.cpp index 2c47363..970568d 100644 --- a/GDP/D3D9/Mesh.cpp +++ b/GDP/D3D9/Mesh.cpp @@ -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(&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(&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; } diff --git a/GDP/D3D9/main.cpp b/GDP/D3D9/main.cpp index cc20ea5..bd13e87 100644 --- a/GDP/D3D9/main.cpp +++ b/GDP/D3D9/main.cpp @@ -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());