From 53cf8b96a349b5e3100e409e3acec8e0456dc822 Mon Sep 17 00:00:00 2001 From: David Hackbarth Date: Wed, 17 Sep 2025 16:08:23 +0200 Subject: [PATCH] add world matrix add view matrix add projection matrix --- GDP/D3D9/Camera.cpp | 25 ++++++++++++++++++++++++- GDP/D3D9/Camera.h | 2 +- GDP/D3D9/Mesh.cpp | 12 ++++++++++++ GDP/D3D9/main.cpp | 2 +- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/GDP/D3D9/Camera.cpp b/GDP/D3D9/Camera.cpp index 882aac6..6f2139c 100644 --- a/GDP/D3D9/Camera.cpp +++ b/GDP/D3D9/Camera.cpp @@ -1,12 +1,35 @@ #include "Camera.h" +#include + +using namespace DirectX; + UINT Camera::init(UINT width, UINT height) { + // view matrix + XMVECTOR position = XMVectorSet(0.0f, 0.0f, -2.0f, 0.0f); // camera position + XMVECTOR forward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); // camera forward vector + XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); // camera up vector + + XMMATRIX viewMatrix = XMMatrixLookToLH(position, forward, up); + XMStoreFloat4x4(reinterpret_cast(&_viewMatrix), viewMatrix); + + // projection matrix + FLOAT fov = XM_PI * 0.3333333f; // field of view + FLOAT aspectRatio = static_cast(width) / static_cast(height); // CAUTION: UINT / UINT => UINT + FLOAT nearClippingPlane = 0.3f; + FLOAT farClippingPlane = 100.0f; + + XMMATRIX projectionMatrix = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane); + XMStoreFloat4x4(reinterpret_cast(&_projectionMatrix), projectionMatrix); + return 0; } -void Camera::update(FLOAT dt) +void Camera::update(FLOAT dt, IDirect3DDevice9* pD3DDevice) { + pD3DDevice->SetTransform(D3DTS_VIEW, &_viewMatrix); + pD3DDevice->SetTransform(D3DTS_PROJECTION, &_projectionMatrix); } void Camera::deInit() diff --git a/GDP/D3D9/Camera.h b/GDP/D3D9/Camera.h index 3cc7e31..9227580 100644 --- a/GDP/D3D9/Camera.h +++ b/GDP/D3D9/Camera.h @@ -5,7 +5,7 @@ class Camera { public: UINT init(UINT width, UINT height); - void update(FLOAT dt); + void update(FLOAT dt, IDirect3DDevice9* pD3DDevice); void deInit(); private: diff --git a/GDP/D3D9/Mesh.cpp b/GDP/D3D9/Mesh.cpp index 970568d..a40bc28 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,6 +16,13 @@ UINT Mesh::init(IDirect3DDevice9* pD3DDevice) error = createIndexBuffer(pD3DDevice); CheckError(error); + // identity matrix + XMMATRIX identityMatrix = XMMatrixIdentity(); + XMFLOAT4X4 worldMatrix; + XMStoreFloat4x4(&worldMatrix, identityMatrix); + D3DMATRIX* pWorldMatrix = reinterpret_cast(&worldMatrix); + _worldMatrix = *pWorldMatrix; + return 0; } @@ -25,6 +36,7 @@ void Mesh::render(IDirect3DDevice9* pD3DDevice) pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride); pD3DDevice->SetFVF(FVF); pD3DDevice->SetIndices(_pIndexBuffer); + pD3DDevice->SetTransform(D3DTS_WORLD, &_worldMatrix); // 2. draw call //pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, _vertexCount / 3); diff --git a/GDP/D3D9/main.cpp b/GDP/D3D9/main.cpp index bd13e87..3900a51 100644 --- a/GDP/D3D9/main.cpp +++ b/GDP/D3D9/main.cpp @@ -65,7 +65,7 @@ int WINAPI WinMain( // 8.1. update objects time.update(); mesh.update(time.getDeltaTime()); - camera.update(time.getDeltaTime()); + camera.update(time.getDeltaTime(), d3d.getDevice()); light.update(time.getDeltaTime()); // 8.2. render objects