From fc698a8525a35f9059023d8fcad66f2d4f0a6c12 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 12 Jan 2015 18:51:26 -0800 Subject: [PATCH 01/14] first step factorizing the packDeferredFragment --- libraries/render-utils/src/DeferredBuffer.slh | 1 - .../render-utils/src/DeferredBufferWrite.slh | 28 +++++++++++++++++++ libraries/render-utils/src/model.slf | 11 ++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100755 libraries/render-utils/src/DeferredBufferWrite.slh diff --git a/libraries/render-utils/src/DeferredBuffer.slh b/libraries/render-utils/src/DeferredBuffer.slh index c5630c3564..da02ef750e 100755 --- a/libraries/render-utils/src/DeferredBuffer.slh +++ b/libraries/render-utils/src/DeferredBuffer.slh @@ -11,7 +11,6 @@ <@if not DEFERRED_BUFFER_SLH@> <@def DEFERRED_BUFFER_SLH@> - // the diffuse texture uniform sampler2D diffuseMap; diff --git a/libraries/render-utils/src/DeferredBufferWrite.slh b/libraries/render-utils/src/DeferredBufferWrite.slh new file mode 100755 index 0000000000..32822af2cb --- /dev/null +++ b/libraries/render-utils/src/DeferredBufferWrite.slh @@ -0,0 +1,28 @@ + +<@if not DEFERRED_BUFFER_WRITE_SLH@> +<@def DEFERRED_BUFFER_WRITE_SLH@> +/* +// the glow intensity +uniform float glowIntensity; + +// the alpha threshold +uniform float alphaThreshold; +*/ + +void packFragment(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); +} + +<@endif@> diff --git a/libraries/render-utils/src/model.slf b/libraries/render-utils/src/model.slf index 42421c71a2..9c263869f0 100755 --- a/libraries/render-utils/src/model.slf +++ b/libraries/render-utils/src/model.slf @@ -11,6 +11,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; @@ -23,7 +25,16 @@ varying vec4 normal; void main(void) { // set the diffuse, normal, specular data vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); + + packDeferredFragment( + normalize(normal.xyz), + mix(gl_Color.a, 1.0 - gl_Color.a, step(diffuse.a, alphaThreshold)), + 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); +*/ } From 522e7698a558845b3290076d10367fc06baea705 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Tue, 13 Jan 2015 10:23:00 -0800 Subject: [PATCH 02/14] first step factorizing the packDeferredFragment --- libraries/render-utils/src/DeferredBufferWrite.slh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/render-utils/src/DeferredBufferWrite.slh b/libraries/render-utils/src/DeferredBufferWrite.slh index 32822af2cb..8a547315cd 100755 --- a/libraries/render-utils/src/DeferredBufferWrite.slh +++ b/libraries/render-utils/src/DeferredBufferWrite.slh @@ -18,7 +18,7 @@ uniform float glowIntensity; uniform float alphaThreshold; */ -void packFragment(vec3 normal, float alpha, vec3 diffuse, vec3 specular, float shininess) { +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); From d9efafac7ee593404224a5ccb454b0d05cc16816 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Tue, 13 Jan 2015 14:11:38 -0800 Subject: [PATCH 03/14] 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); } From 9c083ce86e6e9ed7e5235f40e90c1592c3168a95 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Tue, 13 Jan 2015 15:51:18 -0800 Subject: [PATCH 04/14] Using the model::Material for rendering and in shaders --- libraries/render-utils/src/Material.slh | 59 +++++++++++++++++++++++++ libraries/render-utils/src/Model.cpp | 42 +++++++++++++++--- libraries/render-utils/src/Model.h | 1 + libraries/render-utils/src/model.slf | 14 +++--- 4 files changed, 105 insertions(+), 11 deletions(-) create mode 100755 libraries/render-utils/src/Material.slh diff --git a/libraries/render-utils/src/Material.slh b/libraries/render-utils/src/Material.slh new file mode 100755 index 0000000000..4720b19d00 --- /dev/null +++ b/libraries/render-utils/src/Material.slh @@ -0,0 +1,59 @@ + +<@if not MATERIAL_SLH@> +<@def MATERIAL_SLH@> + +struct Material { + vec4 _diffuse; + vec4 _specular; + + float getOpacity() { return _diffuse.a; } + vec3 getDiffuse() { return _diffuse.rgb; } + vec3 getSpecular() { return _specular.rgb; } + float getShininess() { return _specular.a; } +}; + +<@if GLPROFILE == PC_GL@> +uniform materialBuffer { + Material mat; +}; +Material getMaterial() { + return mat; +} +<@elif GLPROFILE == MAC_GL@> +uniform vec4 materialBuffer[2]; +Material getMaterial() { + Material mat; + mat._diffuse = materialBuffer[0]; + mat._specular = materialBuffer[1]; + return mat; +} +<@else@> +uniform vec4 materialBuffer[2]; +Material getMaterial() { + Material mat; + mat._diffuse = materialBuffer[0]; + mat._specular = materialBuffer[1]; + return mat; +} +<@endif@> + + + +<@endif@> \ No newline at end of file diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 6cfad9931d..2b9418b82f 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -198,6 +198,28 @@ void Model::initProgram(ProgramObject& program, Model::Locations& locations, boo locations.emissiveTextureUnit = -1; } + // bindable uniform version +#if defined(Q_OS_MAC) + loc = program.uniformLocation("materialBuffer"); + if (loc >= 0) { + locations.materialBufferUnit = loc; + } else { + locations.materialBufferUnit = -1; + } +#else + loc = glGetUniformBlockIndex(program.programId(), "materialBuffer"); + if (loc >= 0) { + glUniformBlockBinding(program.programId(), loc, 1); + locations.materialBufferUnit = 1; + } else { + locations.materialBufferUnit = -1; + } +#endif + + if (!program.isLinked()) { + program.release(); + } + program.release(); } @@ -2348,6 +2370,7 @@ int Model::renderMeshesFromList(QVector& list, gpu::Batch& batch, RenderMod for (int j = 0; j < networkMesh.parts.size(); j++) { const NetworkMeshPart& networkPart = networkMesh.parts.at(j); const FBXMeshPart& part = mesh.parts.at(j); + model::MaterialPointer material = part._material; if ((networkPart.isTranslucent() || part.opacity != 1.0f) != translucent) { offset += (part.quadIndices.size() + part.triangleIndices.size()) * sizeof(int); continue; @@ -2366,7 +2389,7 @@ int Model::renderMeshesFromList(QVector& list, gpu::Batch& batch, RenderMod qDebug() << "NEW part.materialID:" << part.materialID; } - glm::vec4 diffuse = glm::vec4(part.diffuseColor, part.opacity); + glm::vec4 diffuse = glm::vec4(material->getDiffuse(), material->getOpacity()); if (locations->glowIntensity >= 0) { GLBATCH(glUniform1f)(locations->glowIntensity, glowEffect->getIntensity()); @@ -2374,11 +2397,18 @@ int Model::renderMeshesFromList(QVector& list, gpu::Batch& batch, RenderMod if (!(translucent && alphaThreshold == 0.0f)) { GLBATCH(glAlphaFunc)(GL_EQUAL, diffuse.a = glowEffect->getIntensity()); } - glm::vec4 specular = glm::vec4(part.specularColor, 1.0f); - GLBATCH(glMaterialfv)(GL_FRONT, GL_AMBIENT, (const float*)&diffuse); - GLBATCH(glMaterialfv)(GL_FRONT, GL_DIFFUSE, (const float*)&diffuse); - GLBATCH(glMaterialfv)(GL_FRONT, GL_SPECULAR, (const float*)&specular); - GLBATCH(glMaterialf)(GL_FRONT, GL_SHININESS, (part.shininess > 128.0f ? 128.0f: part.shininess)); + glm::vec4 specular = glm::vec4(material->getSpecular(), 1.0f); + float shininess = material->getShininess(); + shininess = (shininess > 128.0f ? 128.0f: shininess); + + if (locations->materialBufferUnit >= 0) { + batch.setUniformBuffer(locations->materialBufferUnit, material->getSchemaBuffer()); + } else { + GLBATCH(glMaterialfv)(GL_FRONT, GL_AMBIENT, (const float*)&diffuse); + GLBATCH(glMaterialfv)(GL_FRONT, GL_DIFFUSE, (const float*)&diffuse); + GLBATCH(glMaterialfv)(GL_FRONT, GL_SPECULAR, (const float*)&specular); + GLBATCH(glMaterialf)(GL_FRONT, GL_SHININESS, shininess); + } Texture* diffuseMap = networkPart.diffuseTexture.data(); if (mesh.isEye && diffuseMap) { diff --git a/libraries/render-utils/src/Model.h b/libraries/render-utils/src/Model.h index ef97e7dfd8..e10f218563 100644 --- a/libraries/render-utils/src/Model.h +++ b/libraries/render-utils/src/Model.h @@ -339,6 +339,7 @@ private: int emissiveTextureUnit; int emissiveParams; int glowIntensity; + int materialBufferUnit; }; static Locations _locations; diff --git a/libraries/render-utils/src/model.slf b/libraries/render-utils/src/model.slf index bbff368c2d..396935b4b8 100755 --- a/libraries/render-utils/src/model.slf +++ b/libraries/render-utils/src/model.slf @@ -13,6 +13,8 @@ <@include DeferredBufferWrite.slh@> +<@include Material.slh@> + // the diffuse texture uniform sampler2D diffuseMap; @@ -20,13 +22,15 @@ uniform sampler2D diffuseMap; varying vec4 normal; void main(void) { - // set the diffuse, normal, specular data + // Fetch diffuse map vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); + Material mat = getMaterial(); + packDeferredFragment( normalize(normal.xyz), - evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), - gl_Color.rgb * diffuse.rgb, - gl_FrontMaterial.specular.rgb, - gl_FrontMaterial.shininess); + evalOpaqueFinalAlpha(mat.getOpacity(), diffuse.a), + mat.getDiffuse()/* * diffuse.rgb*/, + mat.getSpecular(), + mat.getShininess()); } From d3edd14638d0be8233ca7970d67beb7bd8ac0064 Mon Sep 17 00:00:00 2001 From: dev Date: Tue, 13 Jan 2015 16:53:22 -0800 Subject: [PATCH 05/14] fixes for glsl mac --- libraries/render-utils/src/Material.slh | 10 ++++++---- libraries/render-utils/src/model.slf | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/libraries/render-utils/src/Material.slh b/libraries/render-utils/src/Material.slh index 4720b19d00..2ea7b2a12c 100755 --- a/libraries/render-utils/src/Material.slh +++ b/libraries/render-utils/src/Material.slh @@ -15,12 +15,14 @@ struct Material { vec4 _diffuse; vec4 _specular; - float getOpacity() { return _diffuse.a; } - vec3 getDiffuse() { return _diffuse.rgb; } - vec3 getSpecular() { return _specular.rgb; } - float getShininess() { return _specular.a; } }; +float getMaterialOpacity(Material m) { return m._diffuse.a; } +vec3 getMaterialDiffuse(Material m) { return m._diffuse.rgb; } +vec3 getMaterialSpecular(Material m) { return m._specular.rgb; } +float getMaterialShininess(Material m) { return m._specular.a; } + + <@if GLPROFILE == PC_GL@> uniform materialBuffer { Material mat; diff --git a/libraries/render-utils/src/model.slf b/libraries/render-utils/src/model.slf index 396935b4b8..bc6f127a77 100755 --- a/libraries/render-utils/src/model.slf +++ b/libraries/render-utils/src/model.slf @@ -29,8 +29,8 @@ void main(void) { packDeferredFragment( normalize(normal.xyz), - evalOpaqueFinalAlpha(mat.getOpacity(), diffuse.a), - mat.getDiffuse()/* * diffuse.rgb*/, - mat.getSpecular(), - mat.getShininess()); + evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), + getMaterialDiffuse(mat) * diffuse.rgb, + getMaterialSpecular(mat), + getMaterialShininess(mat)); } From d7ad5a35d69072d773feccc875d9528e112b6283 Mon Sep 17 00:00:00 2001 From: dev Date: Tue, 13 Jan 2015 18:24:32 -0800 Subject: [PATCH 06/14] fixes for glsl mac --- libraries/gpu/src/gpu/GLBackend.cpp | 8 ++++---- libraries/render-utils/src/Material.slh | 19 ++++++++++--------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/libraries/gpu/src/gpu/GLBackend.cpp b/libraries/gpu/src/gpu/GLBackend.cpp index f5f998d0d9..ed1b97a58f 100644 --- a/libraries/gpu/src/gpu/GLBackend.cpp +++ b/libraries/gpu/src/gpu/GLBackend.cpp @@ -494,13 +494,13 @@ void GLBackend::do_setUniformBuffer(Batch& batch, uint32 paramOffset) { #if defined(Q_OS_MAC) GLfloat* data = (GLfloat*) (uniformBuffer->getData() + rangeStart); glUniform4fv(slot, rangeSize / sizeof(GLfloat[4]), data); + + // NOT working so we ll stick to the uniform float array until we move to core profile + // GLuint bo = getBufferID(*uniformBuffer); + //glUniformBufferEXT(_shader._program, slot, bo); #else GLuint bo = getBufferID(*uniformBuffer); glBindBufferRange(GL_UNIFORM_BUFFER, slot, bo, rangeStart, rangeSize); - - // glUniformBufferEXT(_shader._program, slot, bo); - - //glBindBufferBase(GL_UNIFORM_BUFFER, slot, bo); #endif CHECK_GL_ERROR(); } diff --git a/libraries/render-utils/src/Material.slh b/libraries/render-utils/src/Material.slh index 2ea7b2a12c..e77b664ee1 100755 --- a/libraries/render-utils/src/Material.slh +++ b/libraries/render-utils/src/Material.slh @@ -36,8 +36,17 @@ Material getMaterial() { Material mat; mat._diffuse = materialBuffer[0]; mat._specular = materialBuffer[1]; - return mat; + return mat; } + <@else@> uniform vec4 materialBuffer[2]; Material getMaterial() { @@ -48,14 +57,6 @@ Material getMaterial() { } <@endif@> - <@endif@> \ No newline at end of file From 7be1f41659ca01574e103d8551eee7e8fd28e7ba Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Wed, 14 Jan 2015 10:22:27 -0800 Subject: [PATCH 07/14] using the Material class --- libraries/render-utils/src/model_lightmap.slf | 12 ++++++++---- .../src/model_lightmap_normal_map.slf | 13 ++++++++----- .../model_lightmap_normal_specular_map.slf | 18 ++++++++---------- .../src/model_lightmap_specular_map.slf | 18 ++++++++---------- .../render-utils/src/model_normal_map.slf | 19 +++++++++---------- .../src/model_normal_specular_map.slf | 18 +++++++++--------- .../render-utils/src/model_specular_map.slf | 19 +++++++++---------- 7 files changed, 59 insertions(+), 58 deletions(-) diff --git a/libraries/render-utils/src/model_lightmap.slf b/libraries/render-utils/src/model_lightmap.slf index 6b25921318..59836e9737 100755 --- a/libraries/render-utils/src/model_lightmap.slf +++ b/libraries/render-utils/src/model_lightmap.slf @@ -14,6 +14,8 @@ <@include DeferredBufferWrite.slh@> +<@include Material.slh@> + // the diffuse texture uniform sampler2D diffuseMap; @@ -32,11 +34,13 @@ void main(void) { vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); vec4 emissive = texture2D(emissiveMap, interpolatedTexcoord1.st); + Material mat = getMaterial(); + packDeferredFragmentLightmap( normalize(normal.xyz), - evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), - gl_Color.rgb * diffuse.rgb, - gl_FrontMaterial.specular.rgb, - gl_FrontMaterial.shininess, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), + getMaterialDiffuse(mat) * diffuse.rgb, + getMaterialSpecular(mat), + getMaterialShininess(mat), (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 9ef75802a8..6310ab0086 100755 --- a/libraries/render-utils/src/model_lightmap_normal_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_map.slf @@ -14,6 +14,8 @@ <@include DeferredBufferWrite.slh@> +<@include Material.slh@> + // the diffuse texture uniform sampler2D diffuseMap; @@ -45,12 +47,13 @@ void main(void) { vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); vec4 emissive = texture2D(emissiveMap, interpolatedTexcoord1.st); + Material mat = getMaterial(); + 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, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), + getMaterialDiffuse(mat) * diffuse.rgb, + getMaterialSpecular(mat), + getMaterialShininess(mat), (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 3bc72384f7..ab555ea0c0 100755 --- a/libraries/render-utils/src/model_lightmap_normal_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_normal_specular_map.slf @@ -14,6 +14,8 @@ <@include DeferredBufferWrite.slh@> +<@include Material.slh@> + // the diffuse texture uniform sampler2D diffuseMap; @@ -49,17 +51,13 @@ void main(void) { vec3 specular = texture2D(specularMap, gl_TexCoord[0].st).rgb; vec4 emissive = texture2D(emissiveMap, interpolatedTexcoord1.st); + Material mat = getMaterial(); + packDeferredFragmentLightmap( normalize(viewNormal.xyz), - evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), - gl_Color.rgb * diffuse.rgb, - specular, - gl_FrontMaterial.shininess, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), + getMaterialDiffuse(mat) * diffuse.rgb, + specular, // no use of getMaterialSpecular(mat) + getMaterialShininess(mat), (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 7322da6b3a..2973882e8a 100755 --- a/libraries/render-utils/src/model_lightmap_specular_map.slf +++ b/libraries/render-utils/src/model_lightmap_specular_map.slf @@ -14,6 +14,8 @@ <@include DeferredBufferWrite.slh@> +<@include Material.slh@> + // the diffuse texture uniform sampler2D diffuseMap; @@ -35,17 +37,13 @@ void main(void) { vec3 specular = texture2D(specularMap, gl_TexCoord[0].st).rgb; vec4 emissive = texture2D(emissiveMap, interpolatedTexcoord1.st); + Material mat = getMaterial(); + packDeferredFragmentLightmap( normalize(normal.xyz), - evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), - gl_Color.rgb * diffuse.rgb, - specular, - gl_FrontMaterial.shininess, + evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), + getMaterialDiffuse(mat) * diffuse.rgb, + specular, // no use of getMaterialSpecular(mat) + getMaterialShininess(mat), (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 74da394850..c6baa6f797 100755 --- a/libraries/render-utils/src/model_normal_map.slf +++ b/libraries/render-utils/src/model_normal_map.slf @@ -14,6 +14,8 @@ <@include DeferredBufferWrite.slh@> +<@include Material.slh@> + // the diffuse texture uniform sampler2D diffuseMap; @@ -34,18 +36,15 @@ void main(void) { vec3 localNormal = vec3(texture2D(normalMap, gl_TexCoord[0].st)) - vec3(0.5, 0.5, 0.5); vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); - - // 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[1] = viewNormal + vec4(0.5, 0.5, 0.5, 1.0); - gl_FragData[2] = vec4(gl_FrontMaterial.specular.rgb, gl_FrontMaterial.shininess / 128.0); - */ + + Material mat = getMaterial(); packDeferredFragment( normalize(viewNormal.xyz), - evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), - gl_Color.rgb * diffuse.rgb, - gl_FrontMaterial.specular.rgb, - gl_FrontMaterial.shininess); + evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), + getMaterialDiffuse(mat) * diffuse.rgb, + getMaterialSpecular(mat), + getMaterialShininess(mat)); } diff --git a/libraries/render-utils/src/model_normal_specular_map.slf b/libraries/render-utils/src/model_normal_specular_map.slf index 4248edfc90..8e7c9d3293 100755 --- a/libraries/render-utils/src/model_normal_specular_map.slf +++ b/libraries/render-utils/src/model_normal_specular_map.slf @@ -14,6 +14,8 @@ <@include DeferredBufferWrite.slh@> +<@include Material.slh@> + // the diffuse texture uniform sampler2D diffuseMap; @@ -41,15 +43,13 @@ 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] = 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); - */ + + Material mat = getMaterial(); + packDeferredFragment( normalize(viewNormal.xyz), - evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), - gl_Color.rgb * diffuse.rgb, - specular, - gl_FrontMaterial.shininess); + evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), + getMaterialDiffuse(mat) * diffuse.rgb, + specular, //getMaterialSpecular(mat), + getMaterialShininess(mat)); } diff --git a/libraries/render-utils/src/model_specular_map.slf b/libraries/render-utils/src/model_specular_map.slf index db1c2df9c5..bfcc69287d 100755 --- a/libraries/render-utils/src/model_specular_map.slf +++ b/libraries/render-utils/src/model_specular_map.slf @@ -14,6 +14,8 @@ <@include DeferredBufferWrite.slh@> +<@include Material.slh@> + // the diffuse texture uniform sampler2D diffuseMap; @@ -27,16 +29,13 @@ 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); - */ + + Material mat = getMaterial(); + packDeferredFragment( normalize(normal.xyz), - evalOpaqueFinalAlpha(gl_Color.a, diffuse.a), - gl_Color.rgb * diffuse.rgb, - specular, - gl_FrontMaterial.shininess); + evalOpaqueFinalAlpha(getMaterialOpacity(mat), diffuse.a), + getMaterialDiffuse(mat) * diffuse.rgb, + specular, //getMaterialSpecular(mat), + getMaterialShininess(mat)); } From 614e1aa6ae3e7e31b59a17da95bce01f574cabb2 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Wed, 14 Jan 2015 16:44:44 -0800 Subject: [PATCH 08/14] Cleaning the Model rendering path from any use of glMaterial --- .../render-utils/src/DeferredBufferWrite.slh | 6 ++++++ .../src/DeferredLightingEffect.cpp | 4 +++- libraries/render-utils/src/Material.slh | 1 + libraries/render-utils/src/model.slv | 2 +- libraries/render-utils/src/model_lightmap.slv | 2 +- .../src/model_lightmap_normal_map.slv | 2 +- .../render-utils/src/model_normal_map.slv | 2 +- .../render-utils/src/model_translucent.slf | 21 ++++++++++++++++++- libraries/render-utils/src/skin_model.slv | 4 ++-- .../src/skin_model_normal_map.slv | 4 ++-- 10 files changed, 38 insertions(+), 10 deletions(-) diff --git a/libraries/render-utils/src/DeferredBufferWrite.slh b/libraries/render-utils/src/DeferredBufferWrite.slh index 7b4ca7ba3b..066e0198b1 100755 --- a/libraries/render-utils/src/DeferredBufferWrite.slh +++ b/libraries/render-utils/src/DeferredBufferWrite.slh @@ -34,4 +34,10 @@ void packDeferredFragmentLightmap(vec3 normal, float alpha, vec3 diffuse, vec3 s gl_FragData[2] = vec4(emissive, shininess / 128.0); } +void packDeferredFragmentTranslucent(vec3 normal, float alpha, vec3 diffuse, vec3 specular, float shininess) { + 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); +} + <@endif@> diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index fc84489a2f..8200595ec2 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -169,7 +169,8 @@ void DeferredLightingEffect::render() { QOpenGLFramebufferObject* freeFBO = DependencyManager::get()->getFreeFramebufferObject(); freeFBO->bind(); glClear(GL_COLOR_BUFFER_BIT); - + glEnable(GL_FRAMEBUFFER_SRGB); + glBindTexture(GL_TEXTURE_2D, primaryFBO->texture()); glActiveTexture(GL_TEXTURE1); @@ -371,6 +372,7 @@ void DeferredLightingEffect::render() { glBindTexture(GL_TEXTURE_2D, 0); freeFBO->release(); + glDisable(GL_FRAMEBUFFER_SRGB); glDisable(GL_CULL_FACE); diff --git a/libraries/render-utils/src/Material.slh b/libraries/render-utils/src/Material.slh index e77b664ee1..923158af19 100755 --- a/libraries/render-utils/src/Material.slh +++ b/libraries/render-utils/src/Material.slh @@ -23,6 +23,7 @@ vec3 getMaterialSpecular(Material m) { return m._specular.rgb; } float getMaterialShininess(Material m) { return m._specular.a; } + <@if GLPROFILE == PC_GL@> uniform materialBuffer { Material mat; diff --git a/libraries/render-utils/src/model.slv b/libraries/render-utils/src/model.slv index 496667a062..1c3b03a304 100755 --- a/libraries/render-utils/src/model.slv +++ b/libraries/render-utils/src/model.slv @@ -23,7 +23,7 @@ void main(void) { normal = normalize(gl_ModelViewMatrix * vec4(gl_Normal, 0.0)); // pass along the diffuse color - gl_FrontColor = gl_Color * gl_FrontMaterial.diffuse; + gl_FrontColor = gl_Color; // and the texture coordinates gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); diff --git a/libraries/render-utils/src/model_lightmap.slv b/libraries/render-utils/src/model_lightmap.slv index 9f19ae8de2..23d99b399a 100755 --- a/libraries/render-utils/src/model_lightmap.slv +++ b/libraries/render-utils/src/model_lightmap.slv @@ -29,7 +29,7 @@ void main(void) { normal = normalize(gl_ModelViewMatrix * vec4(gl_Normal, 0.0)); // pass along the diffuse color - gl_FrontColor = gl_Color * gl_FrontMaterial.diffuse; + gl_FrontColor = gl_Color; // and the texture coordinates gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); diff --git a/libraries/render-utils/src/model_lightmap_normal_map.slv b/libraries/render-utils/src/model_lightmap_normal_map.slv index c19336ddae..14e1d52779 100755 --- a/libraries/render-utils/src/model_lightmap_normal_map.slv +++ b/libraries/render-utils/src/model_lightmap_normal_map.slv @@ -36,7 +36,7 @@ void main(void) { interpolatedTangent = gl_ModelViewMatrix * vec4(tangent, 0.0); // pass along the diffuse color - gl_FrontColor = gl_Color * gl_FrontMaterial.diffuse; + gl_FrontColor = gl_Color; // and the texture coordinates gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); diff --git a/libraries/render-utils/src/model_normal_map.slv b/libraries/render-utils/src/model_normal_map.slv index 0eb974912f..a27cd49171 100755 --- a/libraries/render-utils/src/model_normal_map.slv +++ b/libraries/render-utils/src/model_normal_map.slv @@ -31,7 +31,7 @@ void main(void) { interpolatedTangent = gl_ModelViewMatrix * vec4(tangent, 0.0); // pass along the diffuse color - gl_FrontColor = gl_Color * gl_FrontMaterial.diffuse; + gl_FrontColor = gl_Color; // and the texture coordinates gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); diff --git a/libraries/render-utils/src/model_translucent.slf b/libraries/render-utils/src/model_translucent.slf index 497f5962bc..4d1bf7bdec 100755 --- a/libraries/render-utils/src/model_translucent.slf +++ b/libraries/render-utils/src/model_translucent.slf @@ -12,10 +12,29 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +<@include DeferredBufferWrite.slh@> + +<@include Material.slh@> + // the diffuse texture uniform sampler2D diffuseMap; +varying vec4 normal; + void main(void) { + + // Fetch diffuse map + vec4 diffuse = texture2D(diffuseMap, gl_TexCoord[0].st); + + Material mat = getMaterial(); + + packDeferredFragmentTranslucent( + normalize(normal.xyz), + getMaterialOpacity(mat) * diffuse.a, + getMaterialDiffuse(mat) * diffuse.rgb, + getMaterialSpecular(mat), + getMaterialShininess(mat)); + // set the diffuse data - gl_FragData[0] = gl_Color * texture2D(diffuseMap, gl_TexCoord[0].st); + // gl_FragData[0] = gl_Color * texture2D(diffuseMap, gl_TexCoord[0].st); } diff --git a/libraries/render-utils/src/skin_model.slv b/libraries/render-utils/src/skin_model.slv index 4144198969..4cef8fddab 100755 --- a/libraries/render-utils/src/skin_model.slv +++ b/libraries/render-utils/src/skin_model.slv @@ -38,8 +38,8 @@ void main(void) { normal = normalize(gl_ModelViewMatrix * normal); // pass along the diffuse color - gl_FrontColor = gl_FrontMaterial.diffuse; - + gl_FrontColor = gl_Color; + // and the texture coordinates gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); diff --git a/libraries/render-utils/src/skin_model_normal_map.slv b/libraries/render-utils/src/skin_model_normal_map.slv index b021184591..c39f9cc0dd 100755 --- a/libraries/render-utils/src/skin_model_normal_map.slv +++ b/libraries/render-utils/src/skin_model_normal_map.slv @@ -46,8 +46,8 @@ void main(void) { interpolatedTangent = gl_ModelViewMatrix * interpolatedTangent; // pass along the diffuse color - gl_FrontColor = gl_FrontMaterial.diffuse; - + gl_FrontColor = gl_Color; + // and the texture coordinates gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); From 067483fce200a3bf8e09e8dfef79c323aac914b3 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 15 Jan 2015 11:54:01 -0800 Subject: [PATCH 09/14] Adding a prototype of ambient lighting with spherical harmonics --- libraries/render-utils/src/Material.slh | 2 +- libraries/render-utils/src/Model.cpp | 16 ++---- .../src/directional_light_shadow_map.slf | 53 ++++++++++++++++++- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/libraries/render-utils/src/Material.slh b/libraries/render-utils/src/Material.slh index 923158af19..86c6049ec6 100755 --- a/libraries/render-utils/src/Material.slh +++ b/libraries/render-utils/src/Material.slh @@ -60,4 +60,4 @@ Material getMaterial() { -<@endif@> \ No newline at end of file +<@endif@> diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 2b9418b82f..af2f67f429 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -2388,26 +2388,16 @@ int Model::renderMeshesFromList(QVector& list, gpu::Batch& batch, RenderMod qDebug() << "part INDEX:" << j; qDebug() << "NEW part.materialID:" << part.materialID; } - - glm::vec4 diffuse = glm::vec4(material->getDiffuse(), material->getOpacity()); - + if (locations->glowIntensity >= 0) { GLBATCH(glUniform1f)(locations->glowIntensity, glowEffect->getIntensity()); } if (!(translucent && alphaThreshold == 0.0f)) { - GLBATCH(glAlphaFunc)(GL_EQUAL, diffuse.a = glowEffect->getIntensity()); + GLBATCH(glAlphaFunc)(GL_EQUAL, glowEffect->getIntensity()); } - glm::vec4 specular = glm::vec4(material->getSpecular(), 1.0f); - float shininess = material->getShininess(); - shininess = (shininess > 128.0f ? 128.0f: shininess); - + if (locations->materialBufferUnit >= 0) { batch.setUniformBuffer(locations->materialBufferUnit, material->getSchemaBuffer()); - } else { - GLBATCH(glMaterialfv)(GL_FRONT, GL_AMBIENT, (const float*)&diffuse); - GLBATCH(glMaterialfv)(GL_FRONT, GL_DIFFUSE, (const float*)&diffuse); - GLBATCH(glMaterialfv)(GL_FRONT, GL_SPECULAR, (const float*)&specular); - GLBATCH(glMaterialf)(GL_FRONT, GL_SHININESS, shininess); } Texture* diffuseMap = networkPart.diffuseTexture.data(); diff --git a/libraries/render-utils/src/directional_light_shadow_map.slf b/libraries/render-utils/src/directional_light_shadow_map.slf index 1a781af131..1edf85b370 100644 --- a/libraries/render-utils/src/directional_light_shadow_map.slf +++ b/libraries/render-utils/src/directional_light_shadow_map.slf @@ -18,6 +18,40 @@ // Everything about shadow <@include Shadow.slh@> + +struct SphericalHarmonics { + vec4 L00; + vec4 L1m1; + vec4 L10; + vec4 L11; + vec4 L2m2; + vec4 L2m1; + vec4 L20; + vec4 L21; + vec4 L22; +}; + +vec4 evalSphericalLight(SphericalHarmonics sh, vec3 direction ) { + + const float C1 = 0.429043; + const float C2 = 0.511664; + const float C3 = 0.743125; + const float C4 = 0.886227; + const float C5 = 0.247708; + + vec4 value = C1 * sh.L22 * (direction.x * direction.x - direction.y * direction.y) + + C3 * sh.L20 * direction.z * direction.z + + C4 * sh.L00 - C5 * sh.L20 + + 2.0 * C1 * ( sh.L2m2 * direction.x * direction.y + + sh.L21 * direction.x * direction.z + + sh.L2m1 * direction.y * direction.z ) + + 2.0 * C2 * ( sh.L11 * direction.x + + sh.L1m1 * direction.y + + sh.L10 * direction.z ) ; + return value; +} + + void main(void) { DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); vec4 normalVal = frag.normalVal; @@ -31,6 +65,17 @@ void main(void) { // how much this fragment faces the light direction float diffuse = dot(frag.normal, gl_LightSource[0].position.xyz); + SphericalHarmonics sh; + sh.L00 = vec4( 0.79, 0.44, 0.54, 1.0); + sh.L1m1 = vec4( 0.39, 0.35, 0.60, 1.0); + sh.L10 = vec4(-0.34, -0.18, -0.27, 1.0); + sh.L11 = vec4(-0.29, -0.06, 0.01, 1.0); + sh.L2m2 = vec4(-0.11, -0.05, -0.12, 1.0); + sh.L2m1 = vec4(-0.26, -0.22, -0.47, 1.0); + sh.L20 = vec4(-0.16, -0.09, -0.15, 1.0); + sh.L21 = vec4( 0.56, 0.21, 0.14, 1.0); + sh.L22 = vec4( 0.21, -0.05, -0.30, 1.0); + // Light mapped or not ? if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { normalVal.a = 0.0; @@ -53,9 +98,15 @@ void main(void) { // average values from the shadow map float facingLight = step(0.0, diffuse) * shadowAttenuation; + vec4 ambienTerm = 0.5 * evalSphericalLight(sh, frag.normal); + + if (gl_FragCoord.x > 1024) { + ambienTerm = gl_FrontLightProduct[0].ambient.rgba; + } + // compute the base color based on OpenGL lighting model vec3 baseColor = diffuseVal.rgb * (gl_FrontLightModelProduct.sceneColor.rgb + - gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight)); + ambienTerm.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(frag.position.xyz)), From 7ae9635ce7e49c0444e4c9697b54460deff8400a Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 15 Jan 2015 15:27:18 -0800 Subject: [PATCH 10/14] Refactoring the code from directional into DeferredLighting.slh --- libraries/render-utils/src/DeferredBuffer.slh | 9 ++ .../render-utils/src/DeferredLighting.slh | 89 +++++++++++++++++++ .../src/DeferredLightingEffect.cpp | 6 ++ .../render-utils/src/DeferredLightingEffect.h | 19 ++++ .../render-utils/src/directional_light.slf | 11 +++ 5 files changed, 134 insertions(+) create mode 100755 libraries/render-utils/src/DeferredLighting.slh diff --git a/libraries/render-utils/src/DeferredBuffer.slh b/libraries/render-utils/src/DeferredBuffer.slh index da02ef750e..885fa96543 100755 --- a/libraries/render-utils/src/DeferredBuffer.slh +++ b/libraries/render-utils/src/DeferredBuffer.slh @@ -42,6 +42,10 @@ struct DeferredFragment { vec4 specularVal; vec4 position; vec3 normal; + vec3 diffuse; + float opacity; + vec3 specular; + float gloss; }; DeferredFragment unpackDeferredFragment(vec2 texcoord) { @@ -58,6 +62,11 @@ DeferredFragment unpackDeferredFragment(vec2 texcoord) { // Unpack the normal from the map frag.normal = normalize(frag.normalVal.xyz * 2.0 - vec3(1.0)); + frag.diffuse = frag.diffuseVal.xyz; + frag.opacity = frag.diffuseVal.w; + frag.specular = frag.specularVal.xyz; + frag.gloss = frag.specularVal.w; + return frag; } diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh new file mode 100755 index 0000000000..71021255dc --- /dev/null +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -0,0 +1,89 @@ + +<@if not DEFERRED_LIGHTING_SLH@> +<@def DEFERRED_LIGHTING_SLH@> + +struct SphericalHarmonics { + vec4 L00; + vec4 L1m1; + vec4 L10; + vec4 L11; + vec4 L2m2; + vec4 L2m1; + vec4 L20; + vec4 L21; + vec4 L22; +}; + +vec4 evalSphericalLight(SphericalHarmonics sh, vec3 direction ) { + + const float C1 = 0.429043; + const float C2 = 0.511664; + const float C3 = 0.743125; + const float C4 = 0.886227; + const float C5 = 0.247708; + + vec4 value = C1 * sh.L22 * (direction.x * direction.x - direction.y * direction.y) + + C3 * sh.L20 * direction.z * direction.z + + C4 * sh.L00 - C5 * sh.L20 + + 2.0 * C1 * ( sh.L2m2 * direction.x * direction.y + + sh.L21 * direction.x * direction.z + + sh.L2m1 * direction.y * direction.z ) + + 2.0 * C2 * ( sh.L11 * direction.x + + sh.L1m1 * direction.y + + sh.L10 * direction.z ) ; + return value; +} + +uniform SphericalHarmonics ambientSphere; + +vec3 evalAmbientSphereAndDirectionalColor(vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { + // Ambient lighting + vec3 ambientLight = evalSphericalLight(ambientSphere, normal).xyz; + if (gl_FragCoord.x > 1024) { + ambientLight = gl_FrontLightProduct[0].ambient.rgb; + } + vec3 ambientColor = diffuseVal.rgb * ambientLight; + + // Diffuse Lighting + float diffuseDot = dot(frag.normal, gl_LightSource[0].position.xyz); + float facingLight = step(0.0, diffuseDot); + vec3 diffuseColor = diffuse * (gl_FrontLightModelProduct.sceneColor.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuseDot * facingLight)); + + // compute the specular multiplier (sans exponent) + float specularPower = facingLight * max(0.0, + dot(normalize(gl_LightSource[0].position.xyz - normalize(position)), normal)); + vec3 specularColor = pow(specularPower, gloss * 128.0) * specular; + + // add specular contribution + return vec3(ambientColor + diffuseColor + specularColor); +} + + +vec3 evalAmbientAndDirectionalColor(vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { + // Ambient lighting + vec3 ambientLight = gl_FrontLightProduct[0].ambient.rgb; + vec3 ambientColor = diffuseVal.rgb * ambientLight; + + // Diffuse + float diffuseDot = dot(frag.normal, gl_LightSource[0].position.xyz); + float facingLight = step(0.0, diffuseDot); + vec3 diffuseColor = diffuse * (gl_FrontLightModelProduct.sceneColor.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuseDot * facingLight)); + + // compute the specular multiplier (sans exponent) + float specularPower = facingLight * max(0.0, + dot(normalize(gl_LightSource[0].position.xyz - normalize(position)), normal)); + vec3 specularColor = pow(specularPower, gloss * 128.0) * specular; + + // add specular contribution + return vec3(ambientColor + diffuseColor + specularColor); +} +<@endif@> \ No newline at end of file diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 3ce7c42a74..2d44e2c6bd 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -435,3 +435,9 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limit locations.radius = program.uniformLocation("radius"); program.release(); } + +void DeferredLightingEffect::setAmbientLightMode(int preset) { + if ((preset >= -1) && (preset < NUM_PRESET)) { + _ambientLightMode = preset; + } +} \ No newline at end of file diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 810aebd508..2c5087c5cb 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -71,6 +71,23 @@ public: void prepare(); void render(); + enum AmbientLightPreset { + OLD_TOWN_SQUARE = 0, + GRACE_CATHEDRAL, + EUCALYPTUS_GROVE, + ST_PETERS_BASILICA, + UFFIZI_GALLERY, + GALILEOS_TOMB, + VINE_STREET_KITCHEN, + BREEZEWAY, + CAMPUS_SUNSET, + FUNSTON_BEACH_SUNSET, + + NUM_PRESET, + }; + + void setAmbientLightMode(int preset); + private: DeferredLightingEffect() { } virtual ~DeferredLightingEffect() { } @@ -126,6 +143,8 @@ private: QVector _postLightingRenderables; AbstractViewStateInterface* _viewState; + + int _ambientLightMode = -1; }; /// Simple interface for objects that require something to be rendered after deferred lighting. diff --git a/libraries/render-utils/src/directional_light.slf b/libraries/render-utils/src/directional_light.slf index db963be913..733da47f8b 100644 --- a/libraries/render-utils/src/directional_light.slf +++ b/libraries/render-utils/src/directional_light.slf @@ -15,6 +15,8 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> +<@include DeferredLighting.slh@> + void main(void) { DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); @@ -26,6 +28,14 @@ void main(void) { if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { gl_FragColor = vec4(diffuseVal.rgb * specularVal.rgb, 1.0); } else { + glFragColor = vec4( evalAmbientAndDirectionalColor(frag.position.xyz, + frag.normal, + frag.diffuse, + frag.specular, + frag.gloss), + normalVal.a); + +/* // compute the base color based on OpenGL lighting model float diffuse = dot(frag.normal, gl_LightSource[0].position.xyz); float facingLight = step(0.0, diffuse); @@ -39,5 +49,6 @@ void main(void) { // add specular contribution vec4 specularColor = specularVal; gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normalVal.a); +*/ } } From d1fb071208202dc2517e5c7a28017978c2aed936 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Fri, 16 Jan 2015 09:38:53 -0800 Subject: [PATCH 11/14] Adding ambient sphere in the lighting equation and menu to control te presets --- interface/src/Application.cpp | 32 +++- interface/src/Application.h | 1 + interface/src/Menu.cpp | 15 ++ interface/src/Menu.h | 12 ++ .../render-utils/src/DeferredLighting.slh | 57 +++--- .../src/DeferredLightingEffect.cpp | 178 +++++++++++++++++- .../render-utils/src/DeferredLightingEffect.h | 11 +- .../src/directional_ambient_light.slf | 42 +++++ ...onal_ambient_light_cascaded_shadow_map.slf | 49 +++++ .../directional_ambient_light_shadow_map.slf | 50 +++++ .../render-utils/src/directional_light.slf | 42 ++--- .../directional_light_cascaded_shadow_map.slf | 54 ++---- .../src/directional_light_shadow_map.slf | 107 ++--------- 13 files changed, 469 insertions(+), 181 deletions(-) create mode 100755 libraries/render-utils/src/directional_ambient_light.slf create mode 100755 libraries/render-utils/src/directional_ambient_light_cascaded_shadow_map.slf create mode 100755 libraries/render-utils/src/directional_ambient_light_shadow_map.slf diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index dbc409bac2..ff6c87123f 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2797,7 +2797,7 @@ void Application::displaySide(Camera& theCamera, bool selfAvatarOnly, RenderArgs } glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); - + DependencyManager::get()->prepare(); if (!selfAvatarOnly) { @@ -2848,6 +2848,8 @@ void Application::displaySide(Camera& theCamera, bool selfAvatarOnly, RenderArgs { + DependencyManager::get()->setAmbientLightMode(getRenderAmbientLight()); + PROFILE_RANGE("DeferredLighting"); PerformanceTimer perfTimer("lighting"); DependencyManager::get()->render(); @@ -3973,3 +3975,31 @@ float Application::getRenderResolutionScale() const { return 1.0f; } } + +int Application::getRenderAmbientLight() const { + if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLightGlobal)) { + return -1; + } else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight0)) { + return 0; + } else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight1)) { + return 1; + } else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight2)) { + return 2; + } else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight3)) { + return 3; + } else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight4)) { + return 4; + } else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight5)) { + return 5; + } else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight6)) { + return 6; + } else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight7)) { + return 7; + } else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight8)) { + return 8; + } else if (Menu::getInstance()->isOptionChecked(MenuOption::RenderAmbientLight9)) { + return 9; + } else { + return -1; + } +} diff --git a/interface/src/Application.h b/interface/src/Application.h index a66a30abce..b87a4435b9 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -287,6 +287,7 @@ public: bool isLookingAtMyAvatar(Avatar* avatar); float getRenderResolutionScale() const; + int getRenderAmbientLight() const; unsigned int getRenderTargetFramerate() const; bool isVSyncOn() const; diff --git a/interface/src/Menu.cpp b/interface/src/Menu.cpp index c96ca9bb6f..38c7ab843f 100644 --- a/interface/src/Menu.cpp +++ b/interface/src/Menu.cpp @@ -343,6 +343,21 @@ Menu::Menu() : addCheckableActionToQMenuAndActionHash(renderOptionsMenu, MenuOption::AmbientOcclusion); addCheckableActionToQMenuAndActionHash(renderOptionsMenu, MenuOption::DontFadeOnOctreeServerChanges); addCheckableActionToQMenuAndActionHash(renderOptionsMenu, MenuOption::DisableAutoAdjustLOD); + + QMenu* ambientLightMenu = renderOptionsMenu->addMenu(MenuOption::RenderAmbientLight); + QActionGroup* ambientLightGroup = new QActionGroup(ambientLightMenu); + ambientLightGroup->setExclusive(true); + ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLightGlobal, 0, true)); + ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight0, 0, false)); + ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight1, 0, false)); + ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight2, 0, false)); + ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight3, 0, false)); + ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight4, 0, false)); + ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight5, 0, false)); + ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight6, 0, false)); + ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight7, 0, false)); + ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight8, 0, false)); + ambientLightGroup->addAction(addCheckableActionToQMenuAndActionHash(ambientLightMenu, MenuOption::RenderAmbientLight9, 0, false)); QMenu* shadowMenu = renderOptionsMenu->addMenu("Shadows"); QActionGroup* shadowGroup = new QActionGroup(shadowMenu); diff --git a/interface/src/Menu.h b/interface/src/Menu.h index 52fb17a10d..b2854f8131 100644 --- a/interface/src/Menu.h +++ b/interface/src/Menu.h @@ -440,6 +440,18 @@ namespace MenuOption { const QString RenderResolutionHalf = "1/2"; const QString RenderResolutionThird = "1/3"; const QString RenderResolutionQuarter = "1/4"; + const QString RenderAmbientLight = "Ambient Light"; + const QString RenderAmbientLightGlobal = "Global"; + const QString RenderAmbientLight0 = "OLD_TOWN_SQUARE"; + const QString RenderAmbientLight1 = "GRACE_CATHEDRAL"; + const QString RenderAmbientLight2 = "EUCALYPTUS_GROVE"; + const QString RenderAmbientLight3 = "ST_PETERS_BASILICA"; + const QString RenderAmbientLight4 = "UFFIZI_GALLERY"; + const QString RenderAmbientLight5 = "GALILEOS_TOMB"; + const QString RenderAmbientLight6 = "VINE_STREET_KITCHEN"; + const QString RenderAmbientLight7 = "BREEZEWAY"; + const QString RenderAmbientLight8 = "CAMPUS_SUNSET"; + const QString RenderAmbientLight9 = "FUNSTON_BEACH_SUNSET"; const QString ResetAvatarSize = "Reset Avatar Size"; const QString ResetSensors = "Reset Sensors"; const QString RunningScripts = "Running Scripts"; diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index 71021255dc..42d25a3666 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -45,17 +45,20 @@ vec4 evalSphericalLight(SphericalHarmonics sh, vec3 direction ) { uniform SphericalHarmonics ambientSphere; -vec3 evalAmbientSphereAndDirectionalColor(vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { - // Ambient lighting - vec3 ambientLight = evalSphericalLight(ambientSphere, normal).xyz; - if (gl_FragCoord.x > 1024) { - ambientLight = gl_FrontLightProduct[0].ambient.rgb; - } - vec3 ambientColor = diffuseVal.rgb * ambientLight; +vec3 evalAmbientColor(vec3 normal, vec3 diffuse, vec3 specular, float gloss) { + return diffuse.rgb * gl_FrontLightProduct[0].ambient.rgb; +} +vec3 evalAmbientSphereColor(vec3 normal, vec3 diffuse, vec3 specular, float gloss) { + vec3 ambientLight = 0.5 * evalSphericalLight(ambientSphere, normal).xyz; + + return diffuse.rgb * ambientLight; +} + +vec3 evalDirectionalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { // Diffuse Lighting - float diffuseDot = dot(frag.normal, gl_LightSource[0].position.xyz); - float facingLight = step(0.0, diffuseDot); + float diffuseDot = dot(normal, gl_LightSource[0].position.xyz); + float facingLight = step(0.0, diffuseDot) * shadowAttenuation; vec3 diffuseColor = diffuse * (gl_FrontLightModelProduct.sceneColor.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuseDot * facingLight)); // compute the specular multiplier (sans exponent) @@ -64,26 +67,30 @@ vec3 evalAmbientSphereAndDirectionalColor(vec3 position, vec3 normal, vec3 diffu vec3 specularColor = pow(specularPower, gloss * 128.0) * specular; // add specular contribution - return vec3(ambientColor + diffuseColor + specularColor); + return vec3(diffuseColor + specularColor); } -vec3 evalAmbientAndDirectionalColor(vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { - // Ambient lighting - vec3 ambientLight = gl_FrontLightProduct[0].ambient.rgb; - vec3 ambientColor = diffuseVal.rgb * ambientLight; - - // Diffuse - float diffuseDot = dot(frag.normal, gl_LightSource[0].position.xyz); - float facingLight = step(0.0, diffuseDot); - vec3 diffuseColor = diffuse * (gl_FrontLightModelProduct.sceneColor.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuseDot * facingLight)); +vec3 evalLightmappedColor(float shadowAttenuation, vec3 normal, vec3 diffuse, vec3 lightmap) { + + float diffuseDot = dot(normal, gl_LightSource[0].position.xyz); + + // need to catch normals perpendicular to the projection plane hence the magic number for the threshold + // it should be just 0, but we have innacurracy so we need to overshoot + const float PERPENDICULAR_THRESHOLD = -0.005; + float facingLight = step(PERPENDICULAR_THRESHOLD, diffuseDot); - // compute the specular multiplier (sans exponent) - float specularPower = facingLight * max(0.0, - dot(normalize(gl_LightSource[0].position.xyz - normalize(position)), normal)); - vec3 specularColor = pow(specularPower, gloss * 128.0) * specular; + // evaluate the shadow test but only relevant for light facing fragments + float lightAttenuation = (1 - facingLight) + facingLight * shadowAttenuation; + + // diffuse light is the lightmap dimmed by shadow + vec3 diffuseLight = lightAttenuation * lightmap; - // add specular contribution - return vec3(ambientColor + diffuseColor + specularColor); + // ambient is a tiny percentage of the lightmap and only when in the shadow + vec3 ambientLight = (1 - lightAttenuation) * 0.5 * lightmap; + + return diffuse * (ambientLight + diffuseLight); } + + <@endif@> \ No newline at end of file diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 2d44e2c6bd..a7b8d068bb 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -35,9 +35,150 @@ #include "directional_light_shadow_map_frag.h" #include "directional_light_cascaded_shadow_map_frag.h" +#include "directional_ambient_light_frag.h" +#include "directional_ambient_light_shadow_map_frag.h" +#include "directional_ambient_light_cascaded_shadow_map_frag.h" + #include "point_light_frag.h" #include "spot_light_frag.h" +class SphericalHarmonics { +public: + glm::vec3 L00 ; float spare0; + glm::vec3 L1m1 ; float spare1; + glm::vec3 L10 ; float spare2; + glm::vec3 L11 ; float spare3; + glm::vec3 L2m2 ; float spare4; + glm::vec3 L2m1 ; float spare5; + glm::vec3 L20 ; float spare6; + glm::vec3 L21 ; float spare7; + glm::vec3 L22 ; float spare8; + + void assignPreset(int p) { + switch (p) { + case DeferredLightingEffect::OLD_TOWN_SQUARE: { + L00 = glm::vec3( 0.871297f, 0.875222f, 0.864470f); + L1m1 = glm::vec3( 0.175058f, 0.245335f, 0.312891f); + L10 = glm::vec3( 0.034675f, 0.036107f, 0.037362f); + L11 = glm::vec3(-0.004629f,-0.029448f,-0.048028f); + L2m2 = glm::vec3(-0.120535f,-0.121160f,-0.117507f); + L2m1 = glm::vec3( 0.003242f, 0.003624f, 0.007511f); + L20 = glm::vec3(-0.028667f,-0.024926f,-0.020998f); + L21 = glm::vec3(-0.077539f,-0.086325f,-0.091591f); + L22 = glm::vec3(-0.161784f,-0.191783f,-0.219152f); + } + break; + case DeferredLightingEffect::GRACE_CATHEDRAL: { + L00 = glm::vec3( 0.79f, 0.44f, 0.54f); + L1m1 = glm::vec3( 0.39f, 0.35f, 0.60f); + L10 = glm::vec3(-0.34f, -0.18f, -0.27f); + L11 = glm::vec3(-0.29f, -0.06f, 0.01f); + L2m2 = glm::vec3(-0.11f, -0.05f, -0.12f); + L2m1 = glm::vec3(-0.26f, -0.22f, -0.47f); + L20 = glm::vec3(-0.16f, -0.09f, -0.15f); + L21 = glm::vec3( 0.56f, 0.21f, 0.14f); + L22 = glm::vec3( 0.21f, -0.05f, -0.30f); + } + break; + case DeferredLightingEffect::EUCALYPTUS_GROVE: { + L00 = glm::vec3( 0.38f, 0.43f, 0.45f); + L1m1 = glm::vec3( 0.29f, 0.36f, 0.41f); + L10 = glm::vec3( 0.04f, 0.03f, 0.01f); + L11 = glm::vec3(-0.10f, -0.10f, -0.09f); + L2m2 = glm::vec3(-0.06f, -0.06f, -0.04f); + L2m1 = glm::vec3( 0.01f, -0.01f, -0.05f); + L20 = glm::vec3(-0.09f, -0.13f, -0.15f); + L21 = glm::vec3(-0.06f, -0.05f, -0.04f); + L22 = glm::vec3( 0.02f, 0.00f, -0.05f); + } + break; + case DeferredLightingEffect::ST_PETERS_BASILICA: { + L00 = glm::vec3( 0.36f, 0.26f, 0.23f); + L1m1 = glm::vec3( 0.18f, 0.14f, 0.13f); + L10 = glm::vec3(-0.02f, -0.01f, 0.00f); + L11 = glm::vec3( 0.03f, 0.02f, -0.00f); + L2m2 = glm::vec3( 0.02f, 0.01f, -0.00f); + L2m1 = glm::vec3(-0.05f, -0.03f, -0.01f); + L20 = glm::vec3(-0.09f, -0.08f, -0.07f); + L21 = glm::vec3( 0.01f, 0.00f, 0.00f); + L22 = glm::vec3(-0.08f, -0.03f, -0.00f); + } + break; + case DeferredLightingEffect::UFFIZI_GALLERY: { + L00 = glm::vec3( 0.32f, 0.31f, 0.35f); + L1m1 = glm::vec3( 0.37f, 0.37f, 0.43f); + L10 = glm::vec3( 0.00f, 0.00f, 0.00f); + L11 = glm::vec3(-0.01f, -0.01f, -0.01f); + L2m2 = glm::vec3(-0.02f, -0.02f, -0.03f); + L2m1 = glm::vec3(-0.01f, -0.01f, -0.01f); + L20 = glm::vec3(-0.28f, -0.28f, -0.32f); + L21 = glm::vec3( 0.00f, 0.00f, 0.00f); + L22 = glm::vec3(-0.24f, -0.24f, -0.28f); + } + break; + case DeferredLightingEffect::GALILEOS_TOMB: { + L00 = glm::vec3( 1.04f, 0.76f, 0.71f); + L1m1 = glm::vec3( 0.44f, 0.34f, 0.34f); + L10 = glm::vec3(-0.22f, -0.18f, -0.17f); + L11 = glm::vec3( 0.71f, 0.54f, 0.56f); + L2m2 = glm::vec3( 0.64f, 0.50f, 0.52f); + L2m1 = glm::vec3(-0.12f, -0.09f, -0.08f); + L20 = glm::vec3(-0.37f, -0.28f, -0.32f); + L21 = glm::vec3(-0.17f, -0.13f, -0.13f); + L22 = glm::vec3( 0.55f, 0.42f, 0.42f); + } + break; + case DeferredLightingEffect::VINE_STREET_KITCHEN: { + L00 = glm::vec3( 0.64f, 0.67f, 0.73f); + L1m1 = glm::vec3( 0.28f, 0.32f, 0.33f); + L10 = glm::vec3( 0.42f, 0.60f, 0.77f); + L11 = glm::vec3(-0.05f, -0.04f, -0.02f); + L2m2 = glm::vec3(-0.10f, -0.08f, -0.05f); + L2m1 = glm::vec3( 0.25f, 0.39f, 0.53f); + L20 = glm::vec3( 0.38f, 0.54f, 0.71f); + L21 = glm::vec3( 0.06f, 0.01f, -0.02f); + L22 = glm::vec3(-0.03f, -0.02f, -0.03f); + } + break; + case DeferredLightingEffect::BREEZEWAY: { + L00 = glm::vec3( 0.32f, 0.36f, 0.38f); + L1m1 = glm::vec3( 0.37f, 0.41f, 0.45f); + L10 = glm::vec3(-0.01f, -0.01f, -0.01f); + L11 = glm::vec3(-0.10f, -0.12f, -0.12f); + L2m2 = glm::vec3(-0.13f, -0.15f, -0.17f); + L2m1 = glm::vec3(-0.01f, -0.02f, 0.02f); + L20 = glm::vec3(-0.07f, -0.08f, -0.09f); + L21 = glm::vec3( 0.02f, 0.03f, 0.03f); + L22 = glm::vec3(-0.29f, -0.32f, -0.36f); + } + break; + case DeferredLightingEffect::CAMPUS_SUNSET: { + L00 = glm::vec3( 0.79f, 0.94f, 0.98f); + L1m1 = glm::vec3( 0.44f, 0.56f, 0.70f); + L10 = glm::vec3(-0.10f, -0.18f, -0.27f); + L11 = glm::vec3( 0.45f, 0.38f, 0.20f); + L2m2 = glm::vec3( 0.18f, 0.14f, 0.05f); + L2m1 = glm::vec3(-0.14f, -0.22f, -0.31f); + L20 = glm::vec3(-0.39f, -0.40f, -0.36f); + L21 = glm::vec3( 0.09f, 0.07f, 0.04f); + L22 = glm::vec3( 0.67f, 0.67f, 0.52f); + } + break; + case DeferredLightingEffect::FUNSTON_BEACH_SUNSET: { + L00 = glm::vec3( 0.68f, 0.69f, 0.70f); + L1m1 = glm::vec3( 0.32f, 0.37f, 0.44f); + L10 = glm::vec3(-0.17f, -0.17f, -0.17f); + L11 = glm::vec3(-0.45f, -0.42f, -0.34f); + L2m2 = glm::vec3(-0.17f, -0.17f, -0.15f); + L2m1 = glm::vec3(-0.08f, -0.09f, -0.10f); + L20 = glm::vec3(-0.03f, -0.02f, -0.01f); + L21 = glm::vec3( 0.16f, 0.14f, 0.10f); + L22 = glm::vec3( 0.37f, 0.31f, 0.20f); + } + break; + } + } +}; void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) { _viewState = viewState; @@ -54,6 +195,13 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) { _directionalLightShadowMapLocations); loadLightProgram(directional_light_cascaded_shadow_map_frag, false, _directionalLightCascadedShadowMap, _directionalLightCascadedShadowMapLocations); + + loadLightProgram(directional_ambient_light_frag, false, _directionalAmbientSphereLight, _directionalAmbientSphereLightLocations); + loadLightProgram(directional_ambient_light_shadow_map_frag, false, _directionalAmbientSphereLightShadowMap, + _directionalAmbientSphereLightShadowMapLocations); + loadLightProgram(directional_ambient_light_cascaded_shadow_map_frag, false, _directionalAmbientSphereLightCascadedShadowMap, + _directionalAmbientSphereLightCascadedShadowMapLocations); + loadLightProgram(point_light_frag, true, _pointLight, _pointLightLocations); loadLightProgram(spot_light_frag, true, _spotLight, _spotLightLocations); } @@ -206,18 +354,43 @@ void DeferredLightingEffect::render() { if (_viewState->getCascadeShadowsEnabled()) { program = &_directionalLightCascadedShadowMap; locations = &_directionalLightCascadedShadowMapLocations; - _directionalLightCascadedShadowMap.bind(); - _directionalLightCascadedShadowMap.setUniform(locations->shadowDistances, _viewState->getShadowDistances()); + if (_ambientLightMode > -1) { + program = &_directionalAmbientSphereLightCascadedShadowMap; + locations = &_directionalAmbientSphereLightCascadedShadowMapLocations; + } + program->bind(); + program->setUniform(locations->shadowDistances, _viewState->getShadowDistances()); } else { + if (_ambientLightMode > -1) { + program = &_directionalAmbientSphereLightShadowMap; + locations = &_directionalAmbientSphereLightShadowMapLocations; + } program->bind(); } program->setUniformValue(locations->shadowScale, 1.0f / textureCache->getShadowFramebufferObject()->width()); } else { + if (_ambientLightMode > -1) { + program = &_directionalAmbientSphereLight; + locations = &_directionalAmbientSphereLightLocations; + } program->bind(); } + + if (locations->ambientSphere >= 0) { + SphericalHarmonics sh; + if (_ambientLightMode < NUM_PRESET) { + sh.assignPreset(_ambientLightMode); + } else { + sh.assignPreset(0); + } + + for (int i =0; i <9; i++) { + program->setUniformValue(locations->ambientSphere + i, *(((QVector4D*) &sh) + i)); + } + } float left, right, bottom, top, nearVal, farVal; glm::vec4 nearClipPlane, farClipPlane; @@ -433,6 +606,7 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limit locations.depthTexCoordOffset = program.uniformLocation("depthTexCoordOffset"); locations.depthTexCoordScale = program.uniformLocation("depthTexCoordScale"); locations.radius = program.uniformLocation("radius"); + locations.ambientSphere = program.uniformLocation("ambientSphere.L00"); program.release(); } diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 2c5087c5cb..cd8585eade 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -101,6 +101,7 @@ private: int depthTexCoordOffset; int depthTexCoordScale; int radius; + int ambientSphere; }; static void loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations); @@ -108,12 +109,20 @@ private: ProgramObject _simpleProgram; int _glowIntensityLocation; + ProgramObject _directionalAmbientSphereLight; + LightLocations _directionalAmbientSphereLightLocations; + ProgramObject _directionalAmbientSphereLightShadowMap; + LightLocations _directionalAmbientSphereLightShadowMapLocations; + ProgramObject _directionalAmbientSphereLightCascadedShadowMap; + LightLocations _directionalAmbientSphereLightCascadedShadowMapLocations; + ProgramObject _directionalLight; LightLocations _directionalLightLocations; ProgramObject _directionalLightShadowMap; LightLocations _directionalLightShadowMapLocations; ProgramObject _directionalLightCascadedShadowMap; LightLocations _directionalLightCascadedShadowMapLocations; + ProgramObject _pointLight; LightLocations _pointLightLocations; ProgramObject _spotLight; @@ -144,7 +153,7 @@ private: AbstractViewStateInterface* _viewState; - int _ambientLightMode = -1; + int _ambientLightMode = 0; }; /// Simple interface for objects that require something to be rendered after deferred lighting. diff --git a/libraries/render-utils/src/directional_ambient_light.slf b/libraries/render-utils/src/directional_ambient_light.slf new file mode 100755 index 0000000000..803bd5ac30 --- /dev/null +++ b/libraries/render-utils/src/directional_ambient_light.slf @@ -0,0 +1,42 @@ +<@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 +// + +// Everything about deferred buffer +<@include DeferredBuffer.slh@> + +<@include DeferredLighting.slh@> + +void main(void) { + DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); + + // Light mapped or not ? + if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { + gl_FragColor = vec4( evalLightmappedColor( + 1.0, + frag.normal, + frag.diffuse, + frag.specularVal.xyz), + 1.0); + } else { + vec3 color = evalAmbientSphereColor(frag.normal, frag.diffuse, frag.specular, frag.gloss) + + evalDirectionalColor(1.0, + frag.position.xyz, + frag.normal, + frag.diffuse, + frag.specular, + frag.gloss); + + gl_FragColor = vec4(color, frag.normalVal.a); + } +} diff --git a/libraries/render-utils/src/directional_ambient_light_cascaded_shadow_map.slf b/libraries/render-utils/src/directional_ambient_light_cascaded_shadow_map.slf new file mode 100755 index 0000000000..5f88c558d3 --- /dev/null +++ b/libraries/render-utils/src/directional_ambient_light_cascaded_shadow_map.slf @@ -0,0 +1,49 @@ +<@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 +// + +// Everything about deferred buffer +<@include DeferredBuffer.slh@> + +<@include DeferredLighting.slh@> + +// Everything about shadow +<@include Shadow.slh@> + +void main(void) { + DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); + + // Eval shadow Texcoord and then Attenuation + vec4 shadowTexcoord = evalCascadedShadowTexcoord(frag.position); + float shadowAttenuation = evalShadowAttenuation(shadowTexcoord); + + // Light mapped or not ? + if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { + gl_FragColor = vec4(evalLightmappedColor( + shadowAttenuation, + frag.normal, + frag.diffuse, + frag.specularVal.xyz), + 1.0); + } else { + vec3 color = evalAmbientSphereColor(frag.normal, frag.diffuse, frag.specular, frag.gloss) + + evalDirectionalColor(shadowAttenuation, + frag.position.xyz, + frag.normal, + frag.diffuse, + frag.specular, + frag.gloss); + + gl_FragColor = vec4(color, frag.normalVal.a); + } +} diff --git a/libraries/render-utils/src/directional_ambient_light_shadow_map.slf b/libraries/render-utils/src/directional_ambient_light_shadow_map.slf new file mode 100755 index 0000000000..6c241853e3 --- /dev/null +++ b/libraries/render-utils/src/directional_ambient_light_shadow_map.slf @@ -0,0 +1,50 @@ +<@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 +// + +// Everything about deferred buffer +<@include DeferredBuffer.slh@> + +<@include DeferredLighting.slh@> + +// Everything about shadow +<@include Shadow.slh@> + + +void main(void) { + DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); + + // Eval shadow Texcoord and then Attenuation + vec4 shadowTexcoord = evalShadowTexcoord(frag.position); + float shadowAttenuation = evalShadowAttenuation(shadowTexcoord); + + // Light mapped or not ? + if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { + gl_FragColor = vec4(evalLightmappedColor( + shadowAttenuation, + frag.normal, + frag.diffuse, + frag.specularVal.xyz), + 1.0); + } else { + vec3 color = evalAmbientSphereColor(frag.normal, frag.diffuse, frag.specular, frag.gloss) + + evalDirectionalColor(shadowAttenuation, + frag.position.xyz, + frag.normal, + frag.diffuse, + frag.specular, + frag.gloss); + + gl_FragColor = vec4(color, frag.normalVal.a); + } +} diff --git a/libraries/render-utils/src/directional_light.slf b/libraries/render-utils/src/directional_light.slf index 733da47f8b..8ff6cd6c87 100644 --- a/libraries/render-utils/src/directional_light.slf +++ b/libraries/render-utils/src/directional_light.slf @@ -20,35 +20,23 @@ void main(void) { DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); - vec4 normalVal = frag.normalVal; - vec4 diffuseVal = frag.diffuseVal; - vec4 specularVal = frag.specularVal; - // Light mapped or not ? - if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { - gl_FragColor = vec4(diffuseVal.rgb * specularVal.rgb, 1.0); + if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { + gl_FragColor = vec4( evalLightmappedColor( + 1.0, + frag.normal, + frag.diffuse, + frag.specularVal.xyz), + 1.0); } else { - glFragColor = vec4( evalAmbientAndDirectionalColor(frag.position.xyz, - frag.normal, - frag.diffuse, - frag.specular, - frag.gloss), - normalVal.a); + vec3 color = evalAmbientColor(frag.normal, frag.diffuse, frag.specular, frag.gloss) + + evalDirectionalColor(1.0, + frag.position.xyz, + frag.normal, + frag.diffuse, + frag.specular, + frag.gloss); -/* - // compute the base color based on OpenGL lighting model - float diffuse = dot(frag.normal, 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(frag.position.xyz)), - frag.normal)); - - // add specular contribution - vec4 specularColor = specularVal; - gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normalVal.a); -*/ + gl_FragColor = vec4(color, frag.normalVal.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 index f7e21252fb..ccf8909b64 100644 --- a/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf +++ b/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf @@ -15,55 +15,35 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> +<@include DeferredLighting.slh@> + // Everything about shadow <@include Shadow.slh@> void main(void) { DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); - vec4 normalVal = frag.normalVal; - vec4 diffuseVal = frag.diffuseVal; - vec4 specularVal = frag.specularVal; // Eval shadow Texcoord and then Attenuation vec4 shadowTexcoord = evalCascadedShadowTexcoord(frag.position); float shadowAttenuation = evalShadowAttenuation(shadowTexcoord); - // how much this fragment faces the light direction - float diffuse = dot(frag.normal, gl_LightSource[0].position.xyz); - // Light mapped or not ? - if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { - normalVal.a = 1.0; - - // need to catch normals perpendicular to the projection plane hence the magic number for the threshold - // it should be just 0, but we have innacurracy so we need to overshoot - const float PERPENDICULAR_THRESHOLD = -0.005; - float facingLight = step(PERPENDICULAR_THRESHOLD, diffuse); - - // evaluate the shadow test but only relevant for light facing fragments - float lightAttenuation = (1 - facingLight) + facingLight * shadowAttenuation; - - // diffuse light is the lightmap dimmed by shadow - vec3 diffuseLight = lightAttenuation * specularVal.rgb; - // ambient is a tiny percentage of the lightmap and only when in the shadow - vec3 ambientLight = (1 - lightAttenuation) * 0.5 * specularVal.rgb; - - gl_FragColor = vec4(diffuseVal.rgb * (ambientLight + diffuseLight), 1.0); + if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { + gl_FragColor = vec4(evalLightmappedColor( + shadowAttenuation, + frag.normal, + frag.diffuse, + frag.specularVal.xyz), + 1.0); } else { + vec3 color = evalAmbientColor(frag.normal, frag.diffuse, frag.specular, frag.gloss) + + evalDirectionalColor(shadowAttenuation, + frag.position.xyz, + frag.normal, + frag.diffuse, + frag.specular, + frag.gloss); - // average values from the shadow map - float facingLight = step(0.0, diffuse) * shadowAttenuation; - - // compute the base color based on OpenGL lighting model - 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(frag.position.xyz)), - frag.normal)); - - // add specular contribution - vec4 specularColor = specularVal; - gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normalVal.a); + gl_FragColor = vec4(color, frag.normalVal.a); } } diff --git a/libraries/render-utils/src/directional_light_shadow_map.slf b/libraries/render-utils/src/directional_light_shadow_map.slf index 1edf85b370..13435e9101 100644 --- a/libraries/render-utils/src/directional_light_shadow_map.slf +++ b/libraries/render-utils/src/directional_light_shadow_map.slf @@ -15,105 +15,36 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> +<@include DeferredLighting.slh@> + // Everything about shadow -<@include Shadow.slh@> - - -struct SphericalHarmonics { - vec4 L00; - vec4 L1m1; - vec4 L10; - vec4 L11; - vec4 L2m2; - vec4 L2m1; - vec4 L20; - vec4 L21; - vec4 L22; -}; - -vec4 evalSphericalLight(SphericalHarmonics sh, vec3 direction ) { - - const float C1 = 0.429043; - const float C2 = 0.511664; - const float C3 = 0.743125; - const float C4 = 0.886227; - const float C5 = 0.247708; - - vec4 value = C1 * sh.L22 * (direction.x * direction.x - direction.y * direction.y) + - C3 * sh.L20 * direction.z * direction.z + - C4 * sh.L00 - C5 * sh.L20 + - 2.0 * C1 * ( sh.L2m2 * direction.x * direction.y + - sh.L21 * direction.x * direction.z + - sh.L2m1 * direction.y * direction.z ) + - 2.0 * C2 * ( sh.L11 * direction.x + - sh.L1m1 * direction.y + - sh.L10 * direction.z ) ; - return value; -} +<@include Shadow.slh@> void main(void) { DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); - vec4 normalVal = frag.normalVal; - vec4 diffuseVal = frag.diffuseVal; - vec4 specularVal = frag.specularVal; // Eval shadow Texcoord and then Attenuation vec4 shadowTexcoord = evalShadowTexcoord(frag.position); float shadowAttenuation = evalShadowAttenuation(shadowTexcoord); - // how much this fragment faces the light direction - float diffuse = dot(frag.normal, gl_LightSource[0].position.xyz); - - SphericalHarmonics sh; - sh.L00 = vec4( 0.79, 0.44, 0.54, 1.0); - sh.L1m1 = vec4( 0.39, 0.35, 0.60, 1.0); - sh.L10 = vec4(-0.34, -0.18, -0.27, 1.0); - sh.L11 = vec4(-0.29, -0.06, 0.01, 1.0); - sh.L2m2 = vec4(-0.11, -0.05, -0.12, 1.0); - sh.L2m1 = vec4(-0.26, -0.22, -0.47, 1.0); - sh.L20 = vec4(-0.16, -0.09, -0.15, 1.0); - sh.L21 = vec4( 0.56, 0.21, 0.14, 1.0); - sh.L22 = vec4( 0.21, -0.05, -0.30, 1.0); - // Light mapped or not ? - if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { - normalVal.a = 0.0; + if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { + gl_FragColor = vec4(evalLightmappedColor( + shadowAttenuation, + frag.normal, + frag.diffuse, + frag.specularVal.xyz), + 1.0); + } else { + vec3 color = evalAmbientColor(frag.normal, frag.diffuse, frag.specular, frag.gloss) + + evalDirectionalColor(shadowAttenuation, + frag.position.xyz, + frag.normal, + frag.diffuse, + frag.specular, + frag.gloss); - // need to catch normals perpendicular to the projection plane hence the magic number for the threshold - // it should be just 0, be we have innacurracy so we need to overshoot - const float PERPENDICULAR_THRESHOLD = -0.005; - float facingLight = step(PERPENDICULAR_THRESHOLD, diffuse); - - // evaluate the shadow test but only relevant for light facing fragments - float lightAttenuation = (1 - facingLight) + facingLight * shadowAttenuation; - - // diffuse light is the lightmap dimmed by shadow - vec3 diffuseLight = lightAttenuation * specularVal.rgb; - // ambient is a tiny percentage of the lightmap and only when in the shadow - vec3 ambientLight = (1 - lightAttenuation) * 0.5 * specularVal.rgb; - - gl_FragColor = vec4(diffuseVal.rgb * (ambientLight + diffuseLight), 1.0); - } else { - // average values from the shadow map - float facingLight = step(0.0, diffuse) * shadowAttenuation; - - vec4 ambienTerm = 0.5 * evalSphericalLight(sh, frag.normal); - - if (gl_FragCoord.x > 1024) { - ambienTerm = gl_FrontLightProduct[0].ambient.rgba; - } - - // compute the base color based on OpenGL lighting model - vec3 baseColor = diffuseVal.rgb * (gl_FrontLightModelProduct.sceneColor.rgb + - ambienTerm.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(frag.position.xyz)), - frag.normal)); - - // add specular contribution - vec4 specularColor = specularVal; - gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normalVal.a); + gl_FragColor = vec4(color, frag.normalVal.a); } } From 0e38ea88545b0dd41082daf1defdf964c4cb93a9 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Fri, 16 Jan 2015 09:41:43 -0800 Subject: [PATCH 12/14] Adding ambient sphere in the lighting equation and menu to control te presets --- libraries/render-utils/src/DeferredLighting.slh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index 42d25a3666..5582ab8e77 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -93,4 +93,4 @@ vec3 evalLightmappedColor(float shadowAttenuation, vec3 normal, vec3 diffuse, ve } -<@endif@> \ No newline at end of file +<@endif@> From 8634d86167f0371dac0ce6e25a1871454372bd6c Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Fri, 16 Jan 2015 09:43:21 -0800 Subject: [PATCH 13/14] Adding ambient sphere in the lighting equation and menu to control te presets --- libraries/render-utils/src/DeferredLightingEffect.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index a7b8d068bb..0bb3cd9220 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -614,4 +614,4 @@ void DeferredLightingEffect::setAmbientLightMode(int preset) { if ((preset >= -1) && (preset < NUM_PRESET)) { _ambientLightMode = preset; } -} \ No newline at end of file +} From 6b55b4ff821a83473f666ec0a2716b624e64b7cf Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Fri, 16 Jan 2015 10:26:51 -0800 Subject: [PATCH 14/14] No more Magic numbers and fixing the code path for linux --- libraries/gpu/src/gpu/GLBackend.cpp | 5 ++++- libraries/render-utils/src/DeferredLightingEffect.cpp | 4 +++- libraries/render-utils/src/Model.cpp | 9 ++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/libraries/gpu/src/gpu/GLBackend.cpp b/libraries/gpu/src/gpu/GLBackend.cpp index ed1b97a58f..a01b39c35f 100644 --- a/libraries/gpu/src/gpu/GLBackend.cpp +++ b/libraries/gpu/src/gpu/GLBackend.cpp @@ -498,9 +498,12 @@ void GLBackend::do_setUniformBuffer(Batch& batch, uint32 paramOffset) { // NOT working so we ll stick to the uniform float array until we move to core profile // GLuint bo = getBufferID(*uniformBuffer); //glUniformBufferEXT(_shader._program, slot, bo); -#else +#elif defined(Q_OS_WIN) GLuint bo = getBufferID(*uniformBuffer); glBindBufferRange(GL_UNIFORM_BUFFER, slot, bo, rangeStart, rangeSize); +#else + GLfloat* data = (GLfloat*) (uniformBuffer->getData() + rangeStart); + glUniform4fv(slot, rangeSize / sizeof(GLfloat[4]), data); #endif CHECK_GL_ERROR(); } diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 0bb3cd9220..5cbdd04f64 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -54,6 +54,8 @@ public: glm::vec3 L21 ; float spare7; glm::vec3 L22 ; float spare8; + static const int NUM_COEFFICIENTS = 9; + void assignPreset(int p) { switch (p) { case DeferredLightingEffect::OLD_TOWN_SQUARE: { @@ -387,7 +389,7 @@ void DeferredLightingEffect::render() { sh.assignPreset(0); } - for (int i =0; i <9; i++) { + for (int i =0; i setUniformValue(locations->ambientSphere + i, *(((QVector4D*) &sh) + i)); } } diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 1bca37aa10..97e6124517 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -206,7 +206,7 @@ void Model::initProgram(ProgramObject& program, Model::Locations& locations, boo } else { locations.materialBufferUnit = -1; } -#else +#elif defined(Q_OS_WIN) loc = glGetUniformBlockIndex(program.programId(), "materialBuffer"); if (loc >= 0) { glUniformBlockBinding(program.programId(), loc, 1); @@ -214,6 +214,13 @@ void Model::initProgram(ProgramObject& program, Model::Locations& locations, boo } else { locations.materialBufferUnit = -1; } +#else + loc = program.uniformLocation("materialBuffer"); + if (loc >= 0) { + locations.materialBufferUnit = loc; + } else { + locations.materialBufferUnit = -1; + } #endif if (!program.isLinked()) {