PR cleanup + JS API

This commit is contained in:
Atlante45 2014-08-05 17:07:03 -07:00
parent 39a74cbc28
commit b360b6c55d
14 changed files with 139 additions and 67 deletions

View file

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

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() 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.

View file

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

View file

@ -209,7 +209,7 @@ protected:
virtual void renderAttachments(RenderMode renderMode);
virtual void updateJointMappings();
private:
bool _initialized;

View file

@ -22,20 +22,18 @@ ModelReferential::ModelReferential(Referential* referential, ModelTree* tree, Av
unpackExtraData(reinterpret_cast<unsigned char*>(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;
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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<const char*>(sourceBuffer), size);

View file

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

View file

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

View file

@ -179,7 +179,6 @@ void ModelItemIDfromScriptValue(const QScriptValue &object, ModelItemID& propert
/// ModelItem class - this is the actual model item class.
class ModelItem {
public:
ModelItem();