Skip to content

Commit 13d2c35

Browse files
committedJun 17, 2024
07. Lighting 조명 구조 변경
. 각종 조명 정보를 Light 클래스 하나에서 관리하도록 수정.
1 parent a566253 commit 13d2c35

File tree

11 files changed

+199
-247
lines changed

11 files changed

+199
-247
lines changed
 

‎Computer Graphics/06. Tessellation/shader.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ TerrainShader::TerrainShader(const ComPtr<ID3D12Device>& device,
129129
reinterpret_cast<BYTE*>(mpsByteCode->GetBufferPointer()),
130130
mpsByteCode->GetBufferSize() };
131131
psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
132-
psoDesc.RasterizerState.FillMode = D3D12_FILL_MODE_WIREFRAME;
132+
//psoDesc.RasterizerState.FillMode = D3D12_FILL_MODE_WIREFRAME;
133133
psoDesc.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
134134
psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
135135
psoDesc.SampleMask = UINT_MAX;

‎Computer Graphics/07. Lighting/Shader/billboard.hlsl

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ void GEOMETRY_MAIN(point GEOMETRY_INPUT input[1],
6161
output.position = mul(vertices[i], g_viewMatrix);
6262
output.position = mul(output.position, g_projectionMatrix);
6363
output.positionW = vertices[i].xyz;
64-
float3 side = normalize(vertices[i].xyz - center);
65-
output.normal = normalize(front + side);
64+
output.normal = front;
6665
output.uv = uv[i];
6766
output.textureIndex = input[0].textureIndex;
6867
output.materialIndex = input[0].materialIndex;
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,145 @@
1-
#define MaxDirectionalLight 5
2-
#define MaxPointLight 10
3-
#define MaxSpotLight 130
1+
#define MaxLight 30
2+
#define Directional 0
3+
#define Point 1
4+
#define Spot 2
5+
#define Last 3
46

57
struct MaterialData
68
{
7-
float3 fresnelR0;
8-
float roughness;
9-
float3 ambient;
9+
float3 fresnelR0;
10+
float roughness;
11+
float3 ambient;
1012
};
1113

1214
cbuffer Material : register(b0, space1)
1315
{
14-
MaterialData g_material[4];
16+
MaterialData g_material[4];
1517
};
1618

17-
struct DirectionalLightData
19+
struct LightData
1820
{
19-
float3 strength;
20-
uint padding0;
21-
float3 direction;
22-
uint padding1;
23-
};
24-
25-
struct PointLightData
26-
{
27-
float3 strength;
28-
float fallOffStart;
29-
float3 position;
30-
float fallOffEnd;
31-
};
32-
33-
struct SpotLightData
34-
{
35-
float3 strength;
36-
float fallOffStart;
37-
float3 direction;
38-
float fallOffEnd;
39-
float3 position;
40-
float spotPower;
21+
float3 strength;
22+
float fallOffStart;
23+
float3 direction;
24+
float fallOffEnd;
25+
float3 position;
26+
float spotPower;
27+
uint type;
28+
float3 padding;
4129
};
4230

4331
cbuffer Light : register(b0, space2)
4432
{
45-
uint4 g_lightNum; // x : directional, y : point, z : spot
46-
DirectionalLightData g_directionalLights[MaxDirectionalLight];
47-
PointLightData g_pointLights[MaxPointLight];
48-
SpotLightData g_spotLights[MaxSpotLight];
33+
LightData g_lights[MaxLight];
4934
}
5035

5136
float CalcAttenuation(float d, float fallOffStart, float fallOffEnd)
5237
{
53-
return saturate((fallOffEnd - d) / (fallOffEnd - fallOffStart));
38+
return saturate((fallOffEnd - d) / (fallOffEnd - fallOffStart));
5439
}
5540

5641
// r0 = ((n-1)/(n+1))^2, nÀº ±¼Àý Áö¼öÀÌ´Ù.
5742
float3 SchlickFresnel(float3 r0, float3 normal, float3 lightVector)
5843
{
59-
float cosIncidentAngle = saturate(dot(normal, lightVector));
60-
61-
float f0 = 1.f - cosIncidentAngle;
62-
float3 reflectPercent = r0 + (1.f - r0) * (f0 * f0 * f0 * f0 * f0);
44+
float cosIncidentAngle = saturate(dot(normal, lightVector));
45+
46+
float f0 = 1.f - cosIncidentAngle;
47+
float3 reflectPercent = r0 + (1.f - r0) * (f0 * f0 * f0 * f0 * f0);
6348

64-
return reflectPercent;
49+
return reflectPercent;
6550
}
6651

6752
float3 BlinnPhong(float3 lightStrength, float3 lightVector, float3 normal,
68-
float3 toEye, float3 diffuse, MaterialData material)
53+
float3 toEye, float3 diffuse, MaterialData material)
6954
{
70-
float shininess = 1.f - material.roughness;
71-
const float m = shininess * 256.f;
72-
float3 halfVector = normalize(toEye + lightVector);
73-
74-
float roughnessFactor = (m + 8.f) * pow(max(dot(halfVector, normal), 0.f), m) / 8.f;
75-
float3 fresnelFactor = SchlickFresnel(material.fresnelR0, halfVector, lightVector);
55+
float shininess = 1.f - material.roughness;
56+
const float m = shininess * 256.f;
57+
float3 halfVector = normalize(toEye + lightVector);
7658

77-
float3 specular = fresnelFactor * roughnessFactor;
59+
float roughnessFactor = (m + 8.f) * pow(max(dot(halfVector, normal), 0.f), m) / 8.f;
60+
float3 fresnelFactor = SchlickFresnel(material.fresnelR0, halfVector, lightVector);
7861

79-
specular = specular / (specular + 1.f);
62+
float3 specular = fresnelFactor * roughnessFactor;
63+
specular = specular / (specular + 1.f);
8064

81-
return (diffuse + specular) * lightStrength;
65+
return (diffuse + specular) * lightStrength;
8266
}
8367

84-
float3 ComputeDirectionalLight(DirectionalLightData light, float3 normal,
85-
float3 toEye, float3 diffuse, MaterialData material)
68+
float3 ComputeDirectionalLight(LightData light, float3 normal,
69+
float3 toEye, float3 diffuse, MaterialData material)
8670
{
87-
float3 lightDirection = -light.direction;
71+
float3 lightDirection = -light.direction;
8872

89-
float ndotl = max(dot(lightDirection, normal), 0.0f);
90-
float3 lightStrength = light.strength * ndotl;
73+
float ndotl = max(dot(lightDirection, normal), 0.0f);
74+
float3 lightStrength = light.strength * ndotl;
9175

92-
return BlinnPhong(lightStrength, lightDirection, normal, toEye, diffuse, material);
76+
return BlinnPhong(lightStrength, lightDirection, normal, toEye, diffuse, material);
9377
}
9478

95-
float3 ComputePointLight(PointLightData light, float3 objectPosition, float3 normal,
96-
float3 toEye, float3 diffuse, MaterialData material)
79+
float3 ComputePointLight(LightData light, float3 objectPosition, float3 normal,
80+
float3 toEye, float3 diffuse, MaterialData material)
9781
{
98-
float3 lightVector = light.position - objectPosition;
99-
float d = length(lightVector);
82+
float3 lightVector = light.position - objectPosition;
83+
float d = length(lightVector);
10084

101-
if (d > light.fallOffEnd) return float3(0.f, 0.f, 0.f);
85+
if (d > light.fallOffEnd) return float3(0.f, 0.f, 0.f);
10286

103-
lightVector /= d;
104-
float ndotl = max(dot(lightVector, normal), 0.f);
105-
float3 lightStrength = light.strength * ndotl;
87+
lightVector /= d;
88+
float ndotl = max(dot(lightVector, normal), 0.f);
89+
float3 lightStrength = light.strength * ndotl;
10690

107-
float attenuation = CalcAttenuation(d, light.fallOffStart, light.fallOffEnd);
108-
lightStrength *= attenuation;
91+
float attenuation = CalcAttenuation(d, light.fallOffStart, light.fallOffEnd);
92+
lightStrength *= attenuation;
10993

110-
return BlinnPhong(lightStrength, lightVector, normal, toEye, diffuse, material);
94+
return BlinnPhong(lightStrength, lightVector, normal, toEye, diffuse, material);
11195
}
11296

113-
float3 ComputeSpotLight(SpotLightData light, float3 objectPosition, float3 normal,
114-
float3 toEye, float3 diffuse, MaterialData material)
97+
float3 ComputeSpotLight(LightData light, float3 objectPosition, float3 normal,
98+
float3 toEye, float3 diffuse, MaterialData material)
11599
{
116-
float3 lightVector = light.position - objectPosition;
117-
float d = length(lightVector);
100+
float3 lightVector = light.position - objectPosition;
101+
float d = length(lightVector);
118102

119-
if (d > light.fallOffEnd) return 0.0f;
103+
if (d > light.fallOffEnd) return 0.0f;
120104

121-
lightVector /= d;
122-
float ndotl = max(dot(lightVector, normal), 0.0f);
123-
float3 lightStrength = light.strength * ndotl;
105+
lightVector /= d;
106+
float ndotl = max(dot(lightVector, normal), 0.0f);
107+
float3 lightStrength = light.strength * ndotl;
124108

125-
float attenuation = CalcAttenuation(d, light.fallOffStart, light.fallOffEnd);
126-
lightStrength *= attenuation;
109+
float attenuation = CalcAttenuation(d, light.fallOffStart, light.fallOffEnd);
110+
lightStrength *= attenuation;
127111

128-
float spotFactor = pow(max(dot(-lightVector, light.direction), 0.f), light.spotPower);
129-
lightStrength *= spotFactor;
112+
float spotFactor = pow(max(dot(-lightVector, light.direction), 0.f), light.spotPower);
113+
lightStrength *= spotFactor;
130114

131-
return BlinnPhong(lightStrength, lightVector, normal, toEye, diffuse, material);
115+
return BlinnPhong(lightStrength, lightVector, normal, toEye, diffuse, material);
132116
}
133117

134118
float4 Lighting(float3 objectPosition, float3 normal,
135-
float3 toEye, float4 diffuse, MaterialData material)
119+
float3 toEye, float4 diffuse, MaterialData material)
136120
{
137-
float3 output = float3(0.f, 0.f, 0.f);
138-
139-
for (int i = 0; i < g_lightNum.x; ++i) {
140-
output += ComputeDirectionalLight(g_directionalLights[i], normal, toEye, diffuse.rgb, material);
141-
}
142-
for (int j = 0; j < g_lightNum.y; ++j) {
143-
output += ComputePointLight(g_pointLights[j], objectPosition, normal, toEye, diffuse.rgb, material);
144-
}
145-
for (int k = 0; k < g_lightNum.z; ++k) {
146-
output += ComputeSpotLight(g_spotLights[k], objectPosition, normal, toEye, diffuse.rgb, material);
147-
}
148-
149-
float3 ambient = material.ambient * diffuse.rgb;
150-
output += ambient;
151-
152-
return float4(output, diffuse.a);
121+
float3 output = float3(0.f, 0.f, 0.f);
122+
123+
for (int i = 0; i < MaxLight; ++i)
124+
{
125+
if (g_lights[i].type == Last) break;
126+
127+
switch (g_lights[i].type)
128+
{
129+
case Directional:
130+
output += ComputeDirectionalLight(g_lights[i], normal, toEye, diffuse.rgb, material);
131+
break;
132+
case Point:
133+
output += ComputePointLight(g_lights[i], objectPosition, normal, toEye, diffuse.rgb, material);
134+
break;
135+
case Spot:
136+
output += ComputeSpotLight(g_lights[i], objectPosition, normal, toEye, diffuse.rgb, material);
137+
break;
138+
}
139+
}
140+
141+
float3 ambient = material.ambient * diffuse.rgb;
142+
output += ambient;
143+
144+
return float4(output, diffuse.a);
153145
}
+49-68
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,66 @@
11
#include "light.h"
22

3-
Light::Light() : m_strength{ 1.f, 1.f, 1.f }
3+
Light::Light(UINT type, XMFLOAT3 strength, XMFLOAT3 direction) :
4+
m_type{type}, m_strength{strength}, m_direction{direction}, m_position{ 0.f, 0.f, 0.f },
5+
m_fallOffStart{ 0.f }, m_fallOffEnd{ 0.f }, m_spotPower{ 0.f }
46
{
7+
m_direction = Utiles::Vector3::Normalize(m_direction);
58
}
69

7-
Light::Light(XMFLOAT3 strength) : m_strength{ strength }
10+
Light::Light(UINT type, XMFLOAT3 strength, XMFLOAT3 position,
11+
FLOAT fallOffStart, FLOAT fallOffEnd) :
12+
m_type{ type }, m_strength{ strength }, m_direction{ 0.f, 0.f, 0.f }, m_position{ position },
13+
m_fallOffStart{ fallOffStart }, m_fallOffEnd{ fallOffEnd }, m_spotPower{ 0.f }
814
{
915
}
1016

11-
void Light::SetStrength(XMFLOAT3 strength)
17+
Light::Light(UINT type, XMFLOAT3 strength, XMFLOAT3 direction, XMFLOAT3 position,
18+
FLOAT fallOffStart, FLOAT fallOffEnd, FLOAT spotPower) :
19+
m_type{ type }, m_strength{ strength }, m_direction{ direction }, m_position{ position },
20+
m_fallOffStart{ fallOffStart }, m_fallOffEnd{ fallOffEnd }, m_spotPower{ spotPower }
1221
{
13-
m_strength = strength;
22+
m_direction = Utiles::Vector3::Normalize(m_direction);
1423
}
1524

16-
DirectionalLight::DirectionalLight() : Light(),
17-
m_direction{ 0.f, -1.f, 0.f }
25+
void Light::UpdateShaderVariable(LightData& buffer)
1826
{
27+
buffer.type = m_type;
28+
buffer.strength = m_strength;
29+
buffer.direction = m_direction;
30+
buffer.position = m_position;
31+
buffer.fallOffStart = m_fallOffStart;
32+
buffer.fallOffEnd = m_fallOffEnd;
33+
buffer.spotPower = m_spotPower;
1934
}
2035

21-
DirectionalLight::DirectionalLight(XMFLOAT3 strength, XMFLOAT3 direction) : Light(strength), m_direction{ direction }
36+
void Light::SetStrength(XMFLOAT3 strength)
2237
{
23-
m_direction = Utiles::Vector3::Normalize(m_direction);
38+
m_strength = strength;
2439
}
2540

26-
void DirectionalLight::UpdateShaderVariable(DirectionalLightData& buffer)
41+
DirectionalLight::DirectionalLight() :
42+
Light(Settings::Light::Directional, { 1.f, 1.f, 1.f }, { 0.f, -1.f, 0.f })
2743
{
28-
buffer.strength = m_strength;
29-
buffer.direction = m_direction;
3044
}
3145

32-
void DirectionalLight::SetDirection(XMFLOAT3 direction)
46+
DirectionalLight::DirectionalLight(XMFLOAT3 strength, XMFLOAT3 direction) :
47+
Light(Settings::Light::Directional, strength, direction)
3348
{
34-
m_direction = Utiles::Vector3::Normalize(direction);
3549
}
3650

37-
PointLight::PointLight() : Light(),
38-
m_position{ 0.f, 0.f, 0.f }, m_fallOffStart{ 0.1f }, m_fallOffEnd{ 10.f }
51+
void DirectionalLight::SetDirection(XMFLOAT3 direction)
3952
{
53+
m_direction = Utiles::Vector3::Normalize(direction);
4054
}
4155

42-
PointLight::PointLight(XMFLOAT3 strength, XMFLOAT3 position, FLOAT fallOffStart, FLOAT fallOffEnd) :
43-
Light(strength), m_position {position}, m_fallOffStart{fallOffStart}, m_fallOffEnd{fallOffEnd}
56+
PointLight::PointLight() :
57+
Light(Settings::Light::Point, {1.f, 1.f, 1.f}, { 0.f, 0.f, 0.f }, { 0.1f }, { 10.f })
4458
{
4559
}
4660

47-
void PointLight::UpdateShaderVariable(PointLightData& buffer)
61+
PointLight::PointLight(XMFLOAT3 strength, XMFLOAT3 position, FLOAT fallOffStart, FLOAT fallOffEnd) :
62+
Light(Settings::Light::Point, strength, position, fallOffStart, fallOffEnd)
4863
{
49-
buffer.strength = m_strength;
50-
buffer.position = m_position;
51-
buffer.fallOffStart = m_fallOffStart;
52-
buffer.fallOffEnd = m_fallOffEnd;
5364
}
5465

5566
void PointLight::SetPosition(XMFLOAT3 position)
@@ -68,28 +79,16 @@ void PointLight::SetFallOffEnd(FLOAT fallOffEnd)
6879
}
6980

7081
SpotLight::SpotLight() :
71-
Light(), m_direction{ 0.f, 1.f, 0.f }, m_position{ 0.f, 0.f, 0.f },
72-
m_fallOffStart{ 0.1f }, m_fallOffEnd{ 10.f }, m_spotPower{ 10.f }
82+
Light(Settings::Light::Spot, { 1.f, 1.f, 1.f }, {0.f, -1.f, 0.f},
83+
{ 0.f, 0.f, 0.f }, { 0.1f }, { 10.f }, {10.f})
7384
{
74-
m_direction = Utiles::Vector3::Normalize(m_direction);
7585
}
7686

7787
SpotLight::SpotLight(XMFLOAT3 strength, XMFLOAT3 direction, XMFLOAT3 position,
7888
FLOAT fallOffStart, FLOAT fallOffEnd, FLOAT spotPower) :
79-
Light(strength), m_direction{direction}, m_position{position},
80-
m_fallOffStart{fallOffStart}, m_fallOffEnd{fallOffEnd}, m_spotPower{spotPower}
89+
Light(Settings::Light::Spot, strength, direction, position,
90+
fallOffStart, fallOffEnd, spotPower)
8191
{
82-
m_direction = Utiles::Vector3::Normalize(m_direction);
83-
}
84-
85-
void SpotLight::UpdateShaderVariable(SpotLightData& buffer)
86-
{
87-
buffer.strength = m_strength;
88-
buffer.direction = m_direction;
89-
buffer.position = m_position;
90-
buffer.fallOffStart = m_fallOffStart;
91-
buffer.fallOffEnd = m_fallOffEnd;
92-
buffer.spotPower = m_spotPower;
9392
}
9493

9594
void SpotLight::SetDirection(XMFLOAT3 direction)
@@ -117,43 +116,25 @@ void SpotLight::SetSpotPower(FLOAT spotPower)
117116
m_spotPower = spotPower;
118117
}
119118

120-
LightSystem::LightSystem(const ComPtr<ID3D12Device>& device) : m_lightNum{ 0, 0, 0, 0 }
119+
LightSystem::LightSystem(const ComPtr<ID3D12Device>& device)
121120
{
122-
m_constantBuffer = make_unique<UploadBuffer<LightData>>(device, (UINT)RootParameter::Light);
121+
m_constantBuffer = make_unique<UploadBuffer<LightsData>>(device, (UINT)RootParameter::Light);
123122
}
124123

125124
void LightSystem::UpdateShaderVariable(const ComPtr<ID3D12GraphicsCommandList>& commandList)
126125
{
127-
LightData buffer;
128-
buffer.lightNum = m_lightNum;
129-
for (int i = 0; const auto& directionalLight : m_directionalLights) {
130-
directionalLight->UpdateShaderVariable(buffer.directionalLights[i++]); }
131-
for (int i = 0; const auto& pointLight : m_pointLights) {
132-
pointLight->UpdateShaderVariable(buffer.pointLights[i++]); }
133-
for (int i = 0; const auto& spotLight : m_spotLights) {
134-
spotLight->UpdateShaderVariable(buffer.spotLights[i++]); }
135-
m_constantBuffer->Copy(buffer);
126+
LightsData buffer;
127+
int i = 0;
128+
for (const auto& light : m_lights) {
129+
light->UpdateShaderVariable(buffer.lights[i++]); }
130+
if (i != Settings::Light::MaxLight) buffer.lights[i].type = Settings::Light::Last;
136131

132+
m_constantBuffer->Copy(buffer);
137133
m_constantBuffer->UpdateRootConstantBuffer(commandList);
138134
}
139135

140-
void LightSystem::SetDirectionalLight(shared_ptr<DirectionalLight> directionalLight)
136+
void LightSystem::SetLight(const shared_ptr<Light>& light)
141137
{
142-
if (m_directionalLights.size() == Settings::MaxDirectionalLight) assert("");
143-
m_directionalLights.push_back(directionalLight);
144-
m_lightNum.x = static_cast<UINT>(m_directionalLights.size());
145-
}
146-
147-
void LightSystem::SetPointLight(shared_ptr<PointLight> pointLight)
148-
{
149-
if (m_pointLights.size() == Settings::MaxPointLight) assert("");
150-
m_pointLights.push_back(pointLight);
151-
m_lightNum.y = static_cast<UINT>(m_pointLights.size());
152-
}
153-
154-
void LightSystem::SetSpotLight(shared_ptr<SpotLight> spotLight)
155-
{
156-
if (m_spotLights.size() == Settings::MaxSpotLight) assert("");
157-
m_spotLights.push_back(spotLight);
158-
m_lightNum.z = static_cast<UINT>(m_spotLights.size());
159-
}
138+
if (m_lights.size() == Settings::Light::MaxLight) throw out_of_range("Limit Max Light");
139+
m_lights.push_back(light);
140+
}

‎Computer Graphics/07. Lighting/light.h

+31-65
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,38 @@
22
#include "stdafx.h"
33
#include "buffer.h"
44

5-
class Light
5+
struct LightData
6+
{
7+
XMFLOAT3 strength;
8+
FLOAT fallOffStart;
9+
XMFLOAT3 direction;
10+
FLOAT fallOffEnd;
11+
XMFLOAT3 position;
12+
FLOAT spotPower;
13+
UINT type;
14+
XMFLOAT3 padding;
15+
};
16+
17+
class Light abstract
618
{
719
public:
8-
Light();
9-
Light(XMFLOAT3 strength);
20+
Light(UINT type, XMFLOAT3 strength, XMFLOAT3 direction);
21+
Light(UINT type, XMFLOAT3 strength, XMFLOAT3 position, FLOAT fallOffStart, FLOAT fallOffEnd);
22+
Light(UINT type, XMFLOAT3 strength, XMFLOAT3 direction, XMFLOAT3 position,
23+
FLOAT fallOffStart, FLOAT fallOffEnd, FLOAT spotPower);
24+
25+
void UpdateShaderVariable(LightData& buffer);
1026

11-
void SetStrength(XMFLOAT3 strength);
27+
void SetStrength(XMFLOAT3 direction);
1228

1329
protected:
30+
const UINT m_type;
1431
XMFLOAT3 m_strength;
15-
};
16-
17-
struct DirectionalLightData
18-
{
19-
XMFLOAT3 strength;
20-
UINT padding0;
21-
XMFLOAT3 direction;
22-
UINT padding1;
32+
XMFLOAT3 m_direction;
33+
XMFLOAT3 m_position;
34+
FLOAT m_fallOffStart;
35+
FLOAT m_fallOffEnd;
36+
FLOAT m_spotPower;
2337
};
2438

2539
class DirectionalLight : public Light
@@ -28,20 +42,7 @@ class DirectionalLight : public Light
2842
DirectionalLight();
2943
DirectionalLight(XMFLOAT3 strength, XMFLOAT3 direction);
3044

31-
void UpdateShaderVariable(DirectionalLightData& buffer);
32-
3345
void SetDirection(XMFLOAT3 direction);
34-
35-
private:
36-
XMFLOAT3 m_direction;
37-
};
38-
39-
struct PointLightData
40-
{
41-
XMFLOAT3 strength;
42-
FLOAT fallOffStart;
43-
XMFLOAT3 position;
44-
FLOAT fallOffEnd;
4546
};
4647

4748
class PointLight : public Light
@@ -50,26 +51,9 @@ class PointLight : public Light
5051
PointLight();
5152
PointLight(XMFLOAT3 strength, XMFLOAT3 position, FLOAT fallOffStart, FLOAT fallOffEnd);
5253

53-
void UpdateShaderVariable(PointLightData& buffer);
54-
5554
void SetPosition(XMFLOAT3 position);
5655
void SetFallOffStart(FLOAT fallOffStart);
5756
void SetFallOffEnd(FLOAT fallOffEnd);
58-
59-
private:
60-
XMFLOAT3 m_position;
61-
FLOAT m_fallOffStart;
62-
FLOAT m_fallOffEnd;
63-
};
64-
65-
struct SpotLightData
66-
{
67-
XMFLOAT3 strength;
68-
FLOAT fallOffStart;
69-
XMFLOAT3 direction;
70-
FLOAT fallOffEnd;
71-
XMFLOAT3 position;
72-
FLOAT spotPower;
7357
};
7458

7559
class SpotLight : public Light
@@ -79,28 +63,16 @@ class SpotLight : public Light
7963
SpotLight(XMFLOAT3 strength, XMFLOAT3 direction, XMFLOAT3 position,
8064
FLOAT fallOffStart, FLOAT fallOffEnd, FLOAT spotPower);
8165

82-
void UpdateShaderVariable(SpotLightData& buffer);
83-
8466
void SetDirection(XMFLOAT3 direction);
8567
void SetPosition(XMFLOAT3 position);
8668
void SetFallOffStart(FLOAT fallOffStart);
8769
void SetFallOffEnd(FLOAT fallOffEnd);
8870
void SetSpotPower(FLOAT spotPower);
89-
90-
private:
91-
XMFLOAT3 m_direction;
92-
XMFLOAT3 m_position;
93-
FLOAT m_fallOffStart;
94-
FLOAT m_fallOffEnd;
95-
FLOAT m_spotPower;
9671
};
9772

98-
struct LightData : public BufferBase
73+
struct LightsData : public BufferBase
9974
{
100-
XMUINT4 lightNum;
101-
DirectionalLightData directionalLights[Settings::MaxDirectionalLight];
102-
PointLightData pointLights[Settings::MaxPointLight];
103-
SpotLightData spotLights[Settings::MaxSpotLight];
75+
LightData lights[Settings::Light::MaxLight];
10476
};
10577

10678
class LightSystem
@@ -111,16 +83,10 @@ class LightSystem
11183

11284
void UpdateShaderVariable(const ComPtr<ID3D12GraphicsCommandList>& commandList);
11385

114-
void SetDirectionalLight(shared_ptr<DirectionalLight> directionalLight);
115-
void SetPointLight(shared_ptr<PointLight> pointLight);
116-
void SetSpotLight(shared_ptr<SpotLight> spotLight);
86+
void SetLight(const shared_ptr<Light>& light);
11787

11888
private:
119-
XMUINT4 m_lightNum;
120-
vector<shared_ptr<DirectionalLight>> m_directionalLights;
121-
vector<shared_ptr<PointLight>> m_pointLights;
122-
vector<shared_ptr<SpotLight>> m_spotLights;
123-
124-
unique_ptr<UploadBuffer<LightData>> m_constantBuffer;
89+
vector<shared_ptr<Light>> m_lights;
90+
unique_ptr<UploadBuffer<LightsData>> m_constantBuffer;
12591
};
12692

‎Computer Graphics/07. Lighting/object.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void Sun::SetStrength(XMFLOAT3 strength)
150150

151151
void Sun::Update(FLOAT timeElapsed)
152152
{
153-
m_phi += timeElapsed * 0.4f;
153+
m_phi += timeElapsed * 0.1f;
154154

155155
FLOAT cosine = cos(m_phi);
156156
XMFLOAT3 offset{
@@ -162,4 +162,5 @@ void Sun::Update(FLOAT timeElapsed)
162162
m_light->SetDirection(Utiles::Vector3::Negate(GetPosition()));
163163
m_light->SetStrength(cosine > 0.f ?
164164
Utiles::Vector3::Mul(m_strength, cosine) : XMFLOAT3{0.f,0.f,0.f});
165+
//m_light->SetStrength(Utiles::Vector3::Mul(m_strength, cosine));
165166
}

‎Computer Graphics/07. Lighting/scene.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ inline void Scene::BuildObjects(const ComPtr<ID3D12Device>& device)
166166
{
167167
m_lightSystem = make_unique<LightSystem>(device);
168168
auto sunLight = make_shared<DirectionalLight>();
169-
m_lightSystem->SetDirectionalLight(sunLight);
169+
m_lightSystem->SetLight(sunLight);
170170

171171
m_sun = make_unique<Sun>(sunLight);
172172
m_sun->SetStrength(XMFLOAT3{ 1.3f, 1.2f, 1.2f });
@@ -175,15 +175,15 @@ inline void Scene::BuildObjects(const ComPtr<ID3D12Device>& device)
175175
m_player->SetPosition(XMFLOAT3{ 0.f, 0.f, 0.f });
176176
m_player->SetTextureIndex(0);
177177

178-
for (int x = -10; x <= 10; x += 5) {
179-
for (int y = 0; y <= 20; y += 5) {
180-
for (int z = -10; z <= 10; z += 5) {
178+
for (int x = -10; x <= 10; x += 10) {
179+
for (int y = 0; y <= 20; y += 10) {
180+
for (int z = -10; z <= 10; z += 10) {
181181
auto light = make_shared<SpotLight>(
182182
XMFLOAT3{ 0.7f, 0.7f, 0.7f },
183183
XMFLOAT3{ 1.f, 0.f, 0.f },
184184
XMFLOAT3{ 0.f, 0.f, 0.f },
185185
1.f, 50.f, 80.f);
186-
m_lightSystem->SetSpotLight(light);
186+
m_lightSystem->SetLight(light);
187187
auto object = make_shared<LightObject>(light);
188188
object->SetPosition(XMFLOAT3{
189189
static_cast<FLOAT>(x),

‎Computer Graphics/07. Lighting/settings.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ namespace Settings
1616

1717
constexpr FLOAT PlayerSpeed = 10.f;
1818

19-
constexpr UINT MaxDirectionalLight = 5;
20-
constexpr UINT MaxPointLight = 10;
21-
constexpr UINT MaxSpotLight = 130;
19+
namespace Light
20+
{
21+
constexpr UINT MaxLight = 100;
22+
constexpr UINT Directional = 0;
23+
constexpr UINT Point = 1;
24+
constexpr UINT Spot = 2;
25+
constexpr UINT Last = 3;
26+
}
2227
}
2328

2429
namespace RootParameter

‎Computer Graphics/08. Shadow/object.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void LightObject::Update(FLOAT timeElapsed)
139139
}
140140

141141
Sun::Sun(const shared_ptr<DirectionalLight>& light) : m_light{ light },
142-
m_strength{ 1.f, 1.f, 1.f }, m_phi{ 0.f }, m_theta{ 0.f }, m_radius{ Settings::SunRadius }
142+
m_strength{ 1.f, 1.f, 1.f }, m_phi{ XM_1DIV2PI + 0.5f }, m_theta{ XM_PIDIV4 }, m_radius{ Settings::SunRadius }
143143
{
144144
}
145145

@@ -150,7 +150,7 @@ void Sun::SetStrength(XMFLOAT3 strength)
150150

151151
void Sun::Update(FLOAT timeElapsed)
152152
{
153-
m_phi += timeElapsed * 0.4f;
153+
//m_phi += timeElapsed * 0.4f;
154154

155155
FLOAT cosine = cos(m_phi);
156156
XMFLOAT3 offset{

‎Computer Graphics/08. Shadow/scene.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ inline void Scene::BuildObjects(const ComPtr<ID3D12Device>& device)
199199
m_player->SetPosition(XMFLOAT3{ 0.f, 0.f, 0.f });
200200
m_player->SetTextureIndex(0);
201201

202-
for (int x = -10; x <= 10; x += 5) {
203-
for (int y = 0; y <= 20; y += 5) {
204-
for (int z = -10; z <= 10; z += 5) {
202+
for (int x = -10; x <= 10; x += 10) {
203+
for (int y = 0; y <= 20; y += 10) {
204+
for (int z = -10; z <= 10; z += 10) {
205205
auto light = make_shared<SpotLight>(device,
206206
XMFLOAT3{ 0.7f, 0.7f, 0.7f },
207207
XMFLOAT3{ 1.f, 0.f, 0.f },

‎Computer Graphics/08. Shadow/settings.h

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ namespace Settings
1919
constexpr UINT MaxDirectionalLight = 5;
2020
constexpr UINT MaxPointLight = 10;
2121
constexpr UINT MaxSpotLight = 130;
22+
23+
namespace Light
24+
{
25+
constexpr UINT MaxLight = 30;
26+
constexpr UINT Directional = 0;
27+
constexpr UINT Point = 1;
28+
constexpr UINT Spot = 2;
29+
}
2230
}
2331

2432
namespace RootParameter

0 commit comments

Comments
 (0)
Please sign in to comment.