aboutsummaryrefslogtreecommitdiff
path: root/08-august/resources/shaders
diff options
context:
space:
mode:
Diffstat (limited to '08-august/resources/shaders')
-rw-r--r--08-august/resources/shaders/passShader.frag13
-rw-r--r--08-august/resources/shaders/passShader.vert15
-rw-r--r--08-august/resources/shaders/shader.frag58
-rw-r--r--08-august/resources/shaders/shader.vert50
-rw-r--r--08-august/resources/shaders/skyboxShader.frag11
-rw-r--r--08-august/resources/shaders/skyboxShader.vert12
-rw-r--r--08-august/resources/shaders/terrainShader.frag72
-rw-r--r--08-august/resources/shaders/terrainShader.vert39
8 files changed, 270 insertions, 0 deletions
diff --git a/08-august/resources/shaders/passShader.frag b/08-august/resources/shaders/passShader.frag
new file mode 100644
index 0000000..4c8dec1
--- /dev/null
+++ b/08-august/resources/shaders/passShader.frag
@@ -0,0 +1,13 @@
+#version 420
+
+in vec4 Fragment_Color;
+in vec2 Fragment_Uv;
+
+out vec4 out_color;
+
+uniform sampler2D Texture;
+
+void main()
+{
+ out_color = Fragment_Color * texture(Texture, Fragment_Uv);
+}
diff --git a/08-august/resources/shaders/passShader.vert b/08-august/resources/shaders/passShader.vert
new file mode 100644
index 0000000..c75a347
--- /dev/null
+++ b/08-august/resources/shaders/passShader.vert
@@ -0,0 +1,15 @@
+#version 420
+
+in layout(location=0) vec2 Model_Position;
+
+uniform mat4 M_totalTransform;
+
+out vec4 Fragment_Color;
+out vec2 Fragment_Uv;
+
+void main()
+{
+ Fragment_Color = vec4(1.0, 1.0, 1.0, 1.0);
+ Fragment_Uv = vec2( (Model_Position.x + 1.0)/2.0, 1.0 - (Model_Position.y + 1.0)/2.0 );
+ gl_Position = M_totalTransform * vec4(Model_Position, 0.0, 1.0);
+}
diff --git a/08-august/resources/shaders/shader.frag b/08-august/resources/shaders/shader.frag
new file mode 100644
index 0000000..42168d5
--- /dev/null
+++ b/08-august/resources/shaders/shader.frag
@@ -0,0 +1,58 @@
+#version 420
+
+in vec2 Fragment_UV;
+in vec4 Fragment_Color;
+in vec3 toLightVector[4];
+in vec3 toEyeVector;
+in vec3 tangent0;
+
+out vec4 out_color;
+
+uniform vec4 lightColor[4];
+uniform vec3 attenuation[4];
+uniform sampler2D Texture;
+uniform sampler2D normalMap;
+
+void main()
+{
+ vec4 totalDiffuse = vec4(0.0, 0.0, 0.0, 1.0);
+ vec4 totalSpecular = vec4(0.0, 0.0, 0.0, 1.0);
+
+ /* We get the normal from the normal map and we transform it into world space */
+ vec3 normal = normalize( (255.0 / 128.0 * texture(normalMap, Fragment_UV) - 1.0).xyz );
+
+ for(int i = 0; i < 4; i++)
+ {
+ /*Light Attenuation*/
+ float dist = length(toLightVector[i]);
+ float attFactor = attenuation[i].x + (attenuation[i].y * dist) + (attenuation[i].z * dist * dist);
+ vec3 unitToLightVector = normalize(toLightVector[i]);
+
+ /*Diffuse lighting*/
+
+ /*La intensidad es el cos entre la normal y el vector hacia la luz*/
+ float brightness = dot(unitToLightVector, normalize(normal));
+ /*No queremos luz negativa ni mayor a 1*/
+ brightness = max(brightness, 0.0);
+ totalDiffuse = totalDiffuse + (brightness * lightColor[i]) / attFactor;
+
+ /* Specular lighting */
+
+ /* Reflejamos el vector hacia la luz (su inverso) en la normal */
+ vec3 Vector_ReflectedLight = reflect(-unitToLightVector, normal);
+
+ /* La luz especular es el cos del angulo entre el vector hacia el ojo y la luz reflectada en la normal */
+ float specularity = clamp( dot(Vector_ReflectedLight, toEyeVector), 0, 1 );
+ float reflectivity = 0.5;
+ /* Que tan grande es el specular highlight */
+ specularity = pow(specularity, 10);
+ totalSpecular = totalSpecular + clamp( reflectivity * specularity * lightColor[i], 0, 1) / attFactor;
+ }
+
+ /* Ambient Light */
+ totalDiffuse = max(totalDiffuse, 0.2);
+
+ /* Juntamos todo para el color final*/
+ out_color = totalDiffuse * texture(Texture, Fragment_UV) + totalSpecular;
+ out_color = vec4(tangent0, 1.0);
+}
diff --git a/08-august/resources/shaders/shader.vert b/08-august/resources/shaders/shader.vert
new file mode 100644
index 0000000..282ac62
--- /dev/null
+++ b/08-august/resources/shaders/shader.vert
@@ -0,0 +1,50 @@
+#version 420
+
+in layout(location=0) vec3 position;
+in layout(location=1) vec2 Texture_UV;
+in layout(location=2) vec3 normal;
+in layout(location=3) vec3 tangent;
+
+uniform mat4 M_MVP; /* Total Transform matrix */
+uniform mat4 M_model; /* Model to world space transformation matrix */
+
+uniform vec3 lightPosition[4];
+uniform vec3 World_eyePosition;
+
+out vec2 Fragment_UV;
+out vec4 Fragment_Color;
+out vec3 toLightVector[4];
+out vec3 toEyeVector;
+out vec3 tangent0;
+
+void main()
+{
+ /*We add a 0 on the vec4 so we can remove the translation from the matrix
+ (WE DONT WANT THE NORMAL TO BE TRANSLATED) */
+ vec3 n = normalize( (M_model * vec4(normal, 0.0)).xyz );
+ vec3 t = normalize( (M_model * vec4(tangent, 0.0)).xyz );
+
+ /* Orthogonalization */
+ t = normalize(t - dot(t, n) * n);
+
+ vec3 biTangent = normalize( cross(t, n) );
+
+ /* Matrix use by normal mapping */
+ mat3 tbnMatrix = mat3(t, biTangent, n);
+ tbnMatrix = transpose(tbnMatrix);
+
+ vec3 World_Position = vec3(M_model * vec4(position, 1.0));
+
+ for(int i = 0; i < 4; i++)
+ {
+ /* vector que apunta hacia la luz*/
+ toLightVector[i] = tbnMatrix * (lightPosition[i] - World_Position);
+ }
+ /* Vector hacia el ojo*/
+ toEyeVector = normalize( tbnMatrix * (World_eyePosition - World_Position) );
+
+ gl_Position = M_MVP * vec4(position, 1.0);
+
+ Fragment_UV = vec2(Texture_UV.x, 1 - Texture_UV.y); /*Invert y axis*/
+ tangent0 = tangent;
+}
diff --git a/08-august/resources/shaders/skyboxShader.frag b/08-august/resources/shaders/skyboxShader.frag
new file mode 100644
index 0000000..99dcde3
--- /dev/null
+++ b/08-august/resources/shaders/skyboxShader.frag
@@ -0,0 +1,11 @@
+#version 420
+
+in vec3 Fragment_UV;
+out vec4 out_color;
+
+uniform samplerCube cubeMap;
+
+void main()
+{
+ out_color = texture(cubeMap, Fragment_UV);
+}
diff --git a/08-august/resources/shaders/skyboxShader.vert b/08-august/resources/shaders/skyboxShader.vert
new file mode 100644
index 0000000..3286875
--- /dev/null
+++ b/08-august/resources/shaders/skyboxShader.vert
@@ -0,0 +1,12 @@
+#version 420
+
+in layout(location=0) vec3 position; /* vertex position in model coordinates */
+out vec3 Fragment_UV; /* UV coordinates for the fragment */
+
+uniform mat4 M_MVP;
+
+void main()
+{
+ gl_Position = M_MVP * vec4(position, 1.0);
+ Fragment_UV = position;
+}
diff --git a/08-august/resources/shaders/terrainShader.frag b/08-august/resources/shaders/terrainShader.frag
new file mode 100644
index 0000000..21623bb
--- /dev/null
+++ b/08-august/resources/shaders/terrainShader.frag
@@ -0,0 +1,72 @@
+#version 420
+
+in vec2 Fragment_UV;
+in vec3 normal0;
+in vec3 toLightVector[4];
+in vec3 toEyeVector;
+
+out vec4 out_color;
+
+uniform vec4 lightColor[4];
+uniform vec3 attenuation[4];
+uniform vec3 World_eyePosition;
+
+uniform sampler2D Texture_Background;
+uniform sampler2D Texture_R;
+uniform sampler2D Texture_G;
+uniform sampler2D Texture_B;
+uniform sampler2D Texture_BlendMap;
+
+void main()
+{
+ vec4 totalDiffuse = vec4(0.0, 0.0, 0.0, 1.0);
+ vec4 totalSpecular = vec4(0.0, 0.0, 0.0, 1.0);
+
+ for(int i = 0; i < 4; i++)
+ {
+ /*Light Attenuation*/
+ float dist = length(toLightVector[i]);
+ float attFactor = attenuation[i].x + (attenuation[i].y * dist) + (attenuation[i].z * dist * dist);
+ vec3 unitToLightVector = normalize(toLightVector[i]);
+ /*Diffuse lighting*/
+
+ /*La intensidad es el cos entre la normal y el vector hacia la luz*/
+ float brightness = dot(unitToLightVector, normalize(normal0));
+ /*No queremos luz negativa ni mayor a 1*/
+ brightness = max(brightness, 0.0);
+ totalDiffuse = totalDiffuse + (brightness * lightColor[i]) / attFactor;
+
+ /* Specular lighting */
+
+ /* Reflejamos el vector hacia la luz (su inverso) en la normal */
+ vec3 Vector_ReflectedLight = reflect(-unitToLightVector, normal0);
+
+ /* La luz especular es el cos del angulo entre el vector hacia el ojo y la luz reflectada en la normal */
+ float specularity = clamp( dot(Vector_ReflectedLight, toEyeVector), 0, 1 );
+ float reflectivity = 0.5;
+ /* Que tan grande es el specular highlight */
+ specularity = pow(specularity, 10);
+ totalSpecular = totalSpecular + clamp( reflectivity * specularity * lightColor[i], 0, 1) / attFactor;
+ }
+
+ /* Ambient Light */
+ totalDiffuse = max(totalDiffuse, 0.2);
+
+ /*Terrain color*/
+ /*The color of the current fragment from the blendmap*/
+ vec4 blendMapColor = texture(Texture_BlendMap, Fragment_UV);
+ /*We want the background color when we have black in the blend map*/
+ float backTextureAmount = 1 - (blendMapColor.r + blendMapColor.g + blendMapColor.b);
+ /*So we dont lose image quality from the tiles textures (Fragment_UV make the terrain look less HD)*/
+ vec2 Tiled_UV = Fragment_UV * 50.0f;
+ /*We get the color of each map tile (soil1, soil2, etc)*/
+ vec4 backgroundTextureColor = texture(Texture_Background, Tiled_UV) * backTextureAmount;
+ vec4 rTextureColor = texture(Texture_R, Tiled_UV) * blendMapColor.r;
+ vec4 gTextureColor = texture(Texture_G, Tiled_UV) * blendMapColor.g;
+ vec4 bTextureColor = texture(Texture_B, Tiled_UV) * blendMapColor.b;
+ /*We mix them corresponding with the blendmap*/
+ vec4 totalColor = backgroundTextureColor + rTextureColor + gTextureColor + bTextureColor;
+
+ /*Our final color for the fragment*/
+ out_color = totalDiffuse * totalColor + totalSpecular;
+}
diff --git a/08-august/resources/shaders/terrainShader.vert b/08-august/resources/shaders/terrainShader.vert
new file mode 100644
index 0000000..1850947
--- /dev/null
+++ b/08-august/resources/shaders/terrainShader.vert
@@ -0,0 +1,39 @@
+#version 420
+
+in layout(location=0) vec3 position; /* vertex position in model space */
+in layout(location=1) vec2 Texture_UV;
+in layout(location=2) vec3 normal; /* vertex normal in model space */
+in layout(location=3) vec3 tangent;
+
+uniform mat4 M_MVP; /* Total Transform matrix */
+uniform mat4 M_model; /* Model to world space transformation matrix */
+
+uniform vec3 lightPosition[4];
+uniform vec3 World_eyePosition;
+
+out vec2 Fragment_UV; /* UV coordinates for the fragment */
+out vec3 normal0;
+out vec4 Fragment_Color;
+out vec3 toLightVector[4];
+out vec3 toEyeVector;
+
+void main()
+{
+ vec3 World_Position = vec3(M_model * vec4(position, 1.0));
+
+ for(int i = 0; i < 4; i++)
+ {
+ /* vector que apunta hacia la luz*/
+ toLightVector[i] = lightPosition[i] - World_Position;
+ }
+ /* Vector hacia el ojo*/
+ toEyeVector = normalize(World_eyePosition - World_Position);
+
+ gl_Position = M_MVP * vec4(position, 1.0);
+
+ Fragment_UV = vec2(Texture_UV.x, 1 - Texture_UV.y); /*Invert y axis*/
+
+ /*We add a 0 on the vec4 so we can remove the translation from the matrix
+ (WE DONT WANT THE NORMAL TO BE TRANSLATED) */
+ normal0 = normalize( (M_model * vec4(normal, 0.0)).xyz );
+}