update buffer structs/shader to use view/projection and add camera to scene; fix compile error

This commit is contained in:
lightling 2022-02-13 13:11:00 -05:00
parent fd52d57740
commit 4ff75df1a7
Signed by: lightling
GPG key ID: 016F11E0AA296B67
5 changed files with 24 additions and 3 deletions

View file

@ -6,4 +6,6 @@ struct VertexShaderExternalData
{ {
DirectX::XMFLOAT4 colorTint; DirectX::XMFLOAT4 colorTint;
DirectX::XMFLOAT4X4 world; DirectX::XMFLOAT4X4 world;
DirectX::XMFLOAT4X4 view;
DirectX::XMFLOAT4X4 projection;
}; };

View file

@ -28,7 +28,10 @@ void Camera::Update(float _dt)
void Camera::UpdateViewMatrix() void Camera::UpdateViewMatrix()
{ {
XMFLOAT3 worldUp = XMFLOAT3(0, 1, 0); XMFLOAT3 worldUp = XMFLOAT3(0, 1, 0);
XMMATRIX view = XMMatrixLookToLH(XMLoadFloat3(&transform.GetPosition()), XMLoadFloat3(&transform.GetForward()), XMLoadFloat3(&worldUp)); XMFLOAT3 position = transform.GetPosition();
XMFLOAT3 forward = transform.GetForward();
XMMATRIX view = XMMatrixLookToLH(XMLoadFloat3(&position), XMLoadFloat3(&forward), XMLoadFloat3(&worldUp));
XMStoreFloat4x4(&viewMatrix, view); XMStoreFloat4x4(&viewMatrix, view);
} }

View file

@ -32,6 +32,7 @@ Game::Game(HINSTANCE hInstance)
CreateConsoleWindow(500, 120, 32, 120); CreateConsoleWindow(500, 120, 32, 120);
printf("Console window created successfully. Feel free to printf() here.\n"); printf("Console window created successfully. Feel free to printf() here.\n");
#endif #endif
camera = std::make_shared<Camera>(0.0f, 0.0f, -5.0f, (float)width / height, 90, 0.01f, 1000.0f);
} }
// -------------------------------------------------------- // --------------------------------------------------------
@ -220,6 +221,9 @@ void Game::OnResize()
{ {
// Handle base-level DX resize stuff // Handle base-level DX resize stuff
DXCore::OnResize(); DXCore::OnResize();
// Ensure camera has its projection matrix updated when window size changes
camera->SetAspect((float)width / height);
} }
// -------------------------------------------------------- // --------------------------------------------------------
@ -231,6 +235,8 @@ void Game::Update(float deltaTime, float totalTime)
if (Input::GetInstance().KeyDown(VK_ESCAPE)) if (Input::GetInstance().KeyDown(VK_ESCAPE))
Quit(); Quit();
camera->Update(deltaTime);
for (int i = 0; i < entities.size(); ++i) 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()->SetScale(0.1f * (i + 1), 0.1f * (i + 1), 0.1f * (i + 1));
@ -277,6 +283,8 @@ void Game::Draw(float deltaTime, float totalTime)
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.world = entity->GetTransform()->GetWorldMatrix(); vsData.world = entity->GetTransform()->GetWorldMatrix();
vsData.view = camera->GetViewMatrix();
vsData.projection = camera->GetProjectionMatrix();
// copy constant buffer to resource // copy constant buffer to resource
D3D11_MAPPED_SUBRESOURCE mappedBuffer = {}; D3D11_MAPPED_SUBRESOURCE mappedBuffer = {};

5
Game.h
View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "DXCore.h" #include "DXCore.h"
#include "Camera.h"
#include "Mesh.h" #include "Mesh.h"
#include "Entity.h" #include "Entity.h"
#include <DirectXMath.h> #include <DirectXMath.h>
@ -44,8 +45,10 @@ private:
// Temporary A2 shapes // Temporary A2 shapes
std::vector<std::shared_ptr<Mesh>> shapes; std::vector<std::shared_ptr<Mesh>> shapes;
// Temporary A3 entities; // Temporary A4 entities;
std::vector<std::shared_ptr<Entity>> entities; std::vector<std::shared_ptr<Entity>> entities;
// A5 Camera
std::shared_ptr<Camera> camera;
Microsoft::WRL::ComPtr<ID3D11Buffer> constantBufferVS; Microsoft::WRL::ComPtr<ID3D11Buffer> constantBufferVS;
}; };

View file

@ -2,6 +2,8 @@ cbuffer ExternalData : register(b0)
{ {
float4 colorTint; float4 colorTint;
matrix world; matrix world;
matrix view;
matrix projection;
} }
// Struct representing a single vertex worth of data // Struct representing a single vertex worth of data
@ -48,6 +50,9 @@ VertexToPixel main( VertexShaderInput input )
// Set up output struct // Set up output struct
VertexToPixel output; VertexToPixel output;
// Convert vertex to world view projection
matrix worldViewProjection = mul(mul(projection, view), world);
// Here we're essentially passing the input position directly through to the next // Here we're essentially passing the input position directly through to the next
// stage (rasterizer), though it needs to be a 4-component vector now. // stage (rasterizer), though it needs to be a 4-component vector now.
// - To be considered within the bounds of the screen, the X and Y components // - To be considered within the bounds of the screen, the X and Y components
@ -56,7 +61,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 = mul(world, float4(input.localPosition, 1.0f)); output.screenPosition = mul(worldViewProjection, 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