]> gitweb.ps.run Git - subsurface_scattering/blob - shaders/vert_irradiance.glsl
SSSSS comments
[subsurface_scattering] / shaders / vert_irradiance.glsl
1 #version 330 core\r
2 \r
3 layout (location = 0) in vec3 pos;\r
4 layout (location = 1) in vec3 normal;\r
5 \r
6 out vec3 FragPos;\r
7 out vec3 LocalPos;\r
8 out vec3 Backside;\r
9 out float BacksideIrradiance;\r
10 out vec3 Normal;\r
11 \r
12 uniform sampler2D shadowmapTexture;\r
13 uniform vec3 lightPos;\r
14 uniform vec2 samplePositions[13];\r
15 uniform vec3 sampleWeights[13];\r
16 uniform int screenWidth;\r
17 uniform int screenHeight;\r
18 \r
19 uniform mat4 model;\r
20 uniform mat4 view;\r
21 uniform mat4 lightView;\r
22 uniform mat4 lightViewInv;\r
23 uniform mat4 projection;\r
24 uniform mat4 lightProjection;\r
25 \r
26 void main()\r
27 {\r
28   gl_Position = projection * view * model * vec4(pos, 1.0);\r
29   // calculate fragment position in world coordinates\r
30   FragPos = vec3(model * vec4(pos, 1));\r
31   // and local coordinates\r
32   LocalPos = pos;\r
33 \r
34   Normal = normal;\r
35   \r
36   // get fragment position in the light's projection space\r
37   vec4 lightSpace = lightProjection * lightView * model * vec4(pos, 1.0);\r
38   // and transform them to 2D coordinates\r
39   // (this is usually done by OpenGL after applying the vertex shader,\r
40   // so to get them here, we have to divide by w manually)\r
41   lightSpace = lightSpace / lightSpace.w;\r
42   vec2 shadowmapCoords = lightSpace.xy;\r
43   // map coordinates from [0 1] to [-1 +1]\r
44   // multiply by 0.99 first to shift coordinates towards the center slightly\r
45   // to prevent artifacts at the edges\r
46   shadowmapCoords = vec2(\r
47     (shadowmapCoords.x * 0.99 + 1) / 2,\r
48     (shadowmapCoords.y * 0.99 + 1) / 2\r
49   );\r
50 \r
51   // sample shadowmap (brightness encodes distance of fragment to light)\r
52   vec4 t = texture(shadowmapTexture, shadowmapCoords);\r
53 \r
54   BacksideIrradiance = t.r;\r
55   \r
56   // calculate backside with distance(BacksideIrradiance) and lightDir\r
57   vec3 lightDir = normalize(FragPos - lightPos);\r
58   Backside = (lightPos + (lightDir * BacksideIrradiance));\r
59 }\r