From 948c5e067ef2dff00847d113096f84e6803a4221 Mon Sep 17 00:00:00 2001 From: David Hackbarth Date: Mon, 9 Nov 2026 15:50:23 +0100 Subject: [PATCH] Add Vertex Add triangle & quad examples --- GDP/D3D9/D3D9.vcxproj | 1 + GDP/D3D9/D3D9.vcxproj.filters | 3 ++ GDP/D3D9/Mesh.cpp | 78 +++++++++++++++++++++++++++++++++++ GDP/D3D9/Vertex.h | 15 +++++++ 4 files changed, 97 insertions(+) create mode 100644 GDP/D3D9/Vertex.h diff --git a/GDP/D3D9/D3D9.vcxproj b/GDP/D3D9/D3D9.vcxproj index ba356b8..0bfee2a 100644 --- a/GDP/D3D9/D3D9.vcxproj +++ b/GDP/D3D9/D3D9.vcxproj @@ -194,6 +194,7 @@ + diff --git a/GDP/D3D9/D3D9.vcxproj.filters b/GDP/D3D9/D3D9.vcxproj.filters index 8b4a2f3..a081ecc 100644 --- a/GDP/D3D9/D3D9.vcxproj.filters +++ b/GDP/D3D9/D3D9.vcxproj.filters @@ -68,5 +68,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/GDP/D3D9/Mesh.cpp b/GDP/D3D9/Mesh.cpp index 443ec79..244bb30 100644 --- a/GDP/D3D9/Mesh.cpp +++ b/GDP/D3D9/Mesh.cpp @@ -1,5 +1,6 @@ #include "Mesh.h" #include "Utilities.h" +#include "Vertex.h" UINT Mesh::init(IDirect3DDevice9* pD3DDevice) { @@ -20,6 +21,19 @@ void Mesh::update(FLOAT dt) void Mesh::render(IDirect3DDevice9* pD3DDevice) { + // setup pipeline + pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride); + pD3DDevice->SetIndices(_pIndexBuffer); + pD3DDevice->SetFVF(FVF); + + // draw call + pD3DDevice->DrawIndexedPrimitive( + D3DPT_TRIANGLELIST, // primitive type + 0, 0, // vertex buffer offsets + _vertexCount, + 0, // index buffer offset + _indexCount / 3 // primitive count + ); } void Mesh::deInit() @@ -30,10 +44,74 @@ void Mesh::deInit() UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice) { + // define values + _vertexCount = 4; + _vertexStride = sizeof(Vertex); + + // describe & create vertex buffer + HRESULT hr = pD3DDevice->CreateVertexBuffer( + _vertexCount * _vertexStride, // total size of content + D3DUSAGE_WRITEONLY, // read and/or write access + FVF, // FVF - Flexible Vertex Format -> vertex structure + D3DPOOL_MANAGED, // memmory management + &_pVertexBuffer, + nullptr // deprecated, always nullptr + ); + CheckFailed(hr, 300); + + // populate vertex buffer + Vertex* pVertices = nullptr; + hr = _pVertexBuffer->Lock( + 0, 0, // offset & size to lock, here everything + reinterpret_cast(&pVertices), + 0 // optional flags + ); + CheckFailed(hr, 310); + + // triangle + //pVertices[0] = Vertex(0.0f, 1.0f, 0.0f); + //pVertices[1] = Vertex(1.0f, -1.0f, 0.0f); + //pVertices[2] = Vertex(-1.0f, -1.0f, 0.0f); + + // quad + 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; + + HRESULT hr = pD3DDevice->CreateIndexBuffer(_indexCount * sizeof(unsigned short), + D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &_pIndexBuffer, nullptr); + CheckFailed(hr, 330); + + unsigned short* pIndices = nullptr; + hr = _pIndexBuffer->Lock(0, 0, reinterpret_cast(&pIndices), 0); + CheckFailed(hr, 340); + + // triangle + //pIndices[0] = 0; + //pIndices[1] = 1; + //pIndices[2] = 2; + + // quad + pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 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/Vertex.h b/GDP/D3D9/Vertex.h new file mode 100644 index 0000000..b2e1809 --- /dev/null +++ b/GDP/D3D9/Vertex.h @@ -0,0 +1,15 @@ +#pragma once +#include + +#define FVF D3DFVF_XYZ + +struct Vertex +{ + // position + float x; + float y; + float z; + + Vertex() : x(0.0f), y(0.0f), z(0.0f) {} + Vertex(float x, float y, float z) : x(x), y(y), z(z) {} +}; \ No newline at end of file