forked from SurajSharma90/OpenGL-C---Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfragment_core.glsl
92 lines (74 loc) · 2.42 KB
/
fragment_core.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
82
83
84
85
86
87
88
89
90
91
92
#version 440
struct Material
{
vec3 ambient;
vec3 diffuse;
vec3 specular;
sampler2D diffuseTex;
sampler2D specularTex;
};
struct PointLight
{
vec3 position;
float intensity;
vec3 color;
float constant;
float linear;
float quadratic;
};
in vec3 vs_position;
in vec3 vs_color;
in vec2 vs_texcoord;
in vec3 vs_normal;
out vec4 fs_color;
//Uniforms
uniform Material material;
uniform PointLight pointLight;
uniform vec3 lightPos0;
uniform vec3 cameraPos;
//Functions
vec3 calculateAmbient(Material material)
{
return material.ambient;
}
vec3 calculateDiffuse(Material material, vec3 vs_position, vec3 vs_normal, vec3 lightPos0)
{
vec3 posToLightDirVec = normalize(lightPos0 - vs_position);
float diffuse = clamp(dot(posToLightDirVec, normalize(vs_normal)), 0, 1);
vec3 diffuseFinal = material.diffuse * diffuse;
return diffuseFinal;
}
vec3 calculateSpecular(Material material, vec3 vs_position, vec3 vs_normal, vec3 lightPos0, vec3 cameraPos)
{
vec3 lightToPosDirVec = normalize(vs_position - lightPos0);
vec3 reflectDirVec = normalize(reflect(lightToPosDirVec, normalize(vs_normal)));
vec3 posToViewDirVec = normalize(cameraPos - vs_position);
float specularConstant = pow(max(dot(posToViewDirVec, reflectDirVec), 0), 35);
//vec3 specularFinal = material.specular * specularConstant * texture(material.specularTex, vs_texcoord).rgb;
vec3 specularFinal = material.specular * specularConstant;
return specularFinal;
}
void main()
{
//fs_color = vec4(vs_color, 1.f);
//fs_color = texture(texture0, vs_texcoord) * texture(texture1, vs_texcoord) * vec4(vs_color, 1.f);
//Ambient light
vec3 ambientFinal = calculateAmbient(material);
//Diffuse light
vec3 diffuseFinal = calculateDiffuse(material, vs_position, vs_normal, pointLight.position);
//Specular light
vec3 specularFinal = calculateSpecular(material, vs_position, vs_normal, pointLight.position, cameraPos);
//Attenuation
float distance = length(pointLight.position - vs_position);
//constant linear quadratic
float attenuation = pointLight.constant / (1.f + pointLight.linear * distance + pointLight.quadratic * (distance * distance));
//Final light
ambientFinal *= attenuation;
diffuseFinal *= attenuation;
specularFinal *= attenuation;
fs_color =
texture(material.diffuseTex, vs_texcoord)
* (vec4(ambientFinal, 1.f) + vec4(diffuseFinal, 1.f) + vec4(specularFinal, 1.f));
fs_color =
(vec4(ambientFinal, 1.f) + vec4(diffuseFinal, 1.f) + vec4(specularFinal, 1.f));
}