Changed D3D9 to D3D11 api connection
Changed mesh to D3D11 Changed material to D3D11 (WIP)
This commit is contained in:
+51
-95
@@ -2,11 +2,7 @@
|
||||
#include "Utilities.h"
|
||||
#include "Vertex.h"
|
||||
|
||||
#include <DirectXMath.h>
|
||||
|
||||
using namespace DirectX;
|
||||
|
||||
UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
|
||||
UINT Mesh::init(ID3D11Device* pD3DDevice)
|
||||
{
|
||||
UINT error = 0;
|
||||
|
||||
@@ -16,10 +12,7 @@ UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
|
||||
error = createIndexBuffer(pD3DDevice);
|
||||
CheckError(error);
|
||||
|
||||
XMMATRIX identity = XMMatrixIdentity();
|
||||
XMFLOAT4X4 w;
|
||||
XMStoreFloat4x4(&w, identity);
|
||||
_worldMatrix = *reinterpret_cast<D3DMATRIX*>(&w);
|
||||
XMStoreFloat4x4(&_worldMatrix, XMMatrixIdentity());
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -48,28 +41,25 @@ void Mesh::update(FLOAT dt)
|
||||
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, rotY, rotZ);
|
||||
XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
|
||||
|
||||
XMFLOAT4X4 w;
|
||||
XMStoreFloat4x4(&w, scale * rotation * translation);
|
||||
|
||||
_worldMatrix = *reinterpret_cast<D3DMATRIX*>(&w);
|
||||
XMStoreFloat4x4(&_worldMatrix, scale * rotation * translation);
|
||||
}
|
||||
|
||||
void Mesh::render(IDirect3DDevice9* pD3DDevice)
|
||||
void Mesh::render(ID3D11DeviceContext* pD3DDeviceContext)
|
||||
{
|
||||
// setup pipeline
|
||||
pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride);
|
||||
pD3DDevice->SetIndices(_pIndexBuffer);
|
||||
pD3DDevice->SetFVF(FVF);
|
||||
pD3DDevice->SetTransform(D3DTS_WORLD, &_worldMatrix);
|
||||
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
|
||||
pD3DDevice->DrawIndexedPrimitive(
|
||||
D3DPT_TRIANGLELIST, // primitive type
|
||||
0, 0, // vertex buffer offsets
|
||||
_vertexCount,
|
||||
0, // index buffer offset
|
||||
_indexCount / 3 // primitive count
|
||||
);
|
||||
pD3DDeviceContext->DrawIndexed(_indexCount, 0, 0);
|
||||
}
|
||||
|
||||
void Mesh::deInit()
|
||||
@@ -78,88 +68,54 @@ void Mesh::deInit()
|
||||
safeRelease(_pVertexBuffer);
|
||||
}
|
||||
|
||||
UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||
UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice)
|
||||
{
|
||||
// specify values
|
||||
_vertexCount = 4;
|
||||
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
|
||||
HRESULT hr = pD3DDevice->CreateVertexBuffer(
|
||||
_vertexCount * _vertexStride, // total content size
|
||||
D3DUSAGE_WRITEONLY, // read and/or write access
|
||||
FVF, // FVF - Flexible Vertex Format, how the vertex is structured
|
||||
D3DPOOL_MANAGED, // buffer memory management
|
||||
&_pVertexBuffer,
|
||||
nullptr // deprecated, so always nullptr
|
||||
);
|
||||
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);
|
||||
|
||||
// populate vertex buffer
|
||||
Vertex* pVertices = nullptr;
|
||||
hr = _pVertexBuffer->Lock(
|
||||
0, 0, // lock a specific amount of the buffer, here everything
|
||||
reinterpret_cast<void**>(&pVertices),
|
||||
0 // optional flags
|
||||
);
|
||||
CheckFailed(310, hr);
|
||||
|
||||
// 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 colors
|
||||
//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, 0, 0, 255);
|
||||
//pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 255, 255, 0);
|
||||
|
||||
// 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;
|
||||
return 0;
|
||||
}
|
||||
|
||||
UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||
UINT Mesh::createIndexBuffer(ID3D11Device* pD3DDevice)
|
||||
{
|
||||
_indexCount = 6;
|
||||
unsigned short indices[] =
|
||||
{
|
||||
0, 1, 2,
|
||||
0, 2, 3
|
||||
};
|
||||
_indexCount = std::size(indices);
|
||||
|
||||
HRESULT hr = pD3DDevice->CreateIndexBuffer(_indexCount * sizeof(unsigned short),
|
||||
D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &_pIndexBuffer, 0);
|
||||
CheckFailed(hr, 330);
|
||||
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
|
||||
|
||||
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;
|
||||
D3D11_SUBRESOURCE_DATA data = {};
|
||||
data.pSysMem = indices;
|
||||
|
||||
HRESULT hr = pD3DDevice->CreateBuffer(&desc, &data, &_pIndexBuffer);
|
||||
CheckFailed(hr, 320);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user