-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.frag
87 lines (66 loc) · 1.98 KB
/
texture.frag
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
82
83
84
85
86
87
#version 130
// Texture mapping vertex shader
//
// Contributor: Jietong Chen
// INCOMING DATA
// Vertex location (in camera space)
in vec3 position;
// Normal vector at vertex (in camera space)
in vec3 normal;
// Texture coordinate for this vertex
in vec2 texCoord;
// Point light position (in camera space)
in vec3 pLightPos;
// struct of material properties
struct Material
{
// the ambient reflection coefficient
float ka;
// the diffuse reflection coefficient
float kd;
// the specular reflection coefficient
float ks;
// the specular exponent
float shininess;
};
// Material properties of the object
uniform Material material;
// Ambient light color
uniform vec4 aLightColor;
// Point light color
uniform vec4 pLightColor;
// Texture of front face
uniform sampler2D tex;
// OUTGOING DATA
out vec4 finalColor;
void main()
{
// the front facing texture color
vec4 texColor = texture( tex, texCoord );
if( texColor.a < 0.1 ) {
// omit the fragment with alpha channel < 0.1
discard;
}
// the normal vector
vec3 n = normalize( normal );
// the light direction vector
vec3 l = normalize( pLightPos - position );
// the viewing direction vector
vec3 v = normalize( -position );
// the reflection vector
vec3 r = reflect( -l, n );
// the half-way vector (Blinn-Phong shading)
// vec3 h = normalize( l + v );
// the ambient color
vec3 aColor = aLightColor.rgb * material.ka * texColor.rgb;
// the diffuse color
vec3 dColor = pLightColor.rgb * material.kd * texColor.rgb *
max( dot( n, l ), 0 );
// the specular highlight
float spec = pow( max( dot( v, r ), 0 ), material.shininess );
// the specular highlight(Blinn-Phong shading)
// float spec = pow( max( dot( n, h ), 0 ), material.shininess );
// the specular color
vec3 sColor = pLightColor.rgb * material.ks * texColor.rgb * spec;
finalColor = vec4( aColor + dColor + sColor, texColor.a );
}