-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
1,232 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#version 300 es | ||
precision highp float; | ||
|
||
in vec4 vFinalColor; | ||
out vec4 fragColor; | ||
|
||
void main() { | ||
fragColor = vFinalColor; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#version 300 es | ||
precision highp float; | ||
|
||
in vec3 aVertexPosition; | ||
in vec3 aVertexNormal; | ||
|
||
uniform mat4 uMVMatrix; | ||
uniform mat4 uPMatrix; | ||
uniform mat4 uNMatrix; | ||
|
||
uniform vec3 uLightDirection; | ||
uniform vec4 uLightDiffuse; | ||
uniform vec4 uMaterialDiffuse; | ||
|
||
out vec4 vFinalColor; | ||
|
||
void main() { | ||
vec3 light = vec3(uMVMatrix * vec4(uLightDirection, 0.0)); | ||
|
||
vec3 N = normalize(vec3(uNMatrix * vec4(aVertexNormal, 1.0))); | ||
vec3 L = normalize(light); | ||
|
||
float lambertTerm = dot(N, -L); | ||
|
||
vFinalColor = uMaterialDiffuse * uLightDiffuse * lambertTerm; | ||
vFinalColor.a = 1.0; | ||
|
||
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); | ||
} |
122 changes: 122 additions & 0 deletions
122
Proj/lib/CGF/shaders/Gouraud/multiple_light-vertex.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#version 300 es | ||
precision highp float; | ||
|
||
in vec3 aVertexPosition; | ||
in vec3 aVertexNormal; | ||
|
||
struct lightProperties { | ||
vec4 position; // Default: (0, 0, 1, 0) | ||
vec4 ambient; // Default: (0, 0, 0, 1) | ||
vec4 diffuse; // Default: (0, 0, 0, 1) | ||
vec4 specular; // Default: (0, 0, 0, 1) | ||
vec4 half_vector; | ||
vec3 spot_direction; // Default: (0, 0, -1) | ||
float spot_exponent; // Default: 0 (possible values [0, 128] | ||
float spot_cutoff; // Default: 180 (possible values [0, 90] or 180) | ||
float constant_attenuation; // Default: 1 (value must be >= 0) | ||
float linear_attenuation; // Default: 0 (value must be >= 0) | ||
float quadratic_attenuation; // Default: 0 (value must be >= 0) | ||
bool enabled; // Deafult: false | ||
}; | ||
|
||
struct materialProperties { | ||
vec4 ambient; // Default: (0, 0, 0, 1) | ||
vec4 diffuse; // Default: (0, 0, 0, 1) | ||
vec4 specular; // Default: (0, 0, 0, 1) | ||
vec4 emission; // Default: (0, 0, 0, 1) | ||
float shininess; // Default: 0 (possible values [0, 128]) | ||
}; | ||
|
||
uniform mat4 uMVMatrix; | ||
uniform mat4 uPMatrix; | ||
uniform mat4 uNMatrix; | ||
|
||
uniform bool uLightEnabled; | ||
uniform bool uLightModelTwoSided; | ||
|
||
#define NUMBER_OF_LIGHTS 8 | ||
|
||
uniform vec4 uGlobalAmbient; | ||
|
||
uniform lightProperties uLight[NUMBER_OF_LIGHTS]; | ||
|
||
uniform materialProperties uFrontMaterial; | ||
uniform materialProperties uBackMaterial; | ||
|
||
out vec4 vFinalColor; | ||
|
||
vec4 lighting(vec4 vertex, vec3 E, vec3 N) { | ||
|
||
vec4 result = vec4(0.0, 0.0, 0.0, 0.0); | ||
|
||
for (int i = 0; i < NUMBER_OF_LIGHTS; i++) { | ||
if (uLight[i].enabled) { | ||
|
||
float att = 1.0; | ||
float spot_effect = 1.0; | ||
vec3 L = vec3(0.0); | ||
|
||
if (uLight[i].position.w == 1.0) { | ||
L = (uLight[i].position - vertex).xyz; | ||
float dist = length(L); | ||
L = normalize(L); | ||
|
||
if (uLight[i].spot_cutoff != 180.0) { | ||
vec3 sd = normalize(vec3(uLight[i].spot_direction)); | ||
float cos_cur_angle = dot(sd, -L); | ||
float cos_inner_cone_angle = cos(radians(clamp(uLight[i].spot_cutoff, 0.0, 89.0))); | ||
|
||
spot_effect = pow(clamp(cos_cur_angle/ cos_inner_cone_angle, 0.0, 1.0), clamp(uLight[i].spot_exponent, 0.0, 128.0)); | ||
} | ||
|
||
att = 1.0 / (uLight[i].constant_attenuation + uLight[i].linear_attenuation * dist + uLight[i].quadratic_attenuation * dist * dist); | ||
|
||
} else { | ||
L = normalize(uLight[i].position.xyz); | ||
} | ||
|
||
float lambertTerm = max(dot(N, L), 0.0); | ||
|
||
vec4 Ia = uLight[i].ambient * uFrontMaterial.ambient; | ||
|
||
vec4 Id = uLight[i].diffuse * uFrontMaterial.diffuse * lambertTerm; | ||
|
||
vec4 Is = vec4(0.0, 0.0, 0.0, 0.0); | ||
|
||
if (lambertTerm > 0.0) { | ||
vec3 R = reflect(-L, N); | ||
float specular = pow( max( dot(R, E), 0.0 ), uFrontMaterial.shininess); | ||
|
||
Is = uLight[i].specular * uFrontMaterial.specular * specular; | ||
} | ||
|
||
if (uLight[i].position.w == 1.0) | ||
result += att * max(spot_effect * (Id + Is), Ia); | ||
else | ||
result += att * spot_effect * (Ia + Id + Is); | ||
} | ||
} | ||
|
||
result += uGlobalAmbient * uFrontMaterial.ambient + uFrontMaterial.emission; | ||
result = clamp(result, vec4(0.0), vec4(1.0)); | ||
|
||
result.a = 1.0; | ||
return result; | ||
} | ||
|
||
void main() { | ||
|
||
// Transformed Vertex position | ||
vec4 vertex = uMVMatrix * vec4(aVertexPosition, 1.0); | ||
|
||
// Transformed normal position | ||
vec3 N = normalize(vec3(uNMatrix * vec4(aVertexNormal, 1.0))); | ||
|
||
vec3 eyeVec = -vec3(vertex.xyz); | ||
vec3 E = normalize(eyeVec); | ||
|
||
vFinalColor = lighting(vertex, E, N); | ||
|
||
gl_Position = uPMatrix * vertex; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#version 300 es | ||
precision highp float; | ||
|
||
in vec4 vFinalColor; | ||
in vec2 vTextureCoord; | ||
|
||
out vec4 fragColor; | ||
|
||
uniform sampler2D uSampler; | ||
|
||
uniform bool uUseTexture; | ||
|
||
void main() { | ||
// Branching should be reduced to a minimal. | ||
// When based on a non-changing uniform, it is usually optimized. | ||
if (uUseTexture) | ||
{ | ||
vec4 textureColor = texture(uSampler, vTextureCoord); | ||
fragColor = textureColor * vFinalColor; | ||
} | ||
else | ||
fragColor = vFinalColor; | ||
|
||
} |
130 changes: 130 additions & 0 deletions
130
Proj/lib/CGF/shaders/Gouraud/textured/multiple_light-vertex.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#version 300 es | ||
precision highp float; | ||
|
||
in vec3 aVertexPosition; | ||
in vec3 aVertexNormal; | ||
in vec2 aTextureCoord; | ||
|
||
uniform bool uUseTexture; | ||
|
||
struct lightProperties { | ||
vec4 position; // Default: (0, 0, 1, 0) | ||
vec4 ambient; // Default: (0, 0, 0, 1) | ||
vec4 diffuse; // Default: (0, 0, 0, 1) | ||
vec4 specular; // Default: (0, 0, 0, 1) | ||
vec4 half_vector; | ||
vec3 spot_direction; // Default: (0, 0, -1) | ||
float spot_exponent; // Default: 0 (possible values [0, 128] | ||
float spot_cutoff; // Default: 180 (possible values [0, 90] or 180) | ||
float constant_attenuation; // Default: 1 (value must be >= 0) | ||
float linear_attenuation; // Default: 0 (value must be >= 0) | ||
float quadratic_attenuation; // Default: 0 (value must be >= 0) | ||
bool enabled; // Default: false | ||
}; | ||
|
||
struct materialProperties { | ||
vec4 ambient; // Default: (0, 0, 0, 1) | ||
vec4 diffuse; // Default: (0, 0, 0, 1) | ||
vec4 specular; // Default: (0, 0, 0, 1) | ||
vec4 emission; // Default: (0, 0, 0, 1) | ||
float shininess; // Default: 0 (possible values [0, 128]) | ||
}; | ||
|
||
uniform mat4 uMVMatrix; | ||
uniform mat4 uPMatrix; | ||
uniform mat4 uNMatrix; | ||
|
||
uniform bool uLightEnabled; // not being used | ||
uniform bool uLightModelTwoSided; // not being used | ||
|
||
#define NUMBER_OF_LIGHTS 8 | ||
|
||
uniform vec4 uGlobalAmbient; | ||
|
||
uniform lightProperties uLight[NUMBER_OF_LIGHTS]; | ||
|
||
uniform materialProperties uFrontMaterial; | ||
uniform materialProperties uBackMaterial; | ||
|
||
out vec4 vFinalColor; | ||
out vec2 vTextureCoord; | ||
|
||
vec4 lighting(vec4 vertex, vec3 E, vec3 N) { | ||
|
||
vec4 result = vec4(0.0, 0.0, 0.0, 0.0); | ||
|
||
for (int i = 0; i < NUMBER_OF_LIGHTS; i++) { | ||
if (uLight[i].enabled) { | ||
|
||
float att = 1.0; | ||
float spot_effect = 1.0; | ||
vec3 L = vec3(0.0); | ||
|
||
if (uLight[i].position.w == 1.0) { | ||
L = (uLight[i].position - vertex).xyz; | ||
float dist = length(L); | ||
L = normalize(L); | ||
|
||
if (uLight[i].spot_cutoff != 180.0) { | ||
vec3 sd = normalize(vec3(uLight[i].spot_direction)); | ||
float cos_cur_angle = dot(sd, -L); | ||
float cos_inner_cone_angle = cos(radians(clamp(uLight[i].spot_cutoff, 0.0, 89.0))); | ||
|
||
spot_effect = pow(clamp(cos_cur_angle/ cos_inner_cone_angle, 0.0, 1.0), clamp(uLight[i].spot_exponent, 0.0, 128.0)); | ||
} | ||
|
||
att = 1.0 / (uLight[i].constant_attenuation + uLight[i].linear_attenuation * dist + uLight[i].quadratic_attenuation * dist * dist); | ||
|
||
} else { | ||
L = normalize(uLight[i].position.xyz); | ||
} | ||
|
||
float lambertTerm = max(dot(N, L), 0.0); | ||
|
||
vec4 Ia = uLight[i].ambient * uFrontMaterial.ambient; | ||
|
||
vec4 Id = uLight[i].diffuse * uFrontMaterial.diffuse * lambertTerm; | ||
|
||
vec4 Is = vec4(0.0, 0.0, 0.0, 0.0); | ||
|
||
if (lambertTerm > 0.0) { | ||
vec3 R = reflect(-L, N); | ||
float specular = pow( max( dot(R, E), 0.0 ), uFrontMaterial.shininess); | ||
|
||
Is = uLight[i].specular * uFrontMaterial.specular * specular; | ||
} | ||
|
||
if (uLight[i].position.w == 1.0) | ||
result += att * max(spot_effect * (Id + Is), Ia); | ||
else | ||
result += att * spot_effect * (Ia + Id + Is); | ||
} | ||
} | ||
|
||
result += uGlobalAmbient * uFrontMaterial.ambient + uFrontMaterial.emission; | ||
result = clamp(result, vec4(0.0), vec4(1.0)); | ||
|
||
result.a = 1.0; | ||
return result; | ||
} | ||
|
||
void main() { | ||
|
||
// Transformed Vertex position | ||
vec4 vertex = uMVMatrix * vec4(aVertexPosition, 1.0); | ||
|
||
// Transformed normal position | ||
vec3 N = normalize(vec3(uNMatrix * vec4(aVertexNormal, 1.0))); | ||
|
||
vec3 eyeVec = -vec3(vertex.xyz); | ||
vec3 E = normalize(eyeVec); | ||
|
||
vFinalColor = lighting(vertex, E, N); | ||
|
||
gl_Position = uPMatrix * vertex; | ||
|
||
if (uUseTexture) | ||
vTextureCoord = aTextureCoord; | ||
|
||
} | ||
|
Oops, something went wrong.