Skip to content

Commit 20e5aaa

Browse files
committed
Implemented directional, point and spot lighting sources
1 parent 2ccff82 commit 20e5aaa

File tree

8 files changed

+118
-30
lines changed

8 files changed

+118
-30
lines changed

include/core/ecs/component.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace Engine {
3434
const Type type = Type::TRANSFORM;
3535

3636
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
37-
glm::vec3 rotation = glm::vec3(1.0f, 1.0f, 1.0f);
37+
glm::vec3 rotation = glm::vec3(0.0f, 1.0f, 1.0f);
3838
glm::vec3 scale = glm::vec3(1.0f, 1.0f, 1.0f);
3939
};
4040

@@ -87,7 +87,8 @@ namespace Engine {
8787
float quadratic = 0.44f;
8888

8989
// Only for spot lighting
90-
float angle = glm::cos(glm::radians(0.0f));
90+
float cutOff = 12.5f;
91+
float outerCutOff = 17.5f;
9192

9293
Light(Graphics::Lighting::Type type) : lightType(type) {};
9394
};

include/core/graphics/lighting/base.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ namespace Engine {
3333
light = std::make_unique<Ecs::Component::Light>(Type::POINT);
3434
};
3535
};
36+
37+
38+
class SpotLight : public Light {
39+
public:
40+
SpotLight() : Light() {
41+
name = "Spot light";
42+
light = std::make_unique<Ecs::Component::Light>(Type::SPOT);
43+
};
44+
};
3645
}
3746
}
3847
}

include/meta.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef __META_HPP__
22
#define __META_HPP__
33

4-
#define ENGINE_VERSION "0.1.1"
4+
#define ENGINE_VERSION "0.1.2"
55
#define GLSL_VERSION "#version 420"
66

77
#endif

resources/shaders/default.frag

+38-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ struct Light {
99
float linear;
1010
float quadratic;
1111

12+
// Only for spot lighting
13+
float cutOff;
14+
float outerCutOff;
15+
1216
// Type of lighting
1317
int isDirection;
1418
int isPoint;
@@ -31,9 +35,9 @@ uniform Light light;
3135
uniform vec3 viewPosition;
3236

3337

34-
vec3 calculateDirectionLight(Light light, vec3 normal, vec3 viewDir);
35-
vec3 calculatePointLight(Light light, vec3 normal, vec3 fragPos, vec3 viewDir);
36-
vec3 calculateSpotLight();
38+
vec3 calculateDirectionLight(Light, vec3, vec3);
39+
vec3 calculatePointLight(Light, vec3, vec3, vec3);
40+
vec3 calculateSpotLight(Light, vec3, vec3, vec3);
3741

3842

3943
void main() {
@@ -49,7 +53,7 @@ void main() {
4953
result = calculatePointLight(light, norm, FragPos, viewDir);
5054
}
5155
else if (light.isSpot == 1) {
52-
result = calculateSpotLight();
56+
result = calculateSpotLight(light, norm, FragPos, viewDir);
5357
}
5458

5559
color = vec4(result * defaultColor * light.intensity, 1.0f);
@@ -99,6 +103,34 @@ vec3 calculatePointLight(Light light, vec3 normal, vec3 fragPos, vec3 viewDir) {
99103
}
100104

101105

102-
vec3 calculateSpotLight() {
103-
return vec3(0.0f);
106+
vec3 calculateSpotLight(Light light, vec3 normal, vec3 fragPos, vec3 viewDir) {
107+
// Ambient
108+
vec3 ambient = light.ambient * light.color;
109+
110+
// Diffuse
111+
vec3 lightDir = normalize(light.position - fragPos);
112+
float diff = max(dot(normal, lightDir), 0.0);
113+
vec3 diffuse = light.diffuse * (diff * light.color);
114+
115+
// Specular
116+
vec3 reflectDir = reflect(-lightDir, normal);
117+
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
118+
vec3 specular = light.specular * (spec * light.color);
119+
120+
// Spotlight - soft edges
121+
float theta = dot(lightDir, normalize(-light.direction));
122+
float epsilon = (light.cutOff - light.outerCutOff);
123+
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
124+
diffuse *= intensity;
125+
specular *= intensity;
126+
127+
// TODO: Why it's not work??
128+
// Attenuation
129+
// float distance = length(light.position - FragPos);
130+
// float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
131+
// ambient *= attenuation;
132+
// diffuse *= attenuation;
133+
// specular *= attenuation;
134+
135+
return ambient + diffuse + specular;
104136
}

resources/shaders/material.frag

+38-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ struct Light {
99
float linear;
1010
float quadratic;
1111

12+
// Only for spot lighting
13+
float cutOff;
14+
float outerCutOff;
15+
1216
// TODO: Investigate to change on boolean
1317
// Type of lighting
1418
int isDirection;
@@ -41,9 +45,9 @@ uniform vec3 viewPosition;
4145
out vec4 color;
4246

4347

44-
vec3 calculateDirectionLight(Light light, Material material, vec3 normal, vec3 viewDir);
45-
vec3 calculatePointLight(Light light, Material material, vec3 normal, vec3 fragPos, vec3 viewDir);
46-
vec3 calculateSpotLight();
48+
vec3 calculateDirectionLight(Light, Material, vec3, vec3);
49+
vec3 calculatePointLight(Light, Material, vec3, vec3, vec3);
50+
vec3 calculateSpotLight(Light, Material, vec3, vec3, vec3);
4751

4852

4953
void main() {
@@ -58,7 +62,7 @@ void main() {
5862
result = calculatePointLight(light, material, norm, FragPos, viewDir);
5963
}
6064
else if (light.isSpot == 1) {
61-
result = calculateSpotLight();
65+
result = calculateSpotLight(light, material, norm, FragPos, viewDir);
6266
}
6367

6468
color = vec4((result * material.color) * light.intensity, 1.0f);
@@ -109,6 +113,34 @@ vec3 calculatePointLight(Light light, Material material, vec3 normal, vec3 fragP
109113
}
110114

111115

112-
vec3 calculateSpotLight() {
113-
return vec3(0.0f);
116+
vec3 calculateSpotLight(Light light, Material material, vec3 normal, vec3 fragPos, vec3 viewDir) {
117+
// Ambient
118+
vec3 ambient = light.ambient * material.ambient;
119+
120+
// Diffuse
121+
vec3 lightDir = normalize(light.position - fragPos);
122+
float diff = max(dot(normal, lightDir), 0.0);
123+
vec3 diffuse = light.diffuse * (diff * material.diffuse);
124+
125+
// Specular
126+
vec3 reflectDir = reflect(-lightDir, normal);
127+
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
128+
vec3 specular = light.specular * (spec * material.specular);
129+
130+
// Spotlight - soft edges
131+
float theta = dot(lightDir, normalize(-light.direction));
132+
float epsilon = (light.cutOff - light.outerCutOff);
133+
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
134+
diffuse *= intensity;
135+
specular *= intensity;
136+
137+
// TODO: Why it's not work??
138+
// Attenuation
139+
// float distance = length(light.position - FragPos);
140+
// float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
141+
// ambient *= attenuation;
142+
// diffuse *= attenuation;
143+
// specular *= attenuation;
144+
145+
return ambient + diffuse + specular;
114146
}

src/core/render/api.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -159,33 +159,44 @@ void Render::RenderObject(Object *object, glm::mat4 projection, glm::mat4 view,
159159
shader->UniformFloat("light.quadratic", lighting->light->quadratic);
160160
shader->UniformInt("light.isPoint", 1);
161161
}
162+
else if (lighting->light->lightType == Graphics::Lighting::Type::SPOT) {
163+
shader->UniformInt("light.isSpot", 1);
164+
shader->UniformPosition(
165+
"light.direction",
166+
lighting->light->direction.x,
167+
lighting->light->direction.y,
168+
lighting->light->direction.z
169+
);
170+
shader->UniformFloat("light.cutOff", glm::cos(glm::radians(lighting->light->cutOff)));
171+
shader->UniformFloat("light.outerCutOff", glm::cos(glm::radians(lighting->light->outerCutOff)));
172+
}
162173

163174
if (object->HasComponent(Ecs::Component::Type::MATERIAL)) {
164-
materialShader->UniformPosition(
175+
shader->UniformPosition(
165176
"material.ambient",
166177
object->material->ambient.x,
167178
object->material->ambient.y,
168179
object->material->ambient.z
169180
);
170-
materialShader->UniformPosition(
181+
shader->UniformPosition(
171182
"material.diffuse",
172183
object->material->diffuse.x,
173184
object->material->diffuse.y,
174185
object->material->diffuse.z
175186
);
176-
materialShader->UniformPosition(
187+
shader->UniformPosition(
177188
"material.specular",
178189
object->material->specular.x,
179190
object->material->specular.y,
180191
object->material->specular.z
181192
);
182-
materialShader->UniformPosition(
193+
shader->UniformPosition(
183194
"material.color",
184195
object->material->color.x,
185196
object->material->color.y,
186197
object->material->color.z
187198
);
188-
materialShader->UniformFloat("material.shininess", object->material->shininess);
199+
shader->UniformFloat("material.shininess", object->material->shininess);
189200
}
190201

191202
object->mesh->VAO->bind();

src/editor.cpp

+11-8
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ namespace Engine {
6363
ImGui::Separator();
6464

6565
if (ImGui::BeginMenu("mesh")) {
66-
// if (ImGui::MenuItem("Plane")) {
67-
// std::shared_ptr<Engine::Geometry::Plane> plane = std::make_shared<Engine::Geometry::Plane>();
68-
// app->scene->root->entities.push_back(plane);
69-
// }
70-
// if (ImGui::MenuItem("Cube")) {
71-
// std::shared_ptr<Engine::Geometry::Cube> cube = std::make_shared<Engine::Geometry::Cube>();
72-
// app->scene->root->entities.push_back(cube);
73-
// }
66+
if (ImGui::MenuItem("Plane")) {
67+
std::shared_ptr<Engine::Geometry::Plane> plane = std::make_shared<Engine::Geometry::Plane>();
68+
app->scene->root->entities.push_back(plane);
69+
}
70+
if (ImGui::MenuItem("Cube")) {
71+
std::shared_ptr<Engine::Geometry::Cube> cube = std::make_shared<Engine::Geometry::Cube>();
72+
app->scene->root->entities.push_back(cube);
73+
}
7474
ImGui::EndMenu();
7575
}
7676
ImGui::EndPopup();
@@ -167,6 +167,9 @@ namespace Engine {
167167
ImGui::SliderFloat("quadratic", &selectedEntity->light->quadratic, 0, 1.0);
168168
break;
169169
case Engine::Graphics::Lighting::Type::SPOT:
170+
ImGui::SliderFloat("cutOff", &selectedEntity->light->cutOff, 0, 90.0);
171+
ImGui::SliderFloat("outerCutOff", &selectedEntity->light->outerCutOff, 0, 90.0);
172+
ImGui::SliderFloat3("direction", &selectedEntity->light->direction[0], -10.0, 10.0);
170173
break;
171174
default:
172175
// TODO: Throw exception about unknown lighting type

src/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ class UserApplication : public Engine::EngineApplication {
205205
// End geometry primitives
206206

207207
// Add sun to the scene
208-
std::shared_ptr<Engine::Graphics::Lighting::PointLight> sun =
209-
std::make_shared<Engine::Graphics::Lighting::PointLight>();
208+
std::shared_ptr<Engine::Graphics::Lighting::SpotLight> sun =
209+
std::make_shared<Engine::Graphics::Lighting::SpotLight>();
210210

211211
sun->name = "Sun";
212212
sun->light->intensity = 2.0f;

0 commit comments

Comments
 (0)