Skip to content

Commit 79a760e

Browse files
committed
Switch back to DirectXMath
1 parent 9d9eb27 commit 79a760e

File tree

8 files changed

+97
-71
lines changed

8 files changed

+97
-71
lines changed

src/Cpp/1-getting-started/1-3-5-RasterizerState/Assets/Shaders/Main.vs.hlsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ struct VSOutput
1414

1515
cbuffer PerApplication
1616
{
17-
matrix ProjectionMatrix;
17+
column_major matrix ProjectionMatrix;
1818
}
1919

2020
cbuffer PerFrame
2121
{
22-
matrix ViewMatrix;
22+
column_major matrix ViewMatrix;
2323
}
2424

2525
cbuffer PerObject
2626
{
27-
matrix WorldMatrix;
27+
column_major matrix WorldMatrix;
2828
}
2929

3030
VSOutput Main(VSInput input)
3131
{
32-
const matrix modelViewProjection = mul(ProjectionMatrix, mul(ViewMatrix, WorldMatrix));
32+
const matrix modelViewProjection = mul(WorldMatrix, mul(ViewMatrix, ProjectionMatrix));
3333

3434
VSOutput output = (VSOutput)0;
3535
output.Position = mul(modelViewProjection, float4(input.Position, 1.0f));

src/Cpp/1-getting-started/1-3-5-RasterizerState/RasterizerStateApplication.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,6 @@ class RasterizerStateApplication final : public Application
9797
uint32_t _modelIndexCount = 0;
9898
bool _toggledRotation = false;
9999
int32_t _selectedDepthFunction = 1;
100-
int32_t _selectedRasterizerState = 0;
100+
int32_t _selectedRasterizerState = 11;
101101
bool _isWireframe = false;
102102
};

src/Cpp/1-getting-started/1-3-6-Camera/Application.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ void Application::OnMouseMove(
174174
const float x,
175175
const float y)
176176
{
177-
DeltaPosition = glm::vec2(
177+
DeltaPosition = DirectX::XMFLOAT2(
178178
x - CursorPosition.x,
179179
y - CursorPosition.y);
180180

181-
CursorPosition = glm::vec2(x, y);
181+
CursorPosition = DirectX::XMFLOAT2(x, y);
182182
}
183183

184184
void Application::UpdateInput(
@@ -187,11 +187,11 @@ void Application::UpdateInput(
187187
{
188188
const auto window = glfwGetCurrentContext();
189189

190-
DeltaPosition = glm::vec2(0.0f, 0.0f);
190+
DeltaPosition = DirectX::XMFLOAT2(0.0f, 0.0f);
191191

192192
if (_isCaptured)
193193
{
194-
CursorPosition = glm::vec2(centerX, centerY);
194+
CursorPosition = DirectX::XMFLOAT2(centerX, centerY);
195195
glfwSetCursorPos(window, centerX, centerY);
196196
}
197197
}

src/Cpp/1-getting-started/1-3-6-Camera/Application.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <glm/vec2.hpp>
3+
#include <DirectXMath.h>
44

55
#include <string_view>
66
#include <cstdint>
@@ -48,8 +48,8 @@ class Application
4848
[[nodiscard]] bool IsKeyPressed(const int32_t key) const;
4949
[[nodiscard]] bool IsKeyUp(const int32_t key) const;
5050

51-
glm::vec2 DeltaPosition = { 0.0f, 0.0f };
52-
glm::vec2 CursorPosition = { 0.0f, 0.0f };
51+
DirectX::XMFLOAT2 DeltaPosition = { 0.0f, 0.0f };
52+
DirectX::XMFLOAT2 CursorPosition = { 0.0f, 0.0f };
5353

5454
private:
5555
static void HandleResize(

src/Cpp/1-getting-started/1-3-6-Camera/Camera.cpp

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#include "Camera.hpp"
2-
#include <glm/ext/matrix_clip_space.hpp>
3-
#include <glm/ext/matrix_transform.hpp>
42

53
Camera::Camera(
64
const float nearPlane,
@@ -10,12 +8,12 @@ Camera::Camera(
108
_farPlane = farPlane;
119
}
1210

13-
glm::mat4 Camera::GetViewMatrix()
11+
DirectX::XMFLOAT4X4 Camera::GetViewMatrix()
1412
{
1513
return _viewMatrix;
1614
}
1715

18-
glm::mat4 Camera::GetProjectionMatrix()
16+
DirectX::XMFLOAT4X4 Camera::GetProjectionMatrix()
1917
{
2018
return _projectionMatrix;
2119
}
@@ -37,61 +35,100 @@ void Camera::Update()
3735

3836
void Camera::UpdateViewMatrix()
3937
{
40-
_viewMatrix = glm::lookAtRH(_position, _position + _direction, _up);
38+
DirectX::XMVECTOR position = DirectX::XMLoadFloat3(&_position);
39+
DirectX::XMVECTOR direction = DirectX::XMLoadFloat3(&_direction);
40+
DirectX::XMVECTOR up = DirectX::XMLoadFloat3(&_up);
41+
42+
DirectX::XMVECTOR lookAt = DirectX::XMVectorAdd(position, direction);
43+
44+
DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixLookAtRH(position, lookAt, up);
45+
46+
DirectX::XMStoreFloat4x4(&_viewMatrix, viewMatrix);
4147
}
4248

4349
void Camera::UpdateVectors()
4450
{
45-
float x = glm::cos(_pitch) * glm::cos(_yaw);
46-
float y = glm::sin(_pitch);
47-
float z = glm::cos(_pitch) * glm::sin(_yaw);
51+
constexpr DirectX::XMFLOAT3 unitY = DirectX::XMFLOAT3(0.0f, 1.0f, 0.0f);
52+
53+
float pitchSine = 0.0f;
54+
float pitchCosine = 0.0f;
55+
float yawSine = 0.0f;
56+
float yawCosine = 0.0f;
57+
58+
DirectX::XMScalarSinCos(&pitchSine, &pitchCosine, _pitch);
59+
DirectX::XMScalarSinCos(&yawSine, &yawCosine, _yaw);
60+
61+
DirectX::XMFLOAT3 direction = DirectX::XMFLOAT3(
62+
pitchCosine * yawCosine,
63+
pitchSine,
64+
pitchCosine * yawSine);
4865

49-
constexpr glm::vec3 unitY = glm::vec3(0.0f, 1.0f, 0.0f);
66+
DirectX::XMVECTOR directionVector = DirectX::XMVector3Normalize(DirectX::XMLoadFloat3(&direction));
67+
DirectX::XMVECTOR right = DirectX::XMVector3Normalize(DirectX::XMVector3Cross(directionVector, DirectX::XMLoadFloat3(&unitY)));
68+
DirectX::XMVECTOR up = DirectX::XMVector3Normalize(DirectX::XMVector3Cross(right, directionVector));
5069

51-
_direction = glm::normalize(glm::vec3(x, y, z));
52-
_right = glm::normalize(glm::cross(_direction, unitY));
53-
_up = glm::normalize(glm::cross(_right, _direction));
70+
DirectX::XMStoreFloat3(&_direction, directionVector);
71+
DirectX::XMStoreFloat3(&_right, right);
72+
DirectX::XMStoreFloat3(&_up, up);
5473
}
5574

56-
void Camera::SetPosition(const glm::vec3& position)
75+
void Camera::SetPosition(const DirectX::XMFLOAT3& position)
5776
{
5877
_position = position;
5978
}
6079

61-
void Camera::SetDirection(const glm::vec3& direction)
80+
void Camera::SetDirection(const DirectX::XMFLOAT3& direction)
6281
{
6382
_direction = _direction;
6483
}
6584

66-
void Camera::SetUp(const glm::vec3& up)
85+
void Camera::SetUp(const DirectX::XMFLOAT3& up)
6786
{
6887
_up = _up;
6988
}
7089

7190
void Camera::Move(const float& speed)
7291
{
73-
_position += _direction * speed;
92+
DirectX::XMVECTOR scaled = DirectX::XMVectorScale(DirectX::XMLoadFloat3(&_direction), speed);
93+
DirectX::XMVECTOR advancedPosition = DirectX::XMVectorAdd(DirectX::XMLoadFloat3(&_position), scaled);
94+
95+
DirectX::XMStoreFloat3(&_position, advancedPosition);
7496
}
7597

7698
void Camera::Slide(const float& speed)
7799
{
78-
_position += _right * speed;
100+
DirectX::XMVECTOR scaled = DirectX::XMVectorScale(DirectX::XMLoadFloat3(&_right), speed);
101+
DirectX::XMVECTOR advancedPosition = DirectX::XMVectorAdd(DirectX::XMLoadFloat3(&_position), scaled);
102+
103+
DirectX::XMStoreFloat3(&_position, advancedPosition);
79104
}
80105

81106
void Camera::Lift(const float& speed)
82107
{
83-
_position += _up * speed;
108+
DirectX::XMVECTOR scaled = DirectX::XMVectorScale(DirectX::XMLoadFloat3(&_up), speed);
109+
DirectX::XMVECTOR advancedPosition = DirectX::XMVectorAdd(DirectX::XMLoadFloat3(&_position), scaled);
110+
111+
DirectX::XMStoreFloat3(&_position, advancedPosition);
112+
84113
}
85114

86115
void Camera::AddYaw(const float yawInDegrees)
87116
{
88-
_yaw += glm::radians(yawInDegrees);
117+
_yaw += DirectX::XMConvertToRadians(yawInDegrees);
89118
}
90119

91-
void Camera::AddPitch(const float pitchInDegrees)
120+
void Camera::AddPitch(float pitchInDegrees)
92121
{
93-
float pitch = glm::clamp(pitchInDegrees, -89.0f, 89.0f);
94-
_pitch -= glm::radians(pitch);
122+
if (pitchInDegrees >= 89.0f)
123+
{
124+
pitchInDegrees = 89.0f;
125+
}
126+
if (pitchInDegrees <= -89.0f)
127+
{
128+
pitchInDegrees = -89.0f;
129+
}
130+
131+
_pitch -= DirectX::XMConvertToRadians(pitchInDegrees);
95132
}
96133

97134
PerspectiveCamera::PerspectiveCamera(
@@ -103,7 +140,7 @@ PerspectiveCamera::PerspectiveCamera(
103140
{
104141
_width = width;
105142
_height = height;
106-
_fieldOfViewInRadians = glm::radians(fieldOfViewInDegrees);
143+
_fieldOfViewInRadians = DirectX::XMConvertToRadians(fieldOfViewInDegrees);
107144
}
108145

109146
void PerspectiveCamera::Resize(
@@ -118,10 +155,10 @@ void PerspectiveCamera::Resize(
118155

119156
void PerspectiveCamera::UpdateProjectionMatrix()
120157
{
121-
_projectionMatrix = glm::perspectiveFovRH(
158+
DirectX::XMMATRIX projectionMatrix = DirectX::XMMatrixPerspectiveFovRH(
122159
_fieldOfViewInRadians,
123-
_width,
124-
_height,
160+
_width / static_cast<float>(_height),
125161
_nearPlane,
126162
_farPlane);
163+
DirectX::XMStoreFloat4x4(&_projectionMatrix, projectionMatrix);
127164
}

src/Cpp/1-getting-started/1-3-6-Camera/Camera.hpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#pragma once
22

3-
#include <glm/mat4x4.hpp>
4-
#include <glm/vec3.hpp>
3+
#include <DirectXMath.h>
54

65
struct CameraConstants
76
{
8-
glm::mat4 ProjectionMatrix;
9-
glm::mat4 ViewMatrix;
7+
DirectX::XMFLOAT4X4 ProjectionMatrix;
8+
DirectX::XMFLOAT4X4 ViewMatrix;
109
};
1110

1211
class Camera
@@ -18,19 +17,19 @@ class Camera
1817

1918
void Update();
2019

21-
void SetPosition(const glm::vec3& position);
22-
void SetDirection(const glm::vec3& direction);
23-
void SetUp(const glm::vec3& up);
20+
void SetPosition(const DirectX::XMFLOAT3& position);
21+
void SetDirection(const DirectX::XMFLOAT3& direction);
22+
void SetUp(const DirectX::XMFLOAT3& up);
2423

2524
void Move(const float& speed);
2625
void Slide(const float& speed);
2726
void Lift(const float& speed);
2827

2928
void AddYaw(const float yawInDegrees);
30-
void AddPitch(const float pitchInDegrees);
29+
void AddPitch(float pitchInDegrees);
3130

32-
[[nodiscard]] glm::mat4 GetViewMatrix();
33-
[[nodiscard]] glm::mat4 GetProjectionMatrix();
31+
[[nodiscard]] DirectX::XMFLOAT4X4 GetViewMatrix();
32+
[[nodiscard]] DirectX::XMFLOAT4X4 GetProjectionMatrix();
3433
[[nodiscard]] CameraConstants& GetCameraConstants();
3534

3635
protected:
@@ -43,18 +42,18 @@ class Camera
4342
float _nearPlane = 0.0f;
4443
float _farPlane = 0.0f;
4544

46-
glm::mat4 _projectionMatrix = glm::mat4(1.0f);
47-
glm::mat4 _viewMatrix = glm::mat4(1.0f);
45+
DirectX::XMFLOAT4X4 _projectionMatrix = DirectX::XMFLOAT4X4();
46+
DirectX::XMFLOAT4X4 _viewMatrix = DirectX::XMFLOAT4X4();
4847
CameraConstants _cameraConstants = {};
4948

5049
private:
5150
void UpdateVectors();
5251
void UpdateViewMatrix();
5352

54-
glm::vec3 _position = {};
55-
glm::vec3 _direction = {};
56-
glm::vec3 _up = {};
57-
glm::vec3 _right = {};
53+
DirectX::XMFLOAT3 _position = {};
54+
DirectX::XMFLOAT3 _direction = {};
55+
DirectX::XMFLOAT3 _up = {};
56+
DirectX::XMFLOAT3 _right = {};
5857
float _pitch = 0.0f;
5958
float _yaw = -90.0f;
6059
};

src/Cpp/1-getting-started/1-3-6-Camera/CameraApplication.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#define GLFW_EXPOSE_NATIVE_WIN32
1111
#include <GLFW/glfw3native.h>
1212

13-
#include <glm/ext/matrix_transform.hpp>
14-
#include <glm/gtc/type_ptr.hpp>
15-
1613
#include <d3dcompiler.h>
1714

1815
#include <imgui/imgui.h>
@@ -251,9 +248,9 @@ bool CameraApplication::Load()
251248
return false;
252249
}
253250

254-
_camera->SetPosition(glm::vec3{ 0.0f, 50.0f, 400.0f });
255-
_camera->SetDirection(glm::vec3{ 0.0f, 0.0f, 1.0f });
256-
_camera->SetUp(glm::vec3{ 0.0f, 1.0f, 0.0f });
251+
_camera->SetPosition(DirectX::XMFLOAT3{ 0.0f, 50.0f, 400.0f });
252+
_camera->SetDirection(DirectX::XMFLOAT3{ 0.0f, 0.0f, 1.0f });
253+
_camera->SetUp(DirectX::XMFLOAT3{ 0.0f, 1.0f, 0.0f });
257254

258255
return true;
259256
}
@@ -394,15 +391,9 @@ void CameraApplication::Update()
394391
angle -= 90.0f * (10.0 / 60000.0f);
395392
}
396393

397-
const auto rotationAxis = glm::vec3(0, 1, 0);
398-
399-
glm::mat4 identity = glm::mat4(1.0f);
400-
_worldMatrix = glm::rotate(
401-
identity,
402-
glm::radians(angle),
403-
rotationAxis);
404-
405-
_deviceContext->UpdateSubresource(_objectConstantBuffer.Get(), glm::value_ptr(_worldMatrix));
394+
DirectX::XMMATRIX rotationMatrix = DirectX::XMMatrixRotationY(DirectX::XMConvertToRadians(angle));
395+
DirectX::XMStoreFloat4x4(&_worldMatrix, rotationMatrix);
396+
_deviceContext->UpdateSubresource(_objectConstantBuffer.Get(), &_worldMatrix);
406397
}
407398

408399
void CameraApplication::Render()

src/Cpp/1-getting-started/1-3-6-Camera/CameraApplication.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "Definitions.hpp"
55

66
#include <d3d11_2.h>
7-
#include <glm/mat4x4.hpp>
87

98
#include <string_view>
109
#include <memory>
@@ -85,7 +84,7 @@ class CameraApplication final : public Application
8584
WRL::ComPtr<ID3D11Buffer> _cameraConstantBuffer = nullptr;
8685
WRL::ComPtr<ID3D11Buffer> _objectConstantBuffer = nullptr;
8786

88-
glm::mat4 _worldMatrix = glm::mat4();
87+
DirectX::XMFLOAT4X4 _worldMatrix = DirectX::XMFLOAT4X4();
8988

9089
uint32_t _modelVertexCount = 0;
9190
uint32_t _modelIndexCount = 0;

0 commit comments

Comments
 (0)