diff options
| author | raysan5 <raysan5@gmail.com> | 2016-04-07 12:32:32 +0200 |
|---|---|---|
| committer | raysan5 <raysan5@gmail.com> | 2016-04-07 12:32:32 +0200 |
| commit | 1d545449bb19148b45d2d92f213e1001a8eb527c (patch) | |
| tree | 3e9627f74717b957521202fb17c316e936301bbb /examples/resources/shaders/glsl330 | |
| parent | 78b502b0bf1ee796d86837c611150ed5f8d58fd2 (diff) | |
| download | raylib-1d545449bb19148b45d2d92f213e1001a8eb527c.tar.gz raylib-1d545449bb19148b45d2d92f213e1001a8eb527c.zip | |
Reviewed shaders and added comments
Diffstat (limited to 'examples/resources/shaders/glsl330')
| -rw-r--r-- | examples/resources/shaders/glsl330/base.vs | 26 | ||||
| -rw-r--r-- | examples/resources/shaders/glsl330/bloom.fs | 38 | ||||
| -rw-r--r-- | examples/resources/shaders/glsl330/grayscale.fs | 26 | ||||
| -rw-r--r-- | examples/resources/shaders/glsl330/phong.fs | 76 | ||||
| -rw-r--r-- | examples/resources/shaders/glsl330/phong.vs | 29 | ||||
| -rw-r--r-- | examples/resources/shaders/glsl330/swirl.fs | 46 |
6 files changed, 241 insertions, 0 deletions
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 |
