update Game to use new Entity code

This commit is contained in:
lightling 2022-02-05 22:38:32 -05:00
parent ff0d41c301
commit 09f84f9b03
Signed by: lightling
GPG key ID: 016F11E0AA296B67
5 changed files with 66 additions and 38 deletions

View file

@ -124,17 +124,21 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="DXCore.cpp" />
<ClCompile Include="Entity.cpp" />
<ClCompile Include="Game.cpp" />
<ClCompile Include="Input.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="Mesh.cpp" />
<ClCompile Include="Transform.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="BufferStructs.h" />
<ClInclude Include="DXCore.h" />
<ClInclude Include="Entity.h" />
<ClInclude Include="Game.h" />
<ClInclude Include="Input.h" />
<ClInclude Include="Mesh.h" />
<ClInclude Include="Transform.h" />
<ClInclude Include="Vertex.h" />
</ItemGroup>
<ItemGroup>

View file

@ -33,6 +33,12 @@
<ClCompile Include="Mesh.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Transform.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Entity.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Vertex.h">
@ -53,6 +59,12 @@
<ClInclude Include="BufferStructs.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Transform.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Entity.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<FxCompile Include="PixelShader.hlsl">

View file

@ -197,6 +197,18 @@ void Game::CreateBasicGeometry()
std::make_shared<Mesh>(verts2, 04, ind2, 06, device, context),
std::make_shared<Mesh>(verts3, 06, ind3, 12, device, context),
};
entities = {
std::make_shared<Entity>(shapes[0]),
std::make_shared<Entity>(shapes[0]),
std::make_shared<Entity>(shapes[0]),
std::make_shared<Entity>(shapes[1]),
std::make_shared<Entity>(shapes[1]),
std::make_shared<Entity>(shapes[1]),
std::make_shared<Entity>(shapes[2]),
std::make_shared<Entity>(shapes[2]),
std::make_shared<Entity>(shapes[2]),
};
}
@ -225,26 +237,8 @@ void Game::Update(float deltaTime, float totalTime)
// --------------------------------------------------------
void Game::Draw(float deltaTime, float totalTime)
{
// create constant buffer
VertexShaderExternalData vsData;
vsData.colorTint = XMFLOAT4(1.0f, 0.5f, 0.5f, 1.0f);
vsData.offset = XMFLOAT3(0.25f, 0.0f, 0.0f);
// copy constant buffer to resource
D3D11_MAPPED_SUBRESOURCE mappedBuffer = {};
context->Map(constantBufferVS.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedBuffer);
memcpy(mappedBuffer.pData, &vsData, sizeof(vsData));
context->Unmap(constantBufferVS.Get(), 0);
// bind constant buffer
context->VSSetConstantBuffers(
0, // which slot (register) to bind buffer to?
1, // how many are we activating? can do multiple at once?
constantBufferVS.GetAddressOf() // Array of buffers (or address of one)
);
// Background color (Cornflower Blue in this case) for clearing
const float color[4] = { 0.4f, 0.6f, 0.75f, 0.0f };
static const float color[4] = { 0.4f, 0.6f, 0.75f, 0.0f };
// Clear the render target and depth buffer (erases what's on the screen)
// - Do this ONCE PER FRAME
@ -256,25 +250,41 @@ void Game::Draw(float deltaTime, float totalTime)
1.0f,
0);
// Set the vertex and pixel shaders to use for the next Draw() command
// - These don't technically need to be set every frame
// - Once you start applying different shaders to different objects,
// you'll need to swap the current shaders before each draw
context->VSSetShader(vertexShader.Get(), 0, 0);
context->PSSetShader(pixelShader.Get(), 0, 0);
// Ensure the pipeline knows how to interpret the data (numbers)
// from the vertex buffer.
// - If all of your 3D models use the exact same vertex layout,
// this could simply be done once in Init()
// - However, this isn't always the case (but might be for this course)
context->IASetInputLayout(inputLayout.Get());
for (int i = 0; i < shapes.size(); ++i)
for (auto entity : entities)
{
shapes[i]->Draw();
// create constant buffer
VertexShaderExternalData vsData;
vsData.colorTint = XMFLOAT4(1.0f, 0.5f, 0.5f, 1.0f);
vsData.world = entity->GetTransform()->GetWorldMatrix();
// copy constant buffer to resource
D3D11_MAPPED_SUBRESOURCE mappedBuffer = {};
context->Map(constantBufferVS.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedBuffer);
memcpy(mappedBuffer.pData, &vsData, sizeof(vsData));
context->Unmap(constantBufferVS.Get(), 0);
// bind constant buffer
context->VSSetConstantBuffers(
0, // which slot (register) to bind buffer to?
1, // how many are we activating? can do multiple at once?
constantBufferVS.GetAddressOf() // Array of buffers (or address of one)
);
// Set the vertex and pixel shaders to use for the next Draw() command
// - These don't technically need to be set every frame
// - Once you start applying different shaders to different objects,
// you'll need to swap the current shaders before each draw
context->VSSetShader(vertexShader.Get(), 0, 0);
context->PSSetShader(pixelShader.Get(), 0, 0);
// Ensure the pipeline knows how to interpret the data (numbers)
// from the vertex buffer.
// - If all of your 3D models use the exact same vertex layout,
// this could simply be done once in Init()
// - However, this isn't always the case (but might be for this course)
context->IASetInputLayout(inputLayout.Get());
entity->GetMesh()->Draw();
}
// Present the back buffer to the user

3
Game.h
View file

@ -2,6 +2,7 @@
#include "DXCore.h"
#include "Mesh.h"
#include "Entity.h"
#include <DirectXMath.h>
#include <wrl/client.h> // Used for ComPtr - a smart pointer for COM objects
#include <memory>
@ -43,6 +44,8 @@ private:
// Temporary A2 shapes
std::vector<std::shared_ptr<Mesh>> shapes;
// Temporary A3 entities;
std::vector<std::shared_ptr<Entity>> entities;
Microsoft::WRL::ComPtr<ID3D11Buffer> constantBufferVS;
};

View file

@ -22,7 +22,6 @@ public:
void Rotate(float _pitch, float _yaw, float _roll);
void Scale(float _x, float _y, float _z);
private:
DirectX::XMFLOAT3 position;
DirectX::XMFLOAT3 eulerAngles;