Merge pull request #4093 from samcake/temp0

Factorize the shader code for deferred lighting
This commit is contained in:
Brad Hefta-Gaub 2015-01-13 17:11:17 -08:00
commit b3b43b4c14
7 changed files with 111 additions and 160 deletions

View file

@ -753,6 +753,7 @@ public:
float shininess; float shininess;
float opacity; float opacity;
QString id; QString id;
model::MaterialPointer _material;
}; };
class Cluster { class Cluster {
@ -1715,6 +1716,14 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping,
#endif #endif
} }
material.id = getID(object.properties); material.id = getID(object.properties);
material._material = model::MaterialPointer(new model::Material());
material._material->setEmissive(material.emissive);
material._material->setDiffuse(material.diffuse);
material._material->setSpecular(material.specular);
material._material->setShininess(material.shininess);
material._material->setOpacity(material.opacity);
materials.insert(material.id, material); materials.insert(material.id, material);
} else if (object.name == "NodeAttribute") { } else if (object.name == "NodeAttribute") {
@ -2138,6 +2147,8 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping,
for (int j = 0; j < extracted.partMaterialTextures.size(); j++) { for (int j = 0; j < extracted.partMaterialTextures.size(); j++) {
if (extracted.partMaterialTextures.at(j).first == materialIndex) { if (extracted.partMaterialTextures.at(j).first == materialIndex) {
FBXMeshPart& part = extracted.mesh.parts[j]; FBXMeshPart& part = extracted.mesh.parts[j];
part._material = material._material;
part.diffuseColor = material.diffuse; part.diffuseColor = material.diffuse;
part.specularColor = material.specular; part.specularColor = material.specular;
part.emissiveColor = material.emissive; part.emissiveColor = material.emissive;

View file

@ -0,0 +1,65 @@
<!
// DeferredBuffer.slh
// libraries/render-utils/src
//
// Created by Sam Gateau on 1/12/15.
// Copyright 2013 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
!>
<@if not DEFERRED_BUFFER_SLH@>
<@def DEFERRED_BUFFER_SLH@>
// 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;
struct DeferredFragment {
float depthVal;
vec4 normalVal;
vec4 diffuseVal;
vec4 specularVal;
vec4 position;
vec3 normal;
};
DeferredFragment unpackDeferredFragment(vec2 texcoord) {
DeferredFragment frag;
frag.depthVal = texture2D(depthMap, texcoord).r;
frag.normalVal = texture2D(normalMap, texcoord);
frag.diffuseVal = texture2D(diffuseMap, texcoord);
frag.specularVal = texture2D(specularMap, texcoord);
// compute the view space position using the depth
float z = near / (frag.depthVal * depthScale - 1.0);
frag.position = vec4((depthTexCoordOffset + texcoord * depthTexCoordScale) * z, z, 1.0);
// Unpack the normal from the map
frag.normal = normalize(frag.normalVal.xyz * 2.0 - vec3(1.0));
return frag;
}
<@endif@>

View file

@ -12,56 +12,29 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
// the diffuse texture // Everything about deferred buffer
uniform sampler2D diffuseMap; <@include DeferredBuffer.slh@>
// 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) { void main(void) {
float depthVal = texture2D(depthMap, gl_TexCoord[0].st).r; DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st);
vec4 normalVal = texture2D(normalMap, gl_TexCoord[0].st);
vec4 diffuseVal = texture2D(diffuseMap, gl_TexCoord[0].st); vec4 normalVal = frag.normalVal;
vec4 specularVal = texture2D(specularMap, gl_TexCoord[0].st); vec4 diffuseVal = frag.diffuseVal;
vec4 specularVal = frag.specularVal;
// 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);
// Light mapped or not ? // Light mapped or not ?
if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) {
gl_FragColor = vec4(diffuseVal.rgb * specularVal.rgb, 1.0); gl_FragColor = vec4(diffuseVal.rgb * specularVal.rgb, 1.0);
} else { } else {
// get the normal from the map
vec3 normalizedNormal = normalize(normalVal.xyz * 2.0 - vec3(1.0));
// compute the base color based on OpenGL lighting model // compute the base color based on OpenGL lighting model
float diffuse = dot(normalizedNormal, gl_LightSource[0].position.xyz); float diffuse = dot(frag.normal, gl_LightSource[0].position.xyz);
float facingLight = step(0.0, diffuse); float facingLight = step(0.0, diffuse);
vec3 baseColor = diffuseVal.rgb * (gl_FrontLightModelProduct.sceneColor.rgb + vec3 baseColor = diffuseVal.rgb * (gl_FrontLightModelProduct.sceneColor.rgb +
gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight)); gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight));
// compute the specular multiplier (sans exponent) // compute the specular multiplier (sans exponent)
float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(position.xyz)), float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(frag.position.xyz)),
normalizedNormal)); frag.normal));
// add specular contribution // add specular contribution
vec4 specularColor = specularVal; vec4 specularColor = specularVal;

View file

@ -12,52 +12,24 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
// the diffuse texture // Everything about deferred buffer
uniform sampler2D diffuseMap; <@include DeferredBuffer.slh@>
// the normal texture
uniform sampler2D normalMap;
// the specular texture
uniform sampler2D specularMap;
// the depth texture
uniform sampler2D depthMap;
// Everything about shadow // Everything about shadow
<@include Shadow.slh@> <@include Shadow.slh@>
// 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) { void main(void) {
float depthVal = texture2D(depthMap, gl_TexCoord[0].st).r; DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st);
vec4 normalVal = texture2D(normalMap, gl_TexCoord[0].st); vec4 normalVal = frag.normalVal;
vec4 diffuseVal = texture2D(diffuseMap, gl_TexCoord[0].st); vec4 diffuseVal = frag.diffuseVal;
vec4 specularVal = texture2D(specularMap, gl_TexCoord[0].st); vec4 specularVal = frag.specularVal;
// 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, 1.0);
// Eval shadow Texcoord and then Attenuation // Eval shadow Texcoord and then Attenuation
vec4 shadowTexcoord = evalCascadedShadowTexcoord(position); vec4 shadowTexcoord = evalCascadedShadowTexcoord(frag.position);
float shadowAttenuation = evalShadowAttenuation(shadowTexcoord); float shadowAttenuation = evalShadowAttenuation(shadowTexcoord);
// get the normal from the map
vec3 normalizedNormal = normalize(normalVal.xyz * 2.0 - vec3(1.0));
// how much this fragment faces the light direction // how much this fragment faces the light direction
float diffuse = dot(normalizedNormal, gl_LightSource[0].position.xyz); float diffuse = dot(frag.normal, gl_LightSource[0].position.xyz);
// Light mapped or not ? // Light mapped or not ?
if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) {
@ -83,12 +55,12 @@ void main(void) {
float facingLight = step(0.0, diffuse) * shadowAttenuation; float facingLight = step(0.0, diffuse) * shadowAttenuation;
// compute the base color based on OpenGL lighting model // compute the base color based on OpenGL lighting model
vec3 baseColor = diffuseVal.rgb * (gl_FrontLightModelProduct.sceneColor.rgb + vec3 baseColor = diffuseVal.rgb * (gl_FrontLightModelProduct.sceneColor.rgb +
gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight)); gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight));
// compute the specular multiplier (sans exponent) // compute the specular multiplier (sans exponent)
float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(position.xyz)), float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(frag.position.xyz)),
normalizedNormal)); frag.normal));
// add specular contribution // add specular contribution
vec4 specularColor = specularVal; vec4 specularColor = specularVal;

View file

@ -12,52 +12,24 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
// the diffuse texture // Everything about deferred buffer
uniform sampler2D diffuseMap; <@include DeferredBuffer.slh@>
// the normal texture
uniform sampler2D normalMap;
// the specular texture
uniform sampler2D specularMap;
// the depth texture
uniform sampler2D depthMap;
// Everything about shadow // Everything about shadow
<@include Shadow.slh@> <@include Shadow.slh@>
// 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) { void main(void) {
float depthVal = texture2D(depthMap, gl_TexCoord[0].st).r; DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st);
vec4 normalVal = texture2D(normalMap, gl_TexCoord[0].st); vec4 normalVal = frag.normalVal;
vec4 diffuseVal = texture2D(diffuseMap, gl_TexCoord[0].st); vec4 diffuseVal = frag.diffuseVal;
vec4 specularVal = texture2D(specularMap, gl_TexCoord[0].st); vec4 specularVal = frag.specularVal;
// 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, 1.0);
// Eval shadow Texcoord and then Attenuation // Eval shadow Texcoord and then Attenuation
vec4 shadowTexcoord = evalShadowTexcoord(position); vec4 shadowTexcoord = evalShadowTexcoord(frag.position);
float shadowAttenuation = evalShadowAttenuation(shadowTexcoord); float shadowAttenuation = evalShadowAttenuation(shadowTexcoord);
// get the normal from the map
vec3 normalizedNormal = normalize(normalVal.xyz * 2.0 - vec3(1.0));
// how much this fragment faces the light direction // how much this fragment faces the light direction
float diffuse = dot(normalizedNormal, gl_LightSource[0].position.xyz); float diffuse = dot(frag.normal, gl_LightSource[0].position.xyz);
// Light mapped or not ? // Light mapped or not ?
if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) {
@ -86,8 +58,8 @@ void main(void) {
gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight)); gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight));
// compute the specular multiplier (sans exponent) // compute the specular multiplier (sans exponent)
float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(position.xyz)), float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(frag.position.xyz)),
normalizedNormal)); frag.normal));
// add specular contribution // add specular contribution
vec4 specularColor = specularVal; vec4 specularColor = specularVal;

View file

@ -12,29 +12,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
// the diffuse texture // Everything about deferred buffer
uniform sampler2D diffuseMap; <@include DeferredBuffer.slh@>
// 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 // the radius (hard cutoff) of the light effect
uniform float radius; uniform float radius;

View file

@ -12,29 +12,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
// the diffuse texture // Everything about deferred buffer
uniform sampler2D diffuseMap; <@include DeferredBuffer.slh@>
// 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 // the radius (hard cutoff) of the light effect
uniform float radius; uniform float radius;