130 lines
3.6 KiB
C++
130 lines
3.6 KiB
C++
#include "Mesh.h"
|
|
#include "Utilities.h"
|
|
#include "Vertex.h"
|
|
#include <DirectXMath.h>
|
|
|
|
using namespace DirectX;
|
|
|
|
UINT Mesh::init(ID3D11Device* pD3DDevice)
|
|
{
|
|
UINT error = 0;
|
|
|
|
error = createVertexBuffer(pD3DDevice);
|
|
CheckError(error);
|
|
|
|
error = createIndexBuffer(pD3DDevice);
|
|
CheckError(error);
|
|
|
|
// create world matrix
|
|
XMStoreFloat4x4(&_worldMatrix, XMMatrixIdentity());
|
|
|
|
return 0;
|
|
}
|
|
|
|
void Mesh::update(FLOAT dt)
|
|
{
|
|
static FLOAT x = 0.0f;
|
|
static FLOAT y = 0.0f;
|
|
static FLOAT z = 0.0f;
|
|
static FLOAT rot = 0.0f;
|
|
rot += 1.0f * dt;
|
|
|
|
FLOAT move = 1.0f * dt;
|
|
|
|
if ((GetAsyncKeyState(VK_LEFT) & 0x8000) || (GetAsyncKeyState('A') & 0x8000)) x -= move;
|
|
if ((GetAsyncKeyState(VK_RIGHT) & 0x8000) || (GetAsyncKeyState('D') & 0x8000)) x += move;
|
|
if ((GetAsyncKeyState(VK_UP) & 0x8000) || (GetAsyncKeyState('W') & 0x8000)) y += move;
|
|
if ((GetAsyncKeyState(VK_DOWN) & 0x8000) || (GetAsyncKeyState('S') & 0x8000)) y -= move;
|
|
if ((GetAsyncKeyState(VK_ADD) & 0x8000) || (GetAsyncKeyState('Q') & 0x8000)) z -= move;
|
|
if ((GetAsyncKeyState(VK_SUBTRACT) & 0x8000) || (GetAsyncKeyState('E') & 0x8000)) z += move;
|
|
|
|
XMMATRIX translation = XMMatrixTranslation(x, y, z);
|
|
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(rot, 0.0f, rot);
|
|
XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
|
|
|
|
XMVECTOR det = XMMatrixDeterminant(translation);
|
|
|
|
XMStoreFloat4x4(&_worldMatrix, scale * rotation * translation);
|
|
}
|
|
|
|
void Mesh::render(ID3D11DeviceContext* pD3DDeviceContext)
|
|
{
|
|
// set data
|
|
static UINT offset = 0;
|
|
pD3DDeviceContext->IASetVertexBuffers(0, 1, &_pVertexBuffer, &_vertexStride, &offset);
|
|
pD3DDeviceContext->IASetIndexBuffer(_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0);
|
|
pD3DDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
|
|
|
// draw
|
|
pD3DDeviceContext->DrawIndexed(_indexCount, 0, 0);
|
|
}
|
|
|
|
void Mesh::deInit()
|
|
{
|
|
safeRelease(_pIndexBuffer);
|
|
safeRelease(_pVertexBuffer);
|
|
}
|
|
|
|
UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice)
|
|
{
|
|
// describe vertex buffer content
|
|
//// quad with color
|
|
//Vertex vertices[] = { Vertex(-0.5f, 0.5f, 0.0f, 255, 0, 0),
|
|
//Vertex(0.5f, 0.5f, 0.0f, 0, 255, 0),
|
|
//Vertex(0.5f, -0.5f, 0.0f, 255, 0, 255),
|
|
//Vertex(-0.5f, -0.5f, 0.0f, 0, 0, 255) };
|
|
|
|
|
|
// quad with uv & normal
|
|
Vertex vertices[] = {
|
|
Vertex(-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f),
|
|
Vertex(0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f),
|
|
Vertex(0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f),
|
|
Vertex(-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f)
|
|
};
|
|
|
|
// general data
|
|
_vertexCount = std::size(vertices);
|
|
_vertexStride = sizeof(Vertex);
|
|
|
|
// create vertex buffer object
|
|
D3D11_BUFFER_DESC desc = {};
|
|
desc.ByteWidth = _vertexCount * _vertexStride; // size of vertex buffer in bytes
|
|
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
|
desc.Usage = D3D11_USAGE_IMMUTABLE; // https://learn.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage
|
|
|
|
D3D11_SUBRESOURCE_DATA data = {};
|
|
data.pSysMem = vertices;
|
|
|
|
HRESULT hr = pD3DDevice->CreateBuffer(&desc, &data, &_pVertexBuffer);
|
|
CheckFailed(hr, 30);
|
|
|
|
return 0;
|
|
}
|
|
|
|
UINT Mesh::createIndexBuffer(ID3D11Device* pD3DDevice)
|
|
{
|
|
// describe index buffer content
|
|
WORD indices[] = {
|
|
0,1,2,
|
|
0,2,3
|
|
};
|
|
|
|
// general data
|
|
_indexCount = std::size(indices);
|
|
|
|
// create index buffer object
|
|
D3D11_BUFFER_DESC desc = {};
|
|
desc.ByteWidth = _indexCount * sizeof(WORD); // size of index buffer in bytes
|
|
desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
|
desc.Usage = D3D11_USAGE_IMMUTABLE;
|
|
|
|
D3D11_SUBRESOURCE_DATA data = {};
|
|
data.pSysMem = indices;
|
|
|
|
HRESULT hr = pD3DDevice->CreateBuffer(&desc, &data, &_pIndexBuffer);
|
|
CheckFailed(hr, 32);
|
|
|
|
return 0;
|
|
}
|