Add transformation

Add input
Add time
Add vertex colors
This commit is contained in:
2026-11-16 12:10:00 +01:00
parent 17b18859b3
commit b84f9c1052
7 changed files with 102 additions and 10 deletions
+44 -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,40 @@ 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);
XMFLOAT4X4 w;
XMStoreFloat4x4(&w, scale * rotation * translation);
_worldMatrix = *reinterpret_cast<D3DMATRIX*>(&w);
}
void Mesh::render(IDirect3DDevice9* pD3DDevice)
@@ -25,6 +58,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 +108,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 colors
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, 0, 0, 255);
pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 255, 255, 0);
hr = _pVertexBuffer->Unlock();
CheckFailed(hr, 320);