-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFragmentShaderCode.glsl
81 lines (66 loc) · 2.65 KB
/
FragmentShaderCode.glsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#version 430
in vec2 UV;
in vec3 normalWorld;
in vec3 vertexPositionWorld;
uniform sampler2D myTextureSampler0;
uniform vec4 ambientLight;
uniform vec3 lightPositionWorld;
uniform vec3 eyePositionWorld;
uniform vec4 ambientLight0;
uniform vec3 lightPositionWorld0;
uniform vec3 eyePositionWorld0;
out vec4 color;
struct MultiLight
{
vec3 position;
vec3 color;
};
#define LIGHT_NUM 12
uniform MultiLight littleLight[LIGHT_NUM];
void main()
{
vec3 LightColor = vec3(1,1,1);
vec4 MaterialAmbientColor = texture( myTextureSampler0, UV ).rgba;
vec4 MaterialDiffuseColor = texture( myTextureSampler0, UV ).rgba;
//Diffuse
vec3 lightVectorWorld = normalize(lightPositionWorld - vertexPositionWorld);
float brightness = dot(lightVectorWorld, normalize(normalWorld));
vec4 diffuseLight = vec4(brightness, brightness, brightness, 1.0);
//Specular
vec3 reflectedLightVectorWorld = reflect(-lightVectorWorld, normalWorld);
vec3 eyeVectorWorld = normalize(eyePositionWorld - vertexPositionWorld);
float s =clamp(dot(reflectedLightVectorWorld, eyeVectorWorld), 0, 1);
s = pow(s, 50);
vec4 specularLight = vec4(s , s, s, 1);
//dir light
//Diffuse
vec3 lightVectorWorld0 = normalize(lightPositionWorld0 - vertexPositionWorld);
float brightness0 = dot(lightVectorWorld0, normalize(normalWorld));
vec4 diffuseLight0 = vec4(brightness0, brightness0, brightness0, 1.0);
//Specular
vec3 reflectedLightVectorWorld0 = reflect(-lightVectorWorld0, normalWorld);
vec3 eyeVectorWorld0 = normalize(eyePositionWorld0 - vertexPositionWorld);
float s1 =clamp(dot(reflectedLightVectorWorld0, eyeVectorWorld0), 0, 1);
s1 = pow(s1, 50);
vec4 specularLight0 = vec4(s1 , s1, s1, 1);
vec4 multi_result;
for(int i = 0; i < LIGHT_NUM; ++i)
{
vec3 lightVectorWorld2 = normalize(littleLight[i].position - vertexPositionWorld);
float brightness2 = dot(lightVectorWorld2, normalize(normalWorld));
vec4 diffuseLight2 = vec4(brightness2, brightness2, brightness2, 1.0) * vec4(littleLight[i].color, 1.0f);
vec3 reflectedLightVectorWorld2 = reflect(-lightVectorWorld2, normalWorld);
vec3 eyeVectorWorld2 = normalize(eyePositionWorld0 - vertexPositionWorld);
float s2 =clamp(dot(reflectedLightVectorWorld2, eyeVectorWorld2), 0, 1);
s2 = pow(s2, 50);
vec4 specularLight2 = vec4(s2, s2, s2, 1) * vec4(littleLight[i].color, 1.0f);
multi_result += diffuseLight2 * 0.6f + specularLight2 * 0.3f ;
}
color =
MaterialDiffuseColor * clamp(diffuseLight, 0, 1) * 1.5f+
specularLight +
MaterialAmbientColor * 0.2f +
MaterialAmbientColor * ambientLight0 +
MaterialDiffuseColor * clamp(diffuseLight0, 0, 1) * 0.5f+
specularLight0 * MaterialAmbientColor *0.5f + multi_result;
}