Add D3D11 project
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
#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);
|
||||
|
||||
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 rotY = 0.0f;
|
||||
static float rotZ = 0.0f;
|
||||
|
||||
rotY += 0.5f * 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(reinterpret_cast<XMFLOAT4X4*>(&_worldMatrix), scale * rotation * translation);
|
||||
}
|
||||
|
||||
void Mesh::render(IDirect3DDevice9* pD3DDevice)
|
||||
{
|
||||
// setup pipeline
|
||||
pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride);
|
||||
pD3DDevice->SetIndices(_pIndexBuffer);
|
||||
pD3DDevice->SetFVF(FVF);
|
||||
pD3DDevice->SetTransform(D3DTS_WORLD, &_worldMatrix);
|
||||
|
||||
// draw call
|
||||
pD3DDevice->DrawIndexedPrimitive(
|
||||
D3DPT_TRIANGLELIST, // primitive type
|
||||
0, 0, // vertex buffer offsets
|
||||
_vertexCount,
|
||||
0, // index buffer offset
|
||||
_indexCount / 3 // primitive count
|
||||
);
|
||||
}
|
||||
|
||||
void Mesh::deInit()
|
||||
{
|
||||
safeRelease(_pIndexBuffer);
|
||||
safeRelease(_pVertexBuffer);
|
||||
}
|
||||
|
||||
UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||
{
|
||||
// define values
|
||||
_vertexCount = 4;
|
||||
_vertexStride = sizeof(Vertex);
|
||||
|
||||
// describe & create vertex buffer
|
||||
HRESULT hr = pD3DDevice->CreateVertexBuffer(
|
||||
_vertexCount * _vertexStride, // total size of content
|
||||
D3DUSAGE_WRITEONLY, // read and/or write access
|
||||
FVF, // FVF - Flexible Vertex Format -> vertex structure
|
||||
D3DPOOL_MANAGED, // memmory management
|
||||
&_pVertexBuffer,
|
||||
nullptr // deprecated, always nullptr
|
||||
);
|
||||
CheckFailed(hr, 300);
|
||||
|
||||
// populate vertex buffer
|
||||
Vertex* pVertices = nullptr;
|
||||
hr = _pVertexBuffer->Lock(
|
||||
0, 0, // offset & size to lock, here everything
|
||||
reinterpret_cast<void**>(&pVertices),
|
||||
0 // optional flags
|
||||
);
|
||||
CheckFailed(hr, 310);
|
||||
|
||||
// triangle
|
||||
//pVertices[0] = Vertex(0.0f, 1.0f, 0.0f);
|
||||
//pVertices[1] = Vertex(1.0f, -1.0f, 0.0f);
|
||||
//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);
|
||||
|
||||
// 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);
|
||||
|
||||
// quad with uvs & normals
|
||||
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);
|
||||
|
||||
hr = _pVertexBuffer->Unlock();
|
||||
CheckFailed(hr, 320);
|
||||
|
||||
pVertices = nullptr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||
{
|
||||
_indexCount = 6;
|
||||
|
||||
HRESULT hr = pD3DDevice->CreateIndexBuffer(_indexCount * sizeof(unsigned short),
|
||||
D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &_pIndexBuffer, nullptr);
|
||||
CheckFailed(hr, 330);
|
||||
|
||||
unsigned short* pIndices = nullptr;
|
||||
hr = _pIndexBuffer->Lock(0, 0, reinterpret_cast<void**>(&pIndices), 0);
|
||||
CheckFailed(hr, 340);
|
||||
|
||||
// triangle
|
||||
//pIndices[0] = 0;
|
||||
//pIndices[1] = 1;
|
||||
//pIndices[2] = 2;
|
||||
|
||||
// quad
|
||||
pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
|
||||
pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;
|
||||
|
||||
hr = _pIndexBuffer->Unlock();
|
||||
CheckFailed(hr, 350);
|
||||
|
||||
pIndices = nullptr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user