From d9efafac7ee593404224a5ccb454b0d05cc16816 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Tue, 13 Jan 2015 14:11:38 -0800 Subject: [PATCH] updating the model fragment shaders to use DeferredBUfferWrite --- .../render-utils/src/DeferredBufferWrite.slh | 15 ++++++++++++--- libraries/render-utils/src/Model.cpp | 6 +++++- libraries/render-utils/src/Model.h | 1 + libraries/render-utils/src/model.slf | 10 +--------- libraries/render-utils/src/model_lightmap.slf | 16 ++++++++++------ .../src/model_lightmap_normal_map.slf | 17 +++++++++++------ .../src/model_lightmap_normal_specular_map.slf | 16 +++++++++++++--- .../src/model_lightmap_specular_map.slf | 16 +++++++++++++--- libraries/render-utils/src/model_normal_map.slf | 15 +++++++++++---- .../src/model_normal_specular_map.slf | 15 +++++++++++---- .../render-utils/src/model_specular_map.slf | 14 +++++++++++--- libraries/render-utils/src/simple.slf | 14 ++++++++++++-- 12 files changed, 111 insertions(+), 44 deletions(-) diff --git a/libraries/render-utils/src/DeferredBufferWrite.slh b/libraries/render-utils/src/DeferredBufferWrite.slh index 8a547315cd..7b4ca7ba3b 100755 --- a/libraries/render-utils/src/DeferredBufferWrite.slh +++ b/libraries/render-utils/src/DeferredBufferWrite.slh @@ -10,19 +10,28 @@ !> <@if not DEFERRED_BUFFER_WRITE_SLH@> <@def DEFERRED_BUFFER_WRITE_SLH@> -/* + // the glow intensity uniform float glowIntensity; // the alpha threshold uniform float alphaThreshold; -*/ + +float evalOpaqueFinalAlpha(float alpha, float mapAlpha) { + return mix(alpha * glowIntensity, 1.0 - alpha * glowIntensity, step(mapAlpha, alphaThreshold)); +} void packDeferredFragment(vec3 normal, float alpha, vec3 diffuse, vec3 specular, float shininess) { - // gl_FragData[0] = vec4(diffuse.rgb, mix(gl_Color.a, 1.0 - gl_Color.a, step(diffuse.a, alphaThreshold))); gl_FragData[0] = vec4(diffuse.rgb, alpha); gl_FragData[1] = vec4(normal, 0.0) * 0.5 + vec4(0.5, 0.5, 0.5, 1.0); gl_FragData[2] = vec4(specular, shininess / 128.0); } +void packDeferredFragmentLightmap(vec3 normal, float alpha, vec3 diffuse, vec3 specular, float shininess, vec3 emissive) { + gl_FragData[0] = vec4(diffuse.rgb, alpha); + //gl_FragData[1] = vec4(normal, 0.0) * 0.5 + vec4(0.5, 0.5, 0.5, 1.0); + gl_FragData[1] = vec4(normal, 0.0) * 0.5 + vec4(0.5, 0.5, 0.5, 0.5); + gl_FragData[2] = vec4(emissive, shininess / 128.0); +} + <@endif@> diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 46fc6a6e4d..6cfad9931d 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -178,7 +178,7 @@ void Model::initProgram(ProgramObject& program, Model::Locations& locations, boo locations.alphaThreshold = program.uniformLocation("alphaThreshold"); locations.texcoordMatrices = program.uniformLocation("texcoordMatrices"); locations.emissiveParams = program.uniformLocation("emissiveParams"); - + locations.glowIntensity = program.uniformLocation("glowIntensity"); program.setUniformValue("diffuseMap", 0); program.setUniformValue("normalMap", 1); @@ -2367,6 +2367,10 @@ int Model::renderMeshesFromList(QVector& list, gpu::Batch& batch, RenderMod } glm::vec4 diffuse = glm::vec4(part.diffuseColor, part.opacity); + + if (locations->glowIntensity >= 0) { + GLBATCH(glUniform1f)(locations->glowIntensity, glowEffect->getIntensity()); + } if (!(translucent && alphaThreshold == 0.0f)) { GLBATCH(glAlphaFunc)(GL_EQUAL, diffuse.a = glowEffect->getIntensity()); } diff --git a/libraries/render-utils/src/Model.h b/libraries/render-utils/src/Model.h index 83ceab109f..ef97e7dfd8 100644 --- a/libraries/render-utils/src/Model.h +++ b/libraries/render-utils/src/Model.h @@ -338,6 +338,7 @@ private: int specularTextureUnit; int emissiveTextureUnit; int emissiveParams; + int glowIntensity; }; static Locations _locations; diff --git a/libraries/render-utils/src/model.slf b/libraries/render-utils/src/model.slf index 9c263869f0..bbff368c2d 100755 --- a/libraries/render-utils/src/model.slf +++ b/libraries/render-utils/src/model.slf @@ -16,9 +16,6 @@ // the diffuse texture uniform sampler2D diffuseMap; -// the alpha threshold -uniform float alphaThreshold; - // the interpolated normal varying vec4 normal; @@ -28,13 +25,8 @@ void main(void) { packDeferredFragment( normalize(normal.xyz), - mix(gl_Color.a, 1.0 - gl_Color.a, step(diffuse.a, alphaThreshold)), + evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), gl_Color.rgb * diffuse.rgb, gl_FrontMaterial.specular.rgb, gl_FrontMaterial.shininess); -/* - gl_FragData[0] = vec4(gl_Color.rgb * diffuse.rgb, mix(gl_Color.a, 1.0 - gl_Color.a, step(diffuse.a, alphaThreshold))); - 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/model_lightmap.slf b/libraries/render-utils/src/model_lightmap.slf index 9feacbe057..6b25921318 100755 --- a/libraries/render-utils/src/model_lightmap.slf +++ b/libraries/render-utils/src/model_lightmap.slf @@ -12,6 +12,8 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +<@include DeferredBufferWrite.slh@> + // the diffuse texture uniform sampler2D diffuseMap; @@ -19,9 +21,6 @@ uniform sampler2D diffuseMap; uniform sampler2D emissiveMap; uniform vec2 emissiveParams; -// the alpha threshold -uniform float alphaThreshold; - // the interpolated normal varying vec4 normal; @@ -32,7 +31,12 @@ void main(void) { // set the diffuse, normal, specular data vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); vec4 emissive = texture2D(emissiveMap, interpolatedTexcoord1.st); - gl_FragData[0] = vec4(gl_Color.rgb * diffuse.rgb, mix(gl_Color.a, 1.0 - gl_Color.a, step(diffuse.a, alphaThreshold))); - gl_FragData[1] = normalize(normal) * 0.5 + vec4(0.5, 0.5, 0.5, 0.5); - gl_FragData[2] = vec4((vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb), gl_FrontMaterial.shininess / 128.0); + + packDeferredFragmentLightmap( + normalize(normal.xyz), + evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), + gl_Color.rgb * diffuse.rgb, + gl_FrontMaterial.specular.rgb, + gl_FrontMaterial.shininess, + (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); } diff --git a/libraries/render-utils/src/model_lightmap_normal_map.slf b/libraries/render-utils/src/model_lightmap_normal_map.slf index 5bebbee3f6..9ef75802a8 100755 --- a/libraries/render-utils/src/model_lightmap_normal_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_map.slf @@ -12,6 +12,8 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +<@include DeferredBufferWrite.slh@> + // the diffuse texture uniform sampler2D diffuseMap; @@ -22,9 +24,6 @@ uniform sampler2D normalMap; uniform sampler2D emissiveMap; uniform vec2 emissiveParams; -// the alpha threshold -uniform float alphaThreshold; - // the interpolated normal varying vec4 interpolatedNormal; @@ -45,7 +44,13 @@ void main(void) { // set the diffuse, normal, specular data vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); vec4 emissive = texture2D(emissiveMap, interpolatedTexcoord1.st); - gl_FragData[0] = vec4(gl_Color.rgb * diffuse.rgb * (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb), mix(gl_Color.a, 1.0 - gl_Color.a, step(diffuse.a, alphaThreshold))); - gl_FragData[1] = viewNormal + vec4(0.5, 0.5, 0.5, 1.0); - gl_FragData[2] = vec4(gl_FrontMaterial.specular.rgb, gl_FrontMaterial.shininess / 128.0); + + packDeferredFragmentLightmap( + normalize(viewNormal.xyz), + evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), + //gl_Color.rgb * diffuse.rgb, + vec3(1.0, 0.0, 0.0), + gl_FrontMaterial.specular.rgb, + gl_FrontMaterial.shininess, + (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); } diff --git a/libraries/render-utils/src/model_lightmap_normal_specular_map.slf b/libraries/render-utils/src/model_lightmap_normal_specular_map.slf index d2f76e4ef3..3bc72384f7 100755 --- a/libraries/render-utils/src/model_lightmap_normal_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_specular_map.slf @@ -12,6 +12,8 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +<@include DeferredBufferWrite.slh@> + // the diffuse texture uniform sampler2D diffuseMap; @@ -25,9 +27,6 @@ uniform sampler2D normalMap; // the specular map texture uniform sampler2D specularMap; -// the alpha threshold -uniform float alphaThreshold; - // the interpolated normal varying vec4 interpolatedNormal; @@ -47,9 +46,20 @@ void main(void) { // set the diffuse, normal, specular data vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); + vec3 specular = texture2D(specularMap, gl_TexCoord[0].st).rgb; vec4 emissive = texture2D(emissiveMap, interpolatedTexcoord1.st); + + packDeferredFragmentLightmap( + normalize(viewNormal.xyz), + evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), + gl_Color.rgb * diffuse.rgb, + specular, + gl_FrontMaterial.shininess, + (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); +/* gl_FragData[0] = vec4(gl_Color.rgb * diffuse.rgb * (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb), mix(gl_Color.a, 1.0 - gl_Color.a, step(diffuse.a, alphaThreshold))); gl_FragData[1] = viewNormal + vec4(0.5, 0.5, 0.5, 1.0); gl_FragData[2] = vec4(gl_FrontMaterial.specular.rgb * texture2D(specularMap, gl_TexCoord[0].st).rgb, gl_FrontMaterial.shininess / 128.0); +*/ } diff --git a/libraries/render-utils/src/model_lightmap_specular_map.slf b/libraries/render-utils/src/model_lightmap_specular_map.slf index 40879a8fc3..7322da6b3a 100755 --- a/libraries/render-utils/src/model_lightmap_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_specular_map.slf @@ -12,6 +12,8 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +<@include DeferredBufferWrite.slh@> + // the diffuse texture uniform sampler2D diffuseMap; @@ -22,9 +24,6 @@ uniform vec2 emissiveParams; // the specular texture uniform sampler2D specularMap; -// the alpha threshold -uniform float alphaThreshold; - // the interpolated normal varying vec4 normal; @@ -33,9 +32,20 @@ varying vec2 interpolatedTexcoord1; void main(void) { // set the diffuse, normal, specular data vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); + vec3 specular = texture2D(specularMap, gl_TexCoord[0].st).rgb; vec4 emissive = texture2D(emissiveMap, interpolatedTexcoord1.st); + + packDeferredFragmentLightmap( + normalize(normal.xyz), + evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), + gl_Color.rgb * diffuse.rgb, + specular, + gl_FrontMaterial.shininess, + (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb)); +/* gl_FragData[0] = vec4(gl_Color.rgb * diffuse.rgb * (vec3(emissiveParams.x) + emissiveParams.y * emissive.rgb), mix(gl_Color.a, 1.0 - gl_Color.a, step(diffuse.a, alphaThreshold))); gl_FragData[1] = normalize(normal) * 0.5 + vec4(0.5, 0.5, 0.5, 1.0); gl_FragData[2] = vec4(gl_FrontMaterial.specular.rgb * texture2D(specularMap, gl_TexCoord[0].st).rgb, gl_FrontMaterial.shininess / 128.0); +*/ } diff --git a/libraries/render-utils/src/model_normal_map.slf b/libraries/render-utils/src/model_normal_map.slf index b625b346ed..74da394850 100755 --- a/libraries/render-utils/src/model_normal_map.slf +++ b/libraries/render-utils/src/model_normal_map.slf @@ -12,15 +12,14 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +<@include DeferredBufferWrite.slh@> + // the diffuse texture uniform sampler2D diffuseMap; // the normal map texture uniform sampler2D normalMap; -// the alpha threshold -uniform float alphaThreshold; - // the interpolated normal varying vec4 interpolatedNormal; @@ -38,7 +37,15 @@ void main(void) { // set the diffuse, normal, specular data vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); - gl_FragData[0] = vec4(gl_Color.rgb * diffuse.rgb, mix(gl_Color.a, 1.0 - gl_Color.a, step(diffuse.a, alphaThreshold))); +/* gl_FragData[0] = vec4(gl_Color.rgb * diffuse.rgb, mix(gl_Color.a, 1.0 - gl_Color.a, step(diffuse.a, alphaThreshold))); gl_FragData[1] = viewNormal + vec4(0.5, 0.5, 0.5, 1.0); gl_FragData[2] = vec4(gl_FrontMaterial.specular.rgb, gl_FrontMaterial.shininess / 128.0); + */ + + packDeferredFragment( + normalize(viewNormal.xyz), + evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), + gl_Color.rgb * diffuse.rgb, + gl_FrontMaterial.specular.rgb, + gl_FrontMaterial.shininess); } diff --git a/libraries/render-utils/src/model_normal_specular_map.slf b/libraries/render-utils/src/model_normal_specular_map.slf index fd288f0867..4248edfc90 100755 --- a/libraries/render-utils/src/model_normal_specular_map.slf +++ b/libraries/render-utils/src/model_normal_specular_map.slf @@ -12,6 +12,8 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +<@include DeferredBufferWrite.slh@> + // the diffuse texture uniform sampler2D diffuseMap; @@ -21,9 +23,6 @@ uniform sampler2D normalMap; // the specular map texture uniform sampler2D specularMap; -// the alpha threshold -uniform float alphaThreshold; - // the interpolated normal varying vec4 interpolatedNormal; @@ -41,8 +40,16 @@ void main(void) { // set the diffuse, normal, specular data vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); - gl_FragData[0] = vec4(gl_Color.rgb * diffuse.rgb, mix(gl_Color.a, 1.0 - gl_Color.a, step(diffuse.a, alphaThreshold))); + vec3 specular = texture2D(specularMap, gl_TexCoord[0].st).rgb; +/* gl_FragData[0] = vec4(gl_Color.rgb * diffuse.rgb, mix(gl_Color.a, 1.0 - gl_Color.a, step(diffuse.a, alphaThreshold))); gl_FragData[1] = viewNormal + vec4(0.5, 0.5, 0.5, 1.0); gl_FragData[2] = vec4(gl_FrontMaterial.specular.rgb * texture2D(specularMap, gl_TexCoord[0].st).rgb, gl_FrontMaterial.shininess / 128.0); + */ + packDeferredFragment( + normalize(viewNormal.xyz), + evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), + gl_Color.rgb * diffuse.rgb, + specular, + gl_FrontMaterial.shininess); } diff --git a/libraries/render-utils/src/model_specular_map.slf b/libraries/render-utils/src/model_specular_map.slf index 4428173562..db1c2df9c5 100755 --- a/libraries/render-utils/src/model_specular_map.slf +++ b/libraries/render-utils/src/model_specular_map.slf @@ -12,23 +12,31 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +<@include DeferredBufferWrite.slh@> + // the diffuse texture uniform sampler2D diffuseMap; // the specular texture uniform sampler2D specularMap; -// the alpha threshold -uniform float alphaThreshold; - // the interpolated normal varying vec4 normal; void main(void) { // set the diffuse, normal, specular data vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); + vec3 specular = texture2D(specularMap, gl_TexCoord[0].st).rgb; +/* gl_FragData[0] = vec4(gl_Color.rgb * diffuse.rgb, mix(gl_Color.a, 1.0 - gl_Color.a, step(diffuse.a, alphaThreshold))); gl_FragData[1] = normalize(normal) * 0.5 + vec4(0.5, 0.5, 0.5, 1.0); gl_FragData[2] = vec4(gl_FrontMaterial.specular.rgb * texture2D(specularMap, gl_TexCoord[0].st).rgb, gl_FrontMaterial.shininess / 128.0); + */ + packDeferredFragment( + normalize(normal.xyz), + evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), + gl_Color.rgb * diffuse.rgb, + specular, + gl_FrontMaterial.shininess); } diff --git a/libraries/render-utils/src/simple.slf b/libraries/render-utils/src/simple.slf index 50e584669d..faa4af4573 100644 --- a/libraries/render-utils/src/simple.slf +++ b/libraries/render-utils/src/simple.slf @@ -12,15 +12,25 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +<@include DeferredBufferWrite.slh@> + // the interpolated normal varying vec4 normal; // the glow intensity -uniform float glowIntensity; +//uniform float glowIntensity; void main(void) { - // set the diffuse, normal, specular data + /* // 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); + */ + + packDeferredFragment( + normalize(normal.xyz), + glowIntensity, + gl_Color.rgb, + gl_FrontMaterial.specular.rgb, + gl_FrontMaterial.shininess); }