From 9a0c9a50ea56c98929d0189a524a741d9e7515d4 Mon Sep 17 00:00:00 2001 From: David Hackbarth Date: Wed, 10 Sep 2025 11:27:40 +0200 Subject: [PATCH] add create vertex buffer for a triangle --- GDP/D3D9/D3D9.vcxproj | 1 + GDP/D3D9/D3D9.vcxproj.filters | 3 +++ GDP/D3D9/Direct3D.cpp | 13 ++---------- GDP/D3D9/Direct3D.h | 2 +- GDP/D3D9/Mesh.cpp | 39 +++++++++++++++++++++++++++++++++++ GDP/D3D9/Vertex.h | 14 +++++++++++++ GDP/D3D9/main.cpp | 10 ++++++++- 7 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 GDP/D3D9/Vertex.h diff --git a/GDP/D3D9/D3D9.vcxproj b/GDP/D3D9/D3D9.vcxproj index 8407012..c6a49c3 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/Direct3D.cpp b/GDP/D3D9/Direct3D.cpp index 479281c..a607d2a 100644 --- a/GDP/D3D9/Direct3D.cpp +++ b/GDP/D3D9/Direct3D.cpp @@ -1,6 +1,5 @@ #include "Direct3D.h" #include "Utilities.h" -#include INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen) { @@ -49,23 +48,15 @@ INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen) return 0; } -void Direct3D::beginScene() +void Direct3D::beginScene(BYTE red, BYTE green, BYTE blue) { _pD3DDevice->BeginScene(); // init a frame, after this call we are allowed to do draw calls // change background color - static std::default_random_engine e; - static std::uniform_int_distribution d(0, 255); - _pD3DDevice->Clear( 0, nullptr, // areas to clear, null -> everything D3DCLEAR_TARGET, // what to clear, bitmask - - // how to clean - //D3DCOLOR_XRGB(d(e), d(e), d(e)), //random color - //D3DCOLOR_XRGB(255, 0, 0), // red color - D3DCOLOR_XRGB(0, 0, 0), // black color - 1.0f, 0xFFFFFFFF + D3DCOLOR_XRGB(red, green, blue), 1.0f, 0xFFFFFFFF // how to clean ); } diff --git a/GDP/D3D9/Direct3D.h b/GDP/D3D9/Direct3D.h index eee05a9..0bca579 100644 --- a/GDP/D3D9/Direct3D.h +++ b/GDP/D3D9/Direct3D.h @@ -7,7 +7,7 @@ class Direct3D { public: INT init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen); - void beginScene(); + void beginScene(BYTE red, BYTE green, BYTE blue); void endScene(); void deInit(); diff --git a/GDP/D3D9/Mesh.cpp b/GDP/D3D9/Mesh.cpp index 443ec79..7b6e54d 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,13 @@ void Mesh::update(FLOAT dt) void Mesh::render(IDirect3DDevice9* pD3DDevice) { + // 1. set mesh data + pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride); + pD3DDevice->SetFVF(FVF); + + // 2. draw call + // vertex buffer only + pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, _vertexCount / 3); } void Mesh::deInit() @@ -30,6 +38,37 @@ void Mesh::deInit() UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice) { + // 1. define mesh parameters + _vertexCount = 3; + _vertexStride = sizeof(Vertex); + + // 2. create vertex buffer object WITHOUT data + HRESULT hr = pD3DDevice->CreateVertexBuffer( + _vertexCount * _vertexStride, // vertex buffer size in bytes + D3DUSAGE_WRITEONLY, // how to access the buffer + FVF, // FVF - Flexible Vertex Format + D3DPOOL_MANAGED, // video memory management + &_pVertexBuffer, // vertex buffer object + nullptr // not used anymore + ); + CheckFailed(hr, 300); + + // 3. set vertex data + Vertex* pVertices = nullptr; + + hr = _pVertexBuffer->Lock(0, 0, reinterpret_cast(&pVertices), 0); // transfer vertex buffer from video memory to cpu memory + CheckFailed(hr, 310); + + // fill vertex buffer with data + 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); + + hr = _pVertexBuffer->Unlock(); // transfer vertex buffer back from cpu memory to video memory + CheckFailed(hr, 320); + + pVertices = nullptr; + return 0; } diff --git a/GDP/D3D9/Vertex.h b/GDP/D3D9/Vertex.h new file mode 100644 index 0000000..2b87067 --- /dev/null +++ b/GDP/D3D9/Vertex.h @@ -0,0 +1,14 @@ +#pragma once +#include + +#define FVF D3DFVF_XYZ + +struct Vertex +{ + 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 diff --git a/GDP/D3D9/main.cpp b/GDP/D3D9/main.cpp index cc20ea5..e94cea6 100644 --- a/GDP/D3D9/main.cpp +++ b/GDP/D3D9/main.cpp @@ -1,4 +1,5 @@ #include +#include #include "Window.h" #include "Direct3D.h" #include "Mesh.h" @@ -19,6 +20,9 @@ int WINAPI WinMain( const UINT width = 640; const UINT height = 480; const BOOL isFullscreen = FALSE; + const BYTE red = 255; + const BYTE green = 255; + const BYTE blue = 255; UINT error = 0; // error code for identifying errors @@ -67,7 +71,11 @@ int WINAPI WinMain( light.update(time.getDeltaTime()); // 8.2. render objects - d3d.beginScene(); + static std::default_random_engine e; + static std::uniform_int_distribution d(0, 255); + + //d3d.beginScene(d(e), d(e), d(e)); //random color + d3d.beginScene(red, green, blue); light.render(d3d.getDevice()); material.render(d3d.getDevice());