-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.frag
46 lines (37 loc) · 1.58 KB
/
shader.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
#version 410
// Output Color
out vec4 color;
uniform mat4 MVP; // ModelViewProjection Matrix
uniform mat4 MV; // ModelView idMVPMatrix
uniform vec4 cameraPosition;
// Texture-related data;
uniform sampler2D rgbTexture;
uniform int widthTexture;
uniform int heightTexture;
// Data from Vertex Shader
in vec2 textureCoordinate;
in vec3 vertexNormal; // For Lighting computation
in vec3 ToLightVector; // Vector from Vertex to Light;
in vec3 ToCameraVector; // Vector from Vertex to Camera;
void main() {
// Assignment Constants below
// get the texture color
vec4 textureColor = texture(rgbTexture, textureCoordinate);
// apply Phong shading by using the following parameters
vec4 ka = vec4(0.25,0.25,0.25,1.0); // reflectance coeff. for ambient
vec4 Ia = vec4(0.3,0.3,0.3,1.0); // light color for ambient
vec4 Id = vec4(1.0, 1.0, 1.0, 1.0); // light color for diffuse
vec4 kd = vec4(1.0, 1.0, 1.0, 1.0); // reflectance coeff. for diffuse
vec4 Is = vec4(1.0, 1.0, 1.0, 1.0); // light color for specular
vec4 ks = vec4(1.0, 1.0, 1.0, 1.0); // reflectance coeff. for specular
int specExp = 100; // specular exponent
// compute ambient component
vec4 ambient = Ia * ka;
// compute diffuse component
vec4 diffuse = Id * kd * max(0, dot(vertexNormal, ToLightVector));
// compute specular component
vec4 specular = Is * ks * pow(max(0, dot(vertexNormal, normalize(ToLightVector + ToCameraVector))), specExp);
// compute the color using the following equation
color = vec4(clamp( textureColor.xyz * vec3(ambient + diffuse + specular), 0.0, 1.0), 1.0);
//color = textureColor;
}