My engine doesnt want to render a mesh ":/
Here is some parts from my code, if somebody could help and need more code to understand, please ask
HRESULT TD3D::Init(HWND hwnd, int width, int height, bool windowed)
{
HRESULT hr;
hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&m_pFactory);
if (FAILED(hr))
{
return hr;
}
UINT createDeviceFlags = 0;
#ifdef _DEBUG
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_FEATURE_LEVEL featureLevel;
hr = D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
createDeviceFlags,
nullptr,
0,
D3D11_SDK_VERSION,
&m_pDevice,
&featureLevel,
&m_pContext
);
if (FAILED(hr))
{
return hr;
}
DXGI_MODE_DESC bufferDesc = {};
bufferDesc.Width = width;
bufferDesc.Height = height;
bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
bufferDesc.RefreshRate.Numerator = 60;
bufferDesc.RefreshRate.Denominator = 1;
DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hwnd;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.Windowed = windowed;
hr = m_pFactory->CreateSwapChain(m_pDevice, &swapChainDesc, &m_pSwapChain);
if (FAILED(hr))
{
return hr;
}
CreateRenderTarget();
ID3DBlob* errorMessage;
hr = D3DCompileFromFile(L"C:\\Users\\Alan\\Desktop\\game-dev\\TEngine\\TRender\\TD3D\\VertexShader.hlsl",
nullptr, nullptr, "VS", "vs_4_0", 0, 0, &m_pVertexShaderBuffer, &errorMessage);
if (FAILED(hr))
{
const char* errorMsg = static_cast<const char*>(errorMessage->GetBufferPointer());
size_t errorMsgSize = errorMessage->GetBufferSize();
std::string errorString(errorMsg, errorMsgSize);
return hr;
}
hr = m_pDevice->CreateVertexShader(m_pVertexShaderBuffer->GetBufferPointer(), m_pVertexShaderBuffer->GetBufferSize(), nullptr, &m_pVertexShader);
if (FAILED(hr))
{
return hr;
}
hr = D3DCompileFromFile(L"C:\\Users\\Alan\\Desktop\\game-dev\\TEngine\\TRender\\TD3D\\PixelShader.hlsl",
nullptr, nullptr, "PS", "ps_4_0", 0, 0, &m_pPixelShaderBuffer, &errorMessage);
if (FAILED(hr))
{
const char* errorMsg = static_cast<const char*>(errorMessage->GetBufferPointer());
size_t errorMsgSize = errorMessage->GetBufferSize();
std::string errorString(errorMsg, errorMsgSize);
return hr;
}
hr = m_pDevice->CreatePixelShader(m_pPixelShaderBuffer->GetBufferPointer(), m_pPixelShaderBuffer->GetBufferSize(), nullptr, &m_pPixelShader);
if (FAILED(hr))
{
return hr;
}
D3D11_INPUT_ELEMENT_DESC layout[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
UINT numElements = ARRAYSIZE(layout);
hr = m_pDevice->CreateInputLayout(layout, numElements,
m_pVertexShaderBuffer->GetBufferPointer(), m_pVertexShaderBuffer->GetBufferSize(), &m_pInputLayout);
if (FAILED(hr))
{
return hr;
}
m_pContext->IASetInputLayout(m_pInputLayout);
m_isRunning = true;
return T_OK;
}
void TD3D::Release()
{
if (m_pRenderTargetView) { m_pRenderTargetView->Release(); m_pRenderTargetView = nullptr; }
if (m_pSwapChain) { m_pSwapChain->Release(); m_pSwapChain = nullptr; }
if (m_pContext) { m_pContext->Release(); m_pContext = nullptr; }
if (m_pDevice) { m_pDevice->Release(); m_pDevice = nullptr; }
if (m_pInputLayout) { m_pInputLayout->Release(); m_pInputLayout = nullptr; }
if (m_pVertexShader) { m_pVertexShader->Release(); m_pVertexShader = nullptr; }
if (m_pVertexShaderBuffer) { m_pVertexShaderBuffer->Release(); m_pVertexShaderBuffer = nullptr; }
if (m_pPixelShader) { m_pPixelShader->Release(); m_pPixelShader = nullptr; }
if (m_pPixelShaderBuffer) { m_pPixelShaderBuffer->Release(); m_pPixelShaderBuffer = nullptr; }
for (TMesh* mesh : m_pMeshes)
{
delete mesh;
}
}
#include "TMesh.h"
#include <DirectXMath.h>
TMesh::TMesh(ID3D11Device* device, Vertex* verticies, UINT numVerticies, UINT* indicies, UINT numIndicies) {
D3D11_BUFFER_DESC vertexBufferDesc;
ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(Vertex) * numVerticies;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA vertexData;
ZeroMemory(&vertexData, sizeof(vertexData));
vertexData.pSysMem = verticies;
HRESULT hr = device->CreateBuffer(&vertexBufferDesc, &vertexData, &vertexBuffer);
D3D11_BUFFER_DESC indexBufferDesc;
ZeroMemory(&indexBufferDesc, sizeof(indexBufferDesc));
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(UINT) * numIndicies;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA indexData;
ZeroMemory(&indexData, sizeof(indexData));
indexData.pSysMem = indicies;
hr = device->CreateBuffer(&indexBufferDesc, &indexData, &indexBuffer);
indexCount = numIndicies;
}
TMesh::~TMesh() {
if (vertexBuffer) vertexBuffer->Release();
if (indexBuffer) indexBuffer->Release();
}
void TMesh::Render(ID3D11DeviceContext* context) {
UINT stride = sizeof(Vertex);
UINT offset = 0;
context->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
context->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
context->DrawIndexed(indexCount, 0, 0);
}
void TD3D::BeginRendering()
{
m_pContext->OMSetRenderTargets(1, &m_pRenderTargetView, nullptr);
float color[4] = { 0.5f, 0.2f, 0.4f, 1.0f };
m_pContext->ClearRenderTargetView(m_pRenderTargetView, color);
m_pContext->VSSetShader(m_pVertexShader, nullptr, 0);
m_pContext->PSSetShader(m_pPixelShader, nullptr, 0);
for (TMesh* mesh : m_pMeshes)
{
mesh->Render(m_pContext);
}
}
void TD3D::EndRendering()
{
m_pSwapChain->Present(1, 0);
}
Vertex* vertexes = new Vertex[8] {
{TVector(-1.0f, -1.0f, 1.0f), TVector(1.0f, 0.0f, 0.0f)},
{TVector(1.0f, -1.0f, 1.0f), TVector(0.0f, 1.0f, 0.0f)},
{TVector(1.0f, 1.0f, 1.0f), TVector(0.0f, 0.0f, 1.0f)},
{TVector(-1.0f, 1.0f, 1.0f), TVector(1.0f, 1.0f, 0.0f)},
{TVector(-1.0f, -1.0f, -1.0f), TVector(1.0f, 0.0f, 1.0f)},
{TVector(1.0f, -1.0f, -1.0f), TVector(0.0f, 1.0f, 1.0f)},
{TVector(1.0f, 1.0f, -1.0f), TVector(1.0f, 1.0f, 1.0f)},
{TVector(-1.0f, 1.0f, -1.0f), TVector(0.5f, 0.5f, 0.5f)}
};
UINT* indices = new UINT[36]{
0, 1, 2, 0, 2, 3,
4, 5, 6, 4, 6, 7,
0, 3, 7, 0, 7, 4,
1, 5, 6, 1, 6, 2,
3, 2, 6, 3, 6, 7,
0, 4, 5, 0, 5, 1
};
renderDevice->LoadMesh(vertexes, 8, indices, 36);
// Main message loop
MSG msg = {};
while (msg.message != WM_QUIT) {
// Process messages
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
renderDevice->BeginRendering();
renderDevice->EndRendering();
}
}
return 0;