History

JointStates are owned by Rig objects. Model, FaceModel, SkeletonModel call into their Rig pointer to access JointStates.

Seth Alves committed 11 years ago

Commit ee334ff1 parent 47965bc19 files · +549 −622Browse files

Showing the changes to interface/src/avatar/SkeletonModel.cpp (1 of 19 files). Show all

Unified · Split

interface/src/avatar/SkeletonModel.cpp+66−65
@@ -30,7 +30,7 @@ enum StandingFootState { · 29 unmodified lines
30};30};
31 31
32SkeletonModel::SkeletonModel(Avatar* owningAvatar, QObject* parent, RigPointer rig) :32SkeletonModel::SkeletonModel(Avatar* owningAvatar, QObject* parent, RigPointer rig) :
33 Model(parent, rig),33 Model(rig, parent),
34 _triangleFanID(DependencyManager::get<GeometryCache>()->allocateID()),34 _triangleFanID(DependencyManager::get<GeometryCache>()->allocateID()),
35 _owningAvatar(owningAvatar),35 _owningAvatar(owningAvatar),
36 _boundingShape(),36 _boundingShape(),
@@ -51,11 +51,13 @@ SkeletonModel::~SkeletonModel() { · 14 unmodified lines
51}51}
52 52
53void SkeletonModel::initJointStates(QVector<JointState> states) {53void SkeletonModel::initJointStates(QVector<JointState> states) {
54 Model::initJointStates(states);54 const FBXGeometry& geometry = _geometry->getFBXGeometry();
55 glm::mat4 parentTransform = glm::scale(_scale) * glm::translate(_offset) * geometry.offset;
56 _boundingRadius = _rig->initJointStates(states, parentTransform);
55 57
56 // Determine the default eye position for avatar scale = 1.058 // Determine the default eye position for avatar scale = 1.0
57 int headJointIndex = _geometry->getFBXGeometry().headJointIndex;59 int headJointIndex = _geometry->getFBXGeometry().headJointIndex;
58 if (0 <= headJointIndex && headJointIndex < _jointStates.size()) {60 if (0 <= headJointIndex && headJointIndex < _rig->getJointStateCount()) {
59 61
60 glm::vec3 leftEyePosition, rightEyePosition;62 glm::vec3 leftEyePosition, rightEyePosition;
61 getEyeModelPositions(leftEyePosition, rightEyePosition);63 getEyeModelPositions(leftEyePosition, rightEyePosition);
@@ -75,8 +77,8 @@ void SkeletonModel::initJointStates(QVector<JointState> states) { · 13 unmodified lines
75 // the SkeletonModel override of updateJointState() will clear the translation part77 // the SkeletonModel override of updateJointState() will clear the translation part
76 // of its root joint and we need that done before we try to build shapes hence we78 // of its root joint and we need that done before we try to build shapes hence we
77 // recompute all joint transforms at this time.79 // recompute all joint transforms at this time.
78 for (int i = 0; i < _jointStates.size(); i++) {80 for (int i = 0; i < _rig->getJointStateCount(); i++) {
79 updateJointState(i);81 _rig->updateJointState(i, parentTransform);
80 }82 }
81 83
82 clearShapes();84 clearShapes();
@@ -168,7 +170,7 @@ void SkeletonModel::getHandShapes(int jointIndex, QVector<const Shape*>& shapes) · 85 unmodified lines
168 || jointIndex == getRightHandJointIndex()) {170 || jointIndex == getRightHandJointIndex()) {
169 // get all shapes that have this hand as an ancestor in the skeleton heirarchy171 // get all shapes that have this hand as an ancestor in the skeleton heirarchy
170 const FBXGeometry& geometry = _geometry->getFBXGeometry();172 const FBXGeometry& geometry = _geometry->getFBXGeometry();
171 for (int i = 0; i < _jointStates.size(); i++) {173 for (int i = 0; i < _rig->getJointStateCount(); i++) {
172 const FBXJoint& joint = geometry.joints[i];174 const FBXJoint& joint = geometry.joints[i];
173 int parentIndex = joint.parentIndex;175 int parentIndex = joint.parentIndex;
174 Shape* shape = _shapes[i];176 Shape* shape = _shapes[i];
@@ -211,7 +213,7 @@ bool operator<(const IndexValue& firstIndex, const IndexValue& secondIndex) { · 36 unmodified lines
211}213}
212 214
213void SkeletonModel::applyHandPosition(int jointIndex, const glm::vec3& position) {215void SkeletonModel::applyHandPosition(int jointIndex, const glm::vec3& position) {
214 if (jointIndex == -1 || jointIndex >= _jointStates.size()) {216 if (jointIndex == -1 || jointIndex >= _rig->getJointStateCount()) {
215 return;217 return;
216 }218 }
217 // NOTE: 'position' is in model-frame219 // NOTE: 'position' is in model-frame
@@ -226,16 +228,20 @@ void SkeletonModel::applyHandPosition(int jointIndex, const glm::vec3& position) · 8 unmodified lines
226 if (forearmLength < EPSILON) {228 if (forearmLength < EPSILON) {
227 return;229 return;
228 }230 }
229 JointState& state = _jointStates[jointIndex];231 glm::quat handRotation;
230 glm::quat handRotation = state.getRotation();232 if (!_rig->getJointStateRotation(jointIndex, handRotation)) {
233 return;
234 }
231 235
232 // align hand with forearm236 // align hand with forearm
233 float sign = (jointIndex == geometry.rightHandJointIndex) ? 1.0f : -1.0f;237 float sign = (jointIndex == geometry.rightHandJointIndex) ? 1.0f : -1.0f;
234 state.applyRotationDelta(rotationBetween(handRotation * glm::vec3(-sign, 0.0f, 0.0f), forearmVector), true, PALM_PRIORITY);238 _rig->applyJointRotationDelta(jointIndex,
239 rotationBetween(handRotation * glm::vec3(-sign, 0.0f, 0.0f), forearmVector),
240 true, PALM_PRIORITY);
235}241}
236 242
237void SkeletonModel::applyPalmData(int jointIndex, PalmData& palm) {243void SkeletonModel::applyPalmData(int jointIndex, PalmData& palm) {
238 if (jointIndex == -1 || jointIndex >= _jointStates.size()) {244 if (jointIndex == -1 || jointIndex >= _rig->getJointStateCount()) {
239 return;245 return;
240 }246 }
241 const FBXGeometry& geometry = _geometry->getFBXGeometry();247 const FBXGeometry& geometry = _geometry->getFBXGeometry();
@@ -261,43 +267,40 @@ void SkeletonModel::applyPalmData(int jointIndex, PalmData& palm) { · 19 unmodified lines
261 glm::vec3 forearm = palmRotation * glm::vec3(sign * forearmLength, 0.0f, 0.0f);267 glm::vec3 forearm = palmRotation * glm::vec3(sign * forearmLength, 0.0f, 0.0f);
262 setJointPosition(parentJointIndex, palmPosition + forearm,268 setJointPosition(parentJointIndex, palmPosition + forearm,
263 glm::quat(), false, -1, false, glm::vec3(0.0f, -1.0f, 0.0f), PALM_PRIORITY);269 glm::quat(), false, -1, false, glm::vec3(0.0f, -1.0f, 0.0f), PALM_PRIORITY);
264 JointState& parentState = _jointStates[parentJointIndex];270 _rig->setJointRotationInBindFrame(parentJointIndex, palmRotation, PALM_PRIORITY);
265 parentState.setRotationInBindFrame(palmRotation, PALM_PRIORITY);
266 // lock hand to forearm by slamming its rotation (in parent-frame) to identity271 // lock hand to forearm by slamming its rotation (in parent-frame) to identity
267 _jointStates[jointIndex].setRotationInConstrainedFrame(glm::quat(), PALM_PRIORITY);272 _rig->setJointRotationInConstrainedFrame(jointIndex, glm::quat(), PALM_PRIORITY);
268 } else {273 } else {
269 inverseKinematics(jointIndex, palmPosition, palmRotation, PALM_PRIORITY);274 inverseKinematics(jointIndex, palmPosition, palmRotation, PALM_PRIORITY);
270 }275 }
271}276}
272 277
273void SkeletonModel::updateJointState(int index) {278void SkeletonModel::updateJointState(int index) {
274 if (index < 0 && index >= _jointStates.size()) {279 const FBXGeometry& geometry = _geometry->getFBXGeometry();
275 return; // bail280 glm::mat4 parentTransform = glm::scale(_scale) * glm::translate(_offset) * geometry.offset;
276 }281
277 JointState& state = _jointStates[index];282 const JointState joint = _rig->getJointState(index);
278 const FBXJoint& joint = state.getFBXJoint();283 if (joint.getParentIndex() >= 0 && joint.getParentIndex() < _rig->getJointStateCount()) {
279 if (joint.parentIndex >= 0 && joint.parentIndex < _jointStates.size()) {284 const JointState parentState = _rig->getJointState(joint.getParentIndex());
280 const JointState& parentState = _jointStates.at(joint.parentIndex);
281 const FBXGeometry& geometry = _geometry->getFBXGeometry();
282 if (index == geometry.leanJointIndex) {285 if (index == geometry.leanJointIndex) {
283 maybeUpdateLeanRotation(parentState, state);286 maybeUpdateLeanRotation(parentState, index);
284 287
285 } else if (index == geometry.neckJointIndex) {288 } else if (index == geometry.neckJointIndex) {
286 maybeUpdateNeckRotation(parentState, joint, state); 289 maybeUpdateNeckRotation(parentState, joint.getFBXJoint(), index);
287 290
288 } else if (index == geometry.leftEyeJointIndex || index == geometry.rightEyeJointIndex) {291 } else if (index == geometry.leftEyeJointIndex || index == geometry.rightEyeJointIndex) {
289 maybeUpdateEyeRotation(parentState, joint, state);292 maybeUpdateEyeRotation(parentState, joint.getFBXJoint(), index);
290 }293 }
291 }294 }
292 295
293 Model::updateJointState(index);296 _rig->updateJointState(index, parentTransform);
294 297
295 if (index == _geometry->getFBXGeometry().rootJointIndex) {298 if (index == _geometry->getFBXGeometry().rootJointIndex) {
296 state.clearTransformTranslation();299 _rig->clearJointTransformTranslation(index);
297 }300 }
298}301}
299 302
300void SkeletonModel::maybeUpdateLeanRotation(const JointState& parentState, JointState& state) {303void SkeletonModel::maybeUpdateLeanRotation(const JointState& parentState, int index) {
301 if (!_owningAvatar->isMyAvatar()) {304 if (!_owningAvatar->isMyAvatar()) {
302 return;305 return;
303 }306 }
@@ -305,24 +308,24 @@ void SkeletonModel::maybeUpdateLeanRotation(const JointState& parentState, Joint · 1 unmodified line
305 glm::vec3 xAxis(1.0f, 0.0f, 0.0f);308 glm::vec3 xAxis(1.0f, 0.0f, 0.0f);
306 glm::vec3 yAxis(0.0f, 1.0f, 0.0f);309 glm::vec3 yAxis(0.0f, 1.0f, 0.0f);
307 glm::vec3 zAxis(0.0f, 0.0f, 1.0f);310 glm::vec3 zAxis(0.0f, 0.0f, 1.0f);
308 glm::quat inverse = glm::inverse(parentState.getRotation() * state.getDefaultRotationInParentFrame());311 glm::quat inverse = glm::inverse(parentState.getRotation() * _rig->getJointDefaultRotationInParentFrame(index));
309 state.setRotationInConstrainedFrame(312 _rig->setJointRotationInConstrainedFrame(index,
310 glm::angleAxis(- RADIANS_PER_DEGREE * _owningAvatar->getHead()->getFinalLeanSideways(), inverse * zAxis) 313 glm::angleAxis(- RADIANS_PER_DEGREE * _owningAvatar->getHead()->getFinalLeanSideways(), inverse * zAxis)
311 * glm::angleAxis(- RADIANS_PER_DEGREE * _owningAvatar->getHead()->getFinalLeanForward(), inverse * xAxis)314 * glm::angleAxis(- RADIANS_PER_DEGREE * _owningAvatar->getHead()->getFinalLeanForward(), inverse * xAxis)
312 * glm::angleAxis(RADIANS_PER_DEGREE * _owningAvatar->getHead()->getTorsoTwist(), inverse * yAxis)315 * glm::angleAxis(RADIANS_PER_DEGREE * _owningAvatar->getHead()->getTorsoTwist(), inverse * yAxis)
313 * state.getFBXJoint().rotation, LEAN_PRIORITY);316 * _rig->getJointState(index).getFBXJoint().rotation, LEAN_PRIORITY);
314}317}
315 318
316void SkeletonModel::maybeUpdateNeckRotation(const JointState& parentState, const FBXJoint& joint, JointState& state) {319void SkeletonModel::maybeUpdateNeckRotation(const JointState& parentState, const FBXJoint& joint, int index) {
317 _owningAvatar->getHead()->getFaceModel().maybeUpdateNeckRotation(parentState, joint, state);320 _owningAvatar->getHead()->getFaceModel().maybeUpdateNeckRotation(parentState, joint, index);
318}321}
319 322
320void SkeletonModel::maybeUpdateEyeRotation(const JointState& parentState, const FBXJoint& joint, JointState& state) {323void SkeletonModel::maybeUpdateEyeRotation(const JointState& parentState, const FBXJoint& joint, int index) {
321 _owningAvatar->getHead()->getFaceModel().maybeUpdateEyeRotation(this, parentState, joint, state);324 _owningAvatar->getHead()->getFaceModel().maybeUpdateEyeRotation(this, parentState, joint, index);
322}325}
323 326
324void SkeletonModel::renderJointConstraints(gpu::Batch& batch, int jointIndex) {327void SkeletonModel::renderJointConstraints(gpu::Batch& batch, int jointIndex) {
325 if (jointIndex == -1 || jointIndex >= _jointStates.size()) {328 if (jointIndex == -1 || jointIndex >= _rig->getJointStateCount()) {
326 return;329 return;
327 }330 }
328 const FBXGeometry& geometry = _geometry->getFBXGeometry();331 const FBXGeometry& geometry = _geometry->getFBXGeometry();
@@ -331,9 +334,11 @@ void SkeletonModel::renderJointConstraints(gpu::Batch& batch, int jointIndex) { · 2 unmodified lines
331 batch._glLineWidth(3.0f);334 batch._glLineWidth(3.0f);
332 do {335 do {
333 const FBXJoint& joint = geometry.joints.at(jointIndex);336 const FBXJoint& joint = geometry.joints.at(jointIndex);
334 const JointState& jointState = _jointStates.at(jointIndex);337 const JointState& jointState = _rig->getJointState(jointIndex);
335 glm::vec3 position = _rotation * jointState.getPosition() + _translation;338 glm::vec3 position = _rotation * jointState.getPosition() + _translation;
336 glm::quat parentRotation = (joint.parentIndex == -1) ? _rotation : _rotation * _jointStates.at(joint.parentIndex).getRotation();339 glm::quat parentRotation = (joint.parentIndex == -1) ?
340 _rotation :
341 _rotation * _rig->getJointState(joint.parentIndex).getRotation();
337 float fanScale = directionSize * 0.75f;342 float fanScale = directionSize * 0.75f;
338 343
339 Transform transform = Transform();344 Transform transform = Transform();
@@ -464,14 +469,11 @@ void SkeletonModel::setHandPosition(int jointIndex, const glm::vec3& position, c · 124 unmodified lines
464 glm::vec3 forwardVector(rightHand ? -1.0f : 1.0f, 0.0f, 0.0f);469 glm::vec3 forwardVector(rightHand ? -1.0f : 1.0f, 0.0f, 0.0f);
465 glm::quat shoulderRotation = rotationBetween(forwardVector, elbowPosition - shoulderPosition);470 glm::quat shoulderRotation = rotationBetween(forwardVector, elbowPosition - shoulderPosition);
466 471
467 JointState& shoulderState = _jointStates[shoulderJointIndex];472 _rig->setJointRotationInBindFrame(shoulderJointIndex, shoulderRotation, PALM_PRIORITY);
468 shoulderState.setRotationInBindFrame(shoulderRotation, PALM_PRIORITY);473 _rig->setJointRotationInBindFrame(elbowJointIndex,
469 474 rotationBetween(shoulderRotation * forwardVector, wristPosition - elbowPosition) *
470 JointState& elbowState = _jointStates[elbowJointIndex];475 shoulderRotation, PALM_PRIORITY);
471 elbowState.setRotationInBindFrame(rotationBetween(shoulderRotation * forwardVector, wristPosition - elbowPosition) * shoulderRotation, PALM_PRIORITY);476 _rig->setJointRotationInBindFrame(jointIndex, rotation, PALM_PRIORITY);
472
473 JointState& handState = _jointStates[jointIndex];
474 handState.setRotationInBindFrame(rotation, PALM_PRIORITY);
475}477}
476 478
477bool SkeletonModel::getLeftHandPosition(glm::vec3& position) const {479bool SkeletonModel::getLeftHandPosition(glm::vec3& position) const {
@@ -526,7 +528,7 @@ bool SkeletonModel::getNeckParentRotationFromDefaultOrientation(glm::quat& neckP · 48 unmodified lines
526 glm::quat worldFrameRotation;528 glm::quat worldFrameRotation;
527 bool success = getJointRotationInWorldFrame(parentIndex, worldFrameRotation);529 bool success = getJointRotationInWorldFrame(parentIndex, worldFrameRotation);
528 if (success) {530 if (success) {
529 neckParentRotation = worldFrameRotation * _jointStates[parentIndex].getFBXJoint().inverseDefaultRotation;531 neckParentRotation = worldFrameRotation * _rig->getJointState(parentIndex).getFBXJoint().inverseDefaultRotation;
530 }532 }
531 return success;533 return success;
532}534}
@@ -636,7 +638,7 @@ float VERY_BIG_MASS = 1.0e6f; · 103 unmodified lines
636 638
637// virtual639// virtual
638void SkeletonModel::buildShapes() {640void SkeletonModel::buildShapes() {
639 if (_geometry == NULL || _jointStates.isEmpty()) {641 if (_geometry == NULL || _rig->jointStatesEmpty()) {
640 return;642 return;
641 }643 }
642 644
@@ -647,9 +649,8 @@ void SkeletonModel::buildShapes() { · 4 unmodified lines
647 }649 }
648 650
649 float uniformScale = extractUniformScale(_scale);651 float uniformScale = extractUniformScale(_scale);
650 const int numStates = _jointStates.size();652 for (int i = 0; i < _rig->getJointStateCount(); i++) {
651 for (int i = 0; i < numStates; i++) {653 const JointState& state = _rig->getJointState(i);
652 JointState& state = _jointStates[i];
653 const FBXJoint& joint = state.getFBXJoint();654 const FBXJoint& joint = state.getFBXJoint();
654 float radius = uniformScale * joint.boneRadius;655 float radius = uniformScale * joint.boneRadius;
655 float halfHeight = 0.5f * uniformScale * joint.distanceToParent;656 float halfHeight = 0.5f * uniformScale * joint.distanceToParent;
@@ -683,7 +684,7 @@ void SkeletonModel::buildShapes() { · 27 unmodified lines
683 684
684void SkeletonModel::computeBoundingShape(const FBXGeometry& geometry) {685void SkeletonModel::computeBoundingShape(const FBXGeometry& geometry) {
685 // compute default joint transforms686 // compute default joint transforms
686 int numStates = _jointStates.size();687 int numStates = _rig->getJointStateCount();
687 assert(numStates == _shapes.size());688 assert(numStates == _shapes.size());
688 QVector<glm::mat4> transforms;689 QVector<glm::mat4> transforms;
689 transforms.fill(glm::mat4(), numStates);690 transforms.fill(glm::mat4(), numStates);
@@ -694,11 +695,11 @@ void SkeletonModel::computeBoundingShape(const FBXGeometry& geometry) { · 4 unmodified lines
694 totalExtents.addPoint(glm::vec3(0.0f));695 totalExtents.addPoint(glm::vec3(0.0f));
695 for (int i = 0; i < numStates; i++) {696 for (int i = 0; i < numStates; i++) {
696 // compute the default transform of this joint697 // compute the default transform of this joint
697 JointState& state = _jointStates[i];698 const JointState& state = _rig->getJointState(i);
698 const FBXJoint& joint = state.getFBXJoint();699 const FBXJoint& joint = state.getFBXJoint();
699 int parentIndex = joint.parentIndex;700 int parentIndex = joint.parentIndex;
700 if (parentIndex == -1) {701 if (parentIndex == -1) {
701 transforms[i] = _jointStates[i].getTransform();702 transforms[i] = _rig->getJointTransform(i);
702 } else {703 } else {
703 glm::quat modifiedRotation = joint.preRotation * joint.rotation * joint.postRotation; 704 glm::quat modifiedRotation = joint.preRotation * joint.rotation * joint.postRotation;
704 transforms[i] = transforms[parentIndex] * glm::translate(joint.translation) 705 transforms[i] = transforms[parentIndex] * glm::translate(joint.translation)
@@ -748,7 +749,7 @@ void SkeletonModel::computeBoundingShape(const FBXGeometry& geometry) { · 43 unmodified lines
748 _boundingShape.setRadius(capsuleRadius);749 _boundingShape.setRadius(capsuleRadius);
749 _boundingShape.setHalfHeight(0.5f * diagonal.y - capsuleRadius);750 _boundingShape.setHalfHeight(0.5f * diagonal.y - capsuleRadius);
750 751
751 glm::vec3 rootPosition = _jointStates[geometry.rootJointIndex].getPosition();752 glm::vec3 rootPosition = _rig->getJointState(geometry.rootJointIndex).getPosition();
752 _boundingShapeLocalOffset = 0.5f * (totalExtents.maximum + totalExtents.minimum) - rootPosition;753 _boundingShapeLocalOffset = 0.5f * (totalExtents.maximum + totalExtents.minimum) - rootPosition;
753 _boundingRadius = 0.5f * glm::length(diagonal);754 _boundingRadius = 0.5f * glm::length(diagonal);
754}755}
@@ -853,7 +854,7 @@ void SkeletonModel::cauterizeHead() { · 98 unmodified lines
853 if (isActive()) {854 if (isActive()) {
854 const FBXGeometry& geometry = _geometry->getFBXGeometry();855 const FBXGeometry& geometry = _geometry->getFBXGeometry();
855 const int neckJointIndex = geometry.neckJointIndex;856 const int neckJointIndex = geometry.neckJointIndex;
856 if (neckJointIndex > 0 && neckJointIndex < _jointStates.size()) {857 if (neckJointIndex > 0 && neckJointIndex < _rig->getJointStateCount()) {
857 858
858 // lazy init of headBones859 // lazy init of headBones
859 if (_headBones.size() == 0) {860 if (_headBones.size() == 0) {
@@ -861,13 +862,13 @@ void SkeletonModel::cauterizeHead() { · 1 unmodified line
861 }862 }
862 863
863 // preserve the translation for the neck864 // preserve the translation for the neck
864 glm::vec4 trans = _jointStates[neckJointIndex].getTransform()[3];865 // glm::vec4 trans = _jointStates[neckJointIndex].getTransform()[3];
866 glm::vec4 trans = _rig->getJointTransform(neckJointIndex)[3];
865 glm::vec4 zero(0, 0, 0, 0);867 glm::vec4 zero(0, 0, 0, 0);
866 for (const int &i : _headBones) {868 for (const int &i : _headBones) {
867 JointState& joint = _jointStates[i];
868 glm::mat4 newXform(zero, zero, zero, trans);869 glm::mat4 newXform(zero, zero, zero, trans);
869 joint.setTransform(newXform);870 _rig->setJointTransform(i, newXform);
870 joint.setVisibleTransform(newXform);871 _rig->setJointVisibleTransform(i, newXform);
871 }872 }
872 }873 }
873 }874 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t