Various tweaks to referentials

This commit is contained in:
Atlante45 2014-08-04 14:59:15 -07:00
parent 15cbfa0715
commit d9dde06c14
11 changed files with 121 additions and 48 deletions

View file

@ -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.

View file

@ -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);

View file

@ -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;

View file

@ -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;
};

View file

@ -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);
}
}
}
}

View file

@ -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;
}

View file

@ -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;

View file

@ -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<const char*>(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<const char*>(sourceBuffer), size + 1);
sourceBuffer += size + 1;
return sourceBuffer - startPosition;
}

View file

@ -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;

View file

@ -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);
}

View file

@ -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