From 0e82e16b3ba98ff0e500941f87edc7865a190100 Mon Sep 17 00:00:00 2001 From: Lightling Date: Sat, 15 Jan 2022 15:40:38 -0500 Subject: [PATCH] test mesh class with previous hardcoded shape --- Game.cpp | 99 +++++++------------------------------------------------- Game.h | 6 ++++ 2 files changed, 17 insertions(+), 88 deletions(-) diff --git a/Game.cpp b/Game.cpp index 1bdf45b..152bde8 100644 --- a/Game.cpp +++ b/Game.cpp @@ -148,76 +148,19 @@ void Game::CreateBasicGeometry() XMFLOAT4 red = XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f); XMFLOAT4 green = XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f); XMFLOAT4 blue = XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f); + XMFLOAT4 black = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f); + XMFLOAT4 white = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); - // Set up the vertices of the triangle we would like to draw - // - We're going to copy this array, exactly as it exists in memory - // over to a DirectX-controlled data structure (the vertex buffer) - // - Note: Since we don't have a camera or really any concept of - // a "3d world" yet, we're simply describing positions within the - // bounds of how the rasterizer sees our screen: [-1 to +1] on X and Y - // - This means (0,0) is at the very center of the screen. - // - These are known as "Normalized Device Coordinates" or "Homogeneous - // Screen Coords", which are ways to describe a position without - // knowing the exact size (in pixels) of the image/window/etc. - // - Long story short: Resizing the window also resizes the triangle, - // since we're describing the triangle in terms of the window itself - Vertex vertices[] = - { + Vertex verts1[] = { { XMFLOAT3(+0.0f, +0.5f, +0.0f), red }, { XMFLOAT3(+0.5f, -0.5f, +0.0f), blue }, { XMFLOAT3(-0.5f, -0.5f, +0.0f), green }, }; + unsigned int ind1[] = { 0, 1, 2 }; - // Set up the indices, which tell us which vertices to use and in which order - // - This is somewhat redundant for just 3 vertices (it's a simple example) - // - Indices are technically not required if the vertices are in the buffer - // in the correct order and each one will be used exactly once - // - But just to see how it's done... - unsigned int indices[] = { 0, 1, 2 }; - - - // Create the VERTEX BUFFER description ----------------------------------- - // - The description is created on the stack because we only need - // it to create the buffer. The description is then useless. - D3D11_BUFFER_DESC vbd = {}; - vbd.Usage = D3D11_USAGE_IMMUTABLE; - vbd.ByteWidth = sizeof(Vertex) * 3; // 3 = number of vertices in the buffer - vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; // Tells DirectX this is a vertex buffer - vbd.CPUAccessFlags = 0; - vbd.MiscFlags = 0; - vbd.StructureByteStride = 0; - - // Create the proper struct to hold the initial vertex data - // - This is how we put the initial data into the buffer - D3D11_SUBRESOURCE_DATA initialVertexData = {}; - initialVertexData.pSysMem = vertices; - - // Actually create the buffer with the initial data - // - Once we do this, we'll NEVER CHANGE THE BUFFER AGAIN - device->CreateBuffer(&vbd, &initialVertexData, vertexBuffer.GetAddressOf()); - - - - // Create the INDEX BUFFER description ------------------------------------ - // - The description is created on the stack because we only need - // it to create the buffer. The description is then useless. - D3D11_BUFFER_DESC ibd = {}; - ibd.Usage = D3D11_USAGE_IMMUTABLE; - ibd.ByteWidth = sizeof(unsigned int) * 3; // 3 = number of indices in the buffer - ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; // Tells DirectX this is an index buffer - ibd.CPUAccessFlags = 0; - ibd.MiscFlags = 0; - ibd.StructureByteStride = 0; - - // Create the proper struct to hold the initial index data - // - This is how we put the initial data into the buffer - D3D11_SUBRESOURCE_DATA initialIndexData = {}; - initialIndexData.pSysMem = indices; - - // Actually create the buffer with the initial data - // - Once we do this, we'll NEVER CHANGE THE BUFFER AGAIN - device->CreateBuffer(&ibd, &initialIndexData, indexBuffer.GetAddressOf()); - + shapes = { + std::make_shared(verts1, 3, ind1, 3, device, context), + }; } @@ -275,30 +218,10 @@ void Game::Draw(float deltaTime, float totalTime) // - However, this isn't always the case (but might be for this course) context->IASetInputLayout(inputLayout.Get()); - - // Set buffers in the input assembler - // - Do this ONCE PER OBJECT you're drawing, since each object might - // have different geometry. - // - for this demo, this step *could* simply be done once during Init(), - // but I'm doing it here because it's often done multiple times per frame - // in a larger application/game - UINT stride = sizeof(Vertex); - UINT offset = 0; - context->IASetVertexBuffers(0, 1, vertexBuffer.GetAddressOf(), &stride, &offset); - context->IASetIndexBuffer(indexBuffer.Get(), DXGI_FORMAT_R32_UINT, 0); - - - // Finally do the actual drawing - // - Do this ONCE PER OBJECT you intend to draw - // - This will use all of the currently set DirectX "stuff" (shaders, buffers, etc) - // - DrawIndexed() uses the currently set INDEX BUFFER to look up corresponding - // vertices in the currently set VERTEX BUFFER - context->DrawIndexed( - 3, // The number of indices to use (we could draw a subset if we wanted) - 0, // Offset to the first index we want to use - 0); // Offset to add to each index when looking up vertices - - + for (int i = 0; i < shapes.size(); ++i) + { + shapes[i]->Draw(); + } // Present the back buffer to the user // - Puts the final frame we're drawing into the window so the user can see it diff --git a/Game.h b/Game.h index 4871310..9d83c8b 100644 --- a/Game.h +++ b/Game.h @@ -1,8 +1,11 @@ #pragma once #include "DXCore.h" +#include "Mesh.h" #include #include // Used for ComPtr - a smart pointer for COM objects +#include +#include class Game : public DXCore @@ -42,5 +45,8 @@ private: Microsoft::WRL::ComPtr vertexShader; Microsoft::WRL::ComPtr inputLayout; + // Temporary A2 shapes + std::vector> shapes; + };