History

calculate the actual bounding box for the mesh parts

ZappoMan committed 11 years ago

Commit 1e2d2a21 parent 750d9c02 files · +34 −2Browse files

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

Unified · Split

libraries/render-utils/src/Model.cpp+32−2
@@ -81,6 +81,7 @@ Model::Model(QObject* parent) : · 80 unmodified lines
8181 _url("http://invalid.com"),
8282 _blendNumber(0),
8383 _appliedBlendNumber(0),
84+ _calculatedMeshPartBoxesValid(false),
8485 _calculatedMeshBoxesValid(false),
8586 _calculatedMeshTrianglesValid(false),
8687 _meshGroupsKnown(false),
@@ -666,12 +667,13 @@ bool Model::convexHullContains(glm::vec3 point) { · 579 unmodified lines
666667 void Model::recalculateMeshBoxes(bool pickAgainstTriangles) {
667668 bool calculatedMeshTrianglesNeeded = pickAgainstTriangles && !_calculatedMeshTrianglesValid;
668669
669− if (!_calculatedMeshBoxesValid || calculatedMeshTrianglesNeeded) {
670+ if (!_calculatedMeshBoxesValid || calculatedMeshTrianglesNeeded || (!_calculatedMeshPartBoxesValid && pickAgainstTriangles) ) {
670671 const FBXGeometry& geometry = _geometry->getFBXGeometry();
671672 int numberOfMeshes = geometry.meshes.size();
672673 _calculatedMeshBoxes.resize(numberOfMeshes);
673674 _calculatedMeshTriangles.clear();
674675 _calculatedMeshTriangles.resize(numberOfMeshes);
676+ _calculatedMeshPartBoxes.clear();
675677 for (int i = 0; i < numberOfMeshes; i++) {
676678 const FBXMesh& mesh = geometry.meshes.at(i);
677679 Extents scaledMeshExtents = calculateScaledOffsetExtents(mesh.meshExtents);
@@ -682,6 +684,9 @@ void Model::recalculateMeshBoxes(bool pickAgainstTriangles) { · 4 unmodified lines
682684 QVector<Triangle> thisMeshTriangles;
683685 for (int j = 0; j < mesh.parts.size(); j++) {
684686 const FBXMeshPart& part = mesh.parts.at(j);
687+
688+ bool atLeastOnePointInBounds = false;
689+ AABox thisPartBounds;
685690
686691 const int INDICES_PER_TRIANGLE = 3;
687692 const int INDICES_PER_QUAD = 4;
@@ -710,6 +715,15 @@ void Model::recalculateMeshBoxes(bool pickAgainstTriangles) { · 22 unmodified lines
710715
711716 thisMeshTriangles.push_back(tri1);
712717 thisMeshTriangles.push_back(tri2);
718+
719+ if (!atLeastOnePointInBounds) {
720+ thisPartBounds.setBox(v0, 0.0f);
721+ atLeastOnePointInBounds = true;
722+ }
723+ thisPartBounds += v0;
724+ thisPartBounds += v1;
725+ thisPartBounds += v2;
726+ thisPartBounds += v3;
713727 }
714728 }
715729
@@ -728,10 +742,20 @@ void Model::recalculateMeshBoxes(bool pickAgainstTriangles) { · 12 unmodified lines
728742 Triangle tri = { v0, v1, v2 };
729743
730744 thisMeshTriangles.push_back(tri);
745+
746+ if (!atLeastOnePointInBounds) {
747+ thisPartBounds.setBox(v0, 0.0f);
748+ atLeastOnePointInBounds = true;
749+ }
750+ thisPartBounds += v0;
751+ thisPartBounds += v1;
752+ thisPartBounds += v2;
731753 }
732754 }
755+ _calculatedMeshPartBoxes[QPair<int,int>(i, j)] = thisPartBounds;
733756 }
734757 _calculatedMeshTriangles[i] = thisMeshTriangles;
758+ _calculatedMeshPartBoxesValid = true;
735759 }
736760 }
737761 _calculatedMeshBoxesValid = true;
@@ -2176,7 +2200,13 @@ bool Model::renderInScene(float alpha, RenderArgs* args) { · 1438 unmodified lines
21762200 }
21772201
21782202 AABox Model::getPartBounds(int meshIndex, int partIndex) {
2179− if (_calculatedMeshBoxesValid) {
2203+ if (!_calculatedMeshPartBoxesValid) {
2204+ recalculateMeshBoxes(true);
2205+ }
2206+ if (_calculatedMeshPartBoxesValid && _calculatedMeshPartBoxes.contains(QPair<int,int>(meshIndex, partIndex))) {
2207+ return _calculatedMeshPartBoxes[QPair<int,int>(meshIndex, partIndex)];
2208+ }
2209+ if (!_calculatedMeshBoxesValid) {
21802210 return _calculatedMeshBoxes[meshIndex];
21812211 }
21822212 return AABox();