From efbccc3a4e883e20a3010466186de83eb38b2fc2 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Mon, 29 Jun 2015 15:30:51 -0700 Subject: [PATCH 01/30] Fix storing Oculus eye positions --- interface/src/devices/OculusManager.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/interface/src/devices/OculusManager.cpp b/interface/src/devices/OculusManager.cpp index 9d7146cbe7..e5c00a3699 100644 --- a/interface/src/devices/OculusManager.cpp +++ b/interface/src/devices/OculusManager.cpp @@ -692,13 +692,13 @@ void OculusManager::display(QGLWidget * glCanvas, RenderArgs* renderArgs, const _eyeRenderPoses[eye] = eyePoses[eye]; // Set the camera rotation for this eye - vec3 eyePosition = toGlm(_eyeRenderPoses[eye].Position); - eyePosition = whichCamera.getRotation() * eyePosition; + _eyePositions[eye] = toGlm(_eyeRenderPoses[eye].Position); + _eyePositions[eye] = whichCamera.getRotation() * _eyePositions[eye]; quat eyeRotation = toGlm(_eyeRenderPoses[eye].Orientation); // Update our camera to what the application camera is doing _camera->setRotation(whichCamera.getRotation() * eyeRotation); - _camera->setPosition(whichCamera.getPosition() + eyePosition); + _camera->setPosition(whichCamera.getPosition() + _eyePositions[eye]); configureCamera(*_camera); _camera->update(1.0f / Application::getInstance()->getFps()); From 6a48b56e0f48381030fe51c333c6d808e49d6cf3 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Mon, 29 Jun 2015 15:31:15 -0700 Subject: [PATCH 02/30] In mirror mode look directly at the camera in both normal and HMD view --- interface/src/Application.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 2e2722aec2..6975f13291 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2282,17 +2282,14 @@ void Application::updateMyAvatarLookAtPosition() { bool isLookingAtSomeone = false; glm::vec3 lookAtSpot; if (_myCamera.getMode() == CAMERA_MODE_MIRROR) { - // When I am in mirror mode, just look right at the camera (myself) + // When I am in mirror mode, just look right at the camera (myself); don't switch gaze points because when physically + // looking in a mirror one's eyes appear steady. if (!OculusManager::isConnected()) { lookAtSpot = _myCamera.getPosition(); } else { - if (_myAvatar->isLookingAtLeftEye()) { - lookAtSpot = OculusManager::getLeftEyePosition(); - } else { - lookAtSpot = OculusManager::getRightEyePosition(); - } + lookAtSpot = _myCamera.getPosition() + + (OculusManager::getLeftEyePosition() + OculusManager::getRightEyePosition()) / 2.0f; } - } else { AvatarSharedPointer lookingAt = _myAvatar->getLookAtTargetAvatar().lock(); if (lookingAt && _myAvatar != lookingAt.get()) { From bc4c6351068e81c8d4171ac7e7f5c6a0aa260089 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 1 Jul 2015 18:31:16 -0700 Subject: [PATCH 03/30] Fix gazing at avatar (when no head tracking or HMD) Randomly look at avatar's left and right eyes if the face is visible. Otherwise just look at their head. --- interface/src/Application.cpp | 28 ++++++++++++++++++++-------- interface/src/avatar/FaceModel.cpp | 2 +- interface/src/avatar/Head.cpp | 4 ++-- interface/src/avatar/MyAvatar.cpp | 16 ---------------- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 6975f13291..acc34cbc19 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2294,22 +2294,34 @@ void Application::updateMyAvatarLookAtPosition() { AvatarSharedPointer lookingAt = _myAvatar->getLookAtTargetAvatar().lock(); if (lookingAt && _myAvatar != lookingAt.get()) { isLookingAtSomeone = true; + Head* lookingAtHead = static_cast(lookingAt.get())->getHead(); + // If I am looking at someone else, look directly at one of their eyes if (tracker && !tracker->isMuted()) { // If a face tracker is active, look at the eye for the side my gaze is biased toward if (tracker->getEstimatedEyeYaw() > _myAvatar->getHead()->getFinalYaw()) { - // Look at their right eye - lookAtSpot = static_cast(lookingAt.get())->getHead()->getRightEyePosition(); + lookAtSpot = lookingAtHead->getRightEyePosition(); } else { - // Look at their left eye - lookAtSpot = static_cast(lookingAt.get())->getHead()->getLeftEyePosition(); + lookAtSpot = lookingAtHead->getLeftEyePosition(); } } else { - // Need to add randomly looking back and forth between left and right eye for case with no tracker - if (_myAvatar->isLookingAtLeftEye()) { - lookAtSpot = static_cast(lookingAt.get())->getHead()->getLeftEyePosition(); + + const float MAXIMUM_FACE_ANGLE = 65.0f * RADIANS_PER_DEGREE; + glm::vec3 lookingAtFaceOrientation = lookingAtHead->getFinalOrientationInWorldFrame() * IDENTITY_FRONT; + glm::vec3 fromLookingAtToMe = glm::normalize(_myAvatar->getHead()->getEyePosition() + - lookingAtHead->getEyePosition()); + float faceAngle = glm::angle(lookingAtFaceOrientation, fromLookingAtToMe); + + if (faceAngle < MAXIMUM_FACE_ANGLE) { + // Randomly look back and forth between left and right eyes + if (_myAvatar->isLookingAtLeftEye()) { + lookAtSpot = lookingAtHead->getLeftEyePosition(); + } else { + lookAtSpot = lookingAtHead->getRightEyePosition(); + } } else { - lookAtSpot = static_cast(lookingAt.get())->getHead()->getRightEyePosition(); + // Just look at their head (mid point between eyes) + lookAtSpot = lookingAtHead->getEyePosition(); } } } else { diff --git a/interface/src/avatar/FaceModel.cpp b/interface/src/avatar/FaceModel.cpp index 1501c52de5..7a582406a4 100644 --- a/interface/src/avatar/FaceModel.cpp +++ b/interface/src/avatar/FaceModel.cpp @@ -77,7 +77,7 @@ void FaceModel::maybeUpdateEyeRotation(Model* model, const JointState& parentSta glm::translate(state.getDefaultTranslationInConstrainedFrame()) * joint.preTransform * glm::mat4_cast(joint.preRotation * joint.rotation)); glm::vec3 front = glm::vec3(inverse * glm::vec4(_owningHead->getFinalOrientationInWorldFrame() * IDENTITY_FRONT, 0.0f)); - glm::vec3 lookAt = glm::vec3(inverse * glm::vec4(_owningHead->getCorrectedLookAtPosition() + + glm::vec3 lookAt = glm::vec3(inverse * glm::vec4(_owningHead->getLookAtPosition() + _owningHead->getSaccade() - model->getTranslation(), 1.0f)); glm::quat between = rotationBetween(front, lookAt); const float MAX_ANGLE = 30.0f * RADIANS_PER_DEGREE; diff --git a/interface/src/avatar/Head.cpp b/interface/src/avatar/Head.cpp index 02d16a1ca4..61f378c536 100644 --- a/interface/src/avatar/Head.cpp +++ b/interface/src/avatar/Head.cpp @@ -256,7 +256,7 @@ void Head::calculateMouthShapes() { void Head::applyEyelidOffset(glm::quat headOrientation) { // Adjusts the eyelid blendshape coefficients so that the eyelid follows the iris as the head pitches. - glm::quat eyeRotation = rotationBetween(headOrientation * IDENTITY_FRONT, getCorrectedLookAtPosition() - _eyePosition); + glm::quat eyeRotation = rotationBetween(headOrientation * IDENTITY_FRONT, getLookAtPosition() - _eyePosition); eyeRotation = eyeRotation * glm::angleAxis(safeEulerAngles(headOrientation).y, IDENTITY_UP); // Rotation w.r.t. head float eyePitch = safeEulerAngles(eyeRotation).x; @@ -295,7 +295,7 @@ void Head::relaxLean(float deltaTime) { void Head::render(RenderArgs* renderArgs, float alpha, ViewFrustum* renderFrustum, bool postLighting) { if (postLighting) { if (_renderLookatVectors) { - renderLookatVectors(renderArgs, _leftEyePosition, _rightEyePosition, getCorrectedLookAtPosition()); + renderLookatVectors(renderArgs, _leftEyePosition, _rightEyePosition, getLookAtPosition()); } } } diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index e4ed55601a..f0bba200fe 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -880,7 +880,6 @@ void MyAvatar::updateLookAtTargetAvatar() { const float KEEP_LOOKING_AT_CURRENT_ANGLE_FACTOR = 1.3f; const float GREATEST_LOOKING_AT_DISTANCE = 10.0f; - int howManyLookingAtMe = 0; foreach (const AvatarSharedPointer& avatarPointer, DependencyManager::get()->getAvatarHash()) { Avatar* avatar = static_cast(avatarPointer.get()); bool isCurrentTarget = avatar->getIsLookAtTarget(); @@ -893,21 +892,6 @@ void MyAvatar::updateLookAtTargetAvatar() { _targetAvatarPosition = avatarPointer->getPosition(); smallestAngleTo = angleTo; } - // Check if this avatar is looking at me, and fix their gaze on my camera if so - if (Application::getInstance()->isLookingAtMyAvatar(avatar)) { - howManyLookingAtMe++; - // Have that avatar look directly at my camera - // Philip TODO: correct to look at left/right eye - if (qApp->isHMDMode()) { - avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getViewFrustum()->getPosition()); - // FIXME what is the point of this? - // avatar->getHead()->setCorrectedLookAtPosition(OculusManager::getLeftEyePosition()); - } else { - avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getViewFrustum()->getPosition()); - } - } else { - avatar->getHead()->clearCorrectedLookAtPosition(); - } } } auto avatarPointer = _lookAtTargetAvatar.lock(); From fada70fe02f48358bb435935373cd431768ba673 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 1 Jul 2015 18:32:33 -0700 Subject: [PATCH 04/30] Remove redundant code --- interface/src/Application.cpp | 9 --------- interface/src/Application.h | 2 -- interface/src/avatar/Head.cpp | 13 ------------- interface/src/avatar/Head.h | 5 ----- 4 files changed, 29 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index acc34cbc19..f8fb2bb9a6 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2229,15 +2229,6 @@ void Application::shrinkMirrorView() { const float HEAD_SPHERE_RADIUS = 0.1f; -bool Application::isLookingAtMyAvatar(Avatar* avatar) { - glm::vec3 theirLookAt = avatar->getHead()->getLookAtPosition(); - glm::vec3 myEyePosition = _myAvatar->getHead()->getEyePosition(); - if (pointInSphere(theirLookAt, myEyePosition, HEAD_SPHERE_RADIUS * _myAvatar->getScale())) { - return true; - } - return false; -} - void Application::updateLOD() { PerformanceTimer perfTimer("LOD"); // adjust it unless we were asked to disable this feature, or if we're currently in throttleRendering mode diff --git a/interface/src/Application.h b/interface/src/Application.h index 0787cffbdc..375aded8ac 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -322,8 +322,6 @@ public: QStringList getRunningScripts() { return _scriptEnginesHash.keys(); } ScriptEngine* getScriptEngine(QString scriptHash) { return _scriptEnginesHash.contains(scriptHash) ? _scriptEnginesHash[scriptHash] : NULL; } - bool isLookingAtMyAvatar(Avatar* avatar); - float getRenderResolutionScale() const; int getRenderAmbientLight() const; diff --git a/interface/src/avatar/Head.cpp b/interface/src/avatar/Head.cpp index 61f378c536..27888b9d4e 100644 --- a/interface/src/avatar/Head.cpp +++ b/interface/src/avatar/Head.cpp @@ -315,19 +315,6 @@ glm::quat Head::getFinalOrientationInLocalFrame() const { return glm::quat(glm::radians(glm::vec3(getFinalPitch(), getFinalYaw(), getFinalRoll() ))); } -glm::vec3 Head::getCorrectedLookAtPosition() { - if (_isLookingAtMe) { - return _correctedLookAtPosition; - } else { - return getLookAtPosition(); - } -} - -void Head::setCorrectedLookAtPosition(glm::vec3 correctedLookAtPosition) { - _isLookingAtMe = true; - _correctedLookAtPosition = correctedLookAtPosition; -} - glm::quat Head::getCameraOrientation() const { // NOTE: Head::getCameraOrientation() is not used for orienting the camera "view" while in Oculus mode, so // you may wonder why this code is here. This method will be called while in Oculus mode to determine how diff --git a/interface/src/avatar/Head.h b/interface/src/avatar/Head.h index a208574c26..0b216e4a2e 100644 --- a/interface/src/avatar/Head.h +++ b/interface/src/avatar/Head.h @@ -56,9 +56,6 @@ public: /// \return orientationBody * orientationBasePitch glm::quat getCameraOrientation () const; - void setCorrectedLookAtPosition(glm::vec3 correctedLookAtPosition); - glm::vec3 getCorrectedLookAtPosition(); - void clearCorrectedLookAtPosition() { _isLookingAtMe = false; } bool getIsLookingAtMe() { return _isLookingAtMe; } float getScale() const { return _scale; } @@ -147,8 +144,6 @@ private: bool _isLookingAtMe; FaceModel _faceModel; - glm::vec3 _correctedLookAtPosition; - int _leftEyeLookAtID; int _rightEyeLookAtID; From 26cbb14f45c91f788d5f7ce89a6435bcd4e9c6bb Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 1 Jul 2015 20:09:36 -0700 Subject: [PATCH 05/30] Alternative look-at gaze left/right with face trackers too Instead of looking at one or other eye depending on look direction. --- interface/src/Application.cpp | 38 +++++++++++++---------------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index f8fb2bb9a6..29bc9a4f9c 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2284,36 +2284,26 @@ void Application::updateMyAvatarLookAtPosition() { } else { AvatarSharedPointer lookingAt = _myAvatar->getLookAtTargetAvatar().lock(); if (lookingAt && _myAvatar != lookingAt.get()) { + // If I am looking at someone else, look directly at one of their eyes isLookingAtSomeone = true; Head* lookingAtHead = static_cast(lookingAt.get())->getHead(); - // If I am looking at someone else, look directly at one of their eyes - if (tracker && !tracker->isMuted()) { - // If a face tracker is active, look at the eye for the side my gaze is biased toward - if (tracker->getEstimatedEyeYaw() > _myAvatar->getHead()->getFinalYaw()) { - lookAtSpot = lookingAtHead->getRightEyePosition(); - } else { + const float MAXIMUM_FACE_ANGLE = 65.0f * RADIANS_PER_DEGREE; + glm::vec3 lookingAtFaceOrientation = lookingAtHead->getFinalOrientationInWorldFrame() * IDENTITY_FRONT; + glm::vec3 fromLookingAtToMe = glm::normalize(_myAvatar->getHead()->getEyePosition() + - lookingAtHead->getEyePosition()); + float faceAngle = glm::angle(lookingAtFaceOrientation, fromLookingAtToMe); + + if (faceAngle < MAXIMUM_FACE_ANGLE) { + // Randomly look back and forth between left and right eyes + if (_myAvatar->isLookingAtLeftEye()) { lookAtSpot = lookingAtHead->getLeftEyePosition(); + } else { + lookAtSpot = lookingAtHead->getRightEyePosition(); } } else { - - const float MAXIMUM_FACE_ANGLE = 65.0f * RADIANS_PER_DEGREE; - glm::vec3 lookingAtFaceOrientation = lookingAtHead->getFinalOrientationInWorldFrame() * IDENTITY_FRONT; - glm::vec3 fromLookingAtToMe = glm::normalize(_myAvatar->getHead()->getEyePosition() - - lookingAtHead->getEyePosition()); - float faceAngle = glm::angle(lookingAtFaceOrientation, fromLookingAtToMe); - - if (faceAngle < MAXIMUM_FACE_ANGLE) { - // Randomly look back and forth between left and right eyes - if (_myAvatar->isLookingAtLeftEye()) { - lookAtSpot = lookingAtHead->getLeftEyePosition(); - } else { - lookAtSpot = lookingAtHead->getRightEyePosition(); - } - } else { - // Just look at their head (mid point between eyes) - lookAtSpot = lookingAtHead->getEyePosition(); - } + // Just look at their head (mid point between eyes) + lookAtSpot = lookingAtHead->getEyePosition(); } } else { // I am not looking at anyone else, so just look forward From 4ced0dc6c441f926a7bb31e0d762bc69d87387e0 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 1 Jul 2015 20:24:47 -0700 Subject: [PATCH 06/30] Only deflect eyes for Faceshift; DDE doesn't provide eye pitch or yaw --- interface/src/Application.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 29bc9a4f9c..48ea822efc 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2311,14 +2311,13 @@ void Application::updateMyAvatarLookAtPosition() { (_myAvatar->getHead()->getFinalOrientationInWorldFrame() * glm::vec3(0.0f, 0.0f, -TREE_SCALE)); } } - // - // Deflect the eyes a bit to match the detected Gaze from 3D camera if active - // - if (tracker && !tracker->isMuted()) { + + // Deflect the eyes a bit to match the detected gaze from Faceshift if active. + // DDE doesn't track eyes. + if (tracker && typeid(*tracker) == typeid(Faceshift) && !tracker->isMuted()) { float eyePitch = tracker->getEstimatedEyePitch(); float eyeYaw = tracker->getEstimatedEyeYaw(); const float GAZE_DEFLECTION_REDUCTION_DURING_EYE_CONTACT = 0.1f; - // deflect using Faceshift gaze data glm::vec3 origin = _myAvatar->getHead()->getEyePosition(); float pitchSign = (_myCamera.getMode() == CAMERA_MODE_MIRROR) ? -1.0f : 1.0f; float deflection = DependencyManager::get()->getEyeDeflection(); From 026f6d3690165c1adca62a722cf158b6c533590c Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 1 Jul 2015 21:22:30 -0700 Subject: [PATCH 07/30] Add mouth as third gaze target in addition to left and right eyes --- interface/src/Application.cpp | 16 +++++++++++----- interface/src/avatar/Head.h | 6 +----- interface/src/avatar/MyAvatar.cpp | 24 ++++++++++++++++++------ interface/src/avatar/MyAvatar.h | 10 ++++++++-- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 48ea822efc..d68a3813a9 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2295,11 +2295,17 @@ void Application::updateMyAvatarLookAtPosition() { float faceAngle = glm::angle(lookingAtFaceOrientation, fromLookingAtToMe); if (faceAngle < MAXIMUM_FACE_ANGLE) { - // Randomly look back and forth between left and right eyes - if (_myAvatar->isLookingAtLeftEye()) { - lookAtSpot = lookingAtHead->getLeftEyePosition(); - } else { - lookAtSpot = lookingAtHead->getRightEyePosition(); + // Randomly look back and forth between look targets + switch (_myAvatar->getEyeContactTarget()) { + case LEFT_EYE: + lookAtSpot = lookingAtHead->getLeftEyePosition(); + break; + case RIGHT_EYE: + lookAtSpot = lookingAtHead->getRightEyePosition(); + break; + case MOUTH: + lookAtSpot = lookingAtHead->getMouthPosition(); + break; } } else { // Just look at their head (mid point between eyes) diff --git a/interface/src/avatar/Head.h b/interface/src/avatar/Head.h index 0b216e4a2e..a053a5bd44 100644 --- a/interface/src/avatar/Head.h +++ b/interface/src/avatar/Head.h @@ -22,11 +22,6 @@ #include "InterfaceConfig.h" #include "world.h" -enum eyeContactTargets { - LEFT_EYE, - RIGHT_EYE, - MOUTH -}; const float EYE_EAR_GAP = 0.08f; @@ -74,6 +69,7 @@ public: const glm::vec3& getLeftEyePosition() const { return _leftEyePosition; } glm::vec3 getRightEarPosition() const { return _rightEyePosition + (getRightDirection() * EYE_EAR_GAP) + (getFrontDirection() * -EYE_EAR_GAP); } glm::vec3 getLeftEarPosition() const { return _leftEyePosition + (getRightDirection() * -EYE_EAR_GAP) + (getFrontDirection() * -EYE_EAR_GAP); } + glm::vec3 getMouthPosition() const { return _eyePosition - getUpDirection() * glm::length(_rightEyePosition - _leftEyePosition); } FaceModel& getFaceModel() { return _faceModel; } const FaceModel& getFaceModel() const { return _faceModel; } diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index f0bba200fe..170fc03d17 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -95,7 +95,7 @@ MyAvatar::MyAvatar() : _shouldRender(true), _billboardValid(false), _feetTouchFloor(true), - _isLookingAtLeftEye(true), + _eyeContactTarget(LEFT_EYE), _realWorldFieldOfView("realWorldFieldOfView", DEFAULT_REAL_WORLD_FIELD_OF_VIEW_DEGREES), _firstPersonSkeletonModel(this), @@ -904,12 +904,24 @@ void MyAvatar::clearLookAtTargetAvatar() { _lookAtTargetAvatar.reset(); } -bool MyAvatar::isLookingAtLeftEye() { - float const CHANCE_OF_CHANGING_EYE = 0.01f; - if (randFloat() < CHANCE_OF_CHANGING_EYE) { - _isLookingAtLeftEye = !_isLookingAtLeftEye; +eyeContactTarget MyAvatar::getEyeContactTarget() { + float const CHANCE_OF_CHANGING_TARGET = 0.01f; + if (randFloat() < CHANCE_OF_CHANGING_TARGET) { + float const FIFTY_FIFTY_CHANCE = 0.5f; + switch (_eyeContactTarget) { + case LEFT_EYE: + _eyeContactTarget = (randFloat() < FIFTY_FIFTY_CHANCE) ? MOUTH : RIGHT_EYE; + break; + case RIGHT_EYE: + _eyeContactTarget = (randFloat() < FIFTY_FIFTY_CHANCE) ? LEFT_EYE : MOUTH; + break; + case MOUTH: + _eyeContactTarget = (randFloat() < FIFTY_FIFTY_CHANCE) ? RIGHT_EYE : LEFT_EYE; + break; + } } - return _isLookingAtLeftEye; + + return _eyeContactTarget; } glm::vec3 MyAvatar::getDefaultEyePosition() const { diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index 2fea09ee27..daec7d3457 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -19,6 +19,12 @@ class ModelItemID; +enum eyeContactTarget { + LEFT_EYE, + RIGHT_EYE, + MOUTH +}; + class MyAvatar : public Avatar { Q_OBJECT Q_PROPERTY(bool shouldRenderLocally READ getShouldRenderLocally WRITE setShouldRenderLocally) @@ -93,7 +99,7 @@ public: bool isMyAvatar() const { return true; } - bool isLookingAtLeftEye(); + eyeContactTarget getEyeContactTarget(); virtual int parseDataAtOffset(const QByteArray& packet, int offset); @@ -245,7 +251,7 @@ private: QList _animationHandles; bool _feetTouchFloor; - bool _isLookingAtLeftEye; + eyeContactTarget _eyeContactTarget; RecorderPointer _recorder; From 3a92878544669199e2239188ae1080adb7b8e223 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Thu, 2 Jul 2015 13:47:52 -0700 Subject: [PATCH 08/30] Reinstate making someone looking at me look at my camera instead of face --- interface/src/Application.cpp | 9 +++++++++ interface/src/Application.h | 2 ++ interface/src/avatar/FaceModel.cpp | 2 +- interface/src/avatar/Head.cpp | 19 ++++++++++++++++--- interface/src/avatar/Head.h | 5 +++++ interface/src/avatar/MyAvatar.cpp | 6 ++++++ 6 files changed, 39 insertions(+), 4 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index ca264fd42f..01323d3e1d 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2248,6 +2248,15 @@ void Application::shrinkMirrorView() { const float HEAD_SPHERE_RADIUS = 0.1f; +bool Application::isLookingAtMyAvatar(Avatar* avatar) { + glm::vec3 theirLookAt = avatar->getHead()->getLookAtPosition(); + glm::vec3 myEyePosition = _myAvatar->getHead()->getEyePosition(); + if (pointInSphere(theirLookAt, myEyePosition, HEAD_SPHERE_RADIUS * _myAvatar->getScale())) { + return true; + } + return false; +} + void Application::updateLOD() { PerformanceTimer perfTimer("LOD"); // adjust it unless we were asked to disable this feature, or if we're currently in throttleRendering mode diff --git a/interface/src/Application.h b/interface/src/Application.h index 1d3c0dcc70..8dd987fbcd 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -322,6 +322,8 @@ public: QStringList getRunningScripts() { return _scriptEnginesHash.keys(); } ScriptEngine* getScriptEngine(QString scriptHash) { return _scriptEnginesHash.contains(scriptHash) ? _scriptEnginesHash[scriptHash] : NULL; } + bool isLookingAtMyAvatar(Avatar* avatar); + float getRenderResolutionScale() const; int getRenderAmbientLight() const; diff --git a/interface/src/avatar/FaceModel.cpp b/interface/src/avatar/FaceModel.cpp index 7a582406a4..1501c52de5 100644 --- a/interface/src/avatar/FaceModel.cpp +++ b/interface/src/avatar/FaceModel.cpp @@ -77,7 +77,7 @@ void FaceModel::maybeUpdateEyeRotation(Model* model, const JointState& parentSta glm::translate(state.getDefaultTranslationInConstrainedFrame()) * joint.preTransform * glm::mat4_cast(joint.preRotation * joint.rotation)); glm::vec3 front = glm::vec3(inverse * glm::vec4(_owningHead->getFinalOrientationInWorldFrame() * IDENTITY_FRONT, 0.0f)); - glm::vec3 lookAt = glm::vec3(inverse * glm::vec4(_owningHead->getLookAtPosition() + + glm::vec3 lookAt = glm::vec3(inverse * glm::vec4(_owningHead->getCorrectedLookAtPosition() + _owningHead->getSaccade() - model->getTranslation(), 1.0f)); glm::quat between = rotationBetween(front, lookAt); const float MAX_ANGLE = 30.0f * RADIANS_PER_DEGREE; diff --git a/interface/src/avatar/Head.cpp b/interface/src/avatar/Head.cpp index 911bd4f4a4..e5201a8923 100644 --- a/interface/src/avatar/Head.cpp +++ b/interface/src/avatar/Head.cpp @@ -258,7 +258,7 @@ void Head::calculateMouthShapes() { void Head::applyEyelidOffset(glm::quat headOrientation) { // Adjusts the eyelid blendshape coefficients so that the eyelid follows the iris as the head pitches. - glm::quat eyeRotation = rotationBetween(headOrientation * IDENTITY_FRONT, getLookAtPosition() - _eyePosition); + glm::quat eyeRotation = rotationBetween(headOrientation * IDENTITY_FRONT, getCorrectedLookAtPosition() - _eyePosition); eyeRotation = eyeRotation * glm::angleAxis(safeEulerAngles(headOrientation).y, IDENTITY_UP); // Rotation w.r.t. head float eyePitch = safeEulerAngles(eyeRotation).x; @@ -295,8 +295,8 @@ void Head::relaxLean(float deltaTime) { } void Head::render(RenderArgs* renderArgs, float alpha, ViewFrustum* renderFrustum, bool postLighting) { - if (_renderLookatVectors) { - renderLookatVectors(renderArgs, _leftEyePosition, _rightEyePosition, getLookAtPosition()); + if (_renderLookatVectors && _isLookingAtMe) { + renderLookatVectors(renderArgs, _leftEyePosition, _rightEyePosition, getCorrectedLookAtPosition()); } } @@ -315,6 +315,19 @@ glm::quat Head::getFinalOrientationInLocalFrame() const { return glm::quat(glm::radians(glm::vec3(getFinalPitch(), getFinalYaw(), getFinalRoll() ))); } +glm::vec3 Head::getCorrectedLookAtPosition() { + if (_isLookingAtMe) { + return _correctedLookAtPosition; + } else { + return getLookAtPosition(); + } +} + +void Head::setCorrectedLookAtPosition(glm::vec3 correctedLookAtPosition) { + _isLookingAtMe = true; + _correctedLookAtPosition = correctedLookAtPosition; +} + glm::quat Head::getCameraOrientation() const { // NOTE: Head::getCameraOrientation() is not used for orienting the camera "view" while in Oculus mode, so // you may wonder why this code is here. This method will be called while in Oculus mode to determine how diff --git a/interface/src/avatar/Head.h b/interface/src/avatar/Head.h index a053a5bd44..3f839d53bc 100644 --- a/interface/src/avatar/Head.h +++ b/interface/src/avatar/Head.h @@ -51,6 +51,9 @@ public: /// \return orientationBody * orientationBasePitch glm::quat getCameraOrientation () const; + void setCorrectedLookAtPosition(glm::vec3 correctedLookAtPosition); + glm::vec3 getCorrectedLookAtPosition(); + void clearCorrectedLookAtPosition() { _isLookingAtMe = false; } bool getIsLookingAtMe() { return _isLookingAtMe; } float getScale() const { return _scale; } @@ -140,6 +143,8 @@ private: bool _isLookingAtMe; FaceModel _faceModel; + glm::vec3 _correctedLookAtPosition; + int _leftEyeLookAtID; int _rightEyeLookAtID; diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 03b8a56526..9c46edc2d8 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -892,6 +892,12 @@ void MyAvatar::updateLookAtTargetAvatar() { _targetAvatarPosition = avatarPointer->getPosition(); smallestAngleTo = angleTo; } + if (Application::getInstance()->isLookingAtMyAvatar(avatar)) { + // Alter their gaze to look directly at my camera; this looks more natural than looking at my avatar's face + avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getViewFrustum()->getPosition()); + } else { + avatar->getHead()->clearCorrectedLookAtPosition(); + } } } auto avatarPointer = _lookAtTargetAvatar.lock(); From 9efeda9716d68513cb26e1b203a3cae184577ada Mon Sep 17 00:00:00 2001 From: David Rowe Date: Thu, 2 Jul 2015 15:43:23 -0700 Subject: [PATCH 09/30] Adjust gaze target for someone looking at me --- interface/src/Application.cpp | 3 +-- interface/src/avatar/MyAvatar.cpp | 22 +++++++++++++++++++--- interface/src/devices/OculusManager.cpp | 1 + interface/src/devices/OculusManager.h | 1 + 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 01323d3e1d..1a39999301 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2306,8 +2306,7 @@ void Application::updateMyAvatarLookAtPosition() { if (!OculusManager::isConnected()) { lookAtSpot = _myCamera.getPosition(); } else { - lookAtSpot = _myCamera.getPosition() - + (OculusManager::getLeftEyePosition() + OculusManager::getRightEyePosition()) / 2.0f; + lookAtSpot = _myCamera.getPosition() + OculusManager::getMidEyePosition(); } } else { AvatarSharedPointer lookingAt = _myAvatar->getLookAtTargetAvatar().lock(); diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 9c46edc2d8..c3c8ddcd25 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -34,6 +34,9 @@ #include #include +#include "devices/Faceshift.h" +#include "devices/OculusManager.h" + #include "Application.h" #include "AvatarManager.h" #include "Environment.h" @@ -42,7 +45,6 @@ #include "MyAvatar.h" #include "Physics.h" #include "Recorder.h" -#include "devices/Faceshift.h" #include "Util.h" #include "InterfaceLogging.h" @@ -893,8 +895,22 @@ void MyAvatar::updateLookAtTargetAvatar() { smallestAngleTo = angleTo; } if (Application::getInstance()->isLookingAtMyAvatar(avatar)) { - // Alter their gaze to look directly at my camera; this looks more natural than looking at my avatar's face - avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getViewFrustum()->getPosition()); + // Alter their gaze to look directly at my camera; this looks more natural than looking at my avatar's face. + // Offset their gaze according to whether they're looking at one of my eyes or my mouth. + glm::vec3 gazeOffset = avatar->getHead()->getLookAtPosition() - getHead()->getEyePosition(); + const float HUMAN_EYE_SEPARATION = 0.065f; + float myEyeSeparation = glm::length(getHead()->getLeftEyePosition() - getHead()->getRightEyePosition()); + gazeOffset = gazeOffset * HUMAN_EYE_SEPARATION / myEyeSeparation; + + if (Application::getInstance()->isHMDMode()) { + //avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getCamera()->getPosition() + // + OculusManager::getMidEyePosition() + gazeOffset); + avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getViewFrustum()->getPosition() + + OculusManager::getMidEyePosition() + gazeOffset); + } else { + avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getViewFrustum()->getPosition() + + gazeOffset); + } } else { avatar->getHead()->clearCorrectedLookAtPosition(); } diff --git a/interface/src/devices/OculusManager.cpp b/interface/src/devices/OculusManager.cpp index e5c00a3699..eb19031156 100644 --- a/interface/src/devices/OculusManager.cpp +++ b/interface/src/devices/OculusManager.cpp @@ -283,6 +283,7 @@ static ovrVector3f _eyeOffsets[ovrEye_Count]; glm::vec3 OculusManager::getLeftEyePosition() { return _eyePositions[ovrEye_Left]; } glm::vec3 OculusManager::getRightEyePosition() { return _eyePositions[ovrEye_Right]; } +glm::vec3 OculusManager::getMidEyePosition() { return (_eyePositions[ovrEye_Left] + _eyePositions[ovrEye_Right]) / 2.0f; } void OculusManager::connect(QOpenGLContext* shareContext) { qCDebug(interfaceapp) << "Oculus SDK" << OVR_VERSION_STRING; diff --git a/interface/src/devices/OculusManager.h b/interface/src/devices/OculusManager.h index 9d9f091296..83ecbf0fb7 100644 --- a/interface/src/devices/OculusManager.h +++ b/interface/src/devices/OculusManager.h @@ -47,6 +47,7 @@ public: static glm::vec3 getLeftEyePosition(); static glm::vec3 getRightEyePosition(); + static glm::vec3 getMidEyePosition(); static int getHMDScreen(); From 3f38a835d830a0adab8d444190ef1801c4176b65 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Fri, 3 Jul 2015 13:49:10 -0700 Subject: [PATCH 10/30] Fix look-at vectors not rendering if not looking at me --- interface/src/avatar/Head.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/avatar/Head.cpp b/interface/src/avatar/Head.cpp index e5201a8923..98126c3a22 100644 --- a/interface/src/avatar/Head.cpp +++ b/interface/src/avatar/Head.cpp @@ -295,7 +295,7 @@ void Head::relaxLean(float deltaTime) { } void Head::render(RenderArgs* renderArgs, float alpha, ViewFrustum* renderFrustum, bool postLighting) { - if (_renderLookatVectors && _isLookingAtMe) { + if (_renderLookatVectors) { renderLookatVectors(renderArgs, _leftEyePosition, _rightEyePosition, getCorrectedLookAtPosition()); } } From a59fd4401473a9f059c38a25eece73c3da8bc42f Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 7 Jul 2015 09:04:20 -0700 Subject: [PATCH 11/30] quiet some log spam --- libraries/entities/src/EntityTree.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/libraries/entities/src/EntityTree.cpp b/libraries/entities/src/EntityTree.cpp index 3d7fcd8ce5..a951b2bf4f 100644 --- a/libraries/entities/src/EntityTree.cpp +++ b/libraries/entities/src/EntityTree.cpp @@ -92,13 +92,11 @@ void EntityTree::postAddEntity(EntityItemPointer entity) { bool EntityTree::updateEntity(const EntityItemID& entityID, const EntityItemProperties& properties, const SharedNodePointer& senderNode) { EntityTreeElement* containingElement = getContainingElement(entityID); if (!containingElement) { - qCDebug(entities) << "UNEXPECTED!!!! EntityTree::updateEntity() entityID doesn't exist!!! entityID=" << entityID; return false; } EntityItemPointer existingEntity = containingElement->getEntityWithEntityItemID(entityID); if (!existingEntity) { - qCDebug(entities) << "UNEXPECTED!!!! don't call updateEntity() on entity items that don't exist. entityID=" << entityID; return false; } @@ -108,8 +106,6 @@ bool EntityTree::updateEntity(const EntityItemID& entityID, const EntityItemProp bool EntityTree::updateEntity(EntityItemPointer entity, const EntityItemProperties& properties, const SharedNodePointer& senderNode) { EntityTreeElement* containingElement = getContainingElement(entity->getEntityItemID()); if (!containingElement) { - qCDebug(entities) << "UNEXPECTED!!!! EntityTree::updateEntity() entity-->element lookup failed!!! entityID=" - << entity->getEntityItemID(); return false; } return updateEntityWithElement(entity, properties, containingElement, senderNode); From 0a389a9daa628dbd2d28b9c8139da5b25920a91d Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Tue, 7 Jul 2015 11:11:52 -0700 Subject: [PATCH 12/30] Updating oglplus version and switching to url based dist --- cmake/externals/oglplus/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/externals/oglplus/CMakeLists.txt b/cmake/externals/oglplus/CMakeLists.txt index a0b91739f6..43730129a0 100644 --- a/cmake/externals/oglplus/CMakeLists.txt +++ b/cmake/externals/oglplus/CMakeLists.txt @@ -4,8 +4,8 @@ string(TOUPPER ${EXTERNAL_NAME} EXTERNAL_NAME_UPPER) include(ExternalProject) ExternalProject_Add( ${EXTERNAL_NAME} - GIT_REPOSITORY https://github.com/matus-chochlik/oglplus.git - GIT_TAG a2681383928b1166f176512cbe0f95e96fe68d08 + URL http://iweb.dl.sourceforge.net/project/oglplus/oglplus-0.63.x/oglplus-0.63.0.zip + URL_MD5 de984ab245b185b45c87415c0e052135 CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" From 1c2972bd7c5f2b0a037c4b9c033135168523566f Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Tue, 7 Jul 2015 11:49:55 -0700 Subject: [PATCH 13/30] Expose avatar collisions to scripts, and include velocityChange in exposed collision data. --- interface/src/avatar/AvatarManager.cpp | 2 +- interface/src/avatar/MyAvatar.h | 1 + libraries/shared/src/RegisteredMetaTypes.cpp | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/interface/src/avatar/AvatarManager.cpp b/interface/src/avatar/AvatarManager.cpp index dbd46cbfbd..944f16fd34 100644 --- a/interface/src/avatar/AvatarManager.cpp +++ b/interface/src/avatar/AvatarManager.cpp @@ -256,7 +256,6 @@ void AvatarManager::handleOutgoingChanges(VectorOfMotionStates& motionStates) { } void AvatarManager::handleCollisionEvents(CollisionEvents& collisionEvents) { - // TODO: expose avatar collision events to JS for (Collision collision : collisionEvents) { // TODO: Current physics uses null idA or idB for non-entities. The plan is to handle MOTIONSTATE_TYPE_AVATAR, // and then MOTIONSTATE_TYPE_MYAVATAR. As it is, this code only covers the case of my avatar (in which case one @@ -285,6 +284,7 @@ void AvatarManager::handleCollisionEvents(CollisionEvents& collisionEvents) { const float AVATAR_STRETCH_FACTOR = 1.0f; AudioInjector::playSound(collisionSoundURL, energyFactorOfFull, AVATAR_STRETCH_FACTOR, myAvatar->getPosition()); + myAvatar->collisionWithEntity(collision); } } } diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index 8410665ed5..f77eb81060 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -209,6 +209,7 @@ public slots: signals: void transformChanged(); void newCollisionSoundURL(const QUrl& url); + void collisionWithEntity(const Collision& collision); private: diff --git a/libraries/shared/src/RegisteredMetaTypes.cpp b/libraries/shared/src/RegisteredMetaTypes.cpp index 62f2be0512..dce31b2971 100644 --- a/libraries/shared/src/RegisteredMetaTypes.cpp +++ b/libraries/shared/src/RegisteredMetaTypes.cpp @@ -229,6 +229,7 @@ QScriptValue collisionToScriptValue(QScriptEngine* engine, const Collision& coll obj.setProperty("idB", quuidToScriptValue(engine, collision.idB)); obj.setProperty("penetration", vec3toScriptValue(engine, collision.penetration)); obj.setProperty("contactPoint", vec3toScriptValue(engine, collision.contactPoint)); + obj.setProperty("velocityChange", vec3toScriptValue(engine, collision.velocityChange)); return obj; } From 11e52c8ae034bfc02390ca213a8ba075b93c6451 Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Tue, 7 Jul 2015 14:41:03 -0700 Subject: [PATCH 14/30] Baseline versions of fight club scripts. --- examples/example/games/make-dummy.js | 70 ++++++++++++++++++ examples/example/games/sword.js | 104 +++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 examples/example/games/make-dummy.js create mode 100644 examples/example/games/sword.js diff --git a/examples/example/games/make-dummy.js b/examples/example/games/make-dummy.js new file mode 100644 index 0000000000..068a8b7f9a --- /dev/null +++ b/examples/example/games/make-dummy.js @@ -0,0 +1,70 @@ +// +// make-dummy.js +// examples +// +// Created by Seth Alves on 2015-6-10 +// Copyright 2015 High Fidelity, Inc. +// +// Makes a boxing-dummy that responds to collisions. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +// +"use strict"; +/*jslint vars: true*/ +var Overlays, Entities, Controller, Script, MyAvatar, Vec3; // Referenced globals provided by High Fidelity. + +var HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; + +var rezButton = Overlays.addOverlay("image", { + x: 100, + y: 350, + width: 32, + height: 32, + imageURL: HIFI_PUBLIC_BUCKET + "images/close.png", + color: { + red: 255, + green: 255, + blue: 255 + }, + alpha: 1 +}); + + +function mousePressEvent(event) { + var clickedOverlay = Overlays.getOverlayAtPoint({ + x: event.x, + y: event.y + }); + + if (clickedOverlay === rezButton) { + var boxId; + + var position = Vec3.sum(MyAvatar.position, {x: 1.0, y: 0.4, z: 0.0}); + boxId = Entities.addEntity({ + type: "Box", + name: "dummy", + position: position, + dimensions: {x: 0.3, y: 0.7, z: 0.3}, + gravity: {x: 0.0, y: -3.0, z: 0.0}, + damping: 0.2, + collisionsWillMove: true + }); + + var pointToOffsetFrom = Vec3.sum(position, {x: 0.0, y: 2.0, z: 0.0}); + Entities.addAction("offset", boxId, {pointToOffsetFrom: pointToOffsetFrom, + linearDistance: 2.0, + // linearTimeScale: 0.005 + linearTimeScale: 0.1 + }); + } +} + + +function scriptEnding() { + Overlays.deleteOverlay(rezButton); +} + +Controller.mousePressEvent.connect(mousePressEvent); +Script.scriptEnding.connect(scriptEnding); diff --git a/examples/example/games/sword.js b/examples/example/games/sword.js new file mode 100644 index 0000000000..9592c275f6 --- /dev/null +++ b/examples/example/games/sword.js @@ -0,0 +1,104 @@ +// stick.js +// examples +// +// Created by Seth Alves on 2015-6-10 +// Copyright 2015 High Fidelity, Inc. +// +// Allow avatar to hold a stick +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +"use strict"; +/*jslint vars: true*/ +var Script, Entities, MyAvatar, Window, Controller, Vec3, Quat; // Referenced globals provided by High Fidelity. + +var hand = "right"; +var nullActionID = "00000000-0000-0000-0000-000000000000"; +var controllerID; +var controllerActive; +var stickID = null; +var actionID = nullActionID; +var dimensions = { x: 0.3, y: 0.1, z: 2.0 }; + +var stickModel = "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx"; +var swordModel = "https://hifi-public.s3.amazonaws.com/ozan/props/sword/sword.fbx"; +var whichModel = "sword"; + +// sometimes if this is run immediately the stick doesn't get created? use a timer. +Script.setTimeout(function () { + stickID = Entities.addEntity({ + type: "Model", + modelURL: (whichModel === "sword") ? swordModel : stickModel, + //compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj", + shapeType: "box", + dimensions: dimensions, + position: MyAvatar.getRightPalmPosition(), // initial position doesn't matter, as long as it's close + rotation: MyAvatar.orientation, + damping: 0.1, + collisionSoundURL: "http://public.highfidelity.io/sounds/Collisions-hitsandslaps/swordStrike1.wav", + restitution: 0.01, + collisionsWillMove: true + }); + actionID = Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -dimensions.z / 2}, + hand: hand, + timeScale: 0.15}); +}, 3000); + + +function cleanUp() { + Entities.deleteEntity(stickID); +} + + +function positionStick(stickOrientation) { + var baseOffset = {x: 0.0, y: 0.0, z: -dimensions.z / 2}; + var offset = Vec3.multiplyQbyV(stickOrientation, baseOffset); + Entities.updateAction(stickID, actionID, {relativePosition: offset, + relativeRotation: stickOrientation}); +} + + +function mouseMoveEvent(event) { + if (!stickID || actionID === nullActionID) { + return; + } + var windowCenterX = Window.innerWidth / 2; + var windowCenterY = Window.innerHeight / 2; + var mouseXCenterOffset = event.x - windowCenterX; + var mouseYCenterOffset = event.y - windowCenterY; + var mouseXRatio = mouseXCenterOffset / windowCenterX; + var mouseYRatio = mouseYCenterOffset / windowCenterY; + + var stickOrientation = Quat.fromPitchYawRollDegrees(mouseYRatio * -90, mouseXRatio * -90, 0); + positionStick(stickOrientation); +} + + +function initControls() { + if (hand === "right") { + controllerID = 3; // right handed + } else { + controllerID = 4; // left handed + } +} + + +function update() { + var palmPosition = Controller.getSpatialControlPosition(controllerID); + controllerActive = (Vec3.length(palmPosition) > 0); + if (!controllerActive) { + return; + } + + var stickOrientation = Controller.getSpatialControlRawRotation(controllerID); + var adjustment = Quat.fromPitchYawRollDegrees(180, 0, 0); + stickOrientation = Quat.multiply(stickOrientation, adjustment); + + positionStick(stickOrientation); +} + + +Script.scriptEnding.connect(cleanUp); +Controller.mouseMoveEvent.connect(mouseMoveEvent); +Script.update.connect(update); From 77bd7b2821162fa9c5602c7eb6bbdbbf7cb6e816 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Tue, 7 Jul 2015 15:56:16 -0700 Subject: [PATCH 15/30] some ObjectAction methods should be const --- assignment-client/src/AssignmentAction.cpp | 2 +- assignment-client/src/AssignmentAction.h | 4 ++-- interface/src/avatar/AvatarActionHold.cpp | 5 ++--- interface/src/avatar/AvatarActionHold.h | 2 +- libraries/entities/src/EntityActionInterface.h | 4 ++-- libraries/physics/src/ObjectAction.cpp | 8 ++++---- libraries/physics/src/ObjectAction.h | 4 ++-- libraries/physics/src/ObjectActionOffset.cpp | 2 +- libraries/physics/src/ObjectActionOffset.h | 4 ++-- libraries/physics/src/ObjectActionSpring.cpp | 2 +- libraries/physics/src/ObjectActionSpring.h | 4 ++-- 11 files changed, 20 insertions(+), 21 deletions(-) diff --git a/assignment-client/src/AssignmentAction.cpp b/assignment-client/src/AssignmentAction.cpp index 6cb3c06312..8be00f53bd 100644 --- a/assignment-client/src/AssignmentAction.cpp +++ b/assignment-client/src/AssignmentAction.cpp @@ -28,7 +28,7 @@ void AssignmentAction::removeFromSimulation(EntitySimulation* simulation) const simulation->removeAction(_id); } -QByteArray AssignmentAction::serialize() { +QByteArray AssignmentAction::serialize() const { return _data; } diff --git a/assignment-client/src/AssignmentAction.h b/assignment-client/src/AssignmentAction.h index cd72c1f277..cccecd5da5 100644 --- a/assignment-client/src/AssignmentAction.h +++ b/assignment-client/src/AssignmentAction.h @@ -25,14 +25,14 @@ public: virtual ~AssignmentAction(); const QUuid& getID() const { return _id; } - virtual EntityActionType getType() { return _type; } + virtual EntityActionType getType() const { return _type; } virtual void removeFromSimulation(EntitySimulation* simulation) const; virtual EntityItemWeakPointer getOwnerEntity() const { return _ownerEntity; } virtual void setOwnerEntity(const EntityItemPointer ownerEntity) { _ownerEntity = ownerEntity; } virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); - virtual QByteArray serialize(); + virtual QByteArray serialize() const; virtual void deserialize(QByteArray serializedArguments); private: diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 918521c0da..85e7067611 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -166,8 +166,7 @@ QVariantMap AvatarActionHold::getArguments() { void AvatarActionHold::deserialize(QByteArray serializedArguments) { - if (_mine) { - return; + if (!_mine) { + ObjectActionSpring::deserialize(serializedArguments); } - ObjectActionSpring::deserialize(serializedArguments); } diff --git a/interface/src/avatar/AvatarActionHold.h b/interface/src/avatar/AvatarActionHold.h index a47f1ce05d..e5aa0f1ccf 100644 --- a/interface/src/avatar/AvatarActionHold.h +++ b/interface/src/avatar/AvatarActionHold.h @@ -22,7 +22,7 @@ public: AvatarActionHold(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); virtual ~AvatarActionHold(); - virtual EntityActionType getType() { return ACTION_TYPE_HOLD; } + virtual EntityActionType getType() const { return ACTION_TYPE_HOLD; } virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); diff --git a/libraries/entities/src/EntityActionInterface.h b/libraries/entities/src/EntityActionInterface.h index 5693e1fb6f..a543b65f40 100644 --- a/libraries/entities/src/EntityActionInterface.h +++ b/libraries/entities/src/EntityActionInterface.h @@ -32,7 +32,7 @@ public: EntityActionInterface() { } virtual ~EntityActionInterface() { } virtual const QUuid& getID() const = 0; - virtual EntityActionType getType() { assert(false); return ACTION_TYPE_NONE; } + virtual EntityActionType getType() const { assert(false); return ACTION_TYPE_NONE; } virtual void removeFromSimulation(EntitySimulation* simulation) const = 0; virtual EntityItemWeakPointer getOwnerEntity() const = 0; @@ -40,7 +40,7 @@ public: virtual bool updateArguments(QVariantMap arguments) = 0; virtual QVariantMap getArguments() = 0; - virtual QByteArray serialize() = 0; + virtual QByteArray serialize() const = 0; virtual void deserialize(QByteArray serializedArguments) = 0; static EntityActionType actionTypeFromString(QString actionTypeString); diff --git a/libraries/physics/src/ObjectAction.cpp b/libraries/physics/src/ObjectAction.cpp index ae29fe79d3..2dbfba0413 100644 --- a/libraries/physics/src/ObjectAction.cpp +++ b/libraries/physics/src/ObjectAction.cpp @@ -24,15 +24,15 @@ ObjectAction::~ObjectAction() { } void ObjectAction::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) { - if (!_active) { - return; - } if (_ownerEntity.expired()) { qDebug() << "warning -- action with no entity removing self from btCollisionWorld."; btDynamicsWorld* dynamicsWorld = static_cast(collisionWorld); dynamicsWorld->removeAction(this); return; } + if (!_active) { + return; + } updateActionWorker(deltaTimeStep); } @@ -129,7 +129,7 @@ void ObjectAction::setAngularVelocity(glm::vec3 angularVelocity) { rigidBody->activate(); } -QByteArray ObjectAction::serialize() { +QByteArray ObjectAction::serialize() const { assert(false); return QByteArray(); } diff --git a/libraries/physics/src/ObjectAction.h b/libraries/physics/src/ObjectAction.h index 0e982aaacf..928eb6d3a4 100644 --- a/libraries/physics/src/ObjectAction.h +++ b/libraries/physics/src/ObjectAction.h @@ -30,7 +30,7 @@ public: virtual ~ObjectAction(); const QUuid& getID() const { return _id; } - virtual EntityActionType getType() { assert(false); return ACTION_TYPE_NONE; } + virtual EntityActionType getType() const { assert(false); return ACTION_TYPE_NONE; } virtual void removeFromSimulation(EntitySimulation* simulation) const; virtual EntityItemWeakPointer getOwnerEntity() const { return _ownerEntity; } virtual void setOwnerEntity(const EntityItemPointer ownerEntity) { _ownerEntity = ownerEntity; } @@ -45,7 +45,7 @@ public: virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep); virtual void debugDraw(btIDebugDraw* debugDrawer); - virtual QByteArray serialize(); + virtual QByteArray serialize() const; virtual void deserialize(QByteArray serializedArguments); private: diff --git a/libraries/physics/src/ObjectActionOffset.cpp b/libraries/physics/src/ObjectActionOffset.cpp index 22c6b7e0d3..b03cd43876 100644 --- a/libraries/physics/src/ObjectActionOffset.cpp +++ b/libraries/physics/src/ObjectActionOffset.cpp @@ -127,7 +127,7 @@ QVariantMap ObjectActionOffset::getArguments() { return arguments; } -QByteArray ObjectActionOffset::serialize() { +QByteArray ObjectActionOffset::serialize() const { QByteArray ba; QDataStream dataStream(&ba, QIODevice::WriteOnly); dataStream << getType(); diff --git a/libraries/physics/src/ObjectActionOffset.h b/libraries/physics/src/ObjectActionOffset.h index 28a08c2efe..b58d943b2d 100644 --- a/libraries/physics/src/ObjectActionOffset.h +++ b/libraries/physics/src/ObjectActionOffset.h @@ -22,14 +22,14 @@ public: ObjectActionOffset(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); virtual ~ObjectActionOffset(); - virtual EntityActionType getType() { return ACTION_TYPE_OFFSET; } + virtual EntityActionType getType() const { return ACTION_TYPE_OFFSET; } virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); virtual void updateActionWorker(float deltaTimeStep); - virtual QByteArray serialize(); + virtual QByteArray serialize() const; virtual void deserialize(QByteArray serializedArguments); private: diff --git a/libraries/physics/src/ObjectActionSpring.cpp b/libraries/physics/src/ObjectActionSpring.cpp index fae593f3eb..e7d841664d 100644 --- a/libraries/physics/src/ObjectActionSpring.cpp +++ b/libraries/physics/src/ObjectActionSpring.cpp @@ -206,7 +206,7 @@ QVariantMap ObjectActionSpring::getArguments() { return arguments; } -QByteArray ObjectActionSpring::serialize() { +QByteArray ObjectActionSpring::serialize() const { QByteArray serializedActionArguments; QDataStream dataStream(&serializedActionArguments, QIODevice::WriteOnly); diff --git a/libraries/physics/src/ObjectActionSpring.h b/libraries/physics/src/ObjectActionSpring.h index c887a046bb..d45bc78f10 100644 --- a/libraries/physics/src/ObjectActionSpring.h +++ b/libraries/physics/src/ObjectActionSpring.h @@ -22,14 +22,14 @@ public: ObjectActionSpring(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); virtual ~ObjectActionSpring(); - virtual EntityActionType getType() { return ACTION_TYPE_SPRING; } + virtual EntityActionType getType() const { return ACTION_TYPE_SPRING; } virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); virtual void updateActionWorker(float deltaTimeStep); - virtual QByteArray serialize(); + virtual QByteArray serialize() const; virtual void deserialize(QByteArray serializedArguments); protected: From e9df8cacc754f7973da2906444977e4344de735a Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Tue, 7 Jul 2015 16:17:37 -0700 Subject: [PATCH 16/30] make some arguments const, cleanup some #includes --- assignment-client/src/AssignmentAction.cpp | 2 +- assignment-client/src/AssignmentAction.h | 2 +- assignment-client/src/AssignmentActionFactory.cpp | 4 ++-- assignment-client/src/AssignmentActionFactory.h | 2 +- interface/src/InterfaceActionFactory.cpp | 4 ++-- interface/src/InterfaceActionFactory.h | 2 +- interface/src/avatar/AvatarActionHold.cpp | 2 +- interface/src/avatar/AvatarActionHold.h | 2 +- libraries/entities/src/EntityActionFactoryInterface.h | 2 +- libraries/physics/src/ObjectAction.cpp | 2 +- libraries/physics/src/ObjectAction.h | 4 +--- libraries/physics/src/ObjectActionOffset.cpp | 2 +- libraries/physics/src/ObjectActionOffset.h | 2 +- libraries/physics/src/ObjectActionSpring.cpp | 2 +- libraries/physics/src/ObjectActionSpring.h | 5 +---- libraries/shared/src/QVariantGLM.cpp | 8 ++++---- libraries/shared/src/QVariantGLM.h | 8 ++++---- 17 files changed, 25 insertions(+), 30 deletions(-) diff --git a/assignment-client/src/AssignmentAction.cpp b/assignment-client/src/AssignmentAction.cpp index 8be00f53bd..58a6eda473 100644 --- a/assignment-client/src/AssignmentAction.cpp +++ b/assignment-client/src/AssignmentAction.cpp @@ -13,7 +13,7 @@ #include "AssignmentAction.h" -AssignmentAction::AssignmentAction(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) : +AssignmentAction::AssignmentAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : _id(id), _type(type), _data(QByteArray()), diff --git a/assignment-client/src/AssignmentAction.h b/assignment-client/src/AssignmentAction.h index cccecd5da5..77ccf52032 100644 --- a/assignment-client/src/AssignmentAction.h +++ b/assignment-client/src/AssignmentAction.h @@ -21,7 +21,7 @@ class AssignmentAction : public EntityActionInterface { public: - AssignmentAction(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); + AssignmentAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~AssignmentAction(); const QUuid& getID() const { return _id; } diff --git a/assignment-client/src/AssignmentActionFactory.cpp b/assignment-client/src/AssignmentActionFactory.cpp index ba2692c611..e1c5d3adff 100644 --- a/assignment-client/src/AssignmentActionFactory.cpp +++ b/assignment-client/src/AssignmentActionFactory.cpp @@ -12,14 +12,14 @@ #include "AssignmentActionFactory.h" -EntityActionPointer assignmentActionFactory(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) { +EntityActionPointer assignmentActionFactory(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) { return (EntityActionPointer) new AssignmentAction(type, id, ownerEntity); } EntityActionPointer AssignmentActionFactory::factory(EntitySimulation* simulation, EntityActionType type, - QUuid id, + const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments) { EntityActionPointer action = assignmentActionFactory(type, id, ownerEntity); diff --git a/assignment-client/src/AssignmentActionFactory.h b/assignment-client/src/AssignmentActionFactory.h index f71d22c0dd..41245dac68 100644 --- a/assignment-client/src/AssignmentActionFactory.h +++ b/assignment-client/src/AssignmentActionFactory.h @@ -21,7 +21,7 @@ public: virtual ~AssignmentActionFactory() { } virtual EntityActionPointer factory(EntitySimulation* simulation, EntityActionType type, - QUuid id, + const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments); virtual EntityActionPointer factoryBA(EntitySimulation* simulation, diff --git a/interface/src/InterfaceActionFactory.cpp b/interface/src/InterfaceActionFactory.cpp index ccff5b4dc6..b721b10af3 100644 --- a/interface/src/InterfaceActionFactory.cpp +++ b/interface/src/InterfaceActionFactory.cpp @@ -18,7 +18,7 @@ #include "InterfaceActionFactory.h" -EntityActionPointer interfaceActionFactory(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) { +EntityActionPointer interfaceActionFactory(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) { switch (type) { case ACTION_TYPE_NONE: return nullptr; @@ -37,7 +37,7 @@ EntityActionPointer interfaceActionFactory(EntityActionType type, QUuid id, Enti EntityActionPointer InterfaceActionFactory::factory(EntitySimulation* simulation, EntityActionType type, - QUuid id, + const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments) { EntityActionPointer action = interfaceActionFactory(type, id, ownerEntity); diff --git a/interface/src/InterfaceActionFactory.h b/interface/src/InterfaceActionFactory.h index 944e2fb753..004c24163f 100644 --- a/interface/src/InterfaceActionFactory.h +++ b/interface/src/InterfaceActionFactory.h @@ -20,7 +20,7 @@ public: virtual ~InterfaceActionFactory() { } virtual EntityActionPointer factory(EntitySimulation* simulation, EntityActionType type, - QUuid id, + const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments); virtual EntityActionPointer factoryBA(EntitySimulation* simulation, diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 85e7067611..b8c7621094 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -17,7 +17,7 @@ const uint16_t AvatarActionHold::holdVersion = 1; -AvatarActionHold::AvatarActionHold(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) : +AvatarActionHold::AvatarActionHold(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : ObjectActionSpring(type, id, ownerEntity), _relativePosition(glm::vec3(0.0f)), _relativeRotation(glm::quat()), diff --git a/interface/src/avatar/AvatarActionHold.h b/interface/src/avatar/AvatarActionHold.h index e5aa0f1ccf..970fb75c0d 100644 --- a/interface/src/avatar/AvatarActionHold.h +++ b/interface/src/avatar/AvatarActionHold.h @@ -19,7 +19,7 @@ class AvatarActionHold : public ObjectActionSpring { public: - AvatarActionHold(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); + AvatarActionHold(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~AvatarActionHold(); virtual EntityActionType getType() const { return ACTION_TYPE_HOLD; } diff --git a/libraries/entities/src/EntityActionFactoryInterface.h b/libraries/entities/src/EntityActionFactoryInterface.h index 5269405d55..9f4056cdff 100644 --- a/libraries/entities/src/EntityActionFactoryInterface.h +++ b/libraries/entities/src/EntityActionFactoryInterface.h @@ -25,7 +25,7 @@ class EntityActionFactoryInterface : public QObject, public Dependency { virtual ~EntityActionFactoryInterface() { } virtual EntityActionPointer factory(EntitySimulation* simulation, EntityActionType type, - QUuid id, + const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments) { assert(false); return nullptr; } virtual EntityActionPointer factoryBA(EntitySimulation* simulation, diff --git a/libraries/physics/src/ObjectAction.cpp b/libraries/physics/src/ObjectAction.cpp index 2dbfba0413..3c38b084aa 100644 --- a/libraries/physics/src/ObjectAction.cpp +++ b/libraries/physics/src/ObjectAction.cpp @@ -13,7 +13,7 @@ #include "ObjectAction.h" -ObjectAction::ObjectAction(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) : +ObjectAction::ObjectAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : btActionInterface(), _id(id), _active(false), diff --git a/libraries/physics/src/ObjectAction.h b/libraries/physics/src/ObjectAction.h index 928eb6d3a4..1fb3ea9b10 100644 --- a/libraries/physics/src/ObjectAction.h +++ b/libraries/physics/src/ObjectAction.h @@ -17,8 +17,6 @@ #include -#include - #include "ObjectMotionState.h" #include "BulletUtil.h" #include "EntityActionInterface.h" @@ -26,7 +24,7 @@ class ObjectAction : public btActionInterface, public EntityActionInterface { public: - ObjectAction(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); + ObjectAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~ObjectAction(); const QUuid& getID() const { return _id; } diff --git a/libraries/physics/src/ObjectActionOffset.cpp b/libraries/physics/src/ObjectActionOffset.cpp index b03cd43876..90e148971a 100644 --- a/libraries/physics/src/ObjectActionOffset.cpp +++ b/libraries/physics/src/ObjectActionOffset.cpp @@ -15,7 +15,7 @@ const uint16_t ObjectActionOffset::offsetVersion = 1; -ObjectActionOffset::ObjectActionOffset(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) : +ObjectActionOffset::ObjectActionOffset(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : ObjectAction(type, id, ownerEntity) { #if WANT_DEBUG qDebug() << "ObjectActionOffset::ObjectActionOffset"; diff --git a/libraries/physics/src/ObjectActionOffset.h b/libraries/physics/src/ObjectActionOffset.h index b58d943b2d..ef79c8eb6b 100644 --- a/libraries/physics/src/ObjectActionOffset.h +++ b/libraries/physics/src/ObjectActionOffset.h @@ -19,7 +19,7 @@ class ObjectActionOffset : public ObjectAction { public: - ObjectActionOffset(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); + ObjectActionOffset(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~ObjectActionOffset(); virtual EntityActionType getType() const { return ACTION_TYPE_OFFSET; } diff --git a/libraries/physics/src/ObjectActionSpring.cpp b/libraries/physics/src/ObjectActionSpring.cpp index e7d841664d..8e55b8b857 100644 --- a/libraries/physics/src/ObjectActionSpring.cpp +++ b/libraries/physics/src/ObjectActionSpring.cpp @@ -17,7 +17,7 @@ const float SPRING_MAX_SPEED = 10.0f; const uint16_t ObjectActionSpring::springVersion = 1; -ObjectActionSpring::ObjectActionSpring(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) : +ObjectActionSpring::ObjectActionSpring(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : ObjectAction(type, id, ownerEntity), _positionalTarget(glm::vec3(0.0f)), _linearTimeScale(0.2f), diff --git a/libraries/physics/src/ObjectActionSpring.h b/libraries/physics/src/ObjectActionSpring.h index d45bc78f10..6c576e4545 100644 --- a/libraries/physics/src/ObjectActionSpring.h +++ b/libraries/physics/src/ObjectActionSpring.h @@ -12,14 +12,11 @@ #ifndef hifi_ObjectActionSpring_h #define hifi_ObjectActionSpring_h -#include - -#include #include "ObjectAction.h" class ObjectActionSpring : public ObjectAction { public: - ObjectActionSpring(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); + ObjectActionSpring(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~ObjectActionSpring(); virtual EntityActionType getType() const { return ACTION_TYPE_SPRING; } diff --git a/libraries/shared/src/QVariantGLM.cpp b/libraries/shared/src/QVariantGLM.cpp index 7cebacee8e..7a3ab92cca 100644 --- a/libraries/shared/src/QVariantGLM.cpp +++ b/libraries/shared/src/QVariantGLM.cpp @@ -20,7 +20,7 @@ QVariantList glmToQList(const glm::quat& g) { return QVariantList() << g.x << g.y << g.z << g.w; } -QVariantList rgbColorToQList(rgbColor& v) { +QVariantList rgbColorToQList(const rgbColor& v) { return QVariantList() << (int)(v[0]) << (int)(v[1]) << (int)(v[2]); } @@ -42,12 +42,12 @@ QVariantMap glmToQMap(const glm::quat& glmQuat) { } -glm::vec3 qListToGlmVec3(const QVariant q) { +glm::vec3 qListToGlmVec3(const QVariant& q) { QVariantList qList = q.toList(); return glm::vec3(qList[RED_INDEX].toFloat(), qList[GREEN_INDEX].toFloat(), qList[BLUE_INDEX].toFloat()); } -glm::quat qListToGlmQuat(const QVariant q) { +glm::quat qListToGlmQuat(const QVariant& q) { QVariantList qList = q.toList(); float x = qList[0].toFloat(); float y = qList[1].toFloat(); @@ -56,7 +56,7 @@ glm::quat qListToGlmQuat(const QVariant q) { return glm::quat(w, x, y, z); } -void qListtoRgbColor(const QVariant q, rgbColor returnValue) { +void qListtoRgbColor(const QVariant& q, rgbColor& returnValue) { QVariantList qList = q.toList(); returnValue[RED_INDEX] = qList[RED_INDEX].toInt(); returnValue[GREEN_INDEX] = qList[GREEN_INDEX].toInt(); diff --git a/libraries/shared/src/QVariantGLM.h b/libraries/shared/src/QVariantGLM.h index 922aa7c4aa..3a91110250 100644 --- a/libraries/shared/src/QVariantGLM.h +++ b/libraries/shared/src/QVariantGLM.h @@ -19,11 +19,11 @@ QVariantList glmToQList(const glm::vec3& g); QVariantList glmToQList(const glm::quat& g); -QVariantList rgbColorToQList(rgbColor& v); +QVariantList rgbColorToQList(const rgbColor& v); QVariantMap glmToQMap(const glm::vec3& glmVector); QVariantMap glmToQMap(const glm::quat& glmQuat); -glm::vec3 qListToGlmVec3(const QVariant q); -glm::quat qListToGlmQuat(const QVariant q); -void qListtoRgbColor(const QVariant q, rgbColor returnValue); +glm::vec3 qListToGlmVec3(const QVariant& q); +glm::quat qListToGlmQuat(const QVariant& q); +void qListtoRgbColor(const QVariant& q, rgbColor& returnValue); From a0a1b01ea9f7855f3670ce34591090d150a653c0 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Tue, 7 Jul 2015 17:48:05 -0700 Subject: [PATCH 17/30] coalesce dupe action data members: _id and _type --- assignment-client/src/AssignmentAction.cpp | 3 +-- assignment-client/src/AssignmentAction.h | 4 ---- interface/src/InterfaceActionFactory.cpp | 6 +++--- interface/src/avatar/AvatarActionHold.cpp | 5 +++-- interface/src/avatar/AvatarActionHold.h | 4 +--- .../entities/src/EntityActionInterface.h | 8 +++++--- libraries/physics/src/ObjectAction.cpp | 10 +--------- libraries/physics/src/ObjectAction.h | 20 +++++++++---------- libraries/physics/src/ObjectActionOffset.cpp | 11 +++++----- libraries/physics/src/ObjectActionOffset.h | 4 +--- libraries/physics/src/ObjectActionSpring.cpp | 11 +++++----- libraries/physics/src/ObjectActionSpring.h | 4 +--- 12 files changed, 37 insertions(+), 53 deletions(-) diff --git a/assignment-client/src/AssignmentAction.cpp b/assignment-client/src/AssignmentAction.cpp index 58a6eda473..8b5650ee42 100644 --- a/assignment-client/src/AssignmentAction.cpp +++ b/assignment-client/src/AssignmentAction.cpp @@ -14,8 +14,7 @@ #include "AssignmentAction.h" AssignmentAction::AssignmentAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : - _id(id), - _type(type), + EntityActionInterface(type, id), _data(QByteArray()), _active(false), _ownerEntity(ownerEntity) { diff --git a/assignment-client/src/AssignmentAction.h b/assignment-client/src/AssignmentAction.h index 77ccf52032..23720bd465 100644 --- a/assignment-client/src/AssignmentAction.h +++ b/assignment-client/src/AssignmentAction.h @@ -24,8 +24,6 @@ public: AssignmentAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~AssignmentAction(); - const QUuid& getID() const { return _id; } - virtual EntityActionType getType() const { return _type; } virtual void removeFromSimulation(EntitySimulation* simulation) const; virtual EntityItemWeakPointer getOwnerEntity() const { return _ownerEntity; } virtual void setOwnerEntity(const EntityItemPointer ownerEntity) { _ownerEntity = ownerEntity; } @@ -36,8 +34,6 @@ public: virtual void deserialize(QByteArray serializedArguments); private: - QUuid _id; - EntityActionType _type; QByteArray _data; protected: diff --git a/interface/src/InterfaceActionFactory.cpp b/interface/src/InterfaceActionFactory.cpp index b721b10af3..363fb66e76 100644 --- a/interface/src/InterfaceActionFactory.cpp +++ b/interface/src/InterfaceActionFactory.cpp @@ -23,11 +23,11 @@ EntityActionPointer interfaceActionFactory(EntityActionType type, const QUuid& i case ACTION_TYPE_NONE: return nullptr; case ACTION_TYPE_OFFSET: - return (EntityActionPointer) new ObjectActionOffset(type, id, ownerEntity); + return (EntityActionPointer) new ObjectActionOffset(id, ownerEntity); case ACTION_TYPE_SPRING: - return (EntityActionPointer) new ObjectActionSpring(type, id, ownerEntity); + return (EntityActionPointer) new ObjectActionSpring(id, ownerEntity); case ACTION_TYPE_HOLD: - return (EntityActionPointer) new AvatarActionHold(type, id, ownerEntity); + return (EntityActionPointer) new AvatarActionHold(id, ownerEntity); } assert(false); diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index b8c7621094..ca903a9771 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -17,13 +17,14 @@ const uint16_t AvatarActionHold::holdVersion = 1; -AvatarActionHold::AvatarActionHold(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : - ObjectActionSpring(type, id, ownerEntity), +AvatarActionHold::AvatarActionHold(const QUuid& id, EntityItemPointer ownerEntity) : + ObjectActionSpring(id, ownerEntity), _relativePosition(glm::vec3(0.0f)), _relativeRotation(glm::quat()), _hand("right"), _mine(false) { + _type = ACTION_TYPE_HOLD; #if WANT_DEBUG qDebug() << "AvatarActionHold::AvatarActionHold"; #endif diff --git a/interface/src/avatar/AvatarActionHold.h b/interface/src/avatar/AvatarActionHold.h index 970fb75c0d..3500b5dfa1 100644 --- a/interface/src/avatar/AvatarActionHold.h +++ b/interface/src/avatar/AvatarActionHold.h @@ -19,11 +19,9 @@ class AvatarActionHold : public ObjectActionSpring { public: - AvatarActionHold(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); + AvatarActionHold(const QUuid& id, EntityItemPointer ownerEntity); virtual ~AvatarActionHold(); - virtual EntityActionType getType() const { return ACTION_TYPE_HOLD; } - virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); diff --git a/libraries/entities/src/EntityActionInterface.h b/libraries/entities/src/EntityActionInterface.h index a543b65f40..a4f1c8ea15 100644 --- a/libraries/entities/src/EntityActionInterface.h +++ b/libraries/entities/src/EntityActionInterface.h @@ -29,10 +29,10 @@ enum EntityActionType { class EntityActionInterface { public: - EntityActionInterface() { } + EntityActionInterface(EntityActionType type, const QUuid& id) : _id(id), _type(type) { } virtual ~EntityActionInterface() { } - virtual const QUuid& getID() const = 0; - virtual EntityActionType getType() const { assert(false); return ACTION_TYPE_NONE; } + const QUuid& getID() const { return _id; } + EntityActionType getType() const { return _type; } virtual void removeFromSimulation(EntitySimulation* simulation) const = 0; virtual EntityItemWeakPointer getOwnerEntity() const = 0; @@ -68,6 +68,8 @@ protected: static QString extractStringArgument(QString objectName, QVariantMap arguments, QString argumentName, bool& ok, bool required = true); + QUuid _id; + EntityActionType _type; }; diff --git a/libraries/physics/src/ObjectAction.cpp b/libraries/physics/src/ObjectAction.cpp index 3c38b084aa..101b69f03a 100644 --- a/libraries/physics/src/ObjectAction.cpp +++ b/libraries/physics/src/ObjectAction.cpp @@ -15,7 +15,7 @@ ObjectAction::ObjectAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : btActionInterface(), - _id(id), + EntityActionInterface(type, id), _active(false), _ownerEntity(ownerEntity) { } @@ -129,11 +129,3 @@ void ObjectAction::setAngularVelocity(glm::vec3 angularVelocity) { rigidBody->activate(); } -QByteArray ObjectAction::serialize() const { - assert(false); - return QByteArray(); -} - -void ObjectAction::deserialize(QByteArray serializedArguments) { - assert(false); -} diff --git a/libraries/physics/src/ObjectAction.h b/libraries/physics/src/ObjectAction.h index 1fb3ea9b10..9c7a2c9b4b 100644 --- a/libraries/physics/src/ObjectAction.h +++ b/libraries/physics/src/ObjectAction.h @@ -27,28 +27,22 @@ public: ObjectAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~ObjectAction(); - const QUuid& getID() const { return _id; } - virtual EntityActionType getType() const { assert(false); return ACTION_TYPE_NONE; } virtual void removeFromSimulation(EntitySimulation* simulation) const; virtual EntityItemWeakPointer getOwnerEntity() const { return _ownerEntity; } virtual void setOwnerEntity(const EntityItemPointer ownerEntity) { _ownerEntity = ownerEntity; } - virtual bool updateArguments(QVariantMap arguments) { return false; } - virtual QVariantMap getArguments() { return QVariantMap(); } + virtual bool updateArguments(QVariantMap arguments) = 0; + virtual QVariantMap getArguments() = 0; // this is called from updateAction and should be overridden by subclasses - virtual void updateActionWorker(float deltaTimeStep) {} + virtual void updateActionWorker(float deltaTimeStep) = 0; // these are from btActionInterface virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep); virtual void debugDraw(btIDebugDraw* debugDrawer); - virtual QByteArray serialize() const; - virtual void deserialize(QByteArray serializedArguments); - -private: - QUuid _id; - QReadWriteLock _lock; + virtual QByteArray serialize() const = 0; + virtual void deserialize(QByteArray serializedArguments) = 0; protected: @@ -68,6 +62,10 @@ protected: bool tryLockForWrite() { return _lock.tryLockForWrite(); } void unlock() { _lock.unlock(); } +private: + QReadWriteLock _lock; + +protected: bool _active; EntityItemWeakPointer _ownerEntity; }; diff --git a/libraries/physics/src/ObjectActionOffset.cpp b/libraries/physics/src/ObjectActionOffset.cpp index 90e148971a..7cf35b27d7 100644 --- a/libraries/physics/src/ObjectActionOffset.cpp +++ b/libraries/physics/src/ObjectActionOffset.cpp @@ -15,8 +15,8 @@ const uint16_t ObjectActionOffset::offsetVersion = 1; -ObjectActionOffset::ObjectActionOffset(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : - ObjectAction(type, id, ownerEntity) { +ObjectActionOffset::ObjectActionOffset(const QUuid& id, EntityItemPointer ownerEntity) : + ObjectAction(ACTION_TYPE_OFFSET, id, ownerEntity) { #if WANT_DEBUG qDebug() << "ObjectActionOffset::ObjectActionOffset"; #endif @@ -146,13 +146,14 @@ void ObjectActionOffset::deserialize(QByteArray serializedArguments) { QDataStream dataStream(serializedArguments); EntityActionType type; - QUuid id; - uint16_t serializationVersion; - dataStream >> type; assert(type == getType()); + + QUuid id; dataStream >> id; assert(id == getID()); + + uint16_t serializationVersion; dataStream >> serializationVersion; if (serializationVersion != ObjectActionOffset::offsetVersion) { return; diff --git a/libraries/physics/src/ObjectActionOffset.h b/libraries/physics/src/ObjectActionOffset.h index ef79c8eb6b..1918da6996 100644 --- a/libraries/physics/src/ObjectActionOffset.h +++ b/libraries/physics/src/ObjectActionOffset.h @@ -19,11 +19,9 @@ class ObjectActionOffset : public ObjectAction { public: - ObjectActionOffset(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); + ObjectActionOffset(const QUuid& id, EntityItemPointer ownerEntity); virtual ~ObjectActionOffset(); - virtual EntityActionType getType() const { return ACTION_TYPE_OFFSET; } - virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); diff --git a/libraries/physics/src/ObjectActionSpring.cpp b/libraries/physics/src/ObjectActionSpring.cpp index 8e55b8b857..cb1dd20472 100644 --- a/libraries/physics/src/ObjectActionSpring.cpp +++ b/libraries/physics/src/ObjectActionSpring.cpp @@ -17,8 +17,8 @@ const float SPRING_MAX_SPEED = 10.0f; const uint16_t ObjectActionSpring::springVersion = 1; -ObjectActionSpring::ObjectActionSpring(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : - ObjectAction(type, id, ownerEntity), +ObjectActionSpring::ObjectActionSpring(const QUuid& id, EntityItemPointer ownerEntity) : + ObjectAction(ACTION_TYPE_SPRING, id, ownerEntity), _positionalTarget(glm::vec3(0.0f)), _linearTimeScale(0.2f), _positionalTargetSet(false), @@ -229,13 +229,14 @@ void ObjectActionSpring::deserialize(QByteArray serializedArguments) { QDataStream dataStream(serializedArguments); EntityActionType type; - QUuid id; - uint16_t serializationVersion; - dataStream >> type; assert(type == getType()); + + QUuid id; dataStream >> id; assert(id == getID()); + + uint16_t serializationVersion; dataStream >> serializationVersion; if (serializationVersion != ObjectActionSpring::springVersion) { return; diff --git a/libraries/physics/src/ObjectActionSpring.h b/libraries/physics/src/ObjectActionSpring.h index 6c576e4545..caa64c3d3a 100644 --- a/libraries/physics/src/ObjectActionSpring.h +++ b/libraries/physics/src/ObjectActionSpring.h @@ -16,11 +16,9 @@ class ObjectActionSpring : public ObjectAction { public: - ObjectActionSpring(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); + ObjectActionSpring(const QUuid& id, EntityItemPointer ownerEntity); virtual ~ObjectActionSpring(); - virtual EntityActionType getType() const { return ACTION_TYPE_SPRING; } - virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); From 16e79cc85fae5470545d541596cba65d4df47b09 Mon Sep 17 00:00:00 2001 From: Chris Collins Date: Wed, 8 Jul 2015 11:17:03 -0700 Subject: [PATCH 18/30] Remove lobby.js from the default scripts Lobby.js has some bugs that are not going to be fixed in the short term. Therefore we are removing it from default scripts. --- examples/defaultScripts.js | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/defaultScripts.js b/examples/defaultScripts.js index fb76b62dbe..922047b90c 100644 --- a/examples/defaultScripts.js +++ b/examples/defaultScripts.js @@ -12,7 +12,6 @@ Script.load("progress.js"); Script.load("edit.js"); Script.load("selectAudioDevice.js"); Script.load("inspect.js"); -Script.load("lobby.js"); Script.load("notifications.js"); Script.load("users.js"); Script.load("grab.js"); From 58c32dbf2066ec1e1eb084e8aa6927e64f48b746 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Wed, 8 Jul 2015 11:30:04 -0700 Subject: [PATCH 19/30] remove whitespace from end of lines --- libraries/physics/src/ObjectAction.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/physics/src/ObjectAction.h b/libraries/physics/src/ObjectAction.h index 9c7a2c9b4b..f619657e92 100644 --- a/libraries/physics/src/ObjectAction.h +++ b/libraries/physics/src/ObjectAction.h @@ -65,7 +65,7 @@ protected: private: QReadWriteLock _lock; -protected: +protected: bool _active; EntityItemWeakPointer _ownerEntity; }; From ef4620cabb29d3f1b71dfd6f80ce3290c2ebd650 Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Wed, 8 Jul 2015 11:52:37 -0700 Subject: [PATCH 20/30] Add a button to create/toggle-away sword. When brandished, display hit points (in overlay for this user). --- examples/example/games/sword.js | 143 ++++++++++++++++++++++++++------ 1 file changed, 117 insertions(+), 26 deletions(-) diff --git a/examples/example/games/sword.js b/examples/example/games/sword.js index 9592c275f6..995015b227 100644 --- a/examples/example/games/sword.js +++ b/examples/example/games/sword.js @@ -11,7 +11,7 @@ // "use strict"; /*jslint vars: true*/ -var Script, Entities, MyAvatar, Window, Controller, Vec3, Quat; // Referenced globals provided by High Fidelity. +var Script, Entities, MyAvatar, Window, Overlays, Controller, Vec3, Quat, print; // Referenced globals provided by High Fidelity. var hand = "right"; var nullActionID = "00000000-0000-0000-0000-000000000000"; @@ -20,39 +20,86 @@ var controllerActive; var stickID = null; var actionID = nullActionID; var dimensions = { x: 0.3, y: 0.1, z: 2.0 }; +var AWAY_ORIENTATION = Quat.fromPitchYawRollDegrees(-90, 0, 0); var stickModel = "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx"; var swordModel = "https://hifi-public.s3.amazonaws.com/ozan/props/sword/sword.fbx"; var whichModel = "sword"; +var rezButton = Overlays.addOverlay("image", { + x: 100, + y: 380, + width: 32, + height: 32, + imageURL: "http://s3.amazonaws.com/hifi-public/images/delete.png", + color: { + red: 255, + green: 255, + blue: 255 + }, + alpha: 1 +}); -// sometimes if this is run immediately the stick doesn't get created? use a timer. -Script.setTimeout(function () { - stickID = Entities.addEntity({ - type: "Model", - modelURL: (whichModel === "sword") ? swordModel : stickModel, - //compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj", - shapeType: "box", - dimensions: dimensions, - position: MyAvatar.getRightPalmPosition(), // initial position doesn't matter, as long as it's close - rotation: MyAvatar.orientation, - damping: 0.1, - collisionSoundURL: "http://public.highfidelity.io/sounds/Collisions-hitsandslaps/swordStrike1.wav", - restitution: 0.01, - collisionsWillMove: true - }); - actionID = Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -dimensions.z / 2}, - hand: hand, - timeScale: 0.15}); -}, 3000); - - -function cleanUp() { - Entities.deleteEntity(stickID); +var health = 100; +var display; +var isAway = false; +function updateDisplay() { + var text = health.toString(); + if (!display) { + health = 100; + display = Overlays.addOverlay("text", { + text: text, + font: { size: 20 }, + color: {red: 0, green: 255, blue: 0}, + backgroundColor: {red: 100, green: 100, blue: 100}, // Why doesn't this and the next work? + backgroundAlpha: 0.9, + x: Window.innerWidth - 50, + y: 50 + }); + } else { + Overlays.editOverlay(display, {text: text}); + } +} +function removeDisplay() { + if (display) { + Overlays.deleteOverlay(display); + display = null; + } } +function cleanUp() { + if (stickID) { + Entities.deleteAction(stickID, actionID); + Entities.deleteEntity(stickID); + stickID = null; + actionID = null; + } + removeDisplay(); + Overlays.deleteOverlay(rezButton); +} + +function computeEnergy(collision, entityID) { + var id = entityID || collision.idA || collision.idB; + var entity = id && Entities.getEntityProperties(id); + var mass = entity ? (entity.density * entity.dimensions.x * entity.dimensions.y * entity.dimensions.z) : 1; + var linearVelocityChange = Vec3.length(collision.velocityChange); + var energy = 0.5 * mass * linearVelocityChange * linearVelocityChange; + return Math.min(Math.max(1.0, Math.round(energy)), 20); +} +function gotHit(collision) { + if (isAway) { return; } + var energy = computeEnergy(collision); + health -= energy; + updateDisplay(); +} +function scoreHit(idA, idB, collision) { + if (isAway) { return; } + var energy = computeEnergy(collision, idA); + health += energy; + updateDisplay(); +} function positionStick(stickOrientation) { - var baseOffset = {x: 0.0, y: 0.0, z: -dimensions.z / 2}; + var baseOffset = {x: 0.3, y: 0.0, z: -dimensions.z / 2}; // FIXME: don't move yourself by colliding with your own capsule. Fudge of 0.3 in x. var offset = Vec3.multiplyQbyV(stickOrientation, baseOffset); Entities.updateAction(stickID, actionID, {relativePosition: offset, relativeRotation: stickOrientation}); @@ -60,7 +107,7 @@ function positionStick(stickOrientation) { function mouseMoveEvent(event) { - if (!stickID || actionID === nullActionID) { + if (!stickID || actionID === nullActionID || isAway) { return; } var windowCenterX = Window.innerWidth / 2; @@ -98,7 +145,51 @@ function update() { positionStick(stickOrientation); } +function toggleAway() { + isAway = !isAway; + if (isAway) { + positionStick(AWAY_ORIENTATION); + removeDisplay(); + } else { + updateDisplay(); + } +} + +function onClick(event) { + switch (Overlays.getOverlayAtPoint({x: event.x, y: event.y})) { + case rezButton: + if (!stickID) { + stickID = Entities.addEntity({ + type: "Model", + modelURL: (whichModel === "sword") ? swordModel : stickModel, + //compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj", + shapeType: "box", + dimensions: dimensions, + position: MyAvatar.getRightPalmPosition(), // initial position doesn't matter, as long as it's close + rotation: MyAvatar.orientation, + damping: 0.1, + collisionSoundURL: "http://public.highfidelity.io/sounds/Collisions-hitsandslaps/swordStrike1.wav", + restitution: 0.01, + collisionsWillMove: true + }); + actionID = Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -dimensions.z / 2}, + hand: hand, + timeScale: 0.15}); + if (actionID === nullActionID) { + print('*** FAILED TO MAKE SWORD ACTION ***'); + cleanUp(); + } + Script.addEventHandler(stickID, 'collisionWithEntity', scoreHit); + updateDisplay(); + } else { + toggleAway(); + } + break; + } +} Script.scriptEnding.connect(cleanUp); Controller.mouseMoveEvent.connect(mouseMoveEvent); +Controller.mousePressEvent.connect(onClick); Script.update.connect(update); +MyAvatar.collisionWithEntity.connect(gotHit); From f992e875b452c900d43037d35c3493f145b38230 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Wed, 8 Jul 2015 14:25:13 -0700 Subject: [PATCH 21/30] Adding intrsumentation for nsight and hiding the nsight specific code in cpp --- interface/src/Application.cpp | 2 +- interface/src/GLCanvas.cpp | 1 + .../src/RenderableModelEntityItem.cpp | 1 + libraries/gpu/src/gpu/Batch.cpp | 11 ++++++++++ libraries/gpu/src/gpu/Batch.h | 10 ++-------- libraries/gpu/src/gpu/Context.cpp | 2 ++ libraries/render-utils/src/Model.cpp | 20 +++++++++++++++---- libraries/render/CMakeLists.txt | 14 ++++++++++++- libraries/render/src/render/Scene.cpp | 2 ++ 9 files changed, 49 insertions(+), 14 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index c1397beb28..46e127b1b3 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1182,7 +1182,6 @@ bool Application::event(QEvent* event) { } bool Application::eventFilter(QObject* object, QEvent* event) { - if (event->type() == QEvent::ShortcutOverride) { if (DependencyManager::get()->shouldSwallowShortcut(event)) { event->accept(); @@ -1787,6 +1786,7 @@ void Application::checkFPS() { } void Application::idle() { + PROFILE_RANGE(__FUNCTION__); static SimpleAverage interIdleDurations; static uint64_t lastIdleEnd{ 0 }; diff --git a/interface/src/GLCanvas.cpp b/interface/src/GLCanvas.cpp index 0eda678fa9..aff7c75cc9 100644 --- a/interface/src/GLCanvas.cpp +++ b/interface/src/GLCanvas.cpp @@ -58,6 +58,7 @@ void GLCanvas::initializeGL() { } void GLCanvas::paintGL() { + PROFILE_RANGE(__FUNCTION__); if (!_throttleRendering && !Application::getInstance()->getWindow()->isMinimized()) { Application::getInstance()->paintGL(); } diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index ae1c97f07f..85b7bafc78 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -161,6 +161,7 @@ namespace render { template <> void payloadRender(const RenderableModelEntityItemMeta::Pointer& payload, RenderArgs* args) { if (args) { if (payload && payload->entity) { + PROFILE_RANGE("MetaModelRender"); payload->entity->render(args); } } diff --git a/libraries/gpu/src/gpu/Batch.cpp b/libraries/gpu/src/gpu/Batch.cpp index ee028e79e6..98d7b70db8 100644 --- a/libraries/gpu/src/gpu/Batch.cpp +++ b/libraries/gpu/src/gpu/Batch.cpp @@ -12,6 +12,17 @@ #include +#if defined(NSIGHT_FOUND) +#include "nvToolsExt.h" + +ProfileRange::ProfileRange(const char *name) { + nvtxRangePush(name); +} +ProfileRange::~ProfileRange() { + nvtxRangePop(); +} +#endif + #define ADD_COMMAND(call) _commands.push_back(COMMAND_##call); _commandOffsets.push_back(_params.size()); using namespace gpu; diff --git a/libraries/gpu/src/gpu/Batch.h b/libraries/gpu/src/gpu/Batch.h index 835e872b4a..5583294aba 100644 --- a/libraries/gpu/src/gpu/Batch.h +++ b/libraries/gpu/src/gpu/Batch.h @@ -26,17 +26,11 @@ #include "Framebuffer.h" #if defined(NSIGHT_FOUND) - #include "nvToolsExt.h" class ProfileRange { public: - ProfileRange(const char *name) { - nvtxRangePush(name); - } - ~ProfileRange() { - nvtxRangePop(); - } + ProfileRange(const char *name); + ~ProfileRange(); }; - #define PROFILE_RANGE(name) ProfileRange profileRangeThis(name); #else #define PROFILE_RANGE(name) diff --git a/libraries/gpu/src/gpu/Context.cpp b/libraries/gpu/src/gpu/Context.cpp index 9cc6bb3cd7..51335f78df 100644 --- a/libraries/gpu/src/gpu/Context.cpp +++ b/libraries/gpu/src/gpu/Context.cpp @@ -33,9 +33,11 @@ bool Context::makeProgram(Shader& shader, const Shader::BindingSet& bindings) { } void Context::render(Batch& batch) { + PROFILE_RANGE(__FUNCTION__); _backend->render(batch); } void Context::syncCache() { + PROFILE_RANGE(__FUNCTION__); _backend->syncCache(); } \ No newline at end of file diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 1b94c70e57..03140c4dfb 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -417,6 +417,7 @@ void Model::reset() { } bool Model::updateGeometry() { + PROFILE_RANGE(__FUNCTION__); bool needFullUpdate = false; bool needToRebuild = false; @@ -690,6 +691,7 @@ void Model::recalculateMeshPartOffsets() { // entity-scripts to call. I think it would be best to do the picking once-per-frame (in cpu, or gpu if possible) // and then the calls use the most recent such result. void Model::recalculateMeshBoxes(bool pickAgainstTriangles) { + PROFILE_RANGE(__FUNCTION__); bool calculatedMeshTrianglesNeeded = pickAgainstTriangles && !_calculatedMeshTrianglesValid; if (!_calculatedMeshBoxesValid || calculatedMeshTrianglesNeeded || (!_calculatedMeshPartBoxesValid && pickAgainstTriangles) ) { @@ -1281,6 +1283,7 @@ Blender::Blender(Model* model, int blendNumber, const QWeakPointer vertices, normals; if (!_model.isNull()) { int offset = 0; @@ -1392,6 +1395,7 @@ void Model::snapToRegistrationPoint() { } void Model::simulate(float deltaTime, bool fullUpdate) { + PROFILE_RANGE(__FUNCTION__); fullUpdate = updateGeometry() || fullUpdate || (_scaleToFit && !_scaledToFit) || (_snapModelToRegistrationPoint && !_snappedToRegistrationPoint); @@ -1829,6 +1833,7 @@ void Model::setupBatchTransform(gpu::Batch& batch, RenderArgs* args) { } AABox Model::getPartBounds(int meshIndex, int partIndex) { + if (meshIndex < _meshStates.size()) { const MeshState& state = _meshStates.at(meshIndex); bool isSkinned = state.clusterMatrices.size() > 1; @@ -1859,6 +1864,7 @@ AABox Model::getPartBounds(int meshIndex, int partIndex) { } void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool translucent) { + // PROFILE_RANGE(__FUNCTION__); PerformanceTimer perfTimer("Model::renderPart"); if (!_readyWhenAdded) { return; // bail asap @@ -1933,7 +1939,9 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran pickPrograms(batch, mode, translucentMesh, alphaThreshold, hasLightmap, hasTangents, hasSpecular, isSkinned, wireframe, args, locations); - updateVisibleJointStates(); + { + updateVisibleJointStates(); + } // if our index is ever out of range for either meshes or networkMeshes, then skip it, and set our _meshGroupsKnown // to false to rebuild out mesh groups. @@ -2076,9 +2084,13 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran } } - _mutex.lock(); - qint64 offset = _calculatedMeshPartOffset[QPair(meshIndex, partIndex)]; - _mutex.unlock(); + qint64 offset; + { + // FIXME_STUTTER: We should n't have any lock here + _mutex.lock(); + offset = _calculatedMeshPartOffset[QPair(meshIndex, partIndex)]; + _mutex.unlock(); + } if (part.quadIndices.size() > 0) { batch.drawIndexed(gpu::QUADS, part.quadIndices.size(), offset); diff --git a/libraries/render/CMakeLists.txt b/libraries/render/CMakeLists.txt index ee99eb00b9..4d2be949e6 100644 --- a/libraries/render/CMakeLists.txt +++ b/libraries/render/CMakeLists.txt @@ -9,4 +9,16 @@ add_dependency_external_projects(glm) find_package(GLM REQUIRED) target_include_directories(${TARGET_NAME} PUBLIC ${GLM_INCLUDE_DIRS}) -link_hifi_libraries(shared gpu model) \ No newline at end of file +link_hifi_libraries(shared gpu model) + +if (WIN32) + if (USE_NSIGHT) + # try to find the Nsight package and add it to the build if we find it + find_package(NSIGHT) + if (NSIGHT_FOUND) + include_directories(${NSIGHT_INCLUDE_DIRS}) + add_definitions(-DNSIGHT_FOUND) + target_link_libraries(${TARGET_NAME} "${NSIGHT_LIBRARIES}") + endif () + endif() +endif (WIN32) \ No newline at end of file diff --git a/libraries/render/src/render/Scene.cpp b/libraries/render/src/render/Scene.cpp index a7145af4b5..268f2b6841 100644 --- a/libraries/render/src/render/Scene.cpp +++ b/libraries/render/src/render/Scene.cpp @@ -11,6 +11,7 @@ #include "Scene.h" #include +#include "gpu/Batch.h" using namespace render; @@ -167,6 +168,7 @@ void consolidateChangeQueue(PendingChangesQueue& queue, PendingChanges& singleBa } void Scene::processPendingChangesQueue() { + PROFILE_RANGE(__FUNCTION__); _changeQueueMutex.lock(); PendingChanges consolidatedPendingChanges; consolidateChangeQueue(_changeQueue, consolidatedPendingChanges); From 6a9344a3f6b5f966a3d9b8d5c64db8f4008fe6ea Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 8 Jul 2015 15:14:24 -0700 Subject: [PATCH 22/30] fix bug in old version of decode --- libraries/shared/src/ByteCountCoding.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/libraries/shared/src/ByteCountCoding.h b/libraries/shared/src/ByteCountCoding.h index 2a39ee7a8c..efde19d45b 100644 --- a/libraries/shared/src/ByteCountCoding.h +++ b/libraries/shared/src/ByteCountCoding.h @@ -21,6 +21,8 @@ #include #include +#include + #include #include @@ -111,12 +113,12 @@ template inline QByteArray ByteCountCoded::encode() const { } template inline void ByteCountCoded::decode(const QByteArray& fromEncodedBytes) { - // first convert the ByteArray into a BitArray... QBitArray encodedBits; int bitCount = BITS_IN_BYTE * fromEncodedBytes.count(); encodedBits.resize(bitCount); + // copies the QByteArray into a QBitArray for(int byte = 0; byte < fromEncodedBytes.count(); byte++) { char originalByte = fromEncodedBytes.at(byte); for(int bit = 0; bit < BITS_IN_BYTE; bit++) { @@ -128,8 +130,8 @@ template inline void ByteCountCoded::decode(const QByteArray& fro } // next, read the leading bits to determine the correct number of bytes to decode (may not match the QByteArray) - int encodedByteCount = 0; - int leadBits = 1; + int encodedByteCount = 1; // there is at least 1 byte (after the leadBits) + int leadBits = 1; // there is always at least 1 lead bit int bitAt; for (bitAt = 0; bitAt < bitCount; bitAt++) { if (encodedBits.at(bitAt)) { @@ -139,16 +141,15 @@ template inline void ByteCountCoded::decode(const QByteArray& fro break; } } - encodedByteCount++; // always at least one byte - int expectedBitCount = encodedByteCount * BITS_IN_BYTE; + int expectedBitCount = (encodedByteCount * BITS_IN_BYTE) - leadBits; T value = 0; - + if (expectedBitCount <= (encodedBits.size() - leadBits)) { // Now, keep reading... int valueStartsAt = bitAt + 1; T bitValue = 1; - for (bitAt = valueStartsAt; bitAt < expectedBitCount; bitAt++) { + for (bitAt = valueStartsAt; bitAt < (expectedBitCount + leadBits); bitAt++) { if(encodedBits.at(bitAt)) { value += bitValue; } From 6813a3afe134d4b22fc029fb1decf72c47c5fbe0 Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Wed, 8 Jul 2015 15:27:45 -0700 Subject: [PATCH 23/30] Sword: + init hands. + toolbar of brandishToggle/makeTarget/cleanup. + initial hit flash using overlays instead of ogl "fade". --- examples/example/games/sword.js | 102 +++++++++++++++++++++++++++----- 1 file changed, 86 insertions(+), 16 deletions(-) diff --git a/examples/example/games/sword.js b/examples/example/games/sword.js index 995015b227..115a407cc8 100644 --- a/examples/example/games/sword.js +++ b/examples/example/games/sword.js @@ -11,7 +11,8 @@ // "use strict"; /*jslint vars: true*/ -var Script, Entities, MyAvatar, Window, Overlays, Controller, Vec3, Quat, print; // Referenced globals provided by High Fidelity. +var Script, Entities, MyAvatar, Window, Overlays, Controller, Vec3, Quat, print, ToolBar; // Referenced globals provided by High Fidelity. +Script.include(["../../libraries/toolBars.js"]); var hand = "right"; var nullActionID = "00000000-0000-0000-0000-000000000000"; @@ -19,25 +20,61 @@ var controllerID; var controllerActive; var stickID = null; var actionID = nullActionID; +var targetIDs = []; var dimensions = { x: 0.3, y: 0.1, z: 2.0 }; var AWAY_ORIENTATION = Quat.fromPitchYawRollDegrees(-90, 0, 0); +var BUTTON_SIZE = 32; var stickModel = "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx"; var swordModel = "https://hifi-public.s3.amazonaws.com/ozan/props/sword/sword.fbx"; var whichModel = "sword"; -var rezButton = Overlays.addOverlay("image", { - x: 100, - y: 380, - width: 32, - height: 32, - imageURL: "http://s3.amazonaws.com/hifi-public/images/delete.png", - color: { - red: 255, - green: 255, - blue: 255 - }, +var toolBar = new ToolBar(0, 0, ToolBar.vertical, "highfidelity.sword.toolbar", function () { + return {x: 100, y: 380}; +}); + +var SWORD_IMAGE = "http://s3.amazonaws.com/hifi-public/images/billiardsReticle.png"; // Toggle between brandishing/sheathing sword (creating if necessary) +var TARGET_IMAGE = "http://s3.amazonaws.com/hifi-public/images/puck.png"; // Create a target dummy +var CLEANUP_IMAGE = "http://s3.amazonaws.com/hifi-public/images/delete.png"; // Remove sword and all target dummies.f +var swordButton = toolBar.addOverlay("image", { + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: SWORD_IMAGE, alpha: 1 }); +var targetButton = toolBar.addOverlay("image", { + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: TARGET_IMAGE, + alpha: 1 +}); +var cleanupButton = toolBar.addOverlay("image", { + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: CLEANUP_IMAGE, + alpha: 1 +}); + +var flasher; +function clearFlash() { + if (!flasher) { + return; + } + Script.clearTimeout(flasher.timer); + Overlays.deleteOverlay(flasher.overlay); + flasher = null; +} +function flash(color) { + clearFlash(); + flasher = {}; + flasher.overlay = Overlays.addOverlay("text", { + backgroundColor: color, + backgroundAlpha: 0.7, + width: Window.innerWidth, + height: Window.innerHeight + }); + flasher.timer = Script.setTimeout(clearFlash, 500); +} + var health = 100; var display; @@ -66,15 +103,22 @@ function removeDisplay() { } } -function cleanUp() { +function cleanUp(leaveButtons) { if (stickID) { Entities.deleteAction(stickID, actionID); Entities.deleteEntity(stickID); stickID = null; actionID = null; } + targetIDs.forEach(function (id) { + Entities.deleteAction(id.entity, id.action); + Entities.deleteEntity(id.entity); + }); + targetIDs = []; removeDisplay(); - Overlays.deleteOverlay(rezButton); + if (!leaveButtons) { + toolBar.cleanup(); + } } function computeEnergy(collision, entityID) { @@ -89,12 +133,14 @@ function gotHit(collision) { if (isAway) { return; } var energy = computeEnergy(collision); health -= energy; + flash({red: 255, green: 0, blue: 0}); updateDisplay(); } function scoreHit(idA, idB, collision) { if (isAway) { return; } var energy = computeEnergy(collision, idA); health += energy; + flash({red: 0, green: 255, blue: 0}); updateDisplay(); } @@ -156,9 +202,10 @@ function toggleAway() { } function onClick(event) { - switch (Overlays.getOverlayAtPoint({x: event.x, y: event.y})) { - case rezButton: + switch (Overlays.getOverlayAtPoint(event)) { + case swordButton: if (!stickID) { + initControls(); stickID = Entities.addEntity({ type: "Model", modelURL: (whichModel === "sword") ? swordModel : stickModel, @@ -185,6 +232,29 @@ function onClick(event) { toggleAway(); } break; + case targetButton: + var position = Vec3.sum(MyAvatar.position, {x: 1.0, y: 0.4, z: 0.0}); + var boxId = Entities.addEntity({ + type: "Box", + name: "dummy", + position: position, + dimensions: {x: 0.3, y: 0.7, z: 0.3}, + gravity: {x: 0.0, y: -3.0, z: 0.0}, + damping: 0.2, + collisionsWillMove: true + }); + + var pointToOffsetFrom = Vec3.sum(position, {x: 0.0, y: 2.0, z: 0.0}); + var action = Entities.addAction("offset", boxId, {pointToOffsetFrom: pointToOffsetFrom, + linearDistance: 2.0, + // linearTimeScale: 0.005 + linearTimeScale: 0.1 + }); + targetIDs.push({entity: boxId, action: action}); + break; + case cleanupButton: + cleanUp('leaveButtons'); + break; } } From c8da32521365d7ad0b293056d6b7b2ccc5141895 Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Wed, 8 Jul 2015 16:59:54 -0700 Subject: [PATCH 24/30] Fudge the offset so that operates only when using the mouse. --- examples/example/games/sword.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/example/games/sword.js b/examples/example/games/sword.js index 115a407cc8..66503b62aa 100644 --- a/examples/example/games/sword.js +++ b/examples/example/games/sword.js @@ -28,6 +28,8 @@ var BUTTON_SIZE = 32; var stickModel = "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx"; var swordModel = "https://hifi-public.s3.amazonaws.com/ozan/props/sword/sword.fbx"; var whichModel = "sword"; +var attachmentOffset, MOUSE_CONTROLLER_OFFSET = {x: 0.5, y: 0.4, z: 0.0}; // A fudge when using mouse rather than hand-controller, to hit yourself less often. + var toolBar = new ToolBar(0, 0, ToolBar.vertical, "highfidelity.sword.toolbar", function () { return {x: 100, y: 380}; }); @@ -104,6 +106,7 @@ function removeDisplay() { } function cleanUp(leaveButtons) { + attachmentOffset = {x: 0, y: 0, z: 0}; if (stickID) { Entities.deleteAction(stickID, actionID); Entities.deleteEntity(stickID); @@ -145,7 +148,7 @@ function scoreHit(idA, idB, collision) { } function positionStick(stickOrientation) { - var baseOffset = {x: 0.3, y: 0.0, z: -dimensions.z / 2}; // FIXME: don't move yourself by colliding with your own capsule. Fudge of 0.3 in x. + var baseOffset = Vec3.sum(attachmentOffset, {x: 0.0, y: 0.0, z: -dimensions.z / 2}); var offset = Vec3.multiplyQbyV(stickOrientation, baseOffset); Entities.updateAction(stickID, actionID, {relativePosition: offset, relativeRotation: stickOrientation}); @@ -153,6 +156,7 @@ function positionStick(stickOrientation) { function mouseMoveEvent(event) { + attachmentOffset = MOUSE_CONTROLLER_OFFSET; if (!stickID || actionID === nullActionID || isAway) { return; } From 98e0688e9859c5ef51f9f08743f4925df990ac41 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Wed, 8 Jul 2015 17:21:38 -0700 Subject: [PATCH 25/30] Reducing heap allocation in network packet parsing --- libraries/entities/src/EntityItem.cpp | 160 +++++++++++++++++------- libraries/entities/src/EntityItemID.cpp | 7 +- libraries/shared/src/BufferParser.h | 123 ++++++++++++++++++ 3 files changed, 240 insertions(+), 50 deletions(-) create mode 100644 libraries/shared/src/BufferParser.h diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 6c27152a97..1165518097 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -12,9 +12,11 @@ #include "EntityItem.h" #include +#include #include +#include #include #include #include @@ -354,50 +356,78 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef // ~27-35 bytes... const int MINIMUM_HEADER_BYTES = 27; - int bytesRead = 0; if (bytesLeftToRead < MINIMUM_HEADER_BYTES) { return 0; } + int clockSkew = args.sourceNode ? args.sourceNode->getClockSkewUsec() : 0; + + BufferParser parser(data, bytesLeftToRead); + +#ifdef DEBUG +#define VALIDATE_ENTITY_ITEM_PARSER 1 +#endif + +#ifdef VALIDATE_ENTITY_ITEM_PARSER + int bytesRead = 0; int originalLength = bytesLeftToRead; // TODO: figure out a way to avoid the big deep copy below. QByteArray originalDataBuffer((const char*)data, originalLength); // big deep copy! - - int clockSkew = args.sourceNode ? args.sourceNode->getClockSkewUsec() : 0; - const unsigned char* dataAt = data; +#endif // id - QByteArray encodedID = originalDataBuffer.mid(bytesRead, NUM_BYTES_RFC4122_UUID); // maximum possible size - _id = QUuid::fromRfc4122(encodedID); - dataAt += encodedID.size(); - bytesRead += encodedID.size(); - + parser.readUuid(_id); +#ifdef VALIDATE_ENTITY_ITEM_PARSER + { + QByteArray encodedID = originalDataBuffer.mid(bytesRead, NUM_BYTES_RFC4122_UUID); // maximum possible size + QUuid id = QUuid::fromRfc4122(encodedID); + dataAt += encodedID.size(); + bytesRead += encodedID.size(); + Q_ASSERT(id == _id); + Q_ASSERT(parser.offset() == bytesRead); + } +#endif + // type + parser.readCompressedCount((quint32&)_type); +#ifdef VALIDATE_ENTITY_ITEM_PARSER QByteArray encodedType = originalDataBuffer.mid(bytesRead); // maximum possible size ByteCountCoded typeCoder = encodedType; encodedType = typeCoder; // determine true length dataAt += encodedType.size(); bytesRead += encodedType.size(); quint32 type = typeCoder; - _type = (EntityTypes::EntityType)type; + EntityTypes::EntityType oldType = (EntityTypes::EntityType)type; + Q_ASSERT(oldType == _type); + Q_ASSERT(parser.offset() == bytesRead); +#endif bool overwriteLocalData = true; // assume the new content overwrites our local data + quint64 now = usecTimestampNow(); // _created - quint64 createdFromBuffer = 0; - memcpy(&createdFromBuffer, dataAt, sizeof(createdFromBuffer)); - dataAt += sizeof(createdFromBuffer); - bytesRead += sizeof(createdFromBuffer); - - quint64 now = usecTimestampNow(); - if (_created == UNKNOWN_CREATED_TIME) { - // we don't yet have a _created timestamp, so we accept this one - createdFromBuffer -= clockSkew; - if (createdFromBuffer > now || createdFromBuffer == UNKNOWN_CREATED_TIME) { - createdFromBuffer = now; + { + quint64 createdFromBuffer = 0; + parser.readValue(createdFromBuffer); +#ifdef VALIDATE_ENTITY_ITEM_PARSER + { + quint64 createdFromBuffer2 = 0; + memcpy(&createdFromBuffer2, dataAt, sizeof(createdFromBuffer2)); + dataAt += sizeof(createdFromBuffer2); + bytesRead += sizeof(createdFromBuffer2); + Q_ASSERT(createdFromBuffer2 == createdFromBuffer); + Q_ASSERT(parser.offset() == bytesRead); + } +#endif + if (_created == UNKNOWN_CREATED_TIME) { + // we don't yet have a _created timestamp, so we accept this one + createdFromBuffer -= clockSkew; + if (createdFromBuffer > now || createdFromBuffer == UNKNOWN_CREATED_TIME) { + createdFromBuffer = now; + } + _created = createdFromBuffer; } - _created = createdFromBuffer; } #ifdef WANT_DEBUG @@ -417,15 +447,21 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef #endif quint64 lastEditedFromBuffer = 0; - quint64 lastEditedFromBufferAdjusted = 0; // TODO: we could make this encoded as a delta from _created // _lastEdited - memcpy(&lastEditedFromBuffer, dataAt, sizeof(lastEditedFromBuffer)); - dataAt += sizeof(lastEditedFromBuffer); - bytesRead += sizeof(lastEditedFromBuffer); - lastEditedFromBufferAdjusted = lastEditedFromBuffer - clockSkew; - + parser.readValue(lastEditedFromBuffer); +#ifdef VALIDATE_ENTITY_ITEM_PARSER + { + quint64 lastEditedFromBuffer2 = 0; + memcpy(&lastEditedFromBuffer2, dataAt, sizeof(lastEditedFromBuffer2)); + dataAt += sizeof(lastEditedFromBuffer2); + bytesRead += sizeof(lastEditedFromBuffer2); + Q_ASSERT(lastEditedFromBuffer2 == lastEditedFromBuffer); + Q_ASSERT(parser.offset() == bytesRead); + } +#endif + quint64 lastEditedFromBufferAdjusted = lastEditedFromBuffer - clockSkew; if (lastEditedFromBufferAdjusted > now) { lastEditedFromBufferAdjusted = now; } @@ -487,9 +523,21 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef } // last updated is stored as ByteCountCoded delta from lastEdited - QByteArray encodedUpdateDelta = originalDataBuffer.mid(bytesRead); // maximum possible size - ByteCountCoded updateDeltaCoder = encodedUpdateDelta; - quint64 updateDelta = updateDeltaCoder; + quint64 updateDelta; + parser.readCompressedCount(updateDelta); +#ifdef VALIDATE_ENTITY_ITEM_PARSER + { + QByteArray encodedUpdateDelta = originalDataBuffer.mid(bytesRead); // maximum possible size + ByteCountCoded updateDeltaCoder = encodedUpdateDelta; + quint64 updateDelta2 = updateDeltaCoder; + Q_ASSERT(updateDelta == updateDelta2); + encodedUpdateDelta = updateDeltaCoder; // determine true length + dataAt += encodedUpdateDelta.size(); + bytesRead += encodedUpdateDelta.size(); + Q_ASSERT(parser.offset() == bytesRead); + } +#endif + if (overwriteLocalData) { _lastUpdated = lastEditedFromBufferAdjusted + updateDelta; // don't adjust for clock skew since we already did that #ifdef WANT_DEBUG @@ -499,17 +547,25 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef #endif } - encodedUpdateDelta = updateDeltaCoder; // determine true length - dataAt += encodedUpdateDelta.size(); - bytesRead += encodedUpdateDelta.size(); - // Newer bitstreams will have a last simulated and a last updated value quint64 lastSimulatedFromBufferAdjusted = now; if (args.bitstreamVersion >= VERSION_ENTITIES_HAS_LAST_SIMULATED_TIME) { // last simulated is stored as ByteCountCoded delta from lastEdited - QByteArray encodedSimulatedDelta = originalDataBuffer.mid(bytesRead); // maximum possible size - ByteCountCoded simulatedDeltaCoder = encodedSimulatedDelta; - quint64 simulatedDelta = simulatedDeltaCoder; + quint64 simulatedDelta; + parser.readCompressedCount(simulatedDelta); +#ifdef VALIDATE_ENTITY_ITEM_PARSER + { + QByteArray encodedSimulatedDelta = originalDataBuffer.mid(bytesRead); // maximum possible size + ByteCountCoded simulatedDeltaCoder = encodedSimulatedDelta; + quint64 simulatedDelta2 = simulatedDeltaCoder; + Q_ASSERT(simulatedDelta2 == simulatedDelta); + encodedSimulatedDelta = simulatedDeltaCoder; // determine true length + dataAt += encodedSimulatedDelta.size(); + bytesRead += encodedSimulatedDelta.size(); + Q_ASSERT(parser.offset() == bytesRead); + } +#endif + if (overwriteLocalData) { lastSimulatedFromBufferAdjusted = lastEditedFromBufferAdjusted + simulatedDelta; // don't adjust for clock skew since we already did that if (lastSimulatedFromBufferAdjusted > now) { @@ -521,9 +577,6 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef qCDebug(entities) << " lastSimulatedFromBufferAdjusted:" << debugTime(lastSimulatedFromBufferAdjusted, now); #endif } - encodedSimulatedDelta = simulatedDeltaCoder; // determine true length - dataAt += encodedSimulatedDelta.size(); - bytesRead += encodedSimulatedDelta.size(); } #ifdef WANT_DEBUG @@ -537,10 +590,26 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef // Property Flags - QByteArray encodedPropertyFlags = originalDataBuffer.mid(bytesRead); // maximum possible size - EntityPropertyFlags propertyFlags = encodedPropertyFlags; - dataAt += propertyFlags.getEncodedLength(); - bytesRead += propertyFlags.getEncodedLength(); + EntityPropertyFlags propertyFlags; + parser.readFlags(propertyFlags); +#ifdef VALIDATE_ENTITY_ITEM_PARSER + { + QByteArray encodedPropertyFlags = originalDataBuffer.mid(bytesRead); // maximum possible size + EntityPropertyFlags propertyFlags2 = encodedPropertyFlags; + dataAt += propertyFlags.getEncodedLength(); + bytesRead += propertyFlags.getEncodedLength(); + Q_ASSERT(propertyFlags2 == propertyFlags); + Q_ASSERT(parser.offset() == bytesRead); + } +#endif + +#ifdef VALIDATE_ENTITY_ITEM_PARSER + Q_ASSERT(parser.data() + parser.offset() == dataAt); +#else + const unsigned char* dataAt = parser.data() + parser.offset(); + int bytesRead = parser.offset(); +#endif + if (args.bitstreamVersion >= VERSION_ENTITIES_HAVE_SIMULATION_OWNER_AND_ACTIONS_OVER_WIRE) { // pack SimulationOwner and terse update properties near each other @@ -549,6 +618,7 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef // even when we would otherwise ignore the rest of the packet. if (propertyFlags.getHasProperty(PROP_SIMULATION_OWNER)) { + QByteArray simOwnerData; int bytes = OctreePacketData::unpackDataFromBytes(dataAt, simOwnerData); SimulationOwner newSimOwner; diff --git a/libraries/entities/src/EntityItemID.cpp b/libraries/entities/src/EntityItemID.cpp index 4e66f5d156..d882559ee3 100644 --- a/libraries/entities/src/EntityItemID.cpp +++ b/libraries/entities/src/EntityItemID.cpp @@ -11,7 +11,7 @@ #include #include - +#include #include #include "RegisteredMetaTypes.h" @@ -33,11 +33,8 @@ EntityItemID::EntityItemID(const QUuid& id) : QUuid(id) EntityItemID EntityItemID::readEntityItemIDFromBuffer(const unsigned char* data, int bytesLeftToRead) { EntityItemID result; - if (bytesLeftToRead >= NUM_BYTES_RFC4122_UUID) { - // id - QByteArray encodedID((const char*)data, NUM_BYTES_RFC4122_UUID); - result = QUuid::fromRfc4122(encodedID); + BufferParser(data, bytesLeftToRead).readUuid(result); } return result; } diff --git a/libraries/shared/src/BufferParser.h b/libraries/shared/src/BufferParser.h new file mode 100644 index 0000000000..622d59d125 --- /dev/null +++ b/libraries/shared/src/BufferParser.h @@ -0,0 +1,123 @@ +// +// Created by Bradley Austin Davis on 2015/07/08 +// Copyright 2013-2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#pragma once +#ifndef hifi_BufferParser_h +#define hifi_BufferParser_h + +#include + +#include +#include + +#include "GLMHelpers.h" +#include "ByteCountCoding.h" +#include "PropertyFlags.h" + +class BufferParser { +public: + BufferParser(const uint8_t* data, size_t size, size_t offset = 0) : + _offset(offset), _data(data), _size(size) { + } + + template + void readValue(T& result) { + Q_ASSERT(remaining() >= sizeof(T)); + memcpy(&result, _data + _offset, sizeof(T)); + _offset += sizeof(T); + } + + template<> + void readValue(quat& result) { + size_t advance = unpackOrientationQuatFromBytes(_data + _offset, result); + _offset += advance; + } + + template<> + void readValue(QString& result) { + uint16_t length; readValue(length); + result = QString((const char*)_data + _offset); + } + + template<> + void readValue(QUuid& result) { + uint16_t length; readValue(length); + Q_ASSERT(16 == length); + readUuid(result); + } + + template<> + void readValue(xColor& result) { + readValue(result.red); + readValue(result.blue); + readValue(result.green); + } + + template<> + void readValue>(QVector& result) { + uint16_t length; readValue(length); + result.resize(length); + memcpy(result.data(), _data + _offset, sizeof(glm::vec3) * length); + _offset += sizeof(glm::vec3) * length; + } + + template<> + void readValue(QByteArray& result) { + uint16_t length; readValue(length); + result = QByteArray((char*)_data + _offset, (int)length); + _offset += length; + } + + void readUuid(QUuid& result) { + readValue(result.data1); + readValue(result.data2); + readValue(result.data3); + readValue(result.data4); + result.data1 = qFromBigEndian(result.data1); + result.data2 = qFromBigEndian(result.data2); + result.data3 = qFromBigEndian(result.data3); + } + + template + void readFlags(PropertyFlags& result) { + // FIXME doing heap allocation + QByteArray encoded((const char*)(_data + _offset), remaining()); + result.decode(encoded); + _offset += result.getEncodedLength(); + } + + template + void readCompressedCount(T& result) { + // FIXME switch to a heapless implementation as soon as Brad provides it. + QByteArray encoded((const char*)(_data + _offset), std::min(sizeof(T) << 1, remaining())); + ByteCountCoded codec = encoded; + result = codec.data; + encoded = codec; + _offset += encoded.size(); + } + + inline size_t remaining() const { + return _size - _offset; + } + + inline size_t offset() const { + return _offset; + } + + inline const uint8_t* data() const { + return _data; + } + +private: + + size_t _offset{ 0 }; + const uint8_t* const _data; + const size_t _size; +}; + +#endif From e885ac1821c26c7e3ebed8e92db38239e1770255 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 8 Jul 2015 17:32:51 -0700 Subject: [PATCH 26/30] improved performance of ByteCountCoding<> decode --- interface/src/Application.cpp | 1 + interface/src/Util.cpp | 72 +++++++++++++++++++++ interface/src/Util.h | 1 + libraries/shared/src/ByteCountCoding.h | 88 ++++++++++++++------------ 4 files changed, 123 insertions(+), 39 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 48fa53777d..cd15f2f4c7 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1014,6 +1014,7 @@ void Application::paintGL() { void Application::runTests() { runTimingTests(); + //runUnitTests(); } void Application::audioMuteToggled() { diff --git a/interface/src/Util.cpp b/interface/src/Util.cpp index bf4df3f3d2..5f04010b47 100644 --- a/interface/src/Util.cpp +++ b/interface/src/Util.cpp @@ -21,6 +21,7 @@ #include +#include #include #include @@ -229,6 +230,43 @@ void runTimingTests() { elapsedUsecs = (float)startTime.nsecsElapsed() * NSEC_TO_USEC; qCDebug(interfaceapp, "vec3 assign and dot() usecs: %f, last result:%f", (double)(elapsedUsecs / numTests), (double)result); + + + quint64 BYTE_CODE_MAX_VALUE = 99999999; + quint64 BYTE_CODE_TESTS_SKIP = 999; + + QByteArray extraJunk; + const int EXTRA_JUNK_SIZE = 200; + extraJunk.append((unsigned char)255); + for (int i = 0; i < EXTRA_JUNK_SIZE; i++) { + extraJunk.append(QString("junk")); + } + + { + startTime.start(); + quint64 tests = 0; + quint64 failed = 0; + for (quint64 value = 0; value < BYTE_CODE_MAX_VALUE; value += BYTE_CODE_TESTS_SKIP) { + quint64 valueA = value; // usecTimestampNow(); + ByteCountCoded codedValueA = valueA; + QByteArray codedValueABuffer = codedValueA; + codedValueABuffer.append(extraJunk); + ByteCountCoded decodedValueA; + decodedValueA.decode(codedValueABuffer); + quint64 valueADecoded = decodedValueA; + tests++; + if (valueA != valueADecoded) { + qDebug() << "FAILED! value:" << valueA << "decoded:" << valueADecoded; + failed++; + } + + } + elapsedUsecs = (float)startTime.nsecsElapsed() * NSEC_TO_USEC; + qCDebug(interfaceapp) << "ByteCountCoded usecs: " << elapsedUsecs + << "per test:" << (double) (elapsedUsecs / tests) + << "tests:" << tests + << "failed:" << failed; + } } bool rayIntersectsSphere(const glm::vec3& rayStarting, const glm::vec3& rayNormalizedDirection, @@ -271,3 +309,37 @@ bool pointInSphere(glm::vec3& point, glm::vec3& sphereCenter, double sphereRadiu } return false; } + +void runUnitTests() { + + quint64 LAST_TEST = 10; + quint64 SKIP_BY = 1; + + for (quint64 value = 0; value <= LAST_TEST; value += SKIP_BY) { + qDebug() << "value:" << value; + + ByteCountCoded codedValue = value; + + QByteArray codedValueBuffer = codedValue; + + codedValueBuffer.append((unsigned char)255); + codedValueBuffer.append(QString("junk")); + + qDebug() << "codedValueBuffer:"; + outputBufferBits((const unsigned char*)codedValueBuffer.constData(), codedValueBuffer.size()); + + ByteCountCoded valueDecoder = codedValueBuffer; + quint64 valueDecoded = valueDecoder; + qDebug() << "valueDecoded:" << valueDecoded; + + + if (value == valueDecoded) { + qDebug() << "SUCCESS!"; + } else { + qDebug() << "FAILED!"; + } + + } +} + + diff --git a/interface/src/Util.h b/interface/src/Util.h index ed05209747..d252c26bef 100644 --- a/interface/src/Util.h +++ b/interface/src/Util.h @@ -30,6 +30,7 @@ void drawText(int x, int y, float scale, float radians, int mono, void renderCollisionOverlay(int width, int height, float magnitude, float red = 0, float blue = 0, float green = 0); void runTimingTests(); +void runUnitTests(); bool rayIntersectsSphere(const glm::vec3& rayStarting, const glm::vec3& rayNormalizedDirection, const glm::vec3& sphereCenter, float sphereRadius, float& distance); diff --git a/libraries/shared/src/ByteCountCoding.h b/libraries/shared/src/ByteCountCoding.h index efde19d45b..88f07230be 100644 --- a/libraries/shared/src/ByteCountCoding.h +++ b/libraries/shared/src/ByteCountCoding.h @@ -26,6 +26,8 @@ #include #include +#include "SharedUtil.h" + #include "NumericalConstants.h" template class ByteCountCoded { @@ -40,6 +42,7 @@ public: QByteArray encode() const; void decode(const QByteArray& fromEncoded); + void decode(const char* encodedBuffer, int encodedSize); bool operator==(const ByteCountCoded& other) const { return data == other.data; } bool operator!=(const ByteCountCoded& other) const { return data != other.data; } @@ -113,50 +116,57 @@ template inline QByteArray ByteCountCoded::encode() const { } template inline void ByteCountCoded::decode(const QByteArray& fromEncodedBytes) { - // first convert the ByteArray into a BitArray... - QBitArray encodedBits; - int bitCount = BITS_IN_BYTE * fromEncodedBytes.count(); - encodedBits.resize(bitCount); - - // copies the QByteArray into a QBitArray - for(int byte = 0; byte < fromEncodedBytes.count(); byte++) { - char originalByte = fromEncodedBytes.at(byte); - for(int bit = 0; bit < BITS_IN_BYTE; bit++) { - int shiftBy = BITS_IN_BYTE - (bit + 1); - char maskBit = ( 1 << shiftBy); - bool bitValue = originalByte & maskBit; - encodedBits.setBit(byte * BITS_IN_BYTE + bit, bitValue); - } - } - - // next, read the leading bits to determine the correct number of bytes to decode (may not match the QByteArray) + decode(fromEncodedBytes.constData(), fromEncodedBytes.size()); +} + +template inline void ByteCountCoded::decode(const char* encodedBuffer, int encodedSize) { + data = 0; // reset data + int bitCount = BITS_IN_BYTE * encodedSize; + int encodedByteCount = 1; // there is at least 1 byte (after the leadBits) int leadBits = 1; // there is always at least 1 lead bit - int bitAt; - for (bitAt = 0; bitAt < bitCount; bitAt++) { - if (encodedBits.at(bitAt)) { - encodedByteCount++; - leadBits++; - } else { - break; - } - } - int expectedBitCount = (encodedByteCount * BITS_IN_BYTE) - leadBits; - - T value = 0; - - if (expectedBitCount <= (encodedBits.size() - leadBits)) { - // Now, keep reading... - int valueStartsAt = bitAt + 1; - T bitValue = 1; - for (bitAt = valueStartsAt; bitAt < (expectedBitCount + leadBits); bitAt++) { - if(encodedBits.at(bitAt)) { - value += bitValue; + bool inLeadBits = true; + int bitAt = 0; + int expectedBitCount; // unknown at this point + int lastValueBit; + T bitValue = 1; + + for(int byte = 0; byte < encodedSize; byte++) { + char originalByte = encodedBuffer[byte]; + unsigned char maskBit = 128; + for(int bit = 0; bit < BITS_IN_BYTE; bit++) { + //int shiftBy = BITS_IN_BYTE - (bit + 1); + //char maskBit = (1 << shiftBy); + bool bitIsSet = originalByte & maskBit; + + // Processing of the lead bits + if (inLeadBits) { + if (bitIsSet) { + encodedByteCount++; + leadBits++; + } else { + inLeadBits = false; // once we hit our first 0, we know we're out of the lead bits + expectedBitCount = (encodedByteCount * BITS_IN_BYTE) - leadBits; + lastValueBit = expectedBitCount + bitAt; + // check to see if the remainder of our buffer is sufficient + if (expectedBitCount > (bitCount - leadBits)) { + break; + } + } + } else { + if (bitAt > lastValueBit) { + break; + } + + if(bitIsSet) { + data += bitValue; + } + bitValue *= 2; } - bitValue *= 2; + bitAt++; + maskBit = maskBit >> 1; } } - data = value; } #endif // hifi_ByteCountCoding_h From c0cdf3256cca585fdc6f5d72304f45faf22c143f Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 8 Jul 2015 17:34:36 -0700 Subject: [PATCH 27/30] comment and dead code --- libraries/shared/src/ByteCountCoding.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/shared/src/ByteCountCoding.h b/libraries/shared/src/ByteCountCoding.h index 88f07230be..735c51a783 100644 --- a/libraries/shared/src/ByteCountCoding.h +++ b/libraries/shared/src/ByteCountCoding.h @@ -133,10 +133,8 @@ template inline void ByteCountCoded::decode(const char* encodedBu for(int byte = 0; byte < encodedSize; byte++) { char originalByte = encodedBuffer[byte]; - unsigned char maskBit = 128; + unsigned char maskBit = 128; // LEFT MOST BIT set for(int bit = 0; bit < BITS_IN_BYTE; bit++) { - //int shiftBy = BITS_IN_BYTE - (bit + 1); - //char maskBit = (1 << shiftBy); bool bitIsSet = originalByte & maskBit; // Processing of the lead bits @@ -148,6 +146,7 @@ template inline void ByteCountCoded::decode(const char* encodedBu inLeadBits = false; // once we hit our first 0, we know we're out of the lead bits expectedBitCount = (encodedByteCount * BITS_IN_BYTE) - leadBits; lastValueBit = expectedBitCount + bitAt; + // check to see if the remainder of our buffer is sufficient if (expectedBitCount > (bitCount - leadBits)) { break; From 600e9cbf52197467d3f02c945e85c6dba42a1185 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Wed, 8 Jul 2015 18:17:03 -0700 Subject: [PATCH 28/30] Fixing template specialization compilation error on gcc/clang --- libraries/shared/src/BufferParser.h | 91 +++++++++++++++-------------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/libraries/shared/src/BufferParser.h b/libraries/shared/src/BufferParser.h index 622d59d125..5d388a6f5f 100644 --- a/libraries/shared/src/BufferParser.h +++ b/libraries/shared/src/BufferParser.h @@ -26,54 +26,13 @@ public: } template - void readValue(T& result) { + inline void readValue(T& result) { Q_ASSERT(remaining() >= sizeof(T)); memcpy(&result, _data + _offset, sizeof(T)); _offset += sizeof(T); } - template<> - void readValue(quat& result) { - size_t advance = unpackOrientationQuatFromBytes(_data + _offset, result); - _offset += advance; - } - - template<> - void readValue(QString& result) { - uint16_t length; readValue(length); - result = QString((const char*)_data + _offset); - } - - template<> - void readValue(QUuid& result) { - uint16_t length; readValue(length); - Q_ASSERT(16 == length); - readUuid(result); - } - - template<> - void readValue(xColor& result) { - readValue(result.red); - readValue(result.blue); - readValue(result.green); - } - - template<> - void readValue>(QVector& result) { - uint16_t length; readValue(length); - result.resize(length); - memcpy(result.data(), _data + _offset, sizeof(glm::vec3) * length); - _offset += sizeof(glm::vec3) * length; - } - - template<> - void readValue(QByteArray& result) { - uint16_t length; readValue(length); - result = QByteArray((char*)_data + _offset, (int)length); - _offset += length; - } - - void readUuid(QUuid& result) { + inline void readUuid(QUuid& result) { readValue(result.data1); readValue(result.data2); readValue(result.data3); @@ -84,7 +43,7 @@ public: } template - void readFlags(PropertyFlags& result) { + inline void readFlags(PropertyFlags& result) { // FIXME doing heap allocation QByteArray encoded((const char*)(_data + _offset), remaining()); result.decode(encoded); @@ -92,7 +51,7 @@ public: } template - void readCompressedCount(T& result) { + inline void readCompressedCount(T& result) { // FIXME switch to a heapless implementation as soon as Brad provides it. QByteArray encoded((const char*)(_data + _offset), std::min(sizeof(T) << 1, remaining())); ByteCountCoded codec = encoded; @@ -120,4 +79,46 @@ private: const size_t _size; }; + +template<> +inline void BufferParser::readValue(quat& result) { + size_t advance = unpackOrientationQuatFromBytes(_data + _offset, result); + _offset += advance; +} + +template<> +inline void BufferParser::readValue(QString& result) { + uint16_t length; readValue(length); + result = QString((const char*)_data + _offset); +} + +template<> +inline void BufferParser::readValue(QUuid& result) { + uint16_t length; readValue(length); + Q_ASSERT(16 == length); + readUuid(result); +} + +template<> +inline void BufferParser::readValue(xColor& result) { + readValue(result.red); + readValue(result.blue); + readValue(result.green); +} + +template<> +inline void BufferParser::readValue(QVector& result) { + uint16_t length; readValue(length); + result.resize(length); + memcpy(result.data(), _data + _offset, sizeof(glm::vec3) * length); + _offset += sizeof(glm::vec3) * length; +} + +template<> +inline void BufferParser::readValue(QByteArray& result) { + uint16_t length; readValue(length); + result = QByteArray((char*)_data + _offset, (int)length); + _offset += length; +} + #endif From 7790e35d2497ca6ff631413ba943f9bf8ea63b3d Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 8 Jul 2015 18:19:12 -0700 Subject: [PATCH 29/30] added bytes consumed result to decode, and exit early after bytes consumed --- interface/src/Application.cpp | 2 +- interface/src/Util.cpp | 8 +++++--- libraries/shared/src/ByteCountCoding.h | 16 +++++++++++----- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index cd15f2f4c7..a834ce1288 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1014,7 +1014,7 @@ void Application::paintGL() { void Application::runTests() { runTimingTests(); - //runUnitTests(); + runUnitTests(); } void Application::audioMuteToggled() { diff --git a/interface/src/Util.cpp b/interface/src/Util.cpp index 5f04010b47..82f7d55b5d 100644 --- a/interface/src/Util.cpp +++ b/interface/src/Util.cpp @@ -232,7 +232,7 @@ void runTimingTests() { (double)(elapsedUsecs / numTests), (double)result); - quint64 BYTE_CODE_MAX_VALUE = 99999999; + quint64 BYTE_CODE_MAX_TEST_VALUE = 99999999; quint64 BYTE_CODE_TESTS_SKIP = 999; QByteArray extraJunk; @@ -246,7 +246,7 @@ void runTimingTests() { startTime.start(); quint64 tests = 0; quint64 failed = 0; - for (quint64 value = 0; value < BYTE_CODE_MAX_VALUE; value += BYTE_CODE_TESTS_SKIP) { + for (quint64 value = 0; value < BYTE_CODE_MAX_TEST_VALUE; value += BYTE_CODE_TESTS_SKIP) { quint64 valueA = value; // usecTimestampNow(); ByteCountCoded codedValueA = valueA; QByteArray codedValueABuffer = codedValueA; @@ -328,9 +328,11 @@ void runUnitTests() { qDebug() << "codedValueBuffer:"; outputBufferBits((const unsigned char*)codedValueBuffer.constData(), codedValueBuffer.size()); - ByteCountCoded valueDecoder = codedValueBuffer; + ByteCountCoded valueDecoder; + size_t bytesConsumed = valueDecoder.decode(codedValueBuffer); quint64 valueDecoded = valueDecoder; qDebug() << "valueDecoded:" << valueDecoded; + qDebug() << "bytesConsumed:" << bytesConsumed; if (value == valueDecoded) { diff --git a/libraries/shared/src/ByteCountCoding.h b/libraries/shared/src/ByteCountCoding.h index 735c51a783..ce6f121ddb 100644 --- a/libraries/shared/src/ByteCountCoding.h +++ b/libraries/shared/src/ByteCountCoding.h @@ -41,8 +41,8 @@ public: ByteCountCoded(const QByteArray& fromEncoded) : data(0) { decode(fromEncoded); } QByteArray encode() const; - void decode(const QByteArray& fromEncoded); - void decode(const char* encodedBuffer, int encodedSize); + size_t decode(const QByteArray& fromEncoded); + size_t decode(const char* encodedBuffer, int encodedSize); bool operator==(const ByteCountCoded& other) const { return data == other.data; } bool operator!=(const ByteCountCoded& other) const { return data != other.data; } @@ -115,12 +115,13 @@ template inline QByteArray ByteCountCoded::encode() const { return output; } -template inline void ByteCountCoded::decode(const QByteArray& fromEncodedBytes) { - decode(fromEncodedBytes.constData(), fromEncodedBytes.size()); +template inline size_t ByteCountCoded::decode(const QByteArray& fromEncodedBytes) { + return decode(fromEncodedBytes.constData(), fromEncodedBytes.size()); } -template inline void ByteCountCoded::decode(const char* encodedBuffer, int encodedSize) { +template inline size_t ByteCountCoded::decode(const char* encodedBuffer, int encodedSize) { data = 0; // reset data + size_t bytesConsumed = 0; int bitCount = BITS_IN_BYTE * encodedSize; int encodedByteCount = 1; // there is at least 1 byte (after the leadBits) @@ -133,6 +134,7 @@ template inline void ByteCountCoded::decode(const char* encodedBu for(int byte = 0; byte < encodedSize; byte++) { char originalByte = encodedBuffer[byte]; + bytesConsumed++; unsigned char maskBit = 128; // LEFT MOST BIT set for(int bit = 0; bit < BITS_IN_BYTE; bit++) { bool bitIsSet = originalByte & maskBit; @@ -165,7 +167,11 @@ template inline void ByteCountCoded::decode(const char* encodedBu bitAt++; maskBit = maskBit >> 1; } + if (!inLeadBits && bitAt > lastValueBit) { + break; + } } + return bytesConsumed; } #endif // hifi_ByteCountCoding_h From f2beb79d232af270d5af3249116d730c7eb8e5e5 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Wed, 8 Jul 2015 18:54:38 -0700 Subject: [PATCH 30/30] Updating buffer parser to use new heapless API --- libraries/shared/src/BufferParser.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libraries/shared/src/BufferParser.h b/libraries/shared/src/BufferParser.h index 5d388a6f5f..84bde2be31 100644 --- a/libraries/shared/src/BufferParser.h +++ b/libraries/shared/src/BufferParser.h @@ -53,11 +53,9 @@ public: template inline void readCompressedCount(T& result) { // FIXME switch to a heapless implementation as soon as Brad provides it. - QByteArray encoded((const char*)(_data + _offset), std::min(sizeof(T) << 1, remaining())); - ByteCountCoded codec = encoded; + ByteCountCoded codec; + _offset += codec.decode(reinterpret_cast(_data + _offset), remaining()); result = codec.data; - encoded = codec; - _offset += encoded.size(); } inline size_t remaining() const {