From 92b75d29cff1d340366c6aec6c0428b9cbc3ecd7 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 30 Jul 2014 14:18:55 -0700 Subject: [PATCH 01/17] Added referentials base class --- libraries/avatars/src/Referential.cpp | 18 +++++++++++++ libraries/avatars/src/Referential.h | 38 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 libraries/avatars/src/Referential.cpp create mode 100644 libraries/avatars/src/Referential.h diff --git a/libraries/avatars/src/Referential.cpp b/libraries/avatars/src/Referential.cpp new file mode 100644 index 0000000000..c929e2701f --- /dev/null +++ b/libraries/avatars/src/Referential.cpp @@ -0,0 +1,18 @@ +// +// Referential.cpp +// +// +// Created by Clement on 7/30/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "Referential.h" + +Referential::Referential(AvatarData* avatar) : + _isValid(true), + _avatar(avatar) +{ +} diff --git a/libraries/avatars/src/Referential.h b/libraries/avatars/src/Referential.h new file mode 100644 index 0000000000..d125819edc --- /dev/null +++ b/libraries/avatars/src/Referential.h @@ -0,0 +1,38 @@ +// +// Referential.h +// +// +// Created by Clement on 7/30/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_Referential_h +#define hifi_Referential_h + +#include "AvatarData.h" + +class Referential { +public: + virtual bool isValid() { return _isValid; } + virtual void update() = 0; + +protected: + Referential(AvatarData* avatar); + + bool _isValid; + AvatarData* _avatar; + + glm::vec3 _refPosition; + glm::quat _refRotation; + float _refScale; + + glm::vec3 _translation; + glm::quat _rotation; + float _scale; +}; + + +#endif // hifi_Referential_h \ No newline at end of file From 069f1ed247ec1991bd462004cb2ed6af06e50780 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 30 Jul 2014 14:19:25 -0700 Subject: [PATCH 02/17] Added ModelReferential --- libraries/models/CMakeLists.txt | 1 + libraries/models/src/ModelReferential.cpp | 60 +++++++++++++++++++++++ libraries/models/src/ModelReferential.h | 29 +++++++++++ 3 files changed, 90 insertions(+) create mode 100644 libraries/models/src/ModelReferential.cpp create mode 100644 libraries/models/src/ModelReferential.h diff --git a/libraries/models/CMakeLists.txt b/libraries/models/CMakeLists.txt index 06578371cc..3486d329e3 100644 --- a/libraries/models/CMakeLists.txt +++ b/libraries/models/CMakeLists.txt @@ -29,6 +29,7 @@ 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/ModelReferential.cpp b/libraries/models/src/ModelReferential.cpp new file mode 100644 index 0000000000..e54b49a1e7 --- /dev/null +++ b/libraries/models/src/ModelReferential.cpp @@ -0,0 +1,60 @@ +// +// ModelReferential.cpp +// +// +// Created by Clement on 7/30/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "ModelTree.h" + +#include "ModelReferential.h" + +ModelReferential::ModelReferential(uint32_t modelID, ModelTree* tree, AvatarData* avatar) : +Referential(avatar), +_modelID(modelID), +_tree(tree) +{ + const ModelItem* item = _tree->findModelByID(_modelID); + if (!_isValid || item == NULL || _avatar == NULL) { + _isValid = false; + return; + } + + _refScale = item->getRadius(); + _refRotation = item->getModelRotation(); + _refPosition = item->getPosition(); + + glm::quat refInvRot = glm::inverse(_refRotation); + _scale = _avatar->getTargetScale() / _refScale; + _rotation = refInvRot * _avatar->getOrientation(); + _translation = refInvRot * (avatar->getPosition() - _refPosition) / _refScale; +} + +void ModelReferential::update() { + const ModelItem* item = _tree->findModelByID(_modelID); + if (!_isValid || item == NULL || _avatar == NULL) { + _isValid = false; + return; + } + + bool somethingChanged = false; + if (item->getRadius() != _refScale) { + _refScale = item->getRadius(); + _avatar->setTargetScale(_refScale * _scale); + somethingChanged = true; + } + if (item->getModelRotation() != _refRotation) { + _refRotation = item->getModelRotation(); + _avatar->setOrientation(_refRotation * _rotation); + somethingChanged = true; + } + if (item->getPosition() != _refPosition || somethingChanged) { + _refPosition = item->getPosition(); + _avatar->setPosition(_refPosition + _refRotation * (_translation * _refScale)); + somethingChanged = true; + } +} diff --git a/libraries/models/src/ModelReferential.h b/libraries/models/src/ModelReferential.h new file mode 100644 index 0000000000..8ae692568a --- /dev/null +++ b/libraries/models/src/ModelReferential.h @@ -0,0 +1,29 @@ +// +// ModelReferential.h +// +// +// Created by Clement on 7/30/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_ModelReferential_h +#define hifi_ModelReferential_h + +#include + +class ModelTree; + +class ModelReferential : public Referential { +public: + ModelReferential(uint32_t modelID, ModelTree* tree, AvatarData* avatar); + virtual void update(); + +protected: + uint32_t _modelID; + ModelTree* _tree; +}; + +#endif // hifi_ModelReferential_h \ No newline at end of file From bc77630c5bbe885e155bd7e76328b58063e5a60f Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 30 Jul 2014 16:48:11 -0700 Subject: [PATCH 03/17] Extanded FBXService + moved ModelReferential --- .../src/avatar}/ModelReferential.cpp | 35 ++++++++++++++++++- .../src/avatar}/ModelReferential.h | 12 +++++++ interface/src/models/ModelTreeRenderer.cpp | 4 +++ interface/src/models/ModelTreeRenderer.h | 2 +- libraries/models/src/ModelTree.h | 4 +++ 5 files changed, 55 insertions(+), 2 deletions(-) rename {libraries/models/src => interface/src/avatar}/ModelReferential.cpp (66%) rename {libraries/models/src => interface/src/avatar}/ModelReferential.h (68%) diff --git a/libraries/models/src/ModelReferential.cpp b/interface/src/avatar/ModelReferential.cpp similarity index 66% rename from libraries/models/src/ModelReferential.cpp rename to interface/src/avatar/ModelReferential.cpp index e54b49a1e7..c681bb29da 100644 --- a/libraries/models/src/ModelReferential.cpp +++ b/interface/src/avatar/ModelReferential.cpp @@ -9,6 +9,8 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include + #include "ModelTree.h" #include "ModelReferential.h" @@ -19,7 +21,7 @@ _modelID(modelID), _tree(tree) { const ModelItem* item = _tree->findModelByID(_modelID); - if (!_isValid || item == NULL || _avatar == NULL) { + if (!_isValid || item == NULL) { _isValid = false; return; } @@ -58,3 +60,34 @@ void ModelReferential::update() { somethingChanged = true; } } + +JointReferential::JointReferential(uint32_t jointID, uint32_t modelID, ModelTree* tree, AvatarData* avatar) : + ModelReferential(modelID, tree, avatar), + _jointID(jointID) +{ + const Model* model = getModel(_tree->findModelByID(_modelID)); + + if (!_isValid || model == NULL || model->getJointStateCount() <= jointID) { + _isValid = false; + return; + } +} + +void JointReferential::update() { + const ModelItem* item = _tree->findModelByID(_modelID); + if (!_isValid || item == NULL) { + _isValid = false; + return; + } + + +} + +const Model* JointReferential::getModel(const ModelItem* item) { + ModelItemFBXService* fbxService = _tree->getFBXService(); + if (item != NULL && fbxService != NULL) { + return fbxService->getModelForModelItem(*item); + } + + return NULL; +} diff --git a/libraries/models/src/ModelReferential.h b/interface/src/avatar/ModelReferential.h similarity index 68% rename from libraries/models/src/ModelReferential.h rename to interface/src/avatar/ModelReferential.h index 8ae692568a..8f9333451a 100644 --- a/libraries/models/src/ModelReferential.h +++ b/interface/src/avatar/ModelReferential.h @@ -15,6 +15,7 @@ #include class ModelTree; +class Model; class ModelReferential : public Referential { public: @@ -26,4 +27,15 @@ protected: ModelTree* _tree; }; +class JointReferential : public ModelReferential { +public: + JointReferential(uint32_t jointID, uint32_t modelID, ModelTree* tree, AvatarData* avatar); + virtual void update(); + +protected: + const Model* getModel(const ModelItem* item); + + uint32_t _jointID; +}; + #endif // hifi_ModelReferential_h \ No newline at end of file diff --git a/interface/src/models/ModelTreeRenderer.cpp b/interface/src/models/ModelTreeRenderer.cpp index 78107db699..acbb373ffc 100644 --- a/interface/src/models/ModelTreeRenderer.cpp +++ b/interface/src/models/ModelTreeRenderer.cpp @@ -76,6 +76,10 @@ const FBXGeometry* ModelTreeRenderer::getGeometryForModel(const ModelItem& model return result; } +const Model* ModelTreeRenderer::getModelForModelItem(const ModelItem& modelItem) { + return getModel(modelItem); +} + Model* ModelTreeRenderer::getModel(const ModelItem& modelItem) { Model* model = NULL; diff --git a/interface/src/models/ModelTreeRenderer.h b/interface/src/models/ModelTreeRenderer.h index d69b85efe9..63363e4ce2 100644 --- a/interface/src/models/ModelTreeRenderer.h +++ b/interface/src/models/ModelTreeRenderer.h @@ -51,7 +51,7 @@ public: virtual void render(RenderMode renderMode = DEFAULT_RENDER_MODE); virtual const FBXGeometry* getGeometryForModel(const ModelItem& modelItem); - + virtual const Model* getModelForModelItem(const ModelItem& modelItem); /// clears the tree virtual void clear(); diff --git a/libraries/models/src/ModelTree.h b/libraries/models/src/ModelTree.h index a2a3c9cd28..41bcfcdcc3 100644 --- a/libraries/models/src/ModelTree.h +++ b/libraries/models/src/ModelTree.h @@ -15,6 +15,8 @@ #include #include "ModelTreeElement.h" +class Model; + class NewlyCreatedModelHook { public: virtual void modelCreated(const ModelItem& newModel, const SharedNodePointer& senderNode) = 0; @@ -23,6 +25,7 @@ public: class ModelItemFBXService { public: virtual const FBXGeometry* getGeometryForModel(const ModelItem& modelItem) = 0; + virtual const Model* getModelForModelItem(const ModelItem& modelItem) = 0; }; class ModelTree : public Octree { @@ -80,6 +83,7 @@ public: void processEraseMessage(const QByteArray& dataByteArray, const SharedNodePointer& sourceNode); void handleAddModelResponse(const QByteArray& packet); + ModelItemFBXService* getFBXService() const { return _fbxService; } void setFBXService(ModelItemFBXService* service) { _fbxService = service; } const FBXGeometry* getGeometryForModel(const ModelItem& modelItem) { return _fbxService ? _fbxService->getGeometryForModel(modelItem) : NULL; From 1138a3a275edbcecc3ccdbc33fa315b4c8a95a91 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 30 Jul 2014 16:49:53 -0700 Subject: [PATCH 04/17] Added referentials to AvatarData --- interface/src/avatar/MyAvatar.cpp | 5 +++++ interface/src/avatar/MyAvatar.h | 2 ++ libraries/avatars/src/AvatarData.cpp | 2 ++ libraries/avatars/src/AvatarData.h | 3 +++ libraries/avatars/src/Referential.cpp | 4 ++++ libraries/avatars/src/Referential.h | 7 ++++++- 6 files changed, 22 insertions(+), 1 deletion(-) diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 4d2d679956..9126f680b8 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -444,6 +444,11 @@ glm::vec3 MyAvatar::getRightPalmPosition() { return rightHandPosition; } +void MyAvatar::changeReferential(Referential *ref) { + delete _referential; + _referential = ref; +} + void MyAvatar::setLocalGravity(glm::vec3 gravity) { _motionBehaviors |= AVATAR_MOTION_OBEY_LOCAL_GRAVITY; // Environmental and Local gravities are incompatible. Since Local is being set here diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index 581044c522..4744ca80b8 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -149,6 +149,8 @@ public slots: glm::vec3 getLeftPalmPosition(); glm::vec3 getRightPalmPosition(); + void changeReferential(Referential* ref); + signals: void transformChanged(); diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index c3ea2f8b50..005a2abb44 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -36,6 +36,7 @@ using namespace std; AvatarData::AvatarData() : _sessionUUID(), _handPosition(0,0,0), + _referential(NULL), _bodyYaw(-90.f), _bodyPitch(0.0f), _bodyRoll(0.0f), @@ -62,6 +63,7 @@ AvatarData::AvatarData() : AvatarData::~AvatarData() { delete _headData; delete _handData; + delete _referential; } glm::vec3 AvatarData::getHandPosition() const { diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index 008aecc817..aed1047947 100755 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -49,6 +49,7 @@ typedef unsigned long long quint64; #include +#include "Referential.h" #include "HeadData.h" #include "HandData.h" @@ -283,6 +284,8 @@ protected: QUuid _sessionUUID; glm::vec3 _position; glm::vec3 _handPosition; + + Referential* _referential; // Body rotation float _bodyYaw; // degrees diff --git a/libraries/avatars/src/Referential.cpp b/libraries/avatars/src/Referential.cpp index c929e2701f..dd504485f5 100644 --- a/libraries/avatars/src/Referential.cpp +++ b/libraries/avatars/src/Referential.cpp @@ -15,4 +15,8 @@ Referential::Referential(AvatarData* avatar) : _isValid(true), _avatar(avatar) { + if (_avatar == NULL) { + _isValid = false; + return; + } } diff --git a/libraries/avatars/src/Referential.h b/libraries/avatars/src/Referential.h index d125819edc..a1a9fcd0f7 100644 --- a/libraries/avatars/src/Referential.h +++ b/libraries/avatars/src/Referential.h @@ -12,10 +12,15 @@ #ifndef hifi_Referential_h #define hifi_Referential_h -#include "AvatarData.h" +#include +#include + +class AvatarData; class Referential { public: + virtual ~Referential(); + virtual bool isValid() { return _isValid; } virtual void update() = 0; From 490dad6bbd3ffb0d1dcff97efcc841efc63a5680 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 31 Jul 2014 11:52:03 -0700 Subject: [PATCH 05/17] Work on network streaming of referentials --- libraries/avatars/src/AvatarData.cpp | 17 ++++++- libraries/avatars/src/AvatarData.h | 3 +- libraries/avatars/src/Referential.cpp | 64 ++++++++++++++++++++++++++- libraries/avatars/src/Referential.h | 26 +++++++++-- 4 files changed, 104 insertions(+), 6 deletions(-) diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index 005a2abb44..ef2ad581e0 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -137,11 +137,20 @@ QByteArray AvatarData::toByteArray() { // hand state setSemiNibbleAt(bitItems,HAND_STATE_START_BIT,_handState); // faceshift state - if (_headData->_isFaceshiftConnected) { setAtBit(bitItems, IS_FACESHIFT_CONNECTED); } + if (_headData->_isFaceshiftConnected) { + setAtBit(bitItems, IS_FACESHIFT_CONNECTED); + } if (_isChatCirclingEnabled) { setAtBit(bitItems, IS_CHAT_CIRCLING_ENABLED); } + if (_referential != NULL && _referential->isValid()) { + setAtBit(bitItems, HAS_REFERENTIAL); + } *destinationBuffer++ = bitItems; + + if (_referential != NULL && _referential->isValid()) { + destinationBuffer += _referential->packReferential(destinationBuffer); + } // If it is connected, pack up the data if (_headData->_isFaceshiftConnected) { @@ -384,6 +393,12 @@ int AvatarData::parseDataAtOffset(const QByteArray& packet, int offset) { _headData->_isFaceshiftConnected = oneAtBit(bitItems, IS_FACESHIFT_CONNECTED); _isChatCirclingEnabled = oneAtBit(bitItems, IS_CHAT_CIRCLING_ENABLED); + bool hasReferential = oneAtBit(bitItems, HAS_REFERENTIAL); + if (hasReferential) { + _referential = new Referential(sourceBuffer); + } + + if (_headData->_isFaceshiftConnected) { float leftEyeBlink, rightEyeBlink, averageLoudness, browAudioLift; minPossibleSize += sizeof(leftEyeBlink) + sizeof(rightEyeBlink) + sizeof(averageLoudness) + sizeof(browAudioLift); diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index aed1047947..029d48be34 100755 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -81,7 +81,8 @@ const quint32 AVATAR_MOTION_SCRIPTABLE_BITS = const int KEY_STATE_START_BIT = 0; // 1st and 2nd bits const int HAND_STATE_START_BIT = 2; // 3rd and 4th bits const int IS_FACESHIFT_CONNECTED = 4; // 5th bit -const int IS_CHAT_CIRCLING_ENABLED = 5; +const int IS_CHAT_CIRCLING_ENABLED = 5; // 6th bit +const int HAS_REFERENTIAL = 6; static const float MAX_AVATAR_SCALE = 1000.f; static const float MIN_AVATAR_SCALE = .005f; diff --git a/libraries/avatars/src/Referential.cpp b/libraries/avatars/src/Referential.cpp index dd504485f5..f38fb97d99 100644 --- a/libraries/avatars/src/Referential.cpp +++ b/libraries/avatars/src/Referential.cpp @@ -9,9 +9,13 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include + #include "Referential.h" -Referential::Referential(AvatarData* avatar) : +Referential::Referential(Type type, AvatarData* avatar) : + _type(type), + _createdAt(usecTimestampNow()), _isValid(true), _avatar(avatar) { @@ -20,3 +24,61 @@ Referential::Referential(AvatarData* avatar) : return; } } + +Referential::Referential(const unsigned char*& sourceBuffer) : + _isValid(false), + _avatar(NULL) +{ + sourceBuffer += unpack(sourceBuffer); +} + +Referential::~Referential() { +} + +int Referential::packReferential(unsigned char* destinationBuffer) { + const unsigned char* startPosition = destinationBuffer; + destinationBuffer += pack(destinationBuffer); + destinationBuffer += packExtraData(destinationBuffer); + return destinationBuffer - startPosition; +} + +int Referential::unpackReferential(const unsigned char* sourceBuffer) { + const unsigned char* startPosition = sourceBuffer; + sourceBuffer += unpack(sourceBuffer); + sourceBuffer += unpackExtraData(sourceBuffer); + return sourceBuffer - startPosition; +} + +int Referential::unpackExtraData(const unsigned char* sourceBuffer) { + const unsigned char* startPosition = sourceBuffer; + int size = *sourceBuffer++; + _extraDataBuffer.clear(); + _extraDataBuffer.setRawData(reinterpret_cast(sourceBuffer), size); + sourceBuffer += size; + return sourceBuffer - startPosition; +} + +int Referential::pack(unsigned char* destinationBuffer) { + unsigned char* startPosition = destinationBuffer; + *destinationBuffer++ = (unsigned char)_type; + memcpy(destinationBuffer, &_createdAt, sizeof(_createdAt)); + + destinationBuffer += packFloatVec3ToSignedTwoByteFixed(destinationBuffer, _translation, 0); + destinationBuffer += packOrientationQuatToBytes(destinationBuffer, _rotation); + destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, _scale, 0); + return destinationBuffer - startPosition; +} + +int Referential::unpack(const unsigned char* sourceBuffer) { + const unsigned char* startPosition = sourceBuffer; + _type = (Type)*sourceBuffer++; + memcpy(&_createdAt, sourceBuffer, sizeof(_createdAt)); + sourceBuffer += sizeof(_createdAt); + + sourceBuffer += unpackFloatVec3FromSignedTwoByteFixed(sourceBuffer, _translation, 0); + sourceBuffer += unpackOrientationQuatFromBytes(sourceBuffer, _rotation); + sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((int16_t*)sourceBuffer, &_scale, 0); + return sourceBuffer - startPosition; +} + + diff --git a/libraries/avatars/src/Referential.h b/libraries/avatars/src/Referential.h index a1a9fcd0f7..06ddf457e4 100644 --- a/libraries/avatars/src/Referential.h +++ b/libraries/avatars/src/Referential.h @@ -19,16 +19,36 @@ class AvatarData; class Referential { public: + enum Type { + MODEL, + JOINT, + AVATAR + }; + + Referential(const unsigned char*& sourceBuffer); virtual ~Referential(); - virtual bool isValid() { return _isValid; } - virtual void update() = 0; + Type type() { return _type; } + quint64 createdAt() { return _createdAt; } + bool isValid() { return _isValid; } + + virtual void update() {} + int packReferential(unsigned char* destinationBuffer); + int unpackReferential(const unsigned char* sourceBuffer); protected: - Referential(AvatarData* avatar); + Referential(Type type, AvatarData* avatar); + int pack(unsigned char* destinationBuffer); + int unpack(const unsigned char* sourceBuffer); + int packExtraData(unsigned char* destinationBuffer) { return 0; } + int unpackExtraData(const unsigned char* sourceBuffer); + + Type _type; + quint64 _createdAt; bool _isValid; AvatarData* _avatar; + QByteArray _extraDataBuffer; glm::vec3 _refPosition; glm::quat _refRotation; From 5fd3fd9f8f2b88436ebed51513843003244b4e63 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 31 Jul 2014 11:54:39 -0700 Subject: [PATCH 06/17] Moved MyAvatar update at the end of Application::update() to have latest data for simulation --- interface/src/Application.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 9b54b84351..188fc214c9 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2072,12 +2072,6 @@ void Application::update(float deltaTime) { _prioVR.update(deltaTime); } - { - PerformanceTimer perfTimer("myAvatar"); - updateMyAvatarLookAtPosition(); - updateMyAvatar(deltaTime); // Sample hardware, update view frustum if needed, and send avatar data to mixer/nodes - } - // Dispatch input events _controllerScriptingInterface.updateInputControllers(); @@ -2109,6 +2103,12 @@ void Application::update(float deltaTime) { PerformanceTimer perfTimer("overlays"); _overlays.update(deltaTime); } + + { + PerformanceTimer perfTimer("myAvatar"); + updateMyAvatarLookAtPosition(); + updateMyAvatar(deltaTime); // Sample hardware, update view frustum if needed, and send avatar data to mixer/nodes + } { PerformanceTimer perfTimer("emitSimulating"); From c65dc3fb1efd05471830d5babfdea1712f5058bf Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 31 Jul 2014 11:55:28 -0700 Subject: [PATCH 07/17] Various tweaks to ModelReferentials --- interface/src/avatar/ModelReferential.cpp | 61 +++++++++++++++++++---- interface/src/avatar/ModelReferential.h | 6 ++- interface/src/avatar/MyAvatar.cpp | 13 +++++ 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/interface/src/avatar/ModelReferential.cpp b/interface/src/avatar/ModelReferential.cpp index c681bb29da..bb1d66d730 100644 --- a/interface/src/avatar/ModelReferential.cpp +++ b/interface/src/avatar/ModelReferential.cpp @@ -12,23 +12,25 @@ #include #include "ModelTree.h" +#include "../renderer/Model.h" #include "ModelReferential.h" ModelReferential::ModelReferential(uint32_t modelID, ModelTree* tree, AvatarData* avatar) : -Referential(avatar), +Referential(MODEL, avatar), _modelID(modelID), _tree(tree) { const ModelItem* item = _tree->findModelByID(_modelID); if (!_isValid || item == NULL) { + qDebug() << "Not Validd"; _isValid = false; return; } _refScale = item->getRadius(); _refRotation = item->getModelRotation(); - _refPosition = item->getPosition(); + _refPosition = item->getPosition() * (float)TREE_SCALE; glm::quat refInvRot = glm::inverse(_refRotation); _scale = _avatar->getTargetScale() / _refScale; @@ -39,6 +41,7 @@ _tree(tree) void ModelReferential::update() { const ModelItem* item = _tree->findModelByID(_modelID); if (!_isValid || item == NULL || _avatar == NULL) { + qDebug() << "Not Valid"; _isValid = false; return; } @@ -51,36 +54,71 @@ void ModelReferential::update() { } if (item->getModelRotation() != _refRotation) { _refRotation = item->getModelRotation(); - _avatar->setOrientation(_refRotation * _rotation); + _avatar->setOrientation(_refRotation);// * _rotation); somethingChanged = true; } if (item->getPosition() != _refPosition || somethingChanged) { _refPosition = item->getPosition(); - _avatar->setPosition(_refPosition + _refRotation * (_translation * _refScale)); + _avatar->setPosition(_refPosition * (float)TREE_SCALE);// + _refRotation * (_translation * _refScale)); + //qDebug() << "Ref: " << item->getLastUpdated() << " " << item->getLastEdited(); somethingChanged = true; } } -JointReferential::JointReferential(uint32_t jointID, uint32_t modelID, ModelTree* tree, AvatarData* avatar) : +int ModelReferential::packReferential(unsigned char* destinationBuffer) { + int size = packPosition(destinationBuffer); + memcpy(destinationBuffer, &_modelID, sizeof(_modelID)); + return size + sizeof(_modelID); +} + +JointReferential::JointReferential(uint32_t jointIndex, uint32_t modelID, ModelTree* tree, AvatarData* avatar) : ModelReferential(modelID, tree, avatar), - _jointID(jointID) + _jointIndex(jointIndex) { - const Model* model = getModel(_tree->findModelByID(_modelID)); - - if (!_isValid || model == NULL || model->getJointStateCount() <= jointID) { + _type = JOINT; + const ModelItem* item = _tree->findModelByID(_modelID); + const Model* model = getModel(item); + if (!_isValid || model == NULL || _jointIndex >= model->getJointStateCount()) { + qDebug() << "Not Validd"; _isValid = false; return; } + + _refScale = item->getRadius(); + model->getJointRotationInWorldFrame(_jointIndex, _refRotation); + model->getJointPositionInWorldFrame(_jointIndex, _refPosition); + + glm::quat refInvRot = glm::inverse(_refRotation); + _scale = _avatar->getTargetScale() / _refScale; + _rotation = refInvRot * _avatar->getOrientation(); + _translation = refInvRot * (avatar->getPosition() - _refPosition) / _refScale; } void JointReferential::update() { const ModelItem* item = _tree->findModelByID(_modelID); - if (!_isValid || item == NULL) { + const Model* model = getModel(item); + if (!_isValid || model == NULL || _jointIndex >= model->getJointStateCount()) { + qDebug() << "Not Valid"; _isValid = false; return; } - + bool somethingChanged = false; + if (item->getRadius() != _refScale) { + _refScale = item->getRadius(); + _avatar->setTargetScale(_refScale * _scale); + somethingChanged = true; + } + if (item->getModelRotation() != _refRotation) { + model->getJointRotationInWorldFrame(_jointIndex, _refRotation); + _avatar->setOrientation(_refRotation * _rotation); + somethingChanged = true; + } + if (item->getPosition() != _refPosition || somethingChanged) { + model->getJointPositionInWorldFrame(_jointIndex, _refPosition); + _avatar->setPosition(_refPosition + _refRotation * (_translation * _refScale)); + somethingChanged = true; + } } const Model* JointReferential::getModel(const ModelItem* item) { @@ -88,6 +126,7 @@ const Model* JointReferential::getModel(const ModelItem* item) { if (item != NULL && fbxService != NULL) { return fbxService->getModelForModelItem(*item); } + qDebug() << "No Model"; return NULL; } diff --git a/interface/src/avatar/ModelReferential.h b/interface/src/avatar/ModelReferential.h index 8f9333451a..5b0c92c767 100644 --- a/interface/src/avatar/ModelReferential.h +++ b/interface/src/avatar/ModelReferential.h @@ -19,6 +19,7 @@ class Model; class ModelReferential : public Referential { public: + ModelReferential(Referential* ref); ModelReferential(uint32_t modelID, ModelTree* tree, AvatarData* avatar); virtual void update(); @@ -29,13 +30,14 @@ protected: class JointReferential : public ModelReferential { public: - JointReferential(uint32_t jointID, uint32_t modelID, ModelTree* tree, AvatarData* avatar); + JointReferential(Referential* ref); + JointReferential(uint32_t jointIndex, uint32_t modelID, ModelTree* tree, AvatarData* avatar); virtual void update(); protected: const Model* getModel(const ModelItem* item); - uint32_t _jointID; + uint32_t _jointIndex; }; #endif // hifi_ModelReferential_h \ No newline at end of file diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 9126f680b8..92f0a82cd9 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -32,6 +32,7 @@ #include "Audio.h" #include "Environment.h" #include "Menu.h" +#include "ModelReferential.h" #include "MyAvatar.h" #include "Physics.h" #include "devices/Faceshift.h" @@ -126,6 +127,18 @@ void MyAvatar::update(float deltaTime) { } simulate(deltaTime); + + + const ModelItem* item = Application::getInstance()->getModels()->getTree()->findModelByID(278); + 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(278, Application::getInstance()->getModels()->getTree(), this); + } + } } void MyAvatar::simulate(float deltaTime) { From d9dde06c146496752e07d499fbd9ebdafb4c397e Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 4 Aug 2014 14:59:15 -0700 Subject: [PATCH 08/17] Various tweaks to referentials --- interface/src/Application.cpp | 2 +- interface/src/avatar/Avatar.cpp | 22 +++++++++++++ interface/src/avatar/ModelReferential.cpp | 22 +++++++++---- interface/src/avatar/ModelReferential.h | 5 ++- interface/src/avatar/MyAvatar.cpp | 21 ++++++------ libraries/avatars/src/AvatarData.cpp | 35 ++++++++++++++++---- libraries/avatars/src/AvatarData.h | 4 +-- libraries/avatars/src/Referential.cpp | 39 +++++++++++++++-------- libraries/avatars/src/Referential.h | 9 ++++-- libraries/shared/src/SharedUtil.cpp | 8 ++--- libraries/shared/src/SharedUtil.h | 2 +- 11 files changed, 121 insertions(+), 48 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 6ae2452c41..1ae29fcd88 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() method will get the correct details from the camera + // loadViewFrumstum() methodill 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 27c4e39062..21d5e65a15 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -27,6 +27,7 @@ #include "Hand.h" #include "Head.h" #include "Menu.h" +#include "ModelReferential.h" #include "Physics.h" #include "world.h" #include "devices/OculusManager.h" @@ -179,6 +180,21 @@ void Avatar::simulate(float deltaTime) { } _displayNameAlpha = abs(_displayNameAlpha - _displayNameTargetAlpha) < 0.01f ? _displayNameTargetAlpha : _displayNameAlpha; } + if (_referential) { + if (_referential->hasExtraData()) { + switch (_referential->type()) { + case Referential::MODEL: + qDebug() << "[DEBUG] Switching to the right referential"; + _referential = new ModelReferential(_referential, this); + break; + default: + qDebug() << "Non handled referential type"; + break; + } + } + + _referential->update(); + } } void Avatar::updateAcceleration(float deltaTime) { @@ -440,6 +456,12 @@ void Avatar::renderBody(RenderMode renderMode, float glowLevel) { return; } + // make sure we have the right position + _skeletonModel.setTranslation(getPosition()); + static const glm::quat refOrientation = glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f)); + _skeletonModel.setRotation(getOrientation() * refOrientation); + const float MODEL_SCALE = 0.0006f; + _skeletonModel.setScale(glm::vec3(1.0f, 1.0f, 1.0f) * getScale() * MODEL_SCALE); _skeletonModel.render(1.0f, modelRenderMode, Menu::getInstance()->isOptionChecked(MenuOption::AvatarsReceiveShadows)); renderAttachments(renderMode); getHand()->render(false, modelRenderMode); diff --git a/interface/src/avatar/ModelReferential.cpp b/interface/src/avatar/ModelReferential.cpp index bb1d66d730..85533603f2 100644 --- a/interface/src/avatar/ModelReferential.cpp +++ b/interface/src/avatar/ModelReferential.cpp @@ -16,6 +16,10 @@ #include "ModelReferential.h" +ModelReferential::ModelReferential(Referential* referential, AvatarData* avatar) : Referential(MODEL, avatar) { + +} + ModelReferential::ModelReferential(uint32_t modelID, ModelTree* tree, AvatarData* avatar) : Referential(MODEL, avatar), _modelID(modelID), @@ -40,7 +44,7 @@ _tree(tree) void ModelReferential::update() { const ModelItem* item = _tree->findModelByID(_modelID); - if (!_isValid || item == NULL || _avatar == NULL) { + if (item == NULL || _avatar == NULL) { qDebug() << "Not Valid"; _isValid = false; return; @@ -54,21 +58,25 @@ void ModelReferential::update() { } if (item->getModelRotation() != _refRotation) { _refRotation = item->getModelRotation(); - _avatar->setOrientation(_refRotation);// * _rotation); + _avatar->setOrientation(_refRotation * _rotation); somethingChanged = true; } if (item->getPosition() != _refPosition || somethingChanged) { _refPosition = item->getPosition(); - _avatar->setPosition(_refPosition * (float)TREE_SCALE);// + _refRotation * (_translation * _refScale)); + _avatar->setPosition(_refPosition * (float)TREE_SCALE + _refRotation * (_translation * _refScale)); //qDebug() << "Ref: " << item->getLastUpdated() << " " << item->getLastEdited(); somethingChanged = true; } } -int ModelReferential::packReferential(unsigned char* destinationBuffer) { - int size = packPosition(destinationBuffer); +int ModelReferential::packExtraData(unsigned char* destinationBuffer) { memcpy(destinationBuffer, &_modelID, sizeof(_modelID)); - return size + sizeof(_modelID); + return sizeof(_modelID); +} + +int ModelReferential::unpackExtraData(const unsigned char *sourceBuffer) { + memcpy(&_modelID, sourceBuffer, sizeof(_modelID)); + return sizeof(_modelID); } JointReferential::JointReferential(uint32_t jointIndex, uint32_t modelID, ModelTree* tree, AvatarData* avatar) : @@ -97,7 +105,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 (!_isValid || model == NULL || _jointIndex >= model->getJointStateCount()) { + if (model == NULL || _jointIndex >= model->getJointStateCount()) { qDebug() << "Not Valid"; _isValid = false; return; diff --git a/interface/src/avatar/ModelReferential.h b/interface/src/avatar/ModelReferential.h index 5b0c92c767..e87a09594d 100644 --- a/interface/src/avatar/ModelReferential.h +++ b/interface/src/avatar/ModelReferential.h @@ -19,11 +19,14 @@ class Model; class ModelReferential : public Referential { public: - ModelReferential(Referential* ref); + ModelReferential(Referential* ref, AvatarData* avatar); ModelReferential(uint32_t modelID, ModelTree* tree, AvatarData* avatar); virtual void update(); protected: + virtual int packExtraData(unsigned char* destinationBuffer); + virtual int unpackExtraData(const unsigned char* sourceBuffer); + uint32_t _modelID; ModelTree* _tree; }; diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 92f0a82cd9..3428f73ee7 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -128,15 +128,18 @@ void MyAvatar::update(float deltaTime) { simulate(deltaTime); - - const ModelItem* item = Application::getInstance()->getModels()->getTree()->findModelByID(278); - 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(278, Application::getInstance()->getModels()->getTree(), this); + bool WANT_REFERENTIAL = true; + 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); + } } } } diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index ef2ad581e0..67977996b8 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -66,6 +66,13 @@ AvatarData::~AvatarData() { delete _referential; } +const glm::vec3& AvatarData::getPosition() { + if (_referential) { + _referential->update(); + } + return _position; +} + glm::vec3 AvatarData::getHandPosition() const { return getOrientation() * _handPosition + _position; } @@ -148,6 +155,7 @@ QByteArray AvatarData::toByteArray() { } *destinationBuffer++ = bitItems; + // Add referential if (_referential != NULL && _referential->isValid()) { destinationBuffer += _referential->packReferential(destinationBuffer); } @@ -381,21 +389,36 @@ int AvatarData::parseDataAtOffset(const QByteArray& packet, int offset) { } // 1 + chatMessageSize bytes { // bitFlags and face data - unsigned char bitItems = 0; - bitItems = (unsigned char)*sourceBuffer++; - + unsigned char bitItems = *sourceBuffer++; + // key state, stored as a semi-nibble in the bitItems _keyState = (KeyState)getSemiNibbleAt(bitItems,KEY_STATE_START_BIT); - // hand state, stored as a semi-nibble in the bitItems _handState = getSemiNibbleAt(bitItems,HAND_STATE_START_BIT); _headData->_isFaceshiftConnected = oneAtBit(bitItems, IS_FACESHIFT_CONNECTED); _isChatCirclingEnabled = oneAtBit(bitItems, IS_CHAT_CIRCLING_ENABLED); - bool hasReferential = oneAtBit(bitItems, HAS_REFERENTIAL); + + // Referential if (hasReferential) { - _referential = new Referential(sourceBuffer); + qDebug() << "Has referencial: " << hasReferential; + if (_referential == NULL) { + _referential = new Referential(sourceBuffer, this); + } else { + Referential* ref = new Referential(sourceBuffer, this); + if (ref->createdAt() > _referential->createdAt()) { + qDebug() << "Replacing referential"; + delete _referential; + _referential = ref; + } else { + delete ref; + } + } + } else if (_referential != NULL) { + qDebug() << "Erasing referencial"; + delete _referential; + _referential = NULL; } diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index 5462750ed4..c32faba977 100755 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -82,7 +82,7 @@ const int KEY_STATE_START_BIT = 0; // 1st and 2nd bits const int HAND_STATE_START_BIT = 2; // 3rd and 4th bits const int IS_FACESHIFT_CONNECTED = 4; // 5th bit const int IS_CHAT_CIRCLING_ENABLED = 5; // 6th bit -const int HAS_REFERENTIAL = 6; +const int HAS_REFERENTIAL = 6; // 7th bit static const float MAX_AVATAR_SCALE = 1000.f; static const float MIN_AVATAR_SCALE = .005f; @@ -143,7 +143,7 @@ public: const QUuid& getSessionUUID() { return _sessionUUID; } - const glm::vec3& getPosition() const { return _position; } + const glm::vec3& getPosition(); void setPosition(const glm::vec3 position) { _position = position; } glm::vec3 getHandPosition() const; diff --git a/libraries/avatars/src/Referential.cpp b/libraries/avatars/src/Referential.cpp index f38fb97d99..91c3050e2f 100644 --- a/libraries/avatars/src/Referential.cpp +++ b/libraries/avatars/src/Referential.cpp @@ -23,11 +23,12 @@ Referential::Referential(Type type, AvatarData* avatar) : _isValid = false; return; } + qDebug() << "[DEBUG] New Referential"; } -Referential::Referential(const unsigned char*& sourceBuffer) : +Referential::Referential(const unsigned char*& sourceBuffer, AvatarData* avatar) : _isValid(false), - _avatar(NULL) + _avatar(avatar) { sourceBuffer += unpack(sourceBuffer); } @@ -38,23 +39,24 @@ Referential::~Referential() { int Referential::packReferential(unsigned char* destinationBuffer) { const unsigned char* startPosition = destinationBuffer; destinationBuffer += pack(destinationBuffer); - destinationBuffer += packExtraData(destinationBuffer); + + unsigned char* sizePosition = destinationBuffer++; + char size = packExtraData(destinationBuffer); + *sizePosition = size; + destinationBuffer += size; + return destinationBuffer - startPosition; } int Referential::unpackReferential(const unsigned char* sourceBuffer) { const unsigned char* startPosition = sourceBuffer; sourceBuffer += unpack(sourceBuffer); - sourceBuffer += unpackExtraData(sourceBuffer); - return sourceBuffer - startPosition; -} - -int Referential::unpackExtraData(const unsigned char* sourceBuffer) { - const unsigned char* startPosition = sourceBuffer; - int size = *sourceBuffer++; - _extraDataBuffer.clear(); - _extraDataBuffer.setRawData(reinterpret_cast(sourceBuffer), size); - sourceBuffer += size; + + char expectedSize = *sourceBuffer++; + char bytesRead = unpackExtraData(sourceBuffer); + _isValid = (bytesRead == expectedSize); + sourceBuffer += expectedSize; + return sourceBuffer - startPosition; } @@ -77,8 +79,17 @@ int Referential::unpack(const unsigned char* sourceBuffer) { sourceBuffer += unpackFloatVec3FromSignedTwoByteFixed(sourceBuffer, _translation, 0); sourceBuffer += unpackOrientationQuatFromBytes(sourceBuffer, _rotation); - sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((int16_t*)sourceBuffer, &_scale, 0); + sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((const int16_t*) sourceBuffer, &_scale, 0); return sourceBuffer - startPosition; } +int Referential::unpackExtraData(const unsigned char* sourceBuffer) { + const unsigned char* startPosition = sourceBuffer; + int size = *sourceBuffer; + _extraDataBuffer.clear(); + _extraDataBuffer.setRawData(reinterpret_cast(sourceBuffer), size + 1); + sourceBuffer += size + 1; + return sourceBuffer - startPosition; +} + diff --git a/libraries/avatars/src/Referential.h b/libraries/avatars/src/Referential.h index 06ddf457e4..b7e3a248e8 100644 --- a/libraries/avatars/src/Referential.h +++ b/libraries/avatars/src/Referential.h @@ -25,12 +25,13 @@ public: AVATAR }; - Referential(const unsigned char*& sourceBuffer); + Referential(const unsigned char*& sourceBuffer, AvatarData* avatar); virtual ~Referential(); Type type() { return _type; } quint64 createdAt() { return _createdAt; } bool isValid() { return _isValid; } + bool hasExtraData() { return !_extraDataBuffer.isEmpty(); } virtual void update() {} int packReferential(unsigned char* destinationBuffer); @@ -39,10 +40,12 @@ public: protected: Referential(Type type, AvatarData* avatar); + // packs the base class data int pack(unsigned char* destinationBuffer); int unpack(const unsigned char* sourceBuffer); - int packExtraData(unsigned char* destinationBuffer) { return 0; } - int unpackExtraData(const unsigned char* sourceBuffer); + // virtual functions that pack fthe extra data of subclasses (needs to be reimplemented in subclass) + virtual int packExtraData(unsigned char* destinationBuffer) { return 0; } + virtual int unpackExtraData(const unsigned char* sourceBuffer); Type _type; quint64 _createdAt; diff --git a/libraries/shared/src/SharedUtil.cpp b/libraries/shared/src/SharedUtil.cpp index 57d1d7faac..2b8b9929e5 100644 --- a/libraries/shared/src/SharedUtil.cpp +++ b/libraries/shared/src/SharedUtil.cpp @@ -167,7 +167,7 @@ bool oneAtBit(unsigned char byte, int bitIndex) { } void setAtBit(unsigned char& byte, int bitIndex) { - byte += (1 << (7 - bitIndex)); + byte |= (1 << (7 - bitIndex)); } void clearAtBit(unsigned char& byte, int bitIndex) { @@ -176,7 +176,7 @@ void clearAtBit(unsigned char& byte, int bitIndex) { } } -int getSemiNibbleAt(unsigned char& byte, int bitIndex) { +int getSemiNibbleAt(unsigned char byte, int bitIndex) { return (byte >> (6 - bitIndex) & 3); // semi-nibbles store 00, 01, 10, or 11 } @@ -207,7 +207,7 @@ bool isBetween(int64_t value, int64_t max, int64_t min) { void setSemiNibbleAt(unsigned char& byte, int bitIndex, int value) { //assert(value <= 3 && value >= 0); - byte += ((value & 3) << (6 - bitIndex)); // semi-nibbles store 00, 01, 10, or 11 + byte |= ((value & 3) << (6 - bitIndex)); // semi-nibbles store 00, 01, 10, or 11 } bool isInEnvironment(const char* environment) { @@ -496,7 +496,7 @@ int packFloatScalarToSignedTwoByteFixed(unsigned char* buffer, float scalar, int return sizeof(uint16_t); } -int unpackFloatScalarFromSignedTwoByteFixed(int16_t* byteFixedPointer, float* destinationPointer, int radix) { +int unpackFloatScalarFromSignedTwoByteFixed(const int16_t* byteFixedPointer, float* destinationPointer, int radix) { *destinationPointer = *byteFixedPointer / (float)(1 << radix); return sizeof(int16_t); } diff --git a/libraries/shared/src/SharedUtil.h b/libraries/shared/src/SharedUtil.h index 6bb39f7e12..18494d48e4 100644 --- a/libraries/shared/src/SharedUtil.h +++ b/libraries/shared/src/SharedUtil.h @@ -82,7 +82,7 @@ int numberOfOnes(unsigned char byte); bool oneAtBit(unsigned char byte, int bitIndex); void setAtBit(unsigned char& byte, int bitIndex); void clearAtBit(unsigned char& byte, int bitIndex); -int getSemiNibbleAt(unsigned char& byte, int bitIndex); +int getSemiNibbleAt(unsigned char byte, int bitIndex); void setSemiNibbleAt(unsigned char& byte, int bitIndex, int value); int getNthBit(unsigned char byte, int ordinal); /// determines the bit placement 0-7 of the ordinal set bit From f0af2f022e099f347a146cf215582f079b0a03f0 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 4 Aug 2014 17:52:20 -0700 Subject: [PATCH 09/17] Pack/Unpack asymetry fix + added forgotten consts --- interface/src/avatar/ModelReferential.cpp | 4 ++-- interface/src/avatar/ModelReferential.h | 4 ++-- libraries/avatars/src/Referential.cpp | 27 ++++++++++++++--------- libraries/avatars/src/Referential.h | 16 +++++++------- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/interface/src/avatar/ModelReferential.cpp b/interface/src/avatar/ModelReferential.cpp index 85533603f2..8f1360a705 100644 --- a/interface/src/avatar/ModelReferential.cpp +++ b/interface/src/avatar/ModelReferential.cpp @@ -69,12 +69,12 @@ void ModelReferential::update() { } } -int ModelReferential::packExtraData(unsigned char* destinationBuffer) { +int ModelReferential::packExtraData(unsigned char* destinationBuffer) const { memcpy(destinationBuffer, &_modelID, sizeof(_modelID)); return sizeof(_modelID); } -int ModelReferential::unpackExtraData(const unsigned char *sourceBuffer) { +int ModelReferential::unpackExtraData(const unsigned char *sourceBuffer, int size) { memcpy(&_modelID, sourceBuffer, sizeof(_modelID)); return sizeof(_modelID); } diff --git a/interface/src/avatar/ModelReferential.h b/interface/src/avatar/ModelReferential.h index e87a09594d..806e4edfcb 100644 --- a/interface/src/avatar/ModelReferential.h +++ b/interface/src/avatar/ModelReferential.h @@ -24,8 +24,8 @@ public: virtual void update(); protected: - virtual int packExtraData(unsigned char* destinationBuffer); - virtual int unpackExtraData(const unsigned char* sourceBuffer); + virtual int packExtraData(unsigned char* destinationBuffer) const; + virtual int unpackExtraData(const unsigned char* sourceBuffer, int size); uint32_t _modelID; ModelTree* _tree; diff --git a/libraries/avatars/src/Referential.cpp b/libraries/avatars/src/Referential.cpp index 91c3050e2f..12a0f59186 100644 --- a/libraries/avatars/src/Referential.cpp +++ b/libraries/avatars/src/Referential.cpp @@ -36,7 +36,7 @@ Referential::Referential(const unsigned char*& sourceBuffer, AvatarData* avatar) Referential::~Referential() { } -int Referential::packReferential(unsigned char* destinationBuffer) { +int Referential::packReferential(unsigned char* destinationBuffer) const { const unsigned char* startPosition = destinationBuffer; destinationBuffer += pack(destinationBuffer); @@ -53,17 +53,21 @@ int Referential::unpackReferential(const unsigned char* sourceBuffer) { sourceBuffer += unpack(sourceBuffer); char expectedSize = *sourceBuffer++; - char bytesRead = unpackExtraData(sourceBuffer); + char bytesRead = unpackExtraData(sourceBuffer, expectedSize); _isValid = (bytesRead == expectedSize); + if (!_isValid) { + qDebug() << "[ERROR] Referential extra data overflow"; + } sourceBuffer += expectedSize; return sourceBuffer - startPosition; } -int Referential::pack(unsigned char* destinationBuffer) { +int Referential::pack(unsigned char* destinationBuffer) const { unsigned char* startPosition = destinationBuffer; *destinationBuffer++ = (unsigned char)_type; memcpy(destinationBuffer, &_createdAt, sizeof(_createdAt)); + destinationBuffer += sizeof(_createdAt); destinationBuffer += packFloatVec3ToSignedTwoByteFixed(destinationBuffer, _translation, 0); destinationBuffer += packOrientationQuatToBytes(destinationBuffer, _rotation); @@ -83,13 +87,14 @@ int Referential::unpack(const unsigned char* sourceBuffer) { return sourceBuffer - startPosition; } - -int Referential::unpackExtraData(const unsigned char* sourceBuffer) { - const unsigned char* startPosition = sourceBuffer; - int size = *sourceBuffer; - _extraDataBuffer.clear(); - _extraDataBuffer.setRawData(reinterpret_cast(sourceBuffer), size + 1); - sourceBuffer += size + 1; - return sourceBuffer - startPosition; +int Referential::packExtraData(unsigned char *destinationBuffer) const { + 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); + return size; } diff --git a/libraries/avatars/src/Referential.h b/libraries/avatars/src/Referential.h index b7e3a248e8..713b6a028f 100644 --- a/libraries/avatars/src/Referential.h +++ b/libraries/avatars/src/Referential.h @@ -28,24 +28,24 @@ public: Referential(const unsigned char*& sourceBuffer, AvatarData* avatar); virtual ~Referential(); - Type type() { return _type; } - quint64 createdAt() { return _createdAt; } - bool isValid() { return _isValid; } - bool hasExtraData() { return !_extraDataBuffer.isEmpty(); } + Type type() const { return _type; } + quint64 createdAt() const { return _createdAt; } + bool isValid() const { return _isValid; } + bool hasExtraData() const { return !_extraDataBuffer.isEmpty(); } virtual void update() {} - int packReferential(unsigned char* destinationBuffer); + int packReferential(unsigned char* destinationBuffer) const; int unpackReferential(const unsigned char* sourceBuffer); protected: Referential(Type type, AvatarData* avatar); // packs the base class data - int pack(unsigned char* destinationBuffer); + int pack(unsigned char* destinationBuffer) const; int unpack(const unsigned char* sourceBuffer); // virtual functions that pack fthe extra data of subclasses (needs to be reimplemented in subclass) - virtual int packExtraData(unsigned char* destinationBuffer) { return 0; } - virtual int unpackExtraData(const unsigned char* sourceBuffer); + virtual int packExtraData(unsigned char* destinationBuffer) const; + virtual int unpackExtraData(const unsigned char* sourceBuffer, int size); Type _type; quint64 _createdAt; From 9999c88d84276e869975333a5961e0b00ba3ba68 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 5 Aug 2014 12:01:47 -0700 Subject: [PATCH 10/17] Added Referential getters + fixed stupid unpacking bug --- libraries/avatars/src/Referential.cpp | 2 +- libraries/avatars/src/Referential.h | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/libraries/avatars/src/Referential.cpp b/libraries/avatars/src/Referential.cpp index 12a0f59186..722c54cbe3 100644 --- a/libraries/avatars/src/Referential.cpp +++ b/libraries/avatars/src/Referential.cpp @@ -30,7 +30,7 @@ Referential::Referential(const unsigned char*& sourceBuffer, AvatarData* avatar) _isValid(false), _avatar(avatar) { - sourceBuffer += unpack(sourceBuffer); + sourceBuffer += unpackReferential(sourceBuffer); } Referential::~Referential() { diff --git a/libraries/avatars/src/Referential.h b/libraries/avatars/src/Referential.h index 713b6a028f..9353ea0871 100644 --- a/libraries/avatars/src/Referential.h +++ b/libraries/avatars/src/Referential.h @@ -33,6 +33,11 @@ public: bool isValid() const { return _isValid; } bool hasExtraData() const { return !_extraDataBuffer.isEmpty(); } + glm::vec3 getTranslation() const { return _translation; } + glm::quat getRotation() const { return _rotation; } + float getScale() const {return _scale; } + QByteArray getExtraData() const { return _extraDataBuffer; } + virtual void update() {} int packReferential(unsigned char* destinationBuffer) const; int unpackReferential(const unsigned char* sourceBuffer); From 283beab05daab56a7aac116e708adfaa3e1369ea Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 5 Aug 2014 12:03:16 -0700 Subject: [PATCH 11/17] ModelReferential copy constructor --- interface/src/avatar/ModelReferential.cpp | 38 ++++++++++++++++------- interface/src/avatar/ModelReferential.h | 2 +- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/interface/src/avatar/ModelReferential.cpp b/interface/src/avatar/ModelReferential.cpp index 8f1360a705..fb80d7f2ef 100644 --- a/interface/src/avatar/ModelReferential.cpp +++ b/interface/src/avatar/ModelReferential.cpp @@ -16,8 +16,26 @@ #include "ModelReferential.h" -ModelReferential::ModelReferential(Referential* referential, AvatarData* avatar) : Referential(MODEL, avatar) { - +ModelReferential::ModelReferential(Referential* referential, ModelTree* tree, AvatarData* avatar) : + Referential(MODEL, avatar), + _tree(tree) { + unpackExtraData(reinterpret_cast(referential->getExtraData().data()), + referential->getExtraData().size()); + if (!isValid()) { + qDebug() << "ModelReferential::copyConstructo(): Not Valid"; + return; + } + + const ModelItem* item = _tree->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(); } ModelReferential::ModelReferential(uint32_t modelID, ModelTree* tree, AvatarData* avatar) : @@ -26,8 +44,8 @@ _modelID(modelID), _tree(tree) { const ModelItem* item = _tree->findModelByID(_modelID); - if (!_isValid || item == NULL) { - qDebug() << "Not Validd"; + if (!isValid() || item == NULL) { + qDebug() << "ModelReferential::constructor(): Not Valid"; _isValid = false; return; } @@ -44,9 +62,8 @@ _tree(tree) void ModelReferential::update() { const ModelItem* item = _tree->findModelByID(_modelID); - if (item == NULL || _avatar == NULL) { - qDebug() << "Not Valid"; - _isValid = false; + if (!isValid() || item == NULL || _avatar == NULL) { + //qDebug() << "ModelReferential::update(): Not Valid"; return; } @@ -58,13 +75,12 @@ void ModelReferential::update() { } if (item->getModelRotation() != _refRotation) { _refRotation = item->getModelRotation(); - _avatar->setOrientation(_refRotation * _rotation); + _avatar->setOrientation(_refRotation * _rotation, true); somethingChanged = true; } if (item->getPosition() != _refPosition || somethingChanged) { _refPosition = item->getPosition(); - _avatar->setPosition(_refPosition * (float)TREE_SCALE + _refRotation * (_translation * _refScale)); - //qDebug() << "Ref: " << item->getLastUpdated() << " " << item->getLastEdited(); + _avatar->setPosition(_refPosition * (float)TREE_SCALE + _refRotation * (_translation * _refScale), true); somethingChanged = true; } } @@ -87,7 +103,7 @@ JointReferential::JointReferential(uint32_t jointIndex, uint32_t modelID, ModelT const ModelItem* item = _tree->findModelByID(_modelID); const Model* model = getModel(item); if (!_isValid || model == NULL || _jointIndex >= model->getJointStateCount()) { - qDebug() << "Not Validd"; + qDebug() << "Not Valid"; _isValid = false; return; } diff --git a/interface/src/avatar/ModelReferential.h b/interface/src/avatar/ModelReferential.h index 806e4edfcb..53170bcf41 100644 --- a/interface/src/avatar/ModelReferential.h +++ b/interface/src/avatar/ModelReferential.h @@ -19,7 +19,7 @@ class Model; class ModelReferential : public Referential { public: - ModelReferential(Referential* ref, AvatarData* avatar); + ModelReferential(Referential* ref, ModelTree* tree, AvatarData* avatar); ModelReferential(uint32_t modelID, ModelTree* tree, AvatarData* avatar); virtual void update(); From 39a74cbc288500ff1087b8b7b4d3c3a2986337e3 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 5 Aug 2014 12:03:53 -0700 Subject: [PATCH 12/17] Few modifications to how avatars update their positions --- interface/src/avatar/Avatar.cpp | 16 +++++++------ interface/src/avatar/MyAvatar.cpp | 5 ++-- libraries/avatars/src/AvatarData.cpp | 35 +++++++++++++++++++++------- libraries/avatars/src/AvatarData.h | 6 ++--- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index 21d5e65a15..d4df5b21f0 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -182,10 +182,12 @@ void Avatar::simulate(float deltaTime) { } if (_referential) { if (_referential->hasExtraData()) { + qDebug() << "Has extra data"; switch (_referential->type()) { case Referential::MODEL: qDebug() << "[DEBUG] Switching to the right referential"; - _referential = new ModelReferential(_referential, this); + _referential = new ModelReferential(_referential, + Application::getInstance()->getModels()->getTree(), this); break; default: qDebug() << "Non handled referential type"; @@ -234,6 +236,12 @@ static TextRenderer* textRenderer(TextRendererType type) { } void Avatar::render(const glm::vec3& cameraPosition, RenderMode renderMode) { + // make sure we have the right position + _skeletonModel.setTranslation(getPosition()); + static const glm::quat refOrientation = glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f)); + _skeletonModel.setRotation(getOrientation() * refOrientation); + const float MODEL_SCALE = 0.0006f; + _skeletonModel.setScale(glm::vec3(1.0f, 1.0f, 1.0f) * getScale() * MODEL_SCALE); if (glm::distance(Application::getInstance()->getAvatar()->getPosition(), _position) < 10.0f) { @@ -456,12 +464,6 @@ void Avatar::renderBody(RenderMode renderMode, float glowLevel) { return; } - // make sure we have the right position - _skeletonModel.setTranslation(getPosition()); - static const glm::quat refOrientation = glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f)); - _skeletonModel.setRotation(getOrientation() * refOrientation); - const float MODEL_SCALE = 0.0006f; - _skeletonModel.setScale(glm::vec3(1.0f, 1.0f, 1.0f) * getScale() * MODEL_SCALE); _skeletonModel.render(1.0f, modelRenderMode, Menu::getInstance()->isOptionChecked(MenuOption::AvatarsReceiveShadows)); renderAttachments(renderMode); getHand()->render(false, modelRenderMode); diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 3428f73ee7..7c899fcc0a 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -128,7 +128,7 @@ void MyAvatar::update(float deltaTime) { simulate(deltaTime); - bool WANT_REFERENTIAL = true; + bool WANT_REFERENTIAL = false; if (WANT_REFERENTIAL) { int id = 12340; const ModelItem* item = Application::getInstance()->getModels()->getTree()->findModelByID(id); @@ -138,7 +138,8 @@ void MyAvatar::update(float deltaTime) { } else if (item != NULL) { const Model* model = Application::getInstance()->getModels()->getModelForModelItem(*item); if (model != NULL) { - _referential = new ModelReferential(id, Application::getInstance()->getModels()->getTree(), this); + _referential = new ModelReferential(id, + Application::getInstance()->getModels()->getTree(), this); } } } diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index 67977996b8..1bcc46639e 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -73,6 +73,29 @@ const glm::vec3& AvatarData::getPosition() { return _position; } +void AvatarData::setPosition(const glm::vec3 position, bool overideReferential) { + if (!_referential || overideReferential) { + _position = position; + } +} + +glm::quat AvatarData::getOrientation() const { + if (_referential) { + _referential->update(); + } + + return glm::quat(glm::radians(glm::vec3(_bodyPitch, _bodyYaw, _bodyRoll))); +} + +void AvatarData::setOrientation(const glm::quat& orientation, bool overideReferential) { + if (!_referential || overideReferential) { + glm::vec3 eulerAngles = glm::degrees(safeEulerAngles(orientation)); + _bodyPitch = eulerAngles.x; + _bodyYaw = eulerAngles.y; + _bodyRoll = eulerAngles.z; + } +} + glm::vec3 AvatarData::getHandPosition() const { return getOrientation() * _handPosition + _position; } @@ -402,8 +425,9 @@ int AvatarData::parseDataAtOffset(const QByteArray& packet, int offset) { // Referential if (hasReferential) { - qDebug() << "Has referencial: " << hasReferential; + const unsigned char* start = sourceBuffer; if (_referential == NULL) { + qDebug() << "New referential"; _referential = new Referential(sourceBuffer, this); } else { Referential* ref = new Referential(sourceBuffer, this); @@ -415,6 +439,8 @@ int AvatarData::parseDataAtOffset(const QByteArray& packet, int offset) { delete ref; } } + //qDebug() << "Read " << sourceBuffer - start << " bytes."; + _referential->update(); } else if (_referential != NULL) { qDebug() << "Erasing referencial"; delete _referential; @@ -851,13 +877,6 @@ void AvatarData::setClampedTargetScale(float targetScale) { qDebug() << "Changed scale to " << _targetScale; } -void AvatarData::setOrientation(const glm::quat& orientation) { - glm::vec3 eulerAngles = glm::degrees(safeEulerAngles(orientation)); - _bodyPitch = eulerAngles.x; - _bodyYaw = eulerAngles.y; - _bodyRoll = eulerAngles.z; -} - void AvatarData::sendIdentityPacket() { QByteArray identityPacket = byteArrayWithPopulatedHeader(PacketTypeAvatarIdentity); identityPacket.append(identityByteArray()); diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index c32faba977..a0aeb94670 100755 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -144,7 +144,7 @@ public: const QUuid& getSessionUUID() { return _sessionUUID; } const glm::vec3& getPosition(); - void setPosition(const glm::vec3 position) { _position = position; } + void setPosition(const glm::vec3 position, bool overideReferential = false); glm::vec3 getHandPosition() const; void setHandPosition(const glm::vec3& handPosition); @@ -167,8 +167,8 @@ public: float getBodyRoll() const { return _bodyRoll; } void setBodyRoll(float bodyRoll) { _bodyRoll = bodyRoll; } - glm::quat getOrientation() const { return glm::quat(glm::radians(glm::vec3(_bodyPitch, _bodyYaw, _bodyRoll))); } - void setOrientation(const glm::quat& orientation); + glm::quat getOrientation() const; + void setOrientation(const glm::quat& orientation, bool overideReferential = false); glm::quat getHeadOrientation() const { return _headData->getOrientation(); } void setHeadOrientation(const glm::quat& orientation) { _headData->setOrientation(orientation); } From b360b6c55ddf2ee5822e9a3e7d6894126f6db3ab Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 5 Aug 2014 17:07:03 -0700 Subject: [PATCH 13/17] 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(); From 2e7d34a8862988153240035e097887517bfb817d Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 5 Aug 2014 17:08:23 -0700 Subject: [PATCH 14/17] typing error --- interface/src/avatar/ModelReferential.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/interface/src/avatar/ModelReferential.cpp b/interface/src/avatar/ModelReferential.cpp index 88a02a909b..6cf22a11ce 100644 --- a/interface/src/avatar/ModelReferential.cpp +++ b/interface/src/avatar/ModelReferential.cpp @@ -26,8 +26,7 @@ ModelReferential::ModelReferential(Referential* referential, ModelTree* tree, Av return; } - const ModelItem* item = _tr - ree->findModelByID(_modelID); + const ModelItem* item = _tree->findModelByID(_modelID); if (item != NULL) { _refScale = item->getRadius(); _refRotation = item->getModelRotation(); From 293c2e94ad645a370ba2050a0bbd0f1287721a31 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 5 Aug 2014 18:17:49 -0700 Subject: [PATCH 15/17] Correct member initialisation --- interface/src/avatar/Avatar.cpp | 6 ++++++ interface/src/avatar/ModelReferential.cpp | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index f4ed442aeb..1f84d3255b 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -248,6 +248,12 @@ void Avatar::render(const glm::vec3& cameraPosition, RenderMode renderMode) { const float MODEL_SCALE = 0.0006f; _skeletonModel.setScale(glm::vec3(1.0f, 1.0f, 1.0f) * getScale() * MODEL_SCALE); + glm::vec3 headPosition = _position; + _skeletonModel.getHeadPosition(headPosition); + Head* head = getHead(); + head->setPosition(headPosition); + head->setScale(_scale); + if (glm::distance(Application::getInstance()->getAvatar()->getPosition(), _position) < 10.0f) { // render pointing lasers diff --git a/interface/src/avatar/ModelReferential.cpp b/interface/src/avatar/ModelReferential.cpp index 6cf22a11ce..a6e611107f 100644 --- a/interface/src/avatar/ModelReferential.cpp +++ b/interface/src/avatar/ModelReferential.cpp @@ -19,8 +19,12 @@ ModelReferential::ModelReferential(Referential* referential, ModelTree* tree, AvatarData* avatar) : Referential(MODEL, avatar), _tree(tree) { + _translation = referential->getTranslation(); + _rotation = referential->getRotation(); + _scale = referential->getScale(); unpackExtraData(reinterpret_cast(referential->getExtraData().data()), referential->getExtraData().size()); + if (!isValid()) { qDebug() << "ModelReferential::copyConstructor(): Not Valid"; return; @@ -31,8 +35,8 @@ ModelReferential::ModelReferential(Referential* referential, ModelTree* tree, Av _refScale = item->getRadius(); _refRotation = item->getModelRotation(); _refPosition = item->getPosition() * (float)TREE_SCALE; + update(); } - update(); } ModelReferential::ModelReferential(uint32_t modelID, ModelTree* tree, AvatarData* avatar) : From 110b034bcbdcd70beb47a131e0bd359daf3a8fa6 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 5 Aug 2014 18:41:48 -0700 Subject: [PATCH 16/17] Changed updates locations --- interface/src/avatar/Avatar.cpp | 64 ++++++++++------------- interface/src/avatar/ModelReferential.cpp | 8 +-- libraries/avatars/src/AvatarData.cpp | 32 ++++++++---- libraries/avatars/src/AvatarData.h | 6 +-- 4 files changed, 59 insertions(+), 51 deletions(-) diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index 1f84d3255b..c9e03d15cc 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -103,6 +103,31 @@ float Avatar::getLODDistance() const { void Avatar::simulate(float deltaTime) { PerformanceTimer perfTimer("simulate"); + + // update the avatar's position according to its referential + if (_referential) { + if (_referential->hasExtraData()) { + ModelTree* tree = Application::getInstance()->getModels()->getTree(); + switch (_referential->type()) { + case Referential::MODEL: + _referential = new ModelReferential(_referential, + tree, + this); + break; + case Referential::JOINT: + _referential = new JointReferential(_referential, + tree, + this); + break; + default: + qDebug() << "[WARNING] Avatar::simulate(): Unknown referential type."; + break; + } + } + + _referential->update(); + } + if (_scale != _targetScale) { setScale(_targetScale); } @@ -180,28 +205,6 @@ void Avatar::simulate(float deltaTime) { } _displayNameAlpha = abs(_displayNameAlpha - _displayNameTargetAlpha) < 0.01f ? _displayNameTargetAlpha : _displayNameAlpha; } - if (_referential) { - if (_referential->hasExtraData()) { - ModelTree* tree = Application::getInstance()->getModels()->getTree(); - switch (_referential->type()) { - case Referential::MODEL: - _referential = new ModelReferential(_referential, - tree, - this); - break; - case Referential::JOINT: - _referential = new JointReferential(_referential, - tree, - this); - break; - default: - qDebug() << "[WARNING] Avatar::simulate(): Unknown referential type."; - break; - } - } - - _referential->update(); - } } void Avatar::updateAcceleration(float deltaTime) { @@ -241,18 +244,9 @@ static TextRenderer* textRenderer(TextRendererType type) { } void Avatar::render(const glm::vec3& cameraPosition, RenderMode renderMode) { - // make sure we have the right position - _skeletonModel.setTranslation(getPosition()); - static const glm::quat refOrientation = glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f)); - _skeletonModel.setRotation(getOrientation() * refOrientation); - const float MODEL_SCALE = 0.0006f; - _skeletonModel.setScale(glm::vec3(1.0f, 1.0f, 1.0f) * getScale() * MODEL_SCALE); - - glm::vec3 headPosition = _position; - _skeletonModel.getHeadPosition(headPosition); - Head* head = getHead(); - head->setPosition(headPosition); - head->setScale(_scale); + if (_referential) { + _referential->update(); + } if (glm::distance(Application::getInstance()->getAvatar()->getPosition(), _position) < 10.0f) { @@ -303,7 +297,7 @@ void Avatar::render(const glm::vec3& cameraPosition, RenderMode renderMode) { float boundingRadius = getBillboardSize(); ViewFrustum* frustum = (renderMode == Avatar::SHADOW_RENDER_MODE) ? Application::getInstance()->getShadowViewFrustum() : Application::getInstance()->getViewFrustum(); - if (frustum->sphereInFrustum(_position, boundingRadius) == ViewFrustum::OUTSIDE) { + if (frustum->sphereInFrustum(getPosition(), boundingRadius) == ViewFrustum::OUTSIDE) { return; } diff --git a/interface/src/avatar/ModelReferential.cpp b/interface/src/avatar/ModelReferential.cpp index a6e611107f..1b9e6a9f52 100644 --- a/interface/src/avatar/ModelReferential.cpp +++ b/interface/src/avatar/ModelReferential.cpp @@ -70,7 +70,7 @@ void ModelReferential::update() { bool somethingChanged = false; if (item->getRadius() != _refScale) { _refScale = item->getRadius(); - _avatar->setTargetScale(_refScale * _scale); + _avatar->setTargetScale(_refScale * _scale, true); somethingChanged = true; } if (item->getModelRotation() != _refRotation) { @@ -147,17 +147,17 @@ void JointReferential::update() { bool somethingChanged = false; if (item->getRadius() != _refScale) { _refScale = item->getRadius(); - _avatar->setTargetScale(_refScale * _scale); + _avatar->setTargetScale(_refScale * _scale, true); somethingChanged = true; } if (item->getModelRotation() != _refRotation) { model->getJointRotationInWorldFrame(_jointIndex, _refRotation); - _avatar->setOrientation(_refRotation * _rotation); + _avatar->setOrientation(_refRotation * _rotation, true); somethingChanged = true; } if (item->getPosition() != _refPosition || somethingChanged) { model->getJointPositionInWorldFrame(_jointIndex, _refPosition); - _avatar->setPosition(_refPosition + _refRotation * (_translation * _refScale)); + _avatar->setPosition(_refPosition + _refRotation * (_translation * _refScale), true); somethingChanged = true; } } diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index 59f9a440f0..0b41e0ffaa 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -94,7 +94,29 @@ void AvatarData::setOrientation(const glm::quat& orientation, bool overideRefere _bodyYaw = eulerAngles.y; _bodyRoll = eulerAngles.z; } -} +} + +float AvatarData::getTargetScale() const { + if (_referential) { + _referential->update(); + } + + return _targetScale; +} + +void AvatarData::setTargetScale(float targetScale, bool overideReferential) { + if (!_referential || overideReferential) { + _targetScale = targetScale; + } +} + +void AvatarData::setClampedTargetScale(float targetScale, bool overideReferential) { + + targetScale = glm::clamp(targetScale, MIN_AVATAR_SCALE, MAX_AVATAR_SCALE); + + setTargetScale(targetScale, overideReferential); + qDebug() << "Changed scale to " << _targetScale; +} glm::vec3 AvatarData::getHandPosition() const { return getOrientation() * _handPosition + _position; @@ -868,14 +890,6 @@ void AvatarData::setJointMappingsFromNetworkReply() { networkReply->deleteLater(); } -void AvatarData::setClampedTargetScale(float targetScale) { - - targetScale = glm::clamp(targetScale, MIN_AVATAR_SCALE, MAX_AVATAR_SCALE); - - _targetScale = targetScale; - qDebug() << "Changed scale to " << _targetScale; -} - void AvatarData::sendIdentityPacket() { QByteArray identityPacket = byteArrayWithPopulatedHeader(PacketTypeAvatarIdentity); identityPacket.append(identityByteArray()); diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index 767c905322..df64813a28 100755 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -190,9 +190,9 @@ public: void setAudioAverageLoudness(float value) { _headData->setAudioAverageLoudness(value); } // Scale - float getTargetScale() const { return _targetScale; } - void setTargetScale(float targetScale) { _targetScale = targetScale; } - void setClampedTargetScale(float targetScale); + float getTargetScale() const; + void setTargetScale(float targetScale, bool overideReferential = false); + void setClampedTargetScale(float targetScale, bool overideReferential = false); // Hand State Q_INVOKABLE void setHandState(char s) { _handState = s; } From fe8839b4b8f536e1971355a9e7ebc7642fbc0c02 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 6 Aug 2014 10:40:55 -0700 Subject: [PATCH 17/17] CR --- interface/src/avatar/ModelReferential.cpp | 8 +++----- libraries/avatars/src/AvatarData.cpp | 2 +- libraries/avatars/src/AvatarData.h | 2 ++ libraries/avatars/src/Referential.cpp | 17 ++++++++++++----- libraries/avatars/src/Referential.h | 9 ++++++--- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/interface/src/avatar/ModelReferential.cpp b/interface/src/avatar/ModelReferential.cpp index 1b9e6a9f52..d38abae70a 100644 --- a/interface/src/avatar/ModelReferential.cpp +++ b/interface/src/avatar/ModelReferential.cpp @@ -40,9 +40,9 @@ ModelReferential::ModelReferential(Referential* referential, ModelTree* tree, Av } ModelReferential::ModelReferential(uint32_t modelID, ModelTree* tree, AvatarData* avatar) : -Referential(MODEL, avatar), -_modelID(modelID), -_tree(tree) + Referential(MODEL, avatar), + _modelID(modelID), + _tree(tree) { const ModelItem* item = _tree->findModelByID(_modelID); if (!isValid() || item == NULL) { @@ -81,7 +81,6 @@ void ModelReferential::update() { if (item->getPosition() != _refPosition || somethingChanged) { _refPosition = item->getPosition(); _avatar->setPosition(_refPosition * (float)TREE_SCALE + _refRotation * (_translation * _refScale), true); - somethingChanged = true; } } @@ -158,7 +157,6 @@ void JointReferential::update() { if (item->getPosition() != _refPosition || somethingChanged) { model->getJointPositionInWorldFrame(_jointIndex, _refPosition); _avatar->setPosition(_refPosition + _refRotation * (_translation * _refScale), true); - somethingChanged = true; } } diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index 0b41e0ffaa..96a5f2425b 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -449,7 +449,7 @@ int AvatarData::parseDataAtOffset(const QByteArray& packet, int offset) { if (hasReferential) { Referential* ref = new Referential(sourceBuffer, this); if (_referential == NULL || - ref->createdAt() > _referential->createdAt()) { + ref->version() != _referential->version()) { changeReferential(ref); } else { delete ref; diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index df64813a28..2971598c1d 100755 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -282,6 +282,8 @@ public: QElapsedTimer& getLastUpdateTimer() { return _lastUpdateTimer; } virtual float getBoundingRadius() const { return 1.f; } + + const Referential* getReferential() const { return _referential; } public slots: void sendIdentityPacket(); diff --git a/libraries/avatars/src/Referential.cpp b/libraries/avatars/src/Referential.cpp index 63510da732..a5a7e7e3e5 100644 --- a/libraries/avatars/src/Referential.cpp +++ b/libraries/avatars/src/Referential.cpp @@ -11,11 +11,12 @@ #include +#include "AvatarData.h" #include "Referential.h" Referential::Referential(Type type, AvatarData* avatar) : _type(type), - _createdAt(usecTimestampNow()), + _version(0), _isValid(true), _avatar(avatar) { @@ -23,6 +24,9 @@ Referential::Referential(Type type, AvatarData* avatar) : _isValid = false; return; } + if (_avatar->hasReferential()) { + _version = _avatar->getReferential()->version() + 1; + } } Referential::Referential(const unsigned char*& sourceBuffer, AvatarData* avatar) : @@ -70,8 +74,8 @@ int Referential::unpackReferential(const unsigned char* sourceBuffer) { int Referential::pack(unsigned char* destinationBuffer) const { unsigned char* startPosition = destinationBuffer; *destinationBuffer++ = (unsigned char)_type; - memcpy(destinationBuffer, &_createdAt, sizeof(_createdAt)); - destinationBuffer += sizeof(_createdAt); + memcpy(destinationBuffer, &_version, sizeof(_version)); + destinationBuffer += sizeof(_version); destinationBuffer += packFloatVec3ToSignedTwoByteFixed(destinationBuffer, _translation, 0); destinationBuffer += packOrientationQuatToBytes(destinationBuffer, _rotation); @@ -82,8 +86,11 @@ int Referential::pack(unsigned char* destinationBuffer) const { int Referential::unpack(const unsigned char* sourceBuffer) { const unsigned char* startPosition = sourceBuffer; _type = (Type)*sourceBuffer++; - memcpy(&_createdAt, sourceBuffer, sizeof(_createdAt)); - sourceBuffer += sizeof(_createdAt); + if (_type < 0 || _type >= NUM_TYPE) { + _type = UNKNOWN; + } + memcpy(&_version, sourceBuffer, sizeof(_version)); + sourceBuffer += sizeof(_version); sourceBuffer += unpackFloatVec3FromSignedTwoByteFixed(sourceBuffer, _translation, 0); sourceBuffer += unpackOrientationQuatFromBytes(sourceBuffer, _rotation); diff --git a/libraries/avatars/src/Referential.h b/libraries/avatars/src/Referential.h index 905447dcbb..f24d66c160 100644 --- a/libraries/avatars/src/Referential.h +++ b/libraries/avatars/src/Referential.h @@ -21,16 +21,19 @@ class AvatarData; class Referential { public: enum Type { + UNKNOWN, MODEL, JOINT, - AVATAR + AVATAR, + + NUM_TYPE }; Referential(const unsigned char*& sourceBuffer, AvatarData* avatar); virtual ~Referential(); Type type() const { return _type; } - quint64 createdAt() const { return _createdAt; } + quint8 version() const { return _version; } bool isValid() const { return _isValid; } bool hasExtraData() const { return !_extraDataBuffer.isEmpty(); } @@ -54,7 +57,7 @@ protected: virtual int unpackExtraData(const unsigned char* sourceBuffer, int size); Type _type; - quint64 _createdAt; + quint8 _version; bool _isValid; AvatarData* _avatar; QByteArray _extraDataBuffer;