@@ -7,33 +7,101 @@ in vec3 Color;
77in vec2 TexCoord;
88in vec3 Normal;
99
10- uniform mat4 modelMatrix;
1110uniform mat4 viewMatrix;
1211
1312uniform vec3 viewPos;
1413uniform sampler2D sampler ;
1514uniform vec3 uniformColor;
1615uniform bool useTexture;
1716
18- void main() {
19- vec3 l_dir_world = vec3 (5 , 5 , 5 );
20- vec3 l_dir = normalize ((viewMatrix * modelMatrix * vec4 (l_dir_world, 0.0 )).xyz);
17+ struct Material {
18+ vec3 diffuse;
19+ vec3 specular;
20+ vec3 ambient;
21+ float shininess;
22+ };
23+
24+ uniform Material material;
25+
26+ struct PointLight {
27+ vec3 position;
28+
29+ float radius;
30+
31+ float intensity;
32+ vec3 color;
33+ };
34+
35+ struct SunLight {
36+ vec3 direction;
37+
38+ float intensity;
39+ vec3 color;
40+ };
41+
42+ #define MAX_POINT_LIGHTS 25
43+ #define MAX_SUN_LIGHTS 10
44+
45+ uniform int pointLightsCount;
46+ uniform PointLight pointLights[MAX_POINT_LIGHTS];
47+
48+ uniform int sunLightsCount;
49+ uniform SunLight sunLights[MAX_SUN_LIGHTS];
2150
22- vec3 diffuse = vec3 (0.6 , 0.3 , 0.3 );
23- vec3 ambient = diffuse * 0 .3f;
24- vec3 specular = vec3 (0.7 , 0.7 , 0.7 );
25- float shininess = 4 .0f;
51+ vec3 calculateSunLight(SunLight light, Material material, vec3 fragPos, vec3 normal) {
52+ vec3 lightDir = (viewMatrix * vec4 (light.direction, 0.0 )).xyz;
53+ vec3 fragToLight = - lightDir;
54+ vec3 L = normalize (fragToLight);
55+ vec3 N = normalize (normal);
56+ vec3 R = reflect (- L, N);
57+ vec3 V = normalize (- fragPos);
58+
59+ vec3 ambientLight = light.color * 0 .3f * material.ambient;
60+ vec3 diffuseLight = light.color * material.diffuse * max (0.0 , dot (N, L));
61+ vec3 specularLight = light.color * material.specular * pow (max (0.0 , dot (R, V)), material.shininess);
2662
27- vec3 L = l_dir;
28- vec3 N = normalize (Normal);
63+ return light.intensity * (ambientLight + diffuseLight + specularLight);
64+ }
65+
66+ vec3 calculatePointLight(PointLight light, Material material, vec3 fragPos, vec3 normal) {
67+ vec3 lightPos = (viewMatrix * vec4 (light.position, 1.0 )).xyz;
68+ vec3 fragToLight = lightPos - fragPos;
69+ float lightDist = length (fragToLight);
70+ float lightRadius = light.radius;
71+ vec3 L = normalize (fragToLight);
72+ vec3 N = normalize (normal);
2973 vec3 R = reflect (- L, N);
30- vec3 V = normalize (- FragPos);
74+ vec3 V = normalize (- fragPos);
75+
76+ float constant = 1.0 ;
77+ float linear = 0 .09f;
78+ float quadratic = 0 .032f;
3179
32- vec3 ambientLight = ambient;
33- vec3 diffuseLight = diffuse * max (0.0 , dot (N, L));
34- vec3 specularLight = specular * pow (max (0.0 , dot (R, V)), shininess);
80+ vec3 ambientLight = light.color * 0 .3f * material.ambient;
81+ vec3 diffuseLight = light.color * material.diffuse * max (0.0 , dot (N, L));
82+ vec3 specularLight = light.color * material.specular * pow (max (0.0 , dot (R, V)), material.shininess);
83+
84+ // float attenuation = clamp(1.0 / (1.0 + 0.0 * lightDist + 1.0 * lightDist * lightDist), 0.0, 1.0);
85+ // float attenuation = clamp(1.0 - lightDist*lightDist/(lightRadius*lightRadius), 0.0, 1.0);
86+ float attenuation = 1.0 / (constant + linear * lightDist + quadratic * (lightDist * lightDist));
87+
88+ return light.intensity * attenuation * (ambientLight + diffuseLight + specularLight);
89+ }
90+
91+ void main() {
92+ vec3 l_dir_world = vec3 (1 , 0 , 1 );
93+ vec3 l_dir = normalize ((viewMatrix * vec4 (l_dir_world, 0.0 )).xyz);
3594
36- vec3 outColor = ambientLight + diffuseLight + specularLight;
95+ vec3 l_pos_world = vec3 (3 , 0 , 0 );
96+ vec3 l_pos = (viewMatrix * vec4 (l_pos_world, 1.0 )).xyz;
97+
98+ vec3 outColor = vec3 (0 );
99+ for (int i = 0 ; i < sunLightsCount; i++ ) {
100+ outColor += calculateSunLight(sunLights[i], material, FragPos, Normal);
101+ }
102+ for (int i = 0 ; i < pointLightsCount; i++ ) {
103+ outColor += calculatePointLight(pointLights[i], material, FragPos, Normal);
104+ }
37105
38106 FragColor = vec4 (outColor, 1.0 );
39107}
0 commit comments