History

merge from upstream

Seth Alves committed 10 years ago

Commit b7ebe5b2 parents 3402585 6f50d9172 files · +2573 −4128Browse files

Showing the changes to libraries/render-utils/src/Model.cpp (1 of 72 files). Show all

Unified · Split

libraries/render-utils/src/Model.cpp+16−69
@@ -22,7 +22,6 @@ · 21 unmodified lines
22#include <ViewFrustum.h>22#include <ViewFrustum.h>
23 23
24#include "AbstractViewStateInterface.h"24#include "AbstractViewStateInterface.h"
25#include "AnimationHandle.h"
26#include "Model.h"25#include "Model.h"
27#include "MeshPartPayload.h"26#include "MeshPartPayload.h"
28 27
@@ -91,6 +90,8 @@ void Model::setScale(const glm::vec3& scale) { · 62 unmodified lines
91 _scaledToFit = false;90 _scaledToFit = false;
92}91}
93 92
93const float METERS_PER_MILLIMETER = 0.01f;
94
94void Model::setScaleInternal(const glm::vec3& scale) {95void Model::setScaleInternal(const glm::vec3& scale) {
95 if (glm::distance(_scale, scale) > METERS_PER_MILLIMETER) {96 if (glm::distance(_scale, scale) > METERS_PER_MILLIMETER) {
96 _scale = scale;97 _scale = scale;
@@ -120,24 +121,12 @@ void Model::enqueueLocationChange() { · 23 unmodified lines
120 scene->enqueuePendingChanges(pendingChanges);121 scene->enqueuePendingChanges(pendingChanges);
121}122}
122 123
123QVector<JointState> Model::createJointStates(const FBXGeometry& geometry) {
124 QVector<JointState> jointStates;
125 for (int i = 0; i < geometry.joints.size(); ++i) {
126 const FBXJoint& joint = geometry.joints[i];
127 // store a pointer to the FBXJoint in the JointState
128 JointState state(joint);
129 jointStates.append(state);
130 }
131 return jointStates;
132};
133
134void Model::initJointTransforms() {124void Model::initJointTransforms() {
135 if (!_geometry || !_geometry->isLoaded()) {125 if (!_geometry || !_geometry->isLoaded()) {
136 return;126 return;
137 }127 }
138 const FBXGeometry& geometry = _geometry->getFBXGeometry();128 glm::mat4 modelOffset = glm::scale(_scale) * glm::translate(_offset);
139 glm::mat4 parentTransform = glm::scale(_scale) * glm::translate(_offset) * geometry.offset;129 _rig->setModelOffset(modelOffset);
140 _rig->initJointTransforms(parentTransform);
141}130}
142 131
143void Model::init() {132void Model::init() {
@@ -146,14 +135,13 @@ void Model::init() { · 2 unmodified lines
146void Model::reset() {135void Model::reset() {
147 if (_geometry && _geometry->isLoaded()) {136 if (_geometry && _geometry->isLoaded()) {
148 const FBXGeometry& geometry = _geometry->getFBXGeometry();137 const FBXGeometry& geometry = _geometry->getFBXGeometry();
149 _rig->reset(geometry.joints);138 _rig->reset(geometry);
150 }139 }
151}140}
152 141
153bool Model::updateGeometry() {142bool Model::updateGeometry() {
154 PROFILE_RANGE(__FUNCTION__);143 PROFILE_RANGE(__FUNCTION__);
155 bool needFullUpdate = false;144 bool needFullUpdate = false;
156 bool needToRebuild = false;
157 145
158 if (!_geometry || !_geometry->isLoaded()) {146 if (!_geometry || !_geometry->isLoaded()) {
159 // geometry is not ready147 // geometry is not ready
@@ -162,17 +150,10 @@ bool Model::updateGeometry() { · 2 unmodified lines
162 150
163 _needsReload = false;151 _needsReload = false;
164 152
165 QSharedPointer<NetworkGeometry> geometry = _geometry;153 if (_rig->jointStatesEmpty() && _geometry->getFBXGeometry().joints.size() > 0) {
166 if (_rig->jointStatesEmpty()) {154 initJointStates();
167 const FBXGeometry& fbxGeometry = geometry->getFBXGeometry();
168 if (fbxGeometry.joints.size() > 0) {
169 initJointStates(createJointStates(fbxGeometry));
170 needToRebuild = true;
171 }
172 }
173 155
174 if (needToRebuild) {156 const FBXGeometry& fbxGeometry = _geometry->getFBXGeometry();
175 const FBXGeometry& fbxGeometry = geometry->getFBXGeometry();
176 foreach (const FBXMesh& mesh, fbxGeometry.meshes) {157 foreach (const FBXMesh& mesh, fbxGeometry.meshes) {
177 MeshState state;158 MeshState state;
178 state.clusterMatrices.resize(mesh.clusters.size());159 state.clusterMatrices.resize(mesh.clusters.size());
@@ -195,26 +176,11 @@ bool Model::updateGeometry() { · 16 unmodified lines
195}176}
196 177
197// virtual178// virtual
198void Model::initJointStates(QVector<JointState> states) {179void Model::initJointStates() {
199 const FBXGeometry& geometry = _geometry->getFBXGeometry();180 const FBXGeometry& geometry = _geometry->getFBXGeometry();
200 glm::mat4 parentTransform = glm::scale(_scale) * glm::translate(_offset) * geometry.offset;181 glm::mat4 modelOffset = glm::scale(_scale) * glm::translate(_offset);
201 182
202 int rootJointIndex = geometry.rootJointIndex;183 _rig->initJointStates(geometry, modelOffset);
203 int leftHandJointIndex = geometry.leftHandJointIndex;
204 int leftElbowJointIndex = leftHandJointIndex >= 0 ? geometry.joints.at(leftHandJointIndex).parentIndex : -1;
205 int leftShoulderJointIndex = leftElbowJointIndex >= 0 ? geometry.joints.at(leftElbowJointIndex).parentIndex : -1;
206 int rightHandJointIndex = geometry.rightHandJointIndex;
207 int rightElbowJointIndex = rightHandJointIndex >= 0 ? geometry.joints.at(rightHandJointIndex).parentIndex : -1;
208 int rightShoulderJointIndex = rightElbowJointIndex >= 0 ? geometry.joints.at(rightElbowJointIndex).parentIndex : -1;
209
210 _rig->initJointStates(states, parentTransform,
211 rootJointIndex,
212 leftHandJointIndex,
213 leftElbowJointIndex,
214 leftShoulderJointIndex,
215 rightHandJointIndex,
216 rightElbowJointIndex,
217 rightShoulderJointIndex);
218}184}
219 185
220bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const glm::vec3& direction, float& distance,186bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const glm::vec3& direction, float& distance,
@@ -856,6 +822,7 @@ void Blender::run() { · 635 unmodified lines
856 const float NORMAL_COEFFICIENT_SCALE = 0.01f;822 const float NORMAL_COEFFICIENT_SCALE = 0.01f;
857 for (int i = 0, n = qMin(_blendshapeCoefficients.size(), mesh.blendshapes.size()); i < n; i++) {823 for (int i = 0, n = qMin(_blendshapeCoefficients.size(), mesh.blendshapes.size()); i < n; i++) {
858 float vertexCoefficient = _blendshapeCoefficients.at(i);824 float vertexCoefficient = _blendshapeCoefficients.at(i);
825 const float EPSILON = 0.0001f;
859 if (vertexCoefficient < EPSILON) {826 if (vertexCoefficient < EPSILON) {
860 continue;827 continue;
861 }828 }
@@ -983,9 +950,7 @@ void Model::updateRig(float deltaTime, glm::mat4 parentTransform) { · 121 unmodified lines
983}950}
984void Model::simulateInternal(float deltaTime) {951void Model::simulateInternal(float deltaTime) {
985 // update the world space transforms for all joints952 // update the world space transforms for all joints
986 953 glm::mat4 parentTransform = glm::scale(_scale) * glm::translate(_offset);
987 const FBXGeometry& geometry = _geometry->getFBXGeometry();
988 glm::mat4 parentTransform = glm::scale(_scale) * glm::translate(_offset) * geometry.offset;
989 updateRig(deltaTime, parentTransform);954 updateRig(deltaTime, parentTransform);
990}955}
991void Model::updateClusterMatrices(glm::vec3 modelPosition, glm::quat modelOrientation) {956void Model::updateClusterMatrices(glm::vec3 modelPosition, glm::quat modelOrientation) {
@@ -1051,22 +1016,10 @@ void Model::updateClusterMatrices(glm::vec3 modelPosition, glm::quat modelOrient · 59 unmodified lines
1051 }1016 }
1052}1017}
1053 1018
1054bool Model::setJointPosition(int jointIndex, const glm::vec3& position, const glm::quat& rotation, bool useRotation,
1055 int lastFreeIndex, bool allIntermediatesFree, const glm::vec3& alignment, float priority) {
1056 const FBXGeometry& geometry = _geometry->getFBXGeometry();
1057 const QVector<int>& freeLineage = geometry.joints.at(jointIndex).freeLineage;
1058 glm::mat4 parentTransform = glm::scale(_scale) * glm::translate(_offset) * geometry.offset;
1059 if (_rig->setJointPosition(jointIndex, position, rotation, useRotation,
1060 lastFreeIndex, allIntermediatesFree, alignment, priority, freeLineage, parentTransform)) {
1061 return true;
1062 }
1063 return false;
1064}
1065
1066void Model::inverseKinematics(int endIndex, glm::vec3 targetPosition, const glm::quat& targetRotation, float priority) {1019void Model::inverseKinematics(int endIndex, glm::vec3 targetPosition, const glm::quat& targetRotation, float priority) {
1067 const FBXGeometry& geometry = _geometry->getFBXGeometry();1020 const FBXGeometry& geometry = _geometry->getFBXGeometry();
1068 const QVector<int>& freeLineage = geometry.joints.at(endIndex).freeLineage;1021 const QVector<int>& freeLineage = geometry.joints.at(endIndex).freeLineage;
1069 glm::mat4 parentTransform = glm::scale(_scale) * glm::translate(_offset) * geometry.offset;1022 glm::mat4 parentTransform = glm::scale(_scale) * glm::translate(_offset);
1070 _rig->inverseKinematics(endIndex, targetPosition, targetRotation, priority, freeLineage, parentTransform);1023 _rig->inverseKinematics(endIndex, targetPosition, targetRotation, priority, freeLineage, parentTransform);
1071}1024}
1072 1025
@@ -1125,11 +1078,7 @@ void Model::setGeometry(const QSharedPointer<NetworkGeometry>& newGeometry) { · 52 unmodified lines
1125void Model::deleteGeometry() {1078void Model::deleteGeometry() {
1126 _blendedVertexBuffers.clear();1079 _blendedVertexBuffers.clear();
1127 _meshStates.clear();1080 _meshStates.clear();
1128 if (_rig) {1081 _rig->destroyAnimGraph();
1129 _rig->clearJointStates();
1130 _rig->deleteAnimations();
1131 _rig->destroyAnimGraph();
1132 }
1133 _blendedBlendshapeCoefficients.clear();1082 _blendedBlendshapeCoefficients.clear();
1134}1083}
1135 1084
@@ -1177,8 +1126,6 @@ void Model::segregateMeshGroups() { · 41 unmodified lines
1177 const FBXGeometry& geometry = networkGeometry->getFBXGeometry();1126 const FBXGeometry& geometry = networkGeometry->getFBXGeometry();
1178 const std::vector<std::unique_ptr<NetworkMesh>>& networkMeshes = networkGeometry->getMeshes();1127 const std::vector<std::unique_ptr<NetworkMesh>>& networkMeshes = networkGeometry->getMeshes();
1179 1128
1180 _rig->makeAnimSkeleton(geometry);
1181
1182 // all of our mesh vectors must match in size1129 // all of our mesh vectors must match in size
1183 if ((int)networkMeshes.size() != geometry.meshes.size() ||1130 if ((int)networkMeshes.size() != geometry.meshes.size() ||
1184 geometry.meshes.size() != _meshStates.size()) {1131 geometry.meshes.size() != _meshStates.size()) {