22#extension GL_GOOGLE_include_directive : require
33#include "fragment_common.glsl"
44
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;
77
8- layout (std140, set = 0 , binding = 6 ) readonly buffer DirectionalLightSB
8+ layout (std140, set = 0 , binding = 6 ) readonly buffer DirectionalLightSB
99{
10- uint Count;
10+ uint Count;
1111 DirectionalLight Data[];
12- } DirectionalLightBuffer;
12+ }
13+ DirectionalLightBuffer;
1314
14- layout (std140, set = 0 , binding = 7 ) readonly buffer PointLightSB
15+ layout (std140, set = 0 , binding = 7 ) readonly buffer PointLightSB
1516{
16- uint Count;
17+ uint Count;
1718 PointLight Data[];
18- } PointLightBuffer;
19+ }
20+ PointLightBuffer;
1921
20- layout (std140, set = 0 , binding = 8 ) readonly buffer SpotLightSB
22+ layout (std140, set = 0 , binding = 8 ) readonly buffer SpotLightSB
2123{
22- uint Count;
24+ uint Count;
2325 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;
3128
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;
3333
34+ layout (location = 0 ) out vec4 OutColor;
3435
3536vec3 ComputeDirectionalLight(DirectionalLight light, vec3 normal, vec3 viewDir, vec3 albedoMap, vec3 specularMap)
3637{
37- vec3 direction = light.Direction.xyz;
38+ vec3 direction = light.Direction.xyz;
3839
39- vec3 lightDir = normalize (direction);
40- vec3 ambient = light.Ambient.xyz * albedoMap;
40+ vec3 lightDir = normalize (direction);
41+ vec3 ambient = light.Ambient.xyz * albedoMap;
4142
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;
4445
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;
4849
4950 return vec3 (ambient + diffuse + specular);
5051}
5152
5253vec3 ComputePointLight(PointLight light, vec3 normal, vec3 viewDir, vec4 fragPos, vec3 albedoMap, vec3 specularMap)
5354{
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)));
5657
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;
5960
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;
6263
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;
6667
67- ambient *= attenuation;
68- diffuse *= attenuation;
69- specular *= attenuation;
68+ ambient *= attenuation;
69+ diffuse *= attenuation;
70+ specular *= attenuation;
7071
7172 return vec3 (ambient + diffuse + specular);
7273}
7374
7475vec3 ComputeSpotLight(SpotLight light, vec3 normal, vec3 viewDir, vec3 fragPos, vec3 albedoMap, vec3 specularMap)
7576{
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
7980
80- if (theta > light.CutOff)
81+ if (theta > light.CutOff)
8182 {
82- vec3 ambient = light.Ambient.xyz * albedoMap;
83+ vec3 ambient = light.Ambient.xyz * albedoMap;
8384
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;
8687
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;
9091
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)));
9394
94- diffuse *= attenuation;
95- specular *= attenuation;
95+ diffuse *= attenuation;
96+ specular *= attenuation;
9697
9798 return vec3 (ambient + diffuse + specular);
9899 }
@@ -102,33 +103,32 @@ vec3 ComputeSpotLight(SpotLight light, vec3 normal, vec3 viewDir, vec3 fragPos,
102103 }
103104}
104105
105-
106106void main()
107107{
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;
112112
113- vec3 viewDir = normalize ( ViewPos.xyz - fragPos.xyz);
113+ vec3 viewDir = normalize (ViewPos.xyz - fragPos.xyz);
114114
115115 vec3 lighting = vec3 (0.0 );
116- for (uint i = 0 ; i < DirectionalLightBuffer.Count; ++ i)
116+ for (uint i = 0 ; i < DirectionalLightBuffer.Count; ++ i)
117117 {
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);
120120 }
121121
122122 for (uint i = 0 ; i < PointLightBuffer.Count; ++ i)
123123 {
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);
126126 }
127127
128128 for (uint i = 0 ; i < SpotLightBuffer.Count; ++ i)
129129 {
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);
132132 }
133133
134134 OutColor = vec4 (lighting, 1.0 );
0 commit comments