History

IMproving the JOb interface

samcake committed 11 years ago

Commit cc65ad32 parents d1ac314 0ef75da25 files · +749 −371Browse files

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

Unified · Split

libraries/render-utils/src/Model.cpp+38−31
@@ -82,6 +82,7 @@ Model::Model(QObject* parent) : · 81 unmodified lines
8282 _isVisible(true),
8383 _blendNumber(0),
8484 _appliedBlendNumber(0),
85+ _calculatedMeshPartOffsetValid(false),
8586 _calculatedMeshPartBoxesValid(false),
8687 _calculatedMeshBoxesValid(false),
8788 _calculatedMeshTrianglesValid(false),
@@ -544,6 +545,10 @@ bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const g · 456 unmodified lines
544545 // we can use the AABox's ray intersection by mapping our origin and direction into the model frame
545546 // and testing intersection there.
546547 if (modelFrameBox.findRayIntersection(modelFrameOrigin, modelFrameDirection, distance, face)) {
548+
549+ if (!_calculatedMeshBoxesValid) {
550+ recalculateMeshBoxes(pickAgainstTriangles);
551+ }
547552
548553 float bestDistance = std::numeric_limits<float>::max();
549554
@@ -660,6 +665,25 @@ bool Model::convexHullContains(glm::vec3 point) { · 110 unmodified lines
660665 return false;
661666 }
662667
668+void Model::recalculateMeshPartOffsets() {
669+ if (!_calculatedMeshPartOffsetValid) {
670+ const FBXGeometry& geometry = _geometry->getFBXGeometry();
671+ int numberOfMeshes = geometry.meshes.size();
672+ _calculatedMeshPartOffset.clear();
673+ for (int i = 0; i < numberOfMeshes; i++) {
674+ const FBXMesh& mesh = geometry.meshes.at(i);
675+ qint64 partOffset = 0;
676+ for (int j = 0; j < mesh.parts.size(); j++) {
677+ const FBXMeshPart& part = mesh.parts.at(j);
678+ _calculatedMeshPartOffset[QPair<int,int>(i, j)] = partOffset;
679+ partOffset += part.quadIndices.size() * sizeof(int);
680+ partOffset += part.triangleIndices.size() * sizeof(int);
681+
682+ }
683+ }
684+ _calculatedMeshPartOffsetValid = true;
685+ }
686+}
663687 // TODO: we seem to call this too often when things haven't actually changed... look into optimizing this
664688 // Any script might trigger findRayIntersectionAgainstSubMeshes (and maybe convexHullContains), so these
665689 // can occur multiple times. In addition, rendering does it's own ray picking in order to decide which
@@ -675,7 +699,8 @@ void Model::recalculateMeshBoxes(bool pickAgainstTriangles) { · 9 unmodified lines
675699 _calculatedMeshTriangles.clear();
676700 _calculatedMeshTriangles.resize(numberOfMeshes);
677701 _calculatedMeshPartBoxes.clear();
678− _calculatedMeshPartOffet.clear();
702+ _calculatedMeshPartOffset.clear();
703+ _calculatedMeshPartOffsetValid = false;
679704 for (int i = 0; i < numberOfMeshes; i++) {
680705 const FBXMesh& mesh = geometry.meshes.at(i);
681706 Extents scaledMeshExtents = calculateScaledOffsetExtents(mesh.meshExtents);
@@ -770,7 +795,7 @@ void Model::recalculateMeshBoxes(bool pickAgainstTriangles) { · 88 unmodified lines
770795 }
771796 }
772797 _calculatedMeshPartBoxes[QPair<int,int>(i, j)] = thisPartBounds;
773− _calculatedMeshPartOffet[QPair<int,int>(i, j)] = partOffset;
798+ _calculatedMeshPartOffset[QPair<int,int>(i, j)] = partOffset;
774799
775800 partOffset += part.quadIndices.size() * sizeof(int);
776801 partOffset += part.triangleIndices.size() * sizeof(int);
@@ -778,6 +803,7 @@ void Model::recalculateMeshBoxes(bool pickAgainstTriangles) { · 1 unmodified line
778803 }
779804 _calculatedMeshTriangles[i] = thisMeshTriangles;
780805 _calculatedMeshPartBoxesValid = true;
806+ _calculatedMeshPartOffsetValid = true;
781807 }
782808 }
783809 _calculatedMeshBoxesValid = true;
@@ -786,16 +812,6 @@ void Model::recalculateMeshBoxes(bool pickAgainstTriangles) { · 2 unmodified lines
786812 }
787813
788814 void Model::renderSetup(RenderArgs* args) {
789− // if we don't have valid mesh boxes, calculate them now, this only matters in cases
790− // where our caller has passed RenderArgs which will include a view frustum we can cull
791− // against. We cache the results of these calculations so long as the model hasn't been
792− // simulated and the mesh hasn't changed.
793− if (args && !_calculatedMeshBoxesValid) {
794− _mutex.lock();
795− recalculateMeshBoxes();
796− _mutex.unlock();
797− }
798−
799815 // set up dilated textures on first render after load/simulate
800816 const FBXGeometry& geometry = _geometry->getFBXGeometry();
801817 if (_dilatedTextures.isEmpty()) {
@@ -1342,18 +1358,15 @@ void Model::snapToRegistrationPoint() { · 540 unmodified lines
13421358 }
13431359
13441360 void Model::simulate(float deltaTime, bool fullUpdate) {
1345− /*
1346− qDebug() << "Model::simulate()";
1347− qDebug() << " _translation:" << _translation;
1348− qDebug() << " _rotation:" << _rotation;
1349− */
1350−
13511361 fullUpdate = updateGeometry() || fullUpdate || (_scaleToFit && !_scaledToFit)
13521362 || (_snapModelToRegistrationPoint && !_snappedToRegistrationPoint);
13531363
13541364 if (isActive() && fullUpdate) {
1355− // NOTE: this seems problematic... need to review
1356− _calculatedMeshBoxesValid = false; // if we have to simulate, we need to assume our mesh boxes are all invalid
1365+ // NOTE: This is overly aggressive and we are invalidating the MeshBoxes when in fact they may not be invalid
1366+ // they really only become invalid if something about the transform to world space has changed. This is
1367+ // not too bad at this point, because it doesn't impact rendering. However it does slow down ray picking
1368+ // because ray picking needs valid boxes to work
1369+ _calculatedMeshBoxesValid = false;
13571370 _calculatedMeshTrianglesValid = false;
13581371
13591372 // check for scale to fit
@@ -1761,10 +1774,6 @@ void Model::setupBatchTransform(gpu::Batch& batch, RenderArgs* args) { · 401 unmodified lines
17611774 }
17621775
17631776 AABox Model::getPartBounds(int meshIndex, int partIndex) {
1764− if (!_calculatedMeshPartBoxesValid || !_calculatedMeshBoxesValid) {
1765− recalculateMeshBoxes(true);
1766− }
1767−
17681777 if (meshIndex < _meshStates.size()) {
17691778 const MeshState& state = _meshStates.at(meshIndex);
17701779 bool isSkinned = state.clusterMatrices.size() > 1;
@@ -1774,7 +1783,7 @@ AABox Model::getPartBounds(int meshIndex, int partIndex) { · 3 unmodified lines
17741783 }
17751784 }
17761785
1777− if (_calculatedMeshPartBoxesValid && _calculatedMeshPartBoxes.contains(QPair<int,int>(meshIndex, partIndex))) {
1786+ if (_geometry->getFBXGeometry().meshes.size() > meshIndex) {
17781787
17791788 // FIX ME! - This is currently a hack because for some mesh parts our efforts to calculate the bounding
17801789 // box of the mesh part fails. It seems to create boxes that are not consistent with where the
@@ -1796,15 +1805,13 @@ AABox Model::getPartBounds(int meshIndex, int partIndex) { · 15 unmodified lines
17961805
17971806 void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool translucent) {
17981807 PerformanceTimer perfTimer("Model::renderPart");
1799−
18001808 if (!_readyWhenAdded) {
18011809 return; // bail asap
18021810 }
18031811
1804− // we always need these properly calculated before we can render, this will likely already have been done
1805− // since the engine will call our getPartBounds() before rendering us.
1806− if (!_calculatedMeshPartBoxesValid || !_calculatedMeshBoxesValid) {
1807− recalculateMeshBoxes(true);
1812+ // We need to make sure we have valid offsets calculated before we can render
1813+ if (!_calculatedMeshPartOffsetValid) {
1814+ recalculateMeshPartOffsets();
18081815 }
18091816 auto textureCache = DependencyManager::get<TextureCache>();
18101817
@@ -2012,7 +2019,7 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran · 201 unmodified lines
20122019 }
20132020 }
20142021
2015− qint64 offset = _calculatedMeshPartOffet[QPair<int,int>(meshIndex, partIndex)];
2022+ qint64 offset = _calculatedMeshPartOffset[QPair<int,int>(meshIndex, partIndex)];
20162023
20172024 if (part.quadIndices.size() > 0) {
20182025 batch.drawIndexed(gpu::QUADS, part.quadIndices.size(), offset);