moving the deferrered shaders from the resource folder to built-in the executable

This commit is contained in:
Sam Gateau 2014-12-30 17:42:10 -08:00
parent 3409ef3b46
commit 37017d0c16
11 changed files with 27 additions and 475 deletions

View file

@ -1,17 +0,0 @@
#version 120
//
// 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;
}

View file

@ -1,19 +0,0 @@
#version 120
//
// 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);
}

View file

@ -1,71 +0,0 @@
#version 120
//
// 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);
}
}

View file

@ -1,80 +0,0 @@
#version 120
//
// 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);
}

View file

@ -1,75 +0,0 @@
#version 120
//
// 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);
}

View file

@ -1,75 +0,0 @@
#version 120
//
// 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);
}

View file

@ -1,25 +0,0 @@
#version 120
//
// 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);
}

View file

@ -1,26 +0,0 @@
#version 120
//
// 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();
}

View file

@ -1,77 +0,0 @@
#version 120
//
// 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);
}

View file

@ -25,24 +25,37 @@
#include "RenderUtil.h"
#include "TextureCache.h"
#include "simple_vert.h"
#include "simple_frag.h"
#include "deferred_light_vert.h"
#include "deferred_light_limited_vert.h"
#include "directional_light_frag.h"
#include "directional_light_shadow_map_frag.h"
#include "directional_light_cascaded_shadow_map_frag.h"
#include "point_light_frag.h"
#include "spot_light_frag.h"
void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) {
_viewState = viewState;
_simpleProgram.addShaderFromSourceFile(QGLShader::Vertex, PathUtils::resourcesPath() + "shaders/simple.vert");
_simpleProgram.addShaderFromSourceFile(QGLShader::Fragment, PathUtils::resourcesPath() + "shaders/simple.frag");
_simpleProgram.addShaderFromSourceCode(QGLShader::Vertex, simple_vert);
_simpleProgram.addShaderFromSourceCode(QGLShader::Fragment, simple_frag);
_simpleProgram.link();
_simpleProgram.bind();
_glowIntensityLocation = _simpleProgram.uniformLocation("glowIntensity");
_simpleProgram.release();
loadLightProgram("shaders/directional_light.frag", false, _directionalLight, _directionalLightLocations);
loadLightProgram("shaders/directional_light_shadow_map.frag", false, _directionalLightShadowMap,
loadLightProgram(directional_light_frag, false, _directionalLight, _directionalLightLocations);
loadLightProgram(directional_light_shadow_map_frag, false, _directionalLightShadowMap,
_directionalLightShadowMapLocations);
loadLightProgram("shaders/directional_light_cascaded_shadow_map.frag", false, _directionalLightCascadedShadowMap,
loadLightProgram(directional_light_cascaded_shadow_map_frag, false, _directionalLightCascadedShadowMap,
_directionalLightCascadedShadowMapLocations);
loadLightProgram("shaders/point_light.frag", true, _pointLight, _pointLightLocations);
loadLightProgram("shaders/spot_light.frag", true, _spotLight, _spotLightLocations);
loadLightProgram(point_light_frag, true, _pointLight, _pointLightLocations);
loadLightProgram(spot_light_frag, true, _spotLight, _spotLightLocations);
}
void DeferredLightingEffect::bindSimpleProgram() {
@ -400,10 +413,13 @@ void DeferredLightingEffect::render() {
_postLightingRenderables.clear();
}
void DeferredLightingEffect::loadLightProgram(const char* name, bool limited, ProgramObject& program, LightLocations& locations) {
program.addShaderFromSourceFile(QGLShader::Vertex, PathUtils::resourcesPath() +
void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations) {
/* program.addShaderFromSourceFile(QGLShader::Vertex, PathUtils::resourcesPath() +
(limited ? "shaders/deferred_light_limited.vert" : "shaders/deferred_light.vert"));
program.addShaderFromSourceFile(QGLShader::Fragment, PathUtils::resourcesPath() + name);
*/
program.addShaderFromSourceCode(QGLShader::Vertex, (limited ? deferred_light_limited_vert : deferred_light_vert));
program.addShaderFromSourceCode(QGLShader::Fragment, fragSource);
program.link();
program.bind();

View file

@ -86,7 +86,8 @@ private:
int radius;
};
static void loadLightProgram(const char* name, bool limited, ProgramObject& program, LightLocations& locations);
//static void loadLightProgram(const char* name, bool limited, ProgramObject& program, LightLocations& locations);
static void loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations);
ProgramObject _simpleProgram;
int _glowIntensityLocation;