Files
hwr2023_b/GDP/D3D9/Mesh.cpp
T
2025-11-19 09:20:20 +01:00

226 lines
7.1 KiB
C++

#include "Mesh.h"
#include "Utilities.h"
#include "Vertex.h"
#include <DirectXMath.h>
using namespace DirectX;
UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
{
UINT error = 0;
error = createVertexBuffer(pD3DDevice);
CheckError(error);
error = createIndexBuffer(pD3DDevice);
CheckError(error);
// identity world matrix
XMMATRIX identityMatrix = XMMatrixIdentity();
XMFLOAT4X4 worldMatrix;
XMStoreFloat4x4(&worldMatrix, identityMatrix);
D3DMATRIX* pWorldMatrix = reinterpret_cast<D3DMATRIX*>(&worldMatrix);
_worldMatrix = *pWorldMatrix;
return 0;
}
void Mesh::update(FLOAT dt)
{
static FLOAT x = 0.0f;
static FLOAT y = 0.0f;
static FLOAT z = 0.0f;
static FLOAT pitch = 0.0f; // x axis
static FLOAT yaw = 0.0f; // y axis
static FLOAT roll = 0.0f; // z axis
static FLOAT scale = 1.0f;
//yaw += 1.0f * dt;
//scale += 0.001f * 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('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);
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(&_worldMatrix), scaleMatrix * rotationMatrix * translationMatrix);
}
void Mesh::render(IDirect3DDevice9* pD3DDevice)
{
// 1. set mesh data
pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride);
pD3DDevice->SetFVF(FVF);
pD3DDevice->SetIndices(_pIndexBuffer);
// 2. set transform data
pD3DDevice->SetTransform(D3DTS_WORLD, &_worldMatrix);
// 3. draw call
// vertex buffer only
//pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, _vertexCount / 3);
//pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, _vertexCount - 2);
//pD3DDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, _vertexCount - 2);
// vertex & index buffer
pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, _vertexCount, 0, _indexCount / 3);
}
void Mesh::deInit()
{
safeRelease(_pIndexBuffer);
safeRelease(_pVertexBuffer);
}
UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
{
// 1. define mesh parameters
_vertexCount = 4;
_vertexStride = sizeof(Vertex);
// 2. create vertex buffer object WITHOUT data
HRESULT hr = pD3DDevice->CreateVertexBuffer(
_vertexCount * _vertexStride, // vertex buffer size in bytes
D3DUSAGE_WRITEONLY, // how to access the buffer
FVF, // FVF - Flexible Vertex Format
D3DPOOL_MANAGED, // video memory management
&_pVertexBuffer, // vertex buffer object
nullptr // not used anymore
);
CheckFailed(hr, 300);
// 3. set vertex data
Vertex* pVertices = nullptr;
hr = _pVertexBuffer->Lock(0, 0, reinterpret_cast<void**>(&pVertices), 0); // transfer vertex buffer from video memory to cpu memory
CheckFailed(hr, 310);
// fill vertex buffer with data
//// triangle with triangle list
//pVertices[0] = Vertex(0.0f, 0.5f, 0.0f);
//pVertices[1] = Vertex(0.5f, -0.5f, 0.0f);
//pVertices[2] = Vertex(-0.5f, -0.5f, 0.0f);
// quad with triangle list, vertex buffer only
//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[4] = Vertex(0.5f, -0.5f, 0.0f);
//pVertices[5] = Vertex(-0.5f, -0.5f, 0.0f);
//// quad with triangle strip, vertex buffer only
//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 triangle fan, vertex buffer only
// quad with triangle list & index buffer
pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
pVertices[1] = Vertex(0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
pVertices[2] = Vertex(0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
//// cube with color
//// front
//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);
//// 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);
// cube with uv
//// front
//pVertices[0] = Vertex(-0.5f, 0.5f, -0.5f, 0.0f, 0.0f);
//pVertices[1] = Vertex(0.5f, 0.5f, -0.5f, 1.0f, 0.0f);
//pVertices[2] = Vertex(0.5f, -0.5f, -0.5f, 1.0f, 1.0f);
//pVertices[3] = Vertex(-0.5f, -0.5f, -0.5f, 0.0f, 1.0f);
//// back
//pVertices[4] = Vertex(0.5f, 0.5f, 0.5f, 0.0f, 0.0f);
//pVertices[5] = Vertex(-0.5f, 0.5f, 0.5f, 3.0f, 0.0f);
//pVertices[6] = Vertex(-0.5f, -0.5f, 0.5f, 3.0f, 3.0f);
//pVertices[7] = Vertex(0.5f, -0.5f, 0.5f, 0.0f, 3.0f);
hr = _pVertexBuffer->Unlock(); // transfer vertex buffer back from cpu memory to video memory
CheckFailed(hr, 320);
pVertices = nullptr;
return 0;
}
UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice)
{
_indexCount = 6; // triangle count * 3
HRESULT hr = pD3DDevice->CreateIndexBuffer(
_indexCount * sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&_pIndexBuffer,
nullptr
);
CheckFailed(hr, 330);
WORD* pIndices = nullptr;
hr = _pIndexBuffer->Lock(0, 0, reinterpret_cast<void**>(&pIndices), 0);
CheckFailed(hr, 340);
// quad
// triangle 1 - 0, 1, 2
pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
// triangle 2 - 0, 2, 3
pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;
//// cube
//// front
//pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
//pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;
//// back
//pIndices[6] = 4; pIndices[7] = 5; pIndices[8] = 6;
//pIndices[9] = 4; pIndices[10] = 6; pIndices[11] = 7;
//// left
//pIndices[12] = 5; pIndices[13] = 0; pIndices[14] = 3;
//pIndices[15] = 5; pIndices[16] = 3; pIndices[17] = 6;
//// right
//pIndices[18] = 1; pIndices[19] = 4; pIndices[20] = 7;
//pIndices[21] = 1; pIndices[22] = 7; pIndices[23] = 2;
//// top
//pIndices[24] = 5; pIndices[25] = 4; pIndices[26] = 1;
//pIndices[27] = 5; pIndices[28] = 1; pIndices[29] = 0;
//// bottom
//pIndices[30] = 3; pIndices[31] = 2; pIndices[32] = 7;
//pIndices[33] = 3; pIndices[34] = 7; pIndices[35] = 6;
hr = _pIndexBuffer->Unlock();
CheckFailed(hr, 350);
pIndices = nullptr;
return 0;
}