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
This commit is contained in:
Eric Johnston 2013-06-28 11:34:20 -07:00
parent 1c797405da
commit 0ff3554dd8
4 changed files with 50 additions and 15 deletions

View file

@ -21,7 +21,9 @@ Hand::Hand(Avatar* owningAvatar) :
_owningAvatar(owningAvatar), _owningAvatar(owningAvatar),
_renderAlpha(1.0), _renderAlpha(1.0),
_lookingInMirror(false), _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() { 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<glm::vec3>& fingerPositions) { void Hand::setLeapFingers(const std::vector<glm::vec3>& fingerPositions) {
_numLeapBalls = fingerPositions.size(); // just to test _fingerPositions = fingerPositions;
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;
}
} }

View file

@ -10,13 +10,13 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <AvatarData.h> #include <AvatarData.h>
#include <HandData.h>
#include "Balls.h" #include "Balls.h"
#include "world.h" #include "world.h"
#include "InterfaceConfig.h" #include "InterfaceConfig.h"
#include "SerialInterface.h" #include "SerialInterface.h"
#include <SharedUtil.h> #include <SharedUtil.h>
#define MAX_AVATAR_LEAP_BALLS 10
class Avatar; class Avatar;
class ProgramObject; class ProgramObject;
@ -40,7 +40,6 @@ public:
void simulate(float deltaTime, bool isMine); void simulate(float deltaTime, bool isMine);
void render(bool lookingInMirror); void render(bool lookingInMirror);
void setFingerPositions(std::vector<glm::vec3> fingerPositions) { _fingerPositions = fingerPositions; }
void setBallColor (glm::vec3 ballColor ) { _ballColor = ballColor; } void setBallColor (glm::vec3 ballColor ) { _ballColor = ballColor; }
void setLeapFingers (const std::vector<glm::vec3>& fingerPositions); void setLeapFingers (const std::vector<glm::vec3>& fingerPositions);
@ -56,8 +55,9 @@ private:
Avatar* _owningAvatar; Avatar* _owningAvatar;
float _renderAlpha; float _renderAlpha;
bool _lookingInMirror; bool _lookingInMirror;
std::vector<glm::vec3> _fingerPositions;
glm::vec3 _ballColor; glm::vec3 _ballColor;
glm::vec3 _position;
glm::quat _orientation;
int _numLeapBalls; int _numLeapBalls;
HandBall _leapBall[ MAX_AVATAR_LEAP_BALLS ]; HandBall _leapBall[ MAX_AVATAR_LEAP_BALLS ];

View file

@ -124,6 +124,16 @@ int AvatarData::getBroadcastData(unsigned char* destinationBuffer) {
setSemiNibbleAt(bitItems,HAND_STATE_START_BIT,_handState); setSemiNibbleAt(bitItems,HAND_STATE_START_BIT,_handState);
*destinationBuffer++ = bitItems; *destinationBuffer++ = bitItems;
// leap hand data
const std::vector<glm::vec3>& 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; 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 // hand state, stored as a semi-nibble in the bitItems
_handState = getSemiNibbleAt(bitItems,HAND_STATE_START_BIT); _handState = getSemiNibbleAt(bitItems,HAND_STATE_START_BIT);
// leap hand data
if (sourceBuffer - startPosition < numBytes) // safety check
{
std::vector<glm::vec3> 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; return sourceBuffer - startPosition;
} }

View file

@ -14,6 +14,8 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#define MAX_AVATAR_LEAP_BALLS 10
class AvatarData; class AvatarData;
class HandData { class HandData {