From 1bcb5ddd505e5c4bdac6f254e931e9c3145be88d Mon Sep 17 00:00:00 2001 From: victorfisac Date: Mon, 21 Dec 2015 17:25:22 +0100 Subject: Added lighting engine module - New lighting engine module which contains new data types Light and Material. These data types and functions facilitates making a basic 3D iluminated program with a light and a model. - Added lighting engine module example (currently included in raylib.h; it might be compiled by separate and include lighting.h in game source C file). - Corrected some opengl defines control structures and added some TODO to fix raylib-opengl 1.1 source build (note: now source can be compiled without errors, but rlglReadPixels() won't work properly). Note: most of functions of phong version 330 shader are not in v100 shaders, so I couldn't write a version 100 phong shader. These functions are included from version 150. --- examples/resources/shaders/phong.fs | 76 +++++++++++++++++++++++++++++++++++++ examples/resources/shaders/phong.vs | 28 ++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 examples/resources/shaders/phong.fs create mode 100644 examples/resources/shaders/phong.vs (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/phong.fs b/examples/resources/shaders/phong.fs new file mode 100644 index 00000000..bb8826f4 --- /dev/null +++ b/examples/resources/shaders/phong.fs @@ -0,0 +1,76 @@ +#version 330 + +// Vertex shader input data +in vec2 fragTexCoord; +in vec3 fragNormal; + +// Diffuse data +uniform sampler2D texture0; +uniform vec4 tintColor; + +// Light attributes +uniform vec3 light_ambientColor = vec3(0.6, 0.3, 0); +uniform vec3 light_diffuseColor = vec3(1, 0.5, 0); +uniform vec3 light_specularColor = vec3(0, 1, 0); +uniform float light_intensity = 1; +uniform float light_specIntensity = 1; + +// Material attributes +uniform vec3 mat_ambientColor = vec3(1, 1, 1); +uniform vec3 mat_specularColor = vec3(1, 1, 1); +uniform float mat_glossiness = 50; + +// World attributes +uniform vec3 lightPos; +uniform vec3 cameraPos; + +// Fragment shader output data +out vec4 fragColor; + +vec3 AmbientLighting() +{ + return mat_ambientColor * light_ambientColor; +} + +vec3 DiffuseLighting(in vec3 N, in vec3 L) +{ + // Lambertian reflection calculation + float diffuse = clamp(dot(N, L), 0, 1); + + return tintColor.xyz * light_diffuseColor * light_intensity * diffuse; +} + +vec3 SpecularLighting(in vec3 N, in vec3 L, in vec3 V) +{ + float specular = 0; + + // Calculate specular reflection only if the surface is oriented to the light source + if(dot(N, L) > 0) + { + // Calculate half vector + vec3 H = normalize(L + V); + + // Calculate specular intensity + specular = pow(dot(N, H), 3 + mat_glossiness); + } + + return mat_specularColor * light_specularColor * light_specIntensity * specular; +} + +void main() +{ + // Normalize input vectors + vec3 L = normalize(lightPos); + vec3 V = normalize(cameraPos); + vec3 N = normalize(fragNormal); + + vec3 ambient = AmbientLighting(); + vec3 diffuse = DiffuseLighting(N, L); + vec3 specular = SpecularLighting(N, L, V); + + // Get base color from texture + vec4 textureColor = texture(texture0, fragTexCoord); + vec3 finalColor = textureColor.rgb; + + fragColor = vec4(finalColor * (ambient + diffuse + specular), textureColor.a); +} \ No newline at end of file diff --git a/examples/resources/shaders/phong.vs b/examples/resources/shaders/phong.vs new file mode 100644 index 00000000..25163902 --- /dev/null +++ b/examples/resources/shaders/phong.vs @@ -0,0 +1,28 @@ +#version 330 + +// Vertex input data +in vec3 vertexPosition; +in vec2 vertexTexCoord; +in vec3 vertexNormal; + +// Projection and model data +uniform mat4 projectionMatrix; +uniform mat4 modelviewMatrix; +uniform mat4 modelMatrix; + +// Attributes to fragment shader +out vec2 fragTexCoord; +out vec3 fragNormal; + +void main() +{ + // Send texture coord to fragment shader + fragTexCoord = vertexTexCoord; + + // Calculate view vector normal from model + mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); + fragNormal = normalize(normalMatrix * vertexNormal); + + // Calculate final vertex position + gl_Position = projectionMatrix * modelviewMatrix * vec4(vertexPosition, 1.0); +} \ No newline at end of file -- cgit v1.2.3 From fb6ef2c2f4fe22552908d339cda541453e43faec Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 13 Jan 2016 17:13:28 +0100 Subject: Vertex shaders optimization --- examples/resources/shaders/base.vs | 5 ++--- examples/resources/shaders/bloom.fs | 2 +- examples/resources/shaders/grayscale.fs | 6 +++--- examples/resources/shaders/phong.fs | 2 +- examples/resources/shaders/phong.vs | 4 ++-- examples/resources/shaders/shapes_base.vs | 9 ++++----- examples/resources/shaders/shapes_grayscale.fs | 4 ++-- examples/resources/shaders/swirl.fs | 2 +- 8 files changed, 16 insertions(+), 18 deletions(-) (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/base.vs b/examples/resources/shaders/base.vs index 59eae0a0..b0f930b7 100644 --- a/examples/resources/shaders/base.vs +++ b/examples/resources/shaders/base.vs @@ -6,8 +6,7 @@ in vec3 vertexNormal; out vec2 fragTexCoord; -uniform mat4 projectionMatrix; -uniform mat4 modelviewMatrix; +uniform mat4 mvpMatrix; // NOTE: Add here your custom variables @@ -15,5 +14,5 @@ void main() { fragTexCoord = vertexTexCoord; - gl_Position = projectionMatrix*modelviewMatrix*vec4(vertexPosition, 1.0); + gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); } \ No newline at end of file diff --git a/examples/resources/shaders/bloom.fs b/examples/resources/shaders/bloom.fs index f9cebe18..2833ce33 100644 --- a/examples/resources/shaders/bloom.fs +++ b/examples/resources/shaders/bloom.fs @@ -5,7 +5,7 @@ in vec2 fragTexCoord; out vec4 fragColor; uniform sampler2D texture0; -uniform vec4 tintColor; +uniform vec4 fragTintColor; // NOTE: Add here your custom variables diff --git a/examples/resources/shaders/grayscale.fs b/examples/resources/shaders/grayscale.fs index 38337e00..af50b8c1 100644 --- a/examples/resources/shaders/grayscale.fs +++ b/examples/resources/shaders/grayscale.fs @@ -5,16 +5,16 @@ in vec2 fragTexCoord; out vec4 fragColor; uniform sampler2D texture0; -uniform vec4 tintColor; +uniform vec4 fragTintColor; // NOTE: Add here your custom variables void main() { - vec4 base = texture2D(texture0, fragTexCoord)*tintColor; + vec4 base = texture2D(texture0, fragTexCoord)*fragTintColor; // Convert to grayscale using NTSC conversion weights float gray = dot(base.rgb, vec3(0.299, 0.587, 0.114)); - fragColor = vec4(gray, gray, gray, tintColor.a); + fragColor = vec4(gray, gray, gray, fragTintColor.a); } \ No newline at end of file diff --git a/examples/resources/shaders/phong.fs b/examples/resources/shaders/phong.fs index bb8826f4..75b7e6d7 100644 --- a/examples/resources/shaders/phong.fs +++ b/examples/resources/shaders/phong.fs @@ -6,7 +6,7 @@ in vec3 fragNormal; // Diffuse data uniform sampler2D texture0; -uniform vec4 tintColor; +uniform vec4 fragTintColor; // Light attributes uniform vec3 light_ambientColor = vec3(0.6, 0.3, 0); diff --git a/examples/resources/shaders/phong.vs b/examples/resources/shaders/phong.vs index 25163902..c6ef77de 100644 --- a/examples/resources/shaders/phong.vs +++ b/examples/resources/shaders/phong.vs @@ -6,8 +6,8 @@ in vec2 vertexTexCoord; in vec3 vertexNormal; // Projection and model data -uniform mat4 projectionMatrix; -uniform mat4 modelviewMatrix; +uniform mat4 mvpMatrix; + uniform mat4 modelMatrix; // Attributes to fragment shader diff --git a/examples/resources/shaders/shapes_base.vs b/examples/resources/shaders/shapes_base.vs index 78e543b7..1fd686be 100644 --- a/examples/resources/shaders/shapes_base.vs +++ b/examples/resources/shaders/shapes_base.vs @@ -4,16 +4,15 @@ attribute vec3 vertexPosition; attribute vec2 vertexTexCoord; attribute vec4 vertexColor; -uniform mat4 projectionMatrix; -uniform mat4 modelviewMatrix; +uniform mat4 mvpMatrix; varying vec2 fragTexCoord; -varying vec4 fragColor; +varying vec4 fragTintColor; void main() { fragTexCoord = vertexTexCoord; - fragColor = vertexColor; + fragTintColor = vertexColor; - gl_Position = projectionMatrix*modelviewMatrix*vec4(vertexPosition, 1.0); + gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); } \ No newline at end of file diff --git a/examples/resources/shaders/shapes_grayscale.fs b/examples/resources/shaders/shapes_grayscale.fs index 1b778871..23ba9153 100644 --- a/examples/resources/shaders/shapes_grayscale.fs +++ b/examples/resources/shaders/shapes_grayscale.fs @@ -2,11 +2,11 @@ uniform sampler2D texture0; varying vec2 fragTexCoord; -varying vec4 fragColor; +varying vec4 fragTintColor; void main() { - vec4 base = texture2D(texture0, fragTexCoord)*fragColor; + vec4 base = texture2D(texture0, fragTexCoord)*fragTintColor; // Convert to grayscale using NTSC conversion weights float gray = dot(base.rgb, vec3(0.299, 0.587, 0.114)); diff --git a/examples/resources/shaders/swirl.fs b/examples/resources/shaders/swirl.fs index ba26cc05..ace6e79d 100644 --- a/examples/resources/shaders/swirl.fs +++ b/examples/resources/shaders/swirl.fs @@ -5,7 +5,7 @@ in vec2 fragTexCoord; out vec4 fragColor; uniform sampler2D texture0; -uniform vec4 tintColor; +uniform vec4 fragTintColor; // NOTE: Add here your custom variables -- cgit v1.2.3 From 183795b8aa78fdf0b8064d72d77eaea8e7b6397b Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 16 Jan 2016 12:52:55 +0100 Subject: Review literals type --- examples/resources/shaders/phong.fs | 26 +++++++++++++------------- examples/resources/shaders/phong.vs | 5 ++--- examples/resources/shaders/swirl.fs | 6 +++--- 3 files changed, 18 insertions(+), 19 deletions(-) (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/phong.fs b/examples/resources/shaders/phong.fs index 75b7e6d7..f79413d9 100644 --- a/examples/resources/shaders/phong.fs +++ b/examples/resources/shaders/phong.fs @@ -9,16 +9,16 @@ uniform sampler2D texture0; uniform vec4 fragTintColor; // Light attributes -uniform vec3 light_ambientColor = vec3(0.6, 0.3, 0); -uniform vec3 light_diffuseColor = vec3(1, 0.5, 0); -uniform vec3 light_specularColor = vec3(0, 1, 0); -uniform float light_intensity = 1; -uniform float light_specIntensity = 1; +uniform vec3 light_ambientColor = vec3(0.6, 0.3, 0.0); +uniform vec3 light_diffuseColor = vec3(1.0, 0.5, 0.0); +uniform vec3 light_specularColor = vec3(0.0, 1.0, 0.0); +uniform float light_intensity = 1.0; +uniform float light_specIntensity = 1.0; // Material attributes -uniform vec3 mat_ambientColor = vec3(1, 1, 1); -uniform vec3 mat_specularColor = vec3(1, 1, 1); -uniform float mat_glossiness = 50; +uniform vec3 mat_ambientColor = vec3(1.0, 1.0, 1.0); +uniform vec3 mat_specularColor = vec3(1.0, 1.0, 1.0); +uniform float mat_glossiness = 50.0; // World attributes uniform vec3 lightPos; @@ -29,7 +29,7 @@ out vec4 fragColor; vec3 AmbientLighting() { - return mat_ambientColor * light_ambientColor; + return (mat_ambientColor*light_ambientColor); } vec3 DiffuseLighting(in vec3 N, in vec3 L) @@ -37,15 +37,15 @@ vec3 DiffuseLighting(in vec3 N, in vec3 L) // Lambertian reflection calculation float diffuse = clamp(dot(N, L), 0, 1); - return tintColor.xyz * light_diffuseColor * light_intensity * diffuse; + return (fragTintColor.xyz*light_diffuseColor*light_intensity*diffuse); } vec3 SpecularLighting(in vec3 N, in vec3 L, in vec3 V) { - float specular = 0; + float specular = 0.0; // Calculate specular reflection only if the surface is oriented to the light source - if(dot(N, L) > 0) + if (dot(N, L) > 0) { // Calculate half vector vec3 H = normalize(L + V); @@ -54,7 +54,7 @@ vec3 SpecularLighting(in vec3 N, in vec3 L, in vec3 V) specular = pow(dot(N, H), 3 + mat_glossiness); } - return mat_specularColor * light_specularColor * light_specIntensity * specular; + return (mat_specularColor*light_specularColor*light_specIntensity*specular); } void main() diff --git a/examples/resources/shaders/phong.vs b/examples/resources/shaders/phong.vs index c6ef77de..ee6d34bf 100644 --- a/examples/resources/shaders/phong.vs +++ b/examples/resources/shaders/phong.vs @@ -7,7 +7,6 @@ in vec3 vertexNormal; // Projection and model data uniform mat4 mvpMatrix; - uniform mat4 modelMatrix; // Attributes to fragment shader @@ -21,8 +20,8 @@ void main() // Calculate view vector normal from model mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); - fragNormal = normalize(normalMatrix * vertexNormal); + fragNormal = normalize(normalMatrix*vertexNormal); // Calculate final vertex position - gl_Position = projectionMatrix * modelviewMatrix * vec4(vertexPosition, 1.0); + gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); } \ No newline at end of file diff --git a/examples/resources/shaders/swirl.fs b/examples/resources/shaders/swirl.fs index ace6e79d..f89ef406 100644 --- a/examples/resources/shaders/swirl.fs +++ b/examples/resources/shaders/swirl.fs @@ -9,13 +9,13 @@ uniform vec4 fragTintColor; // NOTE: Add here your custom variables -const float renderWidth = 800; // HARDCODED for example! -const float renderHeight = 480; // Use uniforms instead... +const float renderWidth = 800.0; // HARDCODED for example! +const float renderHeight = 480.0; // Use uniforms instead... float radius = 250.0; float angle = 0.8; -uniform vec2 center = vec2(200, 200); +uniform vec2 center = vec2(200.0, 200.0); void main (void) { -- cgit v1.2.3 From d0ff78e7f41be9884e786026ddd22ed53fc0943f Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 25 Jan 2016 13:39:23 +0100 Subject: Move Light struct to example --- examples/resources/shaders/phong.vs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/phong.vs b/examples/resources/shaders/phong.vs index ee6d34bf..52cc2227 100644 --- a/examples/resources/shaders/phong.vs +++ b/examples/resources/shaders/phong.vs @@ -7,7 +7,9 @@ in vec3 vertexNormal; // Projection and model data uniform mat4 mvpMatrix; + uniform mat4 modelMatrix; +//uniform mat4 viewMatrix; // Not used // Attributes to fragment shader out vec2 fragTexCoord; -- cgit v1.2.3 From db5493b783fbdfd95813bc0c9ab253e3fd421e02 Mon Sep 17 00:00:00 2001 From: Constantine Tarasenkov Date: Fri, 29 Jan 2016 03:47:43 +0300 Subject: Fix shader versions --- examples/resources/shaders/shapes_base.vs | 2 +- examples/resources/shaders/shapes_grayscale.fs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/shapes_base.vs b/examples/resources/shaders/shapes_base.vs index 1fd686be..ad272dc1 100644 --- a/examples/resources/shaders/shapes_base.vs +++ b/examples/resources/shaders/shapes_base.vs @@ -1,4 +1,4 @@ -#version 110 +#version 330 attribute vec3 vertexPosition; attribute vec2 vertexTexCoord; diff --git a/examples/resources/shaders/shapes_grayscale.fs b/examples/resources/shaders/shapes_grayscale.fs index 23ba9153..0698e1bf 100644 --- a/examples/resources/shaders/shapes_grayscale.fs +++ b/examples/resources/shaders/shapes_grayscale.fs @@ -1,4 +1,4 @@ -#version 110 +#version 330 uniform sampler2D texture0; varying vec2 fragTexCoord; -- cgit v1.2.3