122 lines
3.5 KiB
C++
122 lines
3.5 KiB
C++
#include "Mesh.h"
|
|
#include "Utilities.h"
|
|
#include "Vertex.h"
|
|
|
|
UINT Mesh::init(ID3D11Device* pD3DDevice)
|
|
{
|
|
UINT error = 0;
|
|
|
|
error = createVertexBuffer(pD3DDevice);
|
|
CheckError(error);
|
|
|
|
error = createIndexBuffer(pD3DDevice);
|
|
CheckError(error);
|
|
|
|
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 rotY = 0.0f;
|
|
static float rotZ = 0.0f;
|
|
|
|
rotY += 1.0f * dt;
|
|
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) || (GetAsyncKeyState('Q') & 0x8000)) z -= move;
|
|
if ((GetAsyncKeyState(VK_ADD) & 0x8000) || (GetAsyncKeyState('E') & 0x8000)) z += move;
|
|
|
|
XMMATRIX translation = XMMatrixTranslation(x, y, z);
|
|
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, rotY, rotZ);
|
|
XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
|
|
|
|
XMStoreFloat4x4(&_worldMatrix, scale * rotation * translation);
|
|
}
|
|
|
|
void Mesh::render(ID3D11DeviceContext* pD3DDeviceContext)
|
|
{
|
|
// setup pipeline
|
|
UINT offset = 0;
|
|
pD3DDeviceContext->IASetVertexBuffers(
|
|
0, // which vertex buffer we want to set (Startslot)
|
|
1, &_pVertexBuffer, // buffers to set
|
|
&_vertexStride, // vertex size for each buffer
|
|
&offset // offset of vertices to start from for each buffer
|
|
);
|
|
|
|
pD3DDeviceContext->IASetIndexBuffer(_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0);
|
|
pD3DDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
|
|
|
// draw call
|
|
pD3DDeviceContext->DrawIndexed(_indexCount, 0, 0);
|
|
}
|
|
|
|
void Mesh::deInit()
|
|
{
|
|
safeRelease(_pIndexBuffer);
|
|
safeRelease(_pVertexBuffer);
|
|
}
|
|
|
|
UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice)
|
|
{
|
|
// specify values
|
|
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),
|
|
};
|
|
|
|
_vertexCount = std::size(vertices);
|
|
_vertexStride = sizeof(Vertex);
|
|
|
|
// describe & create vertex buffer
|
|
D3D11_BUFFER_DESC desc = {};
|
|
desc.ByteWidth = _vertexCount* _vertexStride; // total content size
|
|
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; // buffer type
|
|
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, 300);
|
|
|
|
return 0;
|
|
}
|
|
|
|
UINT Mesh::createIndexBuffer(ID3D11Device* pD3DDevice)
|
|
{
|
|
unsigned short indices[] =
|
|
{
|
|
0, 1, 2,
|
|
0, 2, 3
|
|
};
|
|
_indexCount = std::size(indices);
|
|
|
|
D3D11_BUFFER_DESC desc = {};
|
|
desc.ByteWidth = _indexCount * sizeof(unsigned short); // total content size
|
|
desc.BindFlags = D3D11_BIND_INDEX_BUFFER; // buffer type
|
|
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 = indices;
|
|
|
|
HRESULT hr = pD3DDevice->CreateBuffer(&desc, &data, &_pIndexBuffer);
|
|
CheckFailed(hr, 320);
|
|
|
|
return 0;
|
|
}
|