From 139855f48792594c5e6b5ea2a51557b0b0a5f515 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 10 Jun 2015 16:13:17 -0700 Subject: [PATCH] handle remove attachment case --- interface/src/avatar/Avatar.cpp | 26 ++++++++++++++++++++++---- interface/src/avatar/Avatar.h | 3 +++ interface/src/avatar/MyAvatar.cpp | 18 +----------------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index f644968ff8..dbe7d52a3f 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -107,6 +107,9 @@ Avatar::Avatar() : } Avatar::~Avatar() { + for(auto attachment : _unusedAttachments) { + delete attachment; + } } const float BILLBOARD_LOD_DISTANCE = 40.0f; @@ -525,7 +528,7 @@ glm::quat Avatar::computeRotationFromBodyToWorldUp(float proportion) const { return glm::angleAxis(angle * proportion, axis); } -void Avatar::renderBody(RenderArgs* renderArgs, ViewFrustum* renderFrustum, bool postLighting, float glowLevel) { +void Avatar::fixupModelsInScene() { // check to see if when we added our models to the scene they were ready, if they were not ready, then // fix them up in the scene render::ScenePointer scene = Application::getInstance()->getMain3DScene(); @@ -544,8 +547,18 @@ void Avatar::renderBody(RenderArgs* renderArgs, ViewFrustum* renderFrustum, bool attachmentModel->addToScene(scene, pendingChanges); } } + for (auto attachmentModelToRemove : _attachmentsToRemove) { + attachmentModelToRemove->removeFromScene(scene, pendingChanges); + _unusedAttachments << attachmentModelToRemove; + } + _attachmentsToRemove.clear(); scene->enqueuePendingChanges(pendingChanges); +} +void Avatar::renderBody(RenderArgs* renderArgs, ViewFrustum* renderFrustum, bool postLighting, float glowLevel) { + + fixupModelsInScene(); + { Glower glower(renderArgs, glowLevel); @@ -947,13 +960,18 @@ void Avatar::setAttachmentData(const QVector& attachmentData) { } // make sure we have as many models as attachments while (_attachmentModels.size() < attachmentData.size()) { - Model* model = new Model(this); + Model* model = nullptr; + if (_unusedAttachments.size() > 0) { + model = _unusedAttachments.takeFirst(); + } else { + model = new Model(this); + } model->init(); _attachmentModels.append(model); } while (_attachmentModels.size() > attachmentData.size()) { - // NOTE: what's really going to happen here? This seems dangerous... has the model been removed from the scene? - delete _attachmentModels.takeLast(); + auto attachmentModel = _attachmentModels.takeLast(); + _attachmentsToRemove << attachmentModel; } // update the urls diff --git a/interface/src/avatar/Avatar.h b/interface/src/avatar/Avatar.h index 1113496080..b198d12a6e 100644 --- a/interface/src/avatar/Avatar.h +++ b/interface/src/avatar/Avatar.h @@ -195,6 +195,8 @@ protected: SkeletonModel _skeletonModel; glm::vec3 _skeletonOffset; QVector _attachmentModels; + QVector _attachmentsToRemove; + QVector _unusedAttachments; float _bodyYawDelta; // These position histories and derivatives are in the world-frame. @@ -234,6 +236,7 @@ protected: void renderDisplayName(RenderArgs* renderArgs); virtual void renderBody(RenderArgs* renderArgs, ViewFrustum* renderFrustum, bool postLighting, float glowLevel = 0.0f); virtual bool shouldRenderHead(const RenderArgs* renderArgs, const glm::vec3& cameraPosition) const; + virtual void fixupModelsInScene(); void simulateAttachments(float deltaTime); diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 65b927c2c0..c4e08b5dba 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -1178,23 +1178,7 @@ void MyAvatar::renderBody(RenderArgs* renderArgs, ViewFrustum* renderFrustum, bo // check to see if when we added our models to the scene they were ready, if they were not ready, then // fix them up in the scene - render::ScenePointer scene = Application::getInstance()->getMain3DScene(); - render::PendingChanges pendingChanges; - if (_skeletonModel.needsFixupInScene()) { - _skeletonModel.removeFromScene(scene, pendingChanges); - _skeletonModel.addToScene(scene, pendingChanges); - } - if (getHead()->getFaceModel().needsFixupInScene()) { - getHead()->getFaceModel().removeFromScene(scene, pendingChanges); - getHead()->getFaceModel().addToScene(scene, pendingChanges); - } - for (auto attachmentModel : _attachmentModels) { - if (attachmentModel->needsFixupInScene()) { - attachmentModel->removeFromScene(scene, pendingChanges); - attachmentModel->addToScene(scene, pendingChanges); - } - } - scene->enqueuePendingChanges(pendingChanges); + fixupModelsInScene(); const glm::vec3 cameraPos = Application::getInstance()->getCamera()->getPosition();