#include "Mesh.h" #include "Utilities.h" #include "Vertex.h" #include using namespace DirectX; UINT Mesh::init(IDirect3DDevice9* pD3DDevice) { UINT error = 0; error = createVertexBuffer(pD3DDevice); CheckError(error); error = createIndexBuffer(pD3DDevice); CheckError(error); // identity matrix XMMATRIX identityMatrix = XMMatrixIdentity(); XMFLOAT4X4 worldMatrix; XMStoreFloat4x4(&worldMatrix, identityMatrix); D3DMATRIX* pWorldMatrix = reinterpret_cast(&worldMatrix); _worldMatrix = *pWorldMatrix; return 0; } void Mesh::update(FLOAT dt) { } void Mesh::render(IDirect3DDevice9* pD3DDevice) { // 1. set mesh data pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride); pD3DDevice->SetFVF(FVF); pD3DDevice->SetIndices(_pIndexBuffer); pD3DDevice->SetTransform(D3DTS_WORLD, &_worldMatrix); // 2. draw call //pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, _vertexCount / 3); //pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, _vertexCount - 2); //pD3DDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, _vertexCount - 2); 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 structure _vertexCount = 4; _vertexStride = sizeof(Vertex); // 2. create vertex buffer WITHOUT data HRESULT hr = pD3DDevice->CreateVertexBuffer( _vertexCount * _vertexStride, // vertex buffer byte length D3DUSAGE_WRITEONLY, // vertex buffer access mode FVF, // FVF - Flexible Vertex Format D3DPOOL_MANAGED, // video memory management &_pVertexBuffer, nullptr // not used anymore ); CheckFailed(hr, 300); // 3. fill vertex buffer with data Vertex* pVertices = nullptr; hr = _pVertexBuffer->Lock(0, 0, reinterpret_cast(&pVertices), 0); CheckFailed(hr, 310); // triangle //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 trianglelist WITHOUT index buffer //// triangle 1 //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); //// triangle 2 //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 trianglestrip //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 trianglefan // quad with trianglelist & index buffer 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); hr = _pVertexBuffer->Unlock(); CheckFailed(hr, 320); pVertices = nullptr; return 0; } UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice) { _indexCount = 6; // triangle * 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(&pIndices), 0); CheckFailed(hr, 340); // quad // triangle 1 pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2; // triangle 2 pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3; hr = _pIndexBuffer->Unlock(); CheckFailed(hr, 350); pIndices = nullptr; return 0; }