Solution

Files changed (2) hide show
  1. lab6-shadowmaps/lab6_main.cpp +8 -0
  2. lab6-shadowmaps/shading.frag +17 -0
lab6-shadowmaps/lab6_main.cpp CHANGED
@@ -281,6 +281,14 @@ void drawScene(GLuint currentShaderProgram,
281
281
  labhelper::setUniformSlow(currentShaderProgram, "spotOuterAngle", std::cos(radians(outerSpotlightAngle)));
282
282
 
283
283
 
284
+ glActiveTexture(GL_TEXTURE10);
285
+ glBindTexture(GL_TEXTURE_2D, shadowMapFB.depthBuffer);
286
+
287
+ // Do scaling and translation to (0,0,0) -> (1,1,1) using lightMatrix
288
+ mat4 lightMatrix = translate(vec3(0.5f)) * scale(vec3(0.5f)) * lightProjectionMatrix * lightViewMatrix
289
+ * inverse(viewMatrix);
290
+
291
+ labhelper::setUniformSlow(currentShaderProgram, "lightMatrix", lightMatrix);
284
292
 
285
293
  // Environment
286
294
  labhelper::setUniformSlow(currentShaderProgram, "environment_multiplier", environment_multiplier);
lab6-shadowmaps/shading.frag CHANGED
@@ -55,6 +55,19 @@ uniform vec3 viewSpaceLightPosition;
55
55
  layout(location = 0) out vec4 fragmentColor;
56
56
 
57
57
 
58
+ uniform mat4 lightMatrix;
59
+
60
+ #if SOLUTION_USE_BUILTIN_SHADOW_TEST
61
+ layout(binding = 10) uniform sampler2DShadow shadowMapTex;
62
+ #else // USE_BUILTIN_SHADOW_TEST
63
+ layout(binding = 10) uniform sampler2D shadowMapTex;
64
+ #endif // ~ USE_BUILTIN_SHADOW_TEST
65
+
66
+ uniform int useSpotLight;
67
+ uniform int useSoftFalloff;
68
+ uniform vec3 viewSpaceLightDir;
69
+ uniform float spotInnerAngle;
70
+ uniform float spotOuterAngle;
58
71
 
59
72
 
60
73
  vec3 calculateDirectIllumiunation(vec3 wo, vec3 n, vec3 base_color)
@@ -122,8 +135,12 @@ void main()
122
135
  float visibility = 1.0;
123
136
  float attenuation = 1.0;
124
137
 
138
+ vec4 shadowMapCoord = lightMatrix * vec4(viewSpacePosition, 1.0);
125
139
  #if SOLUTION_USE_BUILTIN_SHADOW_TEST // SOLUTION_CODE >= 8
126
140
  visibility = textureProj(shadowMapTex, shadowMapCoord);
141
+ #else
142
+ float depth = texture(shadowMapTex, shadowMapCoord.xy / shadowMapCoord.w).r;
143
+ visibility = (depth >= (shadowMapCoord.z / shadowMapCoord.w)) ? 1.0 : 0.0;
127
144
  #endif // ~ USE_BUILTIN_SHADOW_TEST
128
145
 
129
146
  vec3 wo = -normalize(viewSpacePosition);