From 0ff3554dd85eb9c9b4e5a726cd2b897c8fcc8a07 Mon Sep 17 00:00:00 2001 From: Eric Johnston Date: Fri, 28 Jun 2013 11:34:20 -0700 Subject: [PATCH] Networked Leap fingers - Simplified finger updates and storage - added pack and unpack for the fingers - not yet tested between multiple machines, but *should* be harmless and backward-packet-compatible --- interface/src/Hand.cpp | 30 +++++++++++++++++----------- interface/src/Hand.h | 6 +++--- libraries/avatars/src/AvatarData.cpp | 27 +++++++++++++++++++++++++ libraries/avatars/src/HandData.h | 2 ++ 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/interface/src/Hand.cpp b/interface/src/Hand.cpp index 30219c27c9..5a44546d7b 100755 --- a/interface/src/Hand.cpp +++ b/interface/src/Hand.cpp @@ -21,7 +21,9 @@ Hand::Hand(Avatar* owningAvatar) : _owningAvatar(owningAvatar), _renderAlpha(1.0), _lookingInMirror(false), - _ballColor(0.0f, 0.4f, 0.0f) + _ballColor(0.0, 0.4, 0.0), + _position(0.0, 0.4, 0.0), + _orientation(0.0, 0.0, 0.0, 1.0) { } @@ -43,6 +45,20 @@ void Hand::simulate(float deltaTime, bool isMine) { } void Hand::calculateGeometry() { + glm::vec3 offset(0.1, -0.1, -0.15); // place the hand in front of the face where we can see it + + Head& head = _owningAvatar->getHead(); + _position = head.getPosition() + head.getOrientation() * offset; + _orientation = head.getOrientation(); + + _numLeapBalls = _fingerPositions.size(); + + float unitScale = 0.001; // convert mm to meters + for (int b = 0; b < _numLeapBalls; b++) { + glm::vec3 pos = unitScale * _fingerPositions[b] + offset; + _leapBall[b].rotation = _orientation; + _leapBall[b].position = _position + _orientation * pos; + } } @@ -78,17 +94,7 @@ void Hand::renderHandSpheres() { } void Hand::setLeapFingers(const std::vector& fingerPositions) { - _numLeapBalls = fingerPositions.size(); // just to test - - float unitScale = 0.001; // convert mm to meters - glm::vec3 offset(0.2, -0.2, -0.3); // place the hand in front of the face where we can see it - - Head& head = _owningAvatar->getHead(); - for (int b = 0; b < _numLeapBalls; b++) { - glm::vec3 pos = unitScale * fingerPositions[b] + offset; - _leapBall[b].rotation = head.getOrientation(); - _leapBall[b].position = head.getPosition() + head.getOrientation() * pos; - } + _fingerPositions = fingerPositions; } diff --git a/interface/src/Hand.h b/interface/src/Hand.h index 180ad00a7b..49eed1f817 100755 --- a/interface/src/Hand.h +++ b/interface/src/Hand.h @@ -10,13 +10,13 @@ #include #include +#include #include "Balls.h" #include "world.h" #include "InterfaceConfig.h" #include "SerialInterface.h" #include -#define MAX_AVATAR_LEAP_BALLS 10 class Avatar; class ProgramObject; @@ -40,7 +40,6 @@ public: void simulate(float deltaTime, bool isMine); void render(bool lookingInMirror); - void setFingerPositions(std::vector fingerPositions) { _fingerPositions = fingerPositions; } void setBallColor (glm::vec3 ballColor ) { _ballColor = ballColor; } void setLeapFingers (const std::vector& fingerPositions); @@ -56,8 +55,9 @@ private: Avatar* _owningAvatar; float _renderAlpha; bool _lookingInMirror; - std::vector _fingerPositions; glm::vec3 _ballColor; + glm::vec3 _position; + glm::quat _orientation; int _numLeapBalls; HandBall _leapBall[ MAX_AVATAR_LEAP_BALLS ]; diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index dba7217d28..c8e53b6652 100755 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -124,6 +124,16 @@ int AvatarData::getBroadcastData(unsigned char* destinationBuffer) { setSemiNibbleAt(bitItems,HAND_STATE_START_BIT,_handState); *destinationBuffer++ = bitItems; + // leap hand data + const std::vector& fingerPositions = _handData->getFingerPositions(); + *destinationBuffer++ = (unsigned char)fingerPositions.size(); + for (size_t i = 0; i < fingerPositions.size(); ++i) + { + destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, fingerPositions[i].x, 4); + destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, fingerPositions[i].y, 4); + destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, fingerPositions[i].z, 4); + } + return destinationBuffer - bufferStart; } @@ -216,6 +226,23 @@ int AvatarData::parseData(unsigned char* sourceBuffer, int numBytes) { // hand state, stored as a semi-nibble in the bitItems _handState = getSemiNibbleAt(bitItems,HAND_STATE_START_BIT); + // leap hand data + if (sourceBuffer - startPosition < numBytes) // safety check + { + std::vector fingerPositions = _handData->getFingerPositions(); + unsigned int numFingers = *sourceBuffer++; + if (numFingers > MAX_AVATAR_LEAP_BALLS) // safety check + numFingers = 0; + fingerPositions.resize(numFingers); + for (size_t i = 0; i < numFingers; ++i) + { + sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((uint16_t*) sourceBuffer, &(fingerPositions[i].x), 4); + sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((uint16_t*) sourceBuffer, &(fingerPositions[i].y), 4); + sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((uint16_t*) sourceBuffer, &(fingerPositions[i].z), 4); + } + _handData->setFingerPositions(fingerPositions); + } + return sourceBuffer - startPosition; } diff --git a/libraries/avatars/src/HandData.h b/libraries/avatars/src/HandData.h index d31bc8bf1a..86c582a07f 100755 --- a/libraries/avatars/src/HandData.h +++ b/libraries/avatars/src/HandData.h @@ -14,6 +14,8 @@ #include +#define MAX_AVATAR_LEAP_BALLS 10 + class AvatarData; class HandData {