From ae3233b11918904aa57bccd56165be44082537f0 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Tue, 27 Jan 2015 15:44:10 -0800 Subject: [PATCH 01/24] in the middle of something --- libraries/model/src/model/Geometry.h | 3 +- libraries/model/src/model/Material.h | 1 - .../render-utils/src/DeferredLighting.slh | 2 +- .../src/DeferredLightingEffect.cpp | 103 ++++++++++++++---- .../render-utils/src/DeferredLightingEffect.h | 13 ++- libraries/render-utils/src/point_light.slf | 11 +- libraries/render-utils/src/spot_light.slf | 16 ++- libraries/shared/src/Transform.h | 9 ++ 8 files changed, 130 insertions(+), 28 deletions(-) diff --git a/libraries/model/src/model/Geometry.h b/libraries/model/src/model/Geometry.h index 0beaa20a83..fbd485e6a0 100755 --- a/libraries/model/src/model/Geometry.h +++ b/libraries/model/src/model/Geometry.h @@ -23,6 +23,7 @@ typedef gpu::BufferView::Index Index; typedef gpu::BufferView BufferView; typedef AABox Box; typedef std::vector< Box > Boxes; +typedef glm::vec3 Vec3; class Mesh { public: @@ -35,7 +36,7 @@ public: typedef gpu::Stream::Format VertexFormat; typedef std::map< Slot, BufferView > BufferViewMap; - typedef glm::vec3 Vec3; + typedef model::Vec3 Vec3; Mesh(); Mesh(const Mesh& mesh); diff --git a/libraries/model/src/model/Material.h b/libraries/model/src/model/Material.h index efd2f35968..57e0f68a9c 100755 --- a/libraries/model/src/model/Material.h +++ b/libraries/model/src/model/Material.h @@ -100,7 +100,6 @@ public: void setTextureView(MapChannel channel, const TextureView& texture); const TextureMap& getTextureMap() const { return _textureMap; } - const Schema* getSchema() const { return &_schemaBuffer.get(); } protected: Flags _flags; diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index 9e421c03c9..cd65fd1053 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -59,7 +59,7 @@ vec3 evalDirectionalColor(float shadowAttenuation, vec3 position, vec3 normal, v // Diffuse Lighting 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)); + vec3 diffuseColor = diffuse * (gl_FrontLightProduct[0].diffuse.rgb * (diffuseDot * facingLight)); // compute the specular multiplier (sans exponent) float specularPower = facingLight * max(0.0, diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 5cbdd04f64..58e90f57d1 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -25,6 +25,9 @@ #include "RenderUtil.h" #include "TextureCache.h" +#include "gpu/Batch.h" +#include "gpu/GLBackend.h" + #include "simple_vert.h" #include "simple_frag.h" @@ -269,7 +272,14 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu light.specular = glm::vec4(specular, 1.0f); light.constantAttenuation = constantAttenuation; light.linearAttenuation = linearAttenuation; - _pointLights.append(light); + + model::LightPointer lp = model::LightPointer(new model::Light()); + lp->setPosition(position); + lp->setAttenuationRadius(radius); + lp->setColor(diffuse); + lp->setIntensity(1.0f); + lp->setType(model::Light::POINT); + _pointLights.push_back(lp); } else { SpotLight light; @@ -283,7 +293,17 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu light.direction = direction; light.exponent = exponent; light.cutoff = cutoff; - _spotLights.append(light); + + model::LightPointer ls = model::LightPointer(new model::Light()); + ls->setPosition(position); + ls->setDirection(direction); + ls->setAttenuationRadius(radius); + ls->setSpotCone(cutoff); + ls->setColor(diffuse); + ls->setIntensity(1.0f); + ls->setType(model::Light::SPOT); + + _spotLights.push_back(ls); } } @@ -436,14 +456,24 @@ void DeferredLightingEffect::render() { auto geometryCache = DependencyManager::get(); - if (!_pointLights.isEmpty()) { + if (!_pointLights.empty()) { _pointLight.bind(); _pointLight.setUniformValue(_pointLightLocations.nearLocation, nearVal); _pointLight.setUniformValue(_pointLightLocations.depthScale, depthScale); _pointLight.setUniformValue(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); _pointLight.setUniformValue(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); - foreach (const PointLight& light, _pointLights) { + for (auto light : _pointLights) { + // foreach (const PointLight& light, _pointLights) { + if (_pointLightLocations.lightBufferUnit >= 0) { + gpu::Batch batch; + batch.setUniformBuffer(_pointLightLocations.lightBufferUnit, light->getSchemaBuffer()); + gpu::GLBackend::renderBatch(batch); + } + _spotLight.setUniformValue(_pointLightLocations.radius, light->getAttenuationRadius()); + + +/* _pointLight.setUniformValue(_pointLightLocations.radius, light.radius); glLightfv(GL_LIGHT1, GL_AMBIENT, (const GLfloat*)&light.ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, (const GLfloat*)&light.diffuse); @@ -452,11 +482,11 @@ void DeferredLightingEffect::render() { glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, (light.constantAttenuation > 0.0f ? light.constantAttenuation : 0.0f)); glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, (light.linearAttenuation > 0.0f ? light.linearAttenuation : 0.0f)); glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, (light.quadraticAttenuation > 0.0f ? light.quadraticAttenuation : 0.0f)); - + */ glPushMatrix(); - float expandedRadius = light.radius * (1.0f + SCALE_EXPANSION); - if (glm::distance(eyePoint, glm::vec3(light.position)) < expandedRadius + nearRadius) { + float expandedRadius = light->getAttenuationRadius() * (1.0f + SCALE_EXPANSION); + if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius) { glLoadIdentity(); glTranslatef(0.0f, 0.0f, -1.0f); @@ -470,7 +500,7 @@ void DeferredLightingEffect::render() { glMatrixMode(GL_MODELVIEW); } else { - glTranslatef(light.position.x, light.position.y, light.position.z); + glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z); geometryCache->renderSphere(expandedRadius, 32, 32); } @@ -481,15 +511,24 @@ void DeferredLightingEffect::render() { _pointLight.release(); } - if (!_spotLights.isEmpty()) { + if (!_spotLights.empty()) { _spotLight.bind(); _spotLight.setUniformValue(_spotLightLocations.nearLocation, nearVal); _spotLight.setUniformValue(_spotLightLocations.depthScale, depthScale); _spotLight.setUniformValue(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); _spotLight.setUniformValue(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); - foreach (const SpotLight& light, _spotLights) { - _spotLight.setUniformValue(_spotLightLocations.radius, light.radius); + for (auto light : _spotLights) { + // foreach (const SpotLight& light, _spotLights) { + if (_spotLightLocations.lightBufferUnit >= 0) { + gpu::Batch batch; + batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer()); + gpu::GLBackend::renderBatch(batch); + } + _spotLight.setUniformValue(_spotLightLocations.radius, light->getAttenuationRadius()); + + /* + _spotLight.setUniformValue(_spotLightLocations.radius, light.radius); glLightfv(GL_LIGHT1, GL_AMBIENT, (const GLfloat*)&light.ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, (const GLfloat*)&light.diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR, (const GLfloat*)&light.specular); @@ -500,12 +539,13 @@ void DeferredLightingEffect::render() { glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, (const GLfloat*)&light.direction); glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, light.exponent); glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, glm::degrees(light.cutoff)); - + */ + glPushMatrix(); - float expandedRadius = light.radius * (1.0f + SCALE_EXPANSION); - float edgeRadius = expandedRadius / glm::cos(light.cutoff); - if (glm::distance(eyePoint, glm::vec3(light.position)) < edgeRadius + nearRadius) { + float expandedRadius = light->getAttenuationRadius() * (1.0f + SCALE_EXPANSION); + float edgeRadius = expandedRadius / glm::cos(light->getSpotConeAngle()); + if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < edgeRadius + nearRadius) { glLoadIdentity(); glTranslatef(0.0f, 0.0f, -1.0f); @@ -519,12 +559,12 @@ void DeferredLightingEffect::render() { glMatrixMode(GL_MODELVIEW); } else { - glTranslatef(light.position.x, light.position.y, light.position.z); - glm::quat spotRotation = rotationBetween(glm::vec3(0.0f, 0.0f, -1.0f), light.direction); + glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z); + glm::quat spotRotation = rotationBetween(glm::vec3(0.0f, 0.0f, -1.0f), light->getDirection()); glm::vec3 axis = glm::axis(spotRotation); glRotatef(glm::degrees(glm::angle(spotRotation)), axis.x, axis.y, axis.z); - glTranslatef(0.0f, 0.0f, -light.radius * (1.0f + SCALE_EXPANSION * 0.5f)); - geometryCache->renderCone(expandedRadius * glm::tan(light.cutoff), + glTranslatef(0.0f, 0.0f, -light->getAttenuationRadius() * (1.0f + SCALE_EXPANSION * 0.5f)); + geometryCache->renderCone(expandedRadius * glm::tan(light->getSpotConeAngle()), expandedRadius, 32, 1); } @@ -609,6 +649,31 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limit locations.depthTexCoordScale = program.uniformLocation("depthTexCoordScale"); locations.radius = program.uniformLocation("radius"); locations.ambientSphere = program.uniformLocation("ambientSphere.L00"); + + GLint loc = -1; +#if defined(Q_OS_MAC) + loc = program.uniformLocation("lightBuffer"); + if (loc >= 0) { + locations.lightBufferUnit = loc; + } else { + locations.lightBufferUnit = -1; + } +#elif defined(Q_OS_WIN) + loc = glGetUniformBlockIndex(program.programId(), "lightBuffer"); + if (loc >= 0) { + glUniformBlockBinding(program.programId(), loc, 1); + locations.lightBufferUnit = 1; + } else { + locations.lightBufferUnit = -1; + } +#else + loc = program.uniformLocation("lightBuffer"); + if (loc >= 0) { + locations.lightBufferUnit = loc; + } else { + locations.lightBufferUnit = -1; + } +#endif program.release(); } diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index cd8585eade..5c39c3041e 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -19,6 +19,8 @@ #include "ProgramObject.h" +#include "model/Light.h" + class AbstractViewStateInterface; class PostLightingRenderable; @@ -102,6 +104,7 @@ private: int depthTexCoordScale; int radius; int ambientSphere; + int lightBufferUnit; }; static void loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations); @@ -146,9 +149,13 @@ private: float exponent; float cutoff; }; - - QVector _pointLights; - QVector _spotLights; + + typedef std::vector< model::LightPointer > Lights; + + Lights _pointLights; + Lights _spotLights; + // QVector _pointLights; + // QVector _spotLights; QVector _postLightingRenderables; AbstractViewStateInterface* _viewState; diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index e5142b25ce..9d9aecda95 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -15,16 +15,23 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> +// Everything about light +<@include Light.slh@> + // the radius (hard cutoff) of the light effect uniform float radius; void main(void) { // get the depth and exit early if it doesn't pass the test vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; - float depth = texture2D(depthMap, texCoord).r; + + DeferredFragment frag = unpackDeferredFragment(texCoord); + + float depth = frag.depthVal; if (depth < gl_FragCoord.z) { discard; } + // compute the view space position using the depth float z = near / (depth * depthScale - 1.0); vec4 position = vec4((depthTexCoordOffset + texCoord * depthTexCoordScale) * z, z, 1.0); @@ -52,4 +59,6 @@ void main(void) { normalizedNormal)); vec4 specularColor = texture2D(specularMap, texCoord); gl_FragColor = vec4((baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb) * attenuation, 0.0); + + gl_FragColor = vec4(frag.normal, 0.0); } diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index f987760eb8..48e8ab0d61 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -15,20 +15,30 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> +// Everything about light +<@include Light.slh@> + // the radius (hard cutoff) of the light effect uniform float radius; void main(void) { - // get the depth and exit early if it doesn't pass the test vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; - float depth = texture2D(depthMap, texCoord).r; + + DeferredFragment frag = unpackDeferredFragment(texCoord); + + // get the depth and exit early if it doesn't pass the test + float depth = frag.depthVal; if (depth < gl_FragCoord.z) { discard; } + + + // compute the view space position using the depth float z = near / (depth * depthScale - 1.0); vec4 position = vec4((depthTexCoordOffset + texCoord * depthTexCoordScale) * z, z, 1.0); + // get the normal from the map vec4 normal = texture2D(normalMap, texCoord); vec4 normalizedNormal = normalize(normal * 2.0 - vec4(1.0, 1.0, 1.0, 2.0)); @@ -54,4 +64,6 @@ void main(void) { normalizedNormal)); vec4 specularColor = texture2D(specularMap, texCoord); gl_FragColor = vec4((baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb) * attenuation, 0.0); + + gl_FragColor = vec4(frag.normal, 0.0); } diff --git a/libraries/shared/src/Transform.h b/libraries/shared/src/Transform.h index e08f00c0ac..346a82a3b0 100644 --- a/libraries/shared/src/Transform.h +++ b/libraries/shared/src/Transform.h @@ -100,6 +100,8 @@ public: // Left will be inversed before the multiplication static Transform& inverseMult(Transform& result, const Transform& left, const Transform& right); + Vec4 transform(const Vec4& pos) const; + protected: enum Flag { @@ -414,6 +416,13 @@ inline Transform& Transform::inverseMult( Transform& result, const Transform& le return result; } +inline Transform::Vec4 Transform::transform(const Vec4& pos) const { + Mat4 m; + getMatrix(m); + return m * pos; +} + + inline Transform::Mat4& Transform::getCachedMatrix(Transform::Mat4& result) const { updateCache(); result = (*_matrix); From db427cdf5e3553088786c29db1254938ece763e1 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Tue, 27 Jan 2015 15:45:10 -0800 Subject: [PATCH 02/24] adding Light class in model and the shader in render utils --- libraries/model/src/model/Light.cpp | 94 ++++++++++++++++++++ libraries/model/src/model/Light.h | 124 +++++++++++++++++++++++++++ libraries/render-utils/src/Light.slh | 88 +++++++++++++++++++ 3 files changed, 306 insertions(+) create mode 100755 libraries/model/src/model/Light.cpp create mode 100755 libraries/model/src/model/Light.h create mode 100755 libraries/render-utils/src/Light.slh diff --git a/libraries/model/src/model/Light.cpp b/libraries/model/src/model/Light.cpp new file mode 100755 index 0000000000..ea62a564d5 --- /dev/null +++ b/libraries/model/src/model/Light.cpp @@ -0,0 +1,94 @@ +// +// Light.cpp +// libraries/model/src/model +// +// Created by Sam Gateau on 1/26/2014. +// 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 +// +#include "Light.h" + +using namespace model; + +Light::Light() : + _flags(0), + _schemaBuffer(), + _transform() { + // only if created from nothing shall we create the Buffer to store the properties + Schema schema; + _schemaBuffer = gpu::BufferView(new gpu::Buffer(sizeof(Schema), (const gpu::Buffer::Byte*) &schema)); +} + +Light::Light(const Light& light) : + _flags(light._flags), + _schemaBuffer(light._schemaBuffer), + _transform(light._transform) { +} + +Light& Light::operator= (const Light& light) { + _flags = (light._flags); + _schemaBuffer = (light._schemaBuffer); + _transform = (light._transform); + + return (*this); +} + +Light::~Light() { +} + +void Light::setPosition(const Vec3& position) { + _transform.setTranslation(position); + _transform.getMatrix(editSchema()._transform); +} + +void Light::setOrientation(const glm::quat& orientation) { + _transform.setRotation(orientation); + _transform.getMatrix(editSchema()._transform); +} + +void Light::setDirection(const Vec3& direction) { + glm::mat3 rotation( + Vec3(1.0f, 0.0f, 0.0f), + Vec3(0.0f, 1.0f, 0.0f), + -direction); + + setOrientation(glm::quat(rotation)); +} + +const Vec3& Light::getDirection() const { + return Vec3(_transform.transform(Vec4(0.0f, 0.0f, 1.0f, 0.0f))); +} + +void Light::setColor(const Color& color) { + editSchema()._color = color; +} + +void Light::setIntensity(float intensity) { + editSchema()._intensity = intensity; +} + +void Light::setAttenuationRadius(float radius) { + if (radius <= 0.f) { + radius = 1.0f; + } + editSchema()._attenuation = Vec4(1.0f, 2.0f/radius, 1.0f/(radius*radius), radius); +} + +void Light::setSpotCone(float angle) { + if (angle <= 0.f) { + angle = 0.0f; + } + editSchema()._spot.x = cos(angle); + editSchema()._spot.z = angle; +} + +void Light::setSpotConeExponent(float exponent) { + if (exponent <= 0.f) { + exponent = 1.0f; + } + editSchema()._spot.y = exponent; + editSchema()._spot.w = exponent; +} + diff --git a/libraries/model/src/model/Light.h b/libraries/model/src/model/Light.h new file mode 100755 index 0000000000..f731255f8e --- /dev/null +++ b/libraries/model/src/model/Light.h @@ -0,0 +1,124 @@ +// +// Light.h +// libraries/model/src/model +// +// Created by Sam Gateau on 12/10/2014. +// 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 +// +#ifndef hifi_model_Light_h +#define hifi_model_Light_h + +#include +#include + +#include +#include "Transform.h" +#include "gpu/Resource.h" +#include "gpu/Texture.h" + +namespace model { +typedef gpu::BufferView UniformBufferView; +typedef gpu::TextureView TextureView; +typedef glm::vec3 Vec3; +typedef glm::vec4 Vec4; +typedef glm::quat Quat; + +class Light { +public: + enum Type { + SUN = 0, + POINT, + SPOT, + + NUM_TYPES, + }; + + typedef Vec3 Color; + + enum FlagBit { + COLOR_BIT = 0, + INTENSITY_BIT, + RANGE_BIT, + SPOT_BIT, + TRANSFORM_BIT, + + NUM_FLAGS, + }; + typedef std::bitset Flags; + + Light(); + Light(const Light& light); + Light& operator= (const Light& light); + virtual ~Light(); + + void setType(Type type) { editSchema()._control.x = float(type); } + Type getType() const { return Type((int) getSchema()._control.x); } + + void setPosition(const Vec3& position); + const Vec3& getPosition() const { return _transform.getTranslation(); } + + void setDirection(const Vec3& direction); + const Vec3& getDirection() const; + + void setOrientation(const Quat& orientation); + const glm::quat& getOrientation() const { return _transform.getRotation(); } + + const Color& getColor() const { return getSchema()._color; } + void setColor(const Color& color); + + float getIntensity() const { return getSchema()._intensity; } + void setIntensity(float intensity); + + bool isRanged() const { return (getType() == POINT) || (getType() == SPOT ); } + + void setAttenuationRadius(float radius); + float getAttenuationRadius() const { return getSchema()._attenuation.w; } + + // Spot properties + bool isSpot() const { return getType() == SPOT; } + void setSpotCone(float angle); + void setSpotConeExponent(float exponent); + float getSpotConeAngle() const { return getSchema()._spot.z; } + float getSpotConeExponent() const { return getSchema()._spot.w; } + + // Schema to access the attribute values of the light + class Schema { + public: + glm::mat4 _transform; + Color _color; + float _intensity; + Vec4 _attenuation; + Vec4 _spot; + Vec4 _shadow; + + Vec4 _control; + + Schema() : + _transform(), + _color(1.0f), + _intensity(1.0f), + _attenuation(1.0f, 2.0f, 1.0f, 1.0f), + _spot(0.0f, 0.0f, 0.0f, 0.0f), + _control(0.0f) + {} + }; + + const UniformBufferView& getSchemaBuffer() const { return _schemaBuffer; } + +protected: + + Flags _flags; + UniformBufferView _schemaBuffer; + Transform _transform; + + const Schema& getSchema() const { return _schemaBuffer.get(); } + Schema& editSchema() { return _schemaBuffer.edit(); } +}; +typedef QSharedPointer< Light > LightPointer; + +}; + +#endif diff --git a/libraries/render-utils/src/Light.slh b/libraries/render-utils/src/Light.slh new file mode 100755 index 0000000000..e91d1d93cb --- /dev/null +++ b/libraries/render-utils/src/Light.slh @@ -0,0 +1,88 @@ + +<@if not LIGHT_SLH@> +<@def LIGHT_SLH@> + +struct Light { + mat4 _transform; + + vec4 _color; + vec4 _attenuation; + vec4 _spot; + + vec4 _shadow; + + vec4 _control; +}; + +vec3 getLightPosition(Light l) { return l._transform[3].xyz; } +vec3 getLightDirection(Light l) { return -l._transform[2].xyz; } // direction is -Z axis + +vec3 getLightColor(Light l) { return l._color.rgb; } +float getLightIntensity(Light l) { return l._color.w; } + +float evalLightAttenuation(Light l, float r) { + return clamp(1.0/(l._attenuation.x + l._attenuation.y * r + l._attenuation.z * r * r), 0.0, 1.0); +} + +float evalLightSpotAttenuation(Light l, float cosA) { + if (cosA > l._spot.x) { + return 0.0; + } + return clamp(pow(cosA / l._spot.x, l._spot.w), 0.0, 1.0); +} + + + +<@if GLPROFILE == PC_GL@> +uniform lightBuffer { + Light light; +}; +Light getLight() { + return light; +} +<@elif GLPROFILE == MAC_GL@> +uniform vec4 lightBuffer[9]; +Light getLight() { + Light light; + light._transform[0] = lightBuffer[0]; + light._transform[1] = lightBuffer[1]; + light._transform[2] = lightBuffer[2]; + light._transform[3] = lightBuffer[3]; + light._color = lightBuffer[4]; + light._range = lightBuffer[5]; + light._spot = lightBuffer[6]; + light._shadow = lightBuffer[7]; + light._control = lightBuffer[8]; + + return light; +} +<@else@> +uniform vec4 lightBuffer[9]; +Light getLight() { + Light light; + light._transform[0] = lightBuffer[0]; + light._transform[1] = lightBuffer[1]; + light._transform[2] = lightBuffer[2]; + light._transform[3] = lightBuffer[3]; + light._color = lightBuffer[4]; + light._range = lightBuffer[5]; + light._spot = lightBuffer[6]; + light._shadow = lightBuffer[7]; + light._control = lightBuffer[8]; + + return light; +} +<@endif@> + + + +<@endif@> From b6836af0711d0cfbdbd8694fc347f92d131e2e5b Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Wed, 28 Jan 2015 00:09:15 -0800 Subject: [PATCH 03/24] improving on the poin light attenuatiion formula --- libraries/model/src/model/Light.cpp | 18 ++++------ libraries/model/src/model/Light.h | 14 ++++---- .../src/DeferredLightingEffect.cpp | 23 +++++++----- .../render-utils/src/DeferredLightingEffect.h | 1 + libraries/render-utils/src/Light.slh | 17 ++++++--- libraries/render-utils/src/point_light.slf | 35 ++++++++++++------- 6 files changed, 64 insertions(+), 44 deletions(-) diff --git a/libraries/model/src/model/Light.cpp b/libraries/model/src/model/Light.cpp index ea62a564d5..201144622f 100755 --- a/libraries/model/src/model/Light.cpp +++ b/libraries/model/src/model/Light.cpp @@ -40,25 +40,19 @@ Light::~Light() { void Light::setPosition(const Vec3& position) { _transform.setTranslation(position); - _transform.getMatrix(editSchema()._transform); + editSchema()._position = Vec4(position, 1.f); } void Light::setOrientation(const glm::quat& orientation) { _transform.setRotation(orientation); - _transform.getMatrix(editSchema()._transform); } void Light::setDirection(const Vec3& direction) { - glm::mat3 rotation( - Vec3(1.0f, 0.0f, 0.0f), - Vec3(0.0f, 1.0f, 0.0f), - -direction); - - setOrientation(glm::quat(rotation)); + editSchema()._direction = Vec4(direction, 0.f); } const Vec3& Light::getDirection() const { - return Vec3(_transform.transform(Vec4(0.0f, 0.0f, 1.0f, 0.0f))); + return Vec3(getSchema()._direction); } void Light::setColor(const Color& color) { @@ -69,11 +63,13 @@ void Light::setIntensity(float intensity) { editSchema()._intensity = intensity; } -void Light::setAttenuationRadius(float radius) { +void Light::setMaximumRadius(float radius) { if (radius <= 0.f) { radius = 1.0f; } - editSchema()._attenuation = Vec4(1.0f, 2.0f/radius, 1.0f/(radius*radius), radius); + float CutOffIntensityRatio = 0.01f; + float surfaceRadius = radius / (sqrt(1.0f / CutOffIntensityRatio) - 1.f); + editSchema()._attenuation = Vec4(surfaceRadius, 1.0f/surfaceRadius, CutOffIntensityRatio, radius); } void Light::setSpotCone(float angle) { diff --git a/libraries/model/src/model/Light.h b/libraries/model/src/model/Light.h index f731255f8e..107d30fb5e 100755 --- a/libraries/model/src/model/Light.h +++ b/libraries/model/src/model/Light.h @@ -73,9 +73,9 @@ public: void setIntensity(float intensity); bool isRanged() const { return (getType() == POINT) || (getType() == SPOT ); } - - void setAttenuationRadius(float radius); - float getAttenuationRadius() const { return getSchema()._attenuation.w; } + + void setMaximumRadius(float radius); + float getMaximumRadius() const { return getSchema()._attenuation.w; } // Spot properties bool isSpot() const { return getType() == SPOT; } @@ -87,7 +87,8 @@ public: // Schema to access the attribute values of the light class Schema { public: - glm::mat4 _transform; + Vec4 _position; + Vec4 _direction; Color _color; float _intensity; Vec4 _attenuation; @@ -97,10 +98,11 @@ public: Vec4 _control; Schema() : - _transform(), + _position(0.0f, 0.0f, 0.0f, 1.0f), + _direction(0.0f, 0.0f, -1.0f, 0.f), _color(1.0f), _intensity(1.0f), - _attenuation(1.0f, 2.0f, 1.0f, 1.0f), + _attenuation(1.0f, 1.0f, 1.0f, 1.0f), _spot(0.0f, 0.0f, 0.0f, 0.0f), _control(0.0f) {} diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 58e90f57d1..6899a47c99 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -275,7 +275,7 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu model::LightPointer lp = model::LightPointer(new model::Light()); lp->setPosition(position); - lp->setAttenuationRadius(radius); + lp->setMaximumRadius(radius); lp->setColor(diffuse); lp->setIntensity(1.0f); lp->setType(model::Light::POINT); @@ -297,7 +297,7 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu model::LightPointer ls = model::LightPointer(new model::Light()); ls->setPosition(position); ls->setDirection(direction); - ls->setAttenuationRadius(radius); + ls->setMaximumRadius(radius); ls->setSpotCone(cutoff); ls->setColor(diffuse); ls->setIntensity(1.0f); @@ -453,6 +453,8 @@ void DeferredLightingEffect::render() { const glm::vec3& eyePoint = _viewState->getCurrentViewFrustum()->getPosition(); float nearRadius = glm::distance(eyePoint, _viewState->getCurrentViewFrustum()->getNearTopLeft()); + glm::mat4 invViewMat; + _viewState->getViewTransform().getMatrix(invViewMat); auto geometryCache = DependencyManager::get(); @@ -470,7 +472,9 @@ void DeferredLightingEffect::render() { batch.setUniformBuffer(_pointLightLocations.lightBufferUnit, light->getSchemaBuffer()); gpu::GLBackend::renderBatch(batch); } - _spotLight.setUniformValue(_pointLightLocations.radius, light->getAttenuationRadius()); + glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + // _pointLight.setUniformValue(_pointLightLocations.viewMat, reinterpret_cast< const GLfloat* >(&viewMat)); + // _spotLight.setUniformValue(_pointLightLocations.radius, light->getAttenuationRadius()); /* @@ -485,7 +489,7 @@ void DeferredLightingEffect::render() { */ glPushMatrix(); - float expandedRadius = light->getAttenuationRadius() * (1.0f + SCALE_EXPANSION); + float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius) { glLoadIdentity(); glTranslatef(0.0f, 0.0f, -1.0f); @@ -525,7 +529,7 @@ void DeferredLightingEffect::render() { batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer()); gpu::GLBackend::renderBatch(batch); } - _spotLight.setUniformValue(_spotLightLocations.radius, light->getAttenuationRadius()); + // _spotLight.setUniformValue(_spotLightLocations.radius, light->getAttenuationRadius()); /* _spotLight.setUniformValue(_spotLightLocations.radius, light.radius); @@ -543,7 +547,7 @@ void DeferredLightingEffect::render() { glPushMatrix(); - float expandedRadius = light->getAttenuationRadius() * (1.0f + SCALE_EXPANSION); + float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); float edgeRadius = expandedRadius / glm::cos(light->getSpotConeAngle()); if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < edgeRadius + nearRadius) { glLoadIdentity(); @@ -563,7 +567,7 @@ void DeferredLightingEffect::render() { glm::quat spotRotation = rotationBetween(glm::vec3(0.0f, 0.0f, -1.0f), light->getDirection()); glm::vec3 axis = glm::axis(spotRotation); glRotatef(glm::degrees(glm::angle(spotRotation)), axis.x, axis.y, axis.z); - glTranslatef(0.0f, 0.0f, -light->getAttenuationRadius() * (1.0f + SCALE_EXPANSION * 0.5f)); + glTranslatef(0.0f, 0.0f, -light->getMaximumRadius() * (1.0f + SCALE_EXPANSION * 0.5f)); geometryCache->renderCone(expandedRadius * glm::tan(light->getSpotConeAngle()), expandedRadius, 32, 1); } @@ -649,6 +653,7 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limit locations.depthTexCoordScale = program.uniformLocation("depthTexCoordScale"); locations.radius = program.uniformLocation("radius"); locations.ambientSphere = program.uniformLocation("ambientSphere.L00"); + locations.invViewMat = program.uniformLocation("invViewMat"); GLint loc = -1; #if defined(Q_OS_MAC) @@ -661,8 +666,8 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limit #elif defined(Q_OS_WIN) loc = glGetUniformBlockIndex(program.programId(), "lightBuffer"); if (loc >= 0) { - glUniformBlockBinding(program.programId(), loc, 1); - locations.lightBufferUnit = 1; + glUniformBlockBinding(program.programId(), loc, 0); + locations.lightBufferUnit = 0; } else { locations.lightBufferUnit = -1; } diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 5c39c3041e..60596dd86f 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -105,6 +105,7 @@ private: int radius; int ambientSphere; int lightBufferUnit; + int invViewMat; }; static void loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations); diff --git a/libraries/render-utils/src/Light.slh b/libraries/render-utils/src/Light.slh index e91d1d93cb..a48f7da81d 100755 --- a/libraries/render-utils/src/Light.slh +++ b/libraries/render-utils/src/Light.slh @@ -12,8 +12,8 @@ <@def LIGHT_SLH@> struct Light { - mat4 _transform; - + vec4 _position; + vec4 _direction; vec4 _color; vec4 _attenuation; vec4 _spot; @@ -23,14 +23,18 @@ struct Light { vec4 _control; }; -vec3 getLightPosition(Light l) { return l._transform[3].xyz; } -vec3 getLightDirection(Light l) { return -l._transform[2].xyz; } // direction is -Z axis +vec3 getLightPosition(Light l) { return l._position.xyz; } +vec3 getLightDirection(Light l) { return l._direction.xyz; } // direction is -Z axis vec3 getLightColor(Light l) { return l._color.rgb; } float getLightIntensity(Light l) { return l._color.w; } float evalLightAttenuation(Light l, float r) { - return clamp(1.0/(l._attenuation.x + l._attenuation.y * r + l._attenuation.z * r * r), 0.0, 1.0); + float d = max(r - l._attenuation.x, 0.0); + float denom = d * l._attenuation.y + 1.0; + float attenuation = 1.0 / (denom * denom); + return max((attenuation - l._attenuation.z)/(1.0 - l._attenuation.z), 0.0); + // return clamp(1.0/(l._attenuation.x + l._attenuation.y * r + l._attenuation.z * r * r), 0.0, 1.0); } float evalLightSpotAttenuation(Light l, float cosA) { @@ -40,6 +44,9 @@ float evalLightSpotAttenuation(Light l, float cosA) { return clamp(pow(cosA / l._spot.x, l._spot.w), 0.0, 1.0); } +float getLightSquareRadius(Light l) { + return l._attenuation.w * l._attenuation.w; +} <@if GLPROFILE == PC_GL@> diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index 9d9aecda95..599d10fbdb 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -21,29 +21,38 @@ // the radius (hard cutoff) of the light effect uniform float radius; +uniform mat4 invViewMat; + void main(void) { // get the depth and exit early if it doesn't pass the test vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; DeferredFragment frag = unpackDeferredFragment(texCoord); - + float depth = frag.depthVal; if (depth < gl_FragCoord.z) { discard; } - // compute the view space position using the depth - float z = near / (depth * depthScale - 1.0); - vec4 position = vec4((depthTexCoordOffset + texCoord * depthTexCoordScale) * z, z, 1.0); - - // get the normal from the map - vec4 normal = texture2D(normalMap, texCoord); - vec4 normalizedNormal = normalize(normal * 2.0 - vec4(1.0, 1.0, 1.0, 2.0)); - - // compute the base color based on OpenGL lighting model - vec4 lightVector = gl_LightSource[1].position - position; + vec4 wPos; + wPos = invViewMat * frag.position; + Light light = getLight(); + gl_FragColor = vec4(frag.normal, 0.0); + + vec3 lightVector = getLightPosition(light) - wPos.xyz; + if (dot(lightVector, lightVector) > getLightSquareRadius(light)) { + discard; + } + float lightDistance = length(lightVector); lightVector = lightVector / lightDistance; + + float lightAttenuation = evalLightAttenuation(light, lightDistance); + gl_FragColor.xyz *= lightAttenuation; +/* + float diffuse = dot(frag.normal, lightVector); + + float diffuse = dot(normalizedNormal, lightVector); float facingLight = step(0.0, diffuse); vec4 baseColor = texture2D(diffuseMap, texCoord) * (gl_FrontLightProduct[1].ambient + @@ -59,6 +68,6 @@ void main(void) { normalizedNormal)); vec4 specularColor = texture2D(specularMap, texCoord); gl_FragColor = vec4((baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb) * attenuation, 0.0); - - gl_FragColor = vec4(frag.normal, 0.0); + */ + // gl_FragColor = vec4(frag.normal, 0.0); } From 471da3935d0ea3d37b86b13257b6a66070ce101c Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 29 Jan 2015 17:11:50 -0800 Subject: [PATCH 04/24] working on the lighting --- .../render-utils/src/DeferredLighting.slh | 1 - libraries/render-utils/src/point_light.slf | 42 +++++++++---------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index cd65fd1053..62d2e1e00f 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -92,5 +92,4 @@ vec3 evalLightmappedColor(float shadowAttenuation, vec3 normal, vec3 diffuse, ve return diffuse * (ambientLight + diffuseLight); } - <@endif@> diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index 599d10fbdb..e91a559eba 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -23,6 +23,8 @@ uniform float radius; uniform mat4 invViewMat; + + void main(void) { // get the depth and exit early if it doesn't pass the test vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; @@ -37,7 +39,6 @@ void main(void) { vec4 wPos; wPos = invViewMat * frag.position; Light light = getLight(); - gl_FragColor = vec4(frag.normal, 0.0); vec3 lightVector = getLightPosition(light) - wPos.xyz; if (dot(lightVector, lightVector) > getLightSquareRadius(light)) { @@ -45,29 +46,24 @@ void main(void) { } float lightDistance = length(lightVector); - lightVector = lightVector / lightDistance; + vec3 lightDir = lightVector / lightDistance; float lightAttenuation = evalLightAttenuation(light, lightDistance); - gl_FragColor.xyz *= lightAttenuation; -/* - float diffuse = dot(frag.normal, lightVector); - - float diffuse = dot(normalizedNormal, lightVector); - float facingLight = step(0.0, diffuse); - vec4 baseColor = texture2D(diffuseMap, texCoord) * (gl_FrontLightProduct[1].ambient + - gl_FrontLightProduct[1].diffuse * (diffuse * facingLight)); - - // compute attenuation based on distance, etc. - float attenuation = step(lightDistance, radius) / dot(vec3(gl_LightSource[1].constantAttenuation, - gl_LightSource[1].linearAttenuation, gl_LightSource[1].quadraticAttenuation), - vec3(1.0, lightDistance, lightDistance * lightDistance)); - - // add base to specular, modulate by attenuation - float specular = facingLight * max(0.0, dot(normalize(lightVector - normalize(vec4(position.xyz, 0.0))), - normalizedNormal)); - vec4 specularColor = texture2D(specularMap, texCoord); - gl_FragColor = vec4((baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb) * attenuation, 0.0); - */ - // gl_FragColor = vec4(frag.normal, 0.0); + vec4 wNor = invViewMat * vec4(frag.normal, 0.0); + vec4 wEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); + vec3 eyeDir = normalize(wEyeVector.xyz); + vec3 wHalfDir = normalize(eyeDir + lightDir); + // Diffuse Lighting + float diffuseDot = dot(wNor.xyz, lightDir); + float facingLight = step(0.0, diffuseDot); + vec3 diffuseColor = frag.diffuse * diffuseDot * facingLight; + + // compute the specular multiplier (sans exponent) + float specularPower = facingLight * max(0.0, + dot(eyeDir, wHalfDir)); + vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; + + // add specular contribution + gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); } From 228e629856ec37e9184a72858094bf7a03b5ebad Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Fri, 30 Jan 2015 11:40:44 -0800 Subject: [PATCH 05/24] working on the spot light now --- .../src/DeferredLightingEffect.cpp | 2 + libraries/render-utils/src/Light.slh | 4 ++ libraries/render-utils/src/Model.cpp | 2 - libraries/render-utils/src/point_light.slf | 2 - libraries/render-utils/src/spot_light.slf | 64 +++++++++++++++++++ 5 files changed, 70 insertions(+), 4 deletions(-) diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 6899a47c99..b626a1a191 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -529,6 +529,8 @@ void DeferredLightingEffect::render() { batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer()); gpu::GLBackend::renderBatch(batch); } + glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + // _spotLight.setUniformValue(_spotLightLocations.radius, light->getAttenuationRadius()); /* diff --git a/libraries/render-utils/src/Light.slh b/libraries/render-utils/src/Light.slh index a48f7da81d..97fa415a0c 100755 --- a/libraries/render-utils/src/Light.slh +++ b/libraries/render-utils/src/Light.slh @@ -37,6 +37,10 @@ float evalLightAttenuation(Light l, float r) { // return clamp(1.0/(l._attenuation.x + l._attenuation.y * r + l._attenuation.z * r * r), 0.0, 1.0); } +float getLightSpotAngleCos(Light l) { + return l._spot.x; +} + float evalLightSpotAttenuation(Light l, float cosA) { if (cosA > l._spot.x) { return 0.0; diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 79f938e7f6..afcab0bc9a 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -2437,11 +2437,9 @@ int Model::renderMeshesFromList(QVector& list, gpu::Batch& batch, RenderMod } static bool showDiffuse = true; if (showDiffuse && diffuseMap) { - // GLBATCH(glBindTexture)(GL_TEXTURE_2D, diffuseMap->getID()); batch.setUniformTexture(0, diffuseMap->getGPUTexture()); } else { - // GLBATCH(glBindTexture)(GL_TEXTURE_2D, textureCache->getWhiteTextureID()); batch.setUniformTexture(0, textureCache->getWhiteTexture()); } diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index e91a559eba..225b975911 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -18,8 +18,6 @@ // Everything about light <@include Light.slh@> -// the radius (hard cutoff) of the light effect -uniform float radius; uniform mat4 invViewMat; diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index 48e8ab0d61..0f33d30f35 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -18,6 +18,69 @@ // Everything about light <@include Light.slh@> +uniform mat4 invViewMat; + +void main(void) { + // get the depth and exit early if it doesn't pass the test + vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; + + DeferredFragment frag = unpackDeferredFragment(texCoord); + + float depth = frag.depthVal; + if (depth < gl_FragCoord.z) { + discard; + } + + vec4 wPos; + wPos = invViewMat * frag.position; + Light light = getLight(); + + vec3 lightVector = getLightPosition(light) - wPos.xyz; + if (dot(lightVector, lightVector) > getLightSquareRadius(light)) { + discard; + } + + float lightDistance = length(lightVector); + vec3 lightDir = lightVector / lightDistance; + + vec3 lightSpotDir = getLightDirection(light); + + // compute attenuation based on spot angle, distance, etc. + float cosSpotAngle = max(-dot(lightDir, lightSpotDir), 0.0); + if (cosSpotAngle < getLightSpotAngleCos(light)) { + discard; + } +/* + float attenuation = step(lightDistance, radius) * step(gl_LightSource[1].spotCosCutoff, cosSpotAngle) * + pow(cosSpotAngle, gl_LightSource[1].spotExponent) / dot(vec3(gl_LightSource[1].constantAttenuation, + gl_LightSource[1].linearAttenuation, gl_LightSource[1].quadraticAttenuation), + vec3(1.0, lightDistance, lightDistance * lightDistance)); + + + + float lightAttenuation = evalLightAttenuation(light, lightDistance); + + vec4 wNor = invViewMat * vec4(frag.normal, 0.0); + vec4 wEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); + vec3 eyeDir = normalize(wEyeVector.xyz); + vec3 wHalfDir = normalize(eyeDir + lightDir); + // Diffuse Lighting + float diffuseDot = dot(wNor.xyz, lightDir); + float facingLight = step(0.0, diffuseDot); + vec3 diffuseColor = frag.diffuse * diffuseDot * facingLight; + + // compute the specular multiplier (sans exponent) + float specularPower = facingLight * max(0.0, + dot(eyeDir, wHalfDir)); + vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; + + // add specular contribution + gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); + */ + gl_FragColor = vec4(frag.normal, 0.0); + +} +/* // the radius (hard cutoff) of the light effect uniform float radius; @@ -67,3 +130,4 @@ void main(void) { gl_FragColor = vec4(frag.normal, 0.0); } +*/ \ No newline at end of file From 6eca52e4f2ba24621fe4f3c8eabe2c6ad4a1cbb0 Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 30 Jan 2015 17:01:46 -0800 Subject: [PATCH 06/24] point light is working with Mac --- libraries/render-utils/src/Light.slh | 32 ++++++++++++---------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/libraries/render-utils/src/Light.slh b/libraries/render-utils/src/Light.slh index 97fa415a0c..169699b89a 100755 --- a/libraries/render-utils/src/Light.slh +++ b/libraries/render-utils/src/Light.slh @@ -64,15 +64,13 @@ Light getLight() { uniform vec4 lightBuffer[9]; Light getLight() { Light light; - light._transform[0] = lightBuffer[0]; - light._transform[1] = lightBuffer[1]; - light._transform[2] = lightBuffer[2]; - light._transform[3] = lightBuffer[3]; - light._color = lightBuffer[4]; - light._range = lightBuffer[5]; - light._spot = lightBuffer[6]; - light._shadow = lightBuffer[7]; - light._control = lightBuffer[8]; + light._position = lightBuffer[0]; + light._direction = lightBuffer[1]; + light._color = lightBuffer[2]; + light._attenuation = lightBuffer[3]; + light._spot = lightBuffer[4]; + light._shadow = lightBuffer[5]; + light._control = lightBuffer[6]; return light; } @@ -80,15 +78,13 @@ Light getLight() { uniform vec4 lightBuffer[9]; Light getLight() { Light light; - light._transform[0] = lightBuffer[0]; - light._transform[1] = lightBuffer[1]; - light._transform[2] = lightBuffer[2]; - light._transform[3] = lightBuffer[3]; - light._color = lightBuffer[4]; - light._range = lightBuffer[5]; - light._spot = lightBuffer[6]; - light._shadow = lightBuffer[7]; - light._control = lightBuffer[8]; + light._position = lightBuffer[0]; + light._direction = lightBuffer[1]; + light._color = lightBuffer[2]; + light._attenuation = lightBuffer[3]; + light._spot = lightBuffer[4]; + light._shadow = lightBuffer[5]; + light._control = lightBuffer[6]; return light; } From c3e1311ed113b788064413e3531b43c48afa9396 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Fri, 30 Jan 2015 17:03:28 -0800 Subject: [PATCH 07/24] avoid recreating lights every frame, just have a pool of lights edited each frames --- .../src/DeferredLightingEffect.cpp | 40 +++++++++++-------- .../render-utils/src/DeferredLightingEffect.h | 5 ++- libraries/render-utils/src/GeometryCache.cpp | 6 +-- libraries/render-utils/src/point_light.slf | 7 ++++ 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index d0a77f9174..fee3b4c947 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -263,6 +263,13 @@ void DeferredLightingEffect::addPointLight(const glm::vec3& position, float radi void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radius, const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular, float constantAttenuation, float linearAttenuation, float quadraticAttenuation, const glm::vec3& direction, float exponent, float cutoff) { + + int lightID = _pointLights.size() + _spotLights.size(); + if (lightID >= _allocatedLights.size()) { + _allocatedLights.push_back(model::LightPointer(new model::Light())); + } + model::LightPointer lp = _allocatedLights[lightID]; + if (exponent == 0.0f && cutoff == PI) { PointLight light; light.position = glm::vec4(position, 1.0f); @@ -273,13 +280,12 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu light.constantAttenuation = constantAttenuation; light.linearAttenuation = linearAttenuation; - model::LightPointer lp = model::LightPointer(new model::Light()); lp->setPosition(position); lp->setMaximumRadius(radius); lp->setColor(diffuse); lp->setIntensity(1.0f); lp->setType(model::Light::POINT); - _pointLights.push_back(lp); + _pointLights.push_back(lightID); } else { SpotLight light; @@ -294,16 +300,15 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu light.exponent = exponent; light.cutoff = cutoff; - model::LightPointer ls = model::LightPointer(new model::Light()); - ls->setPosition(position); - ls->setDirection(direction); - ls->setMaximumRadius(radius); - ls->setSpotCone(cutoff); - ls->setColor(diffuse); - ls->setIntensity(1.0f); - ls->setType(model::Light::SPOT); + lp->setPosition(position); + lp->setDirection(direction); + lp->setMaximumRadius(radius); + lp->setSpotCone(cutoff); + lp->setColor(diffuse); + lp->setIntensity(1.0f); + lp->setType(model::Light::SPOT); - _spotLights.push_back(ls); + _spotLights.push_back(lightID); } } @@ -337,7 +342,7 @@ void DeferredLightingEffect::render() { QOpenGLFramebufferObject* freeFBO = DependencyManager::get()->getFreeFramebufferObject(); freeFBO->bind(); glClear(GL_COLOR_BUFFER_BIT); - glEnable(GL_FRAMEBUFFER_SRGB); + // glEnable(GL_FRAMEBUFFER_SRGB); glBindTexture(GL_TEXTURE_2D, primaryFBO->texture()); @@ -463,7 +468,8 @@ void DeferredLightingEffect::render() { _pointLight.setUniformValue(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); _pointLight.setUniformValue(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); - for (auto light : _pointLights) { + for (auto lightID : _pointLights) { + auto light = _allocatedLights[lightID]; // foreach (const PointLight& light, _pointLights) { if (_pointLightLocations.lightBufferUnit >= 0) { gpu::Batch batch; @@ -471,8 +477,6 @@ void DeferredLightingEffect::render() { gpu::GLBackend::renderBatch(batch); } glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); - // _pointLight.setUniformValue(_pointLightLocations.viewMat, reinterpret_cast< const GLfloat* >(&viewMat)); - // _spotLight.setUniformValue(_pointLightLocations.radius, light->getAttenuationRadius()); /* @@ -520,7 +524,9 @@ void DeferredLightingEffect::render() { _spotLight.setUniformValue(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); _spotLight.setUniformValue(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); - for (auto light : _spotLights) { + for (auto lightID : _spotLights) { + auto light = _allocatedLights[lightID]; + // foreach (const SpotLight& light, _spotLights) { if (_spotLightLocations.lightBufferUnit >= 0) { gpu::Batch batch; @@ -591,7 +597,7 @@ void DeferredLightingEffect::render() { glBindTexture(GL_TEXTURE_2D, 0); freeFBO->release(); - glDisable(GL_FRAMEBUFFER_SRGB); + // glDisable(GL_FRAMEBUFFER_SRGB); glDisable(GL_CULL_FACE); diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 8c0288f2df..8e6606506a 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -153,8 +153,9 @@ private: typedef std::vector< model::LightPointer > Lights; - Lights _pointLights; - Lights _spotLights; + Lights _allocatedLights; + std::vector _pointLights; + std::vector _spotLights; // QVector _pointLights; // QVector _spotLights; QVector _postLightingRenderables; diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index c32b8ff757..cb44ecae5c 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -1152,7 +1152,7 @@ void GeometryCache::renderQuad(const glm::vec2& minCorner, const glm::vec2& maxC gpu::Batch batch; - glEnable(GL_TEXTURE_2D); +// glEnable(GL_TEXTURE_2D); //glBindTexture(GL_TEXTURE_2D, _currentTextureID); // this is quad specific... batch.setInputFormat(details.streamFormat); @@ -1166,8 +1166,8 @@ void GeometryCache::renderQuad(const glm::vec2& minCorner, const glm::vec2& maxC glDisableClientState(GL_COLOR_ARRAY); glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindTexture(GL_TEXTURE_2D, 0); - glDisable(GL_TEXTURE_2D); + // glBindTexture(GL_TEXTURE_2D, 0); + // glDisable(GL_TEXTURE_2D); } void GeometryCache::renderQuad(const glm::vec3& minCorner, const glm::vec3& maxCorner, const glm::vec4& color, int id) { diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index 225b975911..5445d3576e 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -36,6 +36,7 @@ void main(void) { vec4 wPos; wPos = invViewMat * frag.position; + Light light = getLight(); vec3 lightVector = getLightPosition(light) - wPos.xyz; @@ -43,9 +44,14 @@ void main(void) { discard; } + + float lightDistance = length(lightVector); vec3 lightDir = lightVector / lightDistance; + vec4 wNor = invViewMat * vec4(frag.normal, 0.0); + gl_FragColor = vec4(wNor.xyz, 0.0); +/* float lightAttenuation = evalLightAttenuation(light, lightDistance); vec4 wNor = invViewMat * vec4(frag.normal, 0.0); @@ -64,4 +70,5 @@ void main(void) { // add specular contribution gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); + */ } From 0da38c103f38ae6252a88a8c8897b93d02be6ae0 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 2 Feb 2015 11:17:35 -0800 Subject: [PATCH 08/24] working on the spot light shader --- libraries/render-utils/src/Light.slh | 3 ++ libraries/render-utils/src/point_light.slf | 32 +++++++------ libraries/render-utils/src/spot_light.slf | 56 ++++++++++++++++++---- 3 files changed, 67 insertions(+), 24 deletions(-) diff --git a/libraries/render-utils/src/Light.slh b/libraries/render-utils/src/Light.slh index 169699b89a..647708b376 100755 --- a/libraries/render-utils/src/Light.slh +++ b/libraries/render-utils/src/Light.slh @@ -52,6 +52,9 @@ float getLightSquareRadius(Light l) { return l._attenuation.w * l._attenuation.w; } +float getLightAttenuationCutoff(Light l) { + return l._attenuation.z; +} <@if GLPROFILE == PC_GL@> uniform lightBuffer { diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index 5445d3576e..86cdb047ec 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -18,46 +18,44 @@ // Everything about light <@include Light.slh@> - +// The view Matrix uniform mat4 invViewMat; - - void main(void) { // get the depth and exit early if it doesn't pass the test vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; DeferredFragment frag = unpackDeferredFragment(texCoord); + // Kill if in front of the light volume float depth = frag.depthVal; if (depth < gl_FragCoord.z) { discard; } - vec4 wPos; - wPos = invViewMat * frag.position; - + // Need the light now Light light = getLight(); + // Make the Light vector going from fragment to light center in world space + vec4 wPos; + wPos = invViewMat * frag.position; vec3 lightVector = getLightPosition(light) - wPos.xyz; + + // Kill if too far from the light center if (dot(lightVector, lightVector) > getLightSquareRadius(light)) { discard; } - - + // Allright we re valid in the volume float lightDistance = length(lightVector); - vec3 lightDir = lightVector / lightDistance; - vec4 wNor = invViewMat * vec4(frag.normal, 0.0); - - gl_FragColor = vec4(wNor.xyz, 0.0); -/* float lightAttenuation = evalLightAttenuation(light, lightDistance); + vec3 lightDir = lightVector / lightDistance; vec4 wNor = invViewMat * vec4(frag.normal, 0.0); vec4 wEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); vec3 eyeDir = normalize(wEyeVector.xyz); vec3 wHalfDir = normalize(eyeDir + lightDir); + // Diffuse Lighting float diffuseDot = dot(wNor.xyz, lightDir); float facingLight = step(0.0, diffuseDot); @@ -69,6 +67,10 @@ void main(void) { vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; // add specular contribution - gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); - */ + float ring = (lightAttenuation / (0.2 * getLightAttenuationCutoff(light))) - 1; + if (ring < 1) { + gl_FragColor = vec4(ring * ring * getLightColor(light), 0.0); + } else { + gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); + } } diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index 0f33d30f35..e5688718f6 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -18,6 +18,7 @@ // Everything about light <@include Light.slh@> +// The view Matrix uniform mat4 invViewMat; void main(void) { @@ -26,30 +27,67 @@ void main(void) { DeferredFragment frag = unpackDeferredFragment(texCoord); + // Kill if in front of the light volume float depth = frag.depthVal; if (depth < gl_FragCoord.z) { discard; } - vec4 wPos; - wPos = invViewMat * frag.position; + // Need the light now Light light = getLight(); - vec3 lightVector = getLightPosition(light) - wPos.xyz; + // Make the Light vector going from fragment to light center in world space + vec4 wPos; + wPos = invViewMat * frag.position; + vec3 lightVector = wPos.xyz - getLightPosition(light); + + // Kill if too far from the light center if (dot(lightVector, lightVector) > getLightSquareRadius(light)) { discard; } + // Allright we re valid in the volume float lightDistance = length(lightVector); + float lightAttenuation = evalLightAttenuation(light, lightDistance); + vec3 lightDir = lightVector / lightDistance; + + gl_FragColor = vec4(getLightColor(light), 0.0); + // Kill if not in the spot light (ah ah !) vec3 lightSpotDir = getLightDirection(light); - - // compute attenuation based on spot angle, distance, etc. float cosSpotAngle = max(-dot(lightDir, lightSpotDir), 0.0); if (cosSpotAngle < getLightSpotAngleCos(light)) { - discard; + // discard; + gl_FragColor = vec4(vec3(1.0, 0.0, 0.0), 0.0); } + + /* + vec4 wNor = invViewMat * vec4(frag.normal, 0.0); + vec4 wEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); + vec3 eyeDir = normalize(wEyeVector.xyz); + vec3 wHalfDir = normalize(eyeDir + lightDir); + + // Diffuse Lighting + float diffuseDot = dot(wNor.xyz, lightDir); + float facingLight = step(0.0, diffuseDot); + vec3 diffuseColor = frag.diffuse * diffuseDot * facingLight; + + // compute the specular multiplier (sans exponent) + float specularPower = facingLight * max(0.0, + dot(eyeDir, wHalfDir)); + vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; + + // add specular contribution + /* float ring = (lightAttenuation / (0.2 * getLightAttenuationCutoff(light))) - 1; + if (ring < 1) { + gl_FragColor = vec4(ring * ring * getLightColor(light), 0.0); + } else { + gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); + }*/ +} + + /* float attenuation = step(lightDistance, radius) * step(gl_LightSource[1].spotCosCutoff, cosSpotAngle) * pow(cosSpotAngle, gl_LightSource[1].spotExponent) / dot(vec3(gl_LightSource[1].constantAttenuation, @@ -77,9 +115,9 @@ void main(void) { // add specular contribution gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); */ - gl_FragColor = vec4(frag.normal, 0.0); +// gl_FragColor = vec4(frag.normal, 0.0); -} +//} /* // the radius (hard cutoff) of the light effect uniform float radius; @@ -130,4 +168,4 @@ void main(void) { gl_FragColor = vec4(frag.normal, 0.0); } -*/ \ No newline at end of file +*/ From 16f12dd2673189288f4117d0a339cef45a3c8cc3 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 2 Feb 2015 16:31:36 -0800 Subject: [PATCH 09/24] working on the point light shader edge representation --- libraries/model/src/model/Light.cpp | 4 ++-- libraries/model/src/model/Light.h | 6 ++++-- libraries/render-utils/src/Light.slh | 4 ++++ libraries/render-utils/src/point_light.slf | 10 ++++++---- libraries/render-utils/src/spot_light.slf | 16 ++++++++-------- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/libraries/model/src/model/Light.cpp b/libraries/model/src/model/Light.cpp index 201144622f..6348ffbcc7 100755 --- a/libraries/model/src/model/Light.cpp +++ b/libraries/model/src/model/Light.cpp @@ -48,11 +48,11 @@ void Light::setOrientation(const glm::quat& orientation) { } void Light::setDirection(const Vec3& direction) { - editSchema()._direction = Vec4(direction, 0.f); + editSchema()._direction = direction; } const Vec3& Light::getDirection() const { - return Vec3(getSchema()._direction); + return getSchema()._direction; } void Light::setColor(const Color& color) { diff --git a/libraries/model/src/model/Light.h b/libraries/model/src/model/Light.h index 107d30fb5e..5e3bda5e1d 100755 --- a/libraries/model/src/model/Light.h +++ b/libraries/model/src/model/Light.h @@ -88,7 +88,8 @@ public: class Schema { public: Vec4 _position; - Vec4 _direction; + Vec3 _direction; + float _spare0; Color _color; float _intensity; Vec4 _attenuation; @@ -99,7 +100,8 @@ public: Schema() : _position(0.0f, 0.0f, 0.0f, 1.0f), - _direction(0.0f, 0.0f, -1.0f, 0.f), + _direction(0.0f, 0.0f, -1.0f), + _spare0(0.f), _color(1.0f), _intensity(1.0f), _attenuation(1.0f, 1.0f, 1.0f, 1.0f), diff --git a/libraries/render-utils/src/Light.slh b/libraries/render-utils/src/Light.slh index 647708b376..ccf6e52836 100755 --- a/libraries/render-utils/src/Light.slh +++ b/libraries/render-utils/src/Light.slh @@ -52,6 +52,10 @@ float getLightSquareRadius(Light l) { return l._attenuation.w * l._attenuation.w; } +float getLightRadius(Light l) { + return l._attenuation.w; +} + float getLightAttenuationCutoff(Light l) { return l._attenuation.z; } diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index 86cdb047ec..3ba6e8d2bc 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -66,10 +66,12 @@ void main(void) { dot(eyeDir, wHalfDir)); vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; - // add specular contribution - float ring = (lightAttenuation / (0.2 * getLightAttenuationCutoff(light))) - 1; - if (ring < 1) { - gl_FragColor = vec4(ring * ring * getLightColor(light), 0.0); + // Show edge + float maxRadius = getLightRadius(light); + float edge = abs(2.0 * ((maxRadius - lightDistance) / (0.1)) - 1.0); + if (edge < 1) { + float edgeCoord = exp2(-8.0*edge*edge); + gl_FragColor = vec4(edgeCoord * edgeCoord * getLightColor(light), 0.0); } else { gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); } diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index e5688718f6..cb309ea762 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -39,27 +39,27 @@ void main(void) { // Make the Light vector going from fragment to light center in world space vec4 wPos; wPos = invViewMat * frag.position; - vec3 lightVector = wPos.xyz - getLightPosition(light); + vec3 fragLightVec = getLightPosition(light) - wPos.xyz; // Kill if too far from the light center - if (dot(lightVector, lightVector) > getLightSquareRadius(light)) { + if (dot(fragLightVec, fragLightVec) > getLightSquareRadius(light)) { discard; } // Allright we re valid in the volume - float lightDistance = length(lightVector); - float lightAttenuation = evalLightAttenuation(light, lightDistance); + float fragLightDistance = length(fragLightVec); + float lightAttenuation = evalLightAttenuation(light, fragLightDistance); - vec3 lightDir = lightVector / lightDistance; + vec3 fragLightDir = fragLightVec / fragLightDistance; gl_FragColor = vec4(getLightColor(light), 0.0); // Kill if not in the spot light (ah ah !) vec3 lightSpotDir = getLightDirection(light); - float cosSpotAngle = max(-dot(lightDir, lightSpotDir), 0.0); + float cosSpotAngle = max(-dot(fragLightDir, lightSpotDir), 0.0); if (cosSpotAngle < getLightSpotAngleCos(light)) { - // discard; - gl_FragColor = vec4(vec3(1.0, 0.0, 0.0), 0.0); + discard; + //gl_FragColor = vec4(vec3(1.0, 0.0, 0.0), 0.0); } /* From 5c4a8a5cda754a25cf287745a51ffa58bf24a3d7 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 2 Feb 2015 17:24:26 -0800 Subject: [PATCH 10/24] Fixing the spotlight --- libraries/render-utils/src/point_light.slf | 22 ++-- libraries/render-utils/src/spot_light.slf | 123 ++++----------------- 2 files changed, 32 insertions(+), 113 deletions(-) diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index 3ba6e8d2bc..da2d708f06 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -39,36 +39,36 @@ void main(void) { // Make the Light vector going from fragment to light center in world space vec4 wPos; wPos = invViewMat * frag.position; - vec3 lightVector = getLightPosition(light) - wPos.xyz; + vec3 fragLightVec = getLightPosition(light) - wPos.xyz; // Kill if too far from the light center - if (dot(lightVector, lightVector) > getLightSquareRadius(light)) { + if (dot(fragLightVec, fragLightVec) > getLightSquareRadius(light)) { discard; } // Allright we re valid in the volume - float lightDistance = length(lightVector); - float lightAttenuation = evalLightAttenuation(light, lightDistance); + float fragLightDistance = length(fragLightVec); + float lightAttenuation = evalLightAttenuation(light, fragLightDistance); - vec3 lightDir = lightVector / lightDistance; + vec3 fragLightDir = fragLightVec / fragLightDistance; vec4 wNor = invViewMat * vec4(frag.normal, 0.0); - vec4 wEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); - vec3 eyeDir = normalize(wEyeVector.xyz); - vec3 wHalfDir = normalize(eyeDir + lightDir); + vec4 fragEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); + vec3 fragEyeDir = normalize(fragEyeVector.xyz); + vec3 wHalfDir = normalize(fragEyeDir + fragLightDir); // Diffuse Lighting - float diffuseDot = dot(wNor.xyz, lightDir); + float diffuseDot = dot(wNor.xyz, fragLightDir); float facingLight = step(0.0, diffuseDot); vec3 diffuseColor = frag.diffuse * diffuseDot * facingLight; // compute the specular multiplier (sans exponent) float specularPower = facingLight * max(0.0, - dot(eyeDir, wHalfDir)); + dot(fragEyeDir, wHalfDir)); vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; // Show edge float maxRadius = getLightRadius(light); - float edge = abs(2.0 * ((maxRadius - lightDistance) / (0.1)) - 1.0); + float edge = abs(2.0 * ((maxRadius - fragLightDistance) / (0.1)) - 1.0); if (edge < 1) { float edgeCoord = exp2(-8.0*edge*edge); gl_FragColor = vec4(edgeCoord * edgeCoord * getLightColor(light), 0.0); diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index cb309ea762..d8bedc2bea 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -37,8 +37,7 @@ void main(void) { Light light = getLight(); // Make the Light vector going from fragment to light center in world space - vec4 wPos; - wPos = invViewMat * frag.position; + vec4 wPos = invViewMat * frag.position; vec3 fragLightVec = getLightPosition(light) - wPos.xyz; // Kill if too far from the light center @@ -49,123 +48,43 @@ void main(void) { // Allright we re valid in the volume float fragLightDistance = length(fragLightVec); float lightAttenuation = evalLightAttenuation(light, fragLightDistance); - vec3 fragLightDir = fragLightVec / fragLightDistance; - - gl_FragColor = vec4(getLightColor(light), 0.0); // Kill if not in the spot light (ah ah !) vec3 lightSpotDir = getLightDirection(light); float cosSpotAngle = max(-dot(fragLightDir, lightSpotDir), 0.0); if (cosSpotAngle < getLightSpotAngleCos(light)) { discard; - //gl_FragColor = vec4(vec3(1.0, 0.0, 0.0), 0.0); } - /* vec4 wNor = invViewMat * vec4(frag.normal, 0.0); - vec4 wEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); - vec3 eyeDir = normalize(wEyeVector.xyz); - vec3 wHalfDir = normalize(eyeDir + lightDir); + vec4 fragEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); + vec3 fragEyeDir = normalize(fragEyeVector.xyz); + vec3 wHalfDir = normalize(fragEyeDir + fragLightDir); // Diffuse Lighting - float diffuseDot = dot(wNor.xyz, lightDir); + float diffuseDot = dot(wNor.xyz, fragLightDir); float facingLight = step(0.0, diffuseDot); vec3 diffuseColor = frag.diffuse * diffuseDot * facingLight; - + // compute the specular multiplier (sans exponent) - float specularPower = facingLight * max(0.0, - dot(eyeDir, wHalfDir)); + float specularPower = facingLight * max(0.0, dot(fragEyeDir, wHalfDir)); vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; - // add specular contribution - /* float ring = (lightAttenuation / (0.2 * getLightAttenuationCutoff(light))) - 1; - if (ring < 1) { - gl_FragColor = vec4(ring * ring * getLightColor(light), 0.0); + // Eval angular attenuation + float lightAngularAttenuation = clamp((cosSpotAngle - getLightSpotAngleCos(light)) / (1.0 - getLightSpotAngleCos(light)), 0.0, 1.0); + + // Show edge + float maxRadius = getLightRadius(light); + float edge = abs(2.0 * ((maxRadius - fragLightDistance) / (0.1)) - 1.0); + if (cosSpotAngle < 1.01 * getLightSpotAngleCos(light)) { + edge = 0; + } + if (edge < 1) { + float edgeCoord = exp2(-8.0*edge*edge); + gl_FragColor = vec4(edgeCoord * edgeCoord * getLightColor(light), 0.0); } else { - gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); - }*/ -} - - -/* - float attenuation = step(lightDistance, radius) * step(gl_LightSource[1].spotCosCutoff, cosSpotAngle) * - pow(cosSpotAngle, gl_LightSource[1].spotExponent) / dot(vec3(gl_LightSource[1].constantAttenuation, - gl_LightSource[1].linearAttenuation, gl_LightSource[1].quadraticAttenuation), - vec3(1.0, lightDistance, lightDistance * lightDistance)); - - - - float lightAttenuation = evalLightAttenuation(light, lightDistance); - - vec4 wNor = invViewMat * vec4(frag.normal, 0.0); - vec4 wEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); - vec3 eyeDir = normalize(wEyeVector.xyz); - vec3 wHalfDir = normalize(eyeDir + lightDir); - // Diffuse Lighting - float diffuseDot = dot(wNor.xyz, lightDir); - float facingLight = step(0.0, diffuseDot); - vec3 diffuseColor = frag.diffuse * diffuseDot * facingLight; - - // compute the specular multiplier (sans exponent) - float specularPower = facingLight * max(0.0, - dot(eyeDir, wHalfDir)); - vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; - - // add specular contribution - gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); - */ -// gl_FragColor = vec4(frag.normal, 0.0); - -//} -/* -// the radius (hard cutoff) of the light effect -uniform float radius; - -void main(void) { - vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; - - DeferredFragment frag = unpackDeferredFragment(texCoord); - - // get the depth and exit early if it doesn't pass the test - float depth = frag.depthVal; - if (depth < gl_FragCoord.z) { - discard; + gl_FragColor = vec4((diffuseColor + specularColor) * lightAngularAttenuation * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); } - - - - // compute the view space position using the depth - float z = near / (depth * depthScale - 1.0); - vec4 position = vec4((depthTexCoordOffset + texCoord * depthTexCoordScale) * z, z, 1.0); - - - // get the normal from the map - vec4 normal = texture2D(normalMap, texCoord); - vec4 normalizedNormal = normalize(normal * 2.0 - vec4(1.0, 1.0, 1.0, 2.0)); - - // compute the base color based on OpenGL lighting model - vec4 lightVector = gl_LightSource[1].position - position; - float lightDistance = length(lightVector); - lightVector = lightVector / lightDistance; - float diffuse = dot(normalizedNormal, lightVector); - float facingLight = step(0.0, diffuse); - vec4 baseColor = texture2D(diffuseMap, texCoord) * (gl_FrontLightProduct[1].ambient + - gl_FrontLightProduct[1].diffuse * (diffuse * facingLight)); - - // compute attenuation based on spot angle, distance, etc. - float cosSpotAngle = max(-dot(lightVector.xyz, gl_LightSource[1].spotDirection), 0.0); - float attenuation = step(lightDistance, radius) * step(gl_LightSource[1].spotCosCutoff, cosSpotAngle) * - pow(cosSpotAngle, gl_LightSource[1].spotExponent) / dot(vec3(gl_LightSource[1].constantAttenuation, - gl_LightSource[1].linearAttenuation, gl_LightSource[1].quadraticAttenuation), - vec3(1.0, lightDistance, lightDistance * lightDistance)); - - // add base to specular, modulate by attenuation - float specular = facingLight * max(0.0, dot(normalize(lightVector - normalize(vec4(position.xyz, 0.0))), - normalizedNormal)); - vec4 specularColor = texture2D(specularMap, texCoord); - gl_FragColor = vec4((baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb) * attenuation, 0.0); - - gl_FragColor = vec4(frag.normal, 0.0); } -*/ + From d8d0a28d0f11cc43f42d7b8b5fc05b6913fd1c18 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Tue, 3 Feb 2015 14:34:11 -0800 Subject: [PATCH 11/24] FInd a solution for lightmap without 2nd uvset --- libraries/fbx/src/FBXReader.cpp | 3 ++- libraries/render-utils/src/GeometryCache.cpp | 9 ++++++++- libraries/render-utils/src/model_lightmap.slv | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libraries/fbx/src/FBXReader.cpp b/libraries/fbx/src/FBXReader.cpp index 3015de52ff..e0487ec76e 100644 --- a/libraries/fbx/src/FBXReader.cpp +++ b/libraries/fbx/src/FBXReader.cpp @@ -1179,7 +1179,7 @@ FBXTexture getTexture(const QString& textureID, texture.transform.setTranslation(p.translation); texture.transform.setRotation(glm::quat(glm::radians(p.rotation))); texture.transform.setScale(p.scaling); - if ((p.UVSet != "map1") || (p.UVSet != "UVSet0")) { + if ((p.UVSet != "map1") && (p.UVSet != "UVSet0")) { texture.texcoordSet = 1; } texture.texcoordSetName = p.UVSet; @@ -1605,6 +1605,7 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping, if (property.name == propertyName) { QString v = property.properties.at(0).toString(); if (property.properties.at(0) == "UVSet") { + std::string uvName = property.properties.at(index).toString().toStdString(); tex.assign(tex.UVSet, property.properties.at(index).toString()); } else if (property.properties.at(0) == "CurrentTextureBlendMode") { tex.assign(tex.currentTextureBlendMode, property.properties.at(index).value()); diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index cb44ecae5c..678f0b2484 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -2120,6 +2120,7 @@ void NetworkGeometry::setGeometry(const FBXGeometry& geometry) { NetworkMesh networkMesh; int totalIndices = 0; + bool checkForTexcoordLightmap = false; foreach (const FBXMeshPart& part, mesh.parts) { NetworkMeshPart networkPart; if (!part.diffuseTexture.filename.isEmpty()) { @@ -2149,6 +2150,7 @@ void NetworkGeometry::setGeometry(const FBXGeometry& geometry) { false, part.emissiveTexture.content); networkPart.emissiveTextureName = part.emissiveTexture.name; networkPart.emissiveTexture->setLoadPriorities(_loadPriorities); + checkForTexcoordLightmap = true; } networkMesh.parts.append(networkPart); @@ -2215,7 +2217,12 @@ void NetworkGeometry::setGeometry(const FBXGeometry& geometry) { if (mesh.tangents.size()) networkMesh._vertexFormat->setAttribute(gpu::Stream::TANGENT, channelNum++, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ)); if (mesh.colors.size()) networkMesh._vertexFormat->setAttribute(gpu::Stream::COLOR, channelNum++, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::RGB)); if (mesh.texCoords.size()) networkMesh._vertexFormat->setAttribute(gpu::Stream::TEXCOORD, channelNum++, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::UV)); - if (mesh.texCoords1.size()) networkMesh._vertexFormat->setAttribute(gpu::Stream::TEXCOORD1, channelNum++, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::UV)); + if (mesh.texCoords1.size()) { + networkMesh._vertexFormat->setAttribute(gpu::Stream::TEXCOORD1, channelNum++, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::UV)); + } else if (checkForTexcoordLightmap && mesh.texCoords.size()) { + // need lightmap texcoord UV but doesn't have uv#1 so just reuse the same channel + networkMesh._vertexFormat->setAttribute(gpu::Stream::TEXCOORD1, channelNum - 1, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::UV)); + } if (mesh.clusterIndices.size()) networkMesh._vertexFormat->setAttribute(gpu::Stream::SKIN_CLUSTER_INDEX, channelNum++, gpu::Element(gpu::VEC4, gpu::NFLOAT, gpu::XYZW)); if (mesh.clusterWeights.size()) networkMesh._vertexFormat->setAttribute(gpu::Stream::SKIN_CLUSTER_WEIGHT, channelNum++, gpu::Element(gpu::VEC4, gpu::NFLOAT, gpu::XYZW)); } diff --git a/libraries/render-utils/src/model_lightmap.slv b/libraries/render-utils/src/model_lightmap.slv index 23d99b399a..1ea5b7b68a 100755 --- a/libraries/render-utils/src/model_lightmap.slv +++ b/libraries/render-utils/src/model_lightmap.slv @@ -33,8 +33,9 @@ void main(void) { // and the texture coordinates gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); + // interpolatedTexcoord1 = vec2(texcoordMatrices[1] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0)).xy; interpolatedTexcoord1 = vec2(texcoordMatrices[1] * vec4(texcoord1.xy, 0.0, 1.0)).xy; - + // use standard pipeline transform gl_Position = ftransform(); } From 1029af140ee67ac881cf43c385bc3d83ac422db1 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Tue, 3 Feb 2015 18:15:20 -0800 Subject: [PATCH 12/24] tiny bit different... --- libraries/render-utils/src/directional_ambient_light.slf | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libraries/render-utils/src/directional_ambient_light.slf b/libraries/render-utils/src/directional_ambient_light.slf index 803bd5ac30..059c0e150f 100755 --- a/libraries/render-utils/src/directional_ambient_light.slf +++ b/libraries/render-utils/src/directional_ambient_light.slf @@ -22,12 +22,13 @@ void main(void) { // Light mapped or not ? if ((frag.normalVal.a >= 0.45) && (frag.normalVal.a <= 0.55)) { - gl_FragColor = vec4( evalLightmappedColor( + vec3 color = evalLightmappedColor( 1.0, frag.normal, frag.diffuse, - frag.specularVal.xyz), - 1.0); + frag.specularVal.xyz); + + gl_FragColor = vec4(color, 1.0); } else { vec3 color = evalAmbientSphereColor(frag.normal, frag.diffuse, frag.specular, frag.gloss) + evalDirectionalColor(1.0, From 7d6161598b8e4202467d50daee9a74aff27dfcd6 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Wed, 4 Feb 2015 13:31:39 -0800 Subject: [PATCH 13/24] REvisiting the shaders for lighting on deferred pass --- .../render-utils/src/DeferredLighting.slh | 33 ++++++++++++------- .../src/directional_ambient_light.slf | 1 + libraries/render-utils/src/spot_light.slf | 12 ++++--- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index 62d2e1e00f..1c9f77c2e0 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -45,6 +45,9 @@ vec4 evalSphericalLight(SphericalHarmonics sh, vec3 direction ) { uniform SphericalHarmonics ambientSphere; +// Everything about light +<@include Light.slh@> + vec3 evalAmbientColor(vec3 normal, vec3 diffuse, vec3 specular, float gloss) { return diffuse.rgb * gl_FrontLightProduct[0].ambient.rgb; } @@ -55,19 +58,27 @@ vec3 evalAmbientSphereColor(vec3 normal, vec3 diffuse, vec3 specular, float glos return diffuse.rgb * ambientLight; } -vec3 evalDirectionalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { +// Frag Shading returns the diffuse amount as W and the specular rgb as xyz +vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float gloss) { // Diffuse Lighting - float diffuseDot = dot(normal, gl_LightSource[0].position.xyz); - float facingLight = step(0.0, diffuseDot) * shadowAttenuation; - vec3 diffuseColor = diffuse * (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; + float diffuseDot = dot(fragNormal, fragLightDir); + float facingLight = step(0.0, diffuseDot); + float diffuse = diffuseDot * facingLight; + + // Specular Lighting depends on the half vector and the gloss + vec3 halfDir = normalize(fragEyeDir + fragLightDir); - // add specular contribution - return vec3(diffuseColor + specularColor); + float specularPower = facingLight * max(0.0, dot(fragEyeDir, halfDir)); + vec3 reflect = pow(specularPower, gloss * 128.0) * specular; + + return vec4(reflect, diffuse); +} + +vec3 evalDirectionalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { + + vec4 shading = evalFragShading(normal, gl_LightSource[0].position.xyz, normalize(position), specular, gloss); + + return vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * gl_FrontLightProduct[0].diffuse.rgb; } diff --git a/libraries/render-utils/src/directional_ambient_light.slf b/libraries/render-utils/src/directional_ambient_light.slf index 059c0e150f..a0b850e925 100755 --- a/libraries/render-utils/src/directional_ambient_light.slf +++ b/libraries/render-utils/src/directional_ambient_light.slf @@ -20,6 +20,7 @@ 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)) { vec3 color = evalLightmappedColor( diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index d8bedc2bea..75c09508ae 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -57,10 +57,14 @@ void main(void) { discard; } - vec4 wNor = invViewMat * vec4(frag.normal, 0.0); + vec4 fragNormal = invViewMat * vec4(frag.normal, 0.0); vec4 fragEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); - vec3 wHalfDir = normalize(fragEyeDir + fragLightDir); + + vec4 shading = evalFragShading(fragNormal, fragLightDir, fragEyeDir, frag.specular, frag.gloss); + + vec3 fragColor = shading.w * (frag.diffuse + shading.xyz); + /* vec3 wHalfDir = normalize(fragEyeDir + fragLightDir); // Diffuse Lighting float diffuseDot = dot(wNor.xyz, fragLightDir); @@ -70,7 +74,7 @@ void main(void) { // compute the specular multiplier (sans exponent) float specularPower = facingLight * max(0.0, dot(fragEyeDir, wHalfDir)); vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; - + */ // Eval angular attenuation float lightAngularAttenuation = clamp((cosSpotAngle - getLightSpotAngleCos(light)) / (1.0 - getLightSpotAngleCos(light)), 0.0, 1.0); @@ -84,7 +88,7 @@ void main(void) { float edgeCoord = exp2(-8.0*edge*edge); gl_FragColor = vec4(edgeCoord * edgeCoord * getLightColor(light), 0.0); } else { - gl_FragColor = vec4((diffuseColor + specularColor) * lightAngularAttenuation * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); + gl_FragColor = vec4(fragColor * lightAngularAttenuation * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); } } From 6c00f29f2b69130583f91ad502aa77ffb7c828ec Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Wed, 4 Feb 2015 14:01:16 -0800 Subject: [PATCH 14/24] REvisiting the shaders for lighting on deferred pass --- libraries/render-utils/src/spot_light.slf | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index 75c09508ae..bbe4107a4d 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -15,6 +15,9 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> +//Everything about deferred lighting +<@include DeferredLighting.slh@> + // Everything about light <@include Light.slh@> @@ -57,7 +60,7 @@ void main(void) { discard; } - vec4 fragNormal = invViewMat * vec4(frag.normal, 0.0); + vec3 fragNormal = vec3(invViewMat * vec4(frag.normal, 0.0)); vec4 fragEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); From 8e1eebbbd4063b8979c17643a4f935021d8693c8 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Wed, 4 Feb 2015 17:24:23 -0800 Subject: [PATCH 15/24] FInally getting the angular attenuation --- libraries/model/src/model/Light.cpp | 11 +++-- libraries/model/src/model/Light.h | 5 ++- .../src/DeferredLightingEffect.cpp | 31 +------------ libraries/render-utils/src/Light.slh | 9 ++-- libraries/render-utils/src/point_light.slf | 42 +++++++++--------- libraries/render-utils/src/spot_light.slf | 44 +++++++------------ 6 files changed, 54 insertions(+), 88 deletions(-) diff --git a/libraries/model/src/model/Light.cpp b/libraries/model/src/model/Light.cpp index 6348ffbcc7..a658c57be9 100755 --- a/libraries/model/src/model/Light.cpp +++ b/libraries/model/src/model/Light.cpp @@ -31,6 +31,7 @@ Light& Light::operator= (const Light& light) { _flags = (light._flags); _schemaBuffer = (light._schemaBuffer); _transform = (light._transform); + _spotConeAngle = (light._spotConeAngle); return (*this); } @@ -67,7 +68,7 @@ void Light::setMaximumRadius(float radius) { if (radius <= 0.f) { radius = 1.0f; } - float CutOffIntensityRatio = 0.01f; + float CutOffIntensityRatio = 0.05f; float surfaceRadius = radius / (sqrt(1.0f / CutOffIntensityRatio) - 1.f); editSchema()._attenuation = Vec4(surfaceRadius, 1.0f/surfaceRadius, CutOffIntensityRatio, radius); } @@ -76,15 +77,17 @@ void Light::setSpotCone(float angle) { if (angle <= 0.f) { angle = 0.0f; } - editSchema()._spot.x = cos(angle); - editSchema()._spot.z = angle; + float cosAngle = cos(angle); + editSchema()._spot.x = cosAngle; + editSchema()._spot.y = sin(angle); + editSchema()._spot.z = 1.0f / cosAngle; + _spotConeAngle = angle; } void Light::setSpotConeExponent(float exponent) { if (exponent <= 0.f) { exponent = 1.0f; } - editSchema()._spot.y = exponent; editSchema()._spot.w = exponent; } diff --git a/libraries/model/src/model/Light.h b/libraries/model/src/model/Light.h index 5e3bda5e1d..c1b8a351e5 100755 --- a/libraries/model/src/model/Light.h +++ b/libraries/model/src/model/Light.h @@ -81,7 +81,7 @@ public: bool isSpot() const { return getType() == SPOT; } void setSpotCone(float angle); void setSpotConeExponent(float exponent); - float getSpotConeAngle() const { return getSchema()._spot.z; } + float getSpotConeAngle() const { return _spotConeAngle; } float getSpotConeExponent() const { return getSchema()._spot.w; } // Schema to access the attribute values of the light @@ -105,7 +105,7 @@ public: _color(1.0f), _intensity(1.0f), _attenuation(1.0f, 1.0f, 1.0f, 1.0f), - _spot(0.0f, 0.0f, 0.0f, 0.0f), + _spot(0.0f, 0.0f, 0.0f, 3.0f), _control(0.0f) {} }; @@ -117,6 +117,7 @@ protected: Flags _flags; UniformBufferView _schemaBuffer; Transform _transform; + float _spotConeAngle = 0.0f; const Schema& getSchema() const { return _schemaBuffer.get(); } Schema& editSchema() { return _schemaBuffer.edit(); } diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index fee3b4c947..e0fd3787f6 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -304,6 +304,7 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu lp->setDirection(direction); lp->setMaximumRadius(radius); lp->setSpotCone(cutoff); + lp->setSpotConeExponent(exponent); lp->setColor(diffuse); lp->setIntensity(1.0f); lp->setType(model::Light::SPOT); @@ -470,25 +471,14 @@ void DeferredLightingEffect::render() { for (auto lightID : _pointLights) { auto light = _allocatedLights[lightID]; - // foreach (const PointLight& light, _pointLights) { + if (_pointLightLocations.lightBufferUnit >= 0) { gpu::Batch batch; batch.setUniformBuffer(_pointLightLocations.lightBufferUnit, light->getSchemaBuffer()); gpu::GLBackend::renderBatch(batch); } glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); - -/* - _pointLight.setUniformValue(_pointLightLocations.radius, light.radius); - glLightfv(GL_LIGHT1, GL_AMBIENT, (const GLfloat*)&light.ambient); - glLightfv(GL_LIGHT1, GL_DIFFUSE, (const GLfloat*)&light.diffuse); - glLightfv(GL_LIGHT1, GL_SPECULAR, (const GLfloat*)&light.specular); - glLightfv(GL_LIGHT1, GL_POSITION, (const GLfloat*)&light.position); - glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, (light.constantAttenuation > 0.0f ? light.constantAttenuation : 0.0f)); - glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, (light.linearAttenuation > 0.0f ? light.linearAttenuation : 0.0f)); - glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, (light.quadraticAttenuation > 0.0f ? light.quadraticAttenuation : 0.0f)); - */ glPushMatrix(); float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); @@ -527,7 +517,6 @@ void DeferredLightingEffect::render() { for (auto lightID : _spotLights) { auto light = _allocatedLights[lightID]; - // foreach (const SpotLight& light, _spotLights) { if (_spotLightLocations.lightBufferUnit >= 0) { gpu::Batch batch; batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer()); @@ -535,22 +524,6 @@ void DeferredLightingEffect::render() { } glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); - // _spotLight.setUniformValue(_spotLightLocations.radius, light->getAttenuationRadius()); - - /* - _spotLight.setUniformValue(_spotLightLocations.radius, light.radius); - glLightfv(GL_LIGHT1, GL_AMBIENT, (const GLfloat*)&light.ambient); - glLightfv(GL_LIGHT1, GL_DIFFUSE, (const GLfloat*)&light.diffuse); - glLightfv(GL_LIGHT1, GL_SPECULAR, (const GLfloat*)&light.specular); - glLightfv(GL_LIGHT1, GL_POSITION, (const GLfloat*)&light.position); - glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, (light.constantAttenuation > 0.0f ? light.constantAttenuation : 0.0f)); - glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, (light.linearAttenuation > 0.0f ? light.linearAttenuation : 0.0f)); - glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, (light.quadraticAttenuation > 0.0f ? light.quadraticAttenuation : 0.0f)); - glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, (const GLfloat*)&light.direction); - glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, light.exponent); - glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, glm::degrees(light.cutoff)); - */ - glPushMatrix(); float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); diff --git a/libraries/render-utils/src/Light.slh b/libraries/render-utils/src/Light.slh index ccf6e52836..ab9bf216de 100755 --- a/libraries/render-utils/src/Light.slh +++ b/libraries/render-utils/src/Light.slh @@ -41,11 +41,12 @@ float getLightSpotAngleCos(Light l) { return l._spot.x; } +vec2 getLightSpotOutsideNormal2(Light l) { + return vec2(-l._spot.y, l._spot.x); +} + float evalLightSpotAttenuation(Light l, float cosA) { - if (cosA > l._spot.x) { - return 0.0; - } - return clamp(pow(cosA / l._spot.x, l._spot.w), 0.0, 1.0); + return pow(cosA * l._spot.z, l._spot.w); } float getLightSquareRadius(Light l) { diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index da2d708f06..4a96f3ae3d 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -15,6 +15,9 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> +//Everything about deferred lighting +<@include DeferredLighting.slh@> + // Everything about light <@include Light.slh@> @@ -22,9 +25,8 @@ uniform mat4 invViewMat; void main(void) { - // get the depth and exit early if it doesn't pass the test + // Grab the fragment data from the uv vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; - DeferredFragment frag = unpackDeferredFragment(texCoord); // Kill if in front of the light volume @@ -37,9 +39,8 @@ void main(void) { Light light = getLight(); // Make the Light vector going from fragment to light center in world space - vec4 wPos; - wPos = invViewMat * frag.position; - vec3 fragLightVec = getLightPosition(light) - wPos.xyz; + vec4 fragPos = invViewMat * frag.position; + vec3 fragLightVec = getLightPosition(light) - fragPos.xyz; // Kill if too far from the light center if (dot(fragLightVec, fragLightVec) > getLightSquareRadius(light)) { @@ -48,31 +49,28 @@ void main(void) { // Allright we re valid in the volume float fragLightDistance = length(fragLightVec); - float lightAttenuation = evalLightAttenuation(light, fragLightDistance); - vec3 fragLightDir = fragLightVec / fragLightDistance; - vec4 wNor = invViewMat * vec4(frag.normal, 0.0); + + + // Eval shading + vec3 fragNormal = vec3(invViewMat * vec4(frag.normal, 0.0)); vec4 fragEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); - vec3 wHalfDir = normalize(fragEyeDir + fragLightDir); + vec4 shading = evalFragShading(fragNormal, fragLightDir, fragEyeDir, frag.specular, frag.gloss); - // Diffuse Lighting - float diffuseDot = dot(wNor.xyz, fragLightDir); - float facingLight = step(0.0, diffuseDot); - vec3 diffuseColor = frag.diffuse * diffuseDot * facingLight; - - // compute the specular multiplier (sans exponent) - float specularPower = facingLight * max(0.0, - dot(fragEyeDir, wHalfDir)); - vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; + // Eval attenuation + float radialAttenuation = evalLightAttenuation(light, fragLightDistance); + // Final Lighting color + vec3 fragColor = shading.w * (frag.diffuse + shading.xyz); + gl_FragColor = vec4(fragColor * radialAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); + +<@if SHOW_LIGHT_CONTOUR@> // Show edge - float maxRadius = getLightRadius(light); - float edge = abs(2.0 * ((maxRadius - fragLightDistance) / (0.1)) - 1.0); + float edge = abs(2.0 * ((getLightRadius(light) - fragLightDistance) / (0.1)) - 1.0); if (edge < 1) { float edgeCoord = exp2(-8.0*edge*edge); gl_FragColor = vec4(edgeCoord * edgeCoord * getLightColor(light), 0.0); - } else { - gl_FragColor = vec4((diffuseColor + specularColor) * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); } +<@endif@> } diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index bbe4107a4d..aec1ce4b89 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -25,9 +25,8 @@ uniform mat4 invViewMat; void main(void) { - // get the depth and exit early if it doesn't pass the test + // Grab the fragment data from the uv vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; - DeferredFragment frag = unpackDeferredFragment(texCoord); // Kill if in front of the light volume @@ -40,8 +39,8 @@ void main(void) { Light light = getLight(); // Make the Light vector going from fragment to light center in world space - vec4 wPos = invViewMat * frag.position; - vec3 fragLightVec = getLightPosition(light) - wPos.xyz; + vec4 fragPos = invViewMat * frag.position; + vec3 fragLightVec = getLightPosition(light) - fragPos.xyz; // Kill if too far from the light center if (dot(fragLightVec, fragLightVec) > getLightSquareRadius(light)) { @@ -50,7 +49,6 @@ void main(void) { // Allright we re valid in the volume float fragLightDistance = length(fragLightVec); - float lightAttenuation = evalLightAttenuation(light, fragLightDistance); vec3 fragLightDir = fragLightVec / fragLightDistance; // Kill if not in the spot light (ah ah !) @@ -60,38 +58,30 @@ void main(void) { discard; } + // Eval shading vec3 fragNormal = vec3(invViewMat * vec4(frag.normal, 0.0)); vec4 fragEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); - vec4 shading = evalFragShading(fragNormal, fragLightDir, fragEyeDir, frag.specular, frag.gloss); + + // Eval attenuation + float radialAttenuation = evalLightAttenuation(light, fragLightDistance); + float angularAttenuation = evalLightSpotAttenuation(light, cosSpotAngle); + // Final Lighting color vec3 fragColor = shading.w * (frag.diffuse + shading.xyz); - /* vec3 wHalfDir = normalize(fragEyeDir + fragLightDir); + gl_FragColor = vec4(fragColor * angularAttenuation * radialAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); - // Diffuse Lighting - float diffuseDot = dot(wNor.xyz, fragLightDir); - float facingLight = step(0.0, diffuseDot); - vec3 diffuseColor = frag.diffuse * diffuseDot * facingLight; - - // compute the specular multiplier (sans exponent) - float specularPower = facingLight * max(0.0, dot(fragEyeDir, wHalfDir)); - vec3 specularColor = pow(specularPower, frag.gloss * 128.0) * frag.specular; - */ - // Eval angular attenuation - float lightAngularAttenuation = clamp((cosSpotAngle - getLightSpotAngleCos(light)) / (1.0 - getLightSpotAngleCos(light)), 0.0, 1.0); - - // Show edge - float maxRadius = getLightRadius(light); - float edge = abs(2.0 * ((maxRadius - fragLightDistance) / (0.1)) - 1.0); - if (cosSpotAngle < 1.01 * getLightSpotAngleCos(light)) { - edge = 0; - } +<@if SHOW_LIGHT_CONTOUR@> + // Show edges + float edgeDistR = (getLightRadius(light) - fragLightDistance); + float edgeDistS = dot(fragLightDistance * vec2(cosSpotAngle, sqrt(1.0 - cosSpotAngle * cosSpotAngle)), -getLightSpotOutsideNormal2(light)); + float edgeDist = min(edgeDistR, edgeDistS); + float edge = abs(2.0 * (edgeDist / (0.1)) - 1.0); if (edge < 1) { float edgeCoord = exp2(-8.0*edge*edge); gl_FragColor = vec4(edgeCoord * edgeCoord * getLightColor(light), 0.0); - } else { - gl_FragColor = vec4(fragColor * lightAngularAttenuation * lightAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); } +<@endif@> } From 00a9f55bc329b41aa41b1ff128dec10e6f0d2741 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 5 Feb 2015 10:20:19 -0800 Subject: [PATCH 16/24] lightings seems to work correctly now --- libraries/model/src/model/Light.cpp | 10 ++++------ libraries/model/src/model/Light.h | 9 ++++----- libraries/render-utils/src/DeferredLightingEffect.cpp | 8 ++++---- libraries/render-utils/src/Light.slh | 2 +- libraries/render-utils/src/point_light.slf | 1 - 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/libraries/model/src/model/Light.cpp b/libraries/model/src/model/Light.cpp index a658c57be9..d66c82ee16 100755 --- a/libraries/model/src/model/Light.cpp +++ b/libraries/model/src/model/Light.cpp @@ -31,7 +31,6 @@ Light& Light::operator= (const Light& light) { _flags = (light._flags); _schemaBuffer = (light._schemaBuffer); _transform = (light._transform); - _spotConeAngle = (light._spotConeAngle); return (*this); } @@ -73,18 +72,17 @@ void Light::setMaximumRadius(float radius) { editSchema()._attenuation = Vec4(surfaceRadius, 1.0f/surfaceRadius, CutOffIntensityRatio, radius); } -void Light::setSpotCone(float angle) { +void Light::setSpotAngle(float angle) { if (angle <= 0.f) { angle = 0.0f; } float cosAngle = cos(angle); - editSchema()._spot.x = cosAngle; + editSchema()._spot.x = cos(angle); editSchema()._spot.y = sin(angle); - editSchema()._spot.z = 1.0f / cosAngle; - _spotConeAngle = angle; + editSchema()._spot.z = angle; } -void Light::setSpotConeExponent(float exponent) { +void Light::setSpotExponent(float exponent) { if (exponent <= 0.f) { exponent = 1.0f; } diff --git a/libraries/model/src/model/Light.h b/libraries/model/src/model/Light.h index c1b8a351e5..c016d54491 100755 --- a/libraries/model/src/model/Light.h +++ b/libraries/model/src/model/Light.h @@ -79,10 +79,10 @@ public: // Spot properties bool isSpot() const { return getType() == SPOT; } - void setSpotCone(float angle); - void setSpotConeExponent(float exponent); - float getSpotConeAngle() const { return _spotConeAngle; } - float getSpotConeExponent() const { return getSchema()._spot.w; } + void setSpotAngle(float angle); + float getSpotAngle() const { return getSchema()._spot.z; } + void setSpotExponent(float exponent); + float getSpotExponent() const { return getSchema()._spot.w; } // Schema to access the attribute values of the light class Schema { @@ -117,7 +117,6 @@ protected: Flags _flags; UniformBufferView _schemaBuffer; Transform _transform; - float _spotConeAngle = 0.0f; const Schema& getSchema() const { return _schemaBuffer.get(); } Schema& editSchema() { return _schemaBuffer.edit(); } diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index e0fd3787f6..61330b8943 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -303,8 +303,8 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu lp->setPosition(position); lp->setDirection(direction); lp->setMaximumRadius(radius); - lp->setSpotCone(cutoff); - lp->setSpotConeExponent(exponent); + lp->setSpotAngle(cutoff); + lp->setSpotExponent(exponent); lp->setColor(diffuse); lp->setIntensity(1.0f); lp->setType(model::Light::SPOT); @@ -527,7 +527,7 @@ void DeferredLightingEffect::render() { glPushMatrix(); float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); - float edgeRadius = expandedRadius / glm::cos(light->getSpotConeAngle()); + float edgeRadius = expandedRadius / glm::cos(light->getSpotAngle()); if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < edgeRadius + nearRadius) { glLoadIdentity(); glTranslatef(0.0f, 0.0f, -1.0f); @@ -547,7 +547,7 @@ void DeferredLightingEffect::render() { glm::vec3 axis = glm::axis(spotRotation); glRotatef(glm::degrees(glm::angle(spotRotation)), axis.x, axis.y, axis.z); glTranslatef(0.0f, 0.0f, -light->getMaximumRadius() * (1.0f + SCALE_EXPANSION * 0.5f)); - geometryCache->renderCone(expandedRadius * glm::tan(light->getSpotConeAngle()), + geometryCache->renderCone(expandedRadius * glm::tan(light->getSpotAngle()), expandedRadius, 32, 1); } diff --git a/libraries/render-utils/src/Light.slh b/libraries/render-utils/src/Light.slh index ab9bf216de..0560898d09 100755 --- a/libraries/render-utils/src/Light.slh +++ b/libraries/render-utils/src/Light.slh @@ -46,7 +46,7 @@ vec2 getLightSpotOutsideNormal2(Light l) { } float evalLightSpotAttenuation(Light l, float cosA) { - return pow(cosA * l._spot.z, l._spot.w); + return pow(cosA, l._spot.w); } float getLightSquareRadius(Light l) { diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index 4a96f3ae3d..3d3d922203 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -51,7 +51,6 @@ void main(void) { float fragLightDistance = length(fragLightVec); vec3 fragLightDir = fragLightVec / fragLightDistance; - // Eval shading vec3 fragNormal = vec3(invViewMat * vec4(frag.normal, 0.0)); vec4 fragEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); From 9b0036d010474d4466e9e2e07f7a9d863cf73162 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 5 Feb 2015 14:10:06 -0800 Subject: [PATCH 17/24] adding Light and replace glLights also for the first deferred pass --- libraries/model/src/model/Light.cpp | 10 +- libraries/model/src/model/Light.h | 4 + .../render-utils/src/DeferredGlobalLight.slh | 122 ++++++++++++++++++ .../render-utils/src/DeferredLighting.slh | 76 ----------- .../src/DeferredLightingEffect.cpp | 66 +++++----- .../render-utils/src/DeferredLightingEffect.h | 5 +- libraries/render-utils/src/Light.slh | 4 + .../src/directional_ambient_light.slf | 5 +- ...onal_ambient_light_cascaded_shadow_map.slf | 5 +- .../directional_ambient_light_shadow_map.slf | 5 +- .../render-utils/src/directional_light.slf | 5 +- .../directional_light_cascaded_shadow_map.slf | 5 +- .../src/directional_light_shadow_map.slf | 5 +- libraries/render-utils/src/point_light.slf | 14 +- libraries/render-utils/src/spot_light.slf | 20 +-- 15 files changed, 202 insertions(+), 149 deletions(-) create mode 100755 libraries/render-utils/src/DeferredGlobalLight.slh diff --git a/libraries/model/src/model/Light.cpp b/libraries/model/src/model/Light.cpp index d66c82ee16..30e873ea30 100755 --- a/libraries/model/src/model/Light.cpp +++ b/libraries/model/src/model/Light.cpp @@ -48,7 +48,7 @@ void Light::setOrientation(const glm::quat& orientation) { } void Light::setDirection(const Vec3& direction) { - editSchema()._direction = direction; + editSchema()._direction = glm::normalize(direction); } const Vec3& Light::getDirection() const { @@ -56,7 +56,7 @@ const Vec3& Light::getDirection() const { } void Light::setColor(const Color& color) { - editSchema()._color = color; + editSchema()._color = glm::normalize(color); } void Light::setIntensity(float intensity) { @@ -89,3 +89,9 @@ void Light::setSpotExponent(float exponent) { editSchema()._spot.w = exponent; } +void Light::setShowVolumeContour(float show) { + if (show <= 0.f) { + show = 0.0f; + } + editSchema()._control.w = show; +} \ No newline at end of file diff --git a/libraries/model/src/model/Light.h b/libraries/model/src/model/Light.h index c016d54491..4640711f4d 100755 --- a/libraries/model/src/model/Light.h +++ b/libraries/model/src/model/Light.h @@ -84,6 +84,10 @@ public: void setSpotExponent(float exponent); float getSpotExponent() const { return getSchema()._spot.w; } + // For editing purpose, show the light volume contour + void setShowVolumeContour(float show); + float getShowVolumeContour() const { return getSchema()._control.w; } + // Schema to access the attribute values of the light class Schema { public: diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh new file mode 100755 index 0000000000..bb33a124dc --- /dev/null +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -0,0 +1,122 @@ + +<@if not DEFERRED_GLOBAL_LIGHT_SLH@> +<@def DEFERRED_GLOBAL_LIGHT_SLH@> + +<@include DeferredLighting.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; +} + +// Need one SH +uniform SphericalHarmonics ambientSphere; + +// Everything about light +<@include Light.slh@> + +// The view Matrix +uniform mat4 invViewMat; + +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) { + + // Need the light now + Light light = getLight(); + vec4 shading = evalFragShading(normal, getLightDirection(light), normalize(position), specular, gloss); + return vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * getLightColor(light) * getLightIntensity(light); + // return vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * gl_FrontLightProduct[0].diffuse.rgb; +} + +vec3 evalAmbienGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { + vec3 color = evalAmbientColor(normal, diffuse, specular, gloss) + + evalDirectionalColor(shadowAttenuation, + position, + normal, + diffuse, + specular, + gloss); + return color; +} +vec3 evalAmbienSphereGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { + vec3 color = evalAmbientSphereColor(normal, diffuse, specular, gloss) + + evalDirectionalColor(shadowAttenuation, + position, + normal, + diffuse, + specular, + gloss); + return color; +} + +vec3 evalLightmappedColor(float shadowAttenuation, vec3 normal, vec3 diffuse, vec3 lightmap) { + + Light light = getLight(); + float diffuseDot = dot(normal, getLightDirection(light)); + + // 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); + + // 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; + + // 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@> diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index 1c9f77c2e0..f6845fbccb 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -11,53 +11,6 @@ <@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; - -// Everything about light -<@include Light.slh@> - -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; -} - // Frag Shading returns the diffuse amount as W and the specular rgb as xyz vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float gloss) { // Diffuse Lighting @@ -74,33 +27,4 @@ vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 s return vec4(reflect, diffuse); } -vec3 evalDirectionalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { - - vec4 shading = evalFragShading(normal, gl_LightSource[0].position.xyz, normalize(position), specular, gloss); - - return vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * gl_FrontLightProduct[0].diffuse.rgb; -} - - -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); - - // 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; - - // 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@> diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 61330b8943..1241845675 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -209,6 +209,17 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) { loadLightProgram(point_light_frag, true, _pointLight, _pointLightLocations); loadLightProgram(spot_light_frag, true, _spotLight, _spotLightLocations); + + // Allocate 2 global lights representing the GLobal Directional light casting shadow (the sun) and the ambient light + _globalLights.push_back(0); + _allocatedLights.push_back(model::LightPointer(new model::Light())); + + model::LightPointer lp = _allocatedLights[0]; + + lp->setDirection(-glm::vec3(1.0f, 1.0f, 1.0f)); + lp->setColor(glm::vec3(1.0f)); + lp->setIntensity(1.0f); + lp->setType(model::Light::SUN); } void DeferredLightingEffect::bindSimpleProgram() { @@ -264,51 +275,27 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu const glm::vec3& diffuse, const glm::vec3& specular, float constantAttenuation, float linearAttenuation, float quadraticAttenuation, const glm::vec3& direction, float exponent, float cutoff) { - int lightID = _pointLights.size() + _spotLights.size(); + int lightID = _pointLights.size() + _spotLights.size() + _globalLights.size(); if (lightID >= _allocatedLights.size()) { _allocatedLights.push_back(model::LightPointer(new model::Light())); } model::LightPointer lp = _allocatedLights[lightID]; - if (exponent == 0.0f && cutoff == PI) { - PointLight light; - light.position = glm::vec4(position, 1.0f); - light.radius = radius; - light.ambient = glm::vec4(ambient, 1.0f); - light.diffuse = glm::vec4(diffuse, 1.0f); - light.specular = glm::vec4(specular, 1.0f); - light.constantAttenuation = constantAttenuation; - light.linearAttenuation = linearAttenuation; + lp->setPosition(position); + lp->setMaximumRadius(radius); + lp->setColor(diffuse); + lp->setIntensity(1.0f); + lp->setShowVolumeContour(quadraticAttenuation); - lp->setPosition(position); - lp->setMaximumRadius(radius); - lp->setColor(diffuse); - lp->setIntensity(1.0f); + if (exponent == 0.0f && cutoff == PI) { lp->setType(model::Light::POINT); _pointLights.push_back(lightID); } else { - SpotLight light; - light.position = glm::vec4(position, 1.0f); - light.radius = radius; - light.ambient = glm::vec4(ambient, 1.0f); - light.diffuse = glm::vec4(diffuse, 1.0f); - light.specular = glm::vec4(specular, 1.0f); - light.constantAttenuation = constantAttenuation; - light.linearAttenuation = linearAttenuation; - light.direction = direction; - light.exponent = exponent; - light.cutoff = cutoff; - - lp->setPosition(position); lp->setDirection(direction); - lp->setMaximumRadius(radius); lp->setSpotAngle(cutoff); lp->setSpotExponent(exponent); - lp->setColor(diffuse); - lp->setIntensity(1.0f); lp->setType(model::Light::SPOT); - _spotLights.push_back(lightID); } } @@ -368,6 +355,10 @@ void DeferredLightingEffect::render() { float tMin = viewport[VIEWPORT_Y_INDEX] / (float)primaryFBO->height(); float tHeight = viewport[VIEWPORT_HEIGHT_INDEX] / (float)primaryFBO->height(); + // Fetch the ViewMatrix; + glm::mat4 invViewMat; + _viewState->getViewTransform().getMatrix(invViewMat); + ProgramObject* program = &_directionalLight; const LightLocations* locations = &_directionalLightLocations; bool shadowsEnabled = _viewState->getShadowsEnabled(); @@ -418,6 +409,17 @@ void DeferredLightingEffect::render() { } } + { + auto light = _allocatedLights[_globalLights.front()]; + + if (locations->lightBufferUnit >= 0) { + gpu::Batch batch; + batch.setUniformBuffer(locations->lightBufferUnit, light->getSchemaBuffer()); + gpu::GLBackend::renderBatch(batch); + } + glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + } + float left, right, bottom, top, nearVal, farVal; glm::vec4 nearClipPlane, farClipPlane; _viewState->computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane); @@ -457,8 +459,6 @@ void DeferredLightingEffect::render() { const glm::vec3& eyePoint = _viewState->getCurrentViewFrustum()->getPosition(); float nearRadius = glm::distance(eyePoint, _viewState->getCurrentViewFrustum()->getNearTopLeft()); - glm::mat4 invViewMat; - _viewState->getViewTransform().getMatrix(invViewMat); auto geometryCache = DependencyManager::get(); diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 8e6606506a..ad4231a0b7 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -91,7 +91,7 @@ public: void setAmbientLightMode(int preset); private: - DeferredLightingEffect() { } + DeferredLightingEffect() {} virtual ~DeferredLightingEffect() { } class LightLocations { @@ -154,10 +154,9 @@ private: typedef std::vector< model::LightPointer > Lights; Lights _allocatedLights; + std::vector _globalLights; std::vector _pointLights; std::vector _spotLights; - // QVector _pointLights; - // QVector _spotLights; QVector _postLightingRenderables; AbstractViewStateInterface* _viewState; diff --git a/libraries/render-utils/src/Light.slh b/libraries/render-utils/src/Light.slh index 0560898d09..abb9fb6c2a 100755 --- a/libraries/render-utils/src/Light.slh +++ b/libraries/render-utils/src/Light.slh @@ -61,6 +61,10 @@ float getLightAttenuationCutoff(Light l) { return l._attenuation.z; } +float getLightShowContour(Light l) { + return l._control.w; +} + <@if GLPROFILE == PC_GL@> uniform lightBuffer { Light light; diff --git a/libraries/render-utils/src/directional_ambient_light.slf b/libraries/render-utils/src/directional_ambient_light.slf index a0b850e925..308a8a73a7 100755 --- a/libraries/render-utils/src/directional_ambient_light.slf +++ b/libraries/render-utils/src/directional_ambient_light.slf @@ -15,7 +15,7 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> -<@include DeferredLighting.slh@> +<@include DeferredGlobalLight.slh@> void main(void) { DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); @@ -31,8 +31,7 @@ void main(void) { gl_FragColor = vec4(color, 1.0); } else { - vec3 color = evalAmbientSphereColor(frag.normal, frag.diffuse, frag.specular, frag.gloss) - + evalDirectionalColor(1.0, + vec3 color = evalAmbienSphereGlobalColor(1.0, frag.position.xyz, frag.normal, frag.diffuse, 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 index 5f88c558d3..db017cf5ac 100755 --- a/libraries/render-utils/src/directional_ambient_light_cascaded_shadow_map.slf +++ b/libraries/render-utils/src/directional_ambient_light_cascaded_shadow_map.slf @@ -15,7 +15,7 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> -<@include DeferredLighting.slh@> +<@include DeferredGlobalLight.slh@> // Everything about shadow <@include Shadow.slh@> @@ -36,8 +36,7 @@ void main(void) { frag.specularVal.xyz), 1.0); } else { - vec3 color = evalAmbientSphereColor(frag.normal, frag.diffuse, frag.specular, frag.gloss) - + evalDirectionalColor(shadowAttenuation, + vec3 color = evalAmbienSphereGlobalColor(shadowAttenuation, frag.position.xyz, frag.normal, frag.diffuse, diff --git a/libraries/render-utils/src/directional_ambient_light_shadow_map.slf b/libraries/render-utils/src/directional_ambient_light_shadow_map.slf index 6c241853e3..43d3e91dbe 100755 --- a/libraries/render-utils/src/directional_ambient_light_shadow_map.slf +++ b/libraries/render-utils/src/directional_ambient_light_shadow_map.slf @@ -15,7 +15,7 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> -<@include DeferredLighting.slh@> +<@include DeferredGlobalLight.slh@> // Everything about shadow <@include Shadow.slh@> @@ -37,8 +37,7 @@ void main(void) { frag.specularVal.xyz), 1.0); } else { - vec3 color = evalAmbientSphereColor(frag.normal, frag.diffuse, frag.specular, frag.gloss) - + evalDirectionalColor(shadowAttenuation, + vec3 color = evalAmbienSphereGlobalColor(shadowAttenuation, frag.position.xyz, frag.normal, frag.diffuse, diff --git a/libraries/render-utils/src/directional_light.slf b/libraries/render-utils/src/directional_light.slf index 8ff6cd6c87..3e708f849e 100644 --- a/libraries/render-utils/src/directional_light.slf +++ b/libraries/render-utils/src/directional_light.slf @@ -15,7 +15,7 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> -<@include DeferredLighting.slh@> +<@include DeferredGlobalLight.slh@> void main(void) { DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); @@ -29,8 +29,7 @@ void main(void) { frag.specularVal.xyz), 1.0); } else { - vec3 color = evalAmbientColor(frag.normal, frag.diffuse, frag.specular, frag.gloss) - + evalDirectionalColor(1.0, + vec3 color = evalAmbienGlobalColor(1.0, frag.position.xyz, frag.normal, frag.diffuse, 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 ccf8909b64..90b3bf1d2b 100644 --- a/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf +++ b/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf @@ -15,7 +15,7 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> -<@include DeferredLighting.slh@> +<@include DeferredGlobalLight.slh@> // Everything about shadow <@include Shadow.slh@> @@ -36,8 +36,7 @@ void main(void) { frag.specularVal.xyz), 1.0); } else { - vec3 color = evalAmbientColor(frag.normal, frag.diffuse, frag.specular, frag.gloss) - + evalDirectionalColor(shadowAttenuation, + vec3 color = evalAmbienGlobalColor(shadowAttenuation, frag.position.xyz, frag.normal, frag.diffuse, diff --git a/libraries/render-utils/src/directional_light_shadow_map.slf b/libraries/render-utils/src/directional_light_shadow_map.slf index 13435e9101..5029b57020 100644 --- a/libraries/render-utils/src/directional_light_shadow_map.slf +++ b/libraries/render-utils/src/directional_light_shadow_map.slf @@ -15,7 +15,7 @@ // Everything about deferred buffer <@include DeferredBuffer.slh@> -<@include DeferredLighting.slh@> +<@include DeferredGlobalLight.slh@> // Everything about shadow <@include Shadow.slh@> @@ -37,8 +37,7 @@ void main(void) { frag.specularVal.xyz), 1.0); } else { - vec3 color = evalAmbientColor(frag.normal, frag.diffuse, frag.specular, frag.gloss) - + evalDirectionalColor(shadowAttenuation, + vec3 color = evalAmbienGlobalColor(shadowAttenuation, frag.position.xyz, frag.normal, frag.diffuse, diff --git a/libraries/render-utils/src/point_light.slf b/libraries/render-utils/src/point_light.slf index 3d3d922203..abe904ecce 100644 --- a/libraries/render-utils/src/point_light.slf +++ b/libraries/render-utils/src/point_light.slf @@ -64,12 +64,12 @@ void main(void) { vec3 fragColor = shading.w * (frag.diffuse + shading.xyz); gl_FragColor = vec4(fragColor * radialAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); -<@if SHOW_LIGHT_CONTOUR@> - // Show edge - float edge = abs(2.0 * ((getLightRadius(light) - fragLightDistance) / (0.1)) - 1.0); - if (edge < 1) { - float edgeCoord = exp2(-8.0*edge*edge); - gl_FragColor = vec4(edgeCoord * edgeCoord * getLightColor(light), 0.0); + if (getLightShowContour(light) > 0.0) { + // Show edge + float edge = abs(2.0 * ((getLightRadius(light) - fragLightDistance) / (0.1)) - 1.0); + if (edge < 1) { + float edgeCoord = exp2(-8.0*edge*edge); + gl_FragColor = vec4(edgeCoord * edgeCoord * getLightShowContour(light) * getLightColor(light), 0.0); + } } -<@endif@> } diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index aec1ce4b89..95fabae3ed 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -72,16 +72,16 @@ void main(void) { vec3 fragColor = shading.w * (frag.diffuse + shading.xyz); gl_FragColor = vec4(fragColor * angularAttenuation * radialAttenuation * getLightColor(light) * getLightIntensity(light), 0.0); -<@if SHOW_LIGHT_CONTOUR@> - // Show edges - float edgeDistR = (getLightRadius(light) - fragLightDistance); - float edgeDistS = dot(fragLightDistance * vec2(cosSpotAngle, sqrt(1.0 - cosSpotAngle * cosSpotAngle)), -getLightSpotOutsideNormal2(light)); - float edgeDist = min(edgeDistR, edgeDistS); - float edge = abs(2.0 * (edgeDist / (0.1)) - 1.0); - if (edge < 1) { - float edgeCoord = exp2(-8.0*edge*edge); - gl_FragColor = vec4(edgeCoord * edgeCoord * getLightColor(light), 0.0); + if (getLightShowContour(light) > 0.0) { + // Show edges + float edgeDistR = (getLightRadius(light) - fragLightDistance); + float edgeDistS = dot(fragLightDistance * vec2(cosSpotAngle, sqrt(1.0 - cosSpotAngle * cosSpotAngle)), -getLightSpotOutsideNormal2(light)); + float edgeDist = min(edgeDistR, edgeDistS); + float edge = abs(2.0 * (edgeDist / (0.1)) - 1.0); + if (edge < 1) { + float edgeCoord = exp2(-8.0*edge*edge); + gl_FragColor = vec4(edgeCoord * edgeCoord * getLightColor(light), 0.0); + } } -<@endif@> } From 1b22a3ef36a8d851346753319f067468cfaa99cf Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 5 Feb 2015 17:30:57 -0800 Subject: [PATCH 18/24] connecting the globalLight info to the DeferredLightingEffect --- interface/src/Application.cpp | 6 +- libraries/model/src/model/Light.cpp | 4 +- libraries/model/src/model/Light.h | 168 +++++++++++++++- .../render-utils/src/DeferredGlobalLight.slh | 61 +++--- .../src/DeferredLightingEffect.cpp | 180 +++--------------- .../render-utils/src/DeferredLightingEffect.h | 17 +- 6 files changed, 222 insertions(+), 214 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 3c1806b00b..e7afd5ac3d 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2585,6 +2585,9 @@ const GLfloat WORLD_AMBIENT_COLOR[] = { 0.525f, 0.525f, 0.6f }; const GLfloat WORLD_DIFFUSE_COLOR[] = { 0.6f, 0.525f, 0.525f }; const GLfloat WORLD_SPECULAR_COLOR[] = { 0.94f, 0.94f, 0.737f, 1.0f }; +const glm::vec3 GLOBAL_LIGHT_COLOR = { 0.6f, 0.525f, 0.525f }; +const float GLOBAL_LIGHT_INTENSITY = 1.0f; + void Application::setupWorldLight() { // Setup 3D lights (after the camera transform, so that they are positioned in world space) @@ -2599,6 +2602,7 @@ void Application::setupWorldLight() { glLightfv(GL_LIGHT0, GL_SPECULAR, WORLD_SPECULAR_COLOR); glMaterialfv(GL_FRONT, GL_SPECULAR, WORLD_SPECULAR_COLOR); glMateriali(GL_FRONT, GL_SHININESS, 96); + } bool Application::shouldRenderMesh(float largestDimension, float distanceToCamera) { @@ -2805,7 +2809,7 @@ void Application::displaySide(Camera& theCamera, bool selfAvatarOnly, RenderArgs { DependencyManager::get()->setAmbientLightMode(getRenderAmbientLight()); - + DependencyManager::get()->setGlobalLight(getSunDirection(), GLOBAL_LIGHT_COLOR, GLOBAL_LIGHT_INTENSITY); PROFILE_RANGE("DeferredLighting"); PerformanceTimer perfTimer("lighting"); DependencyManager::get()->render(); diff --git a/libraries/model/src/model/Light.cpp b/libraries/model/src/model/Light.cpp index 30e873ea30..003a05eb2f 100755 --- a/libraries/model/src/model/Light.cpp +++ b/libraries/model/src/model/Light.cpp @@ -56,7 +56,7 @@ const Vec3& Light::getDirection() const { } void Light::setColor(const Color& color) { - editSchema()._color = glm::normalize(color); + editSchema()._color = color; } void Light::setIntensity(float intensity) { @@ -89,7 +89,7 @@ void Light::setSpotExponent(float exponent) { editSchema()._spot.w = exponent; } -void Light::setShowVolumeContour(float show) { +void Light::setShowContour(float show) { if (show <= 0.f) { show = 0.0f; } diff --git a/libraries/model/src/model/Light.h b/libraries/model/src/model/Light.h index 4640711f4d..2ef2bf3036 100755 --- a/libraries/model/src/model/Light.h +++ b/libraries/model/src/model/Light.h @@ -26,6 +26,161 @@ typedef glm::vec3 Vec3; typedef glm::vec4 Vec4; typedef glm::quat Quat; +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; + + static const int NUM_COEFFICIENTS = 9; + + enum Preset { + 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 assignPreset(int p) { + switch (p) { + case 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 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 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 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 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 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 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 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 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 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; + } + } +}; + class Light { public: enum Type { @@ -84,9 +239,15 @@ public: void setSpotExponent(float exponent); float getSpotExponent() const { return getSchema()._spot.w; } - // For editing purpose, show the light volume contour - void setShowVolumeContour(float show); - float getShowVolumeContour() const { return getSchema()._control.w; } + // For editing purpose, show the light volume contour. + // Set to non 0 to show it, the value is used as the intensity of the contour color + void setShowContour(float show); + float getShowContour() const { return getSchema()._control.w; } + + // Spherical Harmonics storing the Ambien lighting approximation used for the Sun typed light + void setAmbientSphere(const SphericalHarmonics& sphere) { _ambientSphere = sphere; } + const SphericalHarmonics& getAmbientSphere() const { return _ambientSphere; } + void setAmbientSpherePreset(SphericalHarmonics::Preset preset) { _ambientSphere.assignPreset(preset); } // Schema to access the attribute values of the light class Schema { @@ -121,6 +282,7 @@ protected: Flags _flags; UniformBufferView _schemaBuffer; Transform _transform; + SphericalHarmonics _ambientSphere; const Schema& getSchema() const { return _schemaBuffer.get(); } Schema& editSchema() { return _schemaBuffer.edit(); } diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh index bb33a124dc..7ca416a616 100755 --- a/libraries/render-utils/src/DeferredGlobalLight.slh +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -54,47 +54,38 @@ uniform SphericalHarmonics ambientSphere; // The view Matrix uniform mat4 invViewMat; -vec3 evalAmbientColor(vec3 normal, vec3 diffuse, vec3 specular, float gloss) { - return diffuse.rgb * gl_FrontLightProduct[0].ambient.rgb; -} +vec3 evalAmbienGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { -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) { - // Need the light now Light light = getLight(); - vec4 shading = evalFragShading(normal, getLightDirection(light), normalize(position), specular, gloss); - return vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * getLightColor(light) * getLightIntensity(light); - // return vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * gl_FrontLightProduct[0].diffuse.rgb; -} -vec3 evalAmbienGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { - vec3 color = evalAmbientColor(normal, diffuse, specular, gloss) - + evalDirectionalColor(shadowAttenuation, - position, - normal, - diffuse, - specular, - gloss); - return color; + vec3 fragNormal = vec3(invViewMat * vec4(normal, 0.0)); + vec4 fragEyeVector = invViewMat * vec4(-position, 0.0); + vec3 fragEyeDir = normalize(fragEyeVector.xyz); + + vec3 color = diffuse.rgb * getLightColor(light) * getLightIntensity(light); + + vec4 shading = evalFragShading(fragNormal, getLightDirection(light), fragEyeDir, specular, gloss); + + color += vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * getLightColor(light) * getLightIntensity(light); + + return color; } vec3 evalAmbienSphereGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 diffuse, vec3 specular, float gloss) { - vec3 color = evalAmbientSphereColor(normal, diffuse, specular, gloss) - + evalDirectionalColor(shadowAttenuation, - position, - normal, - diffuse, - specular, - gloss); - return color; + // Need the light now + Light light = getLight(); + + vec3 fragNormal = vec3(invViewMat * vec4(normal, 0.0)); + vec4 fragEyeVector = invViewMat * vec4(-position, 0.0); + vec3 fragEyeDir = normalize(fragEyeVector.xyz); + + vec3 color = diffuse.rgb * 0.5 * evalSphericalLight(ambientSphere, fragNormal).xyz; + + vec4 shading = evalFragShading(fragNormal, getLightDirection(light), fragEyeDir, specular, gloss); + + color += vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * getLightColor(light) * getLightIntensity(light); + + return color; } vec3 evalLightmappedColor(float shadowAttenuation, vec3 normal, vec3 diffuse, vec3 lightmap) { diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 1241845675..58f599be97 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -45,146 +45,6 @@ #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; - - static const int NUM_COEFFICIENTS = 9; - - 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; _simpleProgram.addShaderFromSourceCode(QGLShader::Vertex, simple_vert); @@ -220,6 +80,7 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) { lp->setColor(glm::vec3(1.0f)); lp->setIntensity(1.0f); lp->setType(model::Light::SUN); + lp->setAmbientSpherePreset(model::SphericalHarmonics::Preset(_ambientLightMode % model::SphericalHarmonics::NUM_PRESET)); } void DeferredLightingEffect::bindSimpleProgram() { @@ -285,7 +146,7 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu lp->setMaximumRadius(radius); lp->setColor(diffuse); lp->setIntensity(1.0f); - lp->setShowVolumeContour(quadraticAttenuation); + //lp->setShowContour(quadraticAttenuation); if (exponent == 0.0f && cutoff == PI) { lp->setType(model::Light::POINT); @@ -396,25 +257,19 @@ void DeferredLightingEffect::render() { program->bind(); } - if (locations->ambientSphere >= 0) { - SphericalHarmonics sh; - if (_ambientLightMode < NUM_PRESET) { - sh.assignPreset(_ambientLightMode); - } else { - sh.assignPreset(0); - } - - for (int i =0; i setUniformValue(locations->ambientSphere + i, *(((QVector4D*) &sh) + i)); - } - } - { - auto light = _allocatedLights[_globalLights.front()]; - + auto globalLight = _allocatedLights[_globalLights.front()]; + + if (locations->ambientSphere >= 0) { + auto sh = globalLight->getAmbientSphere(); + for (int i =0; i setUniformValue(locations->ambientSphere + i, *(((QVector4D*) &sh) + i)); + } + } + if (locations->lightBufferUnit >= 0) { gpu::Batch batch; - batch.setUniformBuffer(locations->lightBufferUnit, light->getSchemaBuffer()); + batch.setUniformBuffer(locations->lightBufferUnit, globalLight->getSchemaBuffer()); gpu::GLBackend::renderBatch(batch); } glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); @@ -662,7 +517,16 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limit } void DeferredLightingEffect::setAmbientLightMode(int preset) { - if ((preset >= -1) && (preset < NUM_PRESET)) { + if ((preset >= -1) && (preset < model::SphericalHarmonics::NUM_PRESET)) { _ambientLightMode = preset; + auto light = _allocatedLights.front(); + light->setAmbientSpherePreset(model::SphericalHarmonics::Preset(preset % model::SphericalHarmonics::NUM_PRESET)); } } + +void DeferredLightingEffect::setGlobalLight(const glm::vec3& direction, const glm::vec3& diffuse, float intensity) { + auto light = _allocatedLights.front(); + light->setDirection(direction); + light->setColor(diffuse); + light->setIntensity(intensity); +} diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index ad4231a0b7..eeb92f19c7 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -73,22 +73,9 @@ 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, - }; - + // update global lighting void setAmbientLightMode(int preset); + void setGlobalLight(const glm::vec3& direction, const glm::vec3& diffuse, float intensity); private: DeferredLightingEffect() {} From bdf9164c7b1993c004630d53d097d27848a93567 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Fri, 6 Feb 2015 16:30:07 -0800 Subject: [PATCH 19/24] debugging the new global light pass --- interface/src/Application.cpp | 2 +- libraries/gpu/src/gpu/Texture.cpp | 7 +++++-- libraries/render-utils/src/DeferredGlobalLight.slh | 6 +++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index e7afd5ac3d..3049fb7c8e 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2809,7 +2809,7 @@ void Application::displaySide(Camera& theCamera, bool selfAvatarOnly, RenderArgs { DependencyManager::get()->setAmbientLightMode(getRenderAmbientLight()); - DependencyManager::get()->setGlobalLight(getSunDirection(), GLOBAL_LIGHT_COLOR, GLOBAL_LIGHT_INTENSITY); + DependencyManager::get()->setGlobalLight(-getSunDirection(), GLOBAL_LIGHT_COLOR, GLOBAL_LIGHT_INTENSITY); PROFILE_RANGE("DeferredLighting"); PerformanceTimer perfTimer("lighting"); DependencyManager::get()->render(); diff --git a/libraries/gpu/src/gpu/Texture.cpp b/libraries/gpu/src/gpu/Texture.cpp index 33df50042e..b5191b29c7 100755 --- a/libraries/gpu/src/gpu/Texture.cpp +++ b/libraries/gpu/src/gpu/Texture.cpp @@ -76,8 +76,11 @@ bool Texture::Storage::allocateMip(uint16 level) { bool Texture::Storage::assignMipData(uint16 level, const Element& format, Size size, const Byte* bytes) { // Ok we should be able to do that... allocateMip(level); - _mips[level]->_format = format; - Size allocated = _mips[level]->_sysmem.setData(size, bytes); + auto mip = _mips[level]; + mip->_format = format; + Size allocated = mip->_sysmem.setData(size, bytes); + mip->_isGPULoaded = false; + return allocated == size; } diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh index 7ca416a616..e3788bb325 100755 --- a/libraries/render-utils/src/DeferredGlobalLight.slh +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -63,9 +63,9 @@ vec3 evalAmbienGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec4 fragEyeVector = invViewMat * vec4(-position, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); - vec3 color = diffuse.rgb * getLightColor(light) * getLightIntensity(light); + vec3 color = diffuse.rgb * getLightColor(light) * 0.5; - vec4 shading = evalFragShading(fragNormal, getLightDirection(light), fragEyeDir, specular, gloss); + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); color += vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * getLightColor(light) * getLightIntensity(light); @@ -81,7 +81,7 @@ vec3 evalAmbienSphereGlobalColor(float shadowAttenuation, vec3 position, vec3 no vec3 color = diffuse.rgb * 0.5 * evalSphericalLight(ambientSphere, fragNormal).xyz; - vec4 shading = evalFragShading(fragNormal, getLightDirection(light), fragEyeDir, specular, gloss); + vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); color += vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * getLightColor(light) * getLightIntensity(light); From 48fca4c11d0e15c106b5ced2fd13040e4954c22e Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Sun, 8 Feb 2015 23:22:14 -0800 Subject: [PATCH 20/24] introducing PBR shading --- libraries/render-utils/src/DeferredGlobalLight.slh | 8 +++++++- libraries/render-utils/src/DeferredLighting.slh | 11 +++++++++-- libraries/render-utils/src/DeferredLightingEffect.cpp | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh index e3788bb325..2dad20e4d3 100755 --- a/libraries/render-utils/src/DeferredGlobalLight.slh +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -79,12 +79,18 @@ vec3 evalAmbienSphereGlobalColor(float shadowAttenuation, vec3 position, vec3 no vec4 fragEyeVector = invViewMat * vec4(-position, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); - vec3 color = diffuse.rgb * 0.5 * evalSphericalLight(ambientSphere, fragNormal).xyz; + vec3 ambientNormal = normal; + + vec3 color = diffuse.rgb * 0.5 * evalSphericalLight(ambientSphere, ambientNormal).xyz; vec4 shading = evalFragShading(fragNormal, -getLightDirection(light), fragEyeDir, specular, gloss); color += vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * getLightColor(light) * getLightIntensity(light); + if (gl_FragCoord.x > 1000) { + return vec3(shading.rgb); + } + return color; } diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index f6845fbccb..44667bb530 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -21,8 +21,15 @@ vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 s // Specular Lighting depends on the half vector and the gloss vec3 halfDir = normalize(fragEyeDir + fragLightDir); - float specularPower = facingLight * max(0.0, dot(fragEyeDir, halfDir)); - vec3 reflect = pow(specularPower, gloss * 128.0) * specular; + float specularPower = pow(facingLight * max(0.0, dot(halfDir, fragNormal)), gloss * 128.0); + specularPower *= (gloss * 128.0 * 0.125 + 0.25); + + float shlickPower = (1.0 - dot(fragLightDir,halfDir)); + float shlickPower2 = shlickPower * shlickPower; + float shlickPower5 = shlickPower2 * shlickPower2 * shlickPower; + // vec3 schlick = specular * (1.0 - shlickPower5) + vec3(shlickPower5); + vec3 schlick = vec3(0.21) * (1.0 - shlickPower5) + vec3(shlickPower5); + vec3 reflect = specularPower * schlick; return vec4(reflect, diffuse); } diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 58f599be97..6569e3a018 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -425,7 +425,7 @@ void DeferredLightingEffect::render() { glBindTexture(GL_TEXTURE_2D, 0); freeFBO->release(); - // glDisable(GL_FRAMEBUFFER_SRGB); + // glDisable(GL_FRAMEBUFFER_SRGB); glDisable(GL_CULL_FACE); From c2761ac887ec18129d8369866bb711afe2807847 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 9 Feb 2015 15:26:14 -0800 Subject: [PATCH 21/24] CLean up the shader code for the blinn and the new PBR shading equation --- .../render-utils/src/DeferredGlobalLight.slh | 6 ++--- .../render-utils/src/DeferredLighting.slh | 27 ++++++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/libraries/render-utils/src/DeferredGlobalLight.slh b/libraries/render-utils/src/DeferredGlobalLight.slh index 2dad20e4d3..6868b96c24 100755 --- a/libraries/render-utils/src/DeferredGlobalLight.slh +++ b/libraries/render-utils/src/DeferredGlobalLight.slh @@ -79,6 +79,8 @@ vec3 evalAmbienSphereGlobalColor(float shadowAttenuation, vec3 position, vec3 no vec4 fragEyeVector = invViewMat * vec4(-position, 0.0); vec3 fragEyeDir = normalize(fragEyeVector.xyz); + // TODO: The world space normal doesn;t seem to work properly with the current SH definitions + // FoOr now, we use the normal in view space vec3 ambientNormal = normal; vec3 color = diffuse.rgb * 0.5 * evalSphericalLight(ambientSphere, ambientNormal).xyz; @@ -87,10 +89,6 @@ vec3 evalAmbienSphereGlobalColor(float shadowAttenuation, vec3 position, vec3 no color += vec3(diffuse + shading.rgb) * shading.w * shadowAttenuation * getLightColor(light) * getLightIntensity(light); - if (gl_FragCoord.x > 1000) { - return vec3(shading.rgb); - } - return color; } diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index 44667bb530..dff24afdd9 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -12,7 +12,7 @@ <@def DEFERRED_LIGHTING_SLH@> // Frag Shading returns the diffuse amount as W and the specular rgb as xyz -vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float gloss) { +vec4 evalPBRShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float gloss) { // Diffuse Lighting float diffuseDot = dot(fragNormal, fragLightDir); float facingLight = step(0.0, diffuseDot); @@ -21,6 +21,7 @@ vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 s // Specular Lighting depends on the half vector and the gloss vec3 halfDir = normalize(fragEyeDir + fragLightDir); + // float specularPower = pow(facingLight * max(0.0, dot(halfDir, fragNormal)), gloss * 128.0); float specularPower = pow(facingLight * max(0.0, dot(halfDir, fragNormal)), gloss * 128.0); specularPower *= (gloss * 128.0 * 0.125 + 0.25); @@ -34,4 +35,28 @@ vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 s return vec4(reflect, diffuse); } +vec4 evalBlinnShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float gloss) { + // Diffuse Lighting + float diffuseDot = dot(fragNormal, fragLightDir); + float facingLight = step(0.0, diffuseDot); + float diffuse = diffuseDot * facingLight; + + // Specular Lighting depends on the half vector and the gloss + vec3 halfDir = normalize(fragEyeDir + fragLightDir); + + float specularPower = pow(facingLight * max(0.0, dot(halfDir, fragNormal)), gloss * 128.0); + vec3 reflect = specularPower * specular; + + return vec4(reflect, diffuse); +} + +vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float gloss) { + + /* if (gl_FragCoord.x > 1000) { + return evalBlinnShading(fragNormal, fragLightDir, fragEyeDir, specular, gloss); + } else { */ + return evalPBRShading(fragNormal, fragLightDir, fragEyeDir, specular, gloss); + // } +} + <@endif@> From 871f9a849a7d55d9a9bf85c6bbf3a4cc37ae32ca Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 9 Feb 2015 15:30:17 -0800 Subject: [PATCH 22/24] CLean up the shader code for the blinn and the new PBR shading equation --- libraries/render-utils/src/DeferredLighting.slh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index dff24afdd9..73792f67ac 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -28,8 +28,7 @@ vec4 evalPBRShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 sp float shlickPower = (1.0 - dot(fragLightDir,halfDir)); float shlickPower2 = shlickPower * shlickPower; float shlickPower5 = shlickPower2 * shlickPower2 * shlickPower; - // vec3 schlick = specular * (1.0 - shlickPower5) + vec3(shlickPower5); - vec3 schlick = vec3(0.21) * (1.0 - shlickPower5) + vec3(shlickPower5); + vec3 schlick = specular * (1.0 - shlickPower5) + vec3(shlickPower5); vec3 reflect = specularPower * schlick; return vec4(reflect, diffuse); From 23a54b17ce8def25e82a937c20571114dd241729 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 9 Feb 2015 15:33:55 -0800 Subject: [PATCH 23/24] Clean up comments --- 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 6569e3a018..1f5d0ce4c3 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -70,7 +70,7 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) { loadLightProgram(point_light_frag, true, _pointLight, _pointLightLocations); loadLightProgram(spot_light_frag, true, _spotLight, _spotLightLocations); - // Allocate 2 global lights representing the GLobal Directional light casting shadow (the sun) and the ambient light + // Allocate a global light representing the Global Directional light casting shadow (the sun) and the ambient light _globalLights.push_back(0); _allocatedLights.push_back(model::LightPointer(new model::Light())); From 11beb51ec1fcefd7ce9a16615bebebd9d120ed00 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 9 Feb 2015 18:02:34 -0800 Subject: [PATCH 24/24] Clean up comments --- libraries/model/src/model/Light.cpp | 2 +- libraries/render-utils/src/DeferredLighting.slh | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libraries/model/src/model/Light.cpp b/libraries/model/src/model/Light.cpp index 003a05eb2f..9616ed2106 100755 --- a/libraries/model/src/model/Light.cpp +++ b/libraries/model/src/model/Light.cpp @@ -94,4 +94,4 @@ void Light::setShowContour(float show) { show = 0.0f; } editSchema()._control.w = show; -} \ No newline at end of file +} diff --git a/libraries/render-utils/src/DeferredLighting.slh b/libraries/render-utils/src/DeferredLighting.slh index 73792f67ac..bb37a9e3e8 100755 --- a/libraries/render-utils/src/DeferredLighting.slh +++ b/libraries/render-utils/src/DeferredLighting.slh @@ -22,7 +22,7 @@ vec4 evalPBRShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 sp vec3 halfDir = normalize(fragEyeDir + fragLightDir); // float specularPower = pow(facingLight * max(0.0, dot(halfDir, fragNormal)), gloss * 128.0); - float specularPower = pow(facingLight * max(0.0, dot(halfDir, fragNormal)), gloss * 128.0); + float specularPower = pow(max(0.0, dot(halfDir, fragNormal)), gloss * 128.0); specularPower *= (gloss * 128.0 * 0.125 + 0.25); float shlickPower = (1.0 - dot(fragLightDir,halfDir)); @@ -51,11 +51,11 @@ vec4 evalBlinnShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 vec4 evalFragShading(vec3 fragNormal, vec3 fragLightDir, vec3 fragEyeDir, vec3 specular, float gloss) { - /* if (gl_FragCoord.x > 1000) { + /*if (gl_FragCoord.x > 1000) { return evalBlinnShading(fragNormal, fragLightDir, fragEyeDir, specular, gloss); - } else { */ + } else {*/ return evalPBRShading(fragNormal, fragLightDir, fragEyeDir, specular, gloss); - // } + //} } <@endif@>