Skip to content

Commit 1bf20a5

Browse files
committed
Light: Started with the implementation of the light constant buffer (Spot light)
1 parent a23eef9 commit 1bf20a5

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
11
#include "Core/GameObject/Light/Light.h"
2+
#include "Core/Core.h"
23

4+
Light::Light(std::string name) : GameObject::GameObject(name) {
5+
/* White by default */
6+
this->m_lightColor[0] = 1.f;
7+
this->m_lightColor[1] = 1.f;
8+
this->m_lightColor[2] = 1.f;
9+
this->m_lightColor[3] = 1.f;
10+
this->m_core = Core::GetInstance();
11+
this->m_renderer = this->m_core->GetRenderer();
12+
}
13+
14+
void Light::Init() {
15+
GameObject::Init();
16+
17+
if (D3D12* renderer = reinterpret_cast<D3D12*>(this->m_renderer)) {
18+
Transform transform = this->transform;
19+
XMVECTOR eye = XMVectorSet(
20+
transform.location.x,
21+
transform.location.y,
22+
transform.location.z,
23+
1.0f
24+
);
25+
26+
float pitch = XMConvertToRadians(transform.rotation.x);
27+
float yaw = XMConvertToRadians(transform.rotation.y);
28+
29+
XMVECTOR forward = XMVectorSet(
30+
cosf(pitch) * sinf(yaw),
31+
-sinf(pitch),
32+
-cosf(pitch) * cosf(yaw),
33+
0.0f
34+
);
35+
36+
XMVECTOR at = XMVectorAdd(eye, forward);
37+
38+
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
39+
this->m_lightBuffer.View = XMMatrixTranspose(XMMatrixLookAtLH(eye, at, up));
40+
this->m_lightBuffer.Projection = XMMatrixTranspose(XMMatrixPerspectiveFovLH(
41+
XMConvertToRadians(70.f),
42+
static_cast<float>(renderer->m_nWidth) / static_cast<float>(renderer->m_nHeight),
43+
0.001f,
44+
3000.f));
45+
}
46+
47+
}
48+
49+
void Light::Update() {
50+
GameObject::Update();
51+
}

src/private/Core/Renderer/ScreenQuad.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ void ScreenQuad::InitConstantBuffer() {
308308

309309
this->m_sqCBuffData.InverseView = XMMatrixInverse(nullptr, this->m_sqCBuffData.InverseView);
310310

311-
this->m_sqCBuffData.InverseProjection = (XMMatrixPerspectiveFovLH(
311+
this->m_sqCBuffData.InverseProjection = XMMatrixTranspose(XMMatrixPerspectiveFovLH(
312312
XMConvertToRadians(70.f),
313313
static_cast<float>(renderer->m_nWidth) / static_cast<float>(renderer->m_nHeight),
314314
0.001f,

src/public/Core/GameObject/Light/Light.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
#pragma once
22
#include <iostream>
33
#include "Core/GameObject/GameObject.h"
4+
#include "Util.h"
5+
#include <DXMath/DirectXMath.h>
6+
7+
using namespace DirectX;
8+
9+
struct LightBuffer {
10+
XMMATRIX View;
11+
XMMATRIX Projection;
12+
XMFLOAT4 LightColor;
13+
};
14+
15+
class Core;
416

517
class Light : public GameObject {
18+
private:
19+
Core* m_core;
20+
Renderer* m_renderer;
621
public:
7-
Light();
22+
RGBA m_lightColor;
23+
24+
LightBuffer m_lightBuffer;
25+
26+
Light(std::string name);
827

928
void Init() override;
1029
void Update() override;

0 commit comments

Comments
 (0)