Skip to content

Commit c5c50b8

Browse files
committed
feat(pointLightSystem): added specular light
1 parent 6f761d6 commit c5c50b8

11 files changed

+11084
-13
lines changed

models/suzanne.obj

+11,008
Large diffs are not rendered by default.

shaders/point_light.frag

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct PointLight {
1111
layout (set = 0, binding = 0) uniform GlobalUbo {
1212
mat4 projection;
1313
mat4 view;
14+
mat4 inverseView;
1415
vec4 ambientLightColor;
1516
PointLight pointLights[10];
1617
int numLights;

shaders/point_light.vert

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct PointLight {
1919
layout (set = 0, binding = 0) uniform GlobalUbo {
2020
mat4 projection;
2121
mat4 view;
22+
mat4 inverseView;
2223
vec4 ambientLightColor;
2324
PointLight pointLights[10];
2425
int numLights;

shaders/simple_shader.frag

+18-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct PointLight {
1414
layout (set = 0, binding = 0) uniform GlobalUbo {
1515
mat4 projection;
1616
mat4 view;
17+
mat4 inverseView;
1718
vec4 ambientLightColor;
1819
PointLight pointLights[10];
1920
int numLights;
@@ -27,18 +28,33 @@ layout(push_constant) uniform Push {
2728
void main() {
2829
vec3 ambientLight = ubo.ambientLightColor.xyz * ubo.ambientLightColor.w;
2930
vec3 diffuseLight = ambientLight;
31+
32+
vec3 specularLight = vec3(0.0);
33+
3034
vec3 surfaceNormal = normalize(fragNormalWorld);
3135

36+
vec3 cameraPosWorld = ubo.inverseView[3].xyz;
37+
vec3 viewDirection = normalize(cameraPosWorld - fragPosWorld);
38+
39+
40+
3241
for(int i = 0; i < ubo.numLights; i++) {
3342
PointLight light = ubo.pointLights[i];
3443
vec3 directionToLight = light.position.xyz - fragPosWorld;
3544
float attenuation = 1.0 / dot(directionToLight, directionToLight);
45+
directionToLight = normalize(directionToLight);
3646

37-
float cosAngleIncidence = max(dot(normalize(fragNormalWorld), normalize(directionToLight)), 0);
47+
float cosAngleIncidence = max(dot(normalize(fragNormalWorld), directionToLight), 0);
3848
vec3 intensity = light.color.xyz * light.color.w * attenuation;
3949

4050
diffuseLight += intensity * cosAngleIncidence;
51+
52+
vec3 halfAngle = normalize(directionToLight + viewDirection);
53+
float blinnTerm = dot(surfaceNormal, halfAngle);
54+
blinnTerm = clamp(blinnTerm, 0, 1);
55+
blinnTerm = pow(blinnTerm, 202.0);
56+
specularLight += intensity * blinnTerm;
4157
}
4258

43-
outColor = vec4(diffuseLight * fragColor, 1.0);
59+
outColor = vec4(diffuseLight * fragColor + specularLight * fragColor, 1.0);
4460
}

shaders/simple_shader.vert

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct PointLight {
1717
layout (set = 0, binding = 0) uniform GlobalUbo {
1818
mat4 projection;
1919
mat4 view;
20+
mat4 inverseView;
2021
vec4 ambientLightColor;
2122
PointLight pointLights[10];
2223
int numLights;

src/dvm_app.cpp

+20-9
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ void DvmApp::run()
137137

138138
ubo.projection = camera.getProjection();
139139
ubo.view = camera.getView();
140+
ubo.inverseView = camera.getInverseView();
140141
pointLightSystem.update(frameInfo, ubo);
141142

142143
uboBuffers[frameIndex]->writeToBuffer(&ubo);
@@ -157,13 +158,12 @@ void DvmApp::loadGameObjects()
157158
{
158159
std::shared_ptr<DvmModel> model = DvmModel::createModelFromFile(
159160
dvmDevice,
160-
"C:\\Users\\israel.medina\\projects\\personal\\dvm-engine\\models\\flat_"
161-
"vase.obj");
161+
"models/flat_vase.obj");
162162

163163
auto vase = DvmGameObject::createGameObject();
164164
vase.model = model;
165165
vase.transform.translation = {
166-
0.f,
166+
-0.5f,
167167
.5f,
168168
.0f,
169169
};
@@ -172,24 +172,35 @@ void DvmApp::loadGameObjects()
172172

173173
std::shared_ptr<DvmModel> smoothVaseModel = DvmModel::createModelFromFile(
174174
dvmDevice,
175-
"C:\\Users\\israel.medina\\projects\\personal\\dvm-"
176-
"engine\\models\\smooth_"
177-
"vase.obj");
175+
"models/smooth_vase.obj");
178176

179177
auto smoothVase = DvmGameObject::createGameObject();
180178
smoothVase.model = smoothVaseModel;
181179
smoothVase.transform.translation = {
182-
1.f,
180+
0.5f,
183181
.5f,
184182
.0f,
185183
};
186184
smoothVase.transform.scale = {1.5f, 1.5f, 1.5f};
187185
gameObjects.emplace(smoothVase.getId(), std::move(smoothVase));
186+
187+
std::shared_ptr<DvmModel> suzanneModel = DvmModel::createModelFromFile(
188+
dvmDevice,
189+
"models/suzanne.obj");
190+
191+
auto suzanne = DvmGameObject::createGameObject();
192+
suzanne.model = suzanneModel;
193+
suzanne.transform.translation = {
194+
0.f,
195+
-1.5f,
196+
.0f,
197+
};
198+
suzanne.transform.scale = {0.5f, 0.5f, 0.5f};
199+
gameObjects.emplace(suzanne.getId(), std::move(suzanne));
188200

189201
std::shared_ptr<DvmModel> floorModel = DvmModel::createModelFromFile(
190202
dvmDevice,
191-
"C:\\Users\\israel.medina\\projects\\personal\\dvm-"
192-
"engine\\models\\quad.obj");
203+
"models/quad.obj");
193204

194205
auto floorObject = DvmGameObject::createGameObject();
195206
floorObject.model = floorModel;

src/dvm_camera.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ void DvmCamera::setViewDirection(glm::vec3 position,
4949
viewMatrix[3][0] = -glm::dot(u, position);
5050
viewMatrix[3][1] = -glm::dot(v, position);
5151
viewMatrix[3][2] = -glm::dot(w, position);
52+
53+
inverseViewMatrix = glm::mat4{1.f};
54+
inverseViewMatrix[0][0] = u.x;
55+
inverseViewMatrix[0][1] = u.y;
56+
inverseViewMatrix[0][2] = u.z;
57+
inverseViewMatrix[1][0] = v.x;
58+
inverseViewMatrix[1][1] = v.y;
59+
inverseViewMatrix[1][2] = v.z;
60+
inverseViewMatrix[2][0] = w.x;
61+
inverseViewMatrix[2][1] = w.y;
62+
inverseViewMatrix[2][2] = w.z;
63+
inverseViewMatrix[3][0] = position.x;
64+
inverseViewMatrix[3][1] = position.y;
65+
inverseViewMatrix[3][2] = position.z;
5266
}
5367

5468
void DvmCamera::setViewTarget(glm::vec3 position,
@@ -84,5 +98,19 @@ void DvmCamera::setViewYXZ(glm::vec3 position, glm::vec3 rotation)
8498
viewMatrix[3][0] = -glm::dot(u, position);
8599
viewMatrix[3][1] = -glm::dot(v, position);
86100
viewMatrix[3][2] = -glm::dot(w, position);
101+
102+
inverseViewMatrix = glm::mat4{1.f};
103+
inverseViewMatrix[0][0] = u.x;
104+
inverseViewMatrix[0][1] = u.y;
105+
inverseViewMatrix[0][2] = u.z;
106+
inverseViewMatrix[1][0] = v.x;
107+
inverseViewMatrix[1][1] = v.y;
108+
inverseViewMatrix[1][2] = v.z;
109+
inverseViewMatrix[2][0] = w.x;
110+
inverseViewMatrix[2][1] = w.y;
111+
inverseViewMatrix[2][2] = w.z;
112+
inverseViewMatrix[3][0] = position.x;
113+
inverseViewMatrix[3][1] = position.y;
114+
inverseViewMatrix[3][2] = position.z;
87115
}
88116
} // namespace dvm

src/dvm_camera.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class DvmCamera
2626

2727
const glm::mat4 getProjection() const { return projectionMatrix; }
2828
const glm::mat4 getView() const { return viewMatrix; }
29+
const glm::mat4 getInverseView() const { return inverseViewMatrix; }
2930

3031
private:
3132
glm::mat4 projectionMatrix {1.f};

src/dvm_frame_info.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct GlobalUbo
1818
{
1919
glm::mat4 projection {1.0f};
2020
glm::mat4 view {1.0f};
21-
21+
glm::mat4 inverseView {1.0f};
2222
glm::vec4 ambientLightColor {1.0f, 1.0f, 1.0f, .02f}; // w is intensity
2323
PointLight pointLights[MAX_LIGHTS];
2424
int numLights;

src/dvm_model.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ std::unique_ptr<DvmModel> DvmModel::createModelFromFile(
112112
DvmDevice& device, const std::string& filepath)
113113
{
114114
Builder builder {};
115-
builder.loadModel(filepath);
115+
builder.loadModel(ENGINE_DIR + filepath);
116116
std::cout << "Vertex count: " << builder.vertices.size() << std::endl;
117117
return std::make_unique<DvmModel>(device, builder);
118118
}

src/dvm_model.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
#include <memory>
99

10+
#ifndef ENGINE_DIR
11+
# define ENGINE_DIR "../"
12+
#endif
13+
1014
namespace dvm
1115
{
1216
class DvmModel

0 commit comments

Comments
 (0)