From db73d8d2f4d897caf89dcb69f907fad95701f22a Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Mon, 14 Jul 2014 12:23:00 -0700 Subject: [PATCH 1/8] add body angular acceleration --- interface/src/Hair.cpp | 12 ++++++++---- interface/src/Hair.h | 2 ++ interface/src/avatar/Avatar.cpp | 6 +++++- interface/src/avatar/Avatar.h | 4 ++++ interface/src/avatar/MyAvatar.cpp | 3 ++- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/interface/src/Hair.cpp b/interface/src/Hair.cpp index e07b624d89..810833d750 100644 --- a/interface/src/Hair.cpp +++ b/interface/src/Hair.cpp @@ -18,7 +18,8 @@ const float HAIR_DAMPING = 0.99f; const float CONSTRAINT_RELAXATION = 10.0f; const float HAIR_ACCELERATION_COUPLING = 0.025f; -const float HAIR_ANGULAR_VELOCITY_COUPLING = 0.10f; +const float HAIR_ANGULAR_VELOCITY_COUPLING = 0.01f; +const float HAIR_ANGULAR_ACCELERATION_COUPLING = 0.001f; const float HAIR_MAX_LINEAR_ACCELERATION = 4.0f; const float HAIR_STIFFNESS = 0.005f; const glm::vec3 HAIR_COLOR1(0.98f, 0.92f, 0.843f); @@ -36,6 +37,7 @@ Hair::Hair(int strands, _radius(radius), _acceleration(0.0f), _angularVelocity(0.0f), + _angularAcceleration(0.0f), _gravity(0.0f) { _hairPosition = new glm::vec3[_strands * _links]; @@ -127,13 +129,15 @@ void Hair::simulate(float deltaTime) { const float ANGULAR_VELOCITY_MIN = 0.001f; if (glm::length(_angularVelocity) > ANGULAR_VELOCITY_MIN) { glm::vec3 yawVector = _hairPosition[vertexIndex]; + glm::vec3 angularVelocity = _angularVelocity * HAIR_ANGULAR_VELOCITY_COUPLING; + glm::vec3 angularAcceleration = _angularAcceleration * HAIR_ANGULAR_ACCELERATION_COUPLING; yawVector.y = 0.f; if (glm::length(yawVector) > EPSILON) { float radius = glm::length(yawVector); yawVector = glm::normalize(yawVector); float angle = atan2f(yawVector.x, -yawVector.z) + PI; glm::vec3 delta = glm::vec3(-1.f, 0.f, 0.f) * glm::angleAxis(angle, glm::vec3(0, 1, 0)); - _hairPosition[vertexIndex] -= delta * radius * _angularVelocity.y * HAIR_ANGULAR_VELOCITY_COUPLING * deltaTime; + _hairPosition[vertexIndex] -= delta * radius * (angularVelocity.y - angularAcceleration.y) * deltaTime; } glm::vec3 pitchVector = _hairPosition[vertexIndex]; pitchVector.x = 0.f; @@ -142,7 +146,7 @@ void Hair::simulate(float deltaTime) { pitchVector = glm::normalize(pitchVector); float angle = atan2f(pitchVector.y, -pitchVector.z) + PI; glm::vec3 delta = glm::vec3(0.0f, 1.0f, 0.f) * glm::angleAxis(angle, glm::vec3(1, 0, 0)); - _hairPosition[vertexIndex] -= delta * radius * _angularVelocity.x * HAIR_ANGULAR_VELOCITY_COUPLING * deltaTime; + _hairPosition[vertexIndex] -= delta * radius * (angularVelocity.x - angularAcceleration.x) * deltaTime; } glm::vec3 rollVector = _hairPosition[vertexIndex]; rollVector.z = 0.f; @@ -151,7 +155,7 @@ void Hair::simulate(float deltaTime) { pitchVector = glm::normalize(rollVector); float angle = atan2f(rollVector.x, rollVector.y) + PI; glm::vec3 delta = glm::vec3(-1.0f, 0.0f, 0.f) * glm::angleAxis(angle, glm::vec3(0, 0, 1)); - _hairPosition[vertexIndex] -= delta * radius * _angularVelocity.z * HAIR_ANGULAR_VELOCITY_COUPLING * deltaTime; + _hairPosition[vertexIndex] -= delta * radius * (angularVelocity.z - angularAcceleration.z) * deltaTime; } } diff --git a/interface/src/Hair.h b/interface/src/Hair.h index 0ca507ee23..12a573db58 100644 --- a/interface/src/Hair.h +++ b/interface/src/Hair.h @@ -40,6 +40,7 @@ public: void render(); void setAcceleration(const glm::vec3& acceleration) { _acceleration = acceleration; } void setAngularVelocity(const glm::vec3& angularVelocity) { _angularVelocity = angularVelocity; } + void setAngularAcceleration(const glm::vec3& angularAcceleration) { _angularAcceleration = angularAcceleration; } void setGravity(const glm::vec3& gravity) { _gravity = gravity; } private: @@ -58,6 +59,7 @@ private: int* _hairConstraints; glm::vec3 _acceleration; glm::vec3 _angularVelocity; + glm::vec3 _angularAcceleration; glm::vec3 _gravity; diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index fd8f2d836b..4645dd6363 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -52,6 +52,8 @@ Avatar::Avatar() : _lastVelocity(0.0f, 0.0f, 0.0f), _acceleration(0.0f, 0.0f, 0.0f), _angularVelocity(0.0f, 0.0f, 0.0f), + _lastAngularVelocity(0.0f, 0.0f, 0.0f), + _angularAcceleration(0.0f, 0.0f, 0.0f), _lastOrientation(), _leanScale(0.5f), _scale(1.0f), @@ -168,7 +170,8 @@ void Avatar::simulate(float deltaTime) { if (Menu::getInstance()->isOptionChecked(MenuOption::StringHair)) { PerformanceTimer perfTimer("hair"); _hair.setAcceleration(getAcceleration() * getHead()->getFinalOrientationInWorldFrame()); - _hair.setAngularVelocity(getAngularVelocity() + getHead()->getAngularVelocity() * getHead()->getFinalOrientationInWorldFrame()); + _hair.setAngularVelocity((getAngularVelocity() + getHead()->getAngularVelocity()) * getHead()->getFinalOrientationInWorldFrame()); + _hair.setAngularAcceleration(getAngularAcceleration() * getHead()->getFinalOrientationInWorldFrame()); _hair.setGravity(Application::getInstance()->getEnvironment()->getGravity(getPosition()) * getHead()->getFinalOrientationInWorldFrame()); _hair.simulate(deltaTime); } @@ -204,6 +207,7 @@ void Avatar::updateAcceleration(float deltaTime) { glm::quat orientation = getOrientation(); glm::quat delta = glm::inverse(_lastOrientation) * orientation; _angularVelocity = safeEulerAngles(delta) * (1.f / deltaTime); + _angularAcceleration = (_angularVelocity - _lastAngularVelocity) * (1.f / deltaTime); _lastOrientation = getOrientation(); } diff --git a/interface/src/avatar/Avatar.h b/interface/src/avatar/Avatar.h index 2d85aa70a5..6a6cdb8511 100755 --- a/interface/src/avatar/Avatar.h +++ b/interface/src/avatar/Avatar.h @@ -152,6 +152,8 @@ public: glm::vec3 getAcceleration() const { return _acceleration; } glm::vec3 getAngularVelocity() const { return _angularVelocity; } + glm::vec3 getAngularAcceleration() const { return _angularAcceleration; } + /// Scales a world space position vector relative to the avatar position and scale /// \param vector position to be scaled. Will store the result @@ -176,6 +178,8 @@ protected: glm::vec3 _lastVelocity; glm::vec3 _acceleration; glm::vec3 _angularVelocity; + glm::vec3 _lastAngularVelocity; + glm::vec3 _angularAcceleration; glm::quat _lastOrientation; float _leanScale; float _scale; diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 3ec6652889..d4bafc388f 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -186,7 +186,8 @@ void MyAvatar::simulate(float deltaTime) { PerformanceTimer perfTimer("hair"); if (Menu::getInstance()->isOptionChecked(MenuOption::StringHair)) { _hair.setAcceleration(getAcceleration() * getHead()->getFinalOrientationInWorldFrame()); - _hair.setAngularVelocity(getAngularVelocity() + getHead()->getAngularVelocity() * getHead()->getFinalOrientationInWorldFrame()); + _hair.setAngularVelocity((getAngularVelocity() + getHead()->getAngularVelocity()) * getHead()->getFinalOrientationInWorldFrame()); + _hair.setAngularAcceleration(getAngularAcceleration() * getHead()->getFinalOrientationInWorldFrame()); _hair.setGravity(Application::getInstance()->getEnvironment()->getGravity(getPosition()) * getHead()->getFinalOrientationInWorldFrame()); _hair.simulate(deltaTime); } From be028c2b01100acc8b5460d42b188c19b3a7f28c Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 22 Jul 2014 14:13:51 -0700 Subject: [PATCH 2/8] Fixes for local lights. --- examples/avatarLocalLight.js | 89 +++++-------------- interface/resources/shaders/model.frag | 29 +++--- .../shaders/model_cascaded_shadow_map.frag | 13 ++- .../model_cascaded_shadow_normal_map.frag | 15 +++- ...l_cascaded_shadow_normal_specular_map.frag | 13 ++- .../model_cascaded_shadow_specular_map.frag | 13 ++- .../resources/shaders/model_normal_map.frag | 20 +++-- .../shaders/model_normal_specular_map.frag | 13 ++- .../resources/shaders/model_shadow_map.frag | 13 ++- .../shaders/model_shadow_normal_map.frag | 13 ++- .../model_shadow_normal_specular_map.frag | 13 ++- .../shaders/model_shadow_specular_map.frag | 13 ++- .../resources/shaders/model_specular_map.frag | 27 +++--- interface/src/Application.cpp | 3 + interface/src/avatar/Avatar.cpp | 16 +--- interface/src/avatar/AvatarManager.cpp | 61 +++---------- interface/src/avatar/AvatarManager.h | 13 +-- interface/src/renderer/Model.cpp | 52 +++++++---- interface/src/renderer/Model.h | 24 +++-- 19 files changed, 225 insertions(+), 228 deletions(-) diff --git a/examples/avatarLocalLight.js b/examples/avatarLocalLight.js index 5768388b45..40f1b9a8ca 100644 --- a/examples/avatarLocalLight.js +++ b/examples/avatarLocalLight.js @@ -10,14 +10,15 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -var localLightDirections = [ {x: 1.0, y:1.0, z: 0.0}, {x: 0.0, y:1.0, z: 1.0} ]; -var localLightColors = [ {x: 0.0, y:1.0, z: 0.0}, {x: 1.0, y:0.0, z: 0.0} ]; +var localLightDirections = [ {x: 1.0, y:0.0, z: 0.0}, {x: 0.0, y:0.0, z: 1.0} ]; +var localLightColors = [ {x: 1.0, y:1.0, z: 1.0}, {x: 1.0, y:1.0, z: 1.0} ]; var currentSelection = 0; -var currentNumLights = 1; +var currentNumLights = 2; var maxNumLights = 2; var currentNumAvatars = 0; var changeDelta = 0.1; +var lightsDirty = true; function keyPressEvent(event) { @@ -45,7 +46,7 @@ function keyPressEvent(event) { localLightColors[currentSelection].x = 0.0; } - setAllLightColors(); + lightsDirty = true; print("CHANGE RED light " + currentSelection + " color (" + localLightColors[currentSelection].x + ", " + localLightColors[currentSelection].y + ", " + localLightColors[currentSelection].z + " )" ); } else if (event.text == "6" ) { @@ -54,7 +55,7 @@ function keyPressEvent(event) { localLightColors[currentSelection].y = 0.0; } - setAllLightColors(); + lightsDirty = true; print("CHANGE GREEN light " + currentSelection + " color (" + localLightColors[currentSelection].x + ", " + localLightColors[currentSelection].y + ", " + localLightColors[currentSelection].z + " )" ); } else if (event.text == "7" ) { @@ -63,7 +64,7 @@ function keyPressEvent(event) { localLightColors[currentSelection].z = 0.0; } - setAllLightColors(); + lightsDirty = true; print("CHANGE BLUE light " + currentSelection + " color (" + localLightColors[currentSelection].x + ", " + localLightColors[currentSelection].y + ", " + localLightColors[currentSelection].z + " )" ); } else if (event.text == "8" ) { @@ -72,7 +73,7 @@ function keyPressEvent(event) { localLightDirections[currentSelection].x = -1.0; } - setAllLightDirections(); + lightsDirty = true; print("PLUS X light " + currentSelection + " direction (" + localLightDirections[currentSelection].x + ", " + localLightDirections[currentSelection].y + ", " + localLightDirections[currentSelection].z + " )" ); } else if (event.text == "9" ) { @@ -81,7 +82,7 @@ function keyPressEvent(event) { localLightDirections[currentSelection].x = 1.0; } - setAllLightDirections(); + lightsDirty = true; print("MINUS X light " + currentSelection + " direction (" + localLightDirections[currentSelection].x + ", " + localLightDirections[currentSelection].y + ", " + localLightDirections[currentSelection].z + " )" ); } else if (event.text == "0" ) { @@ -90,7 +91,7 @@ function keyPressEvent(event) { localLightDirections[currentSelection].y = -1.0; } - setAllLightDirections(); + lightsDirty = true; print("PLUS Y light " + currentSelection + " direction (" + localLightDirections[currentSelection].x + ", " + localLightDirections[currentSelection].y + ", " + localLightDirections[currentSelection].z + " )" ); } else if (event.text == "-" ) { @@ -99,21 +100,13 @@ function keyPressEvent(event) { localLightDirections[currentSelection].y = 1.0; } - setAllLightDirections(); + lightsDirty = true; print("MINUS Y light " + currentSelection + " direction (" + localLightDirections[currentSelection].x + ", " + localLightDirections[currentSelection].y + ", " + localLightDirections[currentSelection].z + " )" ); } else if (event.text == "," ) { if (currentNumLights + 1 <= maxNumLights) { ++currentNumLights; - - for (var i = 0; i < currentNumAvatars; i++) { - AvatarManager.addAvatarLocalLight(i); - - for (var j = 0; j < currentNumLights; j++) { - AvatarManager.setAvatarLightColor(localLightColors[j], j, i); - AvatarManager.setAvatarLightDirection(localLightDirections[j], j, i); - } - } + lightsDirty = true; } print("ADD LIGHT, number of lights " + currentNumLights); @@ -121,16 +114,7 @@ function keyPressEvent(event) { else if (event.text == "." ) { if (currentNumLights - 1 >= 0 ) { --currentNumLights; - - for (var i = 0; i < currentNumAvatars; i++) { - AvatarManager.removeAvatarLocalLight(i); - - for (var j = 0; j < currentNumLights; j++) { - AvatarManager.setAvatarLightColor(localLightColors[j], j, i); - AvatarManager.setAvatarLightDirection(localLightDirections[j], j, i); - } - } - + lightsDirty = true; } print("REMOVE LIGHT, number of lights " + currentNumLights); @@ -139,45 +123,14 @@ function keyPressEvent(event) { function updateLocalLights() { - // new avatars, so add lights - var numAvatars = AvatarManager.getNumAvatars(); - if (numAvatars != currentNumAvatars) { - - for (var i = 0; i < numAvatars; i++) { - var numLights = AvatarManager.getNumLightsInAvatar(i); - - // check if new avatar has lights - if (numLights <= 0) { - AvatarManager.addAvatarLocalLight(i); - - // set color and direction for new avatar - for (var j = 0; j < maxNumLights; j++) { - AvatarManager.setAvatarLightColor(localLightColors[j], j, i); - AvatarManager.setAvatarLightDirection(localLightDirections[j], j, i); - } - } - } - - currentNumAvatars = numAvatars; - } -} - -function setAllLightColors() -{ - for (var i = 0; i < currentNumAvatars; i++) { - for (var j = 0; j < maxNumLights; j++) { - AvatarManager.setAvatarLightColor(localLightColors[j], j, i); - } - } -} - -function setAllLightDirections() -{ - for (var i = 0; i < currentNumAvatars; i++) { - for (var j = 0; j < maxNumLights; j++) { - AvatarManager.setAvatarLightDirection(localLightDirections[j], j, i); - } - } + if (lightsDirty) { + var localLights = []; + for (var i = 0; i < currentNumLights; i++) { + localLights.push({ direction: localLightDirections[i], color: localLightColors[i] }); + } + AvatarManager.setLocalLights(localLights); + lightsDirty = false; + } } // main diff --git a/interface/resources/shaders/model.frag b/interface/resources/shaders/model.frag index 468a892686..27f6510477 100644 --- a/interface/resources/shaders/model.frag +++ b/interface/resources/shaders/model.frag @@ -11,16 +11,12 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// the maximum number of local lights to apply +const int MAX_LOCAL_LIGHTS = 2; // the diffuse texture uniform sampler2D diffuseMap; -// local lights -const int MAX_LOCAL_LIGHTS = 2; // 2 lights for now, will probably need more later on -uniform int numLocalLights; -uniform vec3 localLightDirections[MAX_LOCAL_LIGHTS]; -uniform vec3 localLightColors[MAX_LOCAL_LIGHTS]; - // the interpolated position varying vec4 position; @@ -28,23 +24,18 @@ varying vec4 position; varying vec4 normal; void main(void) { - // compute the base color based on OpenGL lighting model + // add up the local lights vec4 normalizedNormal = normalize(normal); - float diffuse = dot(normalizedNormal, gl_LightSource[0].position); - float facingLight = step(0.0, diffuse); - - // the local light that is always present - vec4 totalLocalLight = vec4(0.0, 0.0, 0.0, 1.0); - for (int i = 0; i < numLocalLights; i++) { - float localDiffuse = dot(normalizedNormal, vec4(localLightDirections[i], 1.0)); - float localLight = step(0.0, localDiffuse); - float localLightVal = localDiffuse * localLight; - - totalLocalLight += (localLightVal * vec4( localLightColors[i], 0.0)); + vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); + for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { + localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[i].position)); } + // compute the base color based on OpenGL lighting model + float diffuse = dot(normalizedNormal, gl_LightSource[0].position); + float facingLight = step(0.0, diffuse); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + totalLocalLight); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + localLight); // compute the specular component (sans exponent) float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - normalize(vec4(position.xyz, 0.0))), diff --git a/interface/resources/shaders/model_cascaded_shadow_map.frag b/interface/resources/shaders/model_cascaded_shadow_map.frag index 720c43b656..35026cb3b2 100644 --- a/interface/resources/shaders/model_cascaded_shadow_map.frag +++ b/interface/resources/shaders/model_cascaded_shadow_map.frag @@ -11,6 +11,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// the maximum number of local lights to apply +const int MAX_LOCAL_LIGHTS = 2; + // the diffuse texture uniform sampler2D diffuseMap; @@ -34,9 +37,15 @@ void main(void) { int shadowIndex = int(dot(step(vec3(position.z), shadowDistances), vec3(1.0, 1.0, 1.0))); vec3 shadowTexCoord = vec3(dot(gl_EyePlaneS[shadowIndex], position), dot(gl_EyePlaneT[shadowIndex], position), dot(gl_EyePlaneR[shadowIndex], position)); + + // add up the local lights + vec4 normalizedNormal = normalize(normal); + vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); + for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { + localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[i].position)); + } // compute the base color based on OpenGL lighting model - vec4 normalizedNormal = normalize(normal); float diffuse = dot(normalizedNormal, gl_LightSource[0].position); float facingLight = step(0.0, diffuse) * 0.25 * (shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, -shadowScale, 0.0)).r + @@ -44,7 +53,7 @@ void main(void) { shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, -shadowScale, 0.0)).r + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, shadowScale, 0.0)).r); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + localLight); // compute the specular component (sans exponent) float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - normalize(vec4(position.xyz, 0.0))), diff --git a/interface/resources/shaders/model_cascaded_shadow_normal_map.frag b/interface/resources/shaders/model_cascaded_shadow_normal_map.frag index 5758333392..0e474c173a 100644 --- a/interface/resources/shaders/model_cascaded_shadow_normal_map.frag +++ b/interface/resources/shaders/model_cascaded_shadow_normal_map.frag @@ -11,6 +11,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// the maximum number of local lights to apply +const int MAX_LOCAL_LIGHTS = 2; + // the diffuse texture uniform sampler2D diffuseMap; @@ -46,10 +49,16 @@ void main(void) { vec3 shadowTexCoord = vec3(dot(gl_EyePlaneS[shadowIndex], interpolatedPosition), dot(gl_EyePlaneT[shadowIndex], interpolatedPosition), dot(gl_EyePlaneR[shadowIndex], interpolatedPosition)); - - // compute the base color based on OpenGL lighting model + + // add up the local lights vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); + vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); + for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { + localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(viewNormal, gl_LightSource[i].position)); + } + + // compute the base color based on OpenGL lighting model float diffuse = dot(viewNormal, gl_LightSource[0].position); float facingLight = step(0.0, diffuse) * 0.25 * (shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, -shadowScale, 0.0)).r + @@ -57,7 +66,7 @@ void main(void) { shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, -shadowScale, 0.0)).r + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, shadowScale, 0.0)).r); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + localLight); // compute the specular component (sans exponent) float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - diff --git a/interface/resources/shaders/model_cascaded_shadow_normal_specular_map.frag b/interface/resources/shaders/model_cascaded_shadow_normal_specular_map.frag index 2b949710f3..33c025925e 100644 --- a/interface/resources/shaders/model_cascaded_shadow_normal_specular_map.frag +++ b/interface/resources/shaders/model_cascaded_shadow_normal_specular_map.frag @@ -11,6 +11,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// the maximum number of local lights to apply +const int MAX_LOCAL_LIGHTS = 2; + // the diffuse texture uniform sampler2D diffuseMap; @@ -50,9 +53,15 @@ void main(void) { dot(gl_EyePlaneT[shadowIndex], interpolatedPosition), dot(gl_EyePlaneR[shadowIndex], interpolatedPosition)); - // compute the base color based on OpenGL lighting model + // add up the local lights vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); + vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); + for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { + localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(viewNormal, gl_LightSource[i].position)); + } + + // compute the base color based on OpenGL lighting model float diffuse = dot(viewNormal, gl_LightSource[0].position); float facingLight = step(0.0, diffuse) * 0.25 * (shadow2D(shadowMap, shadowTexCoord + vec3(-shadowScale, -shadowScale, 0.0)).r + @@ -60,7 +69,7 @@ void main(void) { shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, -shadowScale, 0.0)).r + shadow2D(shadowMap, shadowTexCoord + vec3(shadowScale, shadowScale, 0.0)).r); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + localLight); // compute the specular component (sans exponent) float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - diff --git a/interface/resources/shaders/model_cascaded_shadow_specular_map.frag b/interface/resources/shaders/model_cascaded_shadow_specular_map.frag index ba8ba6b85b..9190654431 100644 --- a/interface/resources/shaders/model_cascaded_shadow_specular_map.frag +++ b/interface/resources/shaders/model_cascaded_shadow_specular_map.frag @@ -11,6 +11,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// the maximum number of local lights to apply +const int MAX_LOCAL_LIGHTS = 2; + // the diffuse texture uniform sampler2D diffuseMap; @@ -38,8 +41,14 @@ void main(void) { vec3 shadowTexCoord = vec3(dot(gl_EyePlaneS[shadowIndex], position), dot(gl_EyePlaneT[shadowIndex], position), dot(gl_EyePlaneR[shadowIndex], position)); - // compute the base color based on OpenGL lighting model + // add up the local lights vec4 normalizedNormal = normalize(normal); + vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); + for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { + localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[i].position)); + } + + // compute the base color based on OpenGL lighting model float diffuse = dot(normalizedNormal, gl_LightSource[0].position); float facingLight = step(0.0, diffuse) * 0.25 * (shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(-shadowScale, -shadowScale, 0.0)).r + @@ -47,7 +56,7 @@ void main(void) { shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, -shadowScale, 0.0)).r + shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, shadowScale, 0.0)).r); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + localLight); // compute the specular component (sans exponent) float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - normalize(vec4(position.xyz, 0.0))), diff --git a/interface/resources/shaders/model_normal_map.frag b/interface/resources/shaders/model_normal_map.frag index ca0201f6ab..acd049dc47 100644 --- a/interface/resources/shaders/model_normal_map.frag +++ b/interface/resources/shaders/model_normal_map.frag @@ -11,6 +11,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// the maximum number of local lights to apply +const int MAX_LOCAL_LIGHTS = 2; + // the diffuse texture uniform sampler2D diffuseMap; @@ -32,19 +35,20 @@ void main(void) { vec3 normalizedBitangent = normalize(cross(normalizedNormal, normalizedTangent)); vec3 localNormal = vec3(texture2D(normalMap, gl_TexCoord[0].st)) * 2.0 - vec3(1.0, 1.0, 1.0); - // compute the base color based on OpenGL lighting model + // add up the local lights vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); + vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); + for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { + localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(viewNormal, gl_LightSource[i].position)); + } + + // compute the base color based on OpenGL lighting model float diffuse = dot(viewNormal, gl_LightSource[0].position); float facingLight = step(0.0, diffuse); - float localDiffuse = dot(viewNormal, gl_LightSource[1].position); - float localLight = step(0.0, localDiffuse); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + gl_FrontLightProduct[1].diffuse * (localDiffuse * localLight)); - - - - + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + localLight); + // compute the specular component (sans exponent) float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - normalize(vec4(vec3(interpolatedPosition), 0.0))), viewNormal)); diff --git a/interface/resources/shaders/model_normal_specular_map.frag b/interface/resources/shaders/model_normal_specular_map.frag index 357677d82a..fa997bff7f 100644 --- a/interface/resources/shaders/model_normal_specular_map.frag +++ b/interface/resources/shaders/model_normal_specular_map.frag @@ -11,6 +11,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// the maximum number of local lights to apply +const int MAX_LOCAL_LIGHTS = 2; + // the diffuse texture uniform sampler2D diffuseMap; @@ -35,13 +38,19 @@ void main(void) { vec3 normalizedBitangent = normalize(cross(normalizedNormal, normalizedTangent)); vec3 localNormal = vec3(texture2D(normalMap, gl_TexCoord[0].st)) * 2.0 - vec3(1.0, 1.0, 1.0); - // compute the base color based on OpenGL lighting model + // add up the local lights vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); + vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); + for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { + localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(viewNormal, gl_LightSource[i].position)); + } + + // compute the base color based on OpenGL lighting model float diffuse = dot(viewNormal, gl_LightSource[0].position); float facingLight = step(0.0, diffuse); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + localLight); // compute the specular component (sans exponent) float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - diff --git a/interface/resources/shaders/model_shadow_map.frag b/interface/resources/shaders/model_shadow_map.frag index aa1df03b95..c2bf95fd27 100644 --- a/interface/resources/shaders/model_shadow_map.frag +++ b/interface/resources/shaders/model_shadow_map.frag @@ -11,6 +11,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// the maximum number of local lights to apply +const int MAX_LOCAL_LIGHTS = 2; + // the diffuse texture uniform sampler2D diffuseMap; @@ -27,8 +30,14 @@ varying vec4 position; varying vec4 normal; void main(void) { - // compute the base color based on OpenGL lighting model + // add up the local lights vec4 normalizedNormal = normalize(normal); + vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); + for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { + localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[i].position)); + } + + // compute the base color based on OpenGL lighting model float diffuse = dot(normalizedNormal, gl_LightSource[0].position); float facingLight = step(0.0, diffuse) * 0.25 * (shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(-shadowScale, -shadowScale, 0.0)).r + @@ -36,7 +45,7 @@ void main(void) { shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, -shadowScale, 0.0)).r + shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, shadowScale, 0.0)).r); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + localLight); // compute the specular component (sans exponent) float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - normalize(vec4(position.xyz, 0.0))), diff --git a/interface/resources/shaders/model_shadow_normal_map.frag b/interface/resources/shaders/model_shadow_normal_map.frag index 3461c1b5f3..058c3783be 100644 --- a/interface/resources/shaders/model_shadow_normal_map.frag +++ b/interface/resources/shaders/model_shadow_normal_map.frag @@ -11,6 +11,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// the maximum number of local lights to apply +const int MAX_LOCAL_LIGHTS = 2; + // the diffuse texture uniform sampler2D diffuseMap; @@ -38,9 +41,15 @@ void main(void) { vec3 normalizedBitangent = normalize(cross(normalizedNormal, normalizedTangent)); vec3 localNormal = vec3(texture2D(normalMap, gl_TexCoord[0].st)) * 2.0 - vec3(1.0, 1.0, 1.0); - // compute the base color based on OpenGL lighting model + // add up the local lights vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); + vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); + for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { + localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(viewNormal, gl_LightSource[i].position)); + } + + // compute the base color based on OpenGL lighting model float diffuse = dot(viewNormal, gl_LightSource[0].position); float facingLight = step(0.0, diffuse) * 0.25 * (shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(-shadowScale, -shadowScale, 0.0)).r + @@ -48,7 +57,7 @@ void main(void) { shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, -shadowScale, 0.0)).r + shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, shadowScale, 0.0)).r); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + localLight); // compute the specular component (sans exponent) float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - diff --git a/interface/resources/shaders/model_shadow_normal_specular_map.frag b/interface/resources/shaders/model_shadow_normal_specular_map.frag index 273d197fca..5b8d7c87a4 100644 --- a/interface/resources/shaders/model_shadow_normal_specular_map.frag +++ b/interface/resources/shaders/model_shadow_normal_specular_map.frag @@ -11,6 +11,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// the maximum number of local lights to apply +const int MAX_LOCAL_LIGHTS = 2; + // the diffuse texture uniform sampler2D diffuseMap; @@ -41,9 +44,15 @@ void main(void) { vec3 normalizedBitangent = normalize(cross(normalizedNormal, normalizedTangent)); vec3 localNormal = vec3(texture2D(normalMap, gl_TexCoord[0].st)) * 2.0 - vec3(1.0, 1.0, 1.0); - // compute the base color based on OpenGL lighting model + // add up the local lights vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); + vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); + for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { + localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(viewNormal, gl_LightSource[i].position)); + } + + // compute the base color based on OpenGL lighting model float diffuse = dot(viewNormal, gl_LightSource[0].position); float facingLight = step(0.0, diffuse) * 0.25 * (shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(-shadowScale, -shadowScale, 0.0)).r + @@ -51,7 +60,7 @@ void main(void) { shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, -shadowScale, 0.0)).r + shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, shadowScale, 0.0)).r); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + localLight); // compute the specular component (sans exponent) float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - diff --git a/interface/resources/shaders/model_shadow_specular_map.frag b/interface/resources/shaders/model_shadow_specular_map.frag index 77cff1e04e..9f413f63a4 100644 --- a/interface/resources/shaders/model_shadow_specular_map.frag +++ b/interface/resources/shaders/model_shadow_specular_map.frag @@ -11,6 +11,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// the maximum number of local lights to apply +const int MAX_LOCAL_LIGHTS = 2; + // the diffuse texture uniform sampler2D diffuseMap; @@ -30,8 +33,14 @@ varying vec4 position; varying vec4 normal; void main(void) { - // compute the base color based on OpenGL lighting model + // add up the local lights vec4 normalizedNormal = normalize(normal); + vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); + for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { + localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[i].position)); + } + + // compute the base color based on OpenGL lighting model float diffuse = dot(normalizedNormal, gl_LightSource[0].position); float facingLight = step(0.0, diffuse) * 0.25 * (shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(-shadowScale, -shadowScale, 0.0)).r + @@ -39,7 +48,7 @@ void main(void) { shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, -shadowScale, 0.0)).r + shadow2D(shadowMap, gl_TexCoord[1].stp + vec3(shadowScale, shadowScale, 0.0)).r); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + localLight); // compute the specular component (sans exponent) float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - normalize(vec4(position.xyz, 0.0))), diff --git a/interface/resources/shaders/model_specular_map.frag b/interface/resources/shaders/model_specular_map.frag index 329da65e9e..54790a36fb 100644 --- a/interface/resources/shaders/model_specular_map.frag +++ b/interface/resources/shaders/model_specular_map.frag @@ -10,11 +10,9 @@ // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -const int MAX_LOCAL_LIGHTS = 2; -uniform int numLocalLights; -uniform vec3 localLightDirections[MAX_LOCAL_LIGHTS]; -uniform vec3 localLightColors[MAX_LOCAL_LIGHTS]; +// the maximum number of local lights to apply +const int MAX_LOCAL_LIGHTS = 2; // the diffuse texture uniform sampler2D diffuseMap; @@ -29,23 +27,18 @@ varying vec4 position; varying vec4 normal; void main(void) { - // compute the base color based on OpenGL lighting model + // add up the local lights vec4 normalizedNormal = normalize(normal); - float diffuse = dot(normalizedNormal, gl_LightSource[0].position); - float facingLight = step(0.0, diffuse); - - // the local light that is always present - vec4 totalLocalLight = vec4(0.0, 0.0, 0.0, 1.0); - for (int i = 0; i < numLocalLights; i++) { - float localDiffuse = dot(normalizedNormal, vec4(localLightDirections[i], 1.0)); - float localLight = step(0.0, localDiffuse); - float localLightVal = localDiffuse * localLight; - - totalLocalLight += (localLightVal * vec4( localLightColors[i], 0.0)); + vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); + for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { + localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[i].position)); } + // compute the base color based on OpenGL lighting model + float diffuse = dot(normalizedNormal, gl_LightSource[0].position); + float facingLight = step(0.0, diffuse); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + totalLocalLight); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight) + localLight); // compute the specular component (sans exponent) float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position - normalize(vec4(position.xyz, 0.0))), diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index aad9c4de58..1d0bf8d6a3 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3628,6 +3628,9 @@ ScriptEngine* Application::loadScript(const QString& scriptName, bool loadScript scriptEngine->getModelsScriptingInterface()->setPacketSender(&_modelEditSender); scriptEngine->getModelsScriptingInterface()->setModelTree(_models.getTree()); + // model has some custom types + Model::registerMetaTypes(scriptEngine->getEngine()); + // hook our avatar object into this script engine scriptEngine->setAvatarData(_myAvatar, "MyAvatar"); // leave it as a MyAvatar class to expose thrust features diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index b5590dec09..cddd85964c 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -245,19 +245,9 @@ void Avatar::render(const glm::vec3& cameraPosition, RenderMode renderMode) { // local lights directions and colors - getSkeletonModel().setNumLocalLights(_numLocalLights); - getHead()->getFaceModel().setNumLocalLights(_numLocalLights); - for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { - glm::vec3 normalized = glm::normalize(_localLightDirections[i]); - - // body - getSkeletonModel().setLocalLightColor(_localLightColors[i], i); - getSkeletonModel().setLocalLightDirection(normalized, i); - - // head - getHead()->getFaceModel().setLocalLightColor(_localLightColors[i], i); - getHead()->getFaceModel().setLocalLightDirection(_localLightDirections[i], i); - } + const QVector& localLights = Application::getInstance()->getAvatarManager().getLocalLights(); + _skeletonModel.setLocalLights(localLights); + getHead()->getFaceModel().setLocalLights(localLights); // render body if (Menu::getInstance()->isOptionChecked(MenuOption::Avatars)) { diff --git a/interface/src/avatar/AvatarManager.cpp b/interface/src/avatar/AvatarManager.cpp index c172df5de6..0db12276ad 100644 --- a/interface/src/avatar/AvatarManager.cpp +++ b/interface/src/avatar/AvatarManager.cpp @@ -159,58 +159,21 @@ void AvatarManager::clearOtherAvatars() { _myAvatar->clearLookAtTargetAvatar(); } -Avatar* AvatarManager::getAvatarFromIndex(int avatarIndex) { - Avatar* avatar = NULL; - int numAvatars = _avatarHash.count(); - if (avatarIndex < numAvatars) { - QUuid key = (_avatarHash.keys())[avatarIndex]; - - const AvatarSharedPointer& avatarPointer = _avatarHash.value(key); - avatar = static_cast(avatarPointer.data()); +void AvatarManager::setLocalLights(const QVector& localLights) { + if (QThread::currentThread() != thread()) { + QMetaObject::invokeMethod(this, "setLocalLights", Q_ARG(const QVector&, localLights)); + return; } - - return avatar; + _localLights = localLights; } -void AvatarManager::addAvatarLocalLight(int avatarIndex) { - Avatar* avatar = getAvatarFromIndex(avatarIndex); - if (avatar) { - avatar->addLocalLight(); +QVector AvatarManager::getLocalLights() const { + if (QThread::currentThread() != thread()) { + QVector result; + QMetaObject::invokeMethod(const_cast(this), "getLocalLights", Qt::BlockingQueuedConnection, + Q_RETURN_ARG(QVector, result)); + return result; } + return _localLights; } -void AvatarManager::removeAvatarLocalLight(int avatarIndex) { - Avatar* avatar = getAvatarFromIndex(avatarIndex); - if (avatar) { - avatar->removeLocalLight(); - } -} - -void AvatarManager::setAvatarLightDirection(const glm::vec3& direction, int lightIndex, int avatarIndex) { - Avatar* avatar = getAvatarFromIndex(avatarIndex); - if (avatar) { - avatar->setLocalLightDirection(direction, lightIndex); - } -} - -void AvatarManager::setAvatarLightColor(const glm::vec3& color, int lightIndex, int avatarIndex) { - Avatar* avatar = getAvatarFromIndex(avatarIndex); - if (avatar) { - avatar->setLocalLightColor(color, lightIndex); - } -} - -int AvatarManager::getNumLightsInAvatar(int avatarIndex) { - int numLights = 0; - - Avatar* avatar = getAvatarFromIndex(avatarIndex); - if (avatar) { - numLights = avatar->getNumLocalLights(); - } - - return numLights; -} - -int AvatarManager::getNumAvatars() { - return _avatarHash.count(); -} diff --git a/interface/src/avatar/AvatarManager.h b/interface/src/avatar/AvatarManager.h index e6f058c0ab..ccb7459217 100644 --- a/interface/src/avatar/AvatarManager.h +++ b/interface/src/avatar/AvatarManager.h @@ -37,13 +37,8 @@ public: void clearOtherAvatars(); -public slots: - void setAvatarLightColor(const glm::vec3& color, int lightIndex, int avatarIndex); - void setAvatarLightDirection(const glm::vec3& direction, int lightIndex, int avatarIndex); - void removeAvatarLocalLight(int avatarIndex); - void addAvatarLocalLight(int avatarIndex); - int getNumLightsInAvatar(int avatarIndex); - int getNumAvatars(); + Q_INVOKABLE void setLocalLights(const QVector& localLights); + Q_INVOKABLE QVector getLocalLights() const; private: AvatarManager(const AvatarManager& other); @@ -53,13 +48,13 @@ private: AvatarSharedPointer newSharedAvatar(); - Avatar* getAvatarFromIndex(int avatarIndex); - // virtual override AvatarHash::iterator erase(const AvatarHash::iterator& iterator); QVector _avatarFades; QSharedPointer _myAvatar; + + QVector _localLights; }; #endif // hifi_AvatarManager_h diff --git a/interface/src/renderer/Model.cpp b/interface/src/renderer/Model.cpp index d65cb014c9..c5c83aa01d 100644 --- a/interface/src/renderer/Model.cpp +++ b/interface/src/renderer/Model.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -19,6 +20,7 @@ #include #include #include +#include #include #include @@ -31,6 +33,23 @@ static int modelPointerTypeId = qRegisterMetaType >(); static int weakNetworkGeometryPointerTypeId = qRegisterMetaType >(); static int vec3VectorTypeId = qRegisterMetaType >(); +static QScriptValue localLightToScriptValue(QScriptEngine* engine, const Model::LocalLight& light) { + QScriptValue object = engine->newObject(); + object.setProperty("direction", vec3toScriptValue(engine, light.direction)); + object.setProperty("color", vec3toScriptValue(engine, light.color)); + return object; +} + +static void localLightFromScriptValue(const QScriptValue& value, Model::LocalLight& light) { + vec3FromScriptValue(value.property("direction"), light.direction); + vec3FromScriptValue(value.property("color"), light.color); +} + +void Model::registerMetaTypes(QScriptEngine* engine) { + qScriptRegisterMetaType(engine, localLightToScriptValue, localLightFromScriptValue); + qScriptRegisterSequenceMetaType >(engine); +} + Model::Model(QObject* parent) : QObject(parent), _scale(1.0f, 1.0f, 1.0f), @@ -609,6 +628,20 @@ bool Model::render(float alpha, RenderMode mode, bool receiveShadows) { glEnable(GL_CULL_FACE); if (mode == SHADOW_RENDER_MODE) { glCullFace(GL_FRONT); + + } else if (mode == DEFAULT_RENDER_MODE) { + // set up the local lights + for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { + glm::vec4 color; + GLenum lightName = GL_LIGHT0 + i + 1; + if (i < _localLights.size()) { + const LocalLight& light = _localLights.at(i); + color = glm::vec4(light.color, 1.0f); + glm::vec4 position = glm::vec4(_rotation * light.direction, 0.0f); + glLightfv(lightName, GL_POSITION, (GLfloat*)&position); + } + glLightfv(lightName, GL_DIFFUSE, (GLfloat*)&color); + } } } @@ -1493,11 +1526,6 @@ void Model::renderMeshes(float alpha, RenderMode mode, bool translucent, bool re if (cascadedShadows) { program->setUniform(skinLocations->shadowDistances, Application::getInstance()->getShadowDistances()); } - - // local light uniforms - skinProgram->setUniformValue("numLocalLights", _numLocalLights); - skinProgram->setUniformArray("localLightDirections", _localLightDirections, MAX_LOCAL_LIGHTS); - skinProgram->setUniformArray("localLightColors", _localLightColors, MAX_LOCAL_LIGHTS); } else { glMultMatrixf((const GLfloat*)&state.clusterMatrices[0]); program->bind(); @@ -1632,20 +1660,6 @@ void Model::renderMeshes(float alpha, RenderMode mode, bool translucent, bool re } } -void Model::setLocalLightDirection(const glm::vec3& direction, int lightIndex) { - assert(lightIndex >= 0 && lightIndex < MAX_LOCAL_LIGHTS); - _localLightDirections[lightIndex] = direction; -} - -void Model::setLocalLightColor(const glm::vec3& color, int lightIndex) { - assert(lightIndex >= 0 && lightIndex < MAX_LOCAL_LIGHTS); - _localLightColors[lightIndex] = color; -} - -void Model::setNumLocalLights(int numLocalLights) { - _numLocalLights = numLocalLights; -} - void AnimationHandle::setURL(const QUrl& url) { if (_url != url) { _animation = Application::getInstance()->getAnimationCache()->getAnimation(_url = url); diff --git a/interface/src/renderer/Model.h b/interface/src/renderer/Model.h index 078685345b..fe09710d0a 100644 --- a/interface/src/renderer/Model.h +++ b/interface/src/renderer/Model.h @@ -26,6 +26,8 @@ #include "ProgramObject.h" #include "TextureCache.h" +class QScriptEngine; + class AnimationHandle; class Shape; @@ -40,6 +42,9 @@ class Model : public QObject, public PhysicsEntity { public: + /// Registers the script types associated with models. + static void registerMetaTypes(QScriptEngine* engine); + Model(QObject* parent = NULL); virtual ~Model(); @@ -145,9 +150,14 @@ public: /// Sets blended vertices computed in a separate thread. void setBlendedVertices(const QVector& vertices, const QVector& normals); - void setLocalLightDirection(const glm::vec3& direction, int lightIndex); - void setLocalLightColor(const glm::vec3& color, int lightIndex); - void setNumLocalLights(int numLocalLights); + class LocalLight { + public: + glm::vec3 color; + glm::vec3 direction; + }; + + void setLocalLights(const QVector& localLights) { _localLights = localLights; } + const QVector& getLocalLights() const { return _localLights; } void setShowTrueJointTransforms(bool show) { _showTrueJointTransforms = show; } @@ -165,10 +175,8 @@ protected: bool _snappedToCenter; /// are we currently snapped to center bool _showTrueJointTransforms; - glm::vec3 _localLightDirections[MAX_LOCAL_LIGHTS]; - glm::vec3 _localLightColors[MAX_LOCAL_LIGHTS]; - int _numLocalLights; - + QVector _localLights; + QVector _jointStates; class MeshState { @@ -326,6 +334,8 @@ private: Q_DECLARE_METATYPE(QPointer) Q_DECLARE_METATYPE(QWeakPointer) Q_DECLARE_METATYPE(QVector) +Q_DECLARE_METATYPE(Model::LocalLight) +Q_DECLARE_METATYPE(QVector) /// Represents a handle to a model animation. class AnimationHandle : public QObject { From 7faf64c54ac5aebcf89cf4f4d6bfd2feb33ed9cb Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 22 Jul 2014 14:24:25 -0700 Subject: [PATCH 3/8] Tab fixes. --- examples/avatarLocalLight.js | 150 +++++++++++++++++------------------ 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/examples/avatarLocalLight.js b/examples/avatarLocalLight.js index 40f1b9a8ca..040289af1f 100644 --- a/examples/avatarLocalLight.js +++ b/examples/avatarLocalLight.js @@ -25,110 +25,110 @@ function keyPressEvent(event) { var choice = parseInt(event.text); if (event.text == "1") { - currentSelection = 0; - print("light election = " + currentSelection); + currentSelection = 0; + print("light election = " + currentSelection); } else if (event.text == "2" ) { - currentSelection = 1; - print("light selection = " + currentSelection); + currentSelection = 1; + print("light selection = " + currentSelection); } else if (event.text == "3" ) { - currentSelection = 2; - print("light selection = " + currentSelection); + currentSelection = 2; + print("light selection = " + currentSelection); } else if (event.text == "4" ) { - currentSelection = 3; - print("light selection = " + currentSelection); + currentSelection = 3; + print("light selection = " + currentSelection); } else if (event.text == "5" ) { - localLightColors[currentSelection].x += changeDelta; - if ( localLightColors[currentSelection].x > 1.0) { - localLightColors[currentSelection].x = 0.0; - } - - lightsDirty = true; - print("CHANGE RED light " + currentSelection + " color (" + localLightColors[currentSelection].x + ", " + localLightColors[currentSelection].y + ", " + localLightColors[currentSelection].z + " )" ); + localLightColors[currentSelection].x += changeDelta; + if ( localLightColors[currentSelection].x > 1.0) { + localLightColors[currentSelection].x = 0.0; + } + + lightsDirty = true; + print("CHANGE RED light " + currentSelection + " color (" + localLightColors[currentSelection].x + ", " + localLightColors[currentSelection].y + ", " + localLightColors[currentSelection].z + " )" ); } else if (event.text == "6" ) { - localLightColors[currentSelection].y += changeDelta; - if ( localLightColors[currentSelection].y > 1.0) { - localLightColors[currentSelection].y = 0.0; - } - - lightsDirty = true; - print("CHANGE GREEN light " + currentSelection + " color (" + localLightColors[currentSelection].x + ", " + localLightColors[currentSelection].y + ", " + localLightColors[currentSelection].z + " )" ); + localLightColors[currentSelection].y += changeDelta; + if ( localLightColors[currentSelection].y > 1.0) { + localLightColors[currentSelection].y = 0.0; + } + + lightsDirty = true; + print("CHANGE GREEN light " + currentSelection + " color (" + localLightColors[currentSelection].x + ", " + localLightColors[currentSelection].y + ", " + localLightColors[currentSelection].z + " )" ); } else if (event.text == "7" ) { - localLightColors[currentSelection].z += changeDelta; - if ( localLightColors[currentSelection].z > 1.0) { - localLightColors[currentSelection].z = 0.0; - } - - lightsDirty = true; - print("CHANGE BLUE light " + currentSelection + " color (" + localLightColors[currentSelection].x + ", " + localLightColors[currentSelection].y + ", " + localLightColors[currentSelection].z + " )" ); + localLightColors[currentSelection].z += changeDelta; + if ( localLightColors[currentSelection].z > 1.0) { + localLightColors[currentSelection].z = 0.0; + } + + lightsDirty = true; + print("CHANGE BLUE light " + currentSelection + " color (" + localLightColors[currentSelection].x + ", " + localLightColors[currentSelection].y + ", " + localLightColors[currentSelection].z + " )" ); } else if (event.text == "8" ) { - localLightDirections[currentSelection].x += changeDelta; - if (localLightDirections[currentSelection].x > 1.0) { - localLightDirections[currentSelection].x = -1.0; - } - - lightsDirty = true; - print("PLUS X light " + currentSelection + " direction (" + localLightDirections[currentSelection].x + ", " + localLightDirections[currentSelection].y + ", " + localLightDirections[currentSelection].z + " )" ); + localLightDirections[currentSelection].x += changeDelta; + if (localLightDirections[currentSelection].x > 1.0) { + localLightDirections[currentSelection].x = -1.0; + } + + lightsDirty = true; + print("PLUS X light " + currentSelection + " direction (" + localLightDirections[currentSelection].x + ", " + localLightDirections[currentSelection].y + ", " + localLightDirections[currentSelection].z + " )" ); } else if (event.text == "9" ) { - localLightDirections[currentSelection].x -= changeDelta; - if (localLightDirections[currentSelection].x < -1.0) { - localLightDirections[currentSelection].x = 1.0; - } - - lightsDirty = true; - print("MINUS X light " + currentSelection + " direction (" + localLightDirections[currentSelection].x + ", " + localLightDirections[currentSelection].y + ", " + localLightDirections[currentSelection].z + " )" ); + localLightDirections[currentSelection].x -= changeDelta; + if (localLightDirections[currentSelection].x < -1.0) { + localLightDirections[currentSelection].x = 1.0; + } + + lightsDirty = true; + print("MINUS X light " + currentSelection + " direction (" + localLightDirections[currentSelection].x + ", " + localLightDirections[currentSelection].y + ", " + localLightDirections[currentSelection].z + " )" ); } else if (event.text == "0" ) { - localLightDirections[currentSelection].y += changeDelta; - if (localLightDirections[currentSelection].y > 1.0) { - localLightDirections[currentSelection].y = -1.0; - } - - lightsDirty = true; - print("PLUS Y light " + currentSelection + " direction (" + localLightDirections[currentSelection].x + ", " + localLightDirections[currentSelection].y + ", " + localLightDirections[currentSelection].z + " )" ); + localLightDirections[currentSelection].y += changeDelta; + if (localLightDirections[currentSelection].y > 1.0) { + localLightDirections[currentSelection].y = -1.0; + } + + lightsDirty = true; + print("PLUS Y light " + currentSelection + " direction (" + localLightDirections[currentSelection].x + ", " + localLightDirections[currentSelection].y + ", " + localLightDirections[currentSelection].z + " )" ); } else if (event.text == "-" ) { - localLightDirections[currentSelection].y -= changeDelta; - if (localLightDirections[currentSelection].y < -1.0) { - localLightDirections[currentSelection].y = 1.0; - } - - lightsDirty = true; - print("MINUS Y light " + currentSelection + " direction (" + localLightDirections[currentSelection].x + ", " + localLightDirections[currentSelection].y + ", " + localLightDirections[currentSelection].z + " )" ); + localLightDirections[currentSelection].y -= changeDelta; + if (localLightDirections[currentSelection].y < -1.0) { + localLightDirections[currentSelection].y = 1.0; + } + + lightsDirty = true; + print("MINUS Y light " + currentSelection + " direction (" + localLightDirections[currentSelection].x + ", " + localLightDirections[currentSelection].y + ", " + localLightDirections[currentSelection].z + " )" ); } else if (event.text == "," ) { - if (currentNumLights + 1 <= maxNumLights) { - ++currentNumLights; - lightsDirty = true; - } - - print("ADD LIGHT, number of lights " + currentNumLights); + if (currentNumLights + 1 <= maxNumLights) { + ++currentNumLights; + lightsDirty = true; + } + + print("ADD LIGHT, number of lights " + currentNumLights); } else if (event.text == "." ) { - if (currentNumLights - 1 >= 0 ) { - --currentNumLights; - lightsDirty = true; - } - - print("REMOVE LIGHT, number of lights " + currentNumLights); + if (currentNumLights - 1 >= 0 ) { + --currentNumLights; + lightsDirty = true; + } + + print("REMOVE LIGHT, number of lights " + currentNumLights); } } function updateLocalLights() { - if (lightsDirty) { - var localLights = []; - for (var i = 0; i < currentNumLights; i++) { - localLights.push({ direction: localLightDirections[i], color: localLightColors[i] }); - } - AvatarManager.setLocalLights(localLights); + if (lightsDirty) { + var localLights = []; + for (var i = 0; i < currentNumLights; i++) { + localLights.push({ direction: localLightDirections[i], color: localLightColors[i] }); + } + AvatarManager.setLocalLights(localLights); lightsDirty = false; } } From d4904f976ae23eb698ac834d2766f033b0b7af04 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 22 Jul 2014 15:50:08 -0700 Subject: [PATCH 4/8] Missed the local light stats. --- interface/src/avatar/Avatar.cpp | 34 --------------------------------- interface/src/avatar/Avatar.h | 14 +------------- interface/src/ui/Stats.cpp | 8 ++++---- 3 files changed, 5 insertions(+), 51 deletions(-) diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index cddd85964c..ef0fae1c13 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -60,7 +60,6 @@ Avatar::Avatar() : _mouseRayDirection(0.0f, 0.0f, 0.0f), _moving(false), _collisionGroups(0), - _numLocalLights(0), _initialized(false), _shouldRenderBillboard(true) { @@ -916,36 +915,3 @@ void Avatar::setShowDisplayName(bool showDisplayName) { } -void Avatar::setLocalLightDirection(const glm::vec3& direction, int lightIndex) { - _localLightDirections[lightIndex] = direction; -} - -void Avatar::setLocalLightColor(const glm::vec3& color, int lightIndex) { - _localLightColors[lightIndex] = color; -} - -void Avatar::addLocalLight() { - if (_numLocalLights + 1 <= MAX_LOCAL_LIGHTS) { - ++_numLocalLights; - } -} - -void Avatar::removeLocalLight() { - if (_numLocalLights - 1 >= 0) { - --_numLocalLights; - } -} - -int Avatar::getNumLocalLights() { - return _numLocalLights; -} - -glm::vec3 Avatar::getLocalLightDirection(int lightIndex) { - return _localLightDirections[lightIndex]; -} - -glm::vec3 Avatar::getLocalLightColor(int lightIndex) { - return _localLightColors[lightIndex]; -} - - diff --git a/interface/src/avatar/Avatar.h b/interface/src/avatar/Avatar.h index d2f3ee6ab4..02bc2f227a 100755 --- a/interface/src/avatar/Avatar.h +++ b/interface/src/avatar/Avatar.h @@ -159,14 +159,7 @@ public: public slots: void updateCollisionGroups(); - void setLocalLightDirection(const glm::vec3& direction, int lightIndex); - void setLocalLightColor(const glm::vec3& color, int lightIndex); - void addLocalLight(); - void removeLocalLight(); - int getNumLocalLights(); - glm::vec3 getLocalLightDirection(int lightIndex); - glm::vec3 getLocalLightColor(int lightIndex); - + signals: void collisionWithAvatar(const QUuid& myUUID, const QUuid& theirUUID, const CollisionInfo& collision); @@ -189,11 +182,6 @@ protected: bool _moving; ///< set when position is changing quint32 _collisionGroups; - - // always-present local lighting for the avatar - glm::vec3 _localLightDirections[MAX_LOCAL_LIGHTS]; - glm::vec3 _localLightColors[MAX_LOCAL_LIGHTS]; - int _numLocalLights; // protected methods... glm::vec3 getBodyRightDirection() const { return getOrientation() * IDENTITY_RIGHT; } diff --git a/interface/src/ui/Stats.cpp b/interface/src/ui/Stats.cpp index 412de6e7b0..f82bc7ba17 100644 --- a/interface/src/ui/Stats.cpp +++ b/interface/src/ui/Stats.cpp @@ -828,19 +828,19 @@ void Stats::display( } // draw local light stats - int numLocalLights = myAvatar->getNumLocalLights(); + QVector localLights = Application::getInstance()->getAvatarManager().getLocalLights(); verticalOffset = 400; horizontalOffset = 20; char buffer[128]; - for (int i = 0; i < numLocalLights; i++) { - glm::vec3 lightDirection = myAvatar->getLocalLightDirection(i); + for (int i = 0; i < localLights.size(); i++) { + glm::vec3 lightDirection = localLights.at(i).direction; snprintf(buffer, sizeof(buffer), "Light %d direction (%.2f, %.2f, %.2f)", i, lightDirection.x, lightDirection.y, lightDirection.z); drawText(horizontalOffset, verticalOffset, scale, rotation, font, buffer, color); verticalOffset += STATS_PELS_PER_LINE; - glm::vec3 lightColor = myAvatar->getLocalLightColor(i); + glm::vec3 lightColor = localLights.at(i).color; snprintf(buffer, sizeof(buffer), "Light %d color (%.2f, %.2f, %.2f)", i, lightColor.x, lightColor.y, lightColor.z); drawText(horizontalOffset, verticalOffset, scale, rotation, font, buffer, color); From 56efa46812e95566a702b4a7590da87f45a3c34a Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 22 Jul 2014 15:56:22 -0700 Subject: [PATCH 5/8] repair for Visage find module base includes --- cmake/modules/FindVisage.cmake | 8 ++++---- interface/src/devices/Faceshift.h | 2 -- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/cmake/modules/FindVisage.cmake b/cmake/modules/FindVisage.cmake index 4151d3bec0..7e1d6e60b6 100644 --- a/cmake/modules/FindVisage.cmake +++ b/cmake/modules/FindVisage.cmake @@ -20,7 +20,7 @@ set(VISAGE_SEARCH_DIRS "${VISAGE_ROOT_DIR}" "$ENV{VISAGE_ROOT_DIR}" "$ENV{HIFI_LIB_DIR}/visage") -find_path(VISAGE_INCLUDE_DIR VisageTracker2.h PATH_SUFFIXES include HINTS ${VISAGE_SEARCH_DIRS}) +find_path(VISAGE_BASE_INCLUDE_DIR VisageTracker2.h PATH_SUFFIXES include HINTS ${VISAGE_SEARCH_DIRS}) if (APPLE) find_path(VISAGE_XML_INCLUDE_DIR libxml/xmlreader.h HINTS /usr/include/libxml2 ${VISAGE_SEARCH_DIRS}) @@ -43,15 +43,15 @@ endif () include(FindPackageHandleStandardArgs) find_package_handle_standard_args(VISAGE DEFAULT_MSG - VISAGE_INCLUDE_DIR VISAGE_XML_INCLUDE_DIR VISAGE_OPENCV_INCLUDE_DIR VISAGE_OPENCV2_INCLUDE_DIR + VISAGE_BASE_INCLUDE_DIR VISAGE_XML_INCLUDE_DIR VISAGE_OPENCV_INCLUDE_DIR VISAGE_OPENCV2_INCLUDE_DIR VISAGE_CORE_LIBRARY VISAGE_VISION_LIBRARY VISAGE_OPENCV_LIBRARY ) -set(VISAGE_INCLUDE_DIRS "${VISAGE_XML_INCLUDE_DIR}" "${VISAGE_OPENCV_INCLUDE_DIR}" "${VISAGE_OPENCV2_INCLUDE_DIR}") +set(VISAGE_INCLUDE_DIRS "${VISAGE_XML_INCLUDE_DIR}" "${VISAGE_OPENCV_INCLUDE_DIR}" "${VISAGE_OPENCV2_INCLUDE_DIR}" "${VISAGE_BASE_INCLUDE_DIR}") set(VISAGE_LIBRARIES "${VISAGE_CORE_LIBRARY}" "${VISAGE_VISION_LIBRARY}" "${VISAGE_OPENCV_LIBRARY}") mark_as_advanced( VISAGE_INCLUDE_DIRS VISAGE_LIBRARIES - VISAGE_INCLUDE_DIR VISAGE_XML_INCLUDE_DIR VISAGE_OPENCV_INCLUDE_DIR VISAGE_OPENCV2_INCLUDE_DIR + VISAGE_BASE_INCLUDE_DIR VISAGE_XML_INCLUDE_DIR VISAGE_OPENCV_INCLUDE_DIR VISAGE_OPENCV2_INCLUDE_DIR VISAGE_CORE_LIBRARY VISAGE_VISION_LIBRARY VISAGE_OPENCV_LIBRARY ) diff --git a/interface/src/devices/Faceshift.h b/interface/src/devices/Faceshift.h index c14bbed600..25abd8c0eb 100644 --- a/interface/src/devices/Faceshift.h +++ b/interface/src/devices/Faceshift.h @@ -16,9 +16,7 @@ #include #ifdef HAVE_FACESHIFT - #include - #endif #include "FaceTracker.h" From 4f52098da1c4707d420881e2c18046465ca9e8a3 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Tue, 22 Jul 2014 20:23:23 -0400 Subject: [PATCH 6/8] tuning tweaks to world and avatar lights --- examples/avatarLocalLight.js | 2 +- interface/src/Application.cpp | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/examples/avatarLocalLight.js b/examples/avatarLocalLight.js index 040289af1f..b1f31888d8 100644 --- a/examples/avatarLocalLight.js +++ b/examples/avatarLocalLight.js @@ -11,7 +11,7 @@ // var localLightDirections = [ {x: 1.0, y:0.0, z: 0.0}, {x: 0.0, y:0.0, z: 1.0} ]; -var localLightColors = [ {x: 1.0, y:1.0, z: 1.0}, {x: 1.0, y:1.0, z: 1.0} ]; +var localLightColors = [ {x: 0.4, y:0.335, z: 0.266}, {x: 0.4, y:0.335, z: 0.266} ]; var currentSelection = 0; var currentNumLights = 2; diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 1d0bf8d6a3..8b00bface7 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2600,7 +2600,9 @@ void Application::updateShadowMap() { glViewport(0, 0, _glWidget->width(), _glWidget->height()); } -const GLfloat WHITE_SPECULAR_COLOR[] = { 1.0f, 1.0f, 1.0f, 1.0f }; +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 GLfloat NO_SPECULAR_COLOR[] = { 0.0f, 0.0f, 0.0f, 1.0f }; void Application::setupWorldLight() { @@ -2612,13 +2614,10 @@ void Application::setupWorldLight() { glm::vec3 sunDirection = getSunDirection(); GLfloat light_position0[] = { sunDirection.x, sunDirection.y, sunDirection.z, 0.0 }; glLightfv(GL_LIGHT0, GL_POSITION, light_position0); - GLfloat ambient_color[] = { 0.7f, 0.7f, 0.8f }; - glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color); - GLfloat diffuse_color[] = { 0.8f, 0.7f, 0.7f }; - glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_color); - - glLightfv(GL_LIGHT0, GL_SPECULAR, WHITE_SPECULAR_COLOR); - glMaterialfv(GL_FRONT, GL_SPECULAR, WHITE_SPECULAR_COLOR); + glLightfv(GL_LIGHT0, GL_AMBIENT, WORLD_AMBIENT_COLOR); + glLightfv(GL_LIGHT0, GL_DIFFUSE, WORLD_DIFFUSE_COLOR); + glLightfv(GL_LIGHT0, GL_SPECULAR, WORLD_SPECULAR_COLOR); + glMaterialfv(GL_FRONT, GL_SPECULAR, WORLD_SPECULAR_COLOR); glMateriali(GL_FRONT, GL_SHININESS, 96); } @@ -2796,7 +2795,7 @@ void Application::displaySide(Camera& whichCamera, bool selfAvatarOnly) { } // restore default, white specular - glMaterialfv(GL_FRONT, GL_SPECULAR, WHITE_SPECULAR_COLOR); + glMaterialfv(GL_FRONT, GL_SPECULAR, WORLD_SPECULAR_COLOR); _nodeBoundsDisplay.draw(); From 4f875248216d369ab83128b0f79d408b1796dcc5 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 22 Jul 2014 18:03:13 -0700 Subject: [PATCH 7/8] Switched back to using uniforms for light parameters, as using the fixed function state killed my frame rate. Also cleaned up some of the location tracking. --- interface/resources/shaders/model.frag | 10 +- .../shaders/model_cascaded_shadow_map.frag | 10 +- .../model_cascaded_shadow_normal_map.frag | 10 +- ...l_cascaded_shadow_normal_specular_map.frag | 10 +- .../model_cascaded_shadow_specular_map.frag | 10 +- .../resources/shaders/model_normal_map.frag | 10 +- .../shaders/model_normal_specular_map.frag | 10 +- .../resources/shaders/model_shadow_map.frag | 10 +- .../shaders/model_shadow_normal_map.frag | 10 +- .../model_shadow_normal_specular_map.frag | 10 +- .../shaders/model_shadow_specular_map.frag | 10 +- .../resources/shaders/model_specular_map.frag | 10 +- interface/src/Application.h | 2 + interface/src/renderer/Model.cpp | 185 +++++++----------- interface/src/renderer/Model.h | 33 +++- 15 files changed, 199 insertions(+), 141 deletions(-) diff --git a/interface/resources/shaders/model.frag b/interface/resources/shaders/model.frag index 27f6510477..d8e1efab14 100644 --- a/interface/resources/shaders/model.frag +++ b/interface/resources/shaders/model.frag @@ -14,6 +14,12 @@ // the maximum number of local lights to apply const int MAX_LOCAL_LIGHTS = 2; +// the color of each local light +uniform vec4 localLightColors[MAX_LOCAL_LIGHTS]; + +// the direction of each local light +uniform vec4 localLightDirections[MAX_LOCAL_LIGHTS]; + // the diffuse texture uniform sampler2D diffuseMap; @@ -27,8 +33,8 @@ void main(void) { // add up the local lights vec4 normalizedNormal = normalize(normal); vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); - for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { - localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[i].position)); + for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { + localLight += localLightColors[i] * max(0.0, dot(normalizedNormal, localLightDirections[i])); } // compute the base color based on OpenGL lighting model diff --git a/interface/resources/shaders/model_cascaded_shadow_map.frag b/interface/resources/shaders/model_cascaded_shadow_map.frag index 35026cb3b2..63500e7e2a 100644 --- a/interface/resources/shaders/model_cascaded_shadow_map.frag +++ b/interface/resources/shaders/model_cascaded_shadow_map.frag @@ -14,6 +14,12 @@ // the maximum number of local lights to apply const int MAX_LOCAL_LIGHTS = 2; +// the color of each local light +uniform vec4 localLightColors[MAX_LOCAL_LIGHTS]; + +// the direction of each local light +uniform vec4 localLightDirections[MAX_LOCAL_LIGHTS]; + // the diffuse texture uniform sampler2D diffuseMap; @@ -41,8 +47,8 @@ void main(void) { // add up the local lights vec4 normalizedNormal = normalize(normal); vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); - for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { - localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[i].position)); + for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { + localLight += localLightColors[i] * max(0.0, dot(normalizedNormal, localLightDirections[i])); } // compute the base color based on OpenGL lighting model diff --git a/interface/resources/shaders/model_cascaded_shadow_normal_map.frag b/interface/resources/shaders/model_cascaded_shadow_normal_map.frag index 0e474c173a..1fef1ef82a 100644 --- a/interface/resources/shaders/model_cascaded_shadow_normal_map.frag +++ b/interface/resources/shaders/model_cascaded_shadow_normal_map.frag @@ -14,6 +14,12 @@ // the maximum number of local lights to apply const int MAX_LOCAL_LIGHTS = 2; +// the color of each local light +uniform vec4 localLightColors[MAX_LOCAL_LIGHTS]; + +// the direction of each local light +uniform vec4 localLightDirections[MAX_LOCAL_LIGHTS]; + // the diffuse texture uniform sampler2D diffuseMap; @@ -54,8 +60,8 @@ void main(void) { vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); - for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { - localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(viewNormal, gl_LightSource[i].position)); + for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { + localLight += localLightColors[i] * max(0.0, dot(viewNormal, localLightDirections[i])); } // compute the base color based on OpenGL lighting model diff --git a/interface/resources/shaders/model_cascaded_shadow_normal_specular_map.frag b/interface/resources/shaders/model_cascaded_shadow_normal_specular_map.frag index 33c025925e..f0751bcca9 100644 --- a/interface/resources/shaders/model_cascaded_shadow_normal_specular_map.frag +++ b/interface/resources/shaders/model_cascaded_shadow_normal_specular_map.frag @@ -14,6 +14,12 @@ // the maximum number of local lights to apply const int MAX_LOCAL_LIGHTS = 2; +// the color of each local light +uniform vec4 localLightColors[MAX_LOCAL_LIGHTS]; + +// the direction of each local light +uniform vec4 localLightDirections[MAX_LOCAL_LIGHTS]; + // the diffuse texture uniform sampler2D diffuseMap; @@ -57,8 +63,8 @@ void main(void) { vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); - for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { - localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(viewNormal, gl_LightSource[i].position)); + for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { + localLight += localLightColors[i] * max(0.0, dot(viewNormal, localLightDirections[i])); } // compute the base color based on OpenGL lighting model diff --git a/interface/resources/shaders/model_cascaded_shadow_specular_map.frag b/interface/resources/shaders/model_cascaded_shadow_specular_map.frag index 9190654431..b2881e6c13 100644 --- a/interface/resources/shaders/model_cascaded_shadow_specular_map.frag +++ b/interface/resources/shaders/model_cascaded_shadow_specular_map.frag @@ -14,6 +14,12 @@ // the maximum number of local lights to apply const int MAX_LOCAL_LIGHTS = 2; +// the color of each local light +uniform vec4 localLightColors[MAX_LOCAL_LIGHTS]; + +// the direction of each local light +uniform vec4 localLightDirections[MAX_LOCAL_LIGHTS]; + // the diffuse texture uniform sampler2D diffuseMap; @@ -44,8 +50,8 @@ void main(void) { // add up the local lights vec4 normalizedNormal = normalize(normal); vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); - for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { - localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[i].position)); + for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { + localLight += localLightColors[i] * max(0.0, dot(normalizedNormal, localLightDirections[i])); } // compute the base color based on OpenGL lighting model diff --git a/interface/resources/shaders/model_normal_map.frag b/interface/resources/shaders/model_normal_map.frag index acd049dc47..73d2fc0ef6 100644 --- a/interface/resources/shaders/model_normal_map.frag +++ b/interface/resources/shaders/model_normal_map.frag @@ -14,6 +14,12 @@ // the maximum number of local lights to apply const int MAX_LOCAL_LIGHTS = 2; +// the color of each local light +uniform vec4 localLightColors[MAX_LOCAL_LIGHTS]; + +// the direction of each local light +uniform vec4 localLightDirections[MAX_LOCAL_LIGHTS]; + // the diffuse texture uniform sampler2D diffuseMap; @@ -39,8 +45,8 @@ void main(void) { vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); - for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { - localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(viewNormal, gl_LightSource[i].position)); + for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { + localLight += localLightColors[i] * max(0.0, dot(viewNormal, localLightDirections[i])); } // compute the base color based on OpenGL lighting model diff --git a/interface/resources/shaders/model_normal_specular_map.frag b/interface/resources/shaders/model_normal_specular_map.frag index fa997bff7f..5eb804292c 100644 --- a/interface/resources/shaders/model_normal_specular_map.frag +++ b/interface/resources/shaders/model_normal_specular_map.frag @@ -14,6 +14,12 @@ // the maximum number of local lights to apply const int MAX_LOCAL_LIGHTS = 2; +// the color of each local light +uniform vec4 localLightColors[MAX_LOCAL_LIGHTS]; + +// the direction of each local light +uniform vec4 localLightDirections[MAX_LOCAL_LIGHTS]; + // the diffuse texture uniform sampler2D diffuseMap; @@ -42,8 +48,8 @@ void main(void) { vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); - for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { - localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(viewNormal, gl_LightSource[i].position)); + for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { + localLight += localLightColors[i] * max(0.0, dot(viewNormal, localLightDirections[i])); } // compute the base color based on OpenGL lighting model diff --git a/interface/resources/shaders/model_shadow_map.frag b/interface/resources/shaders/model_shadow_map.frag index c2bf95fd27..807a9d162f 100644 --- a/interface/resources/shaders/model_shadow_map.frag +++ b/interface/resources/shaders/model_shadow_map.frag @@ -14,6 +14,12 @@ // the maximum number of local lights to apply const int MAX_LOCAL_LIGHTS = 2; +// the color of each local light +uniform vec4 localLightColors[MAX_LOCAL_LIGHTS]; + +// the direction of each local light +uniform vec4 localLightDirections[MAX_LOCAL_LIGHTS]; + // the diffuse texture uniform sampler2D diffuseMap; @@ -33,8 +39,8 @@ void main(void) { // add up the local lights vec4 normalizedNormal = normalize(normal); vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); - for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { - localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[i].position)); + for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { + localLight += localLightColors[i] * max(0.0, dot(normalizedNormal, localLightDirections[i])); } // compute the base color based on OpenGL lighting model diff --git a/interface/resources/shaders/model_shadow_normal_map.frag b/interface/resources/shaders/model_shadow_normal_map.frag index 058c3783be..454b88dd23 100644 --- a/interface/resources/shaders/model_shadow_normal_map.frag +++ b/interface/resources/shaders/model_shadow_normal_map.frag @@ -14,6 +14,12 @@ // the maximum number of local lights to apply const int MAX_LOCAL_LIGHTS = 2; +// the color of each local light +uniform vec4 localLightColors[MAX_LOCAL_LIGHTS]; + +// the direction of each local light +uniform vec4 localLightDirections[MAX_LOCAL_LIGHTS]; + // the diffuse texture uniform sampler2D diffuseMap; @@ -45,8 +51,8 @@ void main(void) { vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); - for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { - localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(viewNormal, gl_LightSource[i].position)); + for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { + localLight += localLightColors[i] * max(0.0, dot(viewNormal, localLightDirections[i])); } // compute the base color based on OpenGL lighting model diff --git a/interface/resources/shaders/model_shadow_normal_specular_map.frag b/interface/resources/shaders/model_shadow_normal_specular_map.frag index 5b8d7c87a4..8283d10b93 100644 --- a/interface/resources/shaders/model_shadow_normal_specular_map.frag +++ b/interface/resources/shaders/model_shadow_normal_specular_map.frag @@ -14,6 +14,12 @@ // the maximum number of local lights to apply const int MAX_LOCAL_LIGHTS = 2; +// the color of each local light +uniform vec4 localLightColors[MAX_LOCAL_LIGHTS]; + +// the direction of each local light +uniform vec4 localLightDirections[MAX_LOCAL_LIGHTS]; + // the diffuse texture uniform sampler2D diffuseMap; @@ -48,8 +54,8 @@ void main(void) { vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); - for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { - localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(viewNormal, gl_LightSource[i].position)); + for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { + localLight += localLightColors[i] * max(0.0, dot(viewNormal, localLightDirections[i])); } // compute the base color based on OpenGL lighting model diff --git a/interface/resources/shaders/model_shadow_specular_map.frag b/interface/resources/shaders/model_shadow_specular_map.frag index 9f413f63a4..f30f3d7b06 100644 --- a/interface/resources/shaders/model_shadow_specular_map.frag +++ b/interface/resources/shaders/model_shadow_specular_map.frag @@ -14,6 +14,12 @@ // the maximum number of local lights to apply const int MAX_LOCAL_LIGHTS = 2; +// the color of each local light +uniform vec4 localLightColors[MAX_LOCAL_LIGHTS]; + +// the direction of each local light +uniform vec4 localLightDirections[MAX_LOCAL_LIGHTS]; + // the diffuse texture uniform sampler2D diffuseMap; @@ -36,8 +42,8 @@ void main(void) { // add up the local lights vec4 normalizedNormal = normalize(normal); vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); - for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { - localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[i].position)); + for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { + localLight += localLightColors[i] * max(0.0, dot(normalizedNormal, localLightDirections[i])); } // compute the base color based on OpenGL lighting model diff --git a/interface/resources/shaders/model_specular_map.frag b/interface/resources/shaders/model_specular_map.frag index 54790a36fb..824746d575 100644 --- a/interface/resources/shaders/model_specular_map.frag +++ b/interface/resources/shaders/model_specular_map.frag @@ -14,6 +14,12 @@ // the maximum number of local lights to apply const int MAX_LOCAL_LIGHTS = 2; +// the color of each local light +uniform vec4 localLightColors[MAX_LOCAL_LIGHTS]; + +// the direction of each local light +uniform vec4 localLightDirections[MAX_LOCAL_LIGHTS]; + // the diffuse texture uniform sampler2D diffuseMap; @@ -30,8 +36,8 @@ void main(void) { // add up the local lights vec4 normalizedNormal = normalize(normal); vec4 localLight = vec4(0.0, 0.0, 0.0, 0.0); - for (int i = 1; i <= MAX_LOCAL_LIGHTS; i++) { - localLight += gl_FrontLightProduct[i].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[i].position)); + for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { + localLight += localLightColors[i] * max(0.0, dot(normalizedNormal, localLightDirections[i])); } // compute the base color based on OpenGL lighting model diff --git a/interface/src/Application.h b/interface/src/Application.h index d956a949ac..97457a00c8 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -258,6 +258,8 @@ public: /// the view matrix translation. void updateUntranslatedViewMatrix(const glm::vec3& viewMatrixTranslation = glm::vec3()); + const glm::mat4& getUntranslatedViewMatrix() const { return _untranslatedViewMatrix; } + /// Loads a view matrix that incorporates the specified model translation without the precision issues that can /// result from matrix multiplication at high translation magnitudes. void loadTranslatedViewMatrix(const glm::vec3& translation); diff --git a/interface/src/renderer/Model.cpp b/interface/src/renderer/Model.cpp index c5c83aa01d..14bf3fb684 100644 --- a/interface/src/renderer/Model.cpp +++ b/interface/src/renderer/Model.cpp @@ -104,17 +104,18 @@ ProgramObject Model::_skinCascadedShadowNormalSpecularMapProgram; ProgramObject Model::_skinShadowProgram; -int Model::_normalMapTangentLocation; -int Model::_normalSpecularMapTangentLocation; -int Model::_shadowNormalMapTangentLocation; -int Model::_shadowNormalSpecularMapTangentLocation; -int Model::_cascadedShadowNormalMapTangentLocation; -int Model::_cascadedShadowNormalSpecularMapTangentLocation; - -int Model::_cascadedShadowMapDistancesLocation; -int Model::_cascadedShadowNormalMapDistancesLocation; -int Model::_cascadedShadowSpecularMapDistancesLocation; -int Model::_cascadedShadowNormalSpecularMapDistancesLocation; +Model::Locations Model::_locations; +Model::Locations Model::_normalMapLocations; +Model::Locations Model::_specularMapLocations; +Model::Locations Model::_normalSpecularMapLocations; +Model::Locations Model::_shadowMapLocations; +Model::Locations Model::_shadowNormalMapLocations; +Model::Locations Model::_shadowSpecularMapLocations; +Model::Locations Model::_shadowNormalSpecularMapLocations; +Model::Locations Model::_cascadedShadowMapLocations; +Model::Locations Model::_cascadedShadowNormalMapLocations; +Model::Locations Model::_cascadedShadowSpecularMapLocations; +Model::Locations Model::_cascadedShadowNormalSpecularMapLocations; Model::SkinLocations Model::_skinLocations; Model::SkinLocations Model::_skinNormalMapLocations; @@ -160,15 +161,13 @@ void Model::setOffset(const glm::vec3& offset) { _snappedToCenter = false; } - -void Model::initSkinProgram(ProgramObject& program, Model::SkinLocations& locations, +void Model::initProgram(ProgramObject& program, Model::Locations& locations, int specularTextureUnit, int shadowTextureUnit) { program.bind(); - locations.clusterMatrices = program.uniformLocation("clusterMatrices"); - locations.clusterIndices = program.attributeLocation("clusterIndices"); - locations.clusterWeights = program.attributeLocation("clusterWeights"); locations.tangent = program.attributeLocation("tangent"); locations.shadowDistances = program.uniformLocation("shadowDistances"); + locations.localLightColors = program.uniformLocation("localLightColors"); + locations.localLightDirections = program.uniformLocation("localLightDirections"); program.setUniformValue("diffuseMap", 0); program.setUniformValue("normalMap", 1); program.setUniformValue("specularMap", specularTextureUnit); @@ -176,6 +175,17 @@ void Model::initSkinProgram(ProgramObject& program, Model::SkinLocations& locati program.release(); } +void Model::initSkinProgram(ProgramObject& program, Model::SkinLocations& locations, + int specularTextureUnit, int shadowTextureUnit) { + initProgram(program, locations, specularTextureUnit, shadowTextureUnit); + + program.bind(); + locations.clusterMatrices = program.uniformLocation("clusterMatrices"); + locations.clusterIndices = program.attributeLocation("clusterIndices"); + locations.clusterWeights = program.attributeLocation("clusterWeights"); + program.release(); +} + QVector Model::createJointStates(const FBXGeometry& geometry) { QVector jointStates; foreach (const FBXJoint& joint, geometry.joints) { @@ -212,9 +222,7 @@ void Model::init() { _program.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + "shaders/model.frag"); _program.link(); - _program.bind(); - _program.setUniformValue("diffuseMap", 0); - _program.release(); + initProgram(_program, _locations); _normalMapProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/model_normal_map.vert"); @@ -222,11 +230,7 @@ void Model::init() { Application::resourcesPath() + "shaders/model_normal_map.frag"); _normalMapProgram.link(); - _normalMapProgram.bind(); - _normalMapProgram.setUniformValue("diffuseMap", 0); - _normalMapProgram.setUniformValue("normalMap", 1); - _normalMapTangentLocation = _normalMapProgram.attributeLocation("tangent"); - _normalMapProgram.release(); + initProgram(_normalMapProgram, _normalMapLocations); _specularMapProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/model.vert"); @@ -234,10 +238,7 @@ void Model::init() { Application::resourcesPath() + "shaders/model_specular_map.frag"); _specularMapProgram.link(); - _specularMapProgram.bind(); - _specularMapProgram.setUniformValue("diffuseMap", 0); - _specularMapProgram.setUniformValue("specularMap", 1); - _specularMapProgram.release(); + initProgram(_specularMapProgram, _specularMapLocations); _normalSpecularMapProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/model_normal_map.vert"); @@ -245,12 +246,7 @@ void Model::init() { Application::resourcesPath() + "shaders/model_normal_specular_map.frag"); _normalSpecularMapProgram.link(); - _normalSpecularMapProgram.bind(); - _normalSpecularMapProgram.setUniformValue("diffuseMap", 0); - _normalSpecularMapProgram.setUniformValue("normalMap", 1); - _normalSpecularMapProgram.setUniformValue("specularMap", 2); - _normalSpecularMapTangentLocation = _normalSpecularMapProgram.attributeLocation("tangent"); - _normalSpecularMapProgram.release(); + initProgram(_normalSpecularMapProgram, _normalSpecularMapLocations, 2); _shadowMapProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/model.vert"); @@ -258,10 +254,7 @@ void Model::init() { "shaders/model_shadow_map.frag"); _shadowMapProgram.link(); - _shadowMapProgram.bind(); - _shadowMapProgram.setUniformValue("diffuseMap", 0); - _shadowMapProgram.setUniformValue("shadowMap", 1); - _shadowMapProgram.release(); + initProgram(_shadowMapProgram, _shadowMapLocations); _shadowNormalMapProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/model_normal_map.vert"); @@ -269,12 +262,7 @@ void Model::init() { Application::resourcesPath() + "shaders/model_shadow_normal_map.frag"); _shadowNormalMapProgram.link(); - _shadowNormalMapProgram.bind(); - _shadowNormalMapProgram.setUniformValue("diffuseMap", 0); - _shadowNormalMapProgram.setUniformValue("normalMap", 1); - _shadowNormalMapProgram.setUniformValue("shadowMap", 2); - _shadowNormalMapTangentLocation = _shadowNormalMapProgram.attributeLocation("tangent"); - _shadowNormalMapProgram.release(); + initProgram(_shadowNormalMapProgram, _shadowNormalMapLocations, 1, 2); _shadowSpecularMapProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/model.vert"); @@ -282,11 +270,7 @@ void Model::init() { Application::resourcesPath() + "shaders/model_shadow_specular_map.frag"); _shadowSpecularMapProgram.link(); - _shadowSpecularMapProgram.bind(); - _shadowSpecularMapProgram.setUniformValue("diffuseMap", 0); - _shadowSpecularMapProgram.setUniformValue("specularMap", 1); - _shadowSpecularMapProgram.setUniformValue("shadowMap", 2); - _shadowSpecularMapProgram.release(); + initProgram(_shadowSpecularMapProgram, _shadowSpecularMapLocations, 1, 2); _shadowNormalSpecularMapProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/model_normal_map.vert"); @@ -294,13 +278,7 @@ void Model::init() { Application::resourcesPath() + "shaders/model_shadow_normal_specular_map.frag"); _shadowNormalSpecularMapProgram.link(); - _shadowNormalSpecularMapProgram.bind(); - _shadowNormalSpecularMapProgram.setUniformValue("diffuseMap", 0); - _shadowNormalSpecularMapProgram.setUniformValue("normalMap", 1); - _shadowNormalSpecularMapProgram.setUniformValue("specularMap", 2); - _shadowNormalSpecularMapProgram.setUniformValue("shadowMap", 3); - _shadowNormalSpecularMapTangentLocation = _shadowNormalSpecularMapProgram.attributeLocation("tangent"); - _shadowNormalSpecularMapProgram.release(); + initProgram(_shadowNormalSpecularMapProgram, _shadowNormalSpecularMapLocations, 2, 3); _cascadedShadowMapProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + @@ -309,11 +287,7 @@ void Model::init() { "shaders/model_cascaded_shadow_map.frag"); _cascadedShadowMapProgram.link(); - _cascadedShadowMapProgram.bind(); - _cascadedShadowMapProgram.setUniformValue("diffuseMap", 0); - _cascadedShadowMapProgram.setUniformValue("shadowMap", 1); - _cascadedShadowMapDistancesLocation = _cascadedShadowMapProgram.uniformLocation("shadowDistances"); - _cascadedShadowMapProgram.release(); + initProgram(_cascadedShadowMapProgram, _cascadedShadowMapLocations); _cascadedShadowNormalMapProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/model_normal_map.vert"); @@ -321,13 +295,7 @@ void Model::init() { Application::resourcesPath() + "shaders/model_cascaded_shadow_normal_map.frag"); _cascadedShadowNormalMapProgram.link(); - _cascadedShadowNormalMapProgram.bind(); - _cascadedShadowNormalMapProgram.setUniformValue("diffuseMap", 0); - _cascadedShadowNormalMapProgram.setUniformValue("normalMap", 1); - _cascadedShadowNormalMapProgram.setUniformValue("shadowMap", 2); - _cascadedShadowNormalMapDistancesLocation = _cascadedShadowNormalMapProgram.uniformLocation("shadowDistances"); - _cascadedShadowNormalMapTangentLocation = _cascadedShadowNormalMapProgram.attributeLocation("tangent"); - _cascadedShadowNormalMapProgram.release(); + initProgram(_cascadedShadowNormalMapProgram, _cascadedShadowNormalMapLocations, 1, 2); _cascadedShadowSpecularMapProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/model.vert"); @@ -335,12 +303,7 @@ void Model::init() { Application::resourcesPath() + "shaders/model_cascaded_shadow_specular_map.frag"); _cascadedShadowSpecularMapProgram.link(); - _cascadedShadowSpecularMapProgram.bind(); - _cascadedShadowSpecularMapProgram.setUniformValue("diffuseMap", 0); - _cascadedShadowSpecularMapProgram.setUniformValue("specularMap", 1); - _cascadedShadowSpecularMapProgram.setUniformValue("shadowMap", 2); - _cascadedShadowSpecularMapDistancesLocation = _cascadedShadowSpecularMapProgram.uniformLocation("shadowDistances"); - _cascadedShadowSpecularMapProgram.release(); + initProgram(_cascadedShadowSpecularMapProgram, _cascadedShadowSpecularMapLocations, 1, 2); _cascadedShadowNormalSpecularMapProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/model_normal_map.vert"); @@ -348,15 +311,7 @@ void Model::init() { Application::resourcesPath() + "shaders/model_cascaded_shadow_normal_specular_map.frag"); _cascadedShadowNormalSpecularMapProgram.link(); - _cascadedShadowNormalSpecularMapProgram.bind(); - _cascadedShadowNormalSpecularMapProgram.setUniformValue("diffuseMap", 0); - _cascadedShadowNormalSpecularMapProgram.setUniformValue("normalMap", 1); - _cascadedShadowNormalSpecularMapProgram.setUniformValue("specularMap", 2); - _cascadedShadowNormalSpecularMapProgram.setUniformValue("shadowMap", 3); - _cascadedShadowNormalSpecularMapDistancesLocation = - _cascadedShadowNormalSpecularMapProgram.uniformLocation("shadowDistances"); - _cascadedShadowNormalSpecularMapTangentLocation = _cascadedShadowNormalSpecularMapProgram.attributeLocation("tangent"); - _cascadedShadowNormalSpecularMapProgram.release(); + initProgram(_cascadedShadowNormalSpecularMapProgram, _cascadedShadowNormalSpecularMapLocations, 2, 3); _shadowProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/model_shadow.vert"); @@ -630,17 +585,16 @@ bool Model::render(float alpha, RenderMode mode, bool receiveShadows) { glCullFace(GL_FRONT); } else if (mode == DEFAULT_RENDER_MODE) { - // set up the local lights + // update the local lights for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { - glm::vec4 color; - GLenum lightName = GL_LIGHT0 + i + 1; if (i < _localLights.size()) { const LocalLight& light = _localLights.at(i); - color = glm::vec4(light.color, 1.0f); - glm::vec4 position = glm::vec4(_rotation * light.direction, 0.0f); - glLightfv(lightName, GL_POSITION, (GLfloat*)&position); + _localLightColors[i] = glm::vec4(light.color, 1.0f); + _localLightDirections[i] = glm::normalize(Application::getInstance()->getUntranslatedViewMatrix() * + glm::vec4(_rotation * light.direction, 0.0f)); + } else { + _localLightColors[i] = glm::vec4(); } - glLightfv(lightName, GL_DIFFUSE, (GLfloat*)&color); } } } @@ -1417,10 +1371,9 @@ void Model::renderMeshes(float alpha, RenderMode mode, bool translucent, bool re const_cast(networkMesh.vertexBuffer).bind(); ProgramObject* program = &_program; + Locations* locations = &_locations; ProgramObject* skinProgram = &_skinProgram; SkinLocations* skinLocations = &_skinLocations; - int tangentLocation = _normalMapTangentLocation; - int shadowDistancesLocation = _cascadedShadowMapDistancesLocation; GLenum specularTextureUnit = 0; GLenum shadowTextureUnit = 0; if (mode == SHADOW_RENDER_MODE) { @@ -1433,41 +1386,40 @@ void Model::renderMeshes(float alpha, RenderMode mode, bool translucent, bool re if (receiveShadows) { if (cascadedShadows) { program = &_cascadedShadowNormalSpecularMapProgram; + locations = &_cascadedShadowNormalSpecularMapLocations; skinProgram = &_skinCascadedShadowNormalSpecularMapProgram; skinLocations = &_skinCascadedShadowNormalSpecularMapLocations; - tangentLocation = _cascadedShadowNormalSpecularMapTangentLocation; - shadowDistancesLocation = _cascadedShadowNormalSpecularMapDistancesLocation; } else { program = &_shadowNormalSpecularMapProgram; + locations = &_shadowNormalSpecularMapLocations; skinProgram = &_skinShadowNormalSpecularMapProgram; skinLocations = &_skinShadowNormalSpecularMapLocations; - tangentLocation = _shadowNormalSpecularMapTangentLocation; } shadowTextureUnit = GL_TEXTURE3; } else { program = &_normalSpecularMapProgram; + locations = &_normalSpecularMapLocations; skinProgram = &_skinNormalSpecularMapProgram; skinLocations = &_skinNormalSpecularMapLocations; - tangentLocation = _normalSpecularMapTangentLocation; } specularTextureUnit = GL_TEXTURE2; } else if (receiveShadows) { if (cascadedShadows) { program = &_cascadedShadowNormalMapProgram; + locations = &_cascadedShadowNormalMapLocations; skinProgram = &_skinCascadedShadowNormalMapProgram; skinLocations = &_skinCascadedShadowNormalMapLocations; - tangentLocation = _cascadedShadowNormalMapTangentLocation; - shadowDistancesLocation = _cascadedShadowNormalMapDistancesLocation; } else { program = &_shadowNormalMapProgram; + locations = &_shadowNormalMapLocations; skinProgram = &_skinShadowNormalMapProgram; skinLocations = &_skinShadowNormalMapLocations; - tangentLocation = _shadowNormalMapTangentLocation; } shadowTextureUnit = GL_TEXTURE2; } else { program = &_normalMapProgram; + locations = &_normalMapLocations; skinProgram = &_skinNormalMapProgram; skinLocations = &_skinNormalMapLocations; } @@ -1475,17 +1427,19 @@ void Model::renderMeshes(float alpha, RenderMode mode, bool translucent, bool re if (receiveShadows) { if (cascadedShadows) { program = &_cascadedShadowSpecularMapProgram; + locations = &_cascadedShadowSpecularMapLocations; skinProgram = &_skinCascadedShadowSpecularMapProgram; skinLocations = &_skinCascadedShadowSpecularMapLocations; - shadowDistancesLocation = _cascadedShadowSpecularMapDistancesLocation; } else { program = &_shadowSpecularMapProgram; + locations = &_shadowSpecularMapLocations; skinProgram = &_skinShadowSpecularMapProgram; skinLocations = &_skinShadowSpecularMapLocations; } shadowTextureUnit = GL_TEXTURE2; } else { program = &_specularMapProgram; + locations = &_specularMapLocations; skinProgram = &_skinSpecularMapProgram; skinLocations = &_skinSpecularMapLocations; } @@ -1494,10 +1448,12 @@ void Model::renderMeshes(float alpha, RenderMode mode, bool translucent, bool re } else if (receiveShadows) { if (cascadedShadows) { program = &_cascadedShadowMapProgram; + locations = &_cascadedShadowMapLocations; skinProgram = &_skinCascadedShadowMapProgram; skinLocations = &_skinCascadedShadowMapLocations; } else { program = &_shadowMapProgram; + locations = &_shadowMapLocations; skinProgram = &_skinShadowMapProgram; skinLocations = &_skinShadowMapLocations; } @@ -1506,6 +1462,7 @@ void Model::renderMeshes(float alpha, RenderMode mode, bool translucent, bool re const MeshState& state = _meshStates.at(i); ProgramObject* activeProgram = program; + Locations* activeLocations = locations; glPushMatrix(); Application::getInstance()->loadTranslatedViewMatrix(_translation); @@ -1522,22 +1479,24 @@ void Model::renderMeshes(float alpha, RenderMode mode, bool translucent, bool re skinProgram->enableAttributeArray(skinLocations->clusterIndices); skinProgram->enableAttributeArray(skinLocations->clusterWeights); activeProgram = skinProgram; - tangentLocation = skinLocations->tangent; - if (cascadedShadows) { - program->setUniform(skinLocations->shadowDistances, Application::getInstance()->getShadowDistances()); - } + activeLocations = skinLocations; + } else { glMultMatrixf((const GLfloat*)&state.clusterMatrices[0]); program->bind(); - if (cascadedShadows) { - program->setUniform(shadowDistancesLocation, Application::getInstance()->getShadowDistances()); - } } - + if (cascadedShadows) { + activeProgram->setUniform(activeLocations->shadowDistances, Application::getInstance()->getShadowDistances()); + } + activeProgram->setUniformValueArray(activeLocations->localLightColors, + (const GLfloat*)_localLightColors, MAX_LOCAL_LIGHTS, 4); + activeProgram->setUniformValueArray(activeLocations->localLightDirections, + (const GLfloat*)_localLightDirections, MAX_LOCAL_LIGHTS, 4); + if (mesh.blendshapes.isEmpty()) { if (!(mesh.tangents.isEmpty() || mode == SHADOW_RENDER_MODE)) { - activeProgram->setAttributeBuffer(tangentLocation, GL_FLOAT, vertexCount * 2 * sizeof(glm::vec3), 3); - activeProgram->enableAttributeArray(tangentLocation); + activeProgram->setAttributeBuffer(activeLocations->tangent, GL_FLOAT, vertexCount * 2 * sizeof(glm::vec3), 3); + activeProgram->enableAttributeArray(activeLocations->tangent); } glColorPointer(3, GL_FLOAT, 0, (void*)(vertexCount * 2 * sizeof(glm::vec3) + mesh.tangents.size() * sizeof(glm::vec3))); @@ -1546,8 +1505,8 @@ void Model::renderMeshes(float alpha, RenderMode mode, bool translucent, bool re } else { if (!(mesh.tangents.isEmpty() || mode == SHADOW_RENDER_MODE)) { - activeProgram->setAttributeBuffer(tangentLocation, GL_FLOAT, 0, 3); - activeProgram->enableAttributeArray(tangentLocation); + activeProgram->setAttributeBuffer(activeLocations->tangent, GL_FLOAT, 0, 3); + activeProgram->enableAttributeArray(activeLocations->tangent); } glColorPointer(3, GL_FLOAT, 0, (void*)(mesh.tangents.size() * sizeof(glm::vec3))); glTexCoordPointer(2, GL_FLOAT, 0, (void*)((mesh.tangents.size() + mesh.colors.size()) * sizeof(glm::vec3))); @@ -1635,7 +1594,7 @@ void Model::renderMeshes(float alpha, RenderMode mode, bool translucent, bool re glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE0); - activeProgram->disableAttributeArray(tangentLocation); + activeProgram->disableAttributeArray(activeLocations->tangent); } if (specularTextureUnit) { diff --git a/interface/src/renderer/Model.h b/interface/src/renderer/Model.h index fe09710d0a..a4eae8fd9a 100644 --- a/interface/src/renderer/Model.h +++ b/interface/src/renderer/Model.h @@ -258,6 +258,9 @@ private: QList _runningAnimations; + glm::vec4 _localLightColors[MAX_LOCAL_LIGHTS]; + glm::vec4 _localLightDirections[MAX_LOCAL_LIGHTS]; + static ProgramObject _program; static ProgramObject _normalMapProgram; static ProgramObject _specularMapProgram; @@ -304,13 +307,35 @@ private: static int _cascadedShadowSpecularMapDistancesLocation; static int _cascadedShadowNormalSpecularMapDistancesLocation; - class SkinLocations { + class Locations { + public: + int localLightColors; + int localLightDirections; + int tangent; + int shadowDistances; + }; + + static Locations _locations; + static Locations _normalMapLocations; + static Locations _specularMapLocations; + static Locations _normalSpecularMapLocations; + static Locations _shadowMapLocations; + static Locations _shadowNormalMapLocations; + static Locations _shadowSpecularMapLocations; + static Locations _shadowNormalSpecularMapLocations; + static Locations _cascadedShadowMapLocations; + static Locations _cascadedShadowNormalMapLocations; + static Locations _cascadedShadowSpecularMapLocations; + static Locations _cascadedShadowNormalSpecularMapLocations; + + static void initProgram(ProgramObject& program, Locations& locations, + int specularTextureUnit = 1, int shadowTextureUnit = 1); + + class SkinLocations : public Locations { public: int clusterMatrices; int clusterIndices; - int clusterWeights; - int tangent; - int shadowDistances; + int clusterWeights; }; static SkinLocations _skinLocations; From 02bf76240aa7554d58ed16f173ec4084ba31fa8b Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 22 Jul 2014 18:30:28 -0700 Subject: [PATCH 8/8] Multiply local light colors by material diffuse reflectance. --- interface/src/renderer/Model.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/interface/src/renderer/Model.cpp b/interface/src/renderer/Model.cpp index 14bf3fb684..8fac5d3d03 100644 --- a/interface/src/renderer/Model.cpp +++ b/interface/src/renderer/Model.cpp @@ -588,10 +588,8 @@ bool Model::render(float alpha, RenderMode mode, bool receiveShadows) { // update the local lights for (int i = 0; i < MAX_LOCAL_LIGHTS; i++) { if (i < _localLights.size()) { - const LocalLight& light = _localLights.at(i); - _localLightColors[i] = glm::vec4(light.color, 1.0f); _localLightDirections[i] = glm::normalize(Application::getInstance()->getUntranslatedViewMatrix() * - glm::vec4(_rotation * light.direction, 0.0f)); + glm::vec4(_rotation * _localLights.at(i).direction, 0.0f)); } else { _localLightColors[i] = glm::vec4(); } @@ -1488,8 +1486,6 @@ void Model::renderMeshes(float alpha, RenderMode mode, bool translucent, bool re if (cascadedShadows) { activeProgram->setUniform(activeLocations->shadowDistances, Application::getInstance()->getShadowDistances()); } - activeProgram->setUniformValueArray(activeLocations->localLightColors, - (const GLfloat*)_localLightColors, MAX_LOCAL_LIGHTS, 4); activeProgram->setUniformValueArray(activeLocations->localLightDirections, (const GLfloat*)_localLightDirections, MAX_LOCAL_LIGHTS, 4); @@ -1544,6 +1540,12 @@ void Model::renderMeshes(float alpha, RenderMode mode, bool translucent, bool re glMaterialfv(GL_FRONT, GL_SPECULAR, (const float*)&specular); glMaterialf(GL_FRONT, GL_SHININESS, part.shininess); + for (int k = 0; k < qMin(MAX_LOCAL_LIGHTS, _localLights.size()); k++) { + _localLightColors[k] = glm::vec4(_localLights.at(k).color, 1.0f) * diffuse; + } + activeProgram->setUniformValueArray(activeLocations->localLightColors, + (const GLfloat*)_localLightColors, MAX_LOCAL_LIGHTS, 4); + Texture* diffuseMap = networkPart.diffuseTexture.data(); if (mesh.isEye && diffuseMap) { diffuseMap = (_dilatedTextures[i][j] =