From 8d956394160809e777307873b8e862be074b0a89 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Tue, 30 Dec 2014 17:43:52 -0800 Subject: [PATCH] moving the deferrered shaders from the resource folder to built-in the executable --- libraries/render-utils/src/deferred_light.slv | 18 +++++ .../src/deferred_light_limited.slv | 20 +++++ .../render-utils/src/directional_light.slf | 72 +++++++++++++++++ .../directional_light_cascaded_shadow_map.slf | 81 +++++++++++++++++++ .../src/directional_light_shadow_map.slf | 76 +++++++++++++++++ libraries/render-utils/src/point_light.slf | 76 +++++++++++++++++ libraries/render-utils/src/simple.slf | 26 ++++++ libraries/render-utils/src/simple.slv | 27 +++++++ libraries/render-utils/src/spot_light.slf | 78 ++++++++++++++++++ 9 files changed, 474 insertions(+) create mode 100644 libraries/render-utils/src/deferred_light.slv create mode 100644 libraries/render-utils/src/deferred_light_limited.slv create mode 100644 libraries/render-utils/src/directional_light.slf create mode 100644 libraries/render-utils/src/directional_light_cascaded_shadow_map.slf create mode 100644 libraries/render-utils/src/directional_light_shadow_map.slf create mode 100644 libraries/render-utils/src/point_light.slf create mode 100644 libraries/render-utils/src/simple.slf create mode 100644 libraries/render-utils/src/simple.slv create mode 100644 libraries/render-utils/src/spot_light.slf diff --git a/libraries/render-utils/src/deferred_light.slv b/libraries/render-utils/src/deferred_light.slv new file mode 100644 index 0000000000..f26539fe69 --- /dev/null +++ b/libraries/render-utils/src/deferred_light.slv @@ -0,0 +1,18 @@ +<@include Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// deferred_light.vert +// vertex shader +// +// Created by Andrzej Kapolka on 9/18/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +void main(void) { + gl_TexCoord[0] = gl_MultiTexCoord0; + gl_Position = gl_Vertex; +} diff --git a/libraries/render-utils/src/deferred_light_limited.slv b/libraries/render-utils/src/deferred_light_limited.slv new file mode 100644 index 0000000000..fdfb88d806 --- /dev/null +++ b/libraries/render-utils/src/deferred_light_limited.slv @@ -0,0 +1,20 @@ +<@include Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// deferred_light_limited.vert +// vertex shader +// +// Created by Andrzej Kapolka on 9/19/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +void main(void) { + gl_Position = ftransform(); + vec4 projected = gl_Position / gl_Position.w; + gl_TexCoord[0] = vec4(dot(projected, gl_ObjectPlaneS[3]) * gl_Position.w, + dot(projected, gl_ObjectPlaneT[3]) * gl_Position.w, 0.0, gl_Position.w); +} diff --git a/libraries/render-utils/src/directional_light.slf b/libraries/render-utils/src/directional_light.slf new file mode 100644 index 0000000000..70020320c5 --- /dev/null +++ b/libraries/render-utils/src/directional_light.slf @@ -0,0 +1,72 @@ +<@include Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// directional_light.frag +// fragment shader +// +// Created by Andrzej Kapolka on 9/3/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// the diffuse texture +uniform sampler2D diffuseMap; + +// the normal texture +uniform sampler2D normalMap; + +// the specular texture +uniform sampler2D specularMap; + +// the depth texture +uniform sampler2D depthMap; + +// the distance to the near clip plane +uniform float near; + +// scale factor for depth: (far - near) / far +uniform float depthScale; + +// offset for depth texture coordinates +uniform vec2 depthTexCoordOffset; + +// scale for depth texture coordinates +uniform vec2 depthTexCoordScale; + +void main(void) { + float depthVal = texture2D(depthMap, gl_TexCoord[0].st).r; + vec4 normalVal = texture2D(normalMap, gl_TexCoord[0].st); + vec4 diffuseVal = texture2D(diffuseMap, gl_TexCoord[0].st); + vec4 specularVal = texture2D(specularMap, gl_TexCoord[0].st); + + // compute the view space position using the depth + float z = near / (depthVal * depthScale - 1.0); + vec4 position = vec4((depthTexCoordOffset + gl_TexCoord[0].st * depthTexCoordScale) * z, z, 0.0); + + // get the normal from the map + vec4 normal = normalVal; + if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { + normal.a = 1.0; + normalVal.a = 0.0; + gl_FragColor = vec4(diffuseVal.rgb * specularVal.rgb, 1.0); + } else { + vec3 normalizedNormal = normalize(normal.xyz * 2.0 - vec3(1.0)); + + // compute the base color based on OpenGL lighting model + float diffuse = dot(normalizedNormal, gl_LightSource[0].position.xyz); + float facingLight = step(0.0, diffuse); + vec3 baseColor = diffuseVal.rgb * (gl_FrontLightModelProduct.sceneColor.rgb + + gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight)); + + // compute the specular multiplier (sans exponent) + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(position.xyz)), + normalizedNormal)); + + // add specular contribution + vec4 specularColor = specularVal; + gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normal.a); + } +} diff --git a/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf b/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf new file mode 100644 index 0000000000..c47ac7121d --- /dev/null +++ b/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf @@ -0,0 +1,81 @@ +<@include Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// directional_light.frag +// fragment shader +// +// Created by Andrzej Kapolka on 9/3/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// the diffuse texture +uniform sampler2D diffuseMap; + +// the normal texture +uniform sampler2D normalMap; + +// the specular texture +uniform sampler2D specularMap; + +// the depth texture +uniform sampler2D depthMap; + +// the shadow texture +uniform sampler2DShadow shadowMap; + +// the distances to the cascade sections +uniform vec3 shadowDistances; + +// the inverse of the size of the shadow map +uniform float shadowScale; + +// the distance to the near clip plane +uniform float near; + +// scale factor for depth: (far - near) / far +uniform float depthScale; + +// offset for depth texture coordinates +uniform vec2 depthTexCoordOffset; + +// scale for depth texture coordinates +uniform vec2 depthTexCoordScale; + +void main(void) { + // compute the view space position using the depth + float z = near / (texture2D(depthMap, gl_TexCoord[0].st).r * depthScale - 1.0); + vec4 position = vec4((depthTexCoordOffset + gl_TexCoord[0].st * depthTexCoordScale) * z, z, 1.0); + + // compute the index of the cascade to use and the corresponding texture coordinates + int shadowIndex = int(dot(step(vec3(position.z), shadowDistances), vec3(1.0, 1.0, 1.0))); + vec3 shadowTexCoord = vec3(dot(gl_EyePlaneS[shadowIndex], position), dot(gl_EyePlaneT[shadowIndex], position), + dot(gl_EyePlaneR[shadowIndex], position)); + + // get the normal from the map + vec4 normal = texture2D(normalMap, gl_TexCoord[0].st); + vec4 normalizedNormal = normalize(normal * 2.0 - vec4(1.0, 1.0, 1.0, 2.0)); + + // average values from the shadow map + float diffuse = dot(normalizedNormal, gl_LightSource[0].position); + float facingLight = step(0.0, diffuse) * 0.25 * + (shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, -shadowScale, 0.0)).r + + shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, shadowScale, 0.0)).r + + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, -shadowScale, 0.0)).r + + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, shadowScale, 0.0)).r); + + // compute the base color based on OpenGL lighting model + vec4 baseColor = texture2D(diffuseMap, gl_TexCoord[0].st) * (gl_FrontLightModelProduct.sceneColor + + gl_FrontLightProduct[0].ambient + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); + + // compute the specular multiplier (sans exponent) + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - normalize(vec4(position.xyz, 0.0))), + normalizedNormal)); + + // add specular contribution + vec4 specularColor = texture2D(specularMap, gl_TexCoord[0].st); + gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normal.a); +} diff --git a/libraries/render-utils/src/directional_light_shadow_map.slf b/libraries/render-utils/src/directional_light_shadow_map.slf new file mode 100644 index 0000000000..fd3d5f161f --- /dev/null +++ b/libraries/render-utils/src/directional_light_shadow_map.slf @@ -0,0 +1,76 @@ +<@include Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// directional_light.frag +// fragment shader +// +// Created by Andrzej Kapolka on 9/3/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// the diffuse texture +uniform sampler2D diffuseMap; + +// the normal texture +uniform sampler2D normalMap; + +// the specular texture +uniform sampler2D specularMap; + +// the depth texture +uniform sampler2D depthMap; + +// the shadow texture +uniform sampler2DShadow shadowMap; + +// the inverse of the size of the shadow map +uniform float shadowScale; + +// the distance to the near clip plane +uniform float near; + +// scale factor for depth: (far - near) / far +uniform float depthScale; + +// offset for depth texture coordinates +uniform vec2 depthTexCoordOffset; + +// scale for depth texture coordinates +uniform vec2 depthTexCoordScale; + +void main(void) { + // compute the view space position using the depth + float z = near / (texture2D(depthMap, gl_TexCoord[0].st).r * depthScale - 1.0); + vec4 position = vec4((depthTexCoordOffset + gl_TexCoord[0].st * depthTexCoordScale) * z, z, 1.0); + + // compute the corresponding texture coordinates + vec3 shadowTexCoord = vec3(dot(gl_EyePlaneS[0], position), dot(gl_EyePlaneT[0], position), dot(gl_EyePlaneR[0], position)); + + // get the normal from the map + vec4 normal = texture2D(normalMap, gl_TexCoord[0].st); + vec4 normalizedNormal = normalize(normal * 2.0 - vec4(1.0, 1.0, 1.0, 2.0)); + + // average values from the shadow map + float diffuse = dot(normalizedNormal, gl_LightSource[0].position); + float facingLight = step(0.0, diffuse) * 0.25 * + (shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, -shadowScale, 0.0)).r + + shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, shadowScale, 0.0)).r + + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, -shadowScale, 0.0)).r + + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, shadowScale, 0.0)).r); + + // compute the base color based on OpenGL lighting model + vec4 baseColor = texture2D(diffuseMap, gl_TexCoord[0].st) * (gl_FrontLightModelProduct.sceneColor + + gl_FrontLightProduct[0].ambient + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); + + // compute the specular multiplier (sans exponent) + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - normalize(vec4(position.xyz, 0.0))), + normalizedNormal)); + + // add specular contribution + vec4 specularColor = texture2D(specularMap, gl_TexCoord[0].st); + gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normal.a); +} diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf new file mode 100644 index 0000000000..320dc93f5e --- /dev/null +++ b/libraries/render-utils/src/point_light.slf @@ -0,0 +1,76 @@ +<@include Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// spot_light.frag +// fragment shader +// +// Created by Andrzej Kapolka on 9/18/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// the diffuse texture +uniform sampler2D diffuseMap; + +// the normal texture +uniform sampler2D normalMap; + +// the specular texture +uniform sampler2D specularMap; + +// the depth texture +uniform sampler2D depthMap; + +// the distance to the near clip plane +uniform float near; + +// scale factor for depth: (far - near) / far +uniform float depthScale; + +// offset for depth texture coordinates +uniform vec2 depthTexCoordOffset; + +// scale for depth texture coordinates +uniform vec2 depthTexCoordScale; + +// the radius (hard cutoff) of the light effect +uniform float radius; + +void main(void) { + // get the depth and exit early if it doesn't pass the test + vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; + float depth = texture2D(depthMap, texCoord).r; + if (depth < gl_FragCoord.z) { + discard; + } + // compute the view space position using the depth + float z = near / (depth * depthScale - 1.0); + vec4 position = vec4((depthTexCoordOffset + texCoord * depthTexCoordScale) * z, z, 1.0); + + // get the normal from the map + vec4 normal = texture2D(normalMap, texCoord); + vec4 normalizedNormal = normalize(normal * 2.0 - vec4(1.0, 1.0, 1.0, 2.0)); + + // compute the base color based on OpenGL lighting model + vec4 lightVector = gl_LightSource[1].position - position; + float lightDistance = length(lightVector); + lightVector = lightVector / lightDistance; + float diffuse = dot(normalizedNormal, lightVector); + float facingLight = step(0.0, diffuse); + vec4 baseColor = texture2D(diffuseMap, texCoord) * (gl_FrontLightProduct[1].ambient + + gl_FrontLightProduct[1].diffuse * (diffuse * facingLight)); + + // compute attenuation based on distance, etc. + float attenuation = step(lightDistance, radius) / dot(vec3(gl_LightSource[1].constantAttenuation, + gl_LightSource[1].linearAttenuation, gl_LightSource[1].quadraticAttenuation), + vec3(1.0, lightDistance, lightDistance * lightDistance)); + + // add base to specular, modulate by attenuation + float specular = facingLight * max(0.0, dot(normalize(lightVector - normalize(vec4(position.xyz, 0.0))), + normalizedNormal)); + vec4 specularColor = texture2D(specularMap, texCoord); + gl_FragColor = vec4((baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb) * attenuation, 0.0); +} diff --git a/libraries/render-utils/src/simple.slf b/libraries/render-utils/src/simple.slf new file mode 100644 index 0000000000..50e584669d --- /dev/null +++ b/libraries/render-utils/src/simple.slf @@ -0,0 +1,26 @@ +<@include Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// simple.frag +// fragment shader +// +// Created by Andrzej Kapolka on 9/15/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// the interpolated normal +varying vec4 normal; + +// the glow intensity +uniform float glowIntensity; + +void main(void) { + // set the diffuse, normal, specular data + gl_FragData[0] = vec4(gl_Color.rgb, glowIntensity); + gl_FragData[1] = normalize(normal) * 0.5 + vec4(0.5, 0.5, 0.5, 1.0); + gl_FragData[2] = vec4(gl_FrontMaterial.specular.rgb, gl_FrontMaterial.shininess / 128.0); +} diff --git a/libraries/render-utils/src/simple.slv b/libraries/render-utils/src/simple.slv new file mode 100644 index 0000000000..e874a6d8a9 --- /dev/null +++ b/libraries/render-utils/src/simple.slv @@ -0,0 +1,27 @@ +<@include Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// simple.vert +// vertex shader +// +// Created by Andrzej Kapolka on 9/15/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// the interpolated normal +varying vec4 normal; + +void main(void) { + // transform and store the normal for interpolation + normal = normalize(gl_ModelViewMatrix * vec4(gl_Normal, 0.0)); + + // pass along the diffuse color + gl_FrontColor = gl_Color; + + // use standard pipeline transform + gl_Position = ftransform(); +} diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf new file mode 100644 index 0000000000..489802d061 --- /dev/null +++ b/libraries/render-utils/src/spot_light.slf @@ -0,0 +1,78 @@ +<@include Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// spot_light.frag +// fragment shader +// +// Created by Andrzej Kapolka on 9/18/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// the diffuse texture +uniform sampler2D diffuseMap; + +// the normal texture +uniform sampler2D normalMap; + +// the specular texture +uniform sampler2D specularMap; + +// the depth texture +uniform sampler2D depthMap; + +// the distance to the near clip plane +uniform float near; + +// scale factor for depth: (far - near) / far +uniform float depthScale; + +// offset for depth texture coordinates +uniform vec2 depthTexCoordOffset; + +// scale for depth texture coordinates +uniform vec2 depthTexCoordScale; + +// the radius (hard cutoff) of the light effect +uniform float radius; + +void main(void) { + // get the depth and exit early if it doesn't pass the test + vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; + float depth = texture2D(depthMap, texCoord).r; + if (depth < gl_FragCoord.z) { + discard; + } + // compute the view space position using the depth + float z = near / (depth * depthScale - 1.0); + vec4 position = vec4((depthTexCoordOffset + texCoord * depthTexCoordScale) * z, z, 1.0); + + // get the normal from the map + vec4 normal = texture2D(normalMap, texCoord); + vec4 normalizedNormal = normalize(normal * 2.0 - vec4(1.0, 1.0, 1.0, 2.0)); + + // compute the base color based on OpenGL lighting model + vec4 lightVector = gl_LightSource[1].position - position; + float lightDistance = length(lightVector); + lightVector = lightVector / lightDistance; + float diffuse = dot(normalizedNormal, lightVector); + float facingLight = step(0.0, diffuse); + vec4 baseColor = texture2D(diffuseMap, texCoord) * (gl_FrontLightProduct[1].ambient + + gl_FrontLightProduct[1].diffuse * (diffuse * facingLight)); + + // compute attenuation based on spot angle, distance, etc. + float cosSpotAngle = max(-dot(lightVector.xyz, gl_LightSource[1].spotDirection), 0.0); + float attenuation = step(lightDistance, radius) * step(gl_LightSource[1].spotCosCutoff, cosSpotAngle) * + pow(cosSpotAngle, gl_LightSource[1].spotExponent) / dot(vec3(gl_LightSource[1].constantAttenuation, + gl_LightSource[1].linearAttenuation, gl_LightSource[1].quadraticAttenuation), + vec3(1.0, lightDistance, lightDistance * lightDistance)); + + // add base to specular, modulate by attenuation + float specular = facingLight * max(0.0, dot(normalize(lightVector - normalize(vec4(position.xyz, 0.0))), + normalizedNormal)); + vec4 specularColor = texture2D(specularMap, texCoord); + gl_FragColor = vec4((baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb) * attenuation, 0.0); +}