Skip to content

Commit 961b62e

Browse files
committed
Started with the World View Projection constant buffer
1 parent 5e42566 commit 961b62e

File tree

6 files changed

+43
-4
lines changed

6 files changed

+43
-4
lines changed

src/private/Core/Core.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ Core::Core() {
1010
}
1111

1212
void Core::Init() {
13+
RECT rect;
14+
GetWindowRect(this->m_hwnd, &rect);
15+
16+
this->m_nWidth = rect.right - rect.left;
17+
this->m_nHeight = rect.bottom - rect.top;
18+
1319
this->m_renderer = new D3D12();
1420
m_renderer->Init(this->m_hwnd);
1521
m_sceneManager->Init();
@@ -26,6 +32,11 @@ void Core::MainLoop() {
2632
this->m_renderer->Update();
2733
}
2834

35+
void Core::GetWindowSize(UINT& nWidth, UINT& nHeight) {
36+
nWidth = this->m_nWidth;
37+
nHeight = this->m_nHeight;
38+
}
39+
2940
Core* Core::GetInstance() {
3041
if (Core::m_instance == nullptr)
3142
Core::m_instance = new Core();

src/private/Core/GameObject/Component/Mesh.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "Core/Renderer/ResourceManager.h"
44
#include "Core/Renderer/DescriptorHeap.h"
55

6-
Mesh::Mesh(std::string name) : Component::Component(name) {
6+
Mesh::Mesh(std::string name, Transform& parentTransform) : Component::Component(name) {
77
this->m_core = Core::GetInstance();
88

99
this->m_dev = nullptr;
@@ -16,12 +16,24 @@ Mesh::Mesh(std::string name) : Component::Component(name) {
1616
this->m_nSamplerIndex = -1; // I put -1 cuz 0 can actually be occupied.
1717
this->m_nTotalVertices = 0;
1818
this->m_shader = nullptr;
19+
20+
this->m_transform = parentTransform;
21+
22+
UINT nWidth, nHeight = 0;
23+
this->m_core->GetWindowSize(nWidth, nHeight);
24+
25+
this->m_wvp.World = XMMatrixTranspose(XMMatrixIdentity() * XMMatrixTranslation(this->m_transform.location.x, this->m_transform.location.y, this->m_transform.location.z));
26+
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationX(XMConvertToRadians(this->m_transform.rotation.x)));
27+
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(this->m_transform.rotation.y)));
28+
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationZ(XMConvertToRadians(this->m_transform.rotation.z)));
29+
30+
this->m_wvp.View = XMMatrixTranspose(XMMatrixIdentity());
31+
this->m_wvp.Projection = XMMatrixTranspose(XMMatrixPerspectiveLH(XMConvertToRadians(90.f), static_cast<float>(nWidth) / static_cast<float>(nHeight), 0.01f, 3000.f));
1932
}
2033

2134
void Mesh::Init() {
2235
Component::Init();
2336

24-
2537
if (D3D12* d3d12 = dynamic_cast<D3D12*>(this->m_renderer)) {
2638
this->D3D12Init(d3d12);
2739
}

src/private/Core/GameObject/GameObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ GameObject::GameObject(std::string name) {
66
}
77

88
void GameObject::Init() {
9-
this->m_mesh = new Mesh("StaticMeshComponent");
9+
this->m_mesh = new Mesh("StaticMeshComponent", this->transform);
1010
this->m_components.push_back(this->m_mesh);
1111
this->m_mesh->LoadModel("f16.fbx");
1212
for (Component* component : this->m_components) {

src/public/Core/Core.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class Core {
1616

1717
SceneManager* m_sceneManager;
1818
ResourceManager* m_resMgr;
19+
20+
UINT m_nWidth;
21+
UINT m_nHeight;
1922
public:
2023
Core();
2124
void Init();
@@ -24,6 +27,7 @@ class Core {
2427

2528
static Core* GetInstance();
2629

30+
void GetWindowSize(UINT& nWidth, UINT& nHeight);
2731

2832
void MainLoop();
2933

src/public/Core/GameObject/Component/Mesh.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "Core/GameObject/Component/Component.h"
1515
#include "Core/Renderer/Shader.h"
1616
#include "Core/Renderer/Descriptor.h"
17+
#include "Math/Transform.h"
1718

1819
using namespace Microsoft::WRL;
1920

@@ -62,8 +63,12 @@ class Mesh : public Component {
6263
void UploadVertices();
6364
void InitPipeline();
6465
void InitSampler(D3D12* renderer);
66+
67+
WVP m_wvp;
68+
69+
Transform m_transform;
6570
public:
66-
Mesh(std::string name);
71+
Mesh(std::string name, Transform& parentTransform);
6772

6873
void Init();
6974
void Update();

src/public/Util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
#pragma once
22
#include <iostream>
33
#include <Windows.h>
4+
#include <DXMath/DirectXMath.h>
45

56
typedef float RGBA[4];
67
typedef float RGB[3];
78
typedef float RG[2];
89

10+
struct WVP {
11+
XMMATRIX World;
12+
XMMATRIX View;
13+
XMMATRIX Projection;
14+
};
15+
916
struct Vertex {
1017
RGB xyz;
1118
RGB normal;

0 commit comments

Comments
 (0)