diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 28a1b8b84b..a33a4e4595 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3800,8 +3800,8 @@ ScriptEngine* Application::loadScript(const QString& scriptFilename, bool isUser scriptEngine->getEntityScriptingInterface()->setPacketSender(&_entityEditSender); scriptEngine->getEntityScriptingInterface()->setEntityTree(_entities.getTree()); - // model has some custom types - Model::registerMetaTypes(scriptEngine); + // AvatarManager has some custom types + AvatarManager::registerMetaTypes(scriptEngine); // 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 469494760e..67a00d21df 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -345,17 +345,25 @@ void Avatar::render(const glm::vec3& cameraPosition, RenderMode renderMode) { ? 1.0f : GLOW_FROM_AVERAGE_LOUDNESS; - - // local lights directions and colors - const QVector& localLights = Application::getInstance()->getAvatarManager().getLocalLights(); - _skeletonModel.setLocalLights(localLights); - getHead()->getFaceModel().setLocalLights(localLights); - // render body if (Menu::getInstance()->isOptionChecked(MenuOption::Avatars)) { renderBody(renderMode, glowLevel); } if (renderMode != SHADOW_RENDER_MODE) { + // add local lights + const float BASE_LIGHT_DISTANCE = 2.0f; + const float LIGHT_EXPONENT = 1.0f; + const float LIGHT_CUTOFF = glm::radians(80.0f); + float distance = BASE_LIGHT_DISTANCE * _scale; + glm::vec3 position = glm::mix(getPosition(), getHead()->getEyePosition(), 0.75f); + glm::quat orientation = getOrientation(); + foreach (const AvatarManager::LocalLight& light, Application::getInstance()->getAvatarManager().getLocalLights()) { + glm::vec3 direction = orientation * light.direction; + Application::getInstance()->getDeferredLightingEffect()->addSpotLight(position - direction * distance, + distance * 2.0f, glm::vec3(), light.color, light.color, 1.0f, 0.0f, 0.0f, direction, + LIGHT_EXPONENT, LIGHT_CUTOFF); + } + bool renderSkeleton = Menu::getInstance()->isOptionChecked(MenuOption::RenderSkeletonCollisionShapes); bool renderHead = Menu::getInstance()->isOptionChecked(MenuOption::RenderHeadCollisionShapes); bool renderBounding = Menu::getInstance()->isOptionChecked(MenuOption::RenderBoundingCollisionShapes); diff --git a/interface/src/avatar/AvatarManager.cpp b/interface/src/avatar/AvatarManager.cpp index 0a9cbfe762..a5e74e42ba 100644 --- a/interface/src/avatar/AvatarManager.cpp +++ b/interface/src/avatar/AvatarManager.cpp @@ -11,9 +11,12 @@ #include +#include + #include #include +#include #include #include "Application.h" @@ -26,6 +29,23 @@ // We add _myAvatar into the hash with all the other AvatarData, and we use the default NULL QUid as the key. const QUuid MY_AVATAR_KEY; // NULL key +static QScriptValue localLightToScriptValue(QScriptEngine* engine, const AvatarManager::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, AvatarManager::LocalLight& light) { + vec3FromScriptValue(value.property("direction"), light.direction); + vec3FromScriptValue(value.property("color"), light.color); +} + +void AvatarManager::registerMetaTypes(QScriptEngine* engine) { + qScriptRegisterMetaType(engine, localLightToScriptValue, localLightFromScriptValue); + qScriptRegisterSequenceMetaType >(engine); +} + AvatarManager::AvatarManager(QObject* parent) : _avatarFades() { // register a meta type for the weak pointer we'll use for the owning avatar mixer for each avatar @@ -159,19 +179,19 @@ void AvatarManager::clearOtherAvatars() { _myAvatar->clearLookAtTargetAvatar(); } -void AvatarManager::setLocalLights(const QVector& localLights) { +void AvatarManager::setLocalLights(const QVector& localLights) { if (QThread::currentThread() != thread()) { - QMetaObject::invokeMethod(this, "setLocalLights", Q_ARG(const QVector&, localLights)); + QMetaObject::invokeMethod(this, "setLocalLights", Q_ARG(const QVector&, localLights)); return; } _localLights = localLights; } -QVector AvatarManager::getLocalLights() const { +QVector AvatarManager::getLocalLights() const { if (QThread::currentThread() != thread()) { - QVector result; + QVector result; QMetaObject::invokeMethod(const_cast(this), "getLocalLights", Qt::BlockingQueuedConnection, - Q_RETURN_ARG(QVector, result)); + Q_RETURN_ARG(QVector, result)); return result; } return _localLights; diff --git a/interface/src/avatar/AvatarManager.h b/interface/src/avatar/AvatarManager.h index ccb7459217..48f0546373 100644 --- a/interface/src/avatar/AvatarManager.h +++ b/interface/src/avatar/AvatarManager.h @@ -26,6 +26,10 @@ class AvatarManager : public AvatarHashMap { Q_OBJECT public: + + /// Registers the script types associated with the avatar manager. + static void registerMetaTypes(QScriptEngine* engine); + AvatarManager(QObject* parent = 0); void init(); @@ -37,8 +41,14 @@ public: void clearOtherAvatars(); - Q_INVOKABLE void setLocalLights(const QVector& localLights); - Q_INVOKABLE QVector getLocalLights() const; + class LocalLight { + public: + glm::vec3 color; + glm::vec3 direction; + }; + + Q_INVOKABLE void setLocalLights(const QVector& localLights); + Q_INVOKABLE QVector getLocalLights() const; private: AvatarManager(const AvatarManager& other); @@ -54,7 +64,10 @@ private: QVector _avatarFades; QSharedPointer _myAvatar; - QVector _localLights; + QVector _localLights; }; +Q_DECLARE_METATYPE(AvatarManager::LocalLight) +Q_DECLARE_METATYPE(QVector) + #endif // hifi_AvatarManager_h diff --git a/interface/src/renderer/Model.cpp b/interface/src/renderer/Model.cpp index edbbcb04cb..56ab0c8219 100644 --- a/interface/src/renderer/Model.cpp +++ b/interface/src/renderer/Model.cpp @@ -11,7 +11,6 @@ #include #include -#include #include #include @@ -20,7 +19,6 @@ #include #include #include -#include #include #include @@ -33,23 +31,6 @@ 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), @@ -412,13 +393,6 @@ bool Model::render(float alpha, RenderMode mode) { glEnable(GL_CULL_FACE); if (mode == SHADOW_RENDER_MODE) { glCullFace(GL_FRONT); - - } else if (mode == DEFAULT_RENDER_MODE) { - // add the local lights - foreach (const LocalLight& light, _localLights) { - Application::getInstance()->getDeferredLightingEffect()->addSpotLight(glm::vec3(), 1.0f, glm::vec3(), - light.color, light.color, 1.0f, 0.0f, 0.0f); - } } } diff --git a/interface/src/renderer/Model.h b/interface/src/renderer/Model.h index a9bc775d0a..fbf0167844 100644 --- a/interface/src/renderer/Model.h +++ b/interface/src/renderer/Model.h @@ -34,17 +34,12 @@ class Shape; typedef QSharedPointer AnimationHandlePointer; typedef QWeakPointer WeakAnimationHandlePointer; -const int MAX_LOCAL_LIGHTS = 2; - /// A generic 3D model displaying geometry loaded from a URL. class Model : public QObject, public PhysicsEntity { Q_OBJECT public: - /// Registers the script types associated with models. - static void registerMetaTypes(QScriptEngine* engine); - Model(QObject* parent = NULL); virtual ~Model(); @@ -171,15 +166,6 @@ public: void setBlendedVertices(int blendNumber, const QWeakPointer& geometry, const QVector& vertices, const QVector& normals); - 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; } QVector& getJointStates() { return _jointStates; } @@ -203,8 +189,6 @@ protected: bool _showTrueJointTransforms; - QVector _localLights; - QVector _jointStates; class MeshState { @@ -336,8 +320,6 @@ 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 {