2
2
#extension GL_GOOGLE_include_directive : require
3
3
#include "fragment_common.glsl"
4
4
5
- layout (location = 0 ) in vec2 TexCoord;
6
- layout (location = 1 ) in vec4 ViewPos;
5
+ layout (location = 0 ) in vec2 TexCoord;
6
+ layout (location = 1 ) in vec4 ViewPos;
7
7
8
- layout (std140, set = 0 , binding = 6 ) readonly buffer DirectionalLightSB
8
+ layout (std140, set = 0 , binding = 6 ) readonly buffer DirectionalLightSB
9
9
{
10
- uint Count;
10
+ uint Count;
11
11
DirectionalLight Data[];
12
- } DirectionalLightBuffer;
12
+ }
13
+ DirectionalLightBuffer;
13
14
14
- layout (std140, set = 0 , binding = 7 ) readonly buffer PointLightSB
15
+ layout (std140, set = 0 , binding = 7 ) readonly buffer PointLightSB
15
16
{
16
- uint Count;
17
+ uint Count;
17
18
PointLight Data[];
18
- } PointLightBuffer;
19
+ }
20
+ PointLightBuffer;
19
21
20
- layout (std140, set = 0 , binding = 8 ) readonly buffer SpotLightSB
22
+ layout (std140, set = 0 , binding = 8 ) readonly buffer SpotLightSB
21
23
{
22
- uint Count;
24
+ uint Count;
23
25
SpotLight Data[];
24
- } SpotLightBuffer;
25
-
26
- layout (set = 0 , binding = 10 ) uniform sampler2D AlbedoSampler;
27
- layout (set = 0 , binding = 11 ) uniform sampler2D PositionSampler;
28
- layout (set = 0 , binding = 12 ) uniform sampler2D NormalSampler;
29
- layout (set = 0 , binding = 13 ) uniform sampler2D SpecularSampler;
30
-
26
+ }
27
+ SpotLightBuffer;
31
28
32
- layout (location = 0 ) out vec4 OutColor;
29
+ layout (set = 0 , binding = 10 ) uniform sampler2D AlbedoSampler;
30
+ layout (set = 0 , binding = 11 ) uniform sampler2D PositionSampler;
31
+ layout (set = 0 , binding = 12 ) uniform sampler2D NormalSampler;
32
+ layout (set = 0 , binding = 13 ) uniform sampler2D SpecularSampler;
33
33
34
+ layout (location = 0 ) out vec4 OutColor;
34
35
35
36
vec3 ComputeDirectionalLight(DirectionalLight light, vec3 normal, vec3 viewDir, vec3 albedoMap, vec3 specularMap)
36
37
{
37
- vec3 direction = light.Direction.xyz;
38
+ vec3 direction = light.Direction.xyz;
38
39
39
- vec3 lightDir = normalize (direction);
40
- vec3 ambient = light.Ambient.xyz * albedoMap;
40
+ vec3 lightDir = normalize (direction);
41
+ vec3 ambient = light.Ambient.xyz * albedoMap;
41
42
42
- float diff = max (dot (normal, lightDir), 0.0 );
43
- vec3 diffuse = diff * light.Diffuse.xyz * albedoMap;
43
+ float diff = max (dot (normal, lightDir), 0.0 );
44
+ vec3 diffuse = diff * light.Diffuse.xyz * albedoMap;
44
45
45
- vec3 reflectDir = reflect (- lightDir, normal);
46
- float spec = pow (max (dot (viewDir, reflectDir), 0.0 ), 16 );
47
- vec3 specular = spec * light.Specular.xyz * specularMap;
46
+ vec3 reflectDir = reflect (- lightDir, normal);
47
+ float spec = pow (max (dot (viewDir, reflectDir), 0.0 ), 16 );
48
+ vec3 specular = spec * light.Specular.xyz * specularMap;
48
49
49
50
return vec3 (ambient + diffuse + specular);
50
51
}
51
52
52
53
vec3 ComputePointLight(PointLight light, vec3 normal, vec3 viewDir, vec4 fragPos, vec3 albedoMap, vec3 specularMap)
53
54
{
54
- float dist = length (light.Position.xyz - fragPos.xyz);
55
- float attenuation = 1.0 / ( light.Constant + (light.Linear * dist) + (light.Quadratic * (dist * dist)) );
55
+ float dist = length (light.Position.xyz - fragPos.xyz);
56
+ float attenuation = 1.0 / (light.Constant + (light.Linear * dist) + (light.Quadratic * (dist * dist)));
56
57
57
- vec3 lightDir = normalize (light.Position.xyz - fragPos.xyz);
58
- vec3 ambient = light.Ambient.xyz * albedoMap;
58
+ vec3 lightDir = normalize (light.Position.xyz - fragPos.xyz);
59
+ vec3 ambient = light.Ambient.xyz * albedoMap;
59
60
60
- float diff = max (dot (normal, lightDir), 0.0 );
61
- vec3 diffuse = diff * light.Diffuse.xyz * albedoMap;
61
+ float diff = max (dot (normal, lightDir), 0.0 );
62
+ vec3 diffuse = diff * light.Diffuse.xyz * albedoMap;
62
63
63
- vec3 reflectDir = reflect (- lightDir, normal);
64
- float spec = pow (max (dot (viewDir, reflectDir), 0.0 ), 16 ); // todo : 16 should be replaced by material.shininess
65
- vec3 specular = spec * light.Specular.xyz * specularMap;
64
+ vec3 reflectDir = reflect (- lightDir, normal);
65
+ float spec = pow (max (dot (viewDir, reflectDir), 0.0 ), 16 ); // todo : 16 should be replaced by material.shininess
66
+ vec3 specular = spec * light.Specular.xyz * specularMap;
66
67
67
- ambient *= attenuation;
68
- diffuse *= attenuation;
69
- specular *= attenuation;
68
+ ambient *= attenuation;
69
+ diffuse *= attenuation;
70
+ specular *= attenuation;
70
71
71
72
return vec3 (ambient + diffuse + specular);
72
73
}
73
74
74
75
vec3 ComputeSpotLight(SpotLight light, vec3 normal, vec3 viewDir, vec3 fragPos, vec3 albedoMap, vec3 specularMap)
75
76
{
76
- vec3 lightDir = normalize (light.Position.xyz - fragPos);
77
- vec3 direction = normalize (light.Direction.xyz);
78
- float theta = dot (lightDir, direction); // check if lighting is inside the spotlight cone
77
+ vec3 lightDir = normalize (light.Position.xyz - fragPos);
78
+ vec3 direction = normalize (light.Direction.xyz);
79
+ float theta = dot (lightDir, direction); // check if lighting is inside the spotlight cone
79
80
80
- if (theta > light.CutOff)
81
+ if (theta > light.CutOff)
81
82
{
82
- vec3 ambient = light.Ambient.xyz * albedoMap;
83
+ vec3 ambient = light.Ambient.xyz * albedoMap;
83
84
84
- float diff = max (dot (normal, lightDir), 0.0 );
85
- vec3 diffuse = diff * light.Diffuse.xyz * albedoMap;
85
+ float diff = max (dot (normal, lightDir), 0.0 );
86
+ vec3 diffuse = diff * light.Diffuse.xyz * albedoMap;
86
87
87
- vec3 reflectDir = reflect (- lightDir, normal);
88
- float spec = pow (max (dot (viewDir, reflectDir), 0.0 ), 16 ); // todo : 16 should be replaced by material.shininess
89
- vec3 specular = spec * light.Specular.xyz * specularMap;
88
+ vec3 reflectDir = reflect (- lightDir, normal);
89
+ float spec = pow (max (dot (viewDir, reflectDir), 0.0 ), 16 ); // todo : 16 should be replaced by material.shininess
90
+ vec3 specular = spec * light.Specular.xyz * specularMap;
90
91
91
- float dist = length (light.Position.xyz - fragPos);
92
- float attenuation = 1.0 / ( light.Constant + (light.Linear * dist) + (light.Quadratic * (dist * dist)) );
92
+ float dist = length (light.Position.xyz - fragPos);
93
+ float attenuation = 1.0 / (light.Constant + (light.Linear * dist) + (light.Quadratic * (dist * dist)));
93
94
94
- diffuse *= attenuation;
95
- specular *= attenuation;
95
+ diffuse *= attenuation;
96
+ specular *= attenuation;
96
97
97
98
return vec3 (ambient + diffuse + specular);
98
99
}
@@ -102,33 +103,32 @@ vec3 ComputeSpotLight(SpotLight light, vec3 normal, vec3 viewDir, vec3 fragPos,
102
103
}
103
104
}
104
105
105
-
106
106
void main()
107
107
{
108
- vec3 norm = texture( NormalSampler, TexCoord ).rgb;
109
- vec4 fragPos = texture( PositionSampler, TexCoord );
110
- vec3 albedo = texture( AlbedoSampler, TexCoord ).rgb;
111
- vec3 specular = texture( SpecularSampler, TexCoord ).rgb;
108
+ vec3 norm = texture(NormalSampler, TexCoord).rgb;
109
+ vec4 fragPos = texture(PositionSampler, TexCoord);
110
+ vec3 albedo = texture(AlbedoSampler, TexCoord).rgb;
111
+ vec3 specular = texture(SpecularSampler, TexCoord).rgb;
112
112
113
- vec3 viewDir = normalize ( ViewPos.xyz - fragPos.xyz);
113
+ vec3 viewDir = normalize (ViewPos.xyz - fragPos.xyz);
114
114
115
115
vec3 lighting = vec3 (0.0 );
116
- for (uint i = 0 ; i < DirectionalLightBuffer.Count; ++ i)
116
+ for (uint i = 0 ; i < DirectionalLightBuffer.Count; ++ i)
117
117
{
118
- DirectionalLight directionalLight = DirectionalLightBuffer.Data[i];
119
- lighting += ComputeDirectionalLight(directionalLight, norm, viewDir, albedo, specular);
118
+ DirectionalLight directionalLight = DirectionalLightBuffer.Data[i];
119
+ lighting += ComputeDirectionalLight(directionalLight, norm, viewDir, albedo, specular);
120
120
}
121
121
122
122
for (uint i = 0 ; i < PointLightBuffer.Count; ++ i)
123
123
{
124
- PointLight pointLight = PointLightBuffer.Data[i];
125
- lighting += ComputePointLight(pointLight, norm, viewDir, fragPos, albedo, specular);
124
+ PointLight pointLight = PointLightBuffer.Data[i];
125
+ lighting += ComputePointLight(pointLight, norm, viewDir, fragPos, albedo, specular);
126
126
}
127
127
128
128
for (uint i = 0 ; i < SpotLightBuffer.Count; ++ i)
129
129
{
130
- SpotLight spotLight = SpotLightBuffer.Data[i];
131
- lighting += ComputeSpotLight(spotLight, norm, viewDir, fragPos.xyz, albedo, specular);
130
+ SpotLight spotLight = SpotLightBuffer.Data[i];
131
+ lighting += ComputeSpotLight(spotLight, norm, viewDir, fragPos.xyz, albedo, specular);
132
132
}
133
133
134
134
OutColor = vec4 (lighting, 1.0 );
0 commit comments