From 439871d63f9d6c511a65f1f2b3fc2a9ba676b0c7 Mon Sep 17 00:00:00 2001 From: David Hackbarth Date: Mon, 16 Nov 2026 15:56:44 +0100 Subject: [PATCH] Add transformations Add input Add time Add vertex colors --- GDP/D3D9/Camera.cpp | 28 +++++++++++++++++++++++++++- GDP/D3D9/Camera.h | 2 +- GDP/D3D9/Mesh.cpp | 45 +++++++++++++++++++++++++++++++++++++++++---- GDP/D3D9/Time.cpp | 14 ++++++++++++++ GDP/D3D9/Time.h | 5 +++++ GDP/D3D9/Vertex.h | 10 +++++++--- GDP/D3D9/main.cpp | 4 +++- 7 files changed, 98 insertions(+), 10 deletions(-) diff --git a/GDP/D3D9/Camera.cpp b/GDP/D3D9/Camera.cpp index 882aac6..81b4d2c 100644 --- a/GDP/D3D9/Camera.cpp +++ b/GDP/D3D9/Camera.cpp @@ -1,7 +1,33 @@ #include "Camera.h" -UINT Camera::init(UINT width, UINT height) +#include + +using namespace DirectX; + +UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height) { + // view matrix + XMVECTOR position = XMVectorSet(0.0f, 0.0f, -2.0f, 0.0f); + XMVECTOR forward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); + XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); + + XMMATRIX lookTo = XMMatrixLookToLH(position, forward, up); + XMFLOAT4X4 v; + XMStoreFloat4x4(&v, lookTo); + _viewMatrix = *reinterpret_cast(&v); + + // projection matrix + float fov = XMConvertToRadians(60.0f); + float aspectRatio = static_cast(width) / static_cast(height); + float nearClippingPlane = 0.3f; + float farClippingPlane = 100.0f; + + XMMATRIX perspective = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane); + XMStoreFloat4x4(reinterpret_cast(&_projectionMatrix), perspective); + + pD3DDevice->SetTransform(D3DTS_VIEW, &_viewMatrix); + pD3DDevice->SetTransform(D3DTS_PROJECTION, &_projectionMatrix); + return 0; } diff --git a/GDP/D3D9/Camera.h b/GDP/D3D9/Camera.h index 3cc7e31..2922d26 100644 --- a/GDP/D3D9/Camera.h +++ b/GDP/D3D9/Camera.h @@ -4,7 +4,7 @@ class Camera { public: - UINT init(UINT width, UINT height); + UINT init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height); void update(FLOAT dt); void deInit(); diff --git a/GDP/D3D9/Mesh.cpp b/GDP/D3D9/Mesh.cpp index 244bb30..ec8d117 100644 --- a/GDP/D3D9/Mesh.cpp +++ b/GDP/D3D9/Mesh.cpp @@ -2,6 +2,10 @@ #include "Utilities.h" #include "Vertex.h" +#include + +using namespace DirectX; + UINT Mesh::init(IDirect3DDevice9* pD3DDevice) { UINT error = 0; @@ -12,11 +16,37 @@ UINT Mesh::init(IDirect3DDevice9* pD3DDevice) error = createIndexBuffer(pD3DDevice); CheckError(error); + XMMATRIX identity = XMMatrixIdentity(); + XMFLOAT4X4 w; + XMStoreFloat4x4(&w, identity); + _worldMatrix = *reinterpret_cast(&w); + return 0; } void Mesh::update(FLOAT dt) { + static float x = 0.0f; + static float y = 0.0f; + static float z = 0.0f; + static float rotZ = 0.0f; + + rotZ += 0.0f * dt; + + float move = 1.0f * dt; + + if (GetAsyncKeyState(VK_LEFT) & 0x8000) x -= move; + if (GetAsyncKeyState(VK_RIGHT) & 0x8000) x += move; + if (GetAsyncKeyState(VK_DOWN) & 0x8000) y -= move; + if (GetAsyncKeyState(VK_UP) & 0x8000) y += move; + if (GetAsyncKeyState(VK_SUBTRACT) & 0x8000) z -= move; + if (GetAsyncKeyState(VK_ADD) & 0x8000) z += move; + + XMMATRIX translation = XMMatrixTranslation(x, y, z); + XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, rotZ); + XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f); + + XMStoreFloat4x4(reinterpret_cast(&_worldMatrix), scale * rotation * translation); } void Mesh::render(IDirect3DDevice9* pD3DDevice) @@ -25,6 +55,7 @@ void Mesh::render(IDirect3DDevice9* pD3DDevice) pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride); pD3DDevice->SetIndices(_pIndexBuffer); pD3DDevice->SetFVF(FVF); + pD3DDevice->SetTransform(D3DTS_WORLD, &_worldMatrix); // draw call pD3DDevice->DrawIndexedPrimitive( @@ -74,10 +105,16 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice) //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); + //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 color + pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f, 255, 0, 0); + pVertices[1] = Vertex(0.5f, 0.5f, 0.0f, 0, 255, 0); + pVertices[2] = Vertex(0.5f, -0.5f, 0.0f, 255, 0, 255); + pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 0, 0, 255); hr = _pVertexBuffer->Unlock(); CheckFailed(hr, 320); diff --git a/GDP/D3D9/Time.cpp b/GDP/D3D9/Time.cpp index dc27e1b..3b36343 100644 --- a/GDP/D3D9/Time.cpp +++ b/GDP/D3D9/Time.cpp @@ -1,12 +1,26 @@ #include "Time.h" +#include + UINT Time::init() { + _lastFrame = CLOCK::now(); + return 0; } void Time::update() { + TIMEPOINT now = CLOCK::now(); + SECONDS diff = now - _lastFrame; + _deltaTime = diff.count(); + _lastFrame = now; + +#if _DEBUG && UNICODE + wstring output = L"fps: " + to_wstring(1.0f / _deltaTime) + L"\n"; + OutputDebugString(output.c_str()); +#endif // _DEBUG && UNICODE + } void Time::deInit() diff --git a/GDP/D3D9/Time.h b/GDP/D3D9/Time.h index f277636..abf50a8 100644 --- a/GDP/D3D9/Time.h +++ b/GDP/D3D9/Time.h @@ -4,6 +4,10 @@ using namespace std; +typedef chrono::steady_clock CLOCK; +typedef chrono::duration SECONDS; +typedef CLOCK::time_point TIMEPOINT; + class Time { public: @@ -15,5 +19,6 @@ public: private: FLOAT _deltaTime = 0.0f; + TIMEPOINT _lastFrame = {}; }; diff --git a/GDP/D3D9/Vertex.h b/GDP/D3D9/Vertex.h index b2e1809..00979b6 100644 --- a/GDP/D3D9/Vertex.h +++ b/GDP/D3D9/Vertex.h @@ -1,7 +1,7 @@ #pragma once #include -#define FVF D3DFVF_XYZ +#define FVF D3DFVF_XYZ | D3DFVF_DIFFUSE struct Vertex { @@ -10,6 +10,10 @@ struct Vertex 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) {} + // color + D3DCOLOR color; + + Vertex() : x(0.0f), y(0.0f), z(0.0f), color(0xffffffff) {} + Vertex(float x, float y, float z) : x(x), y(y), z(z), color(0xffffffff) {} + Vertex(float x, float y, float z, UINT r, UINT g, UINT b) : x(x), y(y), z(z), color(D3DCOLOR_XRGB(r, g, b)) {} }; \ No newline at end of file diff --git a/GDP/D3D9/main.cpp b/GDP/D3D9/main.cpp index 2410319..495fa7c 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_LIGHTING, FALSE); + // 3. create a mesh Mesh mesh = {}; error = mesh.init(d3d.getDevice()); @@ -39,7 +41,7 @@ int WINAPI WinMain( // 4. create a camera Camera camera = {}; - error = camera.init(width, height); + error = camera.init(d3d.getDevice(), width, height); CheckError(error); // 5. create time