From 2fb45a5c02ef67e92e4131dd3e47aff8f091a534 Mon Sep 17 00:00:00 2001 From: David Hackbarth Date: Mon, 5 Oct 2026 18:35:10 +0200 Subject: [PATCH] Added: vertex structure Added: mesh creation & rendering --- GraphicsProgramming/D3D11/D3D11.vcxproj | 1 + .../D3D11/D3D11.vcxproj.filters | 3 + GraphicsProgramming/D3D11/Mesh.cpp | 68 +++++++++++++++++++ GraphicsProgramming/D3D11/Vertex.h | 13 ++++ 4 files changed, 85 insertions(+) create mode 100644 GraphicsProgramming/D3D11/Vertex.h diff --git a/GraphicsProgramming/D3D11/D3D11.vcxproj b/GraphicsProgramming/D3D11/D3D11.vcxproj index ba356b8..0bfee2a 100644 --- a/GraphicsProgramming/D3D11/D3D11.vcxproj +++ b/GraphicsProgramming/D3D11/D3D11.vcxproj @@ -194,6 +194,7 @@ + diff --git a/GraphicsProgramming/D3D11/D3D11.vcxproj.filters b/GraphicsProgramming/D3D11/D3D11.vcxproj.filters index 8b4a2f3..a081ecc 100644 --- a/GraphicsProgramming/D3D11/D3D11.vcxproj.filters +++ b/GraphicsProgramming/D3D11/D3D11.vcxproj.filters @@ -68,5 +68,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/GraphicsProgramming/D3D11/Mesh.cpp b/GraphicsProgramming/D3D11/Mesh.cpp index a0f4345..c794c5e 100644 --- a/GraphicsProgramming/D3D11/Mesh.cpp +++ b/GraphicsProgramming/D3D11/Mesh.cpp @@ -1,5 +1,6 @@ #include "Mesh.h" #include "Utilities.h" +#include "Vertex.h" UINT Mesh::init(ID3D11Device* pD3DDevice) { @@ -20,18 +21,85 @@ void Mesh::update(FLOAT dt) void Mesh::render(ID3D11DeviceContext* pD3DDeviceContext) { + // setup pipeline + static UINT offset = 0; + + pD3DDeviceContext->IASetVertexBuffers( + 0, // starting slot, if you want to dump the data with offset in memory + 1, // amount of vertex buffers + &_pVertexBuffer, // array of vertex buffers + &_vertexStride, // array of size of vertices in vertex buffers + &offset // array of vertex buffer offsets (to skip vertices) + ); + + pD3DDeviceContext->IASetIndexBuffer( + _pIndexBuffer, + DXGI_FORMAT_R16_UINT, // index format + 0 // index buffer offset + ); + + pD3DDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + + // draw call + pD3DDeviceContext->DrawIndexed( + _indexCount, + 0, // bias of first index + 0 // bias of vertex index + ); } void Mesh::deInit() { + safeRelease(_pVertexBuffer); + safeRelease(_pIndexBuffer); } UINT Mesh::createVertexBuffer(ID3D11Device* pD3DDevice) { + // 1. define the structure (vertices) + Vertex vertices[] = { + Vertex(0.0f, 1.0f, 0.0f), // 0 + Vertex(1.0f, -1.0f, 0.0f), // 1 + Vertex(-1.0f, -1.0f, 0.0f) // 2 + }; + + _vertexCount = std::size(vertices); + _vertexStride = sizeof(Vertex); + + // 2. create the buffer + // for Usage: https://learn.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage + D3D11_BUFFER_DESC desc = {}; + desc.ByteWidth = _vertexCount * _vertexStride; // size of the buffer + desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; // buffer type + desc.Usage = D3D11_USAGE_IMMUTABLE; // memory management type + + D3D11_SUBRESOURCE_DATA initialData = {}; + initialData.pSysMem = vertices; + + HRESULT hr = pD3DDevice->CreateBuffer(&desc, &initialData, &_pVertexBuffer); + CheckFailed(hr, 300); + return 0; } UINT Mesh::createIndexBuffer(ID3D11Device* pD3DDevice) { + USHORT indices[] = { + 0, 1, 2 + }; + + _indexCount = std::size(indices); + + D3D11_BUFFER_DESC desc = {}; + desc.ByteWidth = _indexCount * sizeof(USHORT); + desc.BindFlags = D3D11_BIND_INDEX_BUFFER; + desc.Usage = D3D11_USAGE_IMMUTABLE; + + D3D11_SUBRESOURCE_DATA initialData = {}; + initialData.pSysMem = indices; + + HRESULT hr = pD3DDevice->CreateBuffer(&desc, &initialData, &_pIndexBuffer); + CheckFailed(hr, 310); + return 0; } diff --git a/GraphicsProgramming/D3D11/Vertex.h b/GraphicsProgramming/D3D11/Vertex.h new file mode 100644 index 0000000..1a936b0 --- /dev/null +++ b/GraphicsProgramming/D3D11/Vertex.h @@ -0,0 +1,13 @@ +#pragma once +#include + +using namespace DirectX; + +struct Vertex +{ + XMFLOAT3 position; + + Vertex() : position(0.0f, 0.0f, 0.0f) {} + Vertex(float x, float y, float z) : position(x, y, z) {} + Vertex(XMFLOAT3 p) : position(p) {} +}; \ No newline at end of file