Add transformations

Add input
Add time
Add vertex colors
This commit is contained in:
2026-11-16 15:56:44 +01:00
parent 948c5e067e
commit 439871d63f
7 changed files with 98 additions and 10 deletions
+27 -1
View File
@@ -1,7 +1,33 @@
#include "Camera.h"
UINT Camera::init(UINT width, UINT height)
#include <DirectXMath.h>
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<D3DMATRIX*>(&v);
// projection matrix
float fov = XMConvertToRadians(60.0f);
float aspectRatio = static_cast<float>(width) / static_cast<float>(height);
float nearClippingPlane = 0.3f;
float farClippingPlane = 100.0f;
XMMATRIX perspective = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane);
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(&_projectionMatrix), perspective);
pD3DDevice->SetTransform(D3DTS_VIEW, &_viewMatrix);
pD3DDevice->SetTransform(D3DTS_PROJECTION, &_projectionMatrix);
return 0;
}
+1 -1
View File
@@ -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();
+41 -4
View File
@@ -2,6 +2,10 @@
#include "Utilities.h"
#include "Vertex.h"
#include <DirectXMath.h>
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<D3DMATRIX*>(&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<XMFLOAT4X4*>(&_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);
+14
View File
@@ -1,12 +1,26 @@
#include "Time.h"
#include <string>
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()
+5
View File
@@ -4,6 +4,10 @@
using namespace std;
typedef chrono::steady_clock CLOCK;
typedef chrono::duration<FLOAT> SECONDS;
typedef CLOCK::time_point TIMEPOINT;
class Time
{
public:
@@ -15,5 +19,6 @@ public:
private:
FLOAT _deltaTime = 0.0f;
TIMEPOINT _lastFrame = {};
};
+7 -3
View File
@@ -1,7 +1,7 @@
#pragma once
#include <d3d9.h>
#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)) {}
};
+3 -1
View File
@@ -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