From 84bcfb7d7142648a27db9595d9d0eab06b09551c Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Tue, 22 Dec 2015 17:43:01 -0800 Subject: [PATCH] hook up a way for scripts to set model-entity joint state --- libraries/animation/src/Rig.cpp | 8 +++++ libraries/animation/src/Rig.h | 2 ++ .../src/RenderableModelEntityItem.cpp | 34 +++++++++++++++++-- .../src/RenderableModelEntityItem.h | 9 +++++ libraries/entities/src/EntityItem.h | 2 ++ .../entities/src/EntityScriptingInterface.cpp | 20 +++++++++++ .../entities/src/EntityScriptingInterface.h | 2 ++ libraries/entities/src/ModelEntityItem.h | 1 - libraries/render-utils/src/Model.cpp | 8 +++++ libraries/render-utils/src/Model.h | 3 ++ libraries/shared/src/SpatiallyNestable.h | 4 ++- 11 files changed, 89 insertions(+), 4 deletions(-) diff --git a/libraries/animation/src/Rig.cpp b/libraries/animation/src/Rig.cpp index 68f382d2d9..4b2d4facc8 100644 --- a/libraries/animation/src/Rig.cpp +++ b/libraries/animation/src/Rig.cpp @@ -386,6 +386,10 @@ bool Rig::getAbsoluteJointRotationInRigFrame(int jointIndex, glm::quat& rotation } } +bool Rig::setAbsoluteJointRotationInRigFrame(int jointIndex, glm::quat& rotation) { + return false; +} + bool Rig::getJointTranslation(int jointIndex, glm::vec3& translation) const { QReadLocker readLock(&_externalPoseSetLock); if (jointIndex >= 0 && jointIndex < (int)_externalPoseSet._relativePoses.size()) { @@ -406,6 +410,10 @@ bool Rig::getAbsoluteJointTranslationInRigFrame(int jointIndex, glm::vec3& trans } } +bool Rig::setAbsoluteJointTranslationInRigFrame(int jointIndex, glm::vec3& translation) { + return false; +} + bool Rig::getJointCombinedRotation(int jointIndex, glm::quat& result, const glm::quat& rotation) const { // AJT: TODO: used by attachments ASSERT(false); diff --git a/libraries/animation/src/Rig.h b/libraries/animation/src/Rig.h index 9faf93e40b..3881b35dfb 100644 --- a/libraries/animation/src/Rig.h +++ b/libraries/animation/src/Rig.h @@ -134,6 +134,8 @@ public: // rig space (thread-safe) bool getAbsoluteJointRotationInRigFrame(int jointIndex, glm::quat& rotation) const; bool getAbsoluteJointTranslationInRigFrame(int jointIndex, glm::vec3& translation) const; + bool setAbsoluteJointRotationInRigFrame(int jointIndex, glm::quat& rotation); + bool setAbsoluteJointTranslationInRigFrame(int jointIndex, glm::vec3& translation); // legacy bool getJointCombinedRotation(int jointIndex, glm::quat& result, const glm::quat& rotation) const; diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index 9127e4ca22..61ad4d2c2a 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -292,7 +292,21 @@ void RenderableModelEntityItem::render(RenderArgs* args) { } if (_model) { - // handle animations.. + // handle script updates... + _scriptSetFrameDataLock.withWriteLock([&] { + while (!_scriptSetFrameDataRotationsIndexes.empty()) { + int index = _scriptSetFrameDataRotationsIndexes.dequeue(); + glm::quat rotation = _scriptSetFrameDataRotations.dequeue(); + _model->setJointRotation(index, true, rotation, 1.0f); + } + while (!_scriptSetFrameDataTranslationsIndexes.empty()) { + int index = _scriptSetFrameDataTranslationsIndexes.dequeue(); + glm::vec3 translation = _scriptSetFrameDataTranslations.dequeue(); + _model->setJointTranslation(index, true, translation, 1.0f); + } + }); + + // handle animations... if (hasAnimation()) { if (!jointsMapped()) { QStringList modelJointNames = _model->getJointNames(); @@ -600,7 +614,7 @@ bool RenderableModelEntityItem::contains(const glm::vec3& point) const { const FBXGeometry& collisionGeometry = collisionNetworkGeometry->getFBXGeometry(); return collisionGeometry.convexHullContains(worldToEntity(point)); } - + return false; } @@ -624,6 +638,22 @@ glm::vec3 RenderableModelEntityItem::getAbsoluteJointTranslationInObjectFrame(in return glm::vec3(0.0f); } +bool RenderableModelEntityItem::setAbsoluteJointRotationInObjectFrame(int index, glm::quat& rotation) { + _scriptSetFrameDataLock.withWriteLock([&] { + _scriptSetFrameDataRotationsIndexes.enqueue(index); + _scriptSetFrameDataRotations.enqueue(rotation); + }); + return true; +} + +bool RenderableModelEntityItem::setAbsoluteJointTranslationInObjectFrame(int index, glm::vec3& translation) { + _scriptSetFrameDataLock.withWriteLock([&] { + _scriptSetFrameDataTranslationsIndexes.enqueue(index); + _scriptSetFrameDataTranslations.enqueue(translation); + }); + return true; +} + void RenderableModelEntityItem::locationChanged() { EntityItem::locationChanged(); if (_model && _model->isActive()) { diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.h b/libraries/entities-renderer/src/RenderableModelEntityItem.h index cf948bd7a0..d0847b66cb 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.h +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.h @@ -71,6 +71,8 @@ public: // these are in the frame of this object (model space) virtual glm::quat getAbsoluteJointRotationInObjectFrame(int index) const override; virtual glm::vec3 getAbsoluteJointTranslationInObjectFrame(int index) const override; + virtual bool setAbsoluteJointRotationInObjectFrame(int index, glm::quat& rotation) override; + virtual bool setAbsoluteJointTranslationInObjectFrame(int index, glm::vec3& translation) override; virtual void loader() override; virtual void locationChanged() override; @@ -91,6 +93,13 @@ private: render::ItemID _myMetaItem; bool _showCollisionHull = false; + + // these are used to let scripts set ModelEntityItem joint data + ReadWriteLockable _scriptSetFrameDataLock; + QQueue _scriptSetFrameDataRotations; + QQueue _scriptSetFrameDataRotationsIndexes; + QQueue _scriptSetFrameDataTranslations; + QQueue _scriptSetFrameDataTranslationsIndexes; }; #endif // hifi_RenderableModelEntityItem_h diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index 244198f7b8..841f68ddc1 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -381,6 +381,8 @@ public: // these are in the frame of this object virtual glm::quat getAbsoluteJointRotationInObjectFrame(int index) const override { return glm::quat(); } virtual glm::vec3 getAbsoluteJointTranslationInObjectFrame(int index) const override { return glm::vec3(0.0f); } + virtual bool setAbsoluteJointRotationInObjectFrame(int index, glm::quat& rotation) override { return false; } + virtual bool setAbsoluteJointTranslationInObjectFrame(int index, glm::vec3& translation) override { return false; } virtual void loader() {} // called indirectly when urls for geometry are updated diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index a0a6719521..c20db60601 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -826,3 +826,23 @@ glm::quat EntityScriptingInterface::getAbsoluteJointRotationInObjectFrame(const return glm::quat(); } } + +bool EntityScriptingInterface::setAbsoluteJointTranslationInObjectFrame(const QUuid& entityID, + int jointIndex, glm::vec3 translation) { + if (auto entity = checkForTreeEntityAndTypeMatch(entityID, EntityTypes::Model)) { + auto modelEntity = std::dynamic_pointer_cast(entity); + return modelEntity->setAbsoluteJointTranslationInObjectFrame(jointIndex, translation); + } else { + return false; + } +} + +bool EntityScriptingInterface::setAbsoluteJointRotationInObjectFrame(const QUuid& entityID, + int jointIndex, glm::quat rotation) { + if (auto entity = checkForTreeEntityAndTypeMatch(entityID, EntityTypes::Model)) { + auto modelEntity = std::dynamic_pointer_cast(entity); + return modelEntity->setAbsoluteJointRotationInObjectFrame(jointIndex, rotation); + } else { + return false; + } +} diff --git a/libraries/entities/src/EntityScriptingInterface.h b/libraries/entities/src/EntityScriptingInterface.h index f745b6b644..5d56ea2b99 100644 --- a/libraries/entities/src/EntityScriptingInterface.h +++ b/libraries/entities/src/EntityScriptingInterface.h @@ -151,6 +151,8 @@ public slots: Q_INVOKABLE glm::vec3 getAbsoluteJointTranslationInObjectFrame(const QUuid& entityID, int jointIndex); Q_INVOKABLE glm::quat getAbsoluteJointRotationInObjectFrame(const QUuid& entityID, int jointIndex); + Q_INVOKABLE bool setAbsoluteJointTranslationInObjectFrame(const QUuid& entityID, int jointIndex, glm::vec3 translation); + Q_INVOKABLE bool setAbsoluteJointRotationInObjectFrame(const QUuid& entityID, int jointIndex, glm::quat rotation); signals: void collisionWithEntity(const EntityItemID& idA, const EntityItemID& idB, const Collision& collision); diff --git a/libraries/entities/src/ModelEntityItem.h b/libraries/entities/src/ModelEntityItem.h index b08fed5970..9978f3359a 100644 --- a/libraries/entities/src/ModelEntityItem.h +++ b/libraries/entities/src/ModelEntityItem.h @@ -129,7 +129,6 @@ protected: QVector _lastKnownFrameDataTranslations; int _lastKnownCurrentFrame; - bool isAnimatingSomething() const; rgbColor _color; diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index dbd3a6289f..91ca8e60cb 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -699,6 +699,14 @@ void Model::setJointTranslation(int index, bool valid, const glm::vec3& translat _rig->setJointTranslation(index, valid, translation, priority); } +bool Model::setAbsoluteJointRotationInRigFrame(int jointIndex, glm::quat& rotation) { + return _rig->setAbsoluteJointRotationInRigFrame(jointIndex, rotation); +} + +bool Model::setAbsoluteJointTranslationInRigFrame(int jointIndex, glm::vec3& translation) { + return _rig->setAbsoluteJointTranslationInRigFrame(jointIndex, translation); +} + int Model::getParentJointIndex(int jointIndex) const { return (isActive() && jointIndex != -1) ? _geometry->getFBXGeometry().joints.at(jointIndex).parentIndex : -1; } diff --git a/libraries/render-utils/src/Model.h b/libraries/render-utils/src/Model.h index a93338e41a..327583c212 100644 --- a/libraries/render-utils/src/Model.h +++ b/libraries/render-utils/src/Model.h @@ -170,6 +170,9 @@ public: bool getAbsoluteJointRotationInRigFrame(int jointIndex, glm::quat& rotationOut) const; bool getAbsoluteJointTranslationInRigFrame(int jointIndex, glm::vec3& translationOut) const; + bool setAbsoluteJointRotationInRigFrame(int jointIndex, glm::quat& rotation); + bool setAbsoluteJointTranslationInRigFrame(int jointIndex, glm::vec3& translation); + bool getRelativeDefaultJointRotation(int jointIndex, glm::quat& rotationOut) const; bool getRelativeDefaultJointTranslation(int jointIndex, glm::vec3& translationOut) const; diff --git a/libraries/shared/src/SpatiallyNestable.h b/libraries/shared/src/SpatiallyNestable.h index 7a43e2a563..f6a9678d34 100644 --- a/libraries/shared/src/SpatiallyNestable.h +++ b/libraries/shared/src/SpatiallyNestable.h @@ -91,7 +91,9 @@ public: virtual const Transform getAbsoluteJointTransformInObjectFrame(int jointIndex) const; virtual glm::quat getAbsoluteJointRotationInObjectFrame(int index) const { assert(false); return glm::quat(); } virtual glm::vec3 getAbsoluteJointTranslationInObjectFrame(int index) const { assert(false); return glm::vec3(); } - + virtual bool setAbsoluteJointRotationInObjectFrame(int index, glm::quat& rotation) { assert(false); return false; } + virtual bool setAbsoluteJointTranslationInObjectFrame(int index, glm::vec3& translation) { assert(false); return false; } + SpatiallyNestablePointer getThisPointer() const; protected: