From b360b6c55ddf2ee5822e9a3e7d6894126f6db3ab Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 5 Aug 2014 17:07:03 -0700 Subject: [PATCH] PR cleanup + JS API --- examples/sit.js | 19 +++++-- interface/src/Application.cpp | 2 +- interface/src/avatar/Avatar.cpp | 13 +++-- interface/src/avatar/Avatar.h | 2 +- interface/src/avatar/ModelReferential.cpp | 60 +++++++++++++++++------ interface/src/avatar/ModelReferential.h | 4 +- interface/src/avatar/MyAvatar.cpp | 47 +++++++++++------- interface/src/avatar/MyAvatar.h | 6 ++- libraries/avatars/src/AvatarData.cpp | 35 +++++++------ libraries/avatars/src/AvatarData.h | 3 ++ libraries/avatars/src/Referential.cpp | 12 +++-- libraries/avatars/src/Referential.h | 1 + libraries/models/CMakeLists.txt | 1 - libraries/models/src/ModelItem.h | 1 - 14 files changed, 139 insertions(+), 67 deletions(-) diff --git a/examples/sit.js b/examples/sit.js index 0f4b199855..072471aa30 100644 --- a/examples/sit.js +++ b/examples/sit.js @@ -89,7 +89,12 @@ var sittingDownAnimation = function(deltaTime) { var pos = { x: startPosition.x - 0.3 * factor, y: startPosition.y - 0.5 * factor, z: startPosition.z}; MyAvatar.position = pos; - } + } else { + Script.update.disconnect(sittingDownAnimation); + if (seat.model) { + MyAvatar.setModelReferential(seat.model.id); + } + } } var standingUpAnimation = function(deltaTime) { @@ -103,7 +108,10 @@ var standingUpAnimation = function(deltaTime) { var pos = { x: startPosition.x + 0.3 * (passedTime/animationLenght), y: startPosition.y + 0.5 * (passedTime/animationLenght), z: startPosition.z}; MyAvatar.position = pos; - } + } else { + Script.update.disconnect(standingUpAnimation); + + } } var goToSeatAnimation = function(deltaTime) { @@ -147,7 +155,8 @@ function standUp() { print("standUp sitting status: " + Settings.getValue(sittingSettingsHandle, false)); passedTime = 0.0; startPosition = MyAvatar.position; - try{ + MyAvatar.clearReferential(); + try{ Script.update.disconnect(sittingDownAnimation); } catch (e){} Script.update.connect(standingUpAnimation); @@ -197,8 +206,10 @@ Controller.mousePressEvent.connect(function(event) { var clickedOverlay = Overlays.getOverlayAtPoint({x: event.x, y: event.y}); if (clickedOverlay == sitDownButton) { + seat.model = null; sitDown(); } else if (clickedOverlay == standUpButton) { + seat.model = null; standUp(); } else { var pickRay = Camera.computePickRay(event.x, event.y); @@ -214,6 +225,7 @@ Controller.mousePressEvent.connect(function(event) { model.properties.sittingPoints[i].indicator.position, model.properties.sittingPoints[i].indicator.scale / 2)) { clickedOnSeat = true; + seat.model = model; seat.position = model.properties.sittingPoints[i].indicator.position; seat.rotation = model.properties.sittingPoints[i].indicator.orientation; } @@ -355,6 +367,7 @@ Script.update.connect(update); Controller.keyPressEvent.connect(keyPressEvent); Script.scriptEnding.connect(function() { + MyAvatar.clearReferential(); for (var i = 0; i < pose.length; i++){ MyAvatar.clearJointData(pose[i].joint); } diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 1ae29fcd88..6ae2452c41 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2176,7 +2176,7 @@ void Application::update(float deltaTime) { // Update _viewFrustum with latest camera and view frustum data... // NOTE: we get this from the view frustum, to make it simpler, since the - // loadViewFrumstum() methodill get the correct details from the camera + // loadViewFrumstum() method will get the correct details from the camera // We could optimize this to not actually load the viewFrustum, since we don't // actually need to calculate the view frustum planes to send these details // to the server. diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index d4df5b21f0..f4ed442aeb 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -182,15 +182,20 @@ void Avatar::simulate(float deltaTime) { } if (_referential) { if (_referential->hasExtraData()) { - qDebug() << "Has extra data"; + ModelTree* tree = Application::getInstance()->getModels()->getTree(); switch (_referential->type()) { case Referential::MODEL: - qDebug() << "[DEBUG] Switching to the right referential"; _referential = new ModelReferential(_referential, - Application::getInstance()->getModels()->getTree(), this); + tree, + this); + break; + case Referential::JOINT: + _referential = new JointReferential(_referential, + tree, + this); break; default: - qDebug() << "Non handled referential type"; + qDebug() << "[WARNING] Avatar::simulate(): Unknown referential type."; break; } } diff --git a/interface/src/avatar/Avatar.h b/interface/src/avatar/Avatar.h index 6b7408309d..555a0f6d32 100755 --- a/interface/src/avatar/Avatar.h +++ b/interface/src/avatar/Avatar.h @@ -209,7 +209,7 @@ protected: virtual void renderAttachments(RenderMode renderMode); virtual void updateJointMappings(); - + private: bool _initialized; diff --git a/interface/src/avatar/ModelReferential.cpp b/interface/src/avatar/ModelReferential.cpp index fb80d7f2ef..88a02a909b 100644 --- a/interface/src/avatar/ModelReferential.cpp +++ b/interface/src/avatar/ModelReferential.cpp @@ -22,20 +22,18 @@ ModelReferential::ModelReferential(Referential* referential, ModelTree* tree, Av unpackExtraData(reinterpret_cast(referential->getExtraData().data()), referential->getExtraData().size()); if (!isValid()) { - qDebug() << "ModelReferential::copyConstructo(): Not Valid"; + qDebug() << "ModelReferential::copyConstructor(): Not Valid"; return; } - const ModelItem* item = _tree->findModelByID(_modelID); + const ModelItem* item = _tr + ree->findModelByID(_modelID); if (item != NULL) { _refScale = item->getRadius(); _refRotation = item->getModelRotation(); _refPosition = item->getPosition() * (float)TREE_SCALE; } - - _scale = referential->getScale(); - _rotation = referential->getRotation(); - _translation = referential->getTranslation(); + update(); } ModelReferential::ModelReferential(uint32_t modelID, ModelTree* tree, AvatarData* avatar) : @@ -63,7 +61,6 @@ _tree(tree) void ModelReferential::update() { const ModelItem* item = _tree->findModelByID(_modelID); if (!isValid() || item == NULL || _avatar == NULL) { - //qDebug() << "ModelReferential::update(): Not Valid"; return; } @@ -95,6 +92,25 @@ int ModelReferential::unpackExtraData(const unsigned char *sourceBuffer, int siz return sizeof(_modelID); } +JointReferential::JointReferential(Referential* referential, ModelTree* tree, AvatarData* avatar) : + ModelReferential(referential, tree, avatar) +{ + _type = JOINT; + if (!isValid()) { + qDebug() << "JointReferential::copyConstructor(): Not Valid"; + return; + } + + const ModelItem* item = _tree->findModelByID(_modelID); + const Model* model = getModel(item); + if (!isValid() || model == NULL || _jointIndex >= model->getJointStateCount()) { + _refScale = item->getRadius(); + model->getJointRotationInWorldFrame(_jointIndex, _refRotation); + model->getJointPositionInWorldFrame(_jointIndex, _refPosition); + } + update(); +} + JointReferential::JointReferential(uint32_t jointIndex, uint32_t modelID, ModelTree* tree, AvatarData* avatar) : ModelReferential(modelID, tree, avatar), _jointIndex(jointIndex) @@ -102,8 +118,8 @@ JointReferential::JointReferential(uint32_t jointIndex, uint32_t modelID, ModelT _type = JOINT; const ModelItem* item = _tree->findModelByID(_modelID); const Model* model = getModel(item); - if (!_isValid || model == NULL || _jointIndex >= model->getJointStateCount()) { - qDebug() << "Not Valid"; + if (!isValid() || model == NULL || _jointIndex >= model->getJointStateCount()) { + qDebug() << "JointReferential::constructor(): Not Valid"; _isValid = false; return; } @@ -121,9 +137,7 @@ JointReferential::JointReferential(uint32_t jointIndex, uint32_t modelID, ModelT void JointReferential::update() { const ModelItem* item = _tree->findModelByID(_modelID); const Model* model = getModel(item); - if (model == NULL || _jointIndex >= model->getJointStateCount()) { - qDebug() << "Not Valid"; - _isValid = false; + if (!isValid() || model == NULL || _jointIndex >= model->getJointStateCount()) { return; } @@ -150,7 +164,25 @@ const Model* JointReferential::getModel(const ModelItem* item) { if (item != NULL && fbxService != NULL) { return fbxService->getModelForModelItem(*item); } - qDebug() << "No Model"; - return NULL; } + +int JointReferential::packExtraData(unsigned char* destinationBuffer) const { + unsigned char* startPosition = destinationBuffer; + destinationBuffer += ModelReferential::packExtraData(destinationBuffer); + + memcpy(destinationBuffer, &_jointIndex, sizeof(_jointIndex)); + destinationBuffer += sizeof(_jointIndex); + + return destinationBuffer - startPosition; +} + +int JointReferential::unpackExtraData(const unsigned char *sourceBuffer, int size) { + const unsigned char* startPosition = sourceBuffer; + sourceBuffer += ModelReferential::unpackExtraData(sourceBuffer, size); + + memcpy(&_jointIndex, sourceBuffer, sizeof(_jointIndex)); + sourceBuffer += sizeof(_jointIndex); + + return sourceBuffer - startPosition; +} \ No newline at end of file diff --git a/interface/src/avatar/ModelReferential.h b/interface/src/avatar/ModelReferential.h index 53170bcf41..b3a718d728 100644 --- a/interface/src/avatar/ModelReferential.h +++ b/interface/src/avatar/ModelReferential.h @@ -33,12 +33,14 @@ protected: class JointReferential : public ModelReferential { public: - JointReferential(Referential* ref); + JointReferential(Referential* ref, ModelTree* tree, AvatarData* avatar); JointReferential(uint32_t jointIndex, uint32_t modelID, ModelTree* tree, AvatarData* avatar); virtual void update(); protected: const Model* getModel(const ModelItem* item); + virtual int packExtraData(unsigned char* destinationBuffer) const; + virtual int unpackExtraData(const unsigned char* sourceBuffer, int size); uint32_t _jointIndex; }; diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 7c899fcc0a..a8dfe1feb6 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -109,6 +109,10 @@ void MyAvatar::reset() { } void MyAvatar::update(float deltaTime) { + if (_referential) { + _referential->update(); + } + Head* head = getHead(); head->relaxLean(deltaTime); updateFromTrackers(deltaTime); @@ -127,22 +131,6 @@ void MyAvatar::update(float deltaTime) { } simulate(deltaTime); - - bool WANT_REFERENTIAL = false; - if (WANT_REFERENTIAL) { - int id = 12340; - const ModelItem* item = Application::getInstance()->getModels()->getTree()->findModelByID(id); - if (_referential) { - PerformanceTimer perfTimer("Referential"); - _referential->update(); - } else if (item != NULL) { - const Model* model = Application::getInstance()->getModels()->getModelForModelItem(*item); - if (model != NULL) { - _referential = new ModelReferential(id, - Application::getInstance()->getModels()->getTree(), this); - } - } - } } void MyAvatar::simulate(float deltaTime) { @@ -461,9 +449,30 @@ glm::vec3 MyAvatar::getRightPalmPosition() { return rightHandPosition; } -void MyAvatar::changeReferential(Referential *ref) { - delete _referential; - _referential = ref; +void MyAvatar::clearReferential() { + changeReferential(NULL); +} + +bool MyAvatar::setModelReferential(int id) { + ModelTree* tree = Application::getInstance()->getModels()->getTree(); + changeReferential(new ModelReferential(id, tree, this)); + if (_referential->isValid()) { + return true; + } else { + changeReferential(NULL); + return false; + } +} + +bool MyAvatar::setJointReferential(int id, int jointIndex) { + ModelTree* tree = Application::getInstance()->getModels()->getTree(); + changeReferential(new JointReferential(jointIndex, id, tree, this)); + if (!_referential->isValid()) { + return true; + } else { + changeReferential(NULL); + return false; + } } void MyAvatar::setLocalGravity(glm::vec3 gravity) { diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index 4744ca80b8..4f2802a35a 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -18,6 +18,8 @@ #include "Avatar.h" +class ModelItemID; + enum AvatarHandState { HAND_STATE_NULL = 0, @@ -149,7 +151,9 @@ public slots: glm::vec3 getLeftPalmPosition(); glm::vec3 getRightPalmPosition(); - void changeReferential(Referential* ref); + void clearReferential(); + bool setModelReferential(int id); + bool setJointReferential(int id, int jointIndex); signals: void transformChanged(); diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index 1bcc46639e..59f9a440f0 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -425,29 +425,19 @@ int AvatarData::parseDataAtOffset(const QByteArray& packet, int offset) { // Referential if (hasReferential) { - const unsigned char* start = sourceBuffer; - if (_referential == NULL) { - qDebug() << "New referential"; - _referential = new Referential(sourceBuffer, this); + Referential* ref = new Referential(sourceBuffer, this); + if (_referential == NULL || + ref->createdAt() > _referential->createdAt()) { + changeReferential(ref); } else { - Referential* ref = new Referential(sourceBuffer, this); - if (ref->createdAt() > _referential->createdAt()) { - qDebug() << "Replacing referential"; - delete _referential; - _referential = ref; - } else { - delete ref; - } + delete ref; } - //qDebug() << "Read " << sourceBuffer - start << " bytes."; _referential->update(); } else if (_referential != NULL) { - qDebug() << "Erasing referencial"; - delete _referential; - _referential = NULL; + changeReferential(NULL); } - - + + if (_headData->_isFaceshiftConnected) { float leftEyeBlink, rightEyeBlink, averageLoudness, browAudioLift; minPossibleSize += sizeof(leftEyeBlink) + sizeof(rightEyeBlink) + sizeof(averageLoudness) + sizeof(browAudioLift); @@ -569,6 +559,15 @@ int AvatarData::parseDataAtOffset(const QByteArray& packet, int offset) { return sourceBuffer - startPosition; } +bool AvatarData::hasReferential() { + return _referential != NULL; +} + +void AvatarData::changeReferential(Referential *ref) { + delete _referential; + _referential = ref; +} + void AvatarData::setJointData(int index, const glm::quat& rotation) { if (index == -1) { return; diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index a0aeb94670..767c905322 100755 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -289,6 +289,8 @@ public slots: void setBillboardFromNetworkReply(); void setJointMappingsFromNetworkReply(); void setSessionUUID(const QUuid& id) { _sessionUUID = id; } + bool hasReferential(); + protected: QUuid _sessionUUID; glm::vec3 _position; @@ -344,6 +346,7 @@ protected: /// Loads the joint indices, names from the FST file (if any) virtual void updateJointMappings(); + void changeReferential(Referential* ref); private: // privatize the copy constructor and assignment operator so they cannot be called diff --git a/libraries/avatars/src/Referential.cpp b/libraries/avatars/src/Referential.cpp index 722c54cbe3..63510da732 100644 --- a/libraries/avatars/src/Referential.cpp +++ b/libraries/avatars/src/Referential.cpp @@ -23,14 +23,17 @@ Referential::Referential(Type type, AvatarData* avatar) : _isValid = false; return; } - qDebug() << "[DEBUG] New Referential"; } Referential::Referential(const unsigned char*& sourceBuffer, AvatarData* avatar) : _isValid(false), _avatar(avatar) { + // Since we can't return how many byte have been read + // We take a reference to the pointer as argument and increment the pointer ouself. sourceBuffer += unpackReferential(sourceBuffer); + // The actual unpacking to the right referential type happens in Avatar::simulate() + // If subclassed, make sure to add a case there to unpack the new referential type correctly } Referential::~Referential() { @@ -40,9 +43,9 @@ int Referential::packReferential(unsigned char* destinationBuffer) const { const unsigned char* startPosition = destinationBuffer; destinationBuffer += pack(destinationBuffer); - unsigned char* sizePosition = destinationBuffer++; + unsigned char* sizePosition = destinationBuffer++; // Save a spot for the extra data size char size = packExtraData(destinationBuffer); - *sizePosition = size; + *sizePosition = size; // write extra data size in saved spot here destinationBuffer += size; return destinationBuffer - startPosition; @@ -56,6 +59,7 @@ int Referential::unpackReferential(const unsigned char* sourceBuffer) { char bytesRead = unpackExtraData(sourceBuffer, expectedSize); _isValid = (bytesRead == expectedSize); if (!_isValid) { + // Will occur if the new instance unpacking is of the wrong type qDebug() << "[ERROR] Referential extra data overflow"; } sourceBuffer += expectedSize; @@ -88,10 +92,12 @@ int Referential::unpack(const unsigned char* sourceBuffer) { } int Referential::packExtraData(unsigned char *destinationBuffer) const { + // Since we can't interpret that data, just store it in a buffer for later use. memcpy(destinationBuffer, _extraDataBuffer.data(), _extraDataBuffer.size()); return _extraDataBuffer.size(); } + int Referential::unpackExtraData(const unsigned char* sourceBuffer, int size) { _extraDataBuffer.clear(); _extraDataBuffer.setRawData(reinterpret_cast(sourceBuffer), size); diff --git a/libraries/avatars/src/Referential.h b/libraries/avatars/src/Referential.h index 9353ea0871..905447dcbb 100644 --- a/libraries/avatars/src/Referential.h +++ b/libraries/avatars/src/Referential.h @@ -17,6 +17,7 @@ class AvatarData; +/// Stores and enforce the relative position of an avatar to a given referential (ie. model, joint, ...) class Referential { public: enum Type { diff --git a/libraries/models/CMakeLists.txt b/libraries/models/CMakeLists.txt index 603bb02a11..8056d215da 100644 --- a/libraries/models/CMakeLists.txt +++ b/libraries/models/CMakeLists.txt @@ -23,7 +23,6 @@ link_hifi_library(octree ${TARGET_NAME} "${ROOT_DIR}") link_hifi_library(fbx ${TARGET_NAME} "${ROOT_DIR}") link_hifi_library(networking ${TARGET_NAME} "${ROOT_DIR}") link_hifi_library(animation ${TARGET_NAME} "${ROOT_DIR}") -link_hifi_library(avatars ${TARGET_NAME} "${ROOT_DIR}") # for streamable link_hifi_library(metavoxels ${TARGET_NAME} "${ROOT_DIR}") diff --git a/libraries/models/src/ModelItem.h b/libraries/models/src/ModelItem.h index 3e3c46a12d..4a9a2af2c4 100644 --- a/libraries/models/src/ModelItem.h +++ b/libraries/models/src/ModelItem.h @@ -179,7 +179,6 @@ void ModelItemIDfromScriptValue(const QScriptValue &object, ModelItemID& propert /// ModelItem class - this is the actual model item class. class ModelItem { - public: ModelItem();