From c9e9dcf5722d0cb82328365a7343fa69bf5dea59 Mon Sep 17 00:00:00 2001 From: David Hackbarth Date: Wed, 24 Sep 2025 11:44:51 +0200 Subject: [PATCH] add input add time add vertex color --- GDP/D3D9/Mesh.cpp | 36 ++++++++++++++++++++++++------------ GDP/D3D9/Time.cpp | 8 ++++++++ GDP/D3D9/Time.h | 5 +++++ GDP/D3D9/Vertex.h | 9 ++++++--- GDP/D3D9/main.cpp | 8 ++++++++ 5 files changed, 51 insertions(+), 15 deletions(-) diff --git a/GDP/D3D9/Mesh.cpp b/GDP/D3D9/Mesh.cpp index 67eb467..518bb3e 100644 --- a/GDP/D3D9/Mesh.cpp +++ b/GDP/D3D9/Mesh.cpp @@ -28,15 +28,27 @@ UINT Mesh::init(IDirect3DDevice9* pD3DDevice) void Mesh::update(FLOAT dt) { + static FLOAT x = 0.0f; + static FLOAT y = 0.0f; + static FLOAT z = 0.0f; static FLOAT pitch = XM_PIDIV4; // x axis static FLOAT yaw = XM_PIDIV4; // y axis static FLOAT roll = XM_PIDIV4; // z axis static FLOAT scale = 1.0f; - yaw += 0.01f; - //scale += 0.001f; + yaw += 1.0f * dt; + //scale += 0.001f * dt; - XMMATRIX translationMatrix = XMMatrixTranslation(0.0f, 0.0f, 0.0f); + 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('E') & 0x8000) z -= move; + if (GetAsyncKeyState('Q') & 0x8000) z += move; + + XMMATRIX translationMatrix = XMMatrixTranslation(x, y, z); XMMATRIX scaleMatrix = XMMatrixScaling(scale, scale, scale); XMMATRIX rotationMatrix = XMMatrixRotationRollPitchYaw(pitch, yaw, roll); @@ -121,16 +133,16 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice) // cube // front - pVertices[0] = Vertex(-0.5f, 0.5f, -0.5f); - pVertices[1] = Vertex(0.5f, 0.5f, -0.5f); - pVertices[2] = Vertex(0.5f, -0.5f, -0.5f); - pVertices[3] = Vertex(-0.5f, -0.5f, -0.5f); + pVertices[0] = Vertex(-0.5f, 0.5f, -0.5f, 255, 0, 0); + pVertices[1] = Vertex(0.5f, 0.5f, -0.5f, 0, 255, 0); + pVertices[2] = Vertex(0.5f, -0.5f, -0.5f, 0, 0, 255); + pVertices[3] = Vertex(-0.5f, -0.5f, -0.5f, 255, 0, 255); - // front - pVertices[4] = Vertex(0.5f, 0.5f, 0.5f); - pVertices[5] = Vertex(-0.5f, 0.5f, 0.5f); - pVertices[6] = Vertex(-0.5f, -0.5f, 0.5f); - pVertices[7] = Vertex(0.5f, -0.5f, 0.5f); + // back + pVertices[4] = Vertex(0.5f, 0.5f, 0.5f, 255, 255, 0); + pVertices[5] = Vertex(-0.5f, 0.5f, 0.5f, 0, 255, 255); + pVertices[6] = Vertex(-0.5f, -0.5f, 0.5f, 0, 0, 0); + pVertices[7] = Vertex(0.5f, -0.5f, 0.5f, 200, 200, 200); hr = _pVertexBuffer->Unlock(); // transfer vertex buffer back from cpu memory to video memory CheckFailed(hr, 320); diff --git a/GDP/D3D9/Time.cpp b/GDP/D3D9/Time.cpp index dc27e1b..fa007c4 100644 --- a/GDP/D3D9/Time.cpp +++ b/GDP/D3D9/Time.cpp @@ -2,11 +2,19 @@ UINT Time::init() { + _lastFrame = CLOCK::now(); + return 0; } void Time::update() { + TIMEPOINT now = CLOCK::now(); + SECONDS diff = now - _lastFrame; + _lastFrame = now; + _deltaTime = diff.count(); + + } void Time::deInit() diff --git a/GDP/D3D9/Time.h b/GDP/D3D9/Time.h index f277636..a6b84d6 100644 --- a/GDP/D3D9/Time.h +++ b/GDP/D3D9/Time.h @@ -4,6 +4,10 @@ using namespace std; +typedef chrono::steady_clock CLOCK; +typedef CLOCK::time_point TIMEPOINT; +typedef chrono::duration SECONDS; + 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 2b87067..04f4bf6 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 { @@ -9,6 +9,9 @@ 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) {} + 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 23b8b90..4075ab3 100644 --- a/GDP/D3D9/main.cpp +++ b/GDP/D3D9/main.cpp @@ -38,6 +38,7 @@ int WINAPI WinMain( // set render states d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); + d3d.getDevice()->SetRenderState(D3DRS_LIGHTING, FALSE); // 3. create a mesh Mesh mesh = {}; @@ -67,6 +68,13 @@ int WINAPI WinMain( // 8. run application while (window.run()) { + if (GetAsyncKeyState(VK_F1) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); + if (GetAsyncKeyState(VK_F2) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); + if (GetAsyncKeyState(VK_F3) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_POINT); + if (GetAsyncKeyState(VK_F4) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); + if (GetAsyncKeyState(VK_F5) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); + if (GetAsyncKeyState(VK_F6) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); + // 8.1. update objects time.update(); mesh.update(time.getDeltaTime());