Skip to content

Commit 45e960c

Browse files
committed
Fixed WVP Constant buffer's View member.
Now is attached to the Current scene's camera transform
1 parent 1a010e1 commit 45e960c

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
#include "Core/Core.h"
33
#include "Core/Renderer/ResourceManager.h"
44
#include "Core/Renderer/DescriptorHeap.h"
5+
#include "Core/Scene/SceneManager.h"
6+
57

68
Mesh::Mesh(std::string name, Transform& parentTransform) : Component::Component(name) {
79
this->m_core = Core::GetInstance();
10+
this->m_sceneMgr = SceneManager::GetInstance();
811

912
this->m_dev = nullptr;
1013
this->m_list = nullptr;
@@ -28,12 +31,19 @@ Mesh::Mesh(std::string name, Transform& parentTransform) : Component::Component(
2831
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(this->m_transform.rotation.y)));
2932
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationZ(XMConvertToRadians(this->m_transform.rotation.z)));
3033

31-
this->m_wvp.View = XMMatrixTranspose(XMMatrixIdentity() * XMMatrixTranslation(0.f, 0.f, 2.f));
34+
this->m_wvp.View = XMMatrixTranspose(XMMatrixIdentity());
3235
this->m_wvp.Projection = XMMatrixTranspose(XMMatrixPerspectiveFovLH(XMConvertToRadians(90.f), static_cast<float>(nWidth) / static_cast<float>(nHeight), 0.01f, 3000.f));
3336
}
3437

3538
void Mesh::Init() {
3639
Component::Init();
40+
Transform cameraTransform = this->m_sceneMgr->GetCurrentScene()->GetCurrentCamera()->transform;
41+
this->m_wvp.View = XMMatrixTranspose(XMMatrixIdentity());
42+
this->m_wvp.View *= XMMatrixTranspose(XMMatrixTranslation(cameraTransform.location.x, cameraTransform.location.y, cameraTransform.location.z));
43+
44+
this->m_wvp.View *= XMMatrixTranspose(XMMatrixRotationX(XMConvertToRadians(cameraTransform.rotation.x)));
45+
this->m_wvp.View *= XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(cameraTransform.rotation.y)));
46+
this->m_wvp.View *= XMMatrixTranspose(XMMatrixRotationZ(XMConvertToRadians(cameraTransform.rotation.z)));
3747

3848
if (D3D12* d3d12 = dynamic_cast<D3D12*>(this->m_renderer)) {
3949
this->D3D12Init(d3d12);
@@ -107,7 +117,14 @@ void Mesh::UpdateConstantBuffer() {
107117
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(this->m_transform.rotation.y)));
108118
this->m_wvp.World *= XMMatrixTranspose(XMMatrixRotationZ(XMConvertToRadians(this->m_transform.rotation.z)));
109119

110-
this->m_wvp.View = XMMatrixTranspose(XMMatrixIdentity() * XMMatrixTranslation(0.f, 0.f, 2.f));
120+
Transform cameraTransform = this->m_sceneMgr->GetCurrentScene()->GetCurrentCamera()->transform;
121+
this->m_wvp.View = XMMatrixTranspose(XMMatrixIdentity());
122+
this->m_wvp.View *= XMMatrixTranspose(XMMatrixTranslation(cameraTransform.location.x, cameraTransform.location.y, cameraTransform.location.z));
123+
124+
this->m_wvp.View *= XMMatrixTranspose(XMMatrixRotationX(XMConvertToRadians(cameraTransform.rotation.x)));
125+
this->m_wvp.View *= XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(cameraTransform.rotation.y)));
126+
this->m_wvp.View *= XMMatrixTranspose(XMMatrixRotationZ(XMConvertToRadians(cameraTransform.rotation.z)));
127+
111128
//this->m_wvp.Projection = XMMatrixTranspose(XMMatrixPerspectiveLH(XMConvertToRadians(90.f), static_cast<float>(nWidth) / static_cast<float>(nHeight), 0.01f, 3000.f));
112129

113130
PVOID pData;

src/private/Core/Scene/SceneManager.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Scene* SceneManager::GetScene(std::string name) {
3737
return this->m_scenes[name];
3838
}
3939

40+
Scene* SceneManager::GetCurrentScene() {
41+
return this->m_actualScene;
42+
}
43+
4044
void SceneManager::LoadScene(std::string name) {
4145
if (!(this->m_scenes.count(name) > 0)) {
4246
MessageBox(NULL, "Scene not found", "Error", MB_OK | MB_ICONERROR);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Core;
2323
class Renderer;
2424
class D3D12;
2525
class DescriptorHeap;
26+
class SceneManager;
2627

2728
class Mesh : public Component {
2829
private:
@@ -71,6 +72,7 @@ class Mesh : public Component {
7172
void InitSampler(D3D12* renderer);
7273

7374
Transform m_transform;
75+
SceneManager* m_sceneMgr;
7476
public:
7577
Mesh(std::string name, Transform& parentTransform);
7678

src/public/Core/Scene/SceneManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class SceneManager {
2222
void Update();
2323

2424
Scene* GetScene(std::string name);
25+
Scene* GetCurrentScene();
2526
void AddScene(Scene* scene);
2627

2728
void Render();

0 commit comments

Comments
 (0)