-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.frag
88 lines (63 loc) · 2.2 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
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
88
#version 330 core
// This is a sample fragment shader.
struct Material {
vec3 diffuse;
vec3 specular;
vec3 ambient;
float shininess;
};
struct pointLight {
vec3 position;
vec3 color;
};
// Inputs to the fragment shader are the outputs of the same name from the vertex shader.
// Note that you do not have access to the vertex shader's default output, gl_Position.
in vec3 posOutput;
in vec3 normalOutput;
uniform pointLight lightSource;
uniform Material material;
uniform vec3 viewPos;
uniform int colormode;
uniform vec3 color;
// You can output many things. The first vec4 type output determines the color of the fragment
out vec4 fragColor;
//function prototypes
vec3 normalColoring(vec3 normal);
vec3 illumination(pointLight light, vec3 normal, vec3 position, vec3 viewDir);
void main()
{
vec3 viewDir = normalize(viewPos - posOutput);
vec3 normal = normalize(normalOutput);
// normal coloring
fragColor = vec4(color,1.0f);
// fragColor = vec4(normalColoring(normal),1.0f);
}
//normal coloring
vec3 normalColoring(vec3 normal)
{
//Calculate RGB values corresponding to x,y,z components of the normal
float red = (normal.x + 1.0f)/2.0f;
float green = (normal.y + 1.0f)/2.0f;
float blue = (normal.x + 1.0f)/2.0f;
vec3 color = vec3(red, green, blue);
return color;
}
// Calculate color using Phong Illumination Model
vec3 illumination(pointLight light, vec3 normal, vec3 position, vec3 viewDir) {
vec3 lightDir = normalize(light.position - position);
// Diffuse shading
vec3 diffuse = light.color* material.diffuse* max(dot(normal, lightDir),0.0);
// Specular shading
vec3 reflectDir = 2* dot(normal,lightDir)*normal - lightDir;
vec3 specular = material.specular * light.color * pow(max(dot(reflectDir,viewDir),0.0), material.shininess);
// Ambient shading
vec3 ambient = light.color * material.ambient;
// Linear Attenuation
// Attenuation
float distance = length(light.position - position);
float attenuation = 1.0f / (1.0f + 0.07 * distance);
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}