From d9dde06c146496752e07d499fbd9ebdafb4c397e Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 4 Aug 2014 14:59:15 -0700 Subject: [PATCH] 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