Merge pull request #3 from xLightling/a4_transform

A4: Transforms
This commit is contained in:
Lightling 2022-02-06 00:04:11 -05:00 committed by GitHub
commit eaf0d2fc32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 254 additions and 40 deletions

View file

@ -5,5 +5,5 @@
struct VertexShaderExternalData struct VertexShaderExternalData
{ {
DirectX::XMFLOAT4 colorTint; DirectX::XMFLOAT4 colorTint;
DirectX::XMFLOAT3 offset; DirectX::XMFLOAT4X4 world;
}; };

View file

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

View file

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

16
Entity.cpp Normal file
View file

@ -0,0 +1,16 @@
#include "Entity.h"
Entity::Entity(std::shared_ptr<Mesh> _mesh)
{
mesh = _mesh;
}
Transform* Entity::GetTransform()
{
return &transform;
}
std::shared_ptr<Mesh> Entity::GetMesh()
{
return mesh;
}

18
Entity.h Normal file
View file

@ -0,0 +1,18 @@
#pragma once
#include "Mesh.h"
#include "Transform.h"
#include <memory>
class Entity
{
public:
Entity(std::shared_ptr<Mesh> _mesh);
Transform* GetTransform();
std::shared_ptr<Mesh> GetMesh();
private:
Transform transform;
std::shared_ptr<Mesh> mesh;
};

View file

@ -197,6 +197,18 @@ void Game::CreateBasicGeometry()
std::make_shared<Mesh>(verts2, 04, ind2, 06, device, context), std::make_shared<Mesh>(verts2, 04, ind2, 06, device, context),
std::make_shared<Mesh>(verts3, 06, ind3, 12, 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]),
};
} }
@ -218,17 +230,53 @@ void Game::Update(float deltaTime, float totalTime)
// Example input checking: Quit if the escape key is pressed // Example input checking: Quit if the escape key is pressed
if (Input::GetInstance().KeyDown(VK_ESCAPE)) if (Input::GetInstance().KeyDown(VK_ESCAPE))
Quit(); Quit();
for (int i = 0; i < entities.size(); ++i)
{
entities[i]->GetTransform()->SetScale(0.1f * (i + 1), 0.1f * (i + 1), 0.1f * (i + 1));
entities[i]->GetTransform()->SetRotation(0.1f * (i + 1) * sin(totalTime), 0.1f * (i + 1) * sin(totalTime), 0.1f * (i + 1) * sin(totalTime));
// this range uses shapes[0] for testing
if (i < 3)
{
entities[i]->GetTransform()->SetPosition(tan((double)totalTime * ((double)i + (double)1)) * 0.1f, 0, 0);
}
// this range uses shapes[1] for testing
else if (i < 6)
{
entities[i]->GetTransform()->SetPosition(sin((double)totalTime * ((double)i + (double)1)) * 0.1f, 0, 0);
}
// this range uses shapes[2] for testing
else
{
entities[i]->GetTransform()->SetPosition(sin((double)totalTime * ((double)i + (double)1)) * cos(totalTime) * 0.1f, 0, 0);
}
}
} }
// -------------------------------------------------------- // --------------------------------------------------------
// Clear the screen, redraw everything, present to the user // Clear the screen, redraw everything, present to the user
// -------------------------------------------------------- // --------------------------------------------------------
void Game::Draw(float deltaTime, float totalTime) void Game::Draw(float deltaTime, float totalTime)
{
// Background color (Cornflower Blue in this case) for clearing
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
// - At the beginning of Draw (before drawing *anything*)
context->ClearRenderTargetView(backBufferRTV.Get(), color);
context->ClearDepthStencilView(
depthStencilView.Get(),
D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL,
1.0f,
0);
for (auto entity : entities)
{ {
// create constant buffer // create constant buffer
VertexShaderExternalData vsData; VertexShaderExternalData vsData;
vsData.colorTint = XMFLOAT4(1.0f, 0.5f, 0.5f, 1.0f); vsData.colorTint = XMFLOAT4(1.0f, 0.5f, 0.5f, 1.0f);
vsData.offset = XMFLOAT3(0.25f, 0.0f, 0.0f); vsData.world = entity->GetTransform()->GetWorldMatrix();
// copy constant buffer to resource // copy constant buffer to resource
D3D11_MAPPED_SUBRESOURCE mappedBuffer = {}; D3D11_MAPPED_SUBRESOURCE mappedBuffer = {};
@ -243,20 +291,6 @@ void Game::Draw(float deltaTime, float totalTime)
constantBufferVS.GetAddressOf() // Array of buffers (or address of one) 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 };
// Clear the render target and depth buffer (erases what's on the screen)
// - Do this ONCE PER FRAME
// - At the beginning of Draw (before drawing *anything*)
context->ClearRenderTargetView(backBufferRTV.Get(), color);
context->ClearDepthStencilView(
depthStencilView.Get(),
D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL,
1.0f,
0);
// Set the vertex and pixel shaders to use for the next Draw() command // Set the vertex and pixel shaders to use for the next Draw() command
// - These don't technically need to be set every frame // - These don't technically need to be set every frame
// - Once you start applying different shaders to different objects, // - Once you start applying different shaders to different objects,
@ -264,7 +298,6 @@ void Game::Draw(float deltaTime, float totalTime)
context->VSSetShader(vertexShader.Get(), 0, 0); context->VSSetShader(vertexShader.Get(), 0, 0);
context->PSSetShader(pixelShader.Get(), 0, 0); context->PSSetShader(pixelShader.Get(), 0, 0);
// Ensure the pipeline knows how to interpret the data (numbers) // Ensure the pipeline knows how to interpret the data (numbers)
// from the vertex buffer. // from the vertex buffer.
// - If all of your 3D models use the exact same vertex layout, // - If all of your 3D models use the exact same vertex layout,
@ -272,9 +305,7 @@ void Game::Draw(float deltaTime, float totalTime)
// - However, this isn't always the case (but might be for this course) // - However, this isn't always the case (but might be for this course)
context->IASetInputLayout(inputLayout.Get()); context->IASetInputLayout(inputLayout.Get());
for (int i = 0; i < shapes.size(); ++i) entity->GetMesh()->Draw();
{
shapes[i]->Draw();
} }
// Present the back buffer to the user // Present the back buffer to the user

3
Game.h
View file

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

96
Transform.cpp Normal file
View file

@ -0,0 +1,96 @@
#include "Transform.h"
using namespace DirectX;
Transform::Transform()
{
SetPosition(0, 0, 0);
SetRotation(0, 0, 0);
SetScale(1, 1, 1);
XMStoreFloat4x4(&worldMatrix, XMMatrixIdentity());
XMStoreFloat4x4(&worldMatrixInverseTranspose, XMMatrixIdentity());
worldMatrixChanged = false;
}
DirectX::XMFLOAT3 Transform::GetPosition() { return position; }
DirectX::XMFLOAT3 Transform::GetEulerAngles() { return eulerAngles; }
DirectX::XMFLOAT3 Transform::GetPitchYawRoll() { return eulerAngles; }
DirectX::XMFLOAT3 Transform::GetScale() { return scale; }
DirectX::XMFLOAT4X4 Transform::GetWorldMatrix()
{
if (worldMatrixChanged)
{
UpdateWorldMatrix();
worldMatrixChanged = false;
}
return worldMatrix;
}
DirectX::XMFLOAT4X4 Transform::GetWorldMatrixInverseTranspose()
{
if (worldMatrixChanged)
{
UpdateWorldMatrix();
worldMatrixChanged = false;
}
return worldMatrixInverseTranspose;
}
// XMVECTOR & XMStoreFloat compiles down to something faster than position += x,y,z because it happens all at once
void Transform::SetPosition(float _x, float _y, float _z)
{
XMVECTOR newPosition = XMVectorSet(_x, _y, _z, 0);
XMStoreFloat3(&position, newPosition);
worldMatrixChanged = true;
}
void Transform::SetRotation(float _pitch, float _yaw, float _roll)
{
XMVECTOR newRotation = XMVectorSet(_pitch, _yaw, _roll, 0);
XMStoreFloat3(&eulerAngles, newRotation);
worldMatrixChanged = true;
}
void Transform::SetScale(float _x, float _y, float _z)
{
XMVECTOR newScale = XMVectorSet(_x, _y, _z, 0);
XMStoreFloat3(&scale, newScale);
worldMatrixChanged = true;
}
void Transform::TranslateAbsolute(float _x, float _y, float _z)
{
XMVECTOR newPosition = XMLoadFloat3(&position);
XMVECTOR offset = XMVectorSet(_x, _y, _z, 0);
XMStoreFloat3(&position, newPosition + offset);
worldMatrixChanged = true;
}
void Transform::Rotate(float _pitch, float _yaw, float _roll)
{
XMVECTOR newRotation = XMLoadFloat3(&position);
XMVECTOR offset = XMVectorSet(_pitch, _yaw, _roll, 0);
XMStoreFloat3(&eulerAngles, newRotation + offset);
worldMatrixChanged = true;
}
void Transform::Scale(float _x, float _y, float _z)
{
XMVECTOR newScale = XMLoadFloat3(&position);
XMVECTOR offset = XMVectorSet(_x, _y, _z, 0);
XMStoreFloat3(&scale, newScale + offset);
worldMatrixChanged = true;
}
void Transform::UpdateWorldMatrix()
{
XMMATRIX matrixPosition = XMMatrixTranslation(position.x, position.y, position.z);
XMMATRIX matrixRotation = XMMatrixRotationRollPitchYaw(eulerAngles.x, eulerAngles.y, eulerAngles.z);
XMMATRIX matrixScale = XMMatrixScaling(scale.x, scale.y, scale.z);
XMStoreFloat4x4(&worldMatrix, matrixScale * matrixRotation * matrixPosition);
XMStoreFloat4x4(&worldMatrixInverseTranspose, XMMatrixInverse(0, XMMatrixTranspose(XMLoadFloat4x4(&worldMatrix))));
}

34
Transform.h Normal file
View file

@ -0,0 +1,34 @@
#pragma once
#include <DirectXMath.h>
class Transform
{
public:
Transform();
DirectX::XMFLOAT3 GetPosition();
DirectX::XMFLOAT3 GetEulerAngles();
DirectX::XMFLOAT3 GetPitchYawRoll(); // an alternative name for euler angles in case preferred
DirectX::XMFLOAT3 GetScale();
DirectX::XMFLOAT4X4 GetWorldMatrix();
DirectX::XMFLOAT4X4 GetWorldMatrixInverseTranspose();
void SetPosition(float _x, float _y, float _z);
void SetRotation(float _pitch, float _yaw, float _roll);
void SetScale(float _x, float _y, float _z);
void TranslateAbsolute(float _x, float _y, float _z);
void Rotate(float _pitch, float _yaw, float _roll);
void Scale(float _x, float _y, float _z);
private:
DirectX::XMFLOAT3 position;
DirectX::XMFLOAT3 eulerAngles;
DirectX::XMFLOAT3 scale;
DirectX::XMFLOAT4X4 worldMatrix;
DirectX::XMFLOAT4X4 worldMatrixInverseTranspose;
bool worldMatrixChanged;
void UpdateWorldMatrix();
};

View file

@ -1,7 +1,7 @@
cbuffer ExternalData : register(b0) cbuffer ExternalData : register(b0)
{ {
float4 colorTint; float4 colorTint;
float3 offset; matrix world;
} }
// Struct representing a single vertex worth of data // Struct representing a single vertex worth of data
@ -56,7 +56,7 @@ VertexToPixel main( VertexShaderInput input )
// - Each of these components is then automatically divided by the W component, // - Each of these components is then automatically divided by the W component,
// which we're leaving at 1.0 for now (this is more useful when dealing with // which we're leaving at 1.0 for now (this is more useful when dealing with
// a perspective projection matrix, which we'll get to in the future). // a perspective projection matrix, which we'll get to in the future).
output.screenPosition = float4(input.localPosition + offset, 1.0f); output.screenPosition = mul(world, float4(input.localPosition, 1.0f));
// Pass the color through // Pass the color through
// - The values will be interpolated per-pixel by the rasterizer // - The values will be interpolated per-pixel by the rasterizer