From b563660bd11839b700c528447d2d147ed948fd2e Mon Sep 17 00:00:00 2001 From: atlante45 Date: Fri, 7 Jun 2013 20:14:08 +0200 Subject: [PATCH 01/13] #19366 - Changing log caracters proportions so the log is smaller and better looking. --- interface/src/LogDisplay.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/LogDisplay.h b/interface/src/LogDisplay.h index e2f9ad8dd4..f699cb5279 100644 --- a/interface/src/LogDisplay.h +++ b/interface/src/LogDisplay.h @@ -27,7 +27,7 @@ public: static unsigned const TEXT_COLOR = 0xb299ff; // text foreground color (bytes, RGB) static FILE* const DEFAULT_STREAM; // = stdout; // stream to also log to (defined in .cpp) - static unsigned const DEFAULT_CHAR_WIDTH = 7; // width of a single character + static unsigned const DEFAULT_CHAR_WIDTH = 5; // width of a single character static unsigned const DEFAULT_CHAR_HEIGHT = 16; // height of a single character static unsigned const DEFAULT_CONSOLE_WIDTH = 400; // width of the (right-aligned) log console From 906fc04e35f01e09d265694b8a69b648099a128a Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 10 Jul 2013 17:35:39 -0700 Subject: [PATCH 02/13] first step toward resizing avatars --- interface/src/Avatar.cpp | 87 ++++++++++++++++++++++++++----------- interface/src/Avatar.h | 26 +++++++++++ interface/src/Head.cpp | 76 +++++++++++++++++--------------- interface/src/Skeleton.cpp | 88 ++++++++++++++++++++++++++------------ interface/src/Skeleton.h | 1 + 5 files changed, 191 insertions(+), 87 deletions(-) diff --git a/interface/src/Avatar.cpp b/interface/src/Avatar.cpp index c2c850c923..9b24625af1 100755 --- a/interface/src/Avatar.cpp +++ b/interface/src/Avatar.cpp @@ -84,6 +84,7 @@ Avatar::Avatar(Node* owningNode) : _leanScale(0.5f), _pelvisStandingHeight(0.0f), _pelvisFloatingHeight(0.0f), + _uniformScaler(1.0f), _distanceToNearestAvatar(std::numeric_limits::max()), _gravity(0.0f, -1.0f, 0.0f), _worldUpDirection(DEFAULT_UP_DIRECTION), @@ -108,6 +109,8 @@ Avatar::Avatar(Node* owningNode) : initializeBodyBalls(); + //uniformScale(0.5f); + _height = _skeleton.getHeight() + _bodyBall[ BODY_BALL_LEFT_HEEL ].radius + _bodyBall[ BODY_BALL_HEAD_BASE ].radius; _maxArmLength = _skeleton.getArmLength(); @@ -141,32 +144,32 @@ void Avatar::initializeBodyBalls() { } // specify the radius of each ball - _bodyBall[ BODY_BALL_PELVIS ].radius = 0.07; - _bodyBall[ BODY_BALL_TORSO ].radius = 0.065; - _bodyBall[ BODY_BALL_CHEST ].radius = 0.08; - _bodyBall[ BODY_BALL_NECK_BASE ].radius = 0.03; - _bodyBall[ BODY_BALL_HEAD_BASE ].radius = 0.07; - _bodyBall[ BODY_BALL_LEFT_COLLAR ].radius = 0.04; - _bodyBall[ BODY_BALL_LEFT_SHOULDER ].radius = 0.03; - _bodyBall[ BODY_BALL_LEFT_ELBOW ].radius = 0.02; - _bodyBall[ BODY_BALL_LEFT_WRIST ].radius = 0.02; - _bodyBall[ BODY_BALL_LEFT_FINGERTIPS ].radius = 0.01; - _bodyBall[ BODY_BALL_RIGHT_COLLAR ].radius = 0.04; - _bodyBall[ BODY_BALL_RIGHT_SHOULDER ].radius = 0.03; - _bodyBall[ BODY_BALL_RIGHT_ELBOW ].radius = 0.02; - _bodyBall[ BODY_BALL_RIGHT_WRIST ].radius = 0.02; - _bodyBall[ BODY_BALL_RIGHT_FINGERTIPS ].radius = 0.01; - _bodyBall[ BODY_BALL_LEFT_HIP ].radius = 0.04; + _bodyBall[ BODY_BALL_PELVIS ].radius = BODY_BALL_RADIUS_PELVIS; + _bodyBall[ BODY_BALL_TORSO ].radius = BODY_BALL_RADIUS_TORSO; + _bodyBall[ BODY_BALL_CHEST ].radius = BODY_BALL_RADIUS_CHEST; + _bodyBall[ BODY_BALL_NECK_BASE ].radius = BODY_BALL_RADIUS_NECK_BASE; + _bodyBall[ BODY_BALL_HEAD_BASE ].radius = BODY_BALL_RADIUS_HEAD_BASE; + _bodyBall[ BODY_BALL_LEFT_COLLAR ].radius = BODY_BALL_RADIUS_LEFT_COLLAR; + _bodyBall[ BODY_BALL_LEFT_SHOULDER ].radius = BODY_BALL_RADIUS_LEFT_SHOULDER; + _bodyBall[ BODY_BALL_LEFT_ELBOW ].radius = BODY_BALL_RADIUS_LEFT_ELBOW; + _bodyBall[ BODY_BALL_LEFT_WRIST ].radius = BODY_BALL_RADIUS_LEFT_WRIST; + _bodyBall[ BODY_BALL_LEFT_FINGERTIPS ].radius = BODY_BALL_RADIUS_LEFT_FINGERTIPS; + _bodyBall[ BODY_BALL_RIGHT_COLLAR ].radius = BODY_BALL_RADIUS_RIGHT_COLLAR; + _bodyBall[ BODY_BALL_RIGHT_SHOULDER ].radius = BODY_BALL_RADIUS_RIGHT_SHOULDER; + _bodyBall[ BODY_BALL_RIGHT_ELBOW ].radius = BODY_BALL_RADIUS_RIGHT_ELBOW; + _bodyBall[ BODY_BALL_RIGHT_WRIST ].radius = BODY_BALL_RADIUS_RIGHT_WRIST; + _bodyBall[ BODY_BALL_RIGHT_FINGERTIPS ].radius = BODY_BALL_RADIUS_RIGHT_FINGERTIPS; + _bodyBall[ BODY_BALL_LEFT_HIP ].radius = BODY_BALL_RADIUS_LEFT_HIP; - //_bodyBall[ BODY_BALL_LEFT_MID_THIGH ].radius = 0.03; + //_bodyBall[ BODY_BALL_LEFT_MID_THIGH ].radius = BODY_BALL_RADIUS_LEFT_MID_THIGH; - _bodyBall[ BODY_BALL_LEFT_KNEE ].radius = 0.025; - _bodyBall[ BODY_BALL_LEFT_HEEL ].radius = 0.025; - _bodyBall[ BODY_BALL_LEFT_TOES ].radius = 0.025; - _bodyBall[ BODY_BALL_RIGHT_HIP ].radius = 0.04; - _bodyBall[ BODY_BALL_RIGHT_KNEE ].radius = 0.025; - _bodyBall[ BODY_BALL_RIGHT_HEEL ].radius = 0.025; - _bodyBall[ BODY_BALL_RIGHT_TOES ].radius = 0.025; + _bodyBall[ BODY_BALL_LEFT_KNEE ].radius = BODY_BALL_RADIUS_LEFT_KNEE; + _bodyBall[ BODY_BALL_LEFT_HEEL ].radius = BODY_BALL_RADIUS_LEFT_HEEL; + _bodyBall[ BODY_BALL_LEFT_TOES ].radius = BODY_BALL_RADIUS_LEFT_TOES; + _bodyBall[ BODY_BALL_RIGHT_HIP ].radius = BODY_BALL_RADIUS_RIGHT_HIP; + _bodyBall[ BODY_BALL_RIGHT_KNEE ].radius = BODY_BALL_RADIUS_RIGHT_KNEE; + _bodyBall[ BODY_BALL_RIGHT_HEEL ].radius = BODY_BALL_RADIUS_RIGHT_HEEL; + _bodyBall[ BODY_BALL_RIGHT_TOES ].radius = BODY_BALL_RADIUS_RIGHT_TOES; // specify the parent joint for each ball @@ -675,7 +678,7 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { _head.setBodyRotation (glm::vec3(_bodyPitch, _bodyYaw, _bodyRoll)); _head.setPosition(_bodyBall[ BODY_BALL_HEAD_BASE ].position); - _head.setScale (_bodyBall[ BODY_BALL_HEAD_BASE ].radius); + _head.setScale (_uniformScaler); _head.setSkinColor(glm::vec3(SKIN_COLOR[0], SKIN_COLOR[1], SKIN_COLOR[2])); _head.simulate(deltaTime, isMyAvatar()); @@ -1400,3 +1403,37 @@ void Avatar::renderJointConnectingCone(glm::vec3 position1, glm::vec3 position2, glEnd(); } + +void Avatar::uniformScale(float uniformScaler) { + _uniformScaler *= uniformScaler; + + _skeleton.uniformScale(_uniformScaler); + + // specify the new radius of each ball + _bodyBall[ BODY_BALL_PELVIS ].radius = _uniformScaler * BODY_BALL_RADIUS_PELVIS; + _bodyBall[ BODY_BALL_TORSO ].radius = _uniformScaler * BODY_BALL_RADIUS_TORSO; + _bodyBall[ BODY_BALL_CHEST ].radius = _uniformScaler * BODY_BALL_RADIUS_CHEST; + _bodyBall[ BODY_BALL_NECK_BASE ].radius = _uniformScaler * BODY_BALL_RADIUS_NECK_BASE; + _bodyBall[ BODY_BALL_HEAD_BASE ].radius = _uniformScaler * BODY_BALL_RADIUS_HEAD_BASE; + _bodyBall[ BODY_BALL_LEFT_COLLAR ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_COLLAR; + _bodyBall[ BODY_BALL_LEFT_SHOULDER ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_SHOULDER; + _bodyBall[ BODY_BALL_LEFT_ELBOW ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_ELBOW; + _bodyBall[ BODY_BALL_LEFT_WRIST ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_WRIST; + _bodyBall[ BODY_BALL_LEFT_FINGERTIPS ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_FINGERTIPS; + _bodyBall[ BODY_BALL_RIGHT_COLLAR ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_COLLAR; + _bodyBall[ BODY_BALL_RIGHT_SHOULDER ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_SHOULDER; + _bodyBall[ BODY_BALL_RIGHT_ELBOW ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_ELBOW; + _bodyBall[ BODY_BALL_RIGHT_WRIST ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_WRIST; + _bodyBall[ BODY_BALL_RIGHT_FINGERTIPS ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_FINGERTIPS; + _bodyBall[ BODY_BALL_LEFT_HIP ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_HIP; + + //_bodyBall[ BODY_BALL_LEFT_MID_THIGH ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_MID_THIGH; + + _bodyBall[ BODY_BALL_LEFT_KNEE ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_KNEE; + _bodyBall[ BODY_BALL_LEFT_HEEL ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_HEEL; + _bodyBall[ BODY_BALL_LEFT_TOES ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_TOES; + _bodyBall[ BODY_BALL_RIGHT_HIP ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_HIP; + _bodyBall[ BODY_BALL_RIGHT_KNEE ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_KNEE; + _bodyBall[ BODY_BALL_RIGHT_HEEL ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_HEEL; + _bodyBall[ BODY_BALL_RIGHT_TOES ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_TOES; +} diff --git a/interface/src/Avatar.h b/interface/src/Avatar.h index 855d49e51c..969db962d9 100755 --- a/interface/src/Avatar.h +++ b/interface/src/Avatar.h @@ -23,6 +23,30 @@ #include "Skeleton.h" #include "Transmitter.h" +const float BODY_BALL_RADIUS_PELVIS = 0.07; +const float BODY_BALL_RADIUS_TORSO = 0.065; +const float BODY_BALL_RADIUS_CHEST = 0.08; +const float BODY_BALL_RADIUS_NECK_BASE = 0.03; +const float BODY_BALL_RADIUS_HEAD_BASE = 0.07; +const float BODY_BALL_RADIUS_LEFT_COLLAR = 0.04; +const float BODY_BALL_RADIUS_LEFT_SHOULDER = 0.03; +const float BODY_BALL_RADIUS_LEFT_ELBOW = 0.02; +const float BODY_BALL_RADIUS_LEFT_WRIST = 0.02; +const float BODY_BALL_RADIUS_LEFT_FINGERTIPS = 0.01; +const float BODY_BALL_RADIUS_RIGHT_COLLAR = 0.04; +const float BODY_BALL_RADIUS_RIGHT_SHOULDER = 0.03; +const float BODY_BALL_RADIUS_RIGHT_ELBOW = 0.02; +const float BODY_BALL_RADIUS_RIGHT_WRIST = 0.02; +const float BODY_BALL_RADIUS_RIGHT_FINGERTIPS = 0.01; +const float BODY_BALL_RADIUS_LEFT_HIP = 0.04; +const float BODY_BALL_RADIUS_LEFT_MID_THIGH = 0.03; +const float BODY_BALL_RADIUS_LEFT_KNEE = 0.025; +const float BODY_BALL_RADIUS_LEFT_HEEL = 0.025; +const float BODY_BALL_RADIUS_LEFT_TOES = 0.025; +const float BODY_BALL_RADIUS_RIGHT_HIP = 0.04; +const float BODY_BALL_RADIUS_RIGHT_KNEE = 0.025; +const float BODY_BALL_RADIUS_RIGHT_HEEL = 0.025; +const float BODY_BALL_RADIUS_RIGHT_TOES = 0.025; enum AvatarBodyBallID { @@ -203,6 +227,7 @@ private: float _pelvisStandingHeight; float _pelvisFloatingHeight; float _pelvisToHeadLength; + float _uniformScaler; float _height; Balls* _balls; AvatarTouch _avatarTouch; @@ -238,6 +263,7 @@ private: void applyHardCollision(const glm::vec3& penetration, float elasticity, float damping); void applyCollisionWithOtherAvatar( Avatar * other, float deltaTime ); void checkForMouseRayTouching(); + void uniformScale(float uniformScaler); }; #endif diff --git a/interface/src/Head.cpp b/interface/src/Head.cpp index 6bf6c65341..5eae0faf9b 100644 --- a/interface/src/Head.cpp +++ b/interface/src/Head.cpp @@ -130,7 +130,7 @@ void Head::resetHairPhysics() { _hairTuft[t].length = HAIR_LENGTH; _hairTuft[t].thickness = HAIR_THICKNESS; - _hairTuft[t].basePosition = _position + up * _scale * 0.9f; + _hairTuft[t].basePosition = _position + up * _scale * BODY_BALL_RADIUS_HEAD_BASE * 0.9f; _hairTuft[t].midPosition = _hairTuft[t].basePosition + up * _hairTuft[t].length * ONE_HALF; _hairTuft[t].endPosition = _hairTuft[t].midPosition + up * _hairTuft[t].length * ONE_HALF; _hairTuft[t].midVelocity = glm::vec3(0.0f, 0.0f, 0.0f); @@ -261,25 +261,25 @@ void Head::calculateGeometry() { //calculate the eye positions _leftEyePosition = _position - - right * _scale * EYE_RIGHT_OFFSET - + up * _scale * EYE_UP_OFFSET - + front * _scale * EYE_FRONT_OFFSET; + - right * _scale * BODY_BALL_RADIUS_HEAD_BASE * EYE_RIGHT_OFFSET + + up * _scale * BODY_BALL_RADIUS_HEAD_BASE * EYE_UP_OFFSET + + front * _scale * BODY_BALL_RADIUS_HEAD_BASE * EYE_FRONT_OFFSET; _rightEyePosition = _position - + right * _scale * EYE_RIGHT_OFFSET - + up * _scale * EYE_UP_OFFSET - + front * _scale * EYE_FRONT_OFFSET; + + right * _scale * BODY_BALL_RADIUS_HEAD_BASE * EYE_RIGHT_OFFSET + + up * _scale * BODY_BALL_RADIUS_HEAD_BASE * EYE_UP_OFFSET + + front * _scale * BODY_BALL_RADIUS_HEAD_BASE * EYE_FRONT_OFFSET; //calculate the eyebrow positions _leftEyeBrowPosition = _leftEyePosition; _rightEyeBrowPosition = _rightEyePosition; //calculate the ear positions - _leftEarPosition = _position - right * _scale * EAR_RIGHT_OFFSET; - _rightEarPosition = _position + right * _scale * EAR_RIGHT_OFFSET; + _leftEarPosition = _position - right * _scale * BODY_BALL_RADIUS_HEAD_BASE * EAR_RIGHT_OFFSET; + _rightEarPosition = _position + right * _scale * BODY_BALL_RADIUS_HEAD_BASE * EAR_RIGHT_OFFSET; //calculate the mouth position - _mouthPosition = _position + up * _scale * MOUTH_UP_OFFSET - + front * _scale; + _mouthPosition = _position + up * _scale * BODY_BALL_RADIUS_HEAD_BASE * MOUTH_UP_OFFSET + + front * _scale * BODY_BALL_RADIUS_HEAD_BASE; } @@ -295,11 +295,11 @@ void Head::render(bool lookingInMirror, float alpha) { renderMohawk(); renderHeadSphere(); - renderEyeBalls(); + renderEyeBalls(); renderEars(); renderMouth(); renderEyeBrows(); - + if (_renderLookatVectors) { renderLookatVectors(_leftEyePosition, _rightEyePosition, _lookAtPosition); } @@ -404,7 +404,9 @@ glm::quat Head::getCameraOrientation () const { void Head::renderHeadSphere() { glPushMatrix(); glTranslatef(_position.x, _position.y, _position.z); //translate to head position - glScalef(_scale, _scale, _scale); //scale to head size + glScalef(_scale * BODY_BALL_RADIUS_HEAD_BASE, + _scale * BODY_BALL_RADIUS_HEAD_BASE, + _scale * BODY_BALL_RADIUS_HEAD_BASE); //scale to head size glColor4f(_skinColor.x, _skinColor.y, _skinColor.z, _renderAlpha); glutSolidSphere(1, 30, 30); glPopMatrix(); @@ -415,13 +417,13 @@ void Head::renderEars() { glPushMatrix(); glColor4f(_skinColor.x, _skinColor.y, _skinColor.z, _renderAlpha); glTranslatef(_leftEarPosition.x, _leftEarPosition.y, _leftEarPosition.z); - glutSolidSphere(0.02, 30, 30); + glutSolidSphere(_scale * 0.02, 30, 30); glPopMatrix(); glPushMatrix(); glColor4f(_skinColor.x, _skinColor.y, _skinColor.z, _renderAlpha); glTranslatef(_rightEarPosition.x, _rightEarPosition.y, _rightEarPosition.z); - glutSolidSphere(0.02, 30, 30); + glutSolidSphere(_scale * 0.02, 30, 30); glPopMatrix(); } @@ -434,9 +436,9 @@ void Head::renderMouth() { glm::vec3 up = orientation * IDENTITY_UP; glm::vec3 front = orientation * IDENTITY_FRONT; - glm::vec3 r = right * _scale * (0.30f + s * 0.0014f ); - glm::vec3 u = up * _scale * (0.05f + s * 0.0040f ); - glm::vec3 f = front * _scale * 0.09f; + glm::vec3 r = right * _scale * BODY_BALL_RADIUS_HEAD_BASE * (0.30f + s * 0.0014f ); + glm::vec3 u = up * _scale * BODY_BALL_RADIUS_HEAD_BASE * (0.05f + s * 0.0040f ); + glm::vec3 f = front * _scale * BODY_BALL_RADIUS_HEAD_BASE * 0.09f; glm::vec3 middle = _mouthPosition; glm::vec3 leftCorner = _mouthPosition - r * 1.0f; @@ -449,7 +451,7 @@ void Head::renderMouth() { // constrain all mouth vertices to a sphere slightly larger than the head... const float MOUTH_OFFSET_OFF_FACE = 0.003f; - float constrainedRadius = _scale + MOUTH_OFFSET_OFF_FACE; + float constrainedRadius = _scale * BODY_BALL_RADIUS_HEAD_BASE + MOUTH_OFFSET_OFF_FACE; middle = _position + glm::normalize(middle - _position) * constrainedRadius; leftCorner = _position + glm::normalize(leftCorner - _position) * constrainedRadius; rightCorner = _position + glm::normalize(rightCorner - _position) * constrainedRadius; @@ -484,9 +486,9 @@ void Head::renderMouth() { void Head::renderEyeBrows() { - float height = _scale * 0.3f + _browAudioLift; - float length = _scale * 0.2f; - float width = _scale * 0.07f; + float height = _scale * BODY_BALL_RADIUS_HEAD_BASE * 0.3f + _browAudioLift; + float length = _scale * BODY_BALL_RADIUS_HEAD_BASE * 0.2f; + float width = _scale * BODY_BALL_RADIUS_HEAD_BASE * 0.07f; glColor3f(0.3f, 0.25f, 0.2f); @@ -505,7 +507,7 @@ void Head::renderEyeBrows() { glm::vec3 r = right * length; glm::vec3 u = up * height; glm::vec3 t = up * (height + width); - glm::vec3 f = front * _scale * -0.1f; + glm::vec3 f = front * _scale * BODY_BALL_RADIUS_HEAD_BASE * -0.1f; for (int i = 0; i < 2; i++) { @@ -553,14 +555,14 @@ void Head::renderEyeBalls() { glPushMatrix(); glColor3fv(EYEBALL_COLOR); glTranslatef(_leftEyePosition.x, _leftEyePosition.y, _leftEyePosition.z); - glutSolidSphere(EYEBALL_RADIUS, 30, 30); + glutSolidSphere(_scale * EYEBALL_RADIUS, 30, 30); glPopMatrix(); //render white ball of right eyeball glPushMatrix(); glColor3fv(EYEBALL_COLOR); glTranslatef(_rightEyePosition.x, _rightEyePosition.y, _rightEyePosition.z); - glutSolidSphere(EYEBALL_RADIUS, 30, 30); + glutSolidSphere(_scale * EYEBALL_RADIUS, 30, 30); glPopMatrix(); _irisProgram->bind(); @@ -580,13 +582,15 @@ void Head::renderEyeBalls() { glm::vec3 rotationAxis = glm::axis(rotation); glRotatef(glm::angle(rotation), rotationAxis.x, rotationAxis.y, rotationAxis.z); glTranslatef(0.0f, 0.0f, -IRIS_PROTRUSION); - glScalef(IRIS_RADIUS * 2.0f, IRIS_RADIUS * 2.0f, IRIS_RADIUS); // flatten the iris + glScalef(_scale * IRIS_RADIUS * 2.0f, + _scale * IRIS_RADIUS * 2.0f, + _scale * IRIS_RADIUS); // flatten the iris // this ugliness is simply to invert the model transform and get the eye position in model space _irisProgram->setUniform(_eyePositionLocation, (glm::inverse(rotation) * (Application::getInstance()->getCamera()->getPosition() - _leftEyePosition) + - glm::vec3(0.0f, 0.0f, IRIS_PROTRUSION)) * glm::vec3(1.0f / (IRIS_RADIUS * 2.0f), - 1.0f / (IRIS_RADIUS * 2.0f), 1.0f / IRIS_RADIUS)); + glm::vec3(0.0f, 0.0f, _scale * IRIS_PROTRUSION)) * glm::vec3(1.0f / (_scale * IRIS_RADIUS * 2.0f), + 1.0f / (_scale * IRIS_RADIUS * 2.0f), 1.0f / _scale * IRIS_RADIUS)); glutSolidSphere(0.5f, 15, 15); } @@ -602,13 +606,15 @@ void Head::renderEyeBalls() { glm::vec3 rotationAxis = glm::axis(rotation); glRotatef(glm::angle(rotation), rotationAxis.x, rotationAxis.y, rotationAxis.z); glTranslatef(0.0f, 0.0f, -IRIS_PROTRUSION); - glScalef(IRIS_RADIUS * 2.0f, IRIS_RADIUS * 2.0f, IRIS_RADIUS); // flatten the iris + glScalef(_scale * IRIS_RADIUS * 2.0f, + _scale * IRIS_RADIUS * 2.0f, + _scale * IRIS_RADIUS); // flatten the iris // this ugliness is simply to invert the model transform and get the eye position in model space _irisProgram->setUniform(_eyePositionLocation, (glm::inverse(rotation) * (Application::getInstance()->getCamera()->getPosition() - _rightEyePosition) + - glm::vec3(0.0f, 0.0f, IRIS_PROTRUSION)) * glm::vec3(1.0f / (IRIS_RADIUS * 2.0f), - 1.0f / (IRIS_RADIUS * 2.0f), 1.0f / IRIS_RADIUS)); + glm::vec3(0.0f, 0.0f, _scale * IRIS_PROTRUSION)) * glm::vec3(1.0f / (_scale * IRIS_RADIUS * 2.0f), + 1.0f / (_scale * IRIS_RADIUS * 2.0f), 1.0f / _scale * IRIS_RADIUS)); glutSolidSphere(0.5f, 15, 15); } @@ -626,7 +632,7 @@ void Head::renderEyeBalls() { glTranslatef(_leftEyePosition.x, _leftEyePosition.y, _leftEyePosition.z); //translate to eyeball position glm::vec3 rotationAxis = glm::axis(orientation); glRotatef(glm::angle(orientation), rotationAxis.x, rotationAxis.y, rotationAxis.z); - glScalef(EYELID_RADIUS, EYELID_RADIUS, EYELID_RADIUS); + glScalef(_scale * EYELID_RADIUS, _scale * EYELID_RADIUS, _scale * EYELID_RADIUS); glRotatef(-90 * _leftEyeBlink, 1, 0, 0); Application::getInstance()->getGeometryCache()->renderHemisphere(15, 10); glRotatef(180 * _leftEyeBlink, 1, 0, 0); @@ -639,7 +645,7 @@ void Head::renderEyeBalls() { glTranslatef(_rightEyePosition.x, _rightEyePosition.y, _rightEyePosition.z); //translate to eyeball position glm::vec3 rotationAxis = glm::axis(orientation); glRotatef(glm::angle(orientation), rotationAxis.x, rotationAxis.y, rotationAxis.z); - glScalef(EYELID_RADIUS, EYELID_RADIUS, EYELID_RADIUS); + glScalef(_scale * EYELID_RADIUS, _scale * EYELID_RADIUS, _scale * EYELID_RADIUS); glRotatef(-90 * _rightEyeBlink, 1, 0, 0); Application::getInstance()->getGeometryCache()->renderHemisphere(15, 10); glRotatef(180 * _rightEyeBlink, 1, 0, 0); @@ -682,7 +688,7 @@ void Head::updateHairPhysics(float deltaTime) { = front * sinf(radian) + up * cosf(radian); - _hairTuft[t].basePosition = _position + _scale * 0.9f * baseDirection; + _hairTuft[t].basePosition = _position + _scale * BODY_BALL_RADIUS_HEAD_BASE * 0.9f * baseDirection; glm::vec3 midAxis = _hairTuft[t].midPosition - _hairTuft[t].basePosition; glm::vec3 endAxis = _hairTuft[t].endPosition - _hairTuft[t].midPosition; diff --git a/interface/src/Skeleton.cpp b/interface/src/Skeleton.cpp index 0d166abfa1..0743e185eb 100644 --- a/interface/src/Skeleton.cpp +++ b/interface/src/Skeleton.cpp @@ -11,10 +11,39 @@ const float BODY_SPRING_DEFAULT_TIGHTNESS = 1000.0f; const float FLOATING_HEIGHT = 0.13f; +const glm::vec3 AVATAR_JOINT_POSITION_PELVIS = glm::vec3(0.0, 0.0, 0.0 ); +const glm::vec3 AVATAR_JOINT_POSITION_TORSO = glm::vec3( 0.0, 0.09, -0.01); +const glm::vec3 AVATAR_JOINT_POSITION_CHEST = glm::vec3( 0.0, 0.09, -0.01); +const glm::vec3 AVATAR_JOINT_POSITION_NECK_BASE = glm::vec3( 0.0, 0.14, 0.01); +const glm::vec3 AVATAR_JOINT_POSITION_HEAD_BASE = glm::vec3( 0.0, 0.04, 0.00); +const glm::vec3 AVATAR_JOINT_POSITION_HEAD_TOP = glm::vec3( 0.0, 0.04, 0.00); + +const glm::vec3 AVATAR_JOINT_POSITION_LEFT_COLLAR = glm::vec3(-0.06, 0.04, 0.01); +const glm::vec3 AVATAR_JOINT_POSITION_LEFT_SHOULDER = glm::vec3(-0.05, 0.0 , 0.01); +const glm::vec3 AVATAR_JOINT_POSITION_LEFT_ELBOW = glm::vec3(-0.16, 0.0 , 0.0 ); +const glm::vec3 AVATAR_JOINT_POSITION_LEFT_WRIST = glm::vec3(-0.12, 0.0 , 0.0 ); +const glm::vec3 AVATAR_JOINT_POSITION_LEFT_FINGERTIPS = glm::vec3(-0.1, 0.0 , 0.0 ); + +const glm::vec3 AVATAR_JOINT_POSITION_RIGHT_COLLAR = glm::vec3( 0.06, 0.04, 0.01); +const glm::vec3 AVATAR_JOINT_POSITION_RIGHT_SHOULDER = glm::vec3( 0.05, 0.0 , 0.01); +const glm::vec3 AVATAR_JOINT_POSITION_RIGHT_ELBOW = glm::vec3( 0.16, 0.0 , 0.0 ); +const glm::vec3 AVATAR_JOINT_POSITION_RIGHT_WRIST = glm::vec3( 0.12, 0.0 , 0.0 ); +const glm::vec3 AVATAR_JOINT_POSITION_RIGHT_FINGERTIPS = glm::vec3( 0.1, 0.0 , 0.0 ); + +const glm::vec3 AVATAR_JOINT_POSITION_LEFT_HIP = glm::vec3(-0.05, 0.0 , 0.02); +const glm::vec3 AVATAR_JOINT_POSITION_LEFT_KNEE = glm::vec3( 0.00, -0.25, 0.00); +const glm::vec3 AVATAR_JOINT_POSITION_LEFT_HEEL = glm::vec3( 0.00, -0.23, 0.00); +const glm::vec3 AVATAR_JOINT_POSITION_LEFT_TOES = glm::vec3( 0.00, 0.00, -0.06); + +const glm::vec3 AVATAR_JOINT_POSITION_RIGHT_HIP = glm::vec3( 0.05, 0.0 , 0.02); +const glm::vec3 AVATAR_JOINT_POSITION_RIGHT_KNEE = glm::vec3( 0.00, -0.25, 0.00); +const glm::vec3 AVATAR_JOINT_POSITION_RIGHT_HEEL = glm::vec3( 0.00, -0.23, 0.00); +const glm::vec3 AVATAR_JOINT_POSITION_RIGHT_TOES = glm::vec3( 0.00, 0.00, -0.06); + Skeleton::Skeleton() { } -void Skeleton::initialize() { +void Skeleton::initialize() { for (int b = 0; b < NUM_AVATAR_JOINTS; b++) { joint[b].parent = AVATAR_JOINT_NULL; @@ -61,34 +90,39 @@ void Skeleton::initialize() { joint[ AVATAR_JOINT_RIGHT_TOES ].parent = AVATAR_JOINT_RIGHT_HEEL; // specify the bind pose position - joint[ AVATAR_JOINT_PELVIS ].bindPosePosition = glm::vec3( 0.0, 0.0, 0.0 ); - joint[ AVATAR_JOINT_TORSO ].bindPosePosition = glm::vec3( 0.0, 0.09, -0.01 ); - joint[ AVATAR_JOINT_CHEST ].bindPosePosition = glm::vec3( 0.0, 0.09, -0.01 ); - joint[ AVATAR_JOINT_NECK_BASE ].bindPosePosition = glm::vec3( 0.0, 0.14, 0.01 ); - joint[ AVATAR_JOINT_HEAD_BASE ].bindPosePosition = glm::vec3( 0.0, 0.04, 0.00 ); - joint[ AVATAR_JOINT_HEAD_TOP ].bindPosePosition = glm::vec3( 0.0, 0.04, 0.00 ); + uniformScale(1.0f); +} + +void Skeleton::uniformScale(float uniformScaler) { + // specify the bind pose position + joint[ AVATAR_JOINT_PELVIS ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_PELVIS; + joint[ AVATAR_JOINT_TORSO ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_TORSO; + joint[ AVATAR_JOINT_CHEST ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_CHEST; + joint[ AVATAR_JOINT_NECK_BASE ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_NECK_BASE; + joint[ AVATAR_JOINT_HEAD_BASE ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_HEAD_BASE; + joint[ AVATAR_JOINT_HEAD_TOP ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_HEAD_TOP; - joint[ AVATAR_JOINT_LEFT_COLLAR ].bindPosePosition = glm::vec3( -0.06, 0.04, 0.01 ); - joint[ AVATAR_JOINT_LEFT_SHOULDER ].bindPosePosition = glm::vec3( -0.05, 0.0, 0.01 ); - joint[ AVATAR_JOINT_LEFT_ELBOW ].bindPosePosition = glm::vec3( -0.16, 0.0, 0.0 ); - joint[ AVATAR_JOINT_LEFT_WRIST ].bindPosePosition = glm::vec3( -0.12, 0.0, 0.0 ); - joint[ AVATAR_JOINT_LEFT_FINGERTIPS ].bindPosePosition = glm::vec3( -0.1, 0.0, 0.0 ); + joint[ AVATAR_JOINT_LEFT_COLLAR ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_COLLAR; + joint[ AVATAR_JOINT_LEFT_SHOULDER ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_SHOULDER; + joint[ AVATAR_JOINT_LEFT_ELBOW ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_ELBOW; + joint[ AVATAR_JOINT_LEFT_WRIST ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_WRIST; + joint[ AVATAR_JOINT_LEFT_FINGERTIPS ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_FINGERTIPS; - joint[ AVATAR_JOINT_RIGHT_COLLAR ].bindPosePosition = glm::vec3( 0.06, 0.04, 0.01 ); - joint[ AVATAR_JOINT_RIGHT_SHOULDER ].bindPosePosition = glm::vec3( 0.05, 0.0, 0.01 ); - joint[ AVATAR_JOINT_RIGHT_ELBOW ].bindPosePosition = glm::vec3( 0.16, 0.0, 0.0 ); - joint[ AVATAR_JOINT_RIGHT_WRIST ].bindPosePosition = glm::vec3( 0.12, 0.0, 0.0 ); - joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].bindPosePosition = glm::vec3( 0.1, 0.0, 0.0 ); + joint[ AVATAR_JOINT_RIGHT_COLLAR ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_COLLAR; + joint[ AVATAR_JOINT_RIGHT_SHOULDER ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_SHOULDER; + joint[ AVATAR_JOINT_RIGHT_ELBOW ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_ELBOW; + joint[ AVATAR_JOINT_RIGHT_WRIST ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_WRIST; + joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_FINGERTIPS; - joint[ AVATAR_JOINT_LEFT_HIP ].bindPosePosition = glm::vec3( -0.05, 0.0, 0.02 ); - joint[ AVATAR_JOINT_LEFT_KNEE ].bindPosePosition = glm::vec3( 0.00, -0.25, 0.00 ); - joint[ AVATAR_JOINT_LEFT_HEEL ].bindPosePosition = glm::vec3( 0.00, -0.23, 0.00 ); - joint[ AVATAR_JOINT_LEFT_TOES ].bindPosePosition = glm::vec3( 0.00, 0.00, -0.06 ); + joint[ AVATAR_JOINT_LEFT_HIP ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_HIP; + joint[ AVATAR_JOINT_LEFT_KNEE ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_KNEE; + joint[ AVATAR_JOINT_LEFT_HEEL ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_HEEL; + joint[ AVATAR_JOINT_LEFT_TOES ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_TOES; - joint[ AVATAR_JOINT_RIGHT_HIP ].bindPosePosition = glm::vec3( 0.05, 0.0, 0.02 ); - joint[ AVATAR_JOINT_RIGHT_KNEE ].bindPosePosition = glm::vec3( 0.00, -0.25, 0.00 ); - joint[ AVATAR_JOINT_RIGHT_HEEL ].bindPosePosition = glm::vec3( 0.00, -0.23, 0.00 ); - joint[ AVATAR_JOINT_RIGHT_TOES ].bindPosePosition = glm::vec3( 0.00, 0.00, -0.06 ); + joint[ AVATAR_JOINT_RIGHT_HIP ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_HIP; + joint[ AVATAR_JOINT_RIGHT_KNEE ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_KNEE; + joint[ AVATAR_JOINT_RIGHT_HEEL ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_HEEL; + joint[ AVATAR_JOINT_RIGHT_TOES ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_TOES; // calculate bone length, absolute bind positions/rotations for (int b = 0; b < NUM_AVATAR_JOINTS; b++) { @@ -99,10 +133,10 @@ void Skeleton::initialize() { joint[b].absoluteBindPoseRotation = glm::quat(); } else { joint[b].absoluteBindPosePosition = joint[ joint[b].parent ].absoluteBindPosePosition + - joint[b].bindPosePosition; + joint[b].bindPosePosition; glm::vec3 parentDirection = joint[ joint[b].parent ].absoluteBindPoseRotation * JOINT_DIRECTION; joint[b].absoluteBindPoseRotation = rotationBetween(parentDirection, joint[b].bindPosePosition) * - joint[ joint[b].parent ].absoluteBindPoseRotation; + joint[ joint[b].parent ].absoluteBindPoseRotation; } } } diff --git a/interface/src/Skeleton.h b/interface/src/Skeleton.h index 8d99a80fa6..29b1c45c9a 100644 --- a/interface/src/Skeleton.h +++ b/interface/src/Skeleton.h @@ -49,6 +49,7 @@ public: Skeleton(); void initialize(); + void uniformScale(float scaler); void update(float deltaTime, const glm::quat&, glm::vec3 position); void render(); From d84963040e118ad685fd0c7bc0e2cbac10c0047e Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 11 Jul 2013 18:16:55 -0700 Subject: [PATCH 03/13] avatar resized + physic adapted --- interface/src/Avatar.cpp | 94 +++++++++++++++++++++----------------- interface/src/Avatar.h | 3 +- interface/src/Head.cpp | 17 +++++-- interface/src/Head.h | 2 +- interface/src/Skeleton.cpp | 7 +-- interface/src/Skeleton.h | 5 +- 6 files changed, 75 insertions(+), 53 deletions(-) diff --git a/interface/src/Avatar.cpp b/interface/src/Avatar.cpp index d042ef5e7b..4d614cb964 100755 --- a/interface/src/Avatar.cpp +++ b/interface/src/Avatar.cpp @@ -84,7 +84,7 @@ Avatar::Avatar(Node* owningNode) : _leanScale(0.5f), _pelvisStandingHeight(0.0f), _pelvisFloatingHeight(0.0f), - _uniformScaler(1.0f), + _scale(1.0f), _distanceToNearestAvatar(std::numeric_limits::max()), _gravity(0.0f, -1.0f, 0.0f), _worldUpDirection(DEFAULT_UP_DIRECTION), @@ -109,8 +109,10 @@ Avatar::Avatar(Node* owningNode) : initializeBodyBalls(); - //uniformScale(0.5f); - + if (isMyAvatar()) { + uniformScale(0.25f); + } + _height = _skeleton.getHeight() + _bodyBall[ BODY_BALL_LEFT_HEEL ].radius + _bodyBall[ BODY_BALL_HEAD_BASE ].radius; _maxArmLength = _skeleton.getArmLength(); @@ -325,7 +327,7 @@ void Avatar::updateFromGyrosAndOrWebcam(bool gyroLook, const glm::vec3& amplifyA _head.setCameraFollowsHead(gyroLook); // Update torso lean distance based on accelerometer data - const float TORSO_LENGTH = 0.5f; + const float TORSO_LENGTH = _scale * 0.5f; const float MAX_LEAN = 45.0f; _head.setLeanSideways(glm::clamp(glm::degrees(atanf(estimatedPosition.x * _leanScale / TORSO_LENGTH)), -MAX_LEAN, MAX_LEAN)); @@ -372,18 +374,18 @@ void Avatar::updateThrust(float deltaTime, Transmitter * transmitter) { const float THRUST_JUMP = 120.f; // Add Thrusts from keyboard - if (_driveKeys[FWD ]) {_thrust += THRUST_MAG_FWD * deltaTime * front;} - if (_driveKeys[BACK ]) {_thrust -= THRUST_MAG_BACK * deltaTime * front;} - if (_driveKeys[RIGHT ]) {_thrust += THRUST_MAG_LATERAL * deltaTime * right;} - if (_driveKeys[LEFT ]) {_thrust -= THRUST_MAG_LATERAL * deltaTime * right;} - if (_driveKeys[UP ]) {_thrust += THRUST_MAG_UP * deltaTime * up;} - if (_driveKeys[DOWN ]) {_thrust -= THRUST_MAG_DOWN * deltaTime * up;} + if (_driveKeys[FWD ]) {_thrust += _scale * THRUST_MAG_FWD * deltaTime * front;} + if (_driveKeys[BACK ]) {_thrust -= _scale * THRUST_MAG_BACK * deltaTime * front;} + if (_driveKeys[RIGHT ]) {_thrust += _scale * THRUST_MAG_LATERAL * deltaTime * right;} + if (_driveKeys[LEFT ]) {_thrust -= _scale * THRUST_MAG_LATERAL * deltaTime * right;} + if (_driveKeys[UP ]) {_thrust += _scale * THRUST_MAG_UP * deltaTime * up;} + if (_driveKeys[DOWN ]) {_thrust -= _scale * THRUST_MAG_DOWN * deltaTime * up;} if (_driveKeys[ROT_RIGHT]) {_bodyYawDelta -= YAW_MAG * deltaTime;} if (_driveKeys[ROT_LEFT ]) {_bodyYawDelta += YAW_MAG * deltaTime;} // Add one time jumping force if requested if (_shouldJump) { - _thrust += THRUST_JUMP * up; + _thrust += _scale * THRUST_JUMP * up; _shouldJump = false; } @@ -521,7 +523,7 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { // apply gravity and collision with the ground/floor if (isMyAvatar() && USING_AVATAR_GRAVITY) { - _velocity += _gravity * (GRAVITY_EARTH * deltaTime); + _velocity += _scale * _gravity * (GRAVITY_EARTH * deltaTime); } if (isMyAvatar()) { updateCollisionWithEnvironment(); @@ -651,7 +653,7 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { _head.setBodyRotation (glm::vec3(_bodyPitch, _bodyYaw, _bodyRoll)); _head.setPosition(_bodyBall[ BODY_BALL_HEAD_BASE ].position); - _head.setScale (_uniformScaler); + _head.setScale(_scale); _head.setSkinColor(glm::vec3(SKIN_COLOR[0], SKIN_COLOR[1], SKIN_COLOR[2])); _head.simulate(deltaTime, isMyAvatar()); @@ -734,7 +736,7 @@ void Avatar::updateHandMovementAndTouching(float deltaTime, bool enableHandMovem if (distance < closestDistance) { closestDistance = distance; - if (distance < PERIPERSONAL_RADIUS) { + if (distance < _scale * PERIPERSONAL_RADIUS) { _interactingOther = otherAvatar; } } @@ -1003,7 +1005,7 @@ void Avatar::render(bool lookingInMirror, bool renderAvatarBalls) { } // render a simple round on the ground projected down from the avatar's position - renderDiskShadow(_position, glm::vec3(0.0f, 1.0f, 0.0f), 0.1f, 0.2f); + renderDiskShadow(_position, glm::vec3(0.0f, 1.0f, 0.0f), _scale * 0.1f, 0.2f); // render body renderBody(lookingInMirror, renderAvatarBalls); @@ -1077,7 +1079,7 @@ void Avatar::resetBodyBalls() { void Avatar::updateBodyBalls(float deltaTime) { // Check for a large repositioning, and re-initialize balls if this has happened - const float BEYOND_BODY_SPRING_RANGE = 2.f; + const float BEYOND_BODY_SPRING_RANGE = _scale * 2.f; if (glm::length(_position - _bodyBall[BODY_BALL_PELVIS].position) > BEYOND_BODY_SPRING_RANGE) { resetBodyBalls(); } @@ -1378,35 +1380,43 @@ void Avatar::renderJointConnectingCone(glm::vec3 position1, glm::vec3 position2, } void Avatar::uniformScale(float uniformScaler) { - _uniformScaler *= uniformScaler; + _scale *= uniformScaler; - _skeleton.uniformScale(_uniformScaler); + _skeleton.uniformScale(_scale); // specify the new radius of each ball - _bodyBall[ BODY_BALL_PELVIS ].radius = _uniformScaler * BODY_BALL_RADIUS_PELVIS; - _bodyBall[ BODY_BALL_TORSO ].radius = _uniformScaler * BODY_BALL_RADIUS_TORSO; - _bodyBall[ BODY_BALL_CHEST ].radius = _uniformScaler * BODY_BALL_RADIUS_CHEST; - _bodyBall[ BODY_BALL_NECK_BASE ].radius = _uniformScaler * BODY_BALL_RADIUS_NECK_BASE; - _bodyBall[ BODY_BALL_HEAD_BASE ].radius = _uniformScaler * BODY_BALL_RADIUS_HEAD_BASE; - _bodyBall[ BODY_BALL_LEFT_COLLAR ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_COLLAR; - _bodyBall[ BODY_BALL_LEFT_SHOULDER ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_SHOULDER; - _bodyBall[ BODY_BALL_LEFT_ELBOW ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_ELBOW; - _bodyBall[ BODY_BALL_LEFT_WRIST ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_WRIST; - _bodyBall[ BODY_BALL_LEFT_FINGERTIPS ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_FINGERTIPS; - _bodyBall[ BODY_BALL_RIGHT_COLLAR ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_COLLAR; - _bodyBall[ BODY_BALL_RIGHT_SHOULDER ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_SHOULDER; - _bodyBall[ BODY_BALL_RIGHT_ELBOW ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_ELBOW; - _bodyBall[ BODY_BALL_RIGHT_WRIST ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_WRIST; - _bodyBall[ BODY_BALL_RIGHT_FINGERTIPS ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_FINGERTIPS; - _bodyBall[ BODY_BALL_LEFT_HIP ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_HIP; + _bodyBall[ BODY_BALL_PELVIS ].radius = _scale * BODY_BALL_RADIUS_PELVIS; + _bodyBall[ BODY_BALL_TORSO ].radius = _scale * BODY_BALL_RADIUS_TORSO; + _bodyBall[ BODY_BALL_CHEST ].radius = _scale * BODY_BALL_RADIUS_CHEST; + _bodyBall[ BODY_BALL_NECK_BASE ].radius = _scale * BODY_BALL_RADIUS_NECK_BASE; + _bodyBall[ BODY_BALL_HEAD_BASE ].radius = _scale * BODY_BALL_RADIUS_HEAD_BASE; + _bodyBall[ BODY_BALL_LEFT_COLLAR ].radius = _scale * BODY_BALL_RADIUS_LEFT_COLLAR; + _bodyBall[ BODY_BALL_LEFT_SHOULDER ].radius = _scale * BODY_BALL_RADIUS_LEFT_SHOULDER; + _bodyBall[ BODY_BALL_LEFT_ELBOW ].radius = _scale * BODY_BALL_RADIUS_LEFT_ELBOW; + _bodyBall[ BODY_BALL_LEFT_WRIST ].radius = _scale * BODY_BALL_RADIUS_LEFT_WRIST; + _bodyBall[ BODY_BALL_LEFT_FINGERTIPS ].radius = _scale * BODY_BALL_RADIUS_LEFT_FINGERTIPS; + _bodyBall[ BODY_BALL_RIGHT_COLLAR ].radius = _scale * BODY_BALL_RADIUS_RIGHT_COLLAR; + _bodyBall[ BODY_BALL_RIGHT_SHOULDER ].radius = _scale * BODY_BALL_RADIUS_RIGHT_SHOULDER; + _bodyBall[ BODY_BALL_RIGHT_ELBOW ].radius = _scale * BODY_BALL_RADIUS_RIGHT_ELBOW; + _bodyBall[ BODY_BALL_RIGHT_WRIST ].radius = _scale * BODY_BALL_RADIUS_RIGHT_WRIST; + _bodyBall[ BODY_BALL_RIGHT_FINGERTIPS ].radius = _scale * BODY_BALL_RADIUS_RIGHT_FINGERTIPS; + _bodyBall[ BODY_BALL_LEFT_HIP ].radius = _scale * BODY_BALL_RADIUS_LEFT_HIP; - //_bodyBall[ BODY_BALL_LEFT_MID_THIGH ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_MID_THIGH; + //_bodyBall[ BODY_BALL_LEFT_MID_THIGH ].radius = _scale * BODY_BALL_RADIUS_LEFT_MID_THIGH; - _bodyBall[ BODY_BALL_LEFT_KNEE ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_KNEE; - _bodyBall[ BODY_BALL_LEFT_HEEL ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_HEEL; - _bodyBall[ BODY_BALL_LEFT_TOES ].radius = _uniformScaler * BODY_BALL_RADIUS_LEFT_TOES; - _bodyBall[ BODY_BALL_RIGHT_HIP ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_HIP; - _bodyBall[ BODY_BALL_RIGHT_KNEE ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_KNEE; - _bodyBall[ BODY_BALL_RIGHT_HEEL ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_HEEL; - _bodyBall[ BODY_BALL_RIGHT_TOES ].radius = _uniformScaler * BODY_BALL_RADIUS_RIGHT_TOES; + _bodyBall[ BODY_BALL_LEFT_KNEE ].radius = _scale * BODY_BALL_RADIUS_LEFT_KNEE; + _bodyBall[ BODY_BALL_LEFT_HEEL ].radius = _scale * BODY_BALL_RADIUS_LEFT_HEEL; + _bodyBall[ BODY_BALL_LEFT_TOES ].radius = _scale * BODY_BALL_RADIUS_LEFT_TOES; + _bodyBall[ BODY_BALL_RIGHT_HIP ].radius = _scale * BODY_BALL_RADIUS_RIGHT_HIP; + _bodyBall[ BODY_BALL_RIGHT_KNEE ].radius = _scale * BODY_BALL_RADIUS_RIGHT_KNEE; + _bodyBall[ BODY_BALL_RIGHT_HEEL ].radius = _scale * BODY_BALL_RADIUS_RIGHT_HEEL; + _bodyBall[ BODY_BALL_RIGHT_TOES ].radius = _scale * BODY_BALL_RADIUS_RIGHT_TOES; + + _height = _skeleton.getHeight() + _bodyBall[ BODY_BALL_LEFT_HEEL ].radius + _bodyBall[ BODY_BALL_HEAD_BASE ].radius; + + _maxArmLength = _skeleton.getArmLength(); + _pelvisStandingHeight = _skeleton.getPelvisStandingHeight() + _bodyBall[ BODY_BALL_LEFT_HEEL ].radius; + _pelvisFloatingHeight = _skeleton.getPelvisFloatingHeight() + _bodyBall[ BODY_BALL_LEFT_HEEL ].radius; + _pelvisToHeadLength = _skeleton.getPelvisToHeadLength(); + _avatarTouch.setReachableRadius(_scale * PERIPERSONAL_RADIUS); } diff --git a/interface/src/Avatar.h b/interface/src/Avatar.h index ee3abef947..f07db1afdb 100755 --- a/interface/src/Avatar.h +++ b/interface/src/Avatar.h @@ -139,6 +139,7 @@ public: glm::vec3 getBodyRightDirection () const { return getOrientation() * IDENTITY_RIGHT; } glm::vec3 getBodyUpDirection () const { return getOrientation() * IDENTITY_UP; } glm::vec3 getBodyFrontDirection () const { return getOrientation() * IDENTITY_FRONT; } + float getScale () const { return _scale;} const glm::vec3& getVelocity () const { return _velocity;} float getSpeed () const { return _speed;} float getHeight () const { return _height;} @@ -221,7 +222,7 @@ private: float _pelvisStandingHeight; float _pelvisFloatingHeight; float _pelvisToHeadLength; - float _uniformScaler; + float _scale; float _height; Balls* _balls; AvatarTouch _avatarTouch; diff --git a/interface/src/Head.cpp b/interface/src/Head.cpp index ae46378fd3..cc6f729325 100644 --- a/interface/src/Head.cpp +++ b/interface/src/Head.cpp @@ -128,8 +128,8 @@ void Head::resetHairPhysics() { glm::vec3 up = getUpDirection(); for (int t = 0; t < NUM_HAIR_TUFTS; t ++) { - _hairTuft[t].length = HAIR_LENGTH; - _hairTuft[t].thickness = HAIR_THICKNESS; + _hairTuft[t].length = _scale * HAIR_LENGTH; + _hairTuft[t].thickness = _scale * HAIR_THICKNESS; _hairTuft[t].basePosition = _position + up * _scale * BODY_BALL_RADIUS_HEAD_BASE * 0.9f; _hairTuft[t].midPosition = _hairTuft[t].basePosition + up * _hairTuft[t].length * ONE_HALF; _hairTuft[t].endPosition = _hairTuft[t].midPosition + up * _hairTuft[t].length * ONE_HALF; @@ -306,6 +306,15 @@ void Head::render(float alpha) { } } +void Head::setScale (float scale) { + _scale = scale; + + delete[] _mohawkTriangleFan; + delete[] _mohawkColors; + createMohawk(); + + resetHairPhysics(); +} void Head::createMohawk() { uint16_t nodeId = 0; @@ -318,7 +327,7 @@ void Head::createMohawk() { } } srand(nodeId); - float height = 0.08f + randFloat() * 0.05f; + float height = _scale * (0.08f + randFloat() * 0.05f); float variance = 0.03 + randFloat() * 0.03f; const float RAD_PER_TRIANGLE = (2.3f + randFloat() * 0.2f) / (float)MOHAWK_TRIANGLES; _mohawkTriangleFan = new glm::vec3[MOHAWK_TRIANGLES]; @@ -326,7 +335,7 @@ void Head::createMohawk() { _mohawkTriangleFan[0] = glm::vec3(0, 0, 0); glm::vec3 basicColor(randFloat(), randFloat(), randFloat()); _mohawkColors[0] = basicColor; - + for (int i = 1; i < MOHAWK_TRIANGLES; i++) { _mohawkTriangleFan[i] = glm::vec3((randFloat() - 0.5f) * variance, height * cosf(i * RAD_PER_TRIANGLE - PIf / 2.f) diff --git a/interface/src/Head.h b/interface/src/Head.h index 3cb41f81ec..1f49854cd7 100644 --- a/interface/src/Head.h +++ b/interface/src/Head.h @@ -38,7 +38,7 @@ public: void render(float alpha); void renderMohawk(); - void setScale (float scale ) { _scale = scale; } + void setScale (float scale ); void setPosition (glm::vec3 position ) { _position = position; } void setBodyRotation (glm::vec3 bodyRotation ) { _bodyRotation = bodyRotation; } void setGravity (glm::vec3 gravity ) { _gravity = gravity; } diff --git a/interface/src/Skeleton.cpp b/interface/src/Skeleton.cpp index 0743e185eb..fcd83bc1ff 100644 --- a/interface/src/Skeleton.cpp +++ b/interface/src/Skeleton.cpp @@ -40,7 +40,7 @@ const glm::vec3 AVATAR_JOINT_POSITION_RIGHT_KNEE = glm::vec3( 0.00, -0.25, const glm::vec3 AVATAR_JOINT_POSITION_RIGHT_HEEL = glm::vec3( 0.00, -0.23, 0.00); const glm::vec3 AVATAR_JOINT_POSITION_RIGHT_TOES = glm::vec3( 0.00, 0.00, -0.06); -Skeleton::Skeleton() { +Skeleton::Skeleton() : _floatingHeight(FLOATING_HEIGHT){ } void Skeleton::initialize() { @@ -89,7 +89,6 @@ void Skeleton::initialize() { joint[ AVATAR_JOINT_RIGHT_HEEL ].parent = AVATAR_JOINT_RIGHT_KNEE; joint[ AVATAR_JOINT_RIGHT_TOES ].parent = AVATAR_JOINT_RIGHT_HEEL; - // specify the bind pose position uniformScale(1.0f); } @@ -139,6 +138,8 @@ void Skeleton::uniformScale(float uniformScaler) { joint[ joint[b].parent ].absoluteBindPoseRotation; } } + + _floatingHeight = uniformScaler * FLOATING_HEIGHT; } // calculate positions and rotations of all bones by traversing the skeleton tree: @@ -185,7 +186,7 @@ float Skeleton::getPelvisStandingHeight() { float Skeleton::getPelvisFloatingHeight() { return joint[ AVATAR_JOINT_LEFT_HEEL ].length + joint[ AVATAR_JOINT_LEFT_KNEE ].length + - FLOATING_HEIGHT; + _floatingHeight; } float Skeleton::getPelvisToHeadLength() { diff --git a/interface/src/Skeleton.h b/interface/src/Skeleton.h index 29b1c45c9a..551b9b23f9 100644 --- a/interface/src/Skeleton.h +++ b/interface/src/Skeleton.h @@ -73,7 +73,8 @@ public: float length; // the length of vector connecting the joint and its parent }; - AvatarJoint joint[ NUM_AVATAR_JOINTS ]; - }; + AvatarJoint joint[ NUM_AVATAR_JOINTS ]; + float _floatingHeight; +}; #endif From 16647e3591c67a32278074eac7b017eb39fc0932 Mon Sep 17 00:00:00 2001 From: atlante45 Date: Fri, 12 Jul 2013 13:27:46 -0700 Subject: [PATCH 04/13] Resolve conflict --- interface/src/Application.cpp | 42 +++++-- interface/src/Application.h | 5 + interface/src/Avatar.cpp | 113 +++++++++++-------- interface/src/Avatar.h | 7 +- interface/src/ParticleSystem.cpp | 183 +++++++++++++++++++++++++++++++ interface/src/ParticleSystem.h | 59 ++++++++++ interface/src/Physics.cpp | 40 +++++++ interface/src/Physics.h | 15 +++ 8 files changed, 403 insertions(+), 61 deletions(-) mode change 100755 => 100644 interface/src/Application.cpp create mode 100644 interface/src/ParticleSystem.cpp create mode 100644 interface/src/ParticleSystem.h create mode 100644 interface/src/Physics.cpp create mode 100644 interface/src/Physics.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp old mode 100755 new mode 100644 index 044f58f3fd..6f70c89f3b --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -73,6 +73,8 @@ using namespace std; static char STAR_FILE[] = "https://s3-us-west-1.amazonaws.com/highfidelity/stars.txt"; static char STAR_CACHE_FILE[] = "cachedStars.txt"; +static const bool TESTING_PARTICLE_SYSTEM = false; + static const int BANDWIDTH_METER_CLICK_MAX_DRAG_LENGTH = 6; // farther dragged clicks are ignored const glm::vec3 START_LOCATION(4.f, 0.f, 5.f); // Where one's own node begins in the world @@ -183,6 +185,8 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : _touchAvgX(0.0f), _touchAvgY(0.0f), _isTouchPressed(false), + _yawFromTouch(0.0f), + _pitchFromTouch(0.0f), _mousePressed(false), _mouseVoxelScale(1.0f / 1024.0f), _justEditedVoxel(false), @@ -969,6 +973,9 @@ void Application::idle() { gettimeofday(&check, NULL); // Only run simulation code if more than IDLE_SIMULATE_MSECS have passed since last time we ran + sendPostedEvents(NULL, QEvent::TouchBegin); + sendPostedEvents(NULL, QEvent::TouchUpdate); + sendPostedEvents(NULL, QEvent::TouchEnd); double timeSinceLastUpdate = diffclock(&_lastTimeUpdated, &check); if (timeSinceLastUpdate > IDLE_SIMULATE_MSECS) { @@ -978,9 +985,6 @@ void Application::idle() { // This is necessary because id the idle() call takes longer than the // interval between idle() calls, the event loop never gets to run, // and touch events get delayed. - sendPostedEvents(NULL, QEvent::TouchBegin); - sendPostedEvents(NULL, QEvent::TouchUpdate); - sendPostedEvents(NULL, QEvent::TouchEnd); const float BIGGEST_DELTA_TIME_SECS = 0.25f; update(glm::clamp((float)timeSinceLastUpdate / 1000.f, 0.f, BIGGEST_DELTA_TIME_SECS)); @@ -1902,12 +1906,9 @@ void Application::update(float deltaTime) { if (_isTouchPressed) { float TOUCH_YAW_SCALE = -50.0f; float TOUCH_PITCH_SCALE = -50.0f; - _myAvatar.getHead().addYaw((_touchAvgX - _lastTouchAvgX) - * TOUCH_YAW_SCALE - * deltaTime); - _myAvatar.getHead().addPitch((_touchAvgY - _lastTouchAvgY) - * TOUCH_PITCH_SCALE - * deltaTime); + _yawFromTouch += ((_touchAvgX - _lastTouchAvgX) * TOUCH_YAW_SCALE * deltaTime); + _pitchFromTouch += ((_touchAvgY - _lastTouchAvgY) * TOUCH_PITCH_SCALE * deltaTime); + _lastTouchAvgX = _touchAvgX; _lastTouchAvgY = _touchAvgY; } @@ -2007,15 +2008,28 @@ void Application::update(float deltaTime) { _audio.setLastVelocity(_myAvatar.getVelocity()); _audio.eventuallyAnalyzePing(); #endif + + if (TESTING_PARTICLE_SYSTEM) { + _particleSystem.simulate(deltaTime); + } } void Application::updateAvatar(float deltaTime) { + // When head is rotated via touch/mouse look, slowly turn body to follow + const float BODY_FOLLOW_HEAD_RATE = 0.5f; + // update body yaw by body yaw delta + _myAvatar.setOrientation(_myAvatar.getOrientation() + * glm::quat(glm::vec3(0, _yawFromTouch * deltaTime * BODY_FOLLOW_HEAD_RATE, 0) * deltaTime)); + _yawFromTouch -= _yawFromTouch * deltaTime * BODY_FOLLOW_HEAD_RATE; + // Update my avatar's state from gyros and/or webcam _myAvatar.updateFromGyrosAndOrWebcam(_gyroLook->isChecked(), glm::vec3(_headCameraPitchYawScale, _headCameraPitchYawScale, - _headCameraPitchYawScale)); + _headCameraPitchYawScale), + _yawFromTouch, + _pitchFromTouch); if (_serialHeadSensor.isActive()) { @@ -2055,8 +2069,8 @@ void Application::updateAvatar(float deltaTime) { float yaw, pitch, roll; OculusManager::getEulerAngles(yaw, pitch, roll); - _myAvatar.getHead().setYaw(yaw); - _myAvatar.getHead().setPitch(pitch); + _myAvatar.getHead().setYaw(yaw + _yawFromTouch); + _myAvatar.getHead().setPitch(pitch + _pitchFromTouch); _myAvatar.getHead().setRoll(roll); } @@ -2441,6 +2455,10 @@ void Application::displaySide(Camera& whichCamera) { _myAvatar.render(_lookingInMirror->isChecked(), _renderAvatarBalls->isChecked()); _myAvatar.setDisplayingLookatVectors(_renderLookatOn->isChecked()); } + + if (TESTING_PARTICLE_SYSTEM) { + _particleSystem.render(); + } // Render the world box if (!_lookingInMirror->isChecked() && _renderStatsOn->isChecked()) { render_world_box(); } diff --git a/interface/src/Application.h b/interface/src/Application.h index cea4d7d46d..c6bbd4eec2 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -41,6 +41,7 @@ #include "ui/ChatEntry.h" #include "ToolsPalette.h" #include "Swatch.h" +#include "ParticleSystem.h" class QAction; class QActionGroup; @@ -332,6 +333,8 @@ private: float _touchDragStartedAvgX; float _touchDragStartedAvgY; bool _isTouchPressed; // true if multitouch has been pressed (clear when finished) + float _yawFromTouch; + float _pitchFromTouch; VoxelDetail _mouseVoxelDragging; glm::vec3 _voxelThrust; @@ -364,6 +367,8 @@ private: GeometryCache _geometryCache; + ParticleSystem _particleSystem; + #ifndef _WIN32 Audio _audio; #endif diff --git a/interface/src/Avatar.cpp b/interface/src/Avatar.cpp index 4d614cb964..82aacdd2c4 100755 --- a/interface/src/Avatar.cpp +++ b/interface/src/Avatar.cpp @@ -16,12 +16,14 @@ #include "Hand.h" #include "Head.h" #include "Log.h" +#include "Physics.h" #include "ui/TextRenderer.h" #include #include #include #include + using namespace std; const bool BALLS_ON = false; @@ -95,6 +97,8 @@ Avatar::Avatar(Node* owningNode) : _elapsedTimeMoving(0.0f), _elapsedTimeStopped(0.0f), _elapsedTimeSinceCollision(0.0f), + _speedBrakes(false), + _isThrustOn(false), _voxels(this) { // give the pointer to our head to inherited _headData variable from AvatarData @@ -290,7 +294,10 @@ void Avatar::reset() { } // Update avatar head rotation with sensor data -void Avatar::updateFromGyrosAndOrWebcam(bool gyroLook, const glm::vec3& amplifyAngle) { +void Avatar::updateFromGyrosAndOrWebcam(bool gyroLook, + const glm::vec3& amplifyAngle, + float yawFromTouch, + float pitchFromTouch) { SerialInterface* gyros = Application::getInstance()->getSerialHeadSensor(); Webcam* webcam = Application::getInstance()->getWebcam(); glm::vec3 estimatedPosition, estimatedRotation; @@ -301,6 +308,8 @@ void Avatar::updateFromGyrosAndOrWebcam(bool gyroLook, const glm::vec3& amplifyA estimatedRotation = webcam->getEstimatedRotation(); } else { + _head.setPitch(pitchFromTouch); + _head.setYaw(yawFromTouch); return; } if (webcam->isActive()) { @@ -321,8 +330,8 @@ void Avatar::updateFromGyrosAndOrWebcam(bool gyroLook, const glm::vec3& amplifyA } } } - _head.setPitch(estimatedRotation.x * amplifyAngle.x); - _head.setYaw(estimatedRotation.y * amplifyAngle.y); + _head.setPitch(estimatedRotation.x * amplifyAngle.x + pitchFromTouch); + _head.setYaw(estimatedRotation.y * amplifyAngle.y + yawFromTouch); _head.setRoll(estimatedRotation.z * amplifyAngle.z); _head.setCameraFollowsHead(gyroLook); @@ -361,16 +370,16 @@ void Avatar::updateThrust(float deltaTime, Transmitter * transmitter) { // // Gather thrust information from keyboard and sensors to apply to avatar motion // - glm::quat orientation = getOrientation(); + glm::quat orientation = getHead().getOrientation(); glm::vec3 front = orientation * IDENTITY_FRONT; glm::vec3 right = orientation * IDENTITY_RIGHT; glm::vec3 up = orientation * IDENTITY_UP; const float THRUST_MAG_UP = 800.0f; - const float THRUST_MAG_DOWN = 200.f; - const float THRUST_MAG_FWD = 300.f; - const float THRUST_MAG_BACK = 150.f; - const float THRUST_MAG_LATERAL = 200.f; + const float THRUST_MAG_DOWN = 300.f; + const float THRUST_MAG_FWD = 500.f; + const float THRUST_MAG_BACK = 300.f; + const float THRUST_MAG_LATERAL = 250.f; const float THRUST_JUMP = 120.f; // Add Thrusts from keyboard @@ -418,14 +427,27 @@ void Avatar::updateThrust(float deltaTime, Transmitter * transmitter) { up; } } -} + // Update speed brake status + + const float MIN_SPEED_BRAKE_VELOCITY = 0.4f; + if ((glm::length(_thrust) == 0.0f) && _isThrustOn && (glm::length(_velocity) > MIN_SPEED_BRAKE_VELOCITY)) { + _speedBrakes = true; + } + + if (_speedBrakes && (glm::length(_velocity) < MIN_SPEED_BRAKE_VELOCITY)) { + _speedBrakes = false; + } + _isThrustOn = (glm::length(_thrust) > EPSILON); + +} + void Avatar::simulate(float deltaTime, Transmitter* transmitter) { glm::quat orientation = getOrientation(); glm::vec3 front = orientation * IDENTITY_FRONT; glm::vec3 right = orientation * IDENTITY_RIGHT; - + // Update movement timers if (isMyAvatar()) { _elapsedTimeSinceCollision += deltaTime; @@ -448,9 +470,6 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { glm::vec3 oldVelocity = getVelocity(); if (isMyAvatar()) { - // update position by velocity - _position += _velocity * deltaTime; - // calculate speed _speed = glm::length(_velocity); } @@ -485,7 +504,7 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { enableHandMovement &= (it->jointID != AVATAR_JOINT_RIGHT_WRIST); } - // update avatar skeleton + // update avatar skeleton _skeleton.update(deltaTime, getOrientation(), _position); //determine the lengths of the body springs now that we have updated the skeleton at least once @@ -506,51 +525,49 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { _ballSpringsInitialized = true; } - // if this is not my avatar, then hand position comes from transmitted data if (!isMyAvatar()) { _skeleton.joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].position = _handPosition; } - - //detect and respond to collisions with other avatars... - if (isMyAvatar()) { - updateAvatarCollisions(deltaTime); - } - + //update the movement of the hand and process handshaking with other avatars... updateHandMovementAndTouching(deltaTime, enableHandMovement); _avatarTouch.simulate(deltaTime); - - // apply gravity and collision with the ground/floor - if (isMyAvatar() && USING_AVATAR_GRAVITY) { - _velocity += _scale * _gravity * (GRAVITY_EARTH * deltaTime); - } + if (isMyAvatar()) { + + // apply gravity + if (USING_AVATAR_GRAVITY) { + // For gravity, always move the avatar by the amount driven by gravity, so that the collision + // routines will detect it and collide every frame when pulled by gravity to a surface + // + _velocity += _scale * _gravity * (GRAVITY_EARTH * deltaTime); + _position += _scale * _gravity * (GRAVITY_EARTH * deltaTime) * deltaTime; + } + updateCollisionWithEnvironment(); + updateCollisionWithVoxels(); + updateAvatarCollisions(deltaTime); } // update body balls updateBodyBalls(deltaTime); + // test for avatar collision response with the big sphere if (usingBigSphereCollisionTest) { updateCollisionWithSphere(_TEST_bigSpherePosition, _TEST_bigSphereRadius, deltaTime); } - // collision response with voxels - if (isMyAvatar()) { - updateCollisionWithVoxels(); - } - + if (isMyAvatar()) { // add thrust to velocity _velocity += _thrust * deltaTime; - + // update body yaw by body yaw delta orientation = orientation * glm::quat(glm::radians( glm::vec3(_bodyPitchDelta, _bodyYawDelta, _bodyRollDelta) * deltaTime)); - // decay body rotation momentum float bodySpinMomentum = 1.0 - BODY_SPIN_FRICTION * deltaTime; if (bodySpinMomentum < 0.0f) { bodySpinMomentum = 0.0f; } @@ -558,22 +575,19 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { _bodyYawDelta *= bodySpinMomentum; _bodyRollDelta *= bodySpinMomentum; - // Decay velocity. If velocity is really low, increase decay to simulate static friction - const float VELOCITY_DECAY_UNDER_THRUST = 0.2; - const float VELOCITY_FAST_DECAY = 0.6; - const float VELOCITY_SLOW_DECAY = 3.0; - const float VELOCITY_FAST_THRESHOLD = 2.0f; - float decayConstant, decay; - if (glm::length(_thrust) > 0.f) { - decayConstant = VELOCITY_DECAY_UNDER_THRUST; - } else if (glm::length(_velocity) > VELOCITY_FAST_THRESHOLD) { - decayConstant = VELOCITY_FAST_DECAY; + const float MAX_STATIC_FRICTION_VELOCITY = 0.5f; + const float STATIC_FRICTION_STRENGTH = 20.f; + applyStaticFriction(deltaTime, _velocity, MAX_STATIC_FRICTION_VELOCITY, STATIC_FRICTION_STRENGTH); + + const float LINEAR_DAMPING_STRENGTH = 1.0f; + const float SPEED_BRAKE_POWER = 10.0f; + const float SQUARED_DAMPING_STRENGTH = 0.2f; + if (_speedBrakes) { + applyDamping(deltaTime, _velocity, LINEAR_DAMPING_STRENGTH * SPEED_BRAKE_POWER, SQUARED_DAMPING_STRENGTH * SPEED_BRAKE_POWER); } else { - decayConstant = VELOCITY_SLOW_DECAY; - } - decay = glm::clamp(1.0f - decayConstant * deltaTime, 0.0f, 1.0f); - _velocity *= decay; - + applyDamping(deltaTime, _velocity, LINEAR_DAMPING_STRENGTH, SQUARED_DAMPING_STRENGTH); + } + //pitch and roll the body as a function of forward speed and turning delta const float BODY_PITCH_WHILE_WALKING = -20.0; const float BODY_ROLL_WHILE_TURNING = 0.2; @@ -664,6 +678,9 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { _mode = AVATAR_MODE_INTERACTING; } + // update position by velocity, and subtract the change added earlier for gravity + _position += _velocity * deltaTime; + // Zero thrust out now that we've added it to velocity in this frame _thrust = glm::vec3(0, 0, 0); diff --git a/interface/src/Avatar.h b/interface/src/Avatar.h index f07db1afdb..ef85d0c2a2 100755 --- a/interface/src/Avatar.h +++ b/interface/src/Avatar.h @@ -111,7 +111,10 @@ public: void reset(); void simulate(float deltaTime, Transmitter* transmitter); void updateThrust(float deltaTime, Transmitter * transmitter); - void updateFromGyrosAndOrWebcam(bool gyroLook, const glm::vec3& amplifyAngles); + void updateFromGyrosAndOrWebcam(bool gyroLook, + const glm::vec3& amplifyAngle, + float yawFromTouch, + float pitchFromTouch); void addBodyYaw(float y) {_bodyYaw += y;}; void render(bool lookingInMirror, bool renderAvatarBalls); @@ -236,6 +239,8 @@ private: float _elapsedTimeMoving; // Timers to drive camera transitions when moving float _elapsedTimeStopped; float _elapsedTimeSinceCollision; + bool _speedBrakes; + bool _isThrustOn; AvatarVoxelSystem _voxels; diff --git a/interface/src/ParticleSystem.cpp b/interface/src/ParticleSystem.cpp new file mode 100644 index 0000000000..3c27f3a8df --- /dev/null +++ b/interface/src/ParticleSystem.cpp @@ -0,0 +1,183 @@ +// +// ParticleSystem.cpp +// hifi +// +// Created by Jeffrey on July 10, 2013 +// + +#include +#include "InterfaceConfig.h" +#include + +#include "ParticleSystem.h" + +ParticleSystem::ParticleSystem() { + + _numberOfParticles = 1500; + assert(_numberOfParticles <= MAX_PARTICLES); + + _bounce = 0.9f; + _timer = 0.0f; + _airFriction = 6.0f; + _jitter = 0.1f; + _homeAttraction = 0.0f; + _tornadoForce = 0.0f; + _neighborAttraction = 0.02f; + _neighborRepulsion = 0.9f; + _tornadoAxis = glm::normalize(glm::vec3(0.1f, 1.0f, 0.1f)); + _home = glm::vec3(5.0f, 1.0f, 5.0f); + + _TEST_bigSphereRadius = 0.5f; + _TEST_bigSpherePosition = glm::vec3( 5.0f, _TEST_bigSphereRadius, 5.0f); + + for (unsigned int p = 0; p < _numberOfParticles; p++) { + _particle[p].position = _home; + _particle[p].velocity = glm::vec3(0.0f, 0.0f, 0.0f); + + float radian = ((float)p / (float)_numberOfParticles) * PI_TIMES_TWO; + float wave = sinf(radian); + + float red = 0.5f + 0.5f * wave; + float green = 0.3f + 0.3f * wave; + float blue = 0.2f - 0.2f * wave; + + _particle[p].color = glm::vec3(red, green, blue); + _particle[p].age = 0.0f; + _particle[p].radius = 0.01f; + } +} + +void ParticleSystem::simulate(float deltaTime) { + + runSpecialEffectsTest(deltaTime); + + for (unsigned int p = 0; p < _numberOfParticles; p++) { + updateParticle(p, deltaTime); + } +} + + + +void ParticleSystem::runSpecialEffectsTest(float deltaTime) { + + _timer += deltaTime; + + _gravity = 0.01f + 0.01f * sinf( _timer * 0.52f ); + _airFriction = 3.0f + 2.0f * sinf( _timer * 0.32f ); + _jitter = 0.05f + 0.05f * sinf( _timer * 0.42f ); + _homeAttraction = 0.01f + 0.01f * cosf( _timer * 0.6f ); + _tornadoForce = 0.0f + 0.03f * sinf( _timer * 0.7f ); + _neighborAttraction = 0.1f + 0.1f * cosf( _timer * 0.8f ); + _neighborRepulsion = 0.4f + 0.3f * sinf( _timer * 0.4f ); + + _tornadoAxis = glm::vec3 + ( + 0.0f + 0.5f * sinf( _timer * 0.55f ), + 1.0f, + 0.0f + 0.5f * cosf( _timer * 0.75f ) + ); +} + + + +void ParticleSystem::updateParticle(int p, float deltaTime) { + + _particle[p].age += deltaTime; + + // apply random jitter + _particle[p].velocity += + glm::vec3 + ( + -_jitter * ONE_HALF + _jitter * randFloat(), + -_jitter * ONE_HALF + _jitter * randFloat(), + -_jitter * ONE_HALF + _jitter * randFloat() + ) * deltaTime; + + + // apply attraction to home position + glm::vec3 vectorToHome = _home - _particle[p].position; + _particle[p].velocity += vectorToHome * _homeAttraction * deltaTime; + + // apply neighbor attraction + int neighbor = p + 1; + if (neighbor == _numberOfParticles ) { + neighbor = 0; + } + glm::vec3 vectorToNeighbor = _particle[p].position - _particle[neighbor].position; + + _particle[p].velocity -= vectorToNeighbor * _neighborAttraction * deltaTime; + + float distanceToNeighbor = glm::length(vectorToNeighbor); + if (distanceToNeighbor > 0.0f) { + _particle[neighbor].velocity += (vectorToNeighbor / ( 1.0f + distanceToNeighbor * distanceToNeighbor)) * _neighborRepulsion * deltaTime; + } + + // apply tornado force + glm::vec3 tornadoDirection = glm::cross(vectorToHome, _tornadoAxis); + _particle[p].velocity += tornadoDirection * _tornadoForce * deltaTime; + + // apply air friction + float drag = 1.0 - _airFriction * deltaTime; + if (drag < 0.0f) { + _particle[p].velocity = glm::vec3(0.0f, 0.0f, 0.0f); + } else { + _particle[p].velocity *= drag; + } + + // apply gravity + _particle[p].velocity.y -= _gravity * deltaTime; + + // update position by velocity + _particle[p].position += _particle[p].velocity; + + // collision with ground + if (_particle[p].position.y < _particle[p].radius) { + _particle[p].position.y = _particle[p].radius; + + if (_particle[p].velocity.y < 0.0f) { + _particle[p].velocity.y *= -_bounce; + } + } + + // collision with sphere + glm::vec3 vectorToSphereCenter = _TEST_bigSpherePosition - _particle[p].position; + float distanceToSphereCenter = glm::length(vectorToSphereCenter); + float combinedRadius = _TEST_bigSphereRadius + _particle[p].radius; + if (distanceToSphereCenter < combinedRadius) { + + if (distanceToSphereCenter > 0.0f){ + glm::vec3 directionToSphereCenter = vectorToSphereCenter / distanceToSphereCenter; + _particle[p].position = _TEST_bigSpherePosition - directionToSphereCenter * combinedRadius; + } + } +} + + +void ParticleSystem::render() { + + for (unsigned int p = 0; p < _numberOfParticles; p++) { + glColor3f(_particle[p].color.x, _particle[p].color.y, _particle[p].color.z); + glPushMatrix(); + glTranslatef(_particle[p].position.x, _particle[p].position.y, _particle[p].position.z); + glutSolidSphere(_particle[p].radius, 6, 6); + glPopMatrix(); + + // render velocity lines + glColor4f( _particle[p].color.x, _particle[p].color.y, _particle[p].color.z, 0.5f); + glm::vec3 end = _particle[p].position - _particle[p].velocity * 2.0f; + glBegin(GL_LINES); + glVertex3f(_particle[p].position.x, _particle[p].position.y, _particle[p].position.z); + glVertex3f(end.x, end.y, end.z); + + glEnd(); + + } +} + + + + + + + + diff --git a/interface/src/ParticleSystem.h b/interface/src/ParticleSystem.h new file mode 100644 index 0000000000..38eb0a1777 --- /dev/null +++ b/interface/src/ParticleSystem.h @@ -0,0 +1,59 @@ +// +// ParticleSystem.h +// hifi +// +// Created by Jeffrey on July 10, 2013 +// +// + +#ifndef hifi_ParticleSystem_h +#define hifi_ParticleSystem_h + +const int MAX_PARTICLES = 5000; +const int MAX_EMITTERS = 10; + +class ParticleSystem { +public: + ParticleSystem(); + + void simulate(float deltaTime); + void render(); + +private: + + struct Particle { + glm::vec3 position; + glm::vec3 velocity; + glm::vec3 color; + float age; + float radius; + }; + + struct Emitter { + glm::vec3 position; + glm::vec3 direction; + }; + + float _bounce; + float _gravity; + float _timer; + Emitter _emitter[MAX_EMITTERS]; + Particle _particle[MAX_PARTICLES]; + int _numberOfParticles; + glm::vec3 _home; + glm::vec3 _tornadoAxis; + float _airFriction; + float _jitter; + float _homeAttraction; + float _tornadoForce; + float _neighborAttraction; + float _neighborRepulsion; + float _TEST_bigSphereRadius; + glm::vec3 _TEST_bigSpherePosition; + + // private methods + void updateParticle(int index, float deltaTime); + void runSpecialEffectsTest(float deltaTime); +}; + +#endif diff --git a/interface/src/Physics.cpp b/interface/src/Physics.cpp new file mode 100644 index 0000000000..31e64ccad3 --- /dev/null +++ b/interface/src/Physics.cpp @@ -0,0 +1,40 @@ +// +// Physics.cpp +// hifi +// +// Created by Philip on July 11, 2013 +// +// Routines to help with doing virtual world physics +// + +#include +#include + +#include "Util.h" +#include "world.h" +#include "Physics.h" + +// +// Applies static friction: maxVelocity is the largest velocity for which there +// there is friction, and strength is the amount of friction force applied to reduce +// velocity. +// +void applyStaticFriction(float deltaTime, glm::vec3& velocity, float maxVelocity, float strength) { + float v = glm::length(velocity); + if (v < maxVelocity) { + velocity *= glm::clamp((1.0f - deltaTime * strength * (1.f - v / maxVelocity)), 0.0f, 1.0f); + } +} + +// +// Applies velocity damping, with a strength value for linear and squared velocity damping +// + +void applyDamping(float deltaTime, glm::vec3& velocity, float linearStrength, float squaredStrength) { + if (squaredStrength == 0.f) { + velocity *= glm::clamp(1.f - deltaTime * linearStrength, 0.f, 1.f); + } else { + velocity *= glm::clamp(1.f - deltaTime * (linearStrength + glm::length(velocity) * squaredStrength), 0.f, 1.f); + } +} + diff --git a/interface/src/Physics.h b/interface/src/Physics.h new file mode 100644 index 0000000000..699497c187 --- /dev/null +++ b/interface/src/Physics.h @@ -0,0 +1,15 @@ +// +// Balls.h +// hifi +// +// Created by Philip on 4/25/13. +// +// + +#ifndef hifi_Physics_h +#define hifi_Physics_h + +void applyStaticFriction(float deltaTime, glm::vec3& velocity, float maxVelocity, float strength); +void applyDamping(float deltaTime, glm::vec3& velocity, float linearStrength, float squaredStrength); + +#endif From 280afed7f09b488ec7bd8b92c19ca3ac80b203be Mon Sep 17 00:00:00 2001 From: atlante45 Date: Fri, 12 Jul 2013 17:51:47 -0700 Subject: [PATCH 05/13] added scale in avatar packets eand slots to change it from the menu --- interface/src/Application.cpp | 27 ++++++++++--- interface/src/Application.h | 2 + interface/src/Avatar.cpp | 12 ++++-- interface/src/Avatar.h | 2 +- interface/src/Camera.cpp | 19 ++++----- interface/src/Camera.h | 4 +- interface/src/Skeleton.cpp | 54 +++++++++++++------------- interface/src/Skeleton.h | 2 +- libraries/avatars/src/AvatarData.cpp | 6 ++- libraries/avatars/src/AvatarData.h | 1 + libraries/shared/src/PacketHeaders.cpp | 5 ++- 11 files changed, 82 insertions(+), 52 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 335fcbd7f4..056d9b2fa9 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1182,6 +1182,24 @@ void Application::setRenderThirdPerson(bool thirdPerson) { } } +void Application::increaseAvatarSize() { + if (3.0f < _myAvatar.getScale() + 0.25f) { + return; + } + + _myAvatar.setScale(_myAvatar.getScale() + 0.25f); + _myCamera.setScale(_myAvatar.getScale() + 0.25f); +} + +void Application::decreaseAvatarSize() { + if (_myAvatar.getScale() + 0.25f < 0.25f) { + return; + } + + _myAvatar.setScale(_myAvatar.getScale() - 0.25f); + _myCamera.setScale(_myAvatar.getScale() - 0.25f); +} + void Application::setFrustumOffset(bool frustumOffset) { // reshape so that OpenGL will get the right lens details for the camera of choice resizeGL(_glWidget->width(), _glWidget->height()); @@ -1555,7 +1573,7 @@ void Application::initMenu() { _renderAvatarsOn->setChecked(true); (_renderAvatarBalls = renderMenu->addAction("Avatar as Balls"))->setCheckable(true); _renderAvatarBalls->setChecked(false); - renderMenu->addAction("Cycle Voxeltar Mode", _myAvatar.getVoxels(), SLOT(cycleMode())); + renderMenu->addAction("Cycle Voxel Mode", _myAvatar.getVoxels(), SLOT(cycleMode())); (_renderFrameTimerOn = renderMenu->addAction("Show Timer"))->setCheckable(true); _renderFrameTimerOn->setChecked(false); (_renderLookatOn = renderMenu->addAction("Lookat Vectors"))->setCheckable(true); @@ -1564,6 +1582,9 @@ void Application::initMenu() { "First Person", this, SLOT(setRenderFirstPerson(bool)), Qt::Key_P))->setCheckable(true); (_manualThirdPerson = renderMenu->addAction( "Third Person", this, SLOT(setRenderThirdPerson(bool))))->setCheckable(true); + renderMenu->addAction("Increase Avatar Size", this, SLOT(increaseAvatarSize()), Qt::SHIFT | Qt::Key_Plus); + renderMenu->addAction("Decrease Avatar Siz+e", this, SLOT(decreaseAvatarSize()), Qt::SHIFT | Qt::Key_Minus); + QMenu* toolsMenu = menuBar->addMenu("Tools"); (_renderStatsOn = toolsMenu->addAction("Stats"))->setCheckable(true); @@ -1750,10 +1771,6 @@ void Application::init() { _palette.addAction(_colorVoxelMode, 0, 2); _palette.addAction(_eyedropperMode, 0, 3); _palette.addAction(_selectVoxelMode, 0, 4); - - float scale = 1.0f; - _myAvatar.uniformScale(scale); - _myCamera.setScale(scale); } const float MAX_AVATAR_EDIT_VELOCITY = 1.0f; diff --git a/interface/src/Application.h b/interface/src/Application.h index c6bbd4eec2..a4ea633e6d 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -118,6 +118,8 @@ private slots: void setRenderFirstPerson(bool firstPerson); void setRenderThirdPerson(bool thirdPerson); + void increaseAvatarSize(); + void decreaseAvatarSize(); void renderThrustAtVoxel(const glm::vec3& thrust); void renderLineToTouchedVoxel(); diff --git a/interface/src/Avatar.cpp b/interface/src/Avatar.cpp index 7a983f1a67..d3988c2704 100755 --- a/interface/src/Avatar.cpp +++ b/interface/src/Avatar.cpp @@ -442,6 +442,11 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { glm::vec3 front = orientation * IDENTITY_FRONT; glm::vec3 right = orientation * IDENTITY_RIGHT; + // + if (!isMyAvatar() && _scale != _newScale) { + setScale(_newScale); + } + // Update movement timers if (isMyAvatar()) { _elapsedTimeSinceCollision += deltaTime; @@ -1391,10 +1396,10 @@ void Avatar::renderJointConnectingCone(glm::vec3 position1, glm::vec3 position2, glEnd(); } -void Avatar::uniformScale(float uniformScaler) { - _scale *= uniformScaler; +void Avatar::setScale(const float scale) {_scale = scale; + _newScale = _scale; - _skeleton.uniformScale(_scale); + _skeleton.setScale(_scale); // specify the new radius of each ball _bodyBall[ BODY_BALL_PELVIS ].radius = _scale * BODY_BALL_RADIUS_PELVIS; @@ -1432,3 +1437,4 @@ void Avatar::uniformScale(float uniformScaler) { _pelvisToHeadLength = _skeleton.getPelvisToHeadLength(); _avatarTouch.setReachableRadius(_scale * PERIPERSONAL_RADIUS); } + diff --git a/interface/src/Avatar.h b/interface/src/Avatar.h index 60e38a0b62..643a187a67 100755 --- a/interface/src/Avatar.h +++ b/interface/src/Avatar.h @@ -129,7 +129,7 @@ public: void setGravity (glm::vec3 gravity); void setMouseRay (const glm::vec3 &origin, const glm::vec3 &direction); void setOrientation (const glm::quat& orientation); - void uniformScale(float uniformScaler); + void setScale (const float scale); //getters bool isInitialized () const { return _initialized;} diff --git a/interface/src/Camera.cpp b/interface/src/Camera.cpp index 47873b0ae4..a3efc650bb 100644 --- a/interface/src/Camera.cpp +++ b/interface/src/Camera.cpp @@ -124,18 +124,18 @@ void Camera::setMode(CameraMode m) { _previousTightness = _tightness; if (_mode == CAMERA_MODE_THIRD_PERSON) { - _newUpShift = _scale * CAMERA_THIRD_PERSON_MODE_UP_SHIFT; - _newDistance = _scale * CAMERA_THIRD_PERSON_MODE_DISTANCE; + _newUpShift = CAMERA_THIRD_PERSON_MODE_UP_SHIFT; + _newDistance = CAMERA_THIRD_PERSON_MODE_DISTANCE; _newTightness = CAMERA_THIRD_PERSON_MODE_TIGHTNESS; } else if (_mode == CAMERA_MODE_FIRST_PERSON) { - _newUpShift = _scale * CAMERA_FIRST_PERSON_MODE_UP_SHIFT; - _newDistance = _scale * CAMERA_FIRST_PERSON_MODE_DISTANCE; + _newUpShift = CAMERA_FIRST_PERSON_MODE_UP_SHIFT; + _newDistance = CAMERA_FIRST_PERSON_MODE_DISTANCE; _newTightness = CAMERA_FIRST_PERSON_MODE_TIGHTNESS; } else if (_mode == CAMERA_MODE_MIRROR) { - _newUpShift = _scale * CAMERA_MIRROR_MODE_UP_SHIFT; - _newDistance = _scale * CAMERA_MIRROR_MODE_DISTANCE; + _newUpShift = CAMERA_MIRROR_MODE_UP_SHIFT; + _newDistance = CAMERA_MIRROR_MODE_DISTANCE; _newTightness = CAMERA_MIRROR_MODE_TIGHTNESS; } } @@ -177,11 +177,8 @@ void Camera::setEyeOffsetOrientation (const glm::quat& o) { void Camera::setScale(float s) { _scale = s; - - _nearClip *= _scale; - _farClip *= _scale; - _newUpShift *= _scale; - _newDistance *= _scale; + _needsToInitialize = true; + _frustumNeedsReshape = true; } void Camera::initialize() { diff --git a/interface/src/Camera.h b/interface/src/Camera.h index 2a33761b13..e890bad400 100644 --- a/interface/src/Camera.h +++ b/interface/src/Camera.h @@ -55,8 +55,8 @@ public: CameraMode getMode () { return _mode; } float getFieldOfView () { return _fieldOfView; } float getAspectRatio () { return _aspectRatio; } - float getNearClip () { return _nearClip; } - float getFarClip () { return _farClip; } + float getNearClip () { return _scale * _nearClip; } + float getFarClip () { return _scale * _farClip; } const glm::vec3& getEyeOffsetPosition () { return _eyeOffsetPosition; } const glm::quat& getEyeOffsetOrientation () { return _eyeOffsetOrientation; } diff --git a/interface/src/Skeleton.cpp b/interface/src/Skeleton.cpp index fcd83bc1ff..49a4ad882d 100644 --- a/interface/src/Skeleton.cpp +++ b/interface/src/Skeleton.cpp @@ -89,39 +89,39 @@ void Skeleton::initialize() { joint[ AVATAR_JOINT_RIGHT_HEEL ].parent = AVATAR_JOINT_RIGHT_KNEE; joint[ AVATAR_JOINT_RIGHT_TOES ].parent = AVATAR_JOINT_RIGHT_HEEL; - uniformScale(1.0f); + setScale(1.0f); } -void Skeleton::uniformScale(float uniformScaler) { +void Skeleton::setScale(float scale) { // specify the bind pose position - joint[ AVATAR_JOINT_PELVIS ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_PELVIS; - joint[ AVATAR_JOINT_TORSO ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_TORSO; - joint[ AVATAR_JOINT_CHEST ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_CHEST; - joint[ AVATAR_JOINT_NECK_BASE ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_NECK_BASE; - joint[ AVATAR_JOINT_HEAD_BASE ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_HEAD_BASE; - joint[ AVATAR_JOINT_HEAD_TOP ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_HEAD_TOP; + joint[ AVATAR_JOINT_PELVIS ].bindPosePosition = scale * AVATAR_JOINT_POSITION_PELVIS; + joint[ AVATAR_JOINT_TORSO ].bindPosePosition = scale * AVATAR_JOINT_POSITION_TORSO; + joint[ AVATAR_JOINT_CHEST ].bindPosePosition = scale * AVATAR_JOINT_POSITION_CHEST; + joint[ AVATAR_JOINT_NECK_BASE ].bindPosePosition = scale * AVATAR_JOINT_POSITION_NECK_BASE; + joint[ AVATAR_JOINT_HEAD_BASE ].bindPosePosition = scale * AVATAR_JOINT_POSITION_HEAD_BASE; + joint[ AVATAR_JOINT_HEAD_TOP ].bindPosePosition = scale * AVATAR_JOINT_POSITION_HEAD_TOP; - joint[ AVATAR_JOINT_LEFT_COLLAR ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_COLLAR; - joint[ AVATAR_JOINT_LEFT_SHOULDER ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_SHOULDER; - joint[ AVATAR_JOINT_LEFT_ELBOW ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_ELBOW; - joint[ AVATAR_JOINT_LEFT_WRIST ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_WRIST; - joint[ AVATAR_JOINT_LEFT_FINGERTIPS ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_FINGERTIPS; + joint[ AVATAR_JOINT_LEFT_COLLAR ].bindPosePosition = scale * AVATAR_JOINT_POSITION_LEFT_COLLAR; + joint[ AVATAR_JOINT_LEFT_SHOULDER ].bindPosePosition = scale * AVATAR_JOINT_POSITION_LEFT_SHOULDER; + joint[ AVATAR_JOINT_LEFT_ELBOW ].bindPosePosition = scale * AVATAR_JOINT_POSITION_LEFT_ELBOW; + joint[ AVATAR_JOINT_LEFT_WRIST ].bindPosePosition = scale * AVATAR_JOINT_POSITION_LEFT_WRIST; + joint[ AVATAR_JOINT_LEFT_FINGERTIPS ].bindPosePosition = scale * AVATAR_JOINT_POSITION_LEFT_FINGERTIPS; - joint[ AVATAR_JOINT_RIGHT_COLLAR ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_COLLAR; - joint[ AVATAR_JOINT_RIGHT_SHOULDER ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_SHOULDER; - joint[ AVATAR_JOINT_RIGHT_ELBOW ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_ELBOW; - joint[ AVATAR_JOINT_RIGHT_WRIST ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_WRIST; - joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_FINGERTIPS; + joint[ AVATAR_JOINT_RIGHT_COLLAR ].bindPosePosition = scale * AVATAR_JOINT_POSITION_RIGHT_COLLAR; + joint[ AVATAR_JOINT_RIGHT_SHOULDER ].bindPosePosition = scale * AVATAR_JOINT_POSITION_RIGHT_SHOULDER; + joint[ AVATAR_JOINT_RIGHT_ELBOW ].bindPosePosition = scale * AVATAR_JOINT_POSITION_RIGHT_ELBOW; + joint[ AVATAR_JOINT_RIGHT_WRIST ].bindPosePosition = scale * AVATAR_JOINT_POSITION_RIGHT_WRIST; + joint[ AVATAR_JOINT_RIGHT_FINGERTIPS ].bindPosePosition = scale * AVATAR_JOINT_POSITION_RIGHT_FINGERTIPS; - joint[ AVATAR_JOINT_LEFT_HIP ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_HIP; - joint[ AVATAR_JOINT_LEFT_KNEE ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_KNEE; - joint[ AVATAR_JOINT_LEFT_HEEL ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_HEEL; - joint[ AVATAR_JOINT_LEFT_TOES ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_LEFT_TOES; + joint[ AVATAR_JOINT_LEFT_HIP ].bindPosePosition = scale * AVATAR_JOINT_POSITION_LEFT_HIP; + joint[ AVATAR_JOINT_LEFT_KNEE ].bindPosePosition = scale * AVATAR_JOINT_POSITION_LEFT_KNEE; + joint[ AVATAR_JOINT_LEFT_HEEL ].bindPosePosition = scale * AVATAR_JOINT_POSITION_LEFT_HEEL; + joint[ AVATAR_JOINT_LEFT_TOES ].bindPosePosition = scale * AVATAR_JOINT_POSITION_LEFT_TOES; - joint[ AVATAR_JOINT_RIGHT_HIP ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_HIP; - joint[ AVATAR_JOINT_RIGHT_KNEE ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_KNEE; - joint[ AVATAR_JOINT_RIGHT_HEEL ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_HEEL; - joint[ AVATAR_JOINT_RIGHT_TOES ].bindPosePosition = uniformScaler * AVATAR_JOINT_POSITION_RIGHT_TOES; + joint[ AVATAR_JOINT_RIGHT_HIP ].bindPosePosition = scale * AVATAR_JOINT_POSITION_RIGHT_HIP; + joint[ AVATAR_JOINT_RIGHT_KNEE ].bindPosePosition = scale * AVATAR_JOINT_POSITION_RIGHT_KNEE; + joint[ AVATAR_JOINT_RIGHT_HEEL ].bindPosePosition = scale * AVATAR_JOINT_POSITION_RIGHT_HEEL; + joint[ AVATAR_JOINT_RIGHT_TOES ].bindPosePosition = scale * AVATAR_JOINT_POSITION_RIGHT_TOES; // calculate bone length, absolute bind positions/rotations for (int b = 0; b < NUM_AVATAR_JOINTS; b++) { @@ -139,7 +139,7 @@ void Skeleton::uniformScale(float uniformScaler) { } } - _floatingHeight = uniformScaler * FLOATING_HEIGHT; + _floatingHeight = scale * FLOATING_HEIGHT; } // calculate positions and rotations of all bones by traversing the skeleton tree: diff --git a/interface/src/Skeleton.h b/interface/src/Skeleton.h index 551b9b23f9..aaaa6d83c3 100644 --- a/interface/src/Skeleton.h +++ b/interface/src/Skeleton.h @@ -49,7 +49,7 @@ public: Skeleton(); void initialize(); - void uniformScale(float scaler); + void setScale(float scale); void update(float deltaTime, const glm::quat&, glm::vec3 position); void render(); diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index 27a755e03f..99961df988 100755 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -24,6 +24,7 @@ AvatarData::AvatarData(Node* owningNode) : _bodyYaw(-90.0), _bodyPitch(0.0), _bodyRoll(0.0), + _newScale(1.0f), _handState(0), _cameraPosition(0,0,0), _cameraOrientation(), @@ -71,6 +72,7 @@ int AvatarData::getBroadcastData(unsigned char* destinationBuffer) { destinationBuffer += packFloatAngleToTwoByte(destinationBuffer, _bodyYaw); destinationBuffer += packFloatAngleToTwoByte(destinationBuffer, _bodyPitch); destinationBuffer += packFloatAngleToTwoByte(destinationBuffer, _bodyRoll); + destinationBuffer += packFloatRatioToTwoByte(destinationBuffer, _newScale); // Head rotation (NOTE: This needs to become a quaternion to save two bytes) destinationBuffer += packFloatAngleToTwoByte(destinationBuffer, _headData->_yaw); @@ -197,7 +199,9 @@ int AvatarData::parseData(unsigned char* sourceBuffer, int numBytes) { sourceBuffer += unpackFloatAngleFromTwoByte((uint16_t*) sourceBuffer, &_bodyYaw); sourceBuffer += unpackFloatAngleFromTwoByte((uint16_t*) sourceBuffer, &_bodyPitch); sourceBuffer += unpackFloatAngleFromTwoByte((uint16_t*) sourceBuffer, &_bodyRoll); - + sourceBuffer += unpackFloatRatioFromTwoByte( sourceBuffer, _newScale); + std::cout << "New Scale : " << _newScale << std::endl; + // Head rotation (NOTE: This needs to become a quaternion to save two bytes) float headYaw, headPitch, headRoll; sourceBuffer += unpackFloatAngleFromTwoByte((uint16_t*) sourceBuffer, &headYaw); diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index 439ec9b5f8..1724b8d73a 100755 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -112,6 +112,7 @@ protected: float _bodyYaw; float _bodyPitch; float _bodyRoll; + float _newScale; // Hand state (are we grabbing something or not) char _handState; diff --git a/libraries/shared/src/PacketHeaders.cpp b/libraries/shared/src/PacketHeaders.cpp index 9f8314e0b7..a025b03403 100644 --- a/libraries/shared/src/PacketHeaders.cpp +++ b/libraries/shared/src/PacketHeaders.cpp @@ -12,6 +12,9 @@ PACKET_VERSION versionForPacketType(PACKET_TYPE type) { switch (type) { + case PACKET_TYPE_HEAD_DATA: + return 1; + break; default: return 0; break; @@ -59,4 +62,4 @@ int numBytesForPacketHeader(unsigned char* packetHeader) { // currently this need not be dynamic - there are 2 bytes for each packet header return 2; -} \ No newline at end of file +} From f415f4081a6ab595f58b2670daa2726b9015a4a4 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 15 Jul 2013 01:32:14 -0700 Subject: [PATCH 06/13] Fixed a bug in LOD boundaries - changed the way we calculate whether or not to render a parent "average" voxel so that if any of it's most distant child would not be visible, then it's used instead of it's children - added precalculated value for topFarLeft corner of AABox (optimization) - changed VoxelSystem::newTreeToArrays() and VoxelTree::encodeTreeBitstreamRecursion() to use the same help function for determining this LOD boundary behavior - deleted old dead code in voxel-server and VoxelTree for picking which node to start sending with, since it wasn't being used - added VoxelNode::furthestDistanceToCamera() which tells you not the distance to the center of the voxel, but the distance from the camera to the furthest corner relative to the camera. - added ViewFrustum::getFurthestPointFromCamera() which given an axis-aligned box will tell you which vertex of the box is furthest from the camera --- interface/src/Audio.cpp | 2 +- interface/src/VoxelSystem.cpp | 9 +-- libraries/voxels/src/AABox.cpp | 2 + libraries/voxels/src/AABox.h | 19 +++-- libraries/voxels/src/ViewFrustum.cpp | 43 ++++++++++- libraries/voxels/src/ViewFrustum.h | 1 + libraries/voxels/src/VoxelNode.cpp | 33 ++++++++ libraries/voxels/src/VoxelNode.h | 3 + libraries/voxels/src/VoxelTree.cpp | 108 +-------------------------- libraries/voxels/src/VoxelTree.h | 7 -- voxel-server/src/main.cpp | 40 +--------- 11 files changed, 98 insertions(+), 169 deletions(-) diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index 75c1198d58..73f971b7da 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -318,8 +318,8 @@ Audio::Audio(Oscilloscope* scope, int16_t initialJitterBufferSamples) : _lastAcceleration(0), _totalPacketsReceived(0), _firstPacketReceivedTime(), - _echoSamplesLeft(NULL), _packetsReceivedThisPlayback(0), + _echoSamplesLeft(NULL), _isSendingEchoPing(false), _pingAnalysisPending(false), _pingFramesToRecord(0), diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index ca7220afa8..2801d16748 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -319,14 +319,7 @@ int VoxelSystem::newTreeToArrays(VoxelNode* node) { int voxelsUpdated = 0; bool shouldRender = false; // assume we don't need to render it // if it's colored, we might need to render it! - if (node->isColored()) { - float distanceToNode = node->distanceToCamera(*Application::getInstance()->getViewFrustum()); - float boundary = boundaryDistanceForRenderLevel(node->getLevel()); - float childBoundary = boundaryDistanceForRenderLevel(node->getLevel() + 1); - bool inBoundary = (distanceToNode <= boundary); - bool inChildBoundary = (distanceToNode <= childBoundary); - shouldRender = (node->isLeaf() && inChildBoundary) || (inBoundary && !inChildBoundary); - } + shouldRender = node->calculateShouldRender(Application::getInstance()->getViewFrustum()); node->setShouldRender(shouldRender && !node->isStagedForDeletion()); // let children figure out their renderness if (!node->isLeaf()) { diff --git a/libraries/voxels/src/AABox.cpp b/libraries/voxels/src/AABox.cpp index 36e65561c3..dc9deb21db 100644 --- a/libraries/voxels/src/AABox.cpp +++ b/libraries/voxels/src/AABox.cpp @@ -18,6 +18,7 @@ void AABox::scale(float scale) { _corner = _corner * scale; _size = _size * scale; _center = _center * scale; + _topFarLeft = _topFarLeft * scale; } @@ -60,6 +61,7 @@ void AABox::setBox(const glm::vec3& corner, const glm::vec3& size) { _corner.z -= _size.z; } _center = _corner + (_size * 0.5f); + _topFarLeft = _corner + _size; } glm::vec3 AABox::getVertexP(const glm::vec3& normal) const { diff --git a/libraries/voxels/src/AABox.h b/libraries/voxels/src/AABox.h index 735799cd05..74d54ba777 100644 --- a/libraries/voxels/src/AABox.h +++ b/libraries/voxels/src/AABox.h @@ -41,10 +41,13 @@ class AABox public: - AABox(const glm::vec3& corner, float size) : _corner(corner), _size(size, size, size) { }; - AABox(const glm::vec3& corner, float x, float y, float z) : _corner(corner), _size(x, y, z) { }; - AABox(const glm::vec3& corner, const glm::vec3& size) : _corner(corner), _size(size) { }; - AABox() : _corner(0,0,0), _size(0,0,0) { } + AABox(const glm::vec3& corner, float size) : + _corner(corner), _size(size, size, size) { _topFarLeft = _corner + _size; }; + AABox(const glm::vec3& corner, float x, float y, float z) : + _corner(corner), _size(x, y, z) { _topFarLeft = _corner + _size; }; + AABox(const glm::vec3& corner, const glm::vec3& size) : + _corner(corner), _size(size) { _topFarLeft = _corner + _size; }; + AABox() : _corner(0,0,0), _size(0,0,0), _topFarLeft(0,0,0) { } ~AABox() { } void setBox(const glm::vec3& corner, float x, float y, float z) { setBox(corner,glm::vec3(x,y,z)); }; @@ -56,9 +59,10 @@ public: void scale(float scale); - const glm::vec3& getCorner() const { return _corner; }; - const glm::vec3& getSize() const { return _size; }; - const glm::vec3& getCenter() const { return _center; }; + const glm::vec3& getCorner() const { return _corner; }; + const glm::vec3& getSize() const { return _size; }; + const glm::vec3& getCenter() const { return _center; }; + const glm::vec3& getTopFarLeft() const { return _topFarLeft; }; glm::vec3 getVertex(BoxVertex vertex) const; @@ -81,6 +85,7 @@ private: glm::vec3 _corner; glm::vec3 _center; glm::vec3 _size; + glm::vec3 _topFarLeft; }; diff --git a/libraries/voxels/src/ViewFrustum.cpp b/libraries/voxels/src/ViewFrustum.cpp index 166a2f2126..415a064e5d 100644 --- a/libraries/voxels/src/ViewFrustum.cpp +++ b/libraries/voxels/src/ViewFrustum.cpp @@ -530,8 +530,10 @@ const int hullVertexLookup[MAX_POSSIBLE_COMBINATIONS][MAX_PROJECTED_POLYGON_VERT }; VoxelProjectedPolygon ViewFrustum::getProjectedPolygon(const AABox& box) const { - glm::vec3 bottomNearRight = box.getCorner(); - glm::vec3 topFarLeft = box.getCorner() + box.getSize(); + const glm::vec3& bottomNearRight = box.getCorner(); + const glm::vec3& topFarLeft = box.getTopFarLeft(); + + int lookUp = ((_position.x < bottomNearRight.x) ) // 1 = right | compute 6-bit + ((_position.x > topFarLeft.x ) << 1) // 2 = left | code to + ((_position.y < bottomNearRight.y) << 2) // 4 = bottom | classify camera @@ -593,3 +595,40 @@ VoxelProjectedPolygon ViewFrustum::getProjectedPolygon(const AABox& box) const { projectedPolygon.setProjectionType(lookUp); // remember the projection type return projectedPolygon; } + + +// Similar strategy to getProjectedPolygon() we use the knowledge of camera position relative to the +// axis-aligned voxels to determine which of the voxels vertices must be the furthest. No need for +// squares and square-roots. Just compares. +glm::vec3 ViewFrustum::getFurthestPointFromCamera(const AABox& box) const { + const glm::vec3& center = box.getCenter(); + const glm::vec3& bottomNearRight = box.getCorner(); + const glm::vec3& topFarLeft = box.getTopFarLeft(); + + glm::vec3 furthestPoint; + if (_position.x < center.x) { + // we are to the right of the center, so the left edge is furthest + furthestPoint.x = topFarLeft.x; + } else { + // we are to the left of the center, so the right edge is furthest (at center ok too) + furthestPoint.x = bottomNearRight.x; + } + + if (_position.y < center.y) { + // we are below of the center, so the top edge is furthest + furthestPoint.y = topFarLeft.y; + } else { + // we are above the center, so the lower edge is furthest (at center ok too) + furthestPoint.y = bottomNearRight.y; + } + + if (_position.z < center.z) { + // we are to the near side of the center, so the far side edge is furthest + furthestPoint.z = topFarLeft.z; + } else { + // we are to the far side of the center, so the near side edge is furthest (at center ok too) + furthestPoint.z = bottomNearRight.z; + } + + return furthestPoint; +} diff --git a/libraries/voxels/src/ViewFrustum.h b/libraries/voxels/src/ViewFrustum.h index 7308d5bb64..e364816c59 100644 --- a/libraries/voxels/src/ViewFrustum.h +++ b/libraries/voxels/src/ViewFrustum.h @@ -90,6 +90,7 @@ public: glm::vec2 projectPoint(glm::vec3 point, bool& pointInView) const; VoxelProjectedPolygon getProjectedPolygon(const AABox& box) const; + glm::vec3 getFurthestPointFromCamera(const AABox& box) const; private: diff --git a/libraries/voxels/src/VoxelNode.cpp b/libraries/voxels/src/VoxelNode.cpp index a20715635f..6a702e75ba 100644 --- a/libraries/voxels/src/VoxelNode.cpp +++ b/libraries/voxels/src/VoxelNode.cpp @@ -335,6 +335,39 @@ ViewFrustum::location VoxelNode::inFrustum(const ViewFrustum& viewFrustum) const return viewFrustum.boxInFrustum(box); } +// There are two types of nodes for which we want to "render" +// 1) Leaves that are in the LOD +// 2) Non-leaves are more complicated though... usually you don't want to render them, but if their children +// wouldn't be rendered, then you do want to render them. But sometimes they have some children that ARE +// in the LOD, and others that are not. In this case we want to render the parent, and none of the children. +// +// Since, if we know the camera position and orientation, we can know which of the corners is the "furthest" +// corner. We can use we can use this corner as our "voxel position" to do our distance calculations off of. +// By doing this, we don't need to test each child voxel's position vs the LOD boundary +bool VoxelNode::calculateShouldRender(const ViewFrustum* viewFrustum, int boundaryLevelAdjust) const { + bool shouldRender = false; + if (isColored()) { + float furthestDistance = furthestDistanceToCamera(*viewFrustum); + float boundary = boundaryDistanceForRenderLevel(getLevel() + boundaryLevelAdjust); + float childBoundary = boundaryDistanceForRenderLevel(getLevel() + 1 + boundaryLevelAdjust); + bool inBoundary = (furthestDistance <= boundary); + bool inChildBoundary = (furthestDistance <= childBoundary); + shouldRender = (isLeaf() && inChildBoundary) || (inBoundary && !inChildBoundary); + } + return shouldRender; +} + +// Calculates the distance to the furthest point of the voxel to the camera +float VoxelNode::furthestDistanceToCamera(const ViewFrustum& viewFrustum) const { + AABox box = getAABox(); + box.scale(TREE_SCALE); + glm::vec3 furthestPoint = viewFrustum.getFurthestPointFromCamera(box); + glm::vec3 temp = viewFrustum.getPosition() - furthestPoint; + float distanceSquared = glm::dot(temp, temp); + float distanceToVoxelCenter = sqrtf(distanceSquared); + return distanceToVoxelCenter; +} + float VoxelNode::distanceToCamera(const ViewFrustum& viewFrustum) const { glm::vec3 center = _box.getCenter() * (float)TREE_SCALE; glm::vec3 temp = viewFrustum.getPosition() - center; diff --git a/libraries/voxels/src/VoxelNode.h b/libraries/voxels/src/VoxelNode.h index a92c562a7e..461a723a8a 100644 --- a/libraries/voxels/src/VoxelNode.h +++ b/libraries/voxels/src/VoxelNode.h @@ -70,6 +70,9 @@ public: bool isInView(const ViewFrustum& viewFrustum) const; ViewFrustum::location inFrustum(const ViewFrustum& viewFrustum) const; float distanceToCamera(const ViewFrustum& viewFrustum) const; + float furthestDistanceToCamera(const ViewFrustum& viewFrustum) const; + + bool calculateShouldRender(const ViewFrustum* viewFrustum, int boundaryLevelAdjust = 0) const; // points are assumed to be in Voxel Coordinates (not TREE_SCALE'd) float distanceSquareToPoint(const glm::vec3& point) const; // when you don't need the actual distance, use this. diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index d2ea383b8e..b84b374309 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -891,17 +891,6 @@ void VoxelTree::createSphere(float radius, float xc, float yc, float zc, float v } } -int VoxelTree::searchForColoredNodes(int maxSearchLevel, VoxelNode* node, const ViewFrustum& viewFrustum, VoxelNodeBag& bag, - bool deltaViewFrustum, const ViewFrustum* lastViewFrustum) { - - // call the recursive version, this will add all found colored node roots to the bag - int currentSearchLevel = 0; - - int levelReached = searchForColoredNodesRecursion(maxSearchLevel, currentSearchLevel, rootNode, - viewFrustum, bag, deltaViewFrustum, lastViewFrustum); - return levelReached; -} - // combines the ray cast arguments into a single object class RayArgs { public: @@ -1014,88 +1003,6 @@ bool VoxelTree::findCapsulePenetration(const glm::vec3& start, const glm::vec3& return args.found; } -int VoxelTree::searchForColoredNodesRecursion(int maxSearchLevel, int& currentSearchLevel, - VoxelNode* node, const ViewFrustum& viewFrustum, VoxelNodeBag& bag, - bool deltaViewFrustum, const ViewFrustum* lastViewFrustum) { - - // Keep track of how deep we've searched. - currentSearchLevel++; - - // If we've passed our max Search Level, then stop searching. return last level searched - if (currentSearchLevel > maxSearchLevel) { - return currentSearchLevel-1; - } - - // If we're at a node that is out of view, then we can return, because no nodes below us will be in view! - if (!node->isInView(viewFrustum)) { - return currentSearchLevel; - } - - // Ok, this is a little tricky, each child may have been deeper than the others, so we need to track - // how deep each child went. And we actually return the maximum of each child. We use these variables below - // when we recurse the children. - int thisLevel = currentSearchLevel; - int maxChildLevel = thisLevel; - - VoxelNode* inViewChildren[NUMBER_OF_CHILDREN]; - float distancesToChildren[NUMBER_OF_CHILDREN]; - int positionOfChildren[NUMBER_OF_CHILDREN]; - int inViewCount = 0; - int inViewNotLeafCount = 0; - int inViewWithColorCount = 0; - - // for each child node, check to see if they exist, are colored, and in view, and if so - // add them to our distance ordered array of children - for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { - VoxelNode* childNode = node->getChildAtIndex(i); - bool childIsColored = (childNode && childNode->isColored()); - bool childIsInView = (childNode && childNode->isInView(viewFrustum)); - bool childIsLeaf = (childNode && childNode->isLeaf()); - - if (childIsInView) { - - // track children in view as existing and not a leaf - if (!childIsLeaf) { - inViewNotLeafCount++; - } - - // track children with actual color - if (childIsColored) { - inViewWithColorCount++; - } - - float distance = childNode->distanceToCamera(viewFrustum); - - if (distance < boundaryDistanceForRenderLevel(*childNode->getOctalCode() + 1)) { - inViewCount = insertIntoSortedArrays((void*)childNode, distance, i, - (void**)&inViewChildren, (float*)&distancesToChildren, - (int*)&positionOfChildren, inViewCount, NUMBER_OF_CHILDREN); - } - } - } - - // If we have children with color, then we do want to add this node (and it's descendants) to the bag to be written - // we don't need to dig deeper. - // - // XXXBHG - this might be a good time to look at colors and add them to a dictionary? But we're not planning - // on scanning the whole tree, so we won't actually see all the colors, so maybe no point in that. - if (inViewWithColorCount) { - bag.insert(node); - } else { - // at this point, we need to iterate the children who are in view, even if not colored - // and we need to determine if there's a deeper tree below them that we care about. We will iterate - // these based on which tree is closer. - for (int i = 0; i < inViewCount; i++) { - VoxelNode* childNode = inViewChildren[i]; - thisLevel = currentSearchLevel; // reset this, since the children will munge it up - int childLevelReached = searchForColoredNodesRecursion(maxSearchLevel, thisLevel, childNode, viewFrustum, bag, - deltaViewFrustum, lastViewFrustum); - maxChildLevel = std::max(maxChildLevel, childLevelReached); - } - } - return maxChildLevel; -} - int VoxelTree::encodeTreeBitstream(VoxelNode* node, unsigned char* outputBuffer, int availableBytes, VoxelNodeBag& bag, EncodeBitstreamParams& params) const { @@ -1344,19 +1251,10 @@ int VoxelTree::encodeTreeBitstreamRecursion(VoxelNode* node, unsigned char* outp } // wants occlusion culling & isLeaf() - // There are two types of nodes for which we want to send colors: - // 1) Leaves - obviously - // 2) Non-leaves who's children would be visible but are beyond our LOD. - bool isLeafOrLOD = childNode->isLeaf(); - if (params.viewFrustum && childNode->isColored() && !childNode->isLeaf()) { - int childLevel = childNode->getLevel(); - float childBoundary = boundaryDistanceForRenderLevel(childLevel + params.boundaryLevelAdjust); - float grandChildBoundary = boundaryDistanceForRenderLevel(childLevel + 1 + params.boundaryLevelAdjust); - isLeafOrLOD = ((distance <= childBoundary) && !(distance <= grandChildBoundary)); - } + bool shouldRender = childNode->calculateShouldRender(params.viewFrustum, params.boundaryLevelAdjust); // track children with actual color, only if the child wasn't previously in view! - if (childNode && isLeafOrLOD && childNode->isColored() && !childIsOccluded) { + if (shouldRender && !childIsOccluded) { bool childWasInView = false; if (childNode && params.deltaViewFrustum && params.lastViewFrustum) { @@ -1368,7 +1266,7 @@ int VoxelTree::encodeTreeBitstreamRecursion(VoxelNode* node, unsigned char* outp } else { childWasInView = location == ViewFrustum::INSIDE; } - } + } // If our child wasn't in view (or we're ignoring wasInView) then we add it to our sending items if (!childWasInView) { diff --git a/libraries/voxels/src/VoxelTree.h b/libraries/voxels/src/VoxelTree.h index 61f2e13056..9c8e6e7d16 100644 --- a/libraries/voxels/src/VoxelTree.h +++ b/libraries/voxels/src/VoxelTree.h @@ -123,9 +123,6 @@ public: int encodeTreeBitstream(VoxelNode* node, unsigned char* outputBuffer, int availableBytes, VoxelNodeBag& bag, EncodeBitstreamParams& params) const; - int searchForColoredNodes(int maxSearchLevel, VoxelNode* node, const ViewFrustum& viewFrustum, VoxelNodeBag& bag, - bool deltaViewFrustum = false, const ViewFrustum* lastViewFrustum = NULL); - bool isDirty() const { return _isDirty; }; void clearDirtyBit() { _isDirty = false; }; void setDirtyBit() { _isDirty = true; }; @@ -171,10 +168,6 @@ private: int encodeTreeBitstreamRecursion(VoxelNode* node, unsigned char* outputBuffer, int availableBytes, VoxelNodeBag& bag, EncodeBitstreamParams& params, int& currentEncodeLevel) const; - int searchForColoredNodesRecursion(int maxSearchLevel, int& currentSearchLevel, - VoxelNode* node, const ViewFrustum& viewFrustum, VoxelNodeBag& bag, - bool deltaViewFrustum, const ViewFrustum* lastViewFrustum); - static bool countVoxelsOperation(VoxelNode* node, void* extraData); VoxelNode* nodeForOctalCode(VoxelNode* ancestorNode, unsigned char* needleCode, VoxelNode** parentOfFoundNode) const; diff --git a/voxel-server/src/main.cpp b/voxel-server/src/main.cpp index cf59f226db..254b1d2d96 100644 --- a/voxel-server/src/main.cpp +++ b/voxel-server/src/main.cpp @@ -60,7 +60,6 @@ bool wantLocalDomain = false; bool wantColorRandomizer = false; bool debugVoxelSending = false; bool shouldShowAnimationDebug = false; -bool wantSearchForColoredNodes = false; EnvironmentData environmentData[3]; @@ -121,8 +120,6 @@ void deepestLevelVoxelDistributor(NodeList* nodeList, pthread_mutex_lock(&::treeLock); - int maxLevelReached = 0; - uint64_t start = usecTimestampNow(); int truePacketsSent = 0; int trueBytesSent = 0; @@ -200,38 +197,7 @@ void deepestLevelVoxelDistributor(NodeList* nodeList, nodeData->map.erase(); } - // For now, we're going to disable the "search for colored nodes" because that strategy doesn't work when we support - // deletion of nodes. Instead if we just start at the root we get the correct behavior we want. We are keeping this - // code for now because we want to be able to go back to it and find a solution to support both. The search method - // helps improve overall bitrate performance. - if (::wantSearchForColoredNodes) { - // If the bag was empty, then send everything in view, not just the delta - maxLevelReached = serverTree.searchForColoredNodes(INT_MAX, serverTree.rootNode, nodeData->getCurrentViewFrustum(), - nodeData->nodeBag, wantDelta, lastViewFrustum); - - // if nothing was found in view, send the root node. - if (nodeData->nodeBag.isEmpty()){ - nodeData->nodeBag.insert(serverTree.rootNode); - } - nodeData->setViewSent(false); - } else { - nodeData->nodeBag.insert(serverTree.rootNode); - } - } - uint64_t end = usecTimestampNow(); - int elapsedmsec = (end - start)/1000; - if (elapsedmsec > 100) { - if (elapsedmsec > 1000) { - int elapsedsec = (end - start)/1000000; - printf("WARNING! searchForColoredNodes() took %d seconds to identify %d nodes at level %d\n", - elapsedsec, nodeData->nodeBag.count(), maxLevelReached); - } else { - printf("WARNING! searchForColoredNodes() took %d milliseconds to identify %d nodes at level %d\n", - elapsedmsec, nodeData->nodeBag.count(), maxLevelReached); - } - } else if (::debugVoxelSending) { - printf("searchForColoredNodes() took %d milliseconds to identify %d nodes at level %d\n", - elapsedmsec, nodeData->nodeBag.count(), maxLevelReached); + nodeData->nodeBag.insert(serverTree.rootNode); } // If we have something in our nodeBag, then turn them into packets and send them out... @@ -438,10 +404,6 @@ int main(int argc, const char * argv[]) { ::wantColorRandomizer = cmdOptionExists(argc, argv, WANT_COLOR_RANDOMIZER); printf("wantColorRandomizer=%s\n", debug::valueOf(::wantColorRandomizer)); - const char* WANT_SEARCH_FOR_NODES = "--wantSearchForColoredNodes"; - ::wantSearchForColoredNodes = cmdOptionExists(argc, argv, WANT_SEARCH_FOR_NODES); - printf("wantSearchForColoredNodes=%s\n", debug::valueOf(::wantSearchForColoredNodes)); - // By default we will voxel persist, if you want to disable this, then pass in this parameter const char* NO_VOXEL_PERSIST = "--NoVoxelPersist"; if (cmdOptionExists(argc, argv, NO_VOXEL_PERSIST)) { From f078d646d76cc355f0e965ac8b53cb8a41a590d8 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 15 Jul 2013 01:41:28 -0700 Subject: [PATCH 07/13] small spacing change --- libraries/voxels/src/AABox.h | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/libraries/voxels/src/AABox.h b/libraries/voxels/src/AABox.h index 74d54ba777..5a9970f2c1 100644 --- a/libraries/voxels/src/AABox.h +++ b/libraries/voxels/src/AABox.h @@ -41,14 +41,11 @@ class AABox public: - AABox(const glm::vec3& corner, float size) : - _corner(corner), _size(size, size, size) { _topFarLeft = _corner + _size; }; - AABox(const glm::vec3& corner, float x, float y, float z) : - _corner(corner), _size(x, y, z) { _topFarLeft = _corner + _size; }; - AABox(const glm::vec3& corner, const glm::vec3& size) : - _corner(corner), _size(size) { _topFarLeft = _corner + _size; }; - AABox() : _corner(0,0,0), _size(0,0,0), _topFarLeft(0,0,0) { } - ~AABox() { } + AABox(const glm::vec3& corner, float size) : _corner(corner), _size(size, size, size), _topFarLeft(_corner + _size){}; + AABox(const glm::vec3& corner, float x, float y, float z) : _corner(corner), _size(x, y, z), _topFarLeft(_corner + _size){}; + AABox(const glm::vec3& corner, const glm::vec3& size) : _corner(corner), _size(size), _topFarLeft(_corner + _size){}; + AABox() : _corner(0,0,0), _size(0,0,0), _topFarLeft(0,0,0){}; + ~AABox() {}; void setBox(const glm::vec3& corner, float x, float y, float z) { setBox(corner,glm::vec3(x,y,z)); }; void setBox(const glm::vec3& corner, const glm::vec3& size); From 133bbd81febe7d26ae849087b2ca9e6c655e7a3d Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 15 Jul 2013 01:42:21 -0700 Subject: [PATCH 08/13] small spacing change --- libraries/voxels/src/ViewFrustum.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/libraries/voxels/src/ViewFrustum.cpp b/libraries/voxels/src/ViewFrustum.cpp index 415a064e5d..dd4562cd8d 100644 --- a/libraries/voxels/src/ViewFrustum.cpp +++ b/libraries/voxels/src/ViewFrustum.cpp @@ -532,8 +532,6 @@ const int hullVertexLookup[MAX_POSSIBLE_COMBINATIONS][MAX_PROJECTED_POLYGON_VERT VoxelProjectedPolygon ViewFrustum::getProjectedPolygon(const AABox& box) const { const glm::vec3& bottomNearRight = box.getCorner(); const glm::vec3& topFarLeft = box.getTopFarLeft(); - - int lookUp = ((_position.x < bottomNearRight.x) ) // 1 = right | compute 6-bit + ((_position.x > topFarLeft.x ) << 1) // 2 = left | code to + ((_position.y < bottomNearRight.y) << 2) // 4 = bottom | classify camera @@ -541,8 +539,6 @@ VoxelProjectedPolygon ViewFrustum::getProjectedPolygon(const AABox& box) const { + ((_position.z < bottomNearRight.z) << 4) // 16 = front/near | the 6 defining + ((_position.z > topFarLeft.z ) << 5); // 32 = back/far | planes - //printLog(">>>>>>>>> ViewFrustum::getProjectedPolygon() lookup=%d\n",lookUp); - int vertexCount = hullVertexLookup[lookUp][0]; //look up number of vertices VoxelProjectedPolygon projectedPolygon(vertexCount); From 64681fa1e455e4507e240ec4e0bea145938309b3 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 15 Jul 2013 09:21:47 -0700 Subject: [PATCH 09/13] CR feedback --- libraries/voxels/src/AABox.cpp | 16 ++++++++++++++++ libraries/voxels/src/AABox.h | 8 ++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/libraries/voxels/src/AABox.cpp b/libraries/voxels/src/AABox.cpp index dc9deb21db..6a9dd68082 100644 --- a/libraries/voxels/src/AABox.cpp +++ b/libraries/voxels/src/AABox.cpp @@ -13,6 +13,22 @@ #include "AABox.h" #include "GeometryUtil.h" +AABox::AABox(const glm::vec3& corner, float size) : _corner(corner), _size(size, size, size), _topFarLeft(_corner + _size) +{ +}; + +AABox::AABox(const glm::vec3& corner, float x, float y, float z) : _corner(corner), _size(x, y, z), _topFarLeft(_corner + _size) +{ +}; + +AABox::AABox(const glm::vec3& corner, const glm::vec3& size) : _corner(corner), _size(size), _topFarLeft(_corner + _size) +{ +}; + +AABox::AABox() : _corner(0,0,0), _size(0,0,0), _topFarLeft(0,0,0) +{ +}; + void AABox::scale(float scale) { _corner = _corner * scale; diff --git a/libraries/voxels/src/AABox.h b/libraries/voxels/src/AABox.h index 5a9970f2c1..7bb203aaf4 100644 --- a/libraries/voxels/src/AABox.h +++ b/libraries/voxels/src/AABox.h @@ -41,10 +41,10 @@ class AABox public: - AABox(const glm::vec3& corner, float size) : _corner(corner), _size(size, size, size), _topFarLeft(_corner + _size){}; - AABox(const glm::vec3& corner, float x, float y, float z) : _corner(corner), _size(x, y, z), _topFarLeft(_corner + _size){}; - AABox(const glm::vec3& corner, const glm::vec3& size) : _corner(corner), _size(size), _topFarLeft(_corner + _size){}; - AABox() : _corner(0,0,0), _size(0,0,0), _topFarLeft(0,0,0){}; + AABox(const glm::vec3& corner, float size); + AABox(const glm::vec3& corner, float x, float y, float z); + AABox(const glm::vec3& corner, const glm::vec3& size); + AABox(); ~AABox() {}; void setBox(const glm::vec3& corner, float x, float y, float z) { setBox(corner,glm::vec3(x,y,z)); }; From 8d4f7f8a2a57ddb49bb83662f84729a967ffbea1 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 15 Jul 2013 09:26:19 -0700 Subject: [PATCH 10/13] CR feedback --- libraries/voxels/src/VoxelNode.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/libraries/voxels/src/VoxelNode.cpp b/libraries/voxels/src/VoxelNode.cpp index 6a702e75ba..61a174f90b 100644 --- a/libraries/voxels/src/VoxelNode.cpp +++ b/libraries/voxels/src/VoxelNode.cpp @@ -363,16 +363,14 @@ float VoxelNode::furthestDistanceToCamera(const ViewFrustum& viewFrustum) const box.scale(TREE_SCALE); glm::vec3 furthestPoint = viewFrustum.getFurthestPointFromCamera(box); glm::vec3 temp = viewFrustum.getPosition() - furthestPoint; - float distanceSquared = glm::dot(temp, temp); - float distanceToVoxelCenter = sqrtf(distanceSquared); + float distanceToVoxelCenter = sqrtf(glm::dot(temp, temp)); return distanceToVoxelCenter; } float VoxelNode::distanceToCamera(const ViewFrustum& viewFrustum) const { glm::vec3 center = _box.getCenter() * (float)TREE_SCALE; glm::vec3 temp = viewFrustum.getPosition() - center; - float distanceSquared = glm::dot(temp, temp); - float distanceToVoxelCenter = sqrtf(distanceSquared); + float distanceToVoxelCenter = sqrtf(glm::dot(temp, temp)); return distanceToVoxelCenter; } @@ -384,7 +382,6 @@ float VoxelNode::distanceSquareToPoint(const glm::vec3& point) const { float VoxelNode::distanceToPoint(const glm::vec3& point) const { glm::vec3 temp = point - _box.getCenter(); - float distanceSquare = glm::dot(temp, temp); - float distance = sqrtf(distanceSquare); + float distance = sqrtf(glm::dot(temp, temp)); return distance; } From bf3c4fa1f8ec324baffdb34ae353875e0651f6a8 Mon Sep 17 00:00:00 2001 From: atlante45 Date: Mon, 15 Jul 2013 12:04:24 -0700 Subject: [PATCH 11/13] Avatar resizing final commit --- interface/src/Application.cpp | 12 ++++++------ interface/src/Avatar.cpp | 4 ++-- interface/src/Camera.cpp | 16 ++++++++-------- interface/src/Camera.h | 1 + libraries/avatars/src/AvatarData.cpp | 1 - 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 056d9b2fa9..a795398b23 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1183,21 +1183,21 @@ void Application::setRenderThirdPerson(bool thirdPerson) { } void Application::increaseAvatarSize() { - if (3.0f < _myAvatar.getScale() + 0.25f) { + if (5.0f < _myAvatar.getScale() + 0.05f) { return; } - _myAvatar.setScale(_myAvatar.getScale() + 0.25f); - _myCamera.setScale(_myAvatar.getScale() + 0.25f); + _myAvatar.setScale(_myAvatar.getScale() + 0.05f); + _myCamera.setScale(_myAvatar.getScale() + 0.05f); } void Application::decreaseAvatarSize() { - if (_myAvatar.getScale() + 0.25f < 0.25f) { + if (_myAvatar.getScale() - 0.05f < 0.15f) { return; } - _myAvatar.setScale(_myAvatar.getScale() - 0.25f); - _myCamera.setScale(_myAvatar.getScale() - 0.25f); + _myAvatar.setScale(_myAvatar.getScale() - 0.05f); + _myCamera.setScale(_myAvatar.getScale() - 0.05f); } void Application::setFrustumOffset(bool frustumOffset) { diff --git a/interface/src/Avatar.cpp b/interface/src/Avatar.cpp index 08d977d975..b384b61da3 100755 --- a/interface/src/Avatar.cpp +++ b/interface/src/Avatar.cpp @@ -575,13 +575,13 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { _bodyYawDelta *= bodySpinMomentum; _bodyRollDelta *= bodySpinMomentum; - const float MAX_STATIC_FRICTION_VELOCITY = _scale * 0.5f; + const float MAX_STATIC_FRICTION_VELOCITY = 0.5f; const float STATIC_FRICTION_STRENGTH = _scale * 20.f; applyStaticFriction(deltaTime, _velocity, MAX_STATIC_FRICTION_VELOCITY, STATIC_FRICTION_STRENGTH); const float LINEAR_DAMPING_STRENGTH = 1.0f; const float SPEED_BRAKE_POWER = _scale * 10.0f; - const float SQUARED_DAMPING_STRENGTH = _scale * 0.2f; + const float SQUARED_DAMPING_STRENGTH = 0.2f; if (_speedBrakes) { applyDamping(deltaTime, _velocity, LINEAR_DAMPING_STRENGTH * SPEED_BRAKE_POWER, SQUARED_DAMPING_STRENGTH * SPEED_BRAKE_POWER); } else { diff --git a/interface/src/Camera.cpp b/interface/src/Camera.cpp index 5ad728d30c..8aaec5b899 100644 --- a/interface/src/Camera.cpp +++ b/interface/src/Camera.cpp @@ -92,14 +92,14 @@ void Camera::updateFollowMode(float deltaTime) { if (_needsToInitialize || (_tightness == 0.0f)) { _rotation = _targetRotation; - _idealPosition = _targetPosition + _rotation * glm::vec3(0.0f, _upShift, _distance); + _idealPosition = _targetPosition + _scale * (_rotation * glm::vec3(0.0f, _upShift, _distance)); _position = _idealPosition; _needsToInitialize = false; } else { // pull rotation towards ideal _rotation = safeMix(_rotation, _targetRotation, t); - _idealPosition = _targetPosition + _rotation * glm::vec3(0.0f, _upShift, _distance); + _idealPosition = _targetPosition + _scale * (_rotation * glm::vec3(0.0f, _upShift, _distance)); _position += (_idealPosition - _position) * t; } } @@ -124,18 +124,18 @@ void Camera::setMode(CameraMode m) { _previousTightness = _tightness; if (_mode == CAMERA_MODE_THIRD_PERSON) { - _newUpShift = _scale * CAMERA_THIRD_PERSON_MODE_UP_SHIFT; - _newDistance = _scale * CAMERA_THIRD_PERSON_MODE_DISTANCE; + _newUpShift = CAMERA_THIRD_PERSON_MODE_UP_SHIFT; + _newDistance = CAMERA_THIRD_PERSON_MODE_DISTANCE; _newTightness = CAMERA_THIRD_PERSON_MODE_TIGHTNESS; } else if (_mode == CAMERA_MODE_FIRST_PERSON) { - _newUpShift = _scale * CAMERA_FIRST_PERSON_MODE_UP_SHIFT; - _newDistance = _scale * CAMERA_FIRST_PERSON_MODE_DISTANCE; + _newUpShift = CAMERA_FIRST_PERSON_MODE_UP_SHIFT; + _newDistance = CAMERA_FIRST_PERSON_MODE_DISTANCE; _newTightness = CAMERA_FIRST_PERSON_MODE_TIGHTNESS; } else if (_mode == CAMERA_MODE_MIRROR) { - _newUpShift = _scale * CAMERA_MIRROR_MODE_UP_SHIFT; - _newDistance = _scale * CAMERA_MIRROR_MODE_DISTANCE; + _newUpShift = CAMERA_MIRROR_MODE_UP_SHIFT; + _newDistance = CAMERA_MIRROR_MODE_DISTANCE; _newTightness = CAMERA_MIRROR_MODE_TIGHTNESS; } } diff --git a/interface/src/Camera.h b/interface/src/Camera.h index e890bad400..d83a36ee16 100644 --- a/interface/src/Camera.h +++ b/interface/src/Camera.h @@ -59,6 +59,7 @@ public: float getFarClip () { return _scale * _farClip; } const glm::vec3& getEyeOffsetPosition () { return _eyeOffsetPosition; } const glm::quat& getEyeOffsetOrientation () { return _eyeOffsetOrientation; } + float getScale () { return _scale; } bool getFrustumNeedsReshape(); // call to find out if the view frustum needs to be reshaped void setFrustumWasReshaped(); // call this after reshaping the view frustum. diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index 99961df988..e198336f54 100755 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -200,7 +200,6 @@ int AvatarData::parseData(unsigned char* sourceBuffer, int numBytes) { sourceBuffer += unpackFloatAngleFromTwoByte((uint16_t*) sourceBuffer, &_bodyPitch); sourceBuffer += unpackFloatAngleFromTwoByte((uint16_t*) sourceBuffer, &_bodyRoll); sourceBuffer += unpackFloatRatioFromTwoByte( sourceBuffer, _newScale); - std::cout << "New Scale : " << _newScale << std::endl; // Head rotation (NOTE: This needs to become a quaternion to save two bytes) float headYaw, headPitch, headRoll; From fbcacc8cf786cfa5fabcd5ddfe98b0b10802b6a4 Mon Sep 17 00:00:00 2001 From: atlante45 Date: Mon, 15 Jul 2013 12:11:29 -0700 Subject: [PATCH 12/13] Few pre-review change for avatar resizing --- interface/src/Avatar.cpp | 3 +- interface/src/Webcam.cpp | 287 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 288 insertions(+), 2 deletions(-) diff --git a/interface/src/Avatar.cpp b/interface/src/Avatar.cpp index b384b61da3..a1175e3b23 100755 --- a/interface/src/Avatar.cpp +++ b/interface/src/Avatar.cpp @@ -1396,7 +1396,8 @@ void Avatar::renderJointConnectingCone(glm::vec3 position1, glm::vec3 position2, glEnd(); } -void Avatar::setScale(const float scale) {_scale = scale; +void Avatar::setScale(const float scale) { + _scale = scale; _newScale = _scale; _skeleton.setScale(_scale); diff --git a/interface/src/Webcam.cpp b/interface/src/Webcam.cpp index 87d26e036b..0d417583d2 100644 --- a/interface/src/Webcam.cpp +++ b/interface/src/Webcam.cpp @@ -144,17 +144,132 @@ Webcam::~Webcam() { // stop the grabber thread _grabberThread.quit(); _grabberThread.wait(); - + delete _grabber; } void Webcam::setFrame(const Mat& frame, int format, const Mat& depth, const RotatedRect& faceRect, const JointVector& joints) { + IplImage image = frame; + glPixelStorei(GL_UNPACK_ROW_LENGTH, image.widthStep / 3); + if (_frameTextureID == 0) { + glGenTextures(1, &_frameTextureID); + glBindTexture(GL_TEXTURE_2D, _frameTextureID); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, _frameWidth = image.width, _frameHeight = image.height, 0, format, + GL_UNSIGNED_BYTE, image.imageData); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + printLog("Capturing video at %dx%d.\n", _frameWidth, _frameHeight); + + } else { + glBindTexture(GL_TEXTURE_2D, _frameTextureID); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _frameWidth, _frameHeight, format, GL_UNSIGNED_BYTE, image.imageData); + } + + if (!depth.empty()) { + IplImage depthImage = depth; + glPixelStorei(GL_UNPACK_ROW_LENGTH, depthImage.widthStep); + if (_depthTextureID == 0) { + glGenTextures(1, &_depthTextureID); + glBindTexture(GL_TEXTURE_2D, _depthTextureID); + glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, _depthWidth = depthImage.width, _depthHeight = depthImage.height, 0, + GL_LUMINANCE, GL_UNSIGNED_BYTE, depthImage.imageData); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + printLog("Capturing depth at %dx%d.\n", _depthWidth, _depthHeight); + + } else { + glBindTexture(GL_TEXTURE_2D, _depthTextureID); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _depthWidth, _depthHeight, GL_LUMINANCE, + GL_UNSIGNED_BYTE, depthImage.imageData); + } + } + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + glBindTexture(GL_TEXTURE_2D, 0); + + // store our face rect and joints, update our frame count for fps computation + _faceRect = faceRect; + _joints = joints; + _frameCount++; + + const int MAX_FPS = 60; + const int MIN_FRAME_DELAY = 1000000 / MAX_FPS; + uint64_t now = usecTimestampNow(); + int remaining = MIN_FRAME_DELAY; + if (_startTimestamp == 0) { + _startTimestamp = now; + } else { + remaining -= (now - _lastFrameTimestamp); + } + _lastFrameTimestamp = now; + + // see if we have joint data + if (!_joints.isEmpty()) { + _estimatedJoints.resize(NUM_AVATAR_JOINTS); + glm::vec3 origin; + if (_joints[AVATAR_JOINT_LEFT_HIP].isValid && _joints[AVATAR_JOINT_RIGHT_HIP].isValid) { + origin = glm::mix(_joints[AVATAR_JOINT_LEFT_HIP].position, _joints[AVATAR_JOINT_RIGHT_HIP].position, 0.5f); + + } else if (_joints[AVATAR_JOINT_TORSO].isValid) { + const glm::vec3 TORSO_TO_PELVIS = glm::vec3(0.0f, -0.09f, -0.01f); + origin = _joints[AVATAR_JOINT_TORSO].position + TORSO_TO_PELVIS; + } + for (int i = 0; i < NUM_AVATAR_JOINTS; i++) { + if (!_joints[i].isValid) { + continue; + } + const float JOINT_SMOOTHING = 0.5f; + _estimatedJoints[i].isValid = true; + _estimatedJoints[i].position = glm::mix(_joints[i].position - origin, + _estimatedJoints[i].position, JOINT_SMOOTHING); + _estimatedJoints[i].rotation = safeMix(_joints[i].rotation, + _estimatedJoints[i].rotation, JOINT_SMOOTHING); + } + _estimatedRotation = safeEulerAngles(_estimatedJoints[AVATAR_JOINT_HEAD_BASE].rotation); + _estimatedPosition = _estimatedJoints[AVATAR_JOINT_HEAD_BASE].position; + + } else { + // roll is just the angle of the face rect (correcting for 180 degree rotations) + float roll = faceRect.angle; + if (roll < -90.0f) { + roll += 180.0f; + + } else if (roll > 90.0f) { + roll -= 180.0f; + } + const float ROTATION_SMOOTHING = 0.95f; + _estimatedRotation.z = glm::mix(roll, _estimatedRotation.z, ROTATION_SMOOTHING); + + // determine position based on translation and scaling of the face rect + if (_initialFaceRect.size.area() == 0) { + _initialFaceRect = faceRect; + _estimatedPosition = glm::vec3(); + + } else { + float proportion = sqrtf(_initialFaceRect.size.area() / (float)faceRect.size.area()); + const float DISTANCE_TO_CAMERA = 0.333f; + const float POSITION_SCALE = 0.5f; + float z = DISTANCE_TO_CAMERA * proportion - DISTANCE_TO_CAMERA; + glm::vec3 position = glm::vec3( + (faceRect.center.x - _initialFaceRect.center.x) * proportion * POSITION_SCALE / _frameWidth, + (faceRect.center.y - _initialFaceRect.center.y) * proportion * POSITION_SCALE / _frameWidth, + z); + const float POSITION_SMOOTHING = 0.95f; + _estimatedPosition = glm::mix(position, _estimatedPosition, POSITION_SMOOTHING); + } + } + + // note that we have data + _active = true; + + // let the grabber know we're ready for the next frame + QTimer::singleShot(qMax((int)remaining / 1000, 0), _grabber, SLOT(grabFrame())); } FrameGrabber::FrameGrabber() : _initialized(false), _capture(0), _searchWindow(0, 0, 0, 0) { } FrameGrabber::~FrameGrabber() { + if (_initialized) { + shutdown(); + } } #ifdef HAVE_OPENNI @@ -242,18 +357,188 @@ static void XN_CALLBACK_TYPE calibrationCompleted(SkeletonCapability& capability #endif void FrameGrabber::reset() { + _searchWindow = cv::Rect(0, 0, 0, 0); + +#ifdef HAVE_OPENNI + if (_userGenerator.IsValid() && _userGenerator.GetSkeletonCap().IsTracking(_userID)) { + _userGenerator.GetSkeletonCap().RequestCalibration(_userID, true); + } +#endif } void FrameGrabber::shutdown() { + if (_capture != 0) { + cvReleaseCapture(&_capture); + _capture = 0; + } + _initialized = false; + + thread()->quit(); } void FrameGrabber::grabFrame() { + if (!(_initialized || init())) { + return; + } + int format = GL_BGR; + Mat frame; + JointVector joints; + +#ifdef HAVE_OPENNI + if (_depthGenerator.IsValid()) { + _xnContext.WaitAnyUpdateAll(); + frame = Mat(_imageMetaData.YRes(), _imageMetaData.XRes(), CV_8UC3, (void*)_imageGenerator.GetImageMap()); + format = GL_RGB; + + Mat depth = Mat(_depthMetaData.YRes(), _depthMetaData.XRes(), CV_16UC1, (void*)_depthGenerator.GetDepthMap()); + const double EIGHT_BIT_MAX = 255; + const double ELEVEN_BIT_MAX = 2047; + depth.convertTo(_grayDepthFrame, CV_8UC1, EIGHT_BIT_MAX / ELEVEN_BIT_MAX); + + _userID = 0; + XnUInt16 userCount = 1; + _userGenerator.GetUsers(&_userID, userCount); + if (userCount > 0 && _userGenerator.GetSkeletonCap().IsTracking(_userID)) { + joints.resize(NUM_AVATAR_JOINTS); + const int MAX_ACTIVE_JOINTS = 16; + XnSkeletonJoint activeJoints[MAX_ACTIVE_JOINTS]; + XnUInt16 activeJointCount = MAX_ACTIVE_JOINTS; + _userGenerator.GetSkeletonCap().EnumerateActiveJoints(activeJoints, activeJointCount); + XnSkeletonJointTransformation transform; + for (int i = 0; i < activeJointCount; i++) { + AvatarJointID avatarJoint = xnToAvatarJoint(activeJoints[i]); + if (avatarJoint == AVATAR_JOINT_NULL) { + continue; + } + _userGenerator.GetSkeletonCap().GetSkeletonJoint(_userID, activeJoints[i], transform); + XnVector3D projected; + _depthGenerator.ConvertRealWorldToProjective(1, &transform.position.position, &projected); + glm::quat rotation = xnToGLM(transform.orientation.orientation); + int parentJoint = getParentJoint(activeJoints[i]); + if (parentJoint != -1) { + XnSkeletonJointOrientation parentOrientation; + _userGenerator.GetSkeletonCap().GetSkeletonJointOrientation( + _userID, (XnSkeletonJoint)parentJoint, parentOrientation); + rotation = glm::inverse(xnToGLM(parentOrientation.orientation)) * rotation; + } + const float METERS_PER_MM = 1.0f / 1000.0f; + joints[avatarJoint] = Joint(xnToGLM(transform.position.position, true) * METERS_PER_MM, + rotation, xnToGLM(projected)); + } + } + } +#endif + + if (frame.empty()) { + IplImage* image = cvQueryFrame(_capture); + if (image == 0) { + // try again later + QMetaObject::invokeMethod(this, "grabFrame", Qt::QueuedConnection); + return; + } + // make sure it's in the format we expect + if (image->nChannels != 3 || image->depth != IPL_DEPTH_8U || image->dataOrder != IPL_DATA_ORDER_PIXEL || + image->origin != 0) { + printLog("Invalid webcam image format.\n"); + return; + } + frame = image; + } + + // if we don't have a search window (yet), try using the face cascade + int channels = 0; + float ranges[] = { 0, 180 }; + const float* range = ranges; + if (_searchWindow.area() == 0) { + vector faces; + _faceCascade.detectMultiScale(frame, faces, 1.1, 6); + if (!faces.empty()) { + _searchWindow = faces.front(); + updateHSVFrame(frame, format); + + Mat faceHsv(_hsvFrame, _searchWindow); + Mat faceMask(_mask, _searchWindow); + int sizes = 30; + calcHist(&faceHsv, 1, &channels, faceMask, _histogram, 1, &sizes, &range); + double min, max; + minMaxLoc(_histogram, &min, &max); + _histogram.convertTo(_histogram, -1, (max == 0.0) ? 0.0 : 255.0 / max); + } + } + RotatedRect faceRect; + if (_searchWindow.area() > 0) { + updateHSVFrame(frame, format); + + calcBackProject(&_hsvFrame, 1, &channels, _histogram, _backProject, &range); + bitwise_and(_backProject, _mask, _backProject); + + faceRect = CamShift(_backProject, _searchWindow, TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1)); + _searchWindow = faceRect.boundingRect(); + } + QMetaObject::invokeMethod(Application::getInstance()->getWebcam(), "setFrame", + Q_ARG(cv::Mat, frame), Q_ARG(int, format), Q_ARG(cv::Mat, _grayDepthFrame), + Q_ARG(cv::RotatedRect, faceRect), Q_ARG(JointVector, joints)); } bool FrameGrabber::init() { + _initialized = true; + + // load our face cascade + switchToResourcesParentIfRequired(); + if (_faceCascade.empty() && !_faceCascade.load("resources/haarcascades/haarcascade_frontalface_alt.xml")) { + printLog("Failed to load Haar cascade for face tracking.\n"); + return false; + } + + // first try for a Kinect +#ifdef HAVE_OPENNI + _xnContext.Init(); + if (_depthGenerator.Create(_xnContext) == XN_STATUS_OK && _imageGenerator.Create(_xnContext) == XN_STATUS_OK && + _userGenerator.Create(_xnContext) == XN_STATUS_OK && + _userGenerator.IsCapabilitySupported(XN_CAPABILITY_SKELETON)) { + _depthGenerator.GetMetaData(_depthMetaData); + _imageGenerator.SetPixelFormat(XN_PIXEL_FORMAT_RGB24); + _imageGenerator.GetMetaData(_imageMetaData); + + XnCallbackHandle userCallbacks, calibrationStartCallback, calibrationCompleteCallback; + _userGenerator.RegisterUserCallbacks(newUser, lostUser, 0, userCallbacks); + _userGenerator.GetSkeletonCap().RegisterToCalibrationStart(calibrationStarted, 0, calibrationStartCallback); + _userGenerator.GetSkeletonCap().RegisterToCalibrationComplete(calibrationCompleted, 0, calibrationCompleteCallback); + + _userGenerator.GetSkeletonCap().SetSkeletonProfile(XN_SKEL_PROFILE_UPPER); + + _xnContext.StartGeneratingAll(); + return true; + } +#endif + + // next, an ordinary webcam + if ((_capture = cvCaptureFromCAM(-1)) == 0) { + printLog("Failed to open webcam.\n"); + return false; + } + const int IDEAL_FRAME_WIDTH = 320; + const int IDEAL_FRAME_HEIGHT = 240; + cvSetCaptureProperty(_capture, CV_CAP_PROP_FRAME_WIDTH, IDEAL_FRAME_WIDTH); + cvSetCaptureProperty(_capture, CV_CAP_PROP_FRAME_HEIGHT, IDEAL_FRAME_HEIGHT); + +#ifdef __APPLE__ + configureCamera(0x5ac, 0x8510, false, 0.975, 0.5, 1.0, 0.5, true, 0.5); +#else + cvSetCaptureProperty(_capture, CV_CAP_PROP_EXPOSURE, 0.5); + cvSetCaptureProperty(_capture, CV_CAP_PROP_CONTRAST, 0.5); + cvSetCaptureProperty(_capture, CV_CAP_PROP_SATURATION, 0.5); + cvSetCaptureProperty(_capture, CV_CAP_PROP_BRIGHTNESS, 0.5); + cvSetCaptureProperty(_capture, CV_CAP_PROP_HUE, 0.5); + cvSetCaptureProperty(_capture, CV_CAP_PROP_GAIN, 0.5); +#endif + + return true; } void FrameGrabber::updateHSVFrame(const Mat& frame, int format) { + cvtColor(frame, _hsvFrame, format == GL_RGB ? CV_RGB2HSV : CV_BGR2HSV); + inRange(_hsvFrame, Scalar(0, 55, 65), Scalar(180, 256, 256), _mask); } Joint::Joint(const glm::vec3& position, const glm::quat& rotation, const glm::vec3& projected) : From 423a0eaac85bf30bd92f673b0fbd8c39b0e50f5c Mon Sep 17 00:00:00 2001 From: atlante45 Date: Mon, 15 Jul 2013 12:28:22 -0700 Subject: [PATCH 13/13] Fixed typo --- interface/src/Application.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index a795398b23..75f45e8d91 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1583,7 +1583,7 @@ void Application::initMenu() { (_manualThirdPerson = renderMenu->addAction( "Third Person", this, SLOT(setRenderThirdPerson(bool))))->setCheckable(true); renderMenu->addAction("Increase Avatar Size", this, SLOT(increaseAvatarSize()), Qt::SHIFT | Qt::Key_Plus); - renderMenu->addAction("Decrease Avatar Siz+e", this, SLOT(decreaseAvatarSize()), Qt::SHIFT | Qt::Key_Minus); + renderMenu->addAction("Decrease Avatar Size", this, SLOT(decreaseAvatarSize()), Qt::SHIFT | Qt::Key_Minus); QMenu* toolsMenu = menuBar->addMenu("Tools");