Skip to content

Commit fe68896

Browse files
committed
why do i have an error every chapter?
1 parent ce71710 commit fe68896

File tree

11 files changed

+166
-84
lines changed

11 files changed

+166
-84
lines changed

lib/README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Fusce eu mauris consectetur, molestie ante in, suscipit libero. Suspendisse sed
9797
tincidunt ante odio ut quam. Mauris laoreet tempor lacus ac feugiat. Etiam nibh elit, pulvinar et est non, vestibulum porta lorem. Curabitur consectetur eu quam vitae commodo.
9898
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam non pellentesque arcu, vitae rhoncus massa. Cras faucibus ex nibh, non mattis augue feugiat in. In feugiat cursus
9999
orci, id condimentum massa elementum eget. Ut porttitor leo sed tellus maximus, ac dapibus ipsum rutrum. Donec diam odio, iaculis id mauris ac, vehicula blandit purus. Vivamus
100-
aliquet semper odio, sit amet dictum erat vehicula euismod.
100+
aliquet semper odio, sit amet dictum erat vehicula euismod. LOL.
101101
Aenean ac mauris non tortor mattis finibus. Maecenas vitae consequat orci, sed bibendum odio. Suspendisse at justo sit amet ipsum mattis eleifend nec et massa. Duis quis sem
102102
sed ipsum maximus sagittis. Ut id porttitor risus. Proin vel massa eget erat euismod finibus malesuada eget arcu. Donec at dolor accumsan justo malesuada condimentum id vel
103103
nisi. Proin mauris lectus, maximus a risus sed, dictum blandit enim. Vivamus a volutpat dolor. Vestibulum nec ligula dolor. Suspendisse quis enim vel eros aliquet ullamcorper.

shaders/depth.glsl

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,37 @@
33
layout(location = 0) in vec4 a_position;
44

55
uniform mat4 u_modelMat;
6-
uniform mat4 u_viewMat;
7-
uniform mat4 u_projectionMat;
86

97
void main() {
10-
gl_Position = u_projectionMat * u_viewMat * u_modelMat * a_position;
8+
gl_Position = u_modelMat * a_position;
9+
}
10+
#shader geometry
11+
#version 330
12+
layout (triangles) in;
13+
layout (triangle_strip, max_vertices = 18) out;
14+
15+
uniform mat4 u_shadowMatrices[6];
16+
out vec4 fragPos;
17+
18+
void main() {
19+
for(int face = 0; face < 6; ++face) {
20+
gl_Layer = face;
21+
fragPos = gl_in[face].gl_Position;
22+
gl_Position = u_shadowMatrices[face] * fragPos;
23+
EmitVertex();
24+
}
25+
EndPrimitive();
1126
}
1227

1328
#shader fragment
1429
#version 330
15-
void main() {} // do nothing
30+
in vec4 fragPos;
31+
32+
struct PointLight {
33+
vec3 position;
34+
};
35+
uniform PointLight u_light;
36+
37+
void main() {
38+
gl_FragDepth = length(fragPos.xyz - u_light.position) / 100;
39+
}

shaders/lighting.glsl

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void main() {
2020
gl_Position = u_projectionMat * u_viewMat * u_modelMat * a_position;
2121
vs_out.v_texCoord = a_texCoord;
2222
vs_out.v_fragPosition = u_modelMat * a_position;
23-
vs_out.v_normal = vec3(u_normalMat * a_normal);
23+
vs_out.v_normal = normalize(vec3(u_normalMat * a_normal));
2424
vs_out.v_viewMat = u_viewMat;
2525
}
2626

@@ -92,12 +92,14 @@ uniform int u_spotLightCount;
9292
uniform vec3 u_viewPos;
9393
uniform bool u_specularSet;
9494

95-
uniform sampler2D u_depthMap; // TODO: move in the light struct, manage by renderer
95+
uniform samplerCube u_depthMap; // TODO: move in the light struct, manage by renderer
9696

9797
vec4 light(PointLight light, Material material, vec3 norm, vec3 viewDir);
9898
vec4 light(DirectionalLight light, Material material, vec3 norm, vec3 viewDir);
9999
vec4 light(SpotLight light, Material material, vec3 norm, vec3 viewDir);
100-
float shadow(vec4 fragPosLightSpace, sampler2D depthMap);
100+
float shadow(PointLight light, samplerCube depthMap);
101+
float shadow(DirectionalLight light, sampler2D depthMap);
102+
float shadow(SpotLight light, sampler2D depthMap);
101103

102104
void main() { // TODO: displacement
103105
vec4 lightColor = vec4(0, 0, 0, 1);
@@ -136,13 +138,12 @@ vec4 light(PointLight light, Material material, vec3 norm, vec3 viewDir) {
136138
attenuation *
137139
pow(max(dot(norm, normalize(lightDir + viewDir)), 0.0), u_material.shininess) *
138140
(u_specularSet ? vec3(texture(material.specular, fs_in.v_texCoord)) : vec3(.25));
139-
float shadow = shadow(light.projectionMat * light.viewMat * fs_in.v_fragPosition, u_depthMap);
141+
float shadow = shadow(light, u_depthMap);
140142

141143
return vec4(ambient + (1 - shadow) * (diffuse + specular), 1.0);
142144
}
143145
vec4 light(DirectionalLight light, Material material, vec3 norm, vec3 viewDir) {
144146
vec3 lightDir = normalize(-light.direction);
145-
vec3 reflectDir = reflect(-lightDir, norm);
146147

147148
vec3 ambient = light.color * 0.125;
148149
vec3 diffuse =
@@ -152,14 +153,14 @@ vec4 light(DirectionalLight light, Material material, vec3 norm, vec3 viewDir) {
152153
light.color *
153154
pow(max(dot(norm, normalize(lightDir + viewDir)), 0.0), u_material.shininess) *
154155
(u_specularSet ? vec3(texture(material.specular, fs_in.v_texCoord)) : vec3(.25));
155-
float shadow = shadow(light.projectionMat * light.viewMat * fs_in.v_fragPosition, u_depthMap);
156+
// float shadow = shadow(light, u_depthMap);
157+
float shadow = 0;
156158

157159
return vec4(ambient + (1 - shadow) * (diffuse + specular), 1.0);
158160
}
159161
vec4 light(SpotLight light, Material material, vec3 norm, vec3 viewDir) {
160162

161163
vec3 lightDir = normalize(light.position - vec3(fs_in.v_fragPosition));
162-
vec3 reflectDir = reflect(-lightDir, norm);
163164
float distanceLightFragment = length(light.position - vec3(fs_in.v_fragPosition));
164165
float attenuation = 1.0 / (light.constant + light.linear * distanceLightFragment + light.quadratic * distanceLightFragment * distanceLightFragment);
165166

@@ -182,22 +183,36 @@ vec4 light(SpotLight light, Material material, vec3 norm, vec3 viewDir) {
182183
attenuation *
183184
pow(max(dot(norm, normalize(lightDir + viewDir)), 0.0), u_material.shininess) *
184185
(u_specularSet ? vec3(texture(material.specular, fs_in.v_texCoord)) : vec3(.25));
185-
float shadow = shadow(light.projectionMat * light.viewMat * fs_in.v_fragPosition, u_depthMap);
186+
// float shadow = shadow(light, u_depthMap);
187+
float shadow = 0;
186188

187189
return vec4(ambient + (1 - shadow) * (diffuse + specular), 1.0);
188190
} else {
189191
return vec4(ambient, 1.0); // unsure about that
190192
}
191193
}
192194

193-
float shadow(vec4 fragPosLightSpace, sampler2D depthMap) {
195+
// TODO: pcf
196+
float shadow(PointLight light, samplerCube depthMap) {
197+
vec3 fragToLight = fs_in.v_fragPosition.xyz - light.position;
198+
float closestDepth = texture(depthMap, fragToLight).r * 100; // 100 -- far plane (too lazy to set uniform)
199+
float currentDepth = length(fragToLight);
200+
201+
float bias = max(0.05 * (1.0 - dot(fs_in.v_normal, normalize(fragToLight))), 0.005);
202+
return currentDepth - bias > closestDepth ? 1.0 : 0.0;
203+
}
204+
float shadow(DirectionalLight light, sampler2D depthMap) {
205+
vec4 fragPosLightSpace = light.projectionMat * light.viewMat * fs_in.v_fragPosition;
194206
vec3 projectedCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
195207
projectedCoords = projectedCoords * 0.5 + 0.5;
196208
if(projectedCoords.z > 0.99) return 0;
197209

198210
float closestDepth = texture(depthMap, projectedCoords.xy).r;
199211
float currentDepth = projectedCoords.z;
200212

201-
float bias = 0.00001;
213+
float bias = max(0.05 * (1.0 - dot(fs_in.v_normal, light.direction)), 0.005);
202214
return currentDepth - bias > closestDepth ? 1.0 : 0.0;
215+
}
216+
float shadow(SpotLight light, sampler2D depthMap) {
217+
return 0;
203218
}

shaders/post_process.glsl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@ uniform sampler2D u_texture;
1717

1818
out vec4 o_color;
1919

20+
float LinearizeDepth(float depth)
21+
{
22+
float near_plane = 0.1;
23+
float far_plane = 100.0;
24+
float z = depth * 2.0 - 1.0; // Back to NDC
25+
return (2.0 * near_plane * far_plane) / (far_plane + near_plane - z * (far_plane - near_plane));
26+
}
27+
2028
void main()
2129
{
2230
o_color = texture(u_texture, v_texCoords);
2331
// o_color = vec4(vec3(0.2126 * o_color.r + 0.7152 * o_color.g + 0.0722 * o_color.b), 1.0); // grayscale
24-
o_color = vec4(vec3(o_color.r), 1);
32+
o_color = vec4(vec3(LinearizeDepth(o_color.r)), 1);
2533
}

src/Application.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ void APIENTRY DebugCallback(GLenum source, GLenum type, GLuint id, GLenum severi
2525

2626
Application::openglError.id = id;
2727
Application::openglError.msg = msg;
28-
std::string errorName;
2928

3029
switch (source) {
3130
case GL_DEBUG_SOURCE_API:
@@ -174,19 +173,6 @@ Application::~Application()
174173
glfwTerminate();
175174
}
176175

177-
void Application::loadModel(char const *filepath, LoadModelQuery query)
178-
{
179-
std::string newFilepath{filepath};
180-
std::replace_if(newFilepath.begin(), newFilepath.end(), [](char c){ return c == '\\'; }, '/');
181-
try {
182-
models.push_back(Model{newFilepath, query.flipTextures, query.flipWindingOrder});
183-
} catch(std::runtime_error &e) {
184-
LOG_ERROR("%s", e.what());
185-
}
186-
currentModelIndex = models.size() - 1;
187-
}
188-
189-
190176
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
191177
{
192178
ControllableCamera &camera = *static_cast<ControllableCamera *>(glfwGetWindowUserPointer(window));

src/Application.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,4 @@ struct Application {
7373
public:
7474
Application();
7575
~Application();
76-
77-
void loadModel(char const *filepath, LoadModelQuery query);
7876
};

src/imguistuff.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,16 @@ void imguistuff(Application &app, ControllableCamera &cam, PointLight &light, Sp
5555

5656

5757
ImGui::Begin("properties");
58-
ImGui::Text("delta time: %f", app.deltatime);
59-
ImGui::Text("FPS: %f", app.deltatime ? 1 / app.deltatime : -1);
58+
if(app.deltatime >= 1/100) {
59+
ImGui::Text("! delta time: %f", app.deltatime);
60+
ImGui::Text("! FPS: %f", app.deltatime ? 1 / app.deltatime : -1);
61+
} else if(app.deltatime >= 1/30) {
62+
ImGui::Text("!! delta time: %f", app.deltatime);
63+
ImGui::Text("!! FPS: %f", app.deltatime ? 1 / app.deltatime : -1);
64+
} else {
65+
ImGui::Text(" delta time: %f", app.deltatime);
66+
ImGui::Text(" FPS: %f", app.deltatime ? 1 / app.deltatime : -1);
67+
}
6068
ImGui::Separator();
6169

6270

0 commit comments

Comments
 (0)