Skip to content

Commit

Permalink
Updated scene and deliverables
Browse files Browse the repository at this point in the history
  • Loading branch information
xRuiAlves committed Oct 21, 2018
1 parent dc78465 commit ed13318
Show file tree
Hide file tree
Showing 15 changed files with 606 additions and 34 deletions.
Binary file modified submissions/tp1/final/LAIG_TP1_T2_G05.zip
Binary file not shown.
1 change: 1 addition & 0 deletions submissions/tp1/final/lib/CGF.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions submissions/tp1/final/lib/CGF/shaders/Gouraud/fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifdef GL_ES
precision highp float;
#endif

varying vec4 vFinalColor;

void main() {
gl_FragColor = vFinalColor;
}
26 changes: 26 additions & 0 deletions submissions/tp1/final/lib/CGF/shaders/Gouraud/lambert-vertex.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;

uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat4 uNMatrix;

uniform vec3 uLightDirection;
uniform vec4 uLightDiffuse;
uniform vec4 uMaterialDiffuse;

varying 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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
attribute vec3 aVertexPosition;
attribute 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;

varying 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;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifdef GL_ES
precision highp float;
#endif

varying vec4 vFinalColor;
varying vec2 vTextureCoord;

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 = texture2D(uSampler, vTextureCoord);
gl_FragColor = textureColor * vFinalColor;
}
else
gl_FragColor = vFinalColor;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute 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;

varying vec4 vFinalColor;
varying 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;

}

Loading

0 comments on commit ed13318

Please sign in to comment.