uniform mat4 projection;\r
uniform mat4 lightProjection;\r
\r
-vec4 blur(sampler2D tex, vec2 uv, vec2 res) {\r
- float Pi = 6.28318530718; // Pi*2\r
- \r
- // GAUSSIAN BLUR SETTINGS {{{\r
- float Directions = 16.0; // BLUR DIRECTIONS (Default 16.0 - More is better but slower)\r
- float Quality = 4.0; // BLUR QUALITY (Default 4.0 - More is better but slower)\r
- float Size = 8.0; // BLUR SIZE (Radius)\r
- // GAUSSIAN BLUR SETTINGS }}}\r
- \r
- vec2 Radius = Size/res;\r
- \r
- // Pixel colour\r
- vec4 Color = texture(tex, uv);\r
- \r
- // Blur calculations\r
- for( float d=0.0; d<Pi; d+=Pi/Directions) {\r
- for(float i=1.0/Quality; i<=1.0; i+=1.0/Quality) {\r
- Color += texture( tex, uv+vec2(cos(d),sin(d))*Radius*i); \r
- }\r
- }\r
- \r
- // Output to screen\r
- Color /= Quality * Directions - 15.0;\r
- return Color;\r
-}\r
-\r
void main()\r
{\r
gl_Position = projection * view * model * vec4(pos, 1.0);\r
+ // calculate fragment position in world coordinates\r
FragPos = vec3(model * vec4(pos, 1));\r
+ // and local coordinates\r
LocalPos = pos;\r
+\r
Normal = normal;\r
\r
+ // get fragment position in the light's projection space\r
vec4 lightSpace = lightProjection * lightView * model * vec4(pos, 1.0);\r
+ // and transform them to 2D coordinates\r
+ // (this is usually done by OpenGL after applying the vertex shader,\r
+ // so to get them here, we have to divide by w manually)\r
lightSpace = lightSpace / lightSpace.w;\r
vec2 shadowmapCoords = lightSpace.xy;\r
+ // map coordinates from [0 1] to [-1 +1]\r
+ // multiply by 0.99 first to shift coordinates towards the center slightly\r
+ // to prevent artifacts at the edges\r
shadowmapCoords = vec2(\r
(shadowmapCoords.x * 0.99 + 1) / 2,\r
(shadowmapCoords.y * 0.99 + 1) / 2\r
);\r
\r
-\r
- // blur\r
+ // sample shadowmap (brightness encodes distance of fragment to light)\r
vec4 t = texture(shadowmapTexture, shadowmapCoords);\r
- //vec4 t = blur(shadowmapTexture, shadowmapCoords, vec2(screenWidth, screenHeight));\r
\r
- BacksideIrradiance = t.r; //*100 + t.g + t.b/100;\r
+ BacksideIrradiance = t.r;\r
\r
+ // calculate backside with distance(BacksideIrradiance) and lightDir\r
vec3 lightDir = normalize(FragPos - lightPos);\r
Backside = (lightPos + (lightDir * BacksideIrradiance));\r
- //Backside = texture(shadowmapTexture, shadowmapCoords).xyz*10;\r
}\r