History

Update users of NetworkGeometry

Zach Pomerantz committed 10 years ago

Commit 3e9e0831 parent 7f05e4414 files · +159 −205Browse files

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

Unified · Split

libraries/render-utils/src/Model.cpp+55−76
@@ -30,7 +30,7 @@ · 29 unmodified lines
3030 using namespace std;
3131
3232 static int nakedModelPointerTypeId = qRegisterMetaType<ModelPointer>();
33−static int weakNetworkGeometryPointerTypeId = qRegisterMetaType<QWeakPointer<NetworkGeometry> >();
33+static int weakNetworkGeometryPointerTypeId = qRegisterMetaType<std::weak_ptr<NetworkGeometry> >();
3434 static int vec3VectorTypeId = qRegisterMetaType<QVector<glm::vec3> >();
3535 float Model::FAKE_DIMENSION_PLACEHOLDER = -1.0f;
3636 #define HTTP_INVALID_COM "http://invalid.com"
@@ -74,12 +74,12 @@ Model::~Model() { · 37 unmodified lines
7474
7575 AbstractViewStateInterface* Model::_viewState = NULL;
7676
77−bool Model::needsFixupInScene() {
77+bool Model::needsFixupInScene() const {
7878 if (readyToAddToScene()) {
7979 // Once textures are loaded, fixup if they are now transparent
80− if (!_needsReload && _needsUpdateTransparentTextures && _geometry->isLoadedWithTextures()) {
80+ if (_needsUpdateTransparentTextures && _geometry->getGeometry()->areTexturesLoaded()) {
8181 _needsUpdateTransparentTextures = false;
82− if (_hasTransparentTextures != _geometry->hasTransparentTextures()) {
82+ if (_hasTransparentTextures != _geometry->getGeometry()->hasTransparentTextures()) {
8383 return true;
8484 }
8585 }
@@ -150,19 +150,18 @@ void Model::enqueueLocationChange() { · 64 unmodified lines
150150 }
151151
152152 void Model::initJointTransforms() {
153− if (!_geometry || !_geometry->isLoaded()) {
154− return;
153+ if (isLoaded()) {
154+ glm::mat4 modelOffset = glm::scale(_scale) * glm::translate(_offset);
155+ _rig->setModelOffset(modelOffset);
155156 }
156− glm::mat4 modelOffset = glm::scale(_scale) * glm::translate(_offset);
157− _rig->setModelOffset(modelOffset);
158157 }
159158
160159 void Model::init() {
161160 }
162161
163162 void Model::reset() {
164− if (_geometry && _geometry->isLoaded()) {
165− const FBXGeometry& geometry = _geometry->getFBXGeometry();
163+ if (isLoaded()) {
164+ const FBXGeometry& geometry = getFBXGeometry();
166165 _rig->reset(geometry);
167166 }
168167 }
@@ -171,17 +170,16 @@ bool Model::updateGeometry() { · 2 unmodified lines
171170 PROFILE_RANGE(__FUNCTION__);
172171 bool needFullUpdate = false;
173172
174− if (!_geometry || !_geometry->isLoaded()) {
175− // geometry is not ready
173+ if (!isLoaded()) {
176174 return false;
177175 }
178176
179177 _needsReload = false;
180178
181− if (_rig->jointStatesEmpty() && _geometry->getFBXGeometry().joints.size() > 0) {
179+ if (_rig->jointStatesEmpty() && getFBXGeometry().joints.size() > 0) {
182180 initJointStates();
183181
184− const FBXGeometry& fbxGeometry = _geometry->getFBXGeometry();
182+ const FBXGeometry& fbxGeometry = getFBXGeometry();
185183 foreach (const FBXMesh& mesh, fbxGeometry.meshes) {
186184 MeshState state;
187185 state.clusterMatrices.resize(mesh.clusters.size());
@@ -205,7 +203,7 @@ bool Model::updateGeometry() { · 17 unmodified lines
205203
206204 // virtual
207205 void Model::initJointStates() {
208− const FBXGeometry& geometry = _geometry->getFBXGeometry();
206+ const FBXGeometry& geometry = getFBXGeometry();
209207 glm::mat4 modelOffset = glm::scale(_scale) * glm::translate(_offset);
210208
211209 _rig->initJointStates(geometry, modelOffset);
@@ -248,7 +246,7 @@ bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const g · 36 unmodified lines
248246 glm::vec3 subMeshSurfaceNormal;
249247 int subMeshIndex = 0;
250248
251− const FBXGeometry& geometry = _geometry->getFBXGeometry();
249+ const FBXGeometry& geometry = getFBXGeometry();
252250
253251 // If we hit the models box, then consider the submeshes...
254252 _mutex.lock();
@@ -367,7 +365,7 @@ void Model::recalculateMeshBoxes(bool pickAgainstTriangles) { · 112 unmodified lines
367365 bool calculatedMeshTrianglesNeeded = pickAgainstTriangles && !_calculatedMeshTrianglesValid;
368366
369367 if (!_calculatedMeshBoxesValid || calculatedMeshTrianglesNeeded || (!_calculatedMeshPartBoxesValid && pickAgainstTriangles) ) {
370− const FBXGeometry& geometry = _geometry->getFBXGeometry();
368+ const FBXGeometry& geometry = getFBXGeometry();
371369 int numberOfMeshes = geometry.meshes.size();
372370 _calculatedMeshBoxes.resize(numberOfMeshes);
373371 _calculatedMeshTriangles.clear();
@@ -478,7 +476,7 @@ void Model::recalculateMeshBoxes(bool pickAgainstTriangles) { · 104 unmodified lines
478476
479477 void Model::renderSetup(RenderArgs* args) {
480478 // set up dilated textures on first render after load/simulate
481− const FBXGeometry& geometry = _geometry->getFBXGeometry();
479+ const FBXGeometry& geometry = getFBXGeometry();
482480 if (_dilatedTextures.isEmpty()) {
483481 foreach (const FBXMesh& mesh, geometry.meshes) {
484482 QVector<QSharedPointer<Texture> > dilated;
@@ -627,7 +625,7 @@ Extents Model::getBindExtents() const { · 142 unmodified lines
627625 if (!isActive()) {
628626 return Extents();
629627 }
630− const Extents& bindExtents = _geometry->getFBXGeometry().bindExtents;
628+ const Extents& bindExtents = getFBXGeometry().bindExtents;
631629 Extents scaledExtents = { bindExtents.minimum * _scale, bindExtents.maximum * _scale };
632630 return scaledExtents;
633631 }
@@ -636,12 +634,12 @@ Extents Model::getMeshExtents() const { · 2 unmodified lines
636634 if (!isActive()) {
637635 return Extents();
638636 }
639− const Extents& extents = _geometry->getFBXGeometry().meshExtents;
637+ const Extents& extents = getFBXGeometry().meshExtents;
640638
641639 // even though our caller asked for "unscaled" we need to include any fst scaling, translation, and rotation, which
642640 // is captured in the offset matrix
643− glm::vec3 minimum = glm::vec3(_geometry->getFBXGeometry().offset * glm::vec4(extents.minimum, 1.0f));
644− glm::vec3 maximum = glm::vec3(_geometry->getFBXGeometry().offset * glm::vec4(extents.maximum, 1.0f));
641+ glm::vec3 minimum = glm::vec3(getFBXGeometry().offset * glm::vec4(extents.minimum, 1.0f));
642+ glm::vec3 maximum = glm::vec3(getFBXGeometry().offset * glm::vec4(extents.maximum, 1.0f));
645643 Extents scaledExtents = { minimum * _scale, maximum * _scale };
646644 return scaledExtents;
647645 }
@@ -651,12 +649,12 @@ Extents Model::getUnscaledMeshExtents() const { · 3 unmodified lines
651649 return Extents();
652650 }
653651
654− const Extents& extents = _geometry->getFBXGeometry().meshExtents;
652+ const Extents& extents = getFBXGeometry().meshExtents;
655653
656654 // even though our caller asked for "unscaled" we need to include any fst scaling, translation, and rotation, which
657655 // is captured in the offset matrix
658− glm::vec3 minimum = glm::vec3(_geometry->getFBXGeometry().offset * glm::vec4(extents.minimum, 1.0f));
659− glm::vec3 maximum = glm::vec3(_geometry->getFBXGeometry().offset * glm::vec4(extents.maximum, 1.0f));
656+ glm::vec3 minimum = glm::vec3(getFBXGeometry().offset * glm::vec4(extents.minimum, 1.0f));
657+ glm::vec3 maximum = glm::vec3(getFBXGeometry().offset * glm::vec4(extents.maximum, 1.0f));
660658 Extents scaledExtents = { minimum, maximum };
661659
662660 return scaledExtents;
@@ -665,8 +663,8 @@ Extents Model::getUnscaledMeshExtents() const { · 2 unmodified lines
665663 Extents Model::calculateScaledOffsetExtents(const Extents& extents,
666664 glm::vec3 modelPosition, glm::quat modelOrientation) const {
667665 // we need to include any fst scaling, translation, and rotation, which is captured in the offset matrix
668− glm::vec3 minimum = glm::vec3(_geometry->getFBXGeometry().offset * glm::vec4(extents.minimum, 1.0f));
669− glm::vec3 maximum = glm::vec3(_geometry->getFBXGeometry().offset * glm::vec4(extents.maximum, 1.0f));
666+ glm::vec3 minimum = glm::vec3(getFBXGeometry().offset * glm::vec4(extents.minimum, 1.0f));
667+ glm::vec3 maximum = glm::vec3(getFBXGeometry().offset * glm::vec4(extents.maximum, 1.0f));
670668
671669 Extents scaledOffsetExtents = { ((minimum + _offset) * _scale),
672670 ((maximum + _offset) * _scale) };
@@ -686,7 +684,7 @@ AABox Model::calculateScaledOffsetAABox(const AABox& box, glm::vec3 modelPositio · 13 unmodified lines
686684
687685 glm::vec3 Model::calculateScaledOffsetPoint(const glm::vec3& point) const {
688686 // we need to include any fst scaling, translation, and rotation, which is captured in the offset matrix
689− glm::vec3 offsetPoint = glm::vec3(_geometry->getFBXGeometry().offset * glm::vec4(point, 1.0f));
687+ glm::vec3 offsetPoint = glm::vec3(getFBXGeometry().offset * glm::vec4(point, 1.0f));
690688 glm::vec3 scaledPoint = ((offsetPoint + _offset) * _scale);
691689 glm::vec3 rotatedPoint = _rotation * scaledPoint;
692690 glm::vec3 translatedPoint = rotatedPoint + _translation;
@@ -714,11 +712,11 @@ void Model::setJointTranslation(int index, bool valid, const glm::vec3& translat · 21 unmodified lines
714712 }
715713
716714 int Model::getParentJointIndex(int jointIndex) const {
717− return (isActive() && jointIndex != -1) ? _geometry->getFBXGeometry().joints.at(jointIndex).parentIndex : -1;
715+ return (isActive() && jointIndex != -1) ? getFBXGeometry().joints.at(jointIndex).parentIndex : -1;
718716 }
719717
720718 int Model::getLastFreeJointIndex(int jointIndex) const {
721− return (isActive() && jointIndex != -1) ? _geometry->getFBXGeometry().joints.at(jointIndex).freeLineage.last() : -1;
719+ return (isActive() && jointIndex != -1) ? getFBXGeometry().joints.at(jointIndex).freeLineage.last() : -1;
722720 }
723721
724722 void Model::setURL(const QUrl& url) {
@@ -743,29 +741,16 @@ void Model::setURL(const QUrl& url) { · 18 unmodified lines
743741 invalidCalculatedMeshBoxes();
744742 deleteGeometry();
745743
746− _geometry.reset(new NetworkGeometry(url, false, QVariantHash()));
744+ _geometry = DependencyManager::get<ModelCache>()->getGeometry(url);
747745 onInvalidate();
748746 }
749747
750−const QSharedPointer<NetworkGeometry> Model::getCollisionGeometry(bool delayLoad)
751−{
752− if (_collisionGeometry.isNull() && !_collisionUrl.isEmpty()) {
753− _collisionGeometry.reset(new NetworkGeometry(_collisionUrl, delayLoad, QVariantHash()));
754− }
755−
756− if (_collisionGeometry && _collisionGeometry->isLoaded()) {
757− return _collisionGeometry;
758− }
759−
760− return QSharedPointer<NetworkGeometry>();
761−}
762−
763748 void Model::setCollisionModelURL(const QUrl& url) {
764749 if (_collisionUrl == url) {
765750 return;
766751 }
767752 _collisionUrl = url;
768− _collisionGeometry.reset(new NetworkGeometry(url, false, QVariantHash()));
753+ _collisionGeometry = DependencyManager::get<ModelCache>()->getGeometry(url);
769754 }
770755
771756 bool Model::getJointPositionInWorldFrame(int jointIndex, glm::vec3& position) const {
@@ -815,13 +800,13 @@ QStringList Model::getJointNames() const { · 43 unmodified lines
815800 Q_RETURN_ARG(QStringList, result));
816801 return result;
817802 }
818− return isActive() ? _geometry->getFBXGeometry().getJointNames() : QStringList();
803+ return isActive() ? getFBXGeometry().getJointNames() : QStringList();
819804 }
820805
821806 class Blender : public QRunnable {
822807 public:
823808
824− Blender(ModelPointer model, int blendNumber, const QWeakPointer<NetworkGeometry>& geometry,
809+ Blender(ModelPointer model, int blendNumber, const std::weak_ptr<NetworkGeometry>& geometry,
825810 const QVector<FBXMesh>& meshes, const QVector<float>& blendshapeCoefficients);
826811
827812 virtual void run();
@@ -830,12 +815,12 @@ private: · 2 unmodified lines
830815
831816 ModelPointer _model;
832817 int _blendNumber;
833− QWeakPointer<NetworkGeometry> _geometry;
818+ std::weak_ptr<NetworkGeometry> _geometry;
834819 QVector<FBXMesh> _meshes;
835820 QVector<float> _blendshapeCoefficients;
836821 };
837822
838−Blender::Blender(ModelPointer model, int blendNumber, const QWeakPointer<NetworkGeometry>& geometry,
823+Blender::Blender(ModelPointer model, int blendNumber, const std::weak_ptr<NetworkGeometry>& geometry,
839824 const QVector<FBXMesh>& meshes, const QVector<float>& blendshapeCoefficients) :
840825 _model(model),
841826 _blendNumber(blendNumber),
@@ -878,7 +863,7 @@ void Blender::run() { · 36 unmodified lines
878863 // post the result to the geometry cache, which will dispatch to the model if still alive
879864 QMetaObject::invokeMethod(DependencyManager::get<ModelBlender>().data(), "setBlendedVertices",
880865 Q_ARG(ModelPointer, _model), Q_ARG(int, _blendNumber),
881− Q_ARG(const QWeakPointer<NetworkGeometry>&, _geometry), Q_ARG(const QVector<glm::vec3>&, vertices),
866+ Q_ARG(const std::weak_ptr<NetworkGeometry>&, _geometry), Q_ARG(const QVector<glm::vec3>&, vertices),
882867 Q_ARG(const QVector<glm::vec3>&, normals));
883868 }
884869
@@ -1010,7 +995,7 @@ void Model::updateClusterMatrices(glm::vec3 modelPosition, glm::quat modelOrient · 125 unmodified lines
1010995 return;
1011996 }
1012997 _needsUpdateClusterMatrices = false;
1013− const FBXGeometry& geometry = _geometry->getFBXGeometry();
998+ const FBXGeometry& geometry = getFBXGeometry();
1014999 glm::mat4 zeroScale(glm::vec4(0.0f, 0.0f, 0.0f, 0.0f),
10151000 glm::vec4(0.0f, 0.0f, 0.0f, 0.0f),
10161001 glm::vec4(0.0f, 0.0f, 0.0f, 0.0f),
@@ -1067,26 +1052,26 @@ void Model::updateClusterMatrices(glm::vec3 modelPosition, glm::quat modelOrient · 50 unmodified lines
10671052 }
10681053
10691054 void Model::inverseKinematics(int endIndex, glm::vec3 targetPosition, const glm::quat& targetRotation, float priority) {
1070− const FBXGeometry& geometry = _geometry->getFBXGeometry();
1055+ const FBXGeometry& geometry = getFBXGeometry();
10711056 const QVector<int>& freeLineage = geometry.joints.at(endIndex).freeLineage;
10721057 glm::mat4 parentTransform = glm::scale(_scale) * glm::translate(_offset);
10731058 _rig->inverseKinematics(endIndex, targetPosition, targetRotation, priority, freeLineage, parentTransform);
10741059 }
10751060
10761061 bool Model::restoreJointPosition(int jointIndex, float fraction, float priority) {
1077− const FBXGeometry& geometry = _geometry->getFBXGeometry();
1062+ const FBXGeometry& geometry = getFBXGeometry();
10781063 const QVector<int>& freeLineage = geometry.joints.at(jointIndex).freeLineage;
10791064 return _rig->restoreJointPosition(jointIndex, fraction, priority, freeLineage);
10801065 }
10811066
10821067 float Model::getLimbLength(int jointIndex) const {
1083− const FBXGeometry& geometry = _geometry->getFBXGeometry();
1068+ const FBXGeometry& geometry = getFBXGeometry();
10841069 const QVector<int>& freeLineage = geometry.joints.at(jointIndex).freeLineage;
10851070 return _rig->getLimbLength(jointIndex, freeLineage, _scale, geometry.joints);
10861071 }
10871072
10881073 bool Model::maybeStartBlender() {
1089− const FBXGeometry& fbxGeometry = _geometry->getFBXGeometry();
1074+ const FBXGeometry& fbxGeometry = getFBXGeometry();
10901075 if (fbxGeometry.hasBlendedMeshes()) {
10911076 QThreadPool::globalInstance()->start(new Blender(getThisPointer(), ++_blendNumber, _geometry,
10921077 fbxGeometry.meshes, _blendshapeCoefficients));
@@ -1095,13 +1080,13 @@ bool Model::maybeStartBlender() { · 2 unmodified lines
10951080 return false;
10961081 }
10971082
1098−void Model::setBlendedVertices(int blendNumber, const QWeakPointer<NetworkGeometry>& geometry,
1083+void Model::setBlendedVertices(int blendNumber, const std::weak_ptr<NetworkGeometry>& geometry,
10991084 const QVector<glm::vec3>& vertices, const QVector<glm::vec3>& normals) {
1100− if (_geometry != geometry || _blendedVertexBuffers.empty() || blendNumber < _appliedBlendNumber) {
1085+ if (_geometry != geometry.lock() || _blendedVertexBuffers.empty() || blendNumber < _appliedBlendNumber) {
11011086 return;
11021087 }
11031088 _appliedBlendNumber = blendNumber;
1104− const FBXGeometry& fbxGeometry = _geometry->getFBXGeometry();
1089+ const FBXGeometry& fbxGeometry = getFBXGeometry();
11051090 int index = 0;
11061091 for (int i = 0; i < fbxGeometry.meshes.size(); i++) {
11071092 const FBXMesh& mesh = fbxGeometry.meshes.at(i);
@@ -1118,13 +1103,6 @@ void Model::setBlendedVertices(int blendNumber, const QWeakPointer<NetworkGeomet · 10 unmodified lines
11181103 }
11191104 }
11201105
1121−void Model::setGeometry(const QSharedPointer<NetworkGeometry>& newGeometry) {
1122− if (_geometry == newGeometry) {
1123− return;
1124− }
1125− _geometry = newGeometry;
1126−}
1127−
11281106 void Model::deleteGeometry() {
11291107 _blendedVertexBuffers.clear();
11301108 _meshStates.clear();
@@ -1134,7 +1112,7 @@ void Model::deleteGeometry() { · 3 unmodified lines
11341112
11351113 AABox Model::getPartBounds(int meshIndex, int partIndex, glm::vec3 modelPosition, glm::quat modelOrientation) const {
11361114
1137− if (!_geometry || !_geometry->isLoaded()) {
1115+ if (!isLoaded()) {
11381116 return AABox();
11391117 }
11401118
@@ -1143,10 +1121,10 @@ AABox Model::getPartBounds(int meshIndex, int partIndex, glm::vec3 modelPosition · 2 unmodified lines
11431121 bool isSkinned = state.clusterMatrices.size() > 1;
11441122 if (isSkinned) {
11451123 // if we're skinned return the entire mesh extents because we can't know for sure our clusters don't move us
1146− return calculateScaledOffsetAABox(_geometry->getFBXGeometry().meshExtents, modelPosition, modelOrientation);
1124+ return calculateScaledOffsetAABox(getFBXGeometry().meshExtents, modelPosition, modelOrientation);
11471125 }
11481126 }
1149− if (_geometry->getFBXGeometry().meshes.size() > meshIndex) {
1127+ if (getFBXGeometry().meshes.size() > meshIndex) {
11501128
11511129 // FIX ME! - This is currently a hack because for some mesh parts our efforts to calculate the bounding
11521130 // box of the mesh part fails. It seems to create boxes that are not consistent with where the
@@ -1160,27 +1138,28 @@ AABox Model::getPartBounds(int meshIndex, int partIndex, glm::vec3 modelPosition · 7 unmodified lines
11601138 // return _calculatedMeshBoxes[meshIndex];
11611139 //
11621140 // If we not skinned use the bounds of the subMesh for all it's parts
1163− const FBXMesh& mesh = _geometry->getFBXGeometry().meshes.at(meshIndex);
1141+ const FBXMesh& mesh = getFBXGeometry().meshes.at(meshIndex);
11641142 return calculateScaledOffsetExtents(mesh.meshExtents, modelPosition, modelOrientation);
11651143 }
11661144 return AABox();
11671145 }
11681146
11691147 void Model::segregateMeshGroups() {
1170− QSharedPointer<NetworkGeometry> networkGeometry;
1148+ NetworkGeometry::Pointer networkGeometry;
11711149 bool showingCollisionHull = false;
11721150 if (_showCollisionHull && _collisionGeometry) {
1173− if (_collisionGeometry->isLoaded()) {
1151+ if (isCollisionLoaded()) {
11741152 networkGeometry = _collisionGeometry;
11751153 showingCollisionHull = true;
11761154 } else {
11771155 return;
11781156 }
11791157 } else {
1158+ assert(isLoaded());
11801159 networkGeometry = _geometry;
11811160 }
1182− const FBXGeometry& geometry = networkGeometry->getFBXGeometry();
1183− const std::vector<std::unique_ptr<NetworkMesh>>& networkMeshes = networkGeometry->getMeshes();
1161+ const FBXGeometry& geometry = networkGeometry->getGeometry()->getGeometry();
1162+ const auto& networkMeshes = networkGeometry->getGeometry()->getMeshes();
11841163
11851164 // all of our mesh vectors must match in size
11861165 auto geoMeshesSize = geometry.meshes.size();
@@ -1208,7 +1187,7 @@ void Model::segregateMeshGroups() { · 21 unmodified lines
12081187 int shapeID = 0;
12091188 for (int i = 0; i < (int)networkMeshes.size(); i++) {
12101189 const FBXMesh& mesh = geometry.meshes.at(i);
1211− const NetworkMesh& networkMesh = *(networkMeshes.at(i).get());
1190+ const auto& networkMesh = networkMeshes.at(i);
12121191
12131192 // Create the render payloads
12141193 int totalParts = mesh.parts.size();
@@ -1220,7 +1199,7 @@ void Model::segregateMeshGroups() { · 5 unmodified lines
12201199 _collisionHullMaterial->setMetallic(0.02f);
12211200 _collisionHullMaterial->setRoughness(0.5f);
12221201 }
1223− _renderItemsSet << std::make_shared<MeshPartPayload>(networkMesh._mesh, partIndex, _collisionHullMaterial, transform, offset);
1202+ _renderItemsSet << std::make_shared<MeshPartPayload>(networkMesh, partIndex, _collisionHullMaterial, transform, offset);
12241203 } else {
12251204 _renderItemsSet << std::make_shared<ModelMeshPartPayload>(this, i, partIndex, shapeID, transform, offset);
12261205 }
@@ -1285,7 +1264,7 @@ void ModelBlender::noteRequiresBlend(ModelPointer model) { · 58 unmodified lines
12851264 }
12861265
12871266 void ModelBlender::setBlendedVertices(ModelPointer model, int blendNumber,
1288− const QWeakPointer<NetworkGeometry>& geometry, const QVector<glm::vec3>& vertices, const QVector<glm::vec3>& normals) {
1267+ const std::weak_ptr<NetworkGeometry>& geometry, const QVector<glm::vec3>& vertices, const QVector<glm::vec3>& normals) {
12891268 if (model) {
12901269 model->setBlendedVertices(blendNumber, geometry, vertices, normals);
12911270 }