From 66b096d97848eb096a043781f1f305e7189f2a00 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 30 Mar 2016 20:09:16 +0200 Subject: Added support for render to texture (use RenderTexture2D) Now it's possible to render to texture, old postprocessing system will be removed on next raylib version. --- examples/resources/shaders/base.vs | 3 +++ examples/resources/shaders/bloom.fs | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/base.vs b/examples/resources/shaders/base.vs index b0f930b7..9b8cca69 100644 --- a/examples/resources/shaders/base.vs +++ b/examples/resources/shaders/base.vs @@ -3,8 +3,10 @@ in vec3 vertexPosition; in vec2 vertexTexCoord; in vec3 vertexNormal; +in vec4 vertexColor; out vec2 fragTexCoord; +out vec4 fragTintColor; uniform mat4 mvpMatrix; @@ -13,6 +15,7 @@ uniform mat4 mvpMatrix; void main() { fragTexCoord = vertexTexCoord; + fragTintColor = vertexColor; 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 2833ce33..0a73161a 100644 --- a/examples/resources/shaders/bloom.fs +++ b/examples/resources/shaders/bloom.fs @@ -1,11 +1,12 @@ #version 330 in vec2 fragTexCoord; +in vec4 fragTintColor; out vec4 fragColor; uniform sampler2D texture0; -uniform vec4 fragTintColor; +//uniform vec4 fragTintColor; // NOTE: Add here your custom variables -- cgit v1.2.3 From 1d545449bb19148b45d2d92f213e1001a8eb527c Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 7 Apr 2016 12:32:32 +0200 Subject: Reviewed shaders and added comments --- examples/resources/shaders/base.vs | 21 ------- examples/resources/shaders/bloom.fs | 43 -------------- examples/resources/shaders/glsl100/base.vs | 26 +++++++++ examples/resources/shaders/glsl100/bloom.fs | 37 ++++++++++++ examples/resources/shaders/glsl100/grayscale.fs | 25 ++++++++ examples/resources/shaders/glsl100/swirl.fs | 45 +++++++++++++++ examples/resources/shaders/glsl330/base.vs | 26 +++++++++ examples/resources/shaders/glsl330/bloom.fs | 38 +++++++++++++ examples/resources/shaders/glsl330/grayscale.fs | 26 +++++++++ examples/resources/shaders/glsl330/phong.fs | 76 +++++++++++++++++++++++++ examples/resources/shaders/glsl330/phong.vs | 29 ++++++++++ examples/resources/shaders/glsl330/swirl.fs | 46 +++++++++++++++ examples/resources/shaders/grayscale.fs | 20 ------- examples/resources/shaders/phong.fs | 76 ------------------------- examples/resources/shaders/phong.vs | 29 ---------- examples/resources/shaders/shapes_base.vs | 18 ------ examples/resources/shaders/shapes_grayscale.fs | 15 ----- examples/resources/shaders/swirl.fs | 41 ------------- 18 files changed, 374 insertions(+), 263 deletions(-) delete mode 100644 examples/resources/shaders/base.vs delete mode 100644 examples/resources/shaders/bloom.fs create mode 100644 examples/resources/shaders/glsl100/base.vs create mode 100644 examples/resources/shaders/glsl100/bloom.fs create mode 100644 examples/resources/shaders/glsl100/grayscale.fs create mode 100644 examples/resources/shaders/glsl100/swirl.fs create mode 100644 examples/resources/shaders/glsl330/base.vs create mode 100644 examples/resources/shaders/glsl330/bloom.fs create mode 100644 examples/resources/shaders/glsl330/grayscale.fs create mode 100644 examples/resources/shaders/glsl330/phong.fs create mode 100644 examples/resources/shaders/glsl330/phong.vs create mode 100644 examples/resources/shaders/glsl330/swirl.fs delete mode 100644 examples/resources/shaders/grayscale.fs delete mode 100644 examples/resources/shaders/phong.fs delete mode 100644 examples/resources/shaders/phong.vs delete mode 100644 examples/resources/shaders/shapes_base.vs delete mode 100644 examples/resources/shaders/shapes_grayscale.fs delete mode 100644 examples/resources/shaders/swirl.fs (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/base.vs b/examples/resources/shaders/base.vs deleted file mode 100644 index 9b8cca69..00000000 --- a/examples/resources/shaders/base.vs +++ /dev/null @@ -1,21 +0,0 @@ -#version 330 - -in vec3 vertexPosition; -in vec2 vertexTexCoord; -in vec3 vertexNormal; -in vec4 vertexColor; - -out vec2 fragTexCoord; -out vec4 fragTintColor; - -uniform mat4 mvpMatrix; - -// NOTE: Add here your custom variables - -void main() -{ - fragTexCoord = vertexTexCoord; - fragTintColor = vertexColor; - - 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 deleted file mode 100644 index 0a73161a..00000000 --- a/examples/resources/shaders/bloom.fs +++ /dev/null @@ -1,43 +0,0 @@ -#version 330 - -in vec2 fragTexCoord; -in vec4 fragTintColor; - -out vec4 fragColor; - -uniform sampler2D texture0; -//uniform vec4 fragTintColor; - -// NOTE: Add here your custom variables - -void main() -{ - vec4 sum = vec4(0); - vec4 tc = vec4(0); - - for (int i = -4; i < 4; i++) - { - for (int j = -3; j < 3; j++) - { - sum += texture2D(texture0, fragTexCoord + vec2(j, i)*0.004) * 0.25; - } - } - - if (texture2D(texture0, fragTexCoord).r < 0.3) - { - tc = sum*sum*0.012 + texture2D(texture0, fragTexCoord); - } - else - { - if (texture2D(texture0, fragTexCoord).r < 0.5) - { - tc = sum*sum*0.009 + texture2D(texture0, fragTexCoord); - } - else - { - tc = sum*sum*0.0075 + texture2D(texture0, fragTexCoord); - } - } - - fragColor = tc; -} \ No newline at end of file diff --git a/examples/resources/shaders/glsl100/base.vs b/examples/resources/shaders/glsl100/base.vs new file mode 100644 index 00000000..e9386939 --- /dev/null +++ b/examples/resources/shaders/glsl100/base.vs @@ -0,0 +1,26 @@ +#version 100 + +// Input vertex attributes +attribute vec3 vertexPosition; +attribute vec2 vertexTexCoord; +attribute vec3 vertexNormal; +attribute vec4 vertexColor; + +// Input uniform values +uniform mat4 mvpMatrix; + +// Output vertex attributes (to fragment shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// NOTE: Add here your custom variables + +void main() +{ + // Send vertex attributes to fragment shader + fragTexCoord = vertexTexCoord; + fragColor = vertexColor; + + // Calculate final vertex position + gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl100/bloom.fs b/examples/resources/shaders/glsl100/bloom.fs new file mode 100644 index 00000000..5a08843d --- /dev/null +++ b/examples/resources/shaders/glsl100/bloom.fs @@ -0,0 +1,37 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// NOTE: Add here your custom variables + +void main() +{ + vec4 sum = vec4(0); + vec4 tc = vec4(0); + + for (int i = -4; i < 4; i++) + { + for (int j = -3; j < 3; j++) + { + sum += texture2D(texture0, fragTexCoord + vec2(j, i)*0.004) * 0.25; + } + } + + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord); + + // Calculate final fragment color + if (texelColor.r < 0.3) tc = sum*sum*0.012 + texelColor; + else if (texelColor.r < 0.5) tc = sum*sum*0.009 + texelColor; + else tc = sum*sum*0.0075 + texelColor; + + finalColor = tc; +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl100/grayscale.fs b/examples/resources/shaders/glsl100/grayscale.fs new file mode 100644 index 00000000..f92ec335 --- /dev/null +++ b/examples/resources/shaders/glsl100/grayscale.fs @@ -0,0 +1,25 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// NOTE: Add here your custom variables + +void main() +{ + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord)*fragTintColor*fragColor; + + // Convert texel color to grayscale using NTSC conversion weights + float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114)); + + // Calculate final fragment color + gl_FragColor = vec4(gray, gray, gray, texelColor.a); +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl100/swirl.fs b/examples/resources/shaders/glsl100/swirl.fs new file mode 100644 index 00000000..e77d4f87 --- /dev/null +++ b/examples/resources/shaders/glsl100/swirl.fs @@ -0,0 +1,45 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// NOTE: Add here your custom variables + +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.0, 200.0); + +void main (void) +{ + vec2 texSize = vec2(renderWidth, renderHeight); + vec2 tc = fragTexCoord*texSize; + tc -= center; + + float dist = length(tc); + + if (dist < radius) + { + float percent = (radius - dist)/radius; + float theta = percent*percent*angle*8.0; + float s = sin(theta); + float c = cos(theta); + + tc = vec2(dot(tc, vec2(c, -s)), dot(tc, vec2(s, c))); + } + + tc += center; + vec3 color = texture2D(texture0, tc/texSize).rgb; + + gl_FragColor = vec4(color, 1.0);; +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl330/base.vs b/examples/resources/shaders/glsl330/base.vs new file mode 100644 index 00000000..638cb8ae --- /dev/null +++ b/examples/resources/shaders/glsl330/base.vs @@ -0,0 +1,26 @@ +#version 330 + +// Input vertex attributes +in vec3 vertexPosition; +in vec2 vertexTexCoord; +in vec3 vertexNormal; +in vec4 vertexColor; + +// Input uniform values +uniform mat4 mvpMatrix; + +// Output vertex attributes (to fragment shader) +out vec2 fragTexCoord; +out vec4 fragColor; + +// NOTE: Add here your custom variables + +void main() +{ + // Send vertex attributes to fragment shader + fragTexCoord = vertexTexCoord; + fragColor = vertexColor; + + // Calculate final vertex position + gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl330/bloom.fs b/examples/resources/shaders/glsl330/bloom.fs new file mode 100644 index 00000000..c8cb0d32 --- /dev/null +++ b/examples/resources/shaders/glsl330/bloom.fs @@ -0,0 +1,38 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// Output fragment color +out vec4 finalColor; + +// NOTE: Add here your custom variables + +void main() +{ + vec4 sum = vec4(0); + vec4 tc = vec4(0); + + for (int i = -4; i < 4; i++) + { + for (int j = -3; j < 3; j++) + { + sum += texture2D(texture0, fragTexCoord + vec2(j, i)*0.004)*0.25; + } + } + + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord); + + // Calculate final fragment color + if (texelColor.r < 0.3) tc = sum*sum*0.012 + texelColor; + else if (texelColor.r < 0.5) tc = sum*sum*0.009 + texelColor; + else tc = sum*sum*0.0075 + texelColor; + + finalColor = tc; +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl330/grayscale.fs b/examples/resources/shaders/glsl330/grayscale.fs new file mode 100644 index 00000000..d4a8824f --- /dev/null +++ b/examples/resources/shaders/glsl330/grayscale.fs @@ -0,0 +1,26 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// Output fragment color +out vec4 finalColor; + +// NOTE: Add here your custom variables + +void main() +{ + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord)*fragTintColor*fragColor; + + // Convert texel color to grayscale using NTSC conversion weights + float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114)); + + // Calculate final fragment color + finalColor = vec4(gray, gray, gray, texelColor.a); +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl330/phong.fs b/examples/resources/shaders/glsl330/phong.fs new file mode 100644 index 00000000..2e7a95f6 --- /dev/null +++ b/examples/resources/shaders/glsl330/phong.fs @@ -0,0 +1,76 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec3 fragNormal; + +// Diffuse data +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// Light attributes +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.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; +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 (fragTintColor.xyz*light_diffuseColor*light_intensity*diffuse); +} + +vec3 SpecularLighting(in vec3 N, in vec3 L, in vec3 V) +{ + float specular = 0.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/glsl330/phong.vs b/examples/resources/shaders/glsl330/phong.vs new file mode 100644 index 00000000..d68d9b3f --- /dev/null +++ b/examples/resources/shaders/glsl330/phong.vs @@ -0,0 +1,29 @@ +#version 330 + +// Input vertex attributes +in vec3 vertexPosition; +in vec2 vertexTexCoord; +in vec3 vertexNormal; + +// Input uniform values +uniform mat4 mvpMatrix; + +// Output vertex attributes (to fragment shader) +out vec2 fragTexCoord; +out vec3 fragNormal; + +// NOTE: Add here your custom variables +uniform mat4 modelMatrix; + +void main() +{ + // Send vertex attributes 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 = mvpMatrix*vec4(vertexPosition, 1.0); +} \ No newline at end of file diff --git a/examples/resources/shaders/glsl330/swirl.fs b/examples/resources/shaders/glsl330/swirl.fs new file mode 100644 index 00000000..b1dc82f0 --- /dev/null +++ b/examples/resources/shaders/glsl330/swirl.fs @@ -0,0 +1,46 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 fragTintColor; + +// Output fragment color +out vec4 finalColor; + +// NOTE: Add here your custom variables + +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.0, 200.0); + +void main (void) +{ + vec2 texSize = vec2(renderWidth, renderHeight); + vec2 tc = fragTexCoord*texSize; + tc -= center; + + float dist = length(tc); + + if (dist < radius) + { + float percent = (radius - dist)/radius; + float theta = percent*percent*angle*8.0; + float s = sin(theta); + float c = cos(theta); + + tc = vec2(dot(tc, vec2(c, -s)), dot(tc, vec2(s, c))); + } + + tc += center; + vec3 color = texture2D(texture0, tc/texSize).rgb; + + finalColor = vec4(color, 1.0);; +} \ No newline at end of file diff --git a/examples/resources/shaders/grayscale.fs b/examples/resources/shaders/grayscale.fs deleted file mode 100644 index af50b8c1..00000000 --- a/examples/resources/shaders/grayscale.fs +++ /dev/null @@ -1,20 +0,0 @@ -#version 330 - -in vec2 fragTexCoord; - -out vec4 fragColor; - -uniform sampler2D texture0; -uniform vec4 fragTintColor; - -// NOTE: Add here your custom variables - -void main() -{ - 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, fragTintColor.a); -} \ No newline at end of file diff --git a/examples/resources/shaders/phong.fs b/examples/resources/shaders/phong.fs deleted file mode 100644 index f79413d9..00000000 --- a/examples/resources/shaders/phong.fs +++ /dev/null @@ -1,76 +0,0 @@ -#version 330 - -// Vertex shader input data -in vec2 fragTexCoord; -in vec3 fragNormal; - -// Diffuse data -uniform sampler2D texture0; -uniform vec4 fragTintColor; - -// Light attributes -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.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; -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 (fragTintColor.xyz*light_diffuseColor*light_intensity*diffuse); -} - -vec3 SpecularLighting(in vec3 N, in vec3 L, in vec3 V) -{ - float specular = 0.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 deleted file mode 100644 index 52cc2227..00000000 --- a/examples/resources/shaders/phong.vs +++ /dev/null @@ -1,29 +0,0 @@ -#version 330 - -// Vertex input data -in vec3 vertexPosition; -in vec2 vertexTexCoord; -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; -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 = mvpMatrix*vec4(vertexPosition, 1.0); -} \ No newline at end of file diff --git a/examples/resources/shaders/shapes_base.vs b/examples/resources/shaders/shapes_base.vs deleted file mode 100644 index ad272dc1..00000000 --- a/examples/resources/shaders/shapes_base.vs +++ /dev/null @@ -1,18 +0,0 @@ -#version 330 - -attribute vec3 vertexPosition; -attribute vec2 vertexTexCoord; -attribute vec4 vertexColor; - -uniform mat4 mvpMatrix; - -varying vec2 fragTexCoord; -varying vec4 fragTintColor; - -void main() -{ - fragTexCoord = vertexTexCoord; - fragTintColor = vertexColor; - - 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 deleted file mode 100644 index 0698e1bf..00000000 --- a/examples/resources/shaders/shapes_grayscale.fs +++ /dev/null @@ -1,15 +0,0 @@ -#version 330 - -uniform sampler2D texture0; -varying vec2 fragTexCoord; -varying vec4 fragTintColor; - -void main() -{ - 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)); - - gl_FragColor = vec4(gray, gray, gray, base.a); -} \ No newline at end of file diff --git a/examples/resources/shaders/swirl.fs b/examples/resources/shaders/swirl.fs deleted file mode 100644 index f89ef406..00000000 --- a/examples/resources/shaders/swirl.fs +++ /dev/null @@ -1,41 +0,0 @@ -#version 330 - -in vec2 fragTexCoord; - -out vec4 fragColor; - -uniform sampler2D texture0; -uniform vec4 fragTintColor; - -// NOTE: Add here your custom variables - -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.0, 200.0); - -void main (void) -{ - vec2 texSize = vec2(renderWidth, renderHeight); - vec2 tc = fragTexCoord*texSize; - tc -= center; - float dist = length(tc); - - if (dist < radius) - { - float percent = (radius - dist)/radius; - float theta = percent*percent*angle*8.0; - float s = sin(theta); - float c = cos(theta); - - tc = vec2(dot(tc, vec2(c, -s)), dot(tc, vec2(s, c))); - } - - tc += center; - vec3 color = texture2D(texture0, tc/texSize).rgb; - - fragColor = vec4(color, 1.0);; -} \ No newline at end of file -- cgit v1.2.3 From 4b51248372302bd9f1baf2452b389f57f0173d59 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 7 Apr 2016 12:43:45 +0200 Subject: Review shader and add comments --- examples/resources/shaders/glsl330/phong.fs | 84 +++++++++++++++-------------- 1 file changed, 45 insertions(+), 39 deletions(-) (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/glsl330/phong.fs b/examples/resources/shaders/glsl330/phong.fs index 2e7a95f6..80e3d673 100644 --- a/examples/resources/shaders/glsl330/phong.fs +++ b/examples/resources/shaders/glsl330/phong.fs @@ -4,73 +4,79 @@ in vec2 fragTexCoord; in vec3 fragNormal; -// Diffuse data +// Input uniform values uniform sampler2D texture0; uniform vec4 fragTintColor; -// Light attributes -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; +// Output fragment color +out vec4 finalColor; -// Material attributes -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; +// NOTE: Add here your custom variables -// World attributes -uniform vec3 lightPos; -uniform vec3 cameraPos; +// Light uniform values +uniform vec3 lightAmbientColor = vec3(0.6, 0.3, 0.0); +uniform vec3 lightDiffuseColor = vec3(1.0, 0.5, 0.0); +uniform vec3 lightSpecularColor = vec3(0.0, 1.0, 0.0); +uniform float lightIntensity = 1.0; +uniform float lightSpecIntensity = 1.0; -// Fragment shader output data -out vec4 fragColor; +// Material uniform values +uniform vec3 matAmbientColor = vec3(1.0, 1.0, 1.0); +uniform vec3 matSpecularColor = vec3(1.0, 1.0, 1.0); +uniform float matGlossiness = 50.0; +// World uniform values +uniform vec3 lightPosition; +uniform vec3 cameraPosition; + +// Calculate ambient lighting component vec3 AmbientLighting() { - return (mat_ambientColor*light_ambientColor); + return (matAmbientColor*lightAmbientColor); } +// Calculate diffuse lighting component vec3 DiffuseLighting(in vec3 N, in vec3 L) { - // Lambertian reflection calculation - float diffuse = clamp(dot(N, L), 0, 1); - - return (fragTintColor.xyz*light_diffuseColor*light_intensity*diffuse); + // Lambertian reflection calculation + float diffuse = clamp(dot(N, L), 0, 1); + + return (fragTintColor.xyz*lightDiffuseColor*lightIntensity*diffuse); } +// Calculate specular lighting component vec3 SpecularLighting(in vec3 N, in vec3 L, in vec3 V) { - float specular = 0.0; + float specular = 0.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); - } + // 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 + matGlossiness); + } - return (mat_specularColor*light_specularColor*light_specIntensity*specular); + return (matSpecularColor*lightSpecularColor*lightSpecIntensity*specular); } void main() { // Normalize input vectors - vec3 L = normalize(lightPos); - vec3 V = normalize(cameraPos); + vec3 L = normalize(lightPosition); + vec3 V = normalize(cameraPosition); vec3 N = normalize(fragNormal); + // Calculate lighting components 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); + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord); + + // Calculate final fragment color + finalColor = vec4(texelColor.rgb*(ambient + diffuse + specular), texelColor.a); } \ No newline at end of file -- cgit v1.2.3 From cde2c1aa6db6577fa415d60280aa53a86b3cc97a Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 8 Apr 2016 00:21:21 +0200 Subject: Added depth drawing shader NOTE: It requires a depth texture as input, it should be configured on rlgl, by default RenderTexture (fbo) uses Depth Renderbuffer instead of Depth Texture. Check rlglLoadRenderTexture() --- examples/resources/shaders/glsl330/depth.fs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 examples/resources/shaders/glsl330/depth.fs (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/glsl330/depth.fs b/examples/resources/shaders/glsl330/depth.fs new file mode 100644 index 00000000..06d399f9 --- /dev/null +++ b/examples/resources/shaders/glsl330/depth.fs @@ -0,0 +1,27 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; // Depth texture +uniform vec4 fragTintColor; + +// Output fragment color +out vec4 finalColor; + +// NOTE: Add here your custom variables + +void main() +{ + float zNear = 0.01; // camera z near + float zFar = 10.0; // camera z far + float z = texture(texture0, fragTexCoord).x; + + // Linearize depth value + float depth = (2.0*zNear)/(zFar + zNear - z*(zFar - zNear)); + + // Calculate final fragment color + finalColor = vec4(depth, depth, depth, 1.0f); +} \ No newline at end of file -- cgit v1.2.3 From 0e29aa2951fe11e9d0fabec2182ed6309fce37bb Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 17 May 2016 00:39:56 +0200 Subject: Corrected function name texture2D() is deprecated on GLSL 330 --- examples/resources/shaders/glsl330/bloom.fs | 2 +- examples/resources/shaders/glsl330/swirl.fs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/glsl330/bloom.fs b/examples/resources/shaders/glsl330/bloom.fs index c8cb0d32..47ddee30 100644 --- a/examples/resources/shaders/glsl330/bloom.fs +++ b/examples/resources/shaders/glsl330/bloom.fs @@ -22,7 +22,7 @@ void main() { for (int j = -3; j < 3; j++) { - sum += texture2D(texture0, fragTexCoord + vec2(j, i)*0.004)*0.25; + sum += texture(texture0, fragTexCoord + vec2(j, i)*0.004)*0.25; } } diff --git a/examples/resources/shaders/glsl330/swirl.fs b/examples/resources/shaders/glsl330/swirl.fs index b1dc82f0..da098754 100644 --- a/examples/resources/shaders/glsl330/swirl.fs +++ b/examples/resources/shaders/glsl330/swirl.fs @@ -40,7 +40,7 @@ void main (void) } tc += center; - vec3 color = texture2D(texture0, tc/texSize).rgb; + vec3 color = texture(texture0, tc/texSize).rgb; finalColor = vec4(color, 1.0);; } \ No newline at end of file -- cgit v1.2.3 From bc08271da3e68d2880f4ef712c13e88b99f1021d Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 18 May 2016 12:04:27 +0200 Subject: Updated shaders with comments --- examples/resources/shaders/glsl100/bloom.fs | 2 +- examples/resources/shaders/glsl100/swirl.fs | 2 +- examples/resources/shaders/glsl330/bloom.fs | 2 +- examples/resources/shaders/glsl330/phong.fs | 3 +++ examples/resources/shaders/glsl330/swirl.fs | 2 +- 5 files changed, 7 insertions(+), 4 deletions(-) (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/glsl100/bloom.fs b/examples/resources/shaders/glsl100/bloom.fs index 5a08843d..280d2fb6 100644 --- a/examples/resources/shaders/glsl100/bloom.fs +++ b/examples/resources/shaders/glsl100/bloom.fs @@ -33,5 +33,5 @@ void main() else if (texelColor.r < 0.5) tc = sum*sum*0.009 + texelColor; else tc = sum*sum*0.0075 + texelColor; - finalColor = tc; + gl_FragColor = tc; } \ No newline at end of file diff --git a/examples/resources/shaders/glsl100/swirl.fs b/examples/resources/shaders/glsl100/swirl.fs index e77d4f87..0d6d24f2 100644 --- a/examples/resources/shaders/glsl100/swirl.fs +++ b/examples/resources/shaders/glsl100/swirl.fs @@ -20,7 +20,7 @@ float angle = 0.8; uniform vec2 center = vec2(200.0, 200.0); -void main (void) +void main() { vec2 texSize = vec2(renderWidth, renderHeight); vec2 tc = fragTexCoord*texSize; diff --git a/examples/resources/shaders/glsl330/bloom.fs b/examples/resources/shaders/glsl330/bloom.fs index 47ddee30..0307bc06 100644 --- a/examples/resources/shaders/glsl330/bloom.fs +++ b/examples/resources/shaders/glsl330/bloom.fs @@ -17,7 +17,7 @@ void main() { vec4 sum = vec4(0); vec4 tc = vec4(0); - + for (int i = -4; i < 4; i++) { for (int j = -3; j < 3; j++) diff --git a/examples/resources/shaders/glsl330/phong.fs b/examples/resources/shaders/glsl330/phong.fs index 80e3d673..c14b346a 100644 --- a/examples/resources/shaders/glsl330/phong.fs +++ b/examples/resources/shaders/glsl330/phong.fs @@ -29,6 +29,9 @@ uniform float matGlossiness = 50.0; uniform vec3 lightPosition; uniform vec3 cameraPosition; +// Fragment shader output data +out vec4 fragColor; + // Calculate ambient lighting component vec3 AmbientLighting() { diff --git a/examples/resources/shaders/glsl330/swirl.fs b/examples/resources/shaders/glsl330/swirl.fs index da098754..80c16cc9 100644 --- a/examples/resources/shaders/glsl330/swirl.fs +++ b/examples/resources/shaders/glsl330/swirl.fs @@ -21,7 +21,7 @@ float angle = 0.8; uniform vec2 center = vec2(200.0, 200.0); -void main (void) +void main() { vec2 texSize = vec2(renderWidth, renderHeight); vec2 tc = fragTexCoord*texSize; -- cgit v1.2.3 From 80eb4f3f50bb9773dd0e5d4b70c50e18df8996e5 Mon Sep 17 00:00:00 2001 From: victorfisac Date: Sat, 21 May 2016 18:11:25 +0200 Subject: Remove deprecated phong lighting shaders and example --- examples/resources/shaders/glsl330/phong.fs | 85 ----------------------------- examples/resources/shaders/glsl330/phong.vs | 29 ---------- 2 files changed, 114 deletions(-) delete mode 100644 examples/resources/shaders/glsl330/phong.fs delete mode 100644 examples/resources/shaders/glsl330/phong.vs (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/glsl330/phong.fs b/examples/resources/shaders/glsl330/phong.fs deleted file mode 100644 index c14b346a..00000000 --- a/examples/resources/shaders/glsl330/phong.fs +++ /dev/null @@ -1,85 +0,0 @@ -#version 330 - -// Input vertex attributes (from vertex shader) -in vec2 fragTexCoord; -in vec3 fragNormal; - -// Input uniform values -uniform sampler2D texture0; -uniform vec4 fragTintColor; - -// Output fragment color -out vec4 finalColor; - -// NOTE: Add here your custom variables - -// Light uniform values -uniform vec3 lightAmbientColor = vec3(0.6, 0.3, 0.0); -uniform vec3 lightDiffuseColor = vec3(1.0, 0.5, 0.0); -uniform vec3 lightSpecularColor = vec3(0.0, 1.0, 0.0); -uniform float lightIntensity = 1.0; -uniform float lightSpecIntensity = 1.0; - -// Material uniform values -uniform vec3 matAmbientColor = vec3(1.0, 1.0, 1.0); -uniform vec3 matSpecularColor = vec3(1.0, 1.0, 1.0); -uniform float matGlossiness = 50.0; - -// World uniform values -uniform vec3 lightPosition; -uniform vec3 cameraPosition; - -// Fragment shader output data -out vec4 fragColor; - -// Calculate ambient lighting component -vec3 AmbientLighting() -{ - return (matAmbientColor*lightAmbientColor); -} - -// Calculate diffuse lighting component -vec3 DiffuseLighting(in vec3 N, in vec3 L) -{ - // Lambertian reflection calculation - float diffuse = clamp(dot(N, L), 0, 1); - - return (fragTintColor.xyz*lightDiffuseColor*lightIntensity*diffuse); -} - -// Calculate specular lighting component -vec3 SpecularLighting(in vec3 N, in vec3 L, in vec3 V) -{ - float specular = 0.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 + matGlossiness); - } - - return (matSpecularColor*lightSpecularColor*lightSpecIntensity*specular); -} - -void main() -{ - // Normalize input vectors - vec3 L = normalize(lightPosition); - vec3 V = normalize(cameraPosition); - vec3 N = normalize(fragNormal); - - // Calculate lighting components - vec3 ambient = AmbientLighting(); - vec3 diffuse = DiffuseLighting(N, L); - vec3 specular = SpecularLighting(N, L, V); - - // Texel color fetching from texture sampler - vec4 texelColor = texture(texture0, fragTexCoord); - - // Calculate final fragment color - finalColor = vec4(texelColor.rgb*(ambient + diffuse + specular), texelColor.a); -} \ No newline at end of file diff --git a/examples/resources/shaders/glsl330/phong.vs b/examples/resources/shaders/glsl330/phong.vs deleted file mode 100644 index d68d9b3f..00000000 --- a/examples/resources/shaders/glsl330/phong.vs +++ /dev/null @@ -1,29 +0,0 @@ -#version 330 - -// Input vertex attributes -in vec3 vertexPosition; -in vec2 vertexTexCoord; -in vec3 vertexNormal; - -// Input uniform values -uniform mat4 mvpMatrix; - -// Output vertex attributes (to fragment shader) -out vec2 fragTexCoord; -out vec3 fragNormal; - -// NOTE: Add here your custom variables -uniform mat4 modelMatrix; - -void main() -{ - // Send vertex attributes 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 = mvpMatrix*vec4(vertexPosition, 1.0); -} \ No newline at end of file -- cgit v1.2.3 From c320a21f2b0e96f7605624e84048ccab9700b516 Mon Sep 17 00:00:00 2001 From: victorfisac Date: Sat, 21 May 2016 18:16:39 +0200 Subject: Add standard lighting (2/3) - 3 light types added (point, directional, spot). - DrawLights() function added using line shapes. - Standard lighting example added. - Removed useless struct variables from material and light. - Fixed light attributes dynamic locations errors. - Standard vertex and fragment shaders temporally added until rewrite it as char pointers in rlgl. TODO: - Add normal and specular maps calculations in standard shader. - Add control structs to handle which attributes needs to be calculated (textures, specular...). - Adapt standard shader to version 110. - Rewrite standard shader as char pointers in rlgl. --- examples/resources/shaders/standard.fs | 136 +++++++++++++++++++++++++++++++++ examples/resources/shaders/standard.vs | 23 ++++++ 2 files changed, 159 insertions(+) create mode 100644 examples/resources/shaders/standard.fs create mode 100644 examples/resources/shaders/standard.vs (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/standard.fs b/examples/resources/shaders/standard.fs new file mode 100644 index 00000000..30c841d2 --- /dev/null +++ b/examples/resources/shaders/standard.fs @@ -0,0 +1,136 @@ +#version 330 + +in vec3 fragPosition; +in vec2 fragTexCoord; +in vec4 fragColor; +in vec3 fragNormal; + +out vec4 finalColor; + +uniform sampler2D texture0; + +uniform vec4 colAmbient; +uniform vec4 colDiffuse; +uniform vec4 colSpecular; +uniform float glossiness; + +uniform mat4 modelMatrix; +uniform vec3 viewDir; + +struct Light { + int enabled; + int type; + vec3 position; + vec3 direction; + vec4 diffuse; + float intensity; + float attenuation; + float coneAngle; +}; + +const int maxLights = 8; +uniform int lightsCount; +uniform Light lights[maxLights]; + +vec3 CalcPointLight(Light l, vec3 n, vec3 v) +{ + vec3 surfacePos = vec3(modelMatrix*vec4(fragPosition, 1)); + vec3 surfaceToLight = l.position - surfacePos; + + // Diffuse shading + float brightness = clamp(dot(n, surfaceToLight)/(length(surfaceToLight)*length(n)), 0, 1); + float diff = 1.0/dot(surfaceToLight/l.attenuation, surfaceToLight/l.attenuation)*brightness*l.intensity; + + // Specular shading + float spec = 0.0; + if(diff > 0.0) + { + vec3 h = normalize(-l.direction + v); + spec = pow(dot(n, h), 3 + glossiness); + } + + return (diff*l.diffuse.rgb*colDiffuse.rgb + spec*colSpecular.rgb); +} + +vec3 CalcDirectionalLight(Light l, vec3 n, vec3 v) +{ + vec3 lightDir = normalize(-l.direction); + + // Diffuse shading + float diff = clamp(dot(n, lightDir), 0.0, 1.0)*l.intensity; + + // Specular shading + float spec = 0.0; + if(diff > 0.0) + { + vec3 h = normalize(lightDir + v); + spec = pow(dot(n, h), 3 + glossiness); + } + + // Combine results + return (diff*l.intensity*l.diffuse.rgb*colDiffuse.rgb + spec*colSpecular.rgb); +} + +vec3 CalcSpotLight(Light l, vec3 n, vec3 v) +{ + vec3 surfacePos = vec3(modelMatrix*vec4(fragPosition, 1)); + vec3 lightToSurface = normalize(surfacePos - l.position); + vec3 lightDir = normalize(-l.direction); + + // Diffuse shading + float diff = clamp(dot(n, lightDir), 0.0, 1.0)*l.intensity; + + // Spot attenuation + float attenuation = clamp(dot(n, lightToSurface), 0.0, 1.0); + attenuation = dot(lightToSurface, -lightDir); + float lightToSurfaceAngle = degrees(acos(attenuation)); + if(lightToSurfaceAngle > l.coneAngle) attenuation = 0.0; + float falloff = (l.coneAngle - lightToSurfaceAngle)/l.coneAngle; + + // Combine diffuse and attenuation + float diffAttenuation = diff*attenuation; + + // Specular shading + float spec = 0.0; + if(diffAttenuation > 0.0) + { + vec3 h = normalize(lightDir + v); + spec = pow(dot(n, h), 3 + glossiness); + } + + return falloff*(diffAttenuation*l.diffuse.rgb + spec*colSpecular.rgb); +} + +void main() +{ + // Calculate fragment normal in screen space + mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); + vec3 normal = normalize(normalMatrix*fragNormal); + + // Normalize normal and view direction vectors + vec3 n = normalize(normal); + vec3 v = normalize(viewDir); + + // Calculate diffuse texture color fetching + vec4 texelColor = texture(texture0, fragTexCoord); + vec3 lighting = colAmbient.rgb; + + for(int i = 0; i < lightsCount; i++) + { + // Check if light is enabled + if(lights[i].enabled == 1) + { + // Calculate lighting based on light type + switch(lights[i].type) + { + case 0: lighting += CalcPointLight(lights[i], n, v); break; + case 1: lighting += CalcDirectionalLight(lights[i], n, v); break; + case 2: lighting += CalcSpotLight(lights[i], n, v); break; + default: break; + } + } + } + + // Calculate final fragment color + finalColor = vec4(texelColor.rgb*lighting, texelColor.a); +} diff --git a/examples/resources/shaders/standard.vs b/examples/resources/shaders/standard.vs new file mode 100644 index 00000000..fc0a5ff4 --- /dev/null +++ b/examples/resources/shaders/standard.vs @@ -0,0 +1,23 @@ +#version 330 + +in vec3 vertexPosition; +in vec3 vertexNormal; +in vec2 vertexTexCoord; +in vec4 vertexColor; + +out vec3 fragPosition; +out vec2 fragTexCoord; +out vec4 fragColor; +out vec3 fragNormal; + +uniform mat4 mvpMatrix; + +void main() +{ + fragPosition = vertexPosition; + fragTexCoord = vertexTexCoord; + fragColor = vertexColor; + fragNormal = vertexNormal; + + gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); +} \ No newline at end of file -- cgit v1.2.3 From dcd6942ed1ab703625f5c7072cbcfd823c681db7 Mon Sep 17 00:00:00 2001 From: victorfisac Date: Sat, 21 May 2016 18:22:15 +0200 Subject: Fix small bug and spacing --- examples/resources/shaders/standard.fs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/standard.fs b/examples/resources/shaders/standard.fs index 30c841d2..3c3bef4b 100644 --- a/examples/resources/shaders/standard.fs +++ b/examples/resources/shaders/standard.fs @@ -43,7 +43,7 @@ vec3 CalcPointLight(Light l, vec3 n, vec3 v) // Specular shading float spec = 0.0; - if(diff > 0.0) + if (diff > 0.0) { vec3 h = normalize(-l.direction + v); spec = pow(dot(n, h), 3 + glossiness); @@ -61,7 +61,7 @@ vec3 CalcDirectionalLight(Light l, vec3 n, vec3 v) // Specular shading float spec = 0.0; - if(diff > 0.0) + if (diff > 0.0) { vec3 h = normalize(lightDir + v); spec = pow(dot(n, h), 3 + glossiness); @@ -84,7 +84,7 @@ vec3 CalcSpotLight(Light l, vec3 n, vec3 v) float attenuation = clamp(dot(n, lightToSurface), 0.0, 1.0); attenuation = dot(lightToSurface, -lightDir); float lightToSurfaceAngle = degrees(acos(attenuation)); - if(lightToSurfaceAngle > l.coneAngle) attenuation = 0.0; + if (lightToSurfaceAngle > l.coneAngle) attenuation = 0.0; float falloff = (l.coneAngle - lightToSurfaceAngle)/l.coneAngle; // Combine diffuse and attenuation @@ -92,7 +92,7 @@ vec3 CalcSpotLight(Light l, vec3 n, vec3 v) // Specular shading float spec = 0.0; - if(diffAttenuation > 0.0) + if (diffAttenuation > 0.0) { vec3 h = normalize(lightDir + v); spec = pow(dot(n, h), 3 + glossiness); @@ -115,13 +115,13 @@ void main() vec4 texelColor = texture(texture0, fragTexCoord); vec3 lighting = colAmbient.rgb; - for(int i = 0; i < lightsCount; i++) + for (int i = 0; i < lightsCount; i++) { // Check if light is enabled - if(lights[i].enabled == 1) + if (lights[i].enabled == 1) { // Calculate lighting based on light type - switch(lights[i].type) + switch (lights[i].type) { case 0: lighting += CalcPointLight(lights[i], n, v); break; case 1: lighting += CalcDirectionalLight(lights[i], n, v); break; -- cgit v1.2.3 From 11cf455fe0d2c956043aa70f7d8256c4a339b430 Mon Sep 17 00:00:00 2001 From: victorfisac Date: Mon, 30 May 2016 19:59:21 +0200 Subject: Standard Lighting (3/3) - Added normal and specular maps to standard shader. - Added full tint attribute to standard shader and material data type. - Changed point light attenuation to radius. --- examples/resources/shaders/standard.fs | 44 ++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 13 deletions(-) (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/standard.fs b/examples/resources/shaders/standard.fs index 3c3bef4b..bb9e6865 100644 --- a/examples/resources/shaders/standard.fs +++ b/examples/resources/shaders/standard.fs @@ -8,12 +8,18 @@ in vec3 fragNormal; out vec4 finalColor; uniform sampler2D texture0; +uniform sampler2D texture1; +uniform sampler2D texture2; +uniform vec4 colTint; uniform vec4 colAmbient; uniform vec4 colDiffuse; uniform vec4 colSpecular; uniform float glossiness; +uniform int useNormal; +uniform int useSpecular; + uniform mat4 modelMatrix; uniform vec3 viewDir; @@ -24,7 +30,7 @@ struct Light { vec3 direction; vec4 diffuse; float intensity; - float attenuation; + float radius; float coneAngle; }; @@ -32,27 +38,27 @@ const int maxLights = 8; uniform int lightsCount; uniform Light lights[maxLights]; -vec3 CalcPointLight(Light l, vec3 n, vec3 v) +vec3 CalcPointLight(Light l, vec3 n, vec3 v, float s) { vec3 surfacePos = vec3(modelMatrix*vec4(fragPosition, 1)); vec3 surfaceToLight = l.position - surfacePos; // Diffuse shading float brightness = clamp(dot(n, surfaceToLight)/(length(surfaceToLight)*length(n)), 0, 1); - float diff = 1.0/dot(surfaceToLight/l.attenuation, surfaceToLight/l.attenuation)*brightness*l.intensity; + float diff = 1.0/dot(surfaceToLight/l.radius, surfaceToLight/l.radius)*brightness*l.intensity; // Specular shading float spec = 0.0; if (diff > 0.0) { vec3 h = normalize(-l.direction + v); - spec = pow(dot(n, h), 3 + glossiness); + spec = pow(dot(n, h), 3 + glossiness)*s; } return (diff*l.diffuse.rgb*colDiffuse.rgb + spec*colSpecular.rgb); } -vec3 CalcDirectionalLight(Light l, vec3 n, vec3 v) +vec3 CalcDirectionalLight(Light l, vec3 n, vec3 v, float s) { vec3 lightDir = normalize(-l.direction); @@ -64,14 +70,14 @@ vec3 CalcDirectionalLight(Light l, vec3 n, vec3 v) if (diff > 0.0) { vec3 h = normalize(lightDir + v); - spec = pow(dot(n, h), 3 + glossiness); + spec = pow(dot(n, h), 3 + glossiness)*s; } // Combine results return (diff*l.intensity*l.diffuse.rgb*colDiffuse.rgb + spec*colSpecular.rgb); } -vec3 CalcSpotLight(Light l, vec3 n, vec3 v) +vec3 CalcSpotLight(Light l, vec3 n, vec3 v, float s) { vec3 surfacePos = vec3(modelMatrix*vec4(fragPosition, 1)); vec3 lightToSurface = normalize(surfacePos - l.position); @@ -95,7 +101,7 @@ vec3 CalcSpotLight(Light l, vec3 n, vec3 v) if (diffAttenuation > 0.0) { vec3 h = normalize(lightDir + v); - spec = pow(dot(n, h), 3 + glossiness); + spec = pow(dot(n, h), 3 + glossiness)*s; } return falloff*(diffAttenuation*l.diffuse.rgb + spec*colSpecular.rgb); @@ -104,9 +110,10 @@ vec3 CalcSpotLight(Light l, vec3 n, vec3 v) void main() { // Calculate fragment normal in screen space + // NOTE: important to multiply model matrix by fragment normal to apply model transformation (rotation and scale) mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); vec3 normal = normalize(normalMatrix*fragNormal); - + // Normalize normal and view direction vectors vec3 n = normalize(normal); vec3 v = normalize(viewDir); @@ -115,6 +122,17 @@ void main() vec4 texelColor = texture(texture0, fragTexCoord); vec3 lighting = colAmbient.rgb; + // Calculate normal texture color fetching or set to maximum normal value by default + if(useNormal == 1) + { + n *= texture(texture1, fragTexCoord).rgb; + n = normalize(n); + } + + // Calculate specular texture color fetching or set to maximum specular value by default + float spec = 1.0; + if(useSpecular == 1) spec *= normalize(texture(texture2, fragTexCoord).r); + for (int i = 0; i < lightsCount; i++) { // Check if light is enabled @@ -123,14 +141,14 @@ void main() // Calculate lighting based on light type switch (lights[i].type) { - case 0: lighting += CalcPointLight(lights[i], n, v); break; - case 1: lighting += CalcDirectionalLight(lights[i], n, v); break; - case 2: lighting += CalcSpotLight(lights[i], n, v); break; + case 0: lighting += CalcPointLight(lights[i], n, v, spec); break; + case 1: lighting += CalcDirectionalLight(lights[i], n, v, spec); break; + case 2: lighting += CalcSpotLight(lights[i], n, v, spec); break; default: break; } } } // Calculate final fragment color - finalColor = vec4(texelColor.rgb*lighting, texelColor.a); + finalColor = vec4(texelColor.rgb*lighting*colTint.rgb, texelColor.a*colTint.a); } -- cgit v1.2.3 From 302ec438dd8a5483e4fcf81d4bd80ac7d09e6a61 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 31 May 2016 18:15:53 +0200 Subject: Removed colTint, tint color is colDiffuse Tint color could be applied to colDiffuse... but what's the best way? Replace it? Multiply by? A point to think about... --- examples/resources/shaders/glsl330/grayscale.fs | 4 ++-- examples/resources/shaders/standard.fs | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/glsl330/grayscale.fs b/examples/resources/shaders/glsl330/grayscale.fs index d4a8824f..5b3e11be 100644 --- a/examples/resources/shaders/glsl330/grayscale.fs +++ b/examples/resources/shaders/glsl330/grayscale.fs @@ -6,7 +6,7 @@ in vec4 fragColor; // Input uniform values uniform sampler2D texture0; -uniform vec4 fragTintColor; +uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; @@ -16,7 +16,7 @@ out vec4 finalColor; void main() { // Texel color fetching from texture sampler - vec4 texelColor = texture(texture0, fragTexCoord)*fragTintColor*fragColor; + vec4 texelColor = texture(texture0, fragTexCoord)*colDiffuse*fragColor; // Convert texel color to grayscale using NTSC conversion weights float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114)); diff --git a/examples/resources/shaders/standard.fs b/examples/resources/shaders/standard.fs index bb9e6865..e5916031 100644 --- a/examples/resources/shaders/standard.fs +++ b/examples/resources/shaders/standard.fs @@ -11,7 +11,6 @@ uniform sampler2D texture0; uniform sampler2D texture1; uniform sampler2D texture2; -uniform vec4 colTint; uniform vec4 colAmbient; uniform vec4 colDiffuse; uniform vec4 colSpecular; @@ -55,7 +54,7 @@ vec3 CalcPointLight(Light l, vec3 n, vec3 v, float s) spec = pow(dot(n, h), 3 + glossiness)*s; } - return (diff*l.diffuse.rgb*colDiffuse.rgb + spec*colSpecular.rgb); + return (diff*l.diffuse.rgb + spec*colSpecular.rgb); } vec3 CalcDirectionalLight(Light l, vec3 n, vec3 v, float s) @@ -74,7 +73,7 @@ vec3 CalcDirectionalLight(Light l, vec3 n, vec3 v, float s) } // Combine results - return (diff*l.intensity*l.diffuse.rgb*colDiffuse.rgb + spec*colSpecular.rgb); + return (diff*l.intensity*l.diffuse.rgb + spec*colSpecular.rgb); } vec3 CalcSpotLight(Light l, vec3 n, vec3 v, float s) @@ -150,5 +149,5 @@ void main() } // Calculate final fragment color - finalColor = vec4(texelColor.rgb*lighting*colTint.rgb, texelColor.a*colTint.a); + finalColor = vec4(texelColor.rgb*lighting*colDiffuse.rgb, texelColor.a*colDiffuse.a); } -- cgit v1.2.3 From d17a0cee1aa53978387e68be58d901bffd1ac0a9 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 31 May 2016 19:12:37 +0200 Subject: Review text formatting (spacing, tabs...) --- examples/resources/shaders/standard.fs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'examples/resources/shaders') diff --git a/examples/resources/shaders/standard.fs b/examples/resources/shaders/standard.fs index e5916031..e5a6d1bc 100644 --- a/examples/resources/shaders/standard.fs +++ b/examples/resources/shaders/standard.fs @@ -88,8 +88,10 @@ vec3 CalcSpotLight(Light l, vec3 n, vec3 v, float s) // Spot attenuation float attenuation = clamp(dot(n, lightToSurface), 0.0, 1.0); attenuation = dot(lightToSurface, -lightDir); + float lightToSurfaceAngle = degrees(acos(attenuation)); if (lightToSurfaceAngle > l.coneAngle) attenuation = 0.0; + float falloff = (l.coneAngle - lightToSurfaceAngle)/l.coneAngle; // Combine diffuse and attenuation @@ -103,7 +105,7 @@ vec3 CalcSpotLight(Light l, vec3 n, vec3 v, float s) spec = pow(dot(n, h), 3 + glossiness)*s; } - return falloff*(diffAttenuation*l.diffuse.rgb + spec*colSpecular.rgb); + return (falloff*(diffAttenuation*l.diffuse.rgb + spec*colSpecular.rgb)); } void main() @@ -122,7 +124,7 @@ void main() vec3 lighting = colAmbient.rgb; // Calculate normal texture color fetching or set to maximum normal value by default - if(useNormal == 1) + if (useNormal == 1) { n *= texture(texture1, fragTexCoord).rgb; n = normalize(n); @@ -130,7 +132,7 @@ void main() // Calculate specular texture color fetching or set to maximum specular value by default float spec = 1.0; - if(useSpecular == 1) spec *= normalize(texture(texture2, fragTexCoord).r); + if (useSpecular == 1) spec *= normalize(texture(texture2, fragTexCoord).r); for (int i = 0; i < lightsCount; i++) { -- cgit v1.2.3