Skip to content

Commit 6b7b287

Browse files
committed
Added multi lighting and materials
1 parent 65f70bd commit 6b7b287

32 files changed

+486
-221
lines changed

data/shaders/phongFrag.glsl

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,101 @@ in vec3 Color;
77
in vec2 TexCoord;
88
in vec3 Normal;
99

10-
uniform mat4 modelMatrix;
1110
uniform mat4 viewMatrix;
1211

1312
uniform vec3 viewPos;
1413
uniform sampler2D sampler;
1514
uniform vec3 uniformColor;
1615
uniform bool useTexture;
1716

18-
void main() {
19-
vec3 l_dir_world = vec3(5, 5, 5);
20-
vec3 l_dir = normalize((viewMatrix * modelMatrix * vec4(l_dir_world, 0.0)).xyz);
17+
struct Material {
18+
vec3 diffuse;
19+
vec3 specular;
20+
vec3 ambient;
21+
float shininess;
22+
};
23+
24+
uniform Material material;
25+
26+
struct PointLight {
27+
vec3 position;
28+
29+
float radius;
30+
31+
float intensity;
32+
vec3 color;
33+
};
34+
35+
struct SunLight {
36+
vec3 direction;
37+
38+
float intensity;
39+
vec3 color;
40+
};
41+
42+
#define MAX_POINT_LIGHTS 25
43+
#define MAX_SUN_LIGHTS 10
44+
45+
uniform int pointLightsCount;
46+
uniform PointLight pointLights[MAX_POINT_LIGHTS];
47+
48+
uniform int sunLightsCount;
49+
uniform SunLight sunLights[MAX_SUN_LIGHTS];
2150

22-
vec3 diffuse = vec3(0.6, 0.3, 0.3);
23-
vec3 ambient = diffuse * 0.3f;
24-
vec3 specular = vec3(0.7, 0.7, 0.7);
25-
float shininess = 4.0f;
51+
vec3 calculateSunLight(SunLight light, Material material, vec3 fragPos, vec3 normal) {
52+
vec3 lightDir = (viewMatrix * vec4(light.direction, 0.0)).xyz;
53+
vec3 fragToLight = -lightDir;
54+
vec3 L = normalize(fragToLight);
55+
vec3 N = normalize(normal);
56+
vec3 R = reflect(-L, N);
57+
vec3 V = normalize(-fragPos);
58+
59+
vec3 ambientLight = light.color * 0.3f * material.ambient;
60+
vec3 diffuseLight = light.color * material.diffuse * max(0.0, dot(N, L));
61+
vec3 specularLight = light.color * material.specular * pow(max(0.0, dot(R, V)), material.shininess);
2662

27-
vec3 L = l_dir;
28-
vec3 N = normalize(Normal);
63+
return light.intensity * (ambientLight + diffuseLight + specularLight);
64+
}
65+
66+
vec3 calculatePointLight(PointLight light, Material material, vec3 fragPos, vec3 normal) {
67+
vec3 lightPos = (viewMatrix * vec4(light.position, 1.0)).xyz;
68+
vec3 fragToLight = lightPos - fragPos;
69+
float lightDist = length(fragToLight);
70+
float lightRadius = light.radius;
71+
vec3 L = normalize(fragToLight);
72+
vec3 N = normalize(normal);
2973
vec3 R = reflect(-L, N);
30-
vec3 V = normalize(-FragPos);
74+
vec3 V = normalize(-fragPos);
75+
76+
float constant = 1.0;
77+
float linear = 0.09f;
78+
float quadratic = 0.032f;
3179

32-
vec3 ambientLight = ambient;
33-
vec3 diffuseLight = diffuse * max(0.0, dot(N, L));
34-
vec3 specularLight = specular * pow(max(0.0, dot(R, V)), shininess);
80+
vec3 ambientLight = light.color * 0.3f * material.ambient;
81+
vec3 diffuseLight = light.color * material.diffuse * max(0.0, dot(N, L));
82+
vec3 specularLight = light.color * material.specular * pow(max(0.0, dot(R, V)), material.shininess);
83+
84+
//float attenuation = clamp(1.0 / (1.0 + 0.0 * lightDist + 1.0 * lightDist * lightDist), 0.0, 1.0);
85+
//float attenuation = clamp(1.0 - lightDist*lightDist/(lightRadius*lightRadius), 0.0, 1.0);
86+
float attenuation = 1.0 / (constant + linear * lightDist + quadratic * (lightDist * lightDist));
87+
88+
return light.intensity * attenuation * (ambientLight + diffuseLight + specularLight);
89+
}
90+
91+
void main() {
92+
vec3 l_dir_world = vec3(1, 0, 1);
93+
vec3 l_dir = normalize((viewMatrix * vec4(l_dir_world, 0.0)).xyz);
3594

36-
vec3 outColor = ambientLight + diffuseLight + specularLight;
95+
vec3 l_pos_world = vec3(3, 0, 0);
96+
vec3 l_pos = (viewMatrix * vec4(l_pos_world, 1.0)).xyz;
97+
98+
vec3 outColor = vec3(0);
99+
for (int i = 0; i < sunLightsCount; i++) {
100+
outColor += calculateSunLight(sunLights[i], material, FragPos, Normal);
101+
}
102+
for (int i = 0; i < pointLightsCount; i++) {
103+
outColor += calculatePointLight(pointLights[i], material, FragPos, Normal);
104+
}
37105

38106
FragColor = vec4(outColor, 1.0);
39107
}

floating_islands.vcxproj

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,27 +145,40 @@
145145
</Link>
146146
</ItemDefinitionGroup>
147147
<ItemGroup>
148-
<ClCompile Include="src\Camera.cpp" />
149-
<ClCompile Include="src\entity\Cube.cpp" />
148+
<ClCompile Include="src\entity\lights\PointLight.cpp" />
149+
<ClCompile Include="src\entity\general\Material.cpp" />
150+
<ClCompile Include="src\entity\lights\SunLight.cpp" />
151+
<ClCompile Include="src\Lighting.cpp" />
152+
<ClCompile Include="src\entity\Camera.cpp" />
153+
<ClCompile Include="src\entity\general\Entity.cpp" />
154+
<ClCompile Include="src\entity\primitives\Cube.cpp" />
155+
<ClCompile Include="src\Game.cpp" />
150156
<ClCompile Include="src\parser\ObjParser.cpp" />
151157
<ClCompile Include="src\shader\PhongShader.cpp" />
152158
<ClCompile Include="src\shader\Shader.cpp" />
153-
<ClCompile Include="src\entity\Quad.cpp" />
154-
<ClCompile Include="src\entity\general\MeshGeometry.cpp" />
159+
<ClCompile Include="src\entity\primitives\Quad.cpp" />
160+
<ClCompile Include="src\entity\general\Mesh.cpp" />
155161
<ClCompile Include="src\entity\general\GameObject.cpp" />
156162
<ClCompile Include="src\main.cpp" />
157163
</ItemGroup>
158164
<ItemGroup>
165+
<ClInclude Include="src\entity\lights\PointLight.h" />
166+
<ClInclude Include="src\entity\general\Material.h" />
167+
<ClInclude Include="src\entity\lights\SunLight.h" />
168+
<ClInclude Include="src\Lighting.h" />
169+
<ClInclude Include="src\entity\lights\Light.h" />
170+
<ClInclude Include="src\entity\general\Entity.h" />
171+
<ClInclude Include="src\Game.h" />
159172
<ClInclude Include="src\parser\ObjParser.h" />
160-
<ClInclude Include="src\Camera.h" />
161-
<ClInclude Include="src\entity\Cube.h" />
173+
<ClInclude Include="src\entity\Camera.h" />
174+
<ClInclude Include="src\entity\primitives\Cube.h" />
162175
<ClInclude Include="src\parser\ObjVertex.h" />
163176
<ClInclude Include="src\shader\PhongShader.h" />
164177
<ClInclude Include="src\shader\Shader.h" />
165-
<ClInclude Include="src\entity\Quad.h" />
166-
<ClInclude Include="src\entity\general\MeshGeometry.h" />
178+
<ClInclude Include="src\entity\primitives\Quad.h" />
179+
<ClInclude Include="src\entity\general\Mesh.h" />
167180
<ClInclude Include="src\entity\general\GameObject.h" />
168-
<ClInclude Include="object.h" />
181+
<ClInclude Include="src\entity\general\object.h" />
169182
<ClInclude Include="src\Utils.h" />
170183
</ItemGroup>
171184
<ItemGroup>

floating_islands.vcxproj.filters

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,39 @@
33
<ItemGroup>
44
<ClCompile Include="src\main.cpp" />
55
<ClCompile Include="src\entity\general\GameObject.cpp" />
6-
<ClCompile Include="src\entity\general\MeshGeometry.cpp" />
7-
<ClCompile Include="src\entity\Quad.cpp" />
6+
<ClCompile Include="src\entity\general\Mesh.cpp" />
7+
<ClCompile Include="src\entity\primitives\Quad.cpp" />
88
<ClCompile Include="src\shader\PhongShader.cpp" />
99
<ClCompile Include="src\shader\Shader.cpp" />
10-
<ClCompile Include="src\entity\Cube.cpp" />
11-
<ClCompile Include="src\Camera.cpp" />
10+
<ClCompile Include="src\entity\primitives\Cube.cpp" />
11+
<ClCompile Include="src\entity\Camera.cpp" />
1212
<ClCompile Include="src\parser\ObjParser.cpp" />
13+
<ClCompile Include="src\Game.cpp" />
14+
<ClCompile Include="src\entity\general\Entity.cpp" />
15+
<ClCompile Include="src\Lighting.cpp" />
16+
<ClCompile Include="src\entity\general\Material.cpp" />
17+
<ClCompile Include="src\entity\lights\PointLight.cpp" />
18+
<ClCompile Include="src\entity\lights\SunLight.cpp" />
1319
</ItemGroup>
1420
<ItemGroup>
15-
<ClInclude Include="object.h" />
21+
<ClInclude Include="src\entity\general\object.h" />
1622
<ClInclude Include="src\entity\general\GameObject.h" />
17-
<ClInclude Include="src\entity\general\MeshGeometry.h" />
18-
<ClInclude Include="src\entity\Quad.h" />
23+
<ClInclude Include="src\entity\general\Mesh.h" />
24+
<ClInclude Include="src\entity\primitives\Quad.h" />
1925
<ClInclude Include="src\shader\PhongShader.h" />
2026
<ClInclude Include="src\shader\Shader.h" />
21-
<ClInclude Include="src\entity\Cube.h" />
22-
<ClInclude Include="src\Camera.h" />
27+
<ClInclude Include="src\entity\primitives\Cube.h" />
28+
<ClInclude Include="src\entity\Camera.h" />
2329
<ClInclude Include="src\Utils.h" />
2430
<ClInclude Include="src\parser\ObjParser.h" />
2531
<ClInclude Include="src\parser\ObjVertex.h" />
32+
<ClInclude Include="src\Game.h" />
33+
<ClInclude Include="src\entity\general\Entity.h" />
34+
<ClInclude Include="src\entity\lights\Light.h" />
35+
<ClInclude Include="src\Lighting.h" />
36+
<ClInclude Include="src\entity\general\Material.h" />
37+
<ClInclude Include="src\entity\lights\PointLight.h" />
38+
<ClInclude Include="src\entity\lights\SunLight.h" />
2639
</ItemGroup>
2740
<ItemGroup>
2841
<Text Include="data\shaders\basicFrag.glsl" />

src/Game.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "Game.h"
2+
3+
float Game::time = 0.0f;

src/Game.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
class Game {
4+
public:
5+
static float time;
6+
};

src/Lighting.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "Lighting.h"
2+
3+
#include "entity/lights/PointLight.h"
4+
#include "entity/lights/SunLight.h"
5+
6+
void Lighting::setUniforms(const PhongShader& shader) const {
7+
int pointLightCount = 0;
8+
int sunLightCount = 0;
9+
for (auto& light : lights) {
10+
if (typeid(*light) == typeid(PointLight)) {
11+
light->setUniforms(shader, pointLightCount);
12+
pointLightCount++;
13+
} else
14+
if (typeid(*light) == typeid(SunLight)) {
15+
light->setUniforms(shader, sunLightCount);
16+
sunLightCount++;
17+
}
18+
}
19+
glUniform1i(glGetUniformLocation(shader.id, "pointLightsCount"), pointLightCount);
20+
glUniform1i(glGetUniformLocation(shader.id, "sunLightsCount"), sunLightCount);
21+
}
22+
23+
void Lighting::addLight(Light* light) {
24+
const auto newLight = std::shared_ptr<Light>(light);
25+
lights.push_back(newLight);
26+
}
27+
28+
void Lighting::addLight(const std::shared_ptr<Light>& light) {
29+
lights.push_back(light);
30+
}
31+
32+
void Lighting::removeLight(std::shared_ptr<Light>& light) {
33+
auto it = std::find(lights.begin(), lights.end(), light);
34+
if (it != lights.end()) {
35+
lights.erase(it);
36+
}
37+
}
38+
39+
std::vector<std::shared_ptr<Light>> Lighting::getLights() const {
40+
return lights;
41+
}

src/Lighting.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <vector>
5+
6+
#include "entity/lights/Light.h"
7+
#include "shader/PhongShader.h"
8+
9+
class Lighting {
10+
private:
11+
std::vector<std::shared_ptr<Light>> lights;
12+
13+
public:
14+
void setUniforms(const PhongShader& shader) const;
15+
void addLight(Light* light);
16+
void addLight(const std::shared_ptr<Light>& light);
17+
void removeLight(std::shared_ptr<Light>& light);
18+
std::vector<std::shared_ptr<Light>> getLights() const;
19+
};

src/Camera.cpp renamed to src/entity/Camera.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "Camera.h"
22

3-
#include "Utils.h"
3+
#include "../Utils.h"
44

55
Camera::Camera(int width, int height, glm::vec3 pivot) {
66
setPivot(pivot);

src/Camera.h renamed to src/entity/Camera.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include "pgr.h";
4-
#include "shader/PhongShader.h"
4+
#include "../shader/PhongShader.h"
55

66
class Camera {
77
public:

src/entity/Cube.cpp

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)