From e3d22ef94bbcb20d07843a0eb0ecb0d00cf3a612 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 23 Jun 2015 16:43:27 -0700 Subject: [PATCH] Fix overlays getBounds + code cleanup --- interface/src/ui/overlays/Base3DOverlay.h | 3 + .../src/ui/overlays/BillboardOverlay.cpp | 52 +++------ interface/src/ui/overlays/BillboardOverlay.h | 11 +- interface/src/ui/overlays/Circle3DOverlay.cpp | 29 ++--- interface/src/ui/overlays/Circle3DOverlay.h | 2 +- interface/src/ui/overlays/Cube3DOverlay.cpp | 6 - interface/src/ui/overlays/Cube3DOverlay.h | 4 +- interface/src/ui/overlays/Grid3DOverlay.cpp | 25 ++-- interface/src/ui/overlays/Grid3DOverlay.h | 8 +- interface/src/ui/overlays/ImageOverlay.cpp | 10 +- interface/src/ui/overlays/ImageOverlay.h | 11 +- interface/src/ui/overlays/Line3DOverlay.cpp | 23 ++-- interface/src/ui/overlays/Line3DOverlay.h | 2 +- .../src/ui/overlays/LocalModelsOverlay.cpp | 6 +- .../src/ui/overlays/LocalModelsOverlay.h | 1 - interface/src/ui/overlays/ModelOverlay.cpp | 83 ++++--------- interface/src/ui/overlays/ModelOverlay.h | 7 +- interface/src/ui/overlays/Planar3DOverlay.cpp | 34 +++--- interface/src/ui/overlays/Planar3DOverlay.h | 27 ++--- .../src/ui/overlays/Rectangle3DOverlay.cpp | 4 +- interface/src/ui/overlays/Sphere3DOverlay.cpp | 12 +- interface/src/ui/overlays/Sphere3DOverlay.h | 4 +- interface/src/ui/overlays/Text3DOverlay.cpp | 109 +++++++++--------- interface/src/ui/overlays/Text3DOverlay.h | 10 +- interface/src/ui/overlays/TextOverlay.cpp | 5 +- interface/src/ui/overlays/TextOverlay.h | 9 +- interface/src/ui/overlays/Volume3DOverlay.cpp | 70 +++++------ interface/src/ui/overlays/Volume3DOverlay.h | 30 ++--- 28 files changed, 220 insertions(+), 377 deletions(-) diff --git a/interface/src/ui/overlays/Base3DOverlay.h b/interface/src/ui/overlays/Base3DOverlay.h index 542b786ae2..3cb4d27021 100644 --- a/interface/src/ui/overlays/Base3DOverlay.h +++ b/interface/src/ui/overlays/Base3DOverlay.h @@ -26,6 +26,7 @@ public: virtual bool is3D() const { return true; } const glm::vec3& getPosition() const { return _transform.getTranslation(); } const glm::quat& getRotation() const { return _transform.getRotation(); } + const glm::vec3& getScale() const { return _transform.getScale(); } // TODO: consider implementing registration points in this class const glm::vec3& getCenter() const { return getPosition(); } @@ -41,6 +42,8 @@ public: // setters void setPosition(const glm::vec3& value) { _transform.setTranslation(value); } void setRotation(const glm::quat& value) { _transform.setRotation(value); } + void setScale(float value) { _transform.setScale(value); } + void setScale(const glm::vec3& value) { _transform.setScale(value); } void setLineWidth(float lineWidth) { _lineWidth = lineWidth; } void setIsSolid(bool isSolid) { _isSolid = isSolid; } diff --git a/interface/src/ui/overlays/BillboardOverlay.cpp b/interface/src/ui/overlays/BillboardOverlay.cpp index e7b043f44f..288a950bbb 100644 --- a/interface/src/ui/overlays/BillboardOverlay.cpp +++ b/interface/src/ui/overlays/BillboardOverlay.cpp @@ -9,26 +9,20 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include "Application.h" -#include "GeometryUtil.h" -#include "PlaneShape.h" - #include "BillboardOverlay.h" -BillboardOverlay::BillboardOverlay() : - _fromImage(), - _scale(1.0f), - _isFacingAvatar(true) -{ +#include "Application.h" +#include "GeometryUtil.h" + +BillboardOverlay::BillboardOverlay() { _isLoaded = false; } BillboardOverlay::BillboardOverlay(const BillboardOverlay* billboardOverlay) : - Base3DOverlay(billboardOverlay), + Planar3DOverlay(billboardOverlay), _url(billboardOverlay->_url), _texture(billboardOverlay->_texture), _fromImage(billboardOverlay->_fromImage), - _scale(billboardOverlay->_scale), _isFacingAvatar(billboardOverlay->_isFacingAvatar) { } @@ -46,8 +40,8 @@ void BillboardOverlay::render(RenderArgs* args) { glm::quat rotation; if (_isFacingAvatar) { // rotate about vertical to face the camera - rotation = Application::getInstance()->getCamera()->getRotation(); - rotation *= glm::angleAxis(glm::pi(), glm::vec3(0.0f, 1.0f, 0.0f)); + rotation = args->_viewFrustum->getOrientation(); + rotation *= glm::angleAxis(glm::pi(), IDENTITY_UP); rotation *= getRotation(); } else { rotation = getRotation(); @@ -89,11 +83,9 @@ void BillboardOverlay::render(RenderArgs* args) { auto batch = args->_batch; if (batch) { - Transform transform; - transform.setTranslation(_position); - transform.setRotation(rotation); - transform.setScale(_scale); - + Transform transform = _transform; + transform.postScale(glm::vec3(getDimensions(), 1.0f)); + batch->setModelTransform(transform); batch->setUniformTexture(0, _texture->getGPUTexture()); @@ -111,10 +103,10 @@ void BillboardOverlay::render(RenderArgs* args) { glBindTexture(GL_TEXTURE_2D, _texture->getID()); glPushMatrix(); { - glTranslatef(_position.x, _position.y, _position.z); + glTranslatef(getPosition().x, getPosition().y, getPosition().z); glm::vec3 axis = glm::axis(rotation); glRotatef(glm::degrees(glm::angle(rotation)), axis.x, axis.y, axis.z); - glScalef(_scale, _scale, _scale); + glScalef(_dimensions.x, _dimensions.y, 1.0f); DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, glm::vec4(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha)); @@ -130,7 +122,7 @@ void BillboardOverlay::render(RenderArgs* args) { } void BillboardOverlay::setProperties(const QScriptValue &properties) { - Base3DOverlay::setProperties(properties); + Planar3DOverlay::setProperties(properties); QScriptValue urlValue = properties.property("url"); if (urlValue.isValid()) { @@ -171,11 +163,6 @@ void BillboardOverlay::setProperties(const QScriptValue &properties) { } } - QScriptValue scaleValue = properties.property("scale"); - if (scaleValue.isValid()) { - _scale = scaleValue.toVariant().toFloat(); - } - QScriptValue isFacingAvatarValue = properties.property("isFacingAvatar"); if (isFacingAvatarValue.isValid()) { _isFacingAvatar = isFacingAvatarValue.toVariant().toBool(); @@ -189,14 +176,11 @@ QScriptValue BillboardOverlay::getProperty(const QString& property) { if (property == "subImage") { return qRectToScriptValue(_scriptEngine, _fromImage); } - if (property == "scale") { - return _scale; - } if (property == "isFacingAvatar") { return _isFacingAvatar; } - return Base3DOverlay::getProperty(property); + return Planar3DOverlay::getProperty(property); } void BillboardOverlay::setURL(const QString& url) { @@ -212,13 +196,11 @@ bool BillboardOverlay::findRayIntersection(const glm::vec3& origin, const glm::v float& distance, BoxFace& face) { if (_texture) { - glm::quat rotation; + glm::quat rotation = getRotation(); if (_isFacingAvatar) { // rotate about vertical to face the camera rotation = Application::getInstance()->getCamera()->getRotation(); rotation *= glm::angleAxis(glm::pi(), glm::vec3(0.0f, 1.0f, 0.0f)); - } else { - rotation = _rotation; } // Produce the dimensions of the billboard based on the image's aspect ratio and the overlay's scale. @@ -226,9 +208,9 @@ bool BillboardOverlay::findRayIntersection(const glm::vec3& origin, const glm::v float width = isNull ? _texture->getWidth() : _fromImage.width(); float height = isNull ? _texture->getHeight() : _fromImage.height(); float maxSize = glm::max(width, height); - glm::vec2 dimensions = _scale * glm::vec2(width / maxSize, height / maxSize); + glm::vec2 dimensions = _dimensions * glm::vec2(width / maxSize, height / maxSize); - return findRayRectangleIntersection(origin, direction, rotation, _position, dimensions, distance); + return findRayRectangleIntersection(origin, direction, rotation, getPosition(), dimensions, distance); } return false; diff --git a/interface/src/ui/overlays/BillboardOverlay.h b/interface/src/ui/overlays/BillboardOverlay.h index a09c0c7528..15be0419a9 100644 --- a/interface/src/ui/overlays/BillboardOverlay.h +++ b/interface/src/ui/overlays/BillboardOverlay.h @@ -12,14 +12,11 @@ #ifndef hifi_BillboardOverlay_h #define hifi_BillboardOverlay_h -#include -#include - #include -#include "Base3DOverlay.h" +#include "Planar3DOverlay.h" -class BillboardOverlay : public Base3DOverlay { +class BillboardOverlay : public Planar3DOverlay { Q_OBJECT public: BillboardOverlay(); @@ -29,7 +26,6 @@ public: // setters void setURL(const QString& url); - void setScale(float scale) { _scale = scale; } void setIsFacingAvatar(bool isFacingAvatar) { _isFacingAvatar = isFacingAvatar; } virtual void setProperties(const QScriptValue& properties); @@ -48,8 +44,7 @@ private: QRect _fromImage; // where from in the image to sample - float _scale; - bool _isFacingAvatar; + bool _isFacingAvatar = true; }; #endif // hifi_BillboardOverlay_h diff --git a/interface/src/ui/overlays/Circle3DOverlay.cpp b/interface/src/ui/overlays/Circle3DOverlay.cpp index 55cb7ac9d2..53f1b4ce21 100644 --- a/interface/src/ui/overlays/Circle3DOverlay.cpp +++ b/interface/src/ui/overlays/Circle3DOverlay.cpp @@ -8,16 +8,12 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// include this before QGLWidget, which includes an earlier version of OpenGL -#include "InterfaceConfig.h" +#include "Circle3DOverlay.h" #include #include -#include -#include -#include +#include -#include "Circle3DOverlay.h" Circle3DOverlay::Circle3DOverlay() : _startAt(0.0f), @@ -66,9 +62,6 @@ Circle3DOverlay::Circle3DOverlay(const Circle3DOverlay* circle3DOverlay) : { } -Circle3DOverlay::~Circle3DOverlay() { -} - void Circle3DOverlay::render(RenderArgs* args) { if (!_visible) { return; // do nothing if we're not visible @@ -103,15 +96,13 @@ void Circle3DOverlay::render(RenderArgs* args) { _lastColor = colorX; auto geometryCache = DependencyManager::get(); - - Transform transform; - transform.setTranslation(getCenter()); - transform.setRotation(getRotation()); - transform.setScale(glm::vec3(getDimensions(), 0.01f) / 2.0f); - + Q_ASSERT(args->_batch); auto& batch = *args->_batch; batch._glLineWidth(_lineWidth); + + auto transform = _transform; + transform.postScale(glm::vec3(getDimensions(), 1.0f)); batch.setModelTransform(transform); DependencyManager::get()->bindSimpleProgram(batch, false, false); @@ -402,12 +393,12 @@ bool Circle3DOverlay::findRayIntersection(const glm::vec3& origin, if (intersects) { glm::vec3 hitPosition = origin + (distance * direction); - glm::vec3 localHitPosition = glm::inverse(_rotation) * (hitPosition - _position); - localHitPosition.y = localHitPosition.y * _dimensions.x / _dimensions.y; // Scale to make circular + glm::vec3 localHitPosition = glm::inverse(getRotation()) * (hitPosition - getPosition()); + localHitPosition.y = localHitPosition.y * getDimensions().x / getDimensions().y; // Scale to make circular float distanceToHit = glm::length(localHitPosition); - float innerRadius = _dimensions.x / 2.0f * _innerRadius; - float outerRadius = _dimensions.x / 2.0f * _outerRadius; + float innerRadius = getDimensions().x / 2.0f * _innerRadius; + float outerRadius = getDimensions().x / 2.0f * _outerRadius; intersects = innerRadius <= distanceToHit && distanceToHit <= outerRadius; } diff --git a/interface/src/ui/overlays/Circle3DOverlay.h b/interface/src/ui/overlays/Circle3DOverlay.h index fa9ecd0f25..c2f25c711d 100644 --- a/interface/src/ui/overlays/Circle3DOverlay.h +++ b/interface/src/ui/overlays/Circle3DOverlay.h @@ -19,7 +19,7 @@ class Circle3DOverlay : public Planar3DOverlay { public: Circle3DOverlay(); Circle3DOverlay(const Circle3DOverlay* circle3DOverlay); - ~Circle3DOverlay(); + virtual void render(RenderArgs* args); virtual void setProperties(const QScriptValue& properties); virtual QScriptValue getProperty(const QString& property); diff --git a/interface/src/ui/overlays/Cube3DOverlay.cpp b/interface/src/ui/overlays/Cube3DOverlay.cpp index 37b30a5bcb..7eb9a5b414 100644 --- a/interface/src/ui/overlays/Cube3DOverlay.cpp +++ b/interface/src/ui/overlays/Cube3DOverlay.cpp @@ -19,17 +19,11 @@ #include "Application.h" #include "Cube3DOverlay.h" -Cube3DOverlay::Cube3DOverlay() : _borderSize(0) { -} - Cube3DOverlay::Cube3DOverlay(const Cube3DOverlay* cube3DOverlay) : Volume3DOverlay(cube3DOverlay) { } -Cube3DOverlay::~Cube3DOverlay() { -} - void Cube3DOverlay::render(RenderArgs* args) { if (!_visible) { return; // do nothing if we're not visible diff --git a/interface/src/ui/overlays/Cube3DOverlay.h b/interface/src/ui/overlays/Cube3DOverlay.h index 16705a9c71..397ad77a9e 100644 --- a/interface/src/ui/overlays/Cube3DOverlay.h +++ b/interface/src/ui/overlays/Cube3DOverlay.h @@ -17,9 +17,9 @@ class Cube3DOverlay : public Volume3DOverlay { Q_OBJECT public: - Cube3DOverlay(); + Cube3DOverlay() {} Cube3DOverlay(const Cube3DOverlay* cube3DOverlay); - ~Cube3DOverlay(); + virtual void render(RenderArgs* args); virtual Cube3DOverlay* createClone() const; diff --git a/interface/src/ui/overlays/Grid3DOverlay.cpp b/interface/src/ui/overlays/Grid3DOverlay.cpp index e68e5b47f2..6316c8cd77 100644 --- a/interface/src/ui/overlays/Grid3DOverlay.cpp +++ b/interface/src/ui/overlays/Grid3DOverlay.cpp @@ -9,28 +9,29 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// include this before QGLWidget, which includes an earlier version of OpenGL +#include "InterfaceConfig.h" + +#include "Grid3DOverlay.h" + #include #include "Application.h" -#include "Grid3DOverlay.h" ProgramObject Grid3DOverlay::_gridProgram; -Grid3DOverlay::Grid3DOverlay() : Base3DOverlay(), +Grid3DOverlay::Grid3DOverlay() : _minorGridWidth(1.0), _majorGridEvery(5) { } Grid3DOverlay::Grid3DOverlay(const Grid3DOverlay* grid3DOverlay) : - Base3DOverlay(grid3DOverlay), + Planar3DOverlay(grid3DOverlay), _minorGridWidth(grid3DOverlay->_minorGridWidth), _majorGridEvery(grid3DOverlay->_majorGridEvery) { } -Grid3DOverlay::~Grid3DOverlay() { -} - void Grid3DOverlay::render(RenderArgs* args) { if (!_visible) { return; // do nothing if we're not visible @@ -41,7 +42,7 @@ void Grid3DOverlay::render(RenderArgs* args) { const float MAX_COLOR = 255.0f; // center the grid around the camera position on the plane - glm::vec3 rotated = glm::inverse(_rotation) * Application::getInstance()->getCamera()->getPosition(); + glm::vec3 rotated = glm::inverse(getRotation()) * Application::getInstance()->getCamera()->getPosition(); float spacing = _minorGridWidth; @@ -53,7 +54,7 @@ void Grid3DOverlay::render(RenderArgs* args) { if (batch) { Transform transform; - transform.setRotation(_rotation); + transform.setRotation(getRotation()); // Minor grid @@ -61,7 +62,7 @@ void Grid3DOverlay::render(RenderArgs* args) { batch->_glLineWidth(1.0f); auto position = glm::vec3(_minorGridWidth * (floorf(rotated.x / spacing) - MINOR_GRID_DIVISIONS / 2), spacing * (floorf(rotated.y / spacing) - MINOR_GRID_DIVISIONS / 2), - _position.z); + getPosition().z); float scale = MINOR_GRID_DIVISIONS * spacing; transform.setTranslation(position); @@ -78,7 +79,7 @@ void Grid3DOverlay::render(RenderArgs* args) { spacing *= _majorGridEvery; auto position = glm::vec3(spacing * (floorf(rotated.x / spacing) - MAJOR_GRID_DIVISIONS / 2), spacing * (floorf(rotated.y / spacing) - MAJOR_GRID_DIVISIONS / 2), - _position.z); + getPosition().z); float scale = MAJOR_GRID_DIVISIONS * spacing; transform.setTranslation(position); @@ -161,7 +162,7 @@ void Grid3DOverlay::render(RenderArgs* args) { } void Grid3DOverlay::setProperties(const QScriptValue& properties) { - Base3DOverlay::setProperties(properties); + Planar3DOverlay::setProperties(properties); if (properties.property("minorGridWidth").isValid()) { _minorGridWidth = properties.property("minorGridWidth").toVariant().toFloat(); @@ -180,7 +181,7 @@ QScriptValue Grid3DOverlay::getProperty(const QString& property) { return _majorGridEvery; } - return Base3DOverlay::getProperty(property); + return Planar3DOverlay::getProperty(property); } Grid3DOverlay* Grid3DOverlay::createClone() const { diff --git a/interface/src/ui/overlays/Grid3DOverlay.h b/interface/src/ui/overlays/Grid3DOverlay.h index 451ce0a498..ac0f2834fe 100644 --- a/interface/src/ui/overlays/Grid3DOverlay.h +++ b/interface/src/ui/overlays/Grid3DOverlay.h @@ -15,20 +15,16 @@ // include this before QGLWidget, which includes an earlier version of OpenGL #include "InterfaceConfig.h" -#include - #include -#include -#include "Base3DOverlay.h" +#include "Planar3DOverlay.h" -class Grid3DOverlay : public Base3DOverlay { +class Grid3DOverlay : public Planar3DOverlay { Q_OBJECT public: Grid3DOverlay(); Grid3DOverlay(const Grid3DOverlay* grid3DOverlay); - ~Grid3DOverlay(); virtual void render(RenderArgs* args); virtual void setProperties(const QScriptValue& properties); diff --git a/interface/src/ui/overlays/ImageOverlay.cpp b/interface/src/ui/overlays/ImageOverlay.cpp index 72e47b5e3d..e84b60cc43 100644 --- a/interface/src/ui/overlays/ImageOverlay.cpp +++ b/interface/src/ui/overlays/ImageOverlay.cpp @@ -11,14 +11,11 @@ // include this before QGLWidget, which includes an earlier version of OpenGL #include "InterfaceConfig.h" -#include -#include +#include "ImageOverlay.h" #include #include -#include - -#include "ImageOverlay.h" +#include ImageOverlay::ImageOverlay() : _imageURL(), @@ -38,9 +35,6 @@ ImageOverlay::ImageOverlay(const ImageOverlay* imageOverlay) : { } -ImageOverlay::~ImageOverlay() { -} - // TODO: handle setting image multiple times, how do we manage releasing the bound texture? void ImageOverlay::setImageURL(const QUrl& url) { _imageURL = url; diff --git a/interface/src/ui/overlays/ImageOverlay.h b/interface/src/ui/overlays/ImageOverlay.h index 31c6031102..e818469643 100644 --- a/interface/src/ui/overlays/ImageOverlay.h +++ b/interface/src/ui/overlays/ImageOverlay.h @@ -11,21 +11,12 @@ #ifndef hifi_ImageOverlay_h #define hifi_ImageOverlay_h -// include this before QGLWidget, which includes an earlier version of OpenGL -#include "InterfaceConfig.h" - #include -#include #include -#include -#include #include -#include -#include #include -#include "Overlay.h" #include "Overlay2D.h" class ImageOverlay : public Overlay2D { @@ -34,7 +25,7 @@ class ImageOverlay : public Overlay2D { public: ImageOverlay(); ImageOverlay(const ImageOverlay* imageOverlay); - ~ImageOverlay(); + virtual void render(RenderArgs* args); // getters diff --git a/interface/src/ui/overlays/Line3DOverlay.cpp b/interface/src/ui/overlays/Line3DOverlay.cpp index 44c6ec7ef6..0bb32b9d6e 100644 --- a/interface/src/ui/overlays/Line3DOverlay.cpp +++ b/interface/src/ui/overlays/Line3DOverlay.cpp @@ -13,6 +13,7 @@ #include #include +#include #include "Line3DOverlay.h" @@ -33,13 +34,12 @@ Line3DOverlay::~Line3DOverlay() { } AABox Line3DOverlay::getBounds() const { - auto start = _position + _start; - auto end = _position + _end; - - auto min = glm::min(start, end); - auto max = glm::max(start, end); - - return AABox(min, max - min); + auto extents = Extents{}; + extents.addPoint(_start); + extents.addPoint(_end); + extents.transform(_transform); + + return AABox(extents); } void Line3DOverlay::render(RenderArgs* args) { @@ -55,14 +55,11 @@ void Line3DOverlay::render(RenderArgs* args) { auto batch = args->_batch; if (batch) { - Transform transform; - transform.setTranslation(_position); - transform.setRotation(_rotation); - batch->setModelTransform(transform); + batch->setModelTransform(_transform); if (getIsDashedLine()) { // TODO: add support for color to renderDashedLine() - DependencyManager::get()->renderDashedLine(*batch, _position, _end, colorv4, _geometryCacheID); + DependencyManager::get()->renderDashedLine(*batch, _start, _end, colorv4, _geometryCacheID); } else { DependencyManager::get()->renderLine(*batch, _start, _end, colorv4, _geometryCacheID); } @@ -87,7 +84,7 @@ void Line3DOverlay::render(RenderArgs* args) { if (getIsDashedLine()) { // TODO: add support for color to renderDashedLine() - DependencyManager::get()->renderDashedLine(_position, _end, colorv4, _geometryCacheID); + DependencyManager::get()->renderDashedLine(_start, _end, colorv4, _geometryCacheID); } else { DependencyManager::get()->renderLine(_start, _end, colorv4, _geometryCacheID); } diff --git a/interface/src/ui/overlays/Line3DOverlay.h b/interface/src/ui/overlays/Line3DOverlay.h index ac5f18d8ed..4a4d8f4d90 100644 --- a/interface/src/ui/overlays/Line3DOverlay.h +++ b/interface/src/ui/overlays/Line3DOverlay.h @@ -21,7 +21,7 @@ public: Line3DOverlay(const Line3DOverlay* line3DOverlay); ~Line3DOverlay(); virtual void render(RenderArgs* args); - virtual AABox getBounds() const override; + virtual AABox getBounds() const; // getters const glm::vec3& getStart() const { return _start; } diff --git a/interface/src/ui/overlays/LocalModelsOverlay.cpp b/interface/src/ui/overlays/LocalModelsOverlay.cpp index b9ce245128..912196041f 100644 --- a/interface/src/ui/overlays/LocalModelsOverlay.cpp +++ b/interface/src/ui/overlays/LocalModelsOverlay.cpp @@ -24,10 +24,6 @@ LocalModelsOverlay::LocalModelsOverlay(const LocalModelsOverlay* localModelsOver Volume3DOverlay(localModelsOverlay), _entityTreeRenderer(localModelsOverlay->_entityTreeRenderer) { - -} - -LocalModelsOverlay::~LocalModelsOverlay() { } void LocalModelsOverlay::update(float deltatime) { @@ -46,7 +42,7 @@ void LocalModelsOverlay::render(RenderArgs* args) { glPushMatrix(); { Application* app = Application::getInstance(); glm::vec3 oldTranslation = app->getViewMatrixTranslation(); - app->setViewMatrixTranslation(oldTranslation + _position); + app->setViewMatrixTranslation(oldTranslation + getPosition()); _entityTreeRenderer->render(args); Application::getInstance()->setViewMatrixTranslation(oldTranslation); } glPopMatrix(); diff --git a/interface/src/ui/overlays/LocalModelsOverlay.h b/interface/src/ui/overlays/LocalModelsOverlay.h index a82943a4a0..c311b2bc1b 100644 --- a/interface/src/ui/overlays/LocalModelsOverlay.h +++ b/interface/src/ui/overlays/LocalModelsOverlay.h @@ -21,7 +21,6 @@ class LocalModelsOverlay : public Volume3DOverlay { public: LocalModelsOverlay(EntityTreeRenderer* entityTreeRenderer); LocalModelsOverlay(const LocalModelsOverlay* localModelsOverlay); - ~LocalModelsOverlay(); virtual void update(float deltatime); virtual void render(RenderArgs* args); diff --git a/interface/src/ui/overlays/ModelOverlay.cpp b/interface/src/ui/overlays/ModelOverlay.cpp index 822fff9808..4476518efe 100644 --- a/interface/src/ui/overlays/ModelOverlay.cpp +++ b/interface/src/ui/overlays/ModelOverlay.cpp @@ -9,15 +9,15 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include "ModelOverlay.h" + #include #include -#include "ModelOverlay.h" ModelOverlay::ModelOverlay() : _model(), _modelTextures(QVariantMap()), - _scale(1.0f), _updateModel(false) { _model.init(); @@ -25,12 +25,10 @@ ModelOverlay::ModelOverlay() } ModelOverlay::ModelOverlay(const ModelOverlay* modelOverlay) : - Base3DOverlay(modelOverlay), + Volume3DOverlay(modelOverlay), _model(), _modelTextures(QVariantMap()), _url(modelOverlay->_url), - _rotation(modelOverlay->_rotation), - _scale(modelOverlay->_scale), _updateModel(false) { _model.init(); @@ -45,8 +43,9 @@ void ModelOverlay::update(float deltatime) { _updateModel = false; _model.setSnapModelToCenter(true); - _model.setRotation(_rotation); - _model.setTranslation(_position); + _model.setScale(getScale()); + _model.setRotation(getRotation()); + _model.setTranslation(getPosition()); _model.setURL(_url); _model.simulate(deltatime, true); } else { @@ -56,13 +55,13 @@ void ModelOverlay::update(float deltatime) { } bool ModelOverlay::addToScene(Overlay::Pointer overlay, std::shared_ptr scene, render::PendingChanges& pendingChanges) { - Base3DOverlay::addToScene(overlay, scene, pendingChanges); + Volume3DOverlay::addToScene(overlay, scene, pendingChanges); _model.addToScene(scene, pendingChanges); return true; } void ModelOverlay::removeFromScene(Overlay::Pointer overlay, std::shared_ptr scene, render::PendingChanges& pendingChanges) { - Base3DOverlay::removeFromScene(overlay, scene, pendingChanges); + Volume3DOverlay::removeFromScene(overlay, scene, pendingChanges); _model.removeFromScene(scene, pendingChanges); } @@ -100,54 +99,26 @@ void ModelOverlay::render(RenderArgs* args) { } void ModelOverlay::setProperties(const QScriptValue &properties) { - Base3DOverlay::setProperties(properties); + auto position = getPosition(); + auto rotation = getRotation(); + auto scale = getDimensions(); + + Volume3DOverlay::setProperties(properties); + + if (position != getPosition() || rotation != getRotation() || scale != getDimensions()) { + _model.setScaleToFit(true, getScale()); + _updateModel = true; + } QScriptValue urlValue = properties.property("url"); - if (urlValue.isValid()) { - _url = urlValue.toVariant().toString(); + if (urlValue.isValid() && urlValue.isString()) { + _url = urlValue.toString(); _updateModel = true; _isLoaded = false; } - QScriptValue scaleValue = properties.property("scale"); - if (scaleValue.isValid()) { - _scale = scaleValue.toVariant().toFloat(); - _model.setScaleToFit(true, _scale); - _updateModel = true; - } - - QScriptValue rotationValue = properties.property("rotation"); - if (rotationValue.isValid()) { - QScriptValue x = rotationValue.property("x"); - QScriptValue y = rotationValue.property("y"); - QScriptValue z = rotationValue.property("z"); - QScriptValue w = rotationValue.property("w"); - if (x.isValid() && y.isValid() && z.isValid() && w.isValid()) { - _rotation.x = x.toVariant().toFloat(); - _rotation.y = y.toVariant().toFloat(); - _rotation.z = z.toVariant().toFloat(); - _rotation.w = w.toVariant().toFloat(); - } - _updateModel = true; - } - - QScriptValue dimensionsValue = properties.property("dimensions"); - if (dimensionsValue.isValid()) { - QScriptValue x = dimensionsValue.property("x"); - QScriptValue y = dimensionsValue.property("y"); - QScriptValue z = dimensionsValue.property("z"); - if (x.isValid() && y.isValid() && z.isValid()) { - glm::vec3 dimensions; - dimensions.x = x.toVariant().toFloat(); - dimensions.y = y.toVariant().toFloat(); - dimensions.z = z.toVariant().toFloat(); - _model.setScaleToFit(true, dimensions); - } - _updateModel = true; - } - QScriptValue texturesValue = properties.property("textures"); - if (texturesValue.isValid()) { + if (texturesValue.isValid() && texturesValue.toVariant().canConvert(QVariant::Map)) { QVariantMap textureMap = texturesValue.toVariant().toMap(); foreach(const QString& key, textureMap.keys()) { @@ -161,22 +132,12 @@ void ModelOverlay::setProperties(const QScriptValue &properties) { _modelTextures[key] = newTextureURL; // Keep local track of textures for getProperty() } } - - if (properties.property("position").isValid()) { - _updateModel = true; - } } QScriptValue ModelOverlay::getProperty(const QString& property) { if (property == "url") { return _url.toString(); } - if (property == "scale") { - return _scale; - } - if (property == "rotation") { - return quatToScriptValue(_scriptEngine, _rotation); - } if (property == "dimensions") { return vec3toScriptValue(_scriptEngine, _model.getScaleToFitDimensions()); } @@ -192,7 +153,7 @@ QScriptValue ModelOverlay::getProperty(const QString& property) { } } - return Base3DOverlay::getProperty(property); + return Volume3DOverlay::getProperty(property); } bool ModelOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, diff --git a/interface/src/ui/overlays/ModelOverlay.h b/interface/src/ui/overlays/ModelOverlay.h index a81cae530a..1c43f42909 100644 --- a/interface/src/ui/overlays/ModelOverlay.h +++ b/interface/src/ui/overlays/ModelOverlay.h @@ -14,9 +14,9 @@ #include -#include "Base3DOverlay.h" +#include "Volume3DOverlay.h" -class ModelOverlay : public Base3DOverlay { +class ModelOverlay : public Volume3DOverlay { Q_OBJECT public: ModelOverlay(); @@ -41,9 +41,6 @@ private: QVariantMap _modelTextures; QUrl _url; - glm::quat _rotation; - float _scale; - bool _updateModel; }; diff --git a/interface/src/ui/overlays/Planar3DOverlay.cpp b/interface/src/ui/overlays/Planar3DOverlay.cpp index 7ed7332f19..0ca092ba23 100644 --- a/interface/src/ui/overlays/Planar3DOverlay.cpp +++ b/interface/src/ui/overlays/Planar3DOverlay.cpp @@ -8,32 +8,24 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// include this before QGLWidget, which includes an earlier version of OpenGL -#include "InterfaceConfig.h" - -#include -#include -#include -#include - -#include "GeometryUtil.h" - #include "Planar3DOverlay.h" -const float DEFAULT_SIZE = 1.0f; - -Planar3DOverlay::Planar3DOverlay() : - _dimensions(glm::vec2(DEFAULT_SIZE, DEFAULT_SIZE)) -{ -} +#include +#include +#include Planar3DOverlay::Planar3DOverlay(const Planar3DOverlay* planar3DOverlay) : - Base3DOverlay(planar3DOverlay), - _dimensions(planar3DOverlay->_dimensions) + Base3DOverlay(planar3DOverlay) { } -Planar3DOverlay::~Planar3DOverlay() { +AABox Planar3DOverlay::getBounds() const { + auto halfDimensions = glm::vec3{_dimensions / 2.0f, 0.01f}; + + auto extents = Extents{-halfDimensions, halfDimensions}; + extents.transform(_transform); + + return AABox(extents); } void Planar3DOverlay::setProperties(const QScriptValue& properties) { @@ -86,7 +78,7 @@ void Planar3DOverlay::setProperties(const QScriptValue& properties) { QScriptValue Planar3DOverlay::getProperty(const QString& property) { if (property == "dimensions" || property == "scale" || property == "size") { - return vec2toScriptValue(_scriptEngine, _dimensions); + return vec2toScriptValue(_scriptEngine, getDimensions()); } return Base3DOverlay::getProperty(property); @@ -94,5 +86,5 @@ QScriptValue Planar3DOverlay::getProperty(const QString& property) { bool Planar3DOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face) { - return findRayRectangleIntersection(origin, direction, _rotation, _position, _dimensions, distance); + return findRayRectangleIntersection(origin, direction, getRotation(), getPosition(), getDimensions(), distance); } diff --git a/interface/src/ui/overlays/Planar3DOverlay.h b/interface/src/ui/overlays/Planar3DOverlay.h index 8a02b35bd2..fe8c513efd 100644 --- a/interface/src/ui/overlays/Planar3DOverlay.h +++ b/interface/src/ui/overlays/Planar3DOverlay.h @@ -11,37 +11,28 @@ #ifndef hifi_Planar3DOverlay_h #define hifi_Planar3DOverlay_h -// include this before QGLWidget, which includes an earlier version of OpenGL -#include "InterfaceConfig.h" - -#include - -#include - #include "Base3DOverlay.h" class Planar3DOverlay : public Base3DOverlay { Q_OBJECT public: - Planar3DOverlay(); + Planar3DOverlay() {} Planar3DOverlay(const Planar3DOverlay* planar3DOverlay); - ~Planar3DOverlay(); - - // getters - const glm::vec2& getDimensions() const { return _dimensions; } - - // setters - void setSize(float size) { _dimensions = glm::vec2(size, size); } + + AABox getBounds() const; + + glm::vec2 getDimensions() const { return _dimensions; } + void setDimensions(float value) { _dimensions = glm::vec2(value); } void setDimensions(const glm::vec2& value) { _dimensions = value; } - + virtual void setProperties(const QScriptValue& properties); virtual QScriptValue getProperty(const QString& property); virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face); - + protected: - glm::vec2 _dimensions; + glm::vec2 _dimensions{1.0f, 1.0f}; }; diff --git a/interface/src/ui/overlays/Rectangle3DOverlay.cpp b/interface/src/ui/overlays/Rectangle3DOverlay.cpp index dc5fdeabb2..74bbd1bca8 100644 --- a/interface/src/ui/overlays/Rectangle3DOverlay.cpp +++ b/interface/src/ui/overlays/Rectangle3DOverlay.cpp @@ -11,12 +11,12 @@ // include this before QGLWidget, which includes an earlier version of OpenGL #include "InterfaceConfig.h" +#include "Rectangle3DOverlay.h" + #include #include #include -#include "Rectangle3DOverlay.h" - Rectangle3DOverlay::Rectangle3DOverlay() : _geometryCacheID(DependencyManager::get()->allocateID()) { diff --git a/interface/src/ui/overlays/Sphere3DOverlay.cpp b/interface/src/ui/overlays/Sphere3DOverlay.cpp index f5fba0ed05..e129954db9 100644 --- a/interface/src/ui/overlays/Sphere3DOverlay.cpp +++ b/interface/src/ui/overlays/Sphere3DOverlay.cpp @@ -17,17 +17,12 @@ #include "Sphere3DOverlay.h" #include "Application.h" -Sphere3DOverlay::Sphere3DOverlay() { -} Sphere3DOverlay::Sphere3DOverlay(const Sphere3DOverlay* Sphere3DOverlay) : Volume3DOverlay(Sphere3DOverlay) { } -Sphere3DOverlay::~Sphere3DOverlay() { -} - void Sphere3DOverlay::render(RenderArgs* args) { if (!_visible) { return; // do nothing if we're not visible @@ -42,11 +37,8 @@ void Sphere3DOverlay::render(RenderArgs* args) { auto batch = args->_batch; if (batch) { - Transform transform; - transform.setTranslation(_position); - transform.setRotation(_rotation); - transform.setScale(_dimensions); - + Transform transform = _transform; + transform.postScale(getDimensions()); batch->setModelTransform(transform); DependencyManager::get()->renderSphere(*batch, 1.0f, SLICES, SLICES, sphereColor, _isSolid); } else { diff --git a/interface/src/ui/overlays/Sphere3DOverlay.h b/interface/src/ui/overlays/Sphere3DOverlay.h index 3b881e5ba6..b82dc548f1 100644 --- a/interface/src/ui/overlays/Sphere3DOverlay.h +++ b/interface/src/ui/overlays/Sphere3DOverlay.h @@ -17,9 +17,9 @@ class Sphere3DOverlay : public Volume3DOverlay { Q_OBJECT public: - Sphere3DOverlay(); + Sphere3DOverlay() {} Sphere3DOverlay(const Sphere3DOverlay* Sphere3DOverlay); - ~Sphere3DOverlay(); + virtual void render(RenderArgs* args); virtual Sphere3DOverlay* createClone() const; diff --git a/interface/src/ui/overlays/Text3DOverlay.cpp b/interface/src/ui/overlays/Text3DOverlay.cpp index 5acd10eb45..37774094de 100644 --- a/interface/src/ui/overlays/Text3DOverlay.cpp +++ b/interface/src/ui/overlays/Text3DOverlay.cpp @@ -11,14 +11,17 @@ // include this before QGLWidget, which includes an earlier version of OpenGL #include "InterfaceConfig.h" -#include "Application.h" #include "Text3DOverlay.h" #include +#include + +#include "Application.h" const xColor DEFAULT_BACKGROUND_COLOR = { 0, 0, 0 }; const float DEFAULT_BACKGROUND_ALPHA = 0.7f; const float DEFAULT_MARGIN = 0.1f; +const int FIXED_FONT_POINT_SIZE = 40; const int FIXED_FONT_SCALING_RATIO = FIXED_FONT_POINT_SIZE * 80.0f; // this is a ratio determined through experimentation const float LINE_SCALE_RATIO = 1.2f; @@ -32,6 +35,7 @@ Text3DOverlay::Text3DOverlay() : _bottomMargin(DEFAULT_MARGIN), _isFacingAvatar(false) { + _textRenderer = TextRenderer3D::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE); } Text3DOverlay::Text3DOverlay(const Text3DOverlay* text3DOverlay) : @@ -46,6 +50,7 @@ Text3DOverlay::Text3DOverlay(const Text3DOverlay* text3DOverlay) : _bottomMargin(text3DOverlay->_bottomMargin), _isFacingAvatar(text3DOverlay->_isFacingAvatar) { + _textRenderer = TextRenderer3D::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE); } Text3DOverlay::~Text3DOverlay() { @@ -77,58 +82,58 @@ void Text3DOverlay::render(RenderArgs* args) { return; // do nothing if we're not visible } - auto batch = args->_batch; - - if (batch) { - glm::quat rotation; - - if (_isFacingAvatar) { - // rotate about vertical to face the camera - rotation = Application::getInstance()->getCamera()->getRotation(); - } else { - rotation = getRotation(); - } - - Transform transform; - transform.setTranslation(_position); - transform.setRotation(rotation); - - batch->setModelTransform(transform); - - const float MAX_COLOR = 255.0f; - xColor backgroundColor = getBackgroundColor(); - glm::vec4 quadColor(backgroundColor.red / MAX_COLOR, backgroundColor.green / MAX_COLOR, backgroundColor.blue / MAX_COLOR, - getBackgroundAlpha()); - - glm::vec2 dimensions = getDimensions(); - glm::vec2 halfDimensions = dimensions * 0.5f; - - const float SLIGHTLY_BEHIND = -0.005f; - - glm::vec3 topLeft(-halfDimensions.x, -halfDimensions.y, SLIGHTLY_BEHIND); - glm::vec3 bottomRight(halfDimensions.x, halfDimensions.y, SLIGHTLY_BEHIND); - DependencyManager::get()->renderQuad(*batch, topLeft, bottomRight, quadColor); - - // Same font properties as textSize() - float maxHeight = (float)_textRenderer->computeExtent("Xy").y * LINE_SCALE_RATIO; - - float scaleFactor = (maxHeight / FIXED_FONT_SCALING_RATIO) * _lineHeight; - - glm::vec2 clipMinimum(0.0f, 0.0f); - glm::vec2 clipDimensions((dimensions.x - (_leftMargin + _rightMargin)) / scaleFactor, - (dimensions.y - (_topMargin + _bottomMargin)) / scaleFactor); - - transform.setTranslation(_position); - transform.postTranslate(glm::vec3(-(halfDimensions.x - _leftMargin) , halfDimensions.y - _topMargin, 0.01f)); - transform.setScale(scaleFactor); - batch->setModelTransform(transform); - - glm::vec4 textColor = { _color.red / MAX_COLOR, _color.green / MAX_COLOR, _color.blue / MAX_COLOR, getAlpha() }; - _textRenderer->draw(*batch, 0, 0, _text, textColor); - - batch->setPipeline(DrawOverlay3D::getOpaquePipeline()); + + Q_ASSERT(args->_batch); + auto& batch = *args->_batch; + + glm::quat rotation; + + if (_isFacingAvatar) { + // rotate about vertical to face the camera + rotation = args->_viewFrustum->getOrientation(); + } else { + rotation = getRotation(); } - + + Transform transform; + transform.setTranslation(getPosition()); + transform.setRotation(rotation); + transform.setScale(getScale()); + + batch.setModelTransform(transform); + + const float MAX_COLOR = 255.0f; + xColor backgroundColor = getBackgroundColor(); + glm::vec4 quadColor(backgroundColor.red / MAX_COLOR, backgroundColor.green / MAX_COLOR, backgroundColor.blue / MAX_COLOR, + getBackgroundAlpha()); + + glm::vec2 dimensions = getDimensions(); + glm::vec2 halfDimensions = dimensions * 0.5f; + + const float SLIGHTLY_BEHIND = -0.005f; + + glm::vec3 topLeft(-halfDimensions.x, -halfDimensions.y, SLIGHTLY_BEHIND); + glm::vec3 bottomRight(halfDimensions.x, halfDimensions.y, SLIGHTLY_BEHIND); + DependencyManager::get()->renderQuad(batch, topLeft, bottomRight, quadColor); + + // Same font properties as textSize() + float maxHeight = (float)_textRenderer->computeExtent("Xy").y * LINE_SCALE_RATIO; + + float scaleFactor = (maxHeight / FIXED_FONT_SCALING_RATIO) * _lineHeight; + + glm::vec2 clipMinimum(0.0f, 0.0f); + glm::vec2 clipDimensions((dimensions.x - (_leftMargin + _rightMargin)) / scaleFactor, + (dimensions.y - (_topMargin + _bottomMargin)) / scaleFactor); + + transform.setTranslation(getPosition()); + transform.postTranslate(glm::vec3(-(halfDimensions.x - _leftMargin) , halfDimensions.y - _topMargin, 0.01f)); + transform.setScale(scaleFactor); + batch.setModelTransform(transform); + + glm::vec4 textColor = { _color.red / MAX_COLOR, _color.green / MAX_COLOR, _color.blue / MAX_COLOR, getAlpha() }; + _textRenderer->draw(batch, 0, 0, _text, textColor); + + batch.setPipeline(DrawOverlay3D::getOpaquePipeline()); } void Text3DOverlay::setProperties(const QScriptValue& properties) { diff --git a/interface/src/ui/overlays/Text3DOverlay.h b/interface/src/ui/overlays/Text3DOverlay.h index a30a720414..666b43d8b1 100644 --- a/interface/src/ui/overlays/Text3DOverlay.h +++ b/interface/src/ui/overlays/Text3DOverlay.h @@ -11,17 +11,11 @@ #ifndef hifi_Text3DOverlay_h #define hifi_Text3DOverlay_h -// include this before QGLWidget, which includes an earlier version of OpenGL -#include "InterfaceConfig.h" - #include -#include -#include - #include "Planar3DOverlay.h" -const int FIXED_FONT_POINT_SIZE = 40; +class TextRenderer3D; class Text3DOverlay : public Planar3DOverlay { Q_OBJECT @@ -60,7 +54,7 @@ public: virtual Text3DOverlay* createClone() const; private: - TextRenderer3D* _textRenderer = TextRenderer3D::getInstance(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE); + TextRenderer3D* _textRenderer = nullptr; QString _text; xColor _backgroundColor; diff --git a/interface/src/ui/overlays/TextOverlay.cpp b/interface/src/ui/overlays/TextOverlay.cpp index 1a8b74dea8..ccad3bd295 100644 --- a/interface/src/ui/overlays/TextOverlay.cpp +++ b/interface/src/ui/overlays/TextOverlay.cpp @@ -11,11 +11,14 @@ // include this before QGLWidget, which includes an earlier version of OpenGL #include "InterfaceConfig.h" +#include "TextOverlay.h" + #include #include +#include #include +#include -#include "TextOverlay.h" TextOverlay::TextOverlay() : _backgroundColor(DEFAULT_BACKGROUND_COLOR), diff --git a/interface/src/ui/overlays/TextOverlay.h b/interface/src/ui/overlays/TextOverlay.h index 5a715ebfdf..b5d5dba11b 100644 --- a/interface/src/ui/overlays/TextOverlay.h +++ b/interface/src/ui/overlays/TextOverlay.h @@ -11,17 +11,10 @@ #ifndef hifi_TextOverlay_h #define hifi_TextOverlay_h -// include this before QGLWidget, which includes an earlier version of OpenGL -#include "InterfaceConfig.h" - -#include -#include #include #include -#include -#include "Overlay.h" #include "Overlay2D.h" const xColor DEFAULT_BACKGROUND_COLOR = { 0, 0, 0 }; @@ -30,6 +23,8 @@ const int DEFAULT_MARGIN = 10; const int DEFAULT_FONTSIZE = 11; const int DEFAULT_FONT_WEIGHT = 50; +class TextRenderer; + class TextOverlay : public Overlay2D { Q_OBJECT diff --git a/interface/src/ui/overlays/Volume3DOverlay.cpp b/interface/src/ui/overlays/Volume3DOverlay.cpp index b0310f8155..a7c9835f5d 100644 --- a/interface/src/ui/overlays/Volume3DOverlay.cpp +++ b/interface/src/ui/overlays/Volume3DOverlay.cpp @@ -8,32 +8,22 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include -#include - -// include this before QGLWidget, which includes an earlier version of OpenGL -#include "InterfaceConfig.h" - -#include -#include -#include - #include "Volume3DOverlay.h" -const float DEFAULT_SIZE = 1.0f; - -Volume3DOverlay::Volume3DOverlay() : - _dimensions(glm::vec3(DEFAULT_SIZE, DEFAULT_SIZE, DEFAULT_SIZE)) -{ -} +#include +#include Volume3DOverlay::Volume3DOverlay(const Volume3DOverlay* volume3DOverlay) : - Base3DOverlay(volume3DOverlay), - _dimensions(volume3DOverlay->_dimensions) + Base3DOverlay(volume3DOverlay) { } -Volume3DOverlay::~Volume3DOverlay() { +AABox Volume3DOverlay::getBounds() const { + auto extents = Extents{_localBoundingBox}; + extents.rotate(getRotation()); + extents.shiftBy(getPosition()); + + return AABox(extents); } void Volume3DOverlay::setProperties(const QScriptValue& properties) { @@ -58,26 +48,30 @@ void Volume3DOverlay::setProperties(const QScriptValue& properties) { QScriptValue z = dimensions.property("z"); - if (x.isValid() && y.isValid() && z.isValid()) { - newDimensions.x = x.toVariant().toFloat(); - newDimensions.y = y.toVariant().toFloat(); - newDimensions.z = z.toVariant().toFloat(); + if (x.isValid() && x.isNumber() && + y.isValid() && y.isNumber() && + z.isValid() && z.isNumber()) { + newDimensions.x = x.toNumber(); + newDimensions.y = y.toNumber(); + newDimensions.z = z.toNumber(); validDimensions = true; } else { QScriptValue width = dimensions.property("width"); QScriptValue height = dimensions.property("height"); QScriptValue depth = dimensions.property("depth"); - if (width.isValid() && height.isValid() && depth.isValid()) { - newDimensions.x = width.toVariant().toFloat(); - newDimensions.y = height.toVariant().toFloat(); - newDimensions.z = depth.toVariant().toFloat(); + if (width.isValid() && width.isNumber() && + height.isValid() && height.isNumber() && + depth.isValid() && depth.isNumber()) { + newDimensions.x = width.toNumber(); + newDimensions.y = height.toNumber(); + newDimensions.z = depth.toNumber(); validDimensions = true; } } // size, scale, dimensions is special, it might just be a single scalar, check that here if (!validDimensions && dimensions.isNumber()) { - float size = dimensions.toVariant().toFloat(); + float size = dimensions.toNumber(); newDimensions.x = size; newDimensions.y = size; newDimensions.z = size; @@ -92,7 +86,7 @@ void Volume3DOverlay::setProperties(const QScriptValue& properties) { QScriptValue Volume3DOverlay::getProperty(const QString& property) { if (property == "dimensions" || property == "scale" || property == "size") { - return vec3toScriptValue(_scriptEngine, _dimensions); + return vec3toScriptValue(_scriptEngine, getDimensions()); } return Base3DOverlay::getProperty(property); @@ -100,24 +94,14 @@ QScriptValue Volume3DOverlay::getProperty(const QString& property) { bool Volume3DOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face) { - // extents is the entity relative, scaled, centered extents of the entity - glm::vec3 position = getPosition(); - glm::mat4 rotation = glm::mat4_cast(getRotation()); - glm::mat4 translation = glm::translate(position); - glm::mat4 entityToWorldMatrix = translation * rotation; - glm::mat4 worldToEntityMatrix = glm::inverse(entityToWorldMatrix); - - glm::vec3 dimensions = _dimensions; - glm::vec3 corner = dimensions * -0.5f; // since we're going to do the ray picking in the overlay frame of reference - AABox overlayFrameBox(corner, dimensions); + glm::mat4 worldToEntityMatrix; + _transform.getInverseMatrix(worldToEntityMatrix); + glm::vec3 overlayFrameOrigin = glm::vec3(worldToEntityMatrix * glm::vec4(origin, 1.0f)); glm::vec3 overlayFrameDirection = glm::vec3(worldToEntityMatrix * glm::vec4(direction, 0.0f)); // we can use the AABox's ray intersection by mapping our origin and direction into the overlays frame // and testing intersection there. - if (overlayFrameBox.findRayIntersection(overlayFrameOrigin, overlayFrameDirection, distance, face)) { - return true; - } - return false; + return _localBoundingBox.findRayIntersection(overlayFrameOrigin, overlayFrameDirection, distance, face); } diff --git a/interface/src/ui/overlays/Volume3DOverlay.h b/interface/src/ui/overlays/Volume3DOverlay.h index b7c59b2ace..ada485a663 100644 --- a/interface/src/ui/overlays/Volume3DOverlay.h +++ b/interface/src/ui/overlays/Volume3DOverlay.h @@ -11,39 +11,29 @@ #ifndef hifi_Volume3DOverlay_h #define hifi_Volume3DOverlay_h -// include this before QGLWidget, which includes an earlier version of OpenGL -#include "InterfaceConfig.h" - -#include - -#include - #include "Base3DOverlay.h" class Volume3DOverlay : public Base3DOverlay { Q_OBJECT public: - Volume3DOverlay(); + Volume3DOverlay() {} Volume3DOverlay(const Volume3DOverlay* volume3DOverlay); - ~Volume3DOverlay(); - - // getters - const glm::vec3& getCenter() const { return _position; } // TODO: consider adding registration point!! - glm::vec3 getCorner() const { return _position - (_dimensions * 0.5f); } // TODO: consider adding registration point!! - const glm::vec3& getDimensions() const { return _dimensions; } - - // setters - void setSize(float size) { _dimensions = glm::vec3(size, size, size); } - void setDimensions(const glm::vec3& value) { _dimensions = value; } + + virtual AABox getBounds() const; + + const glm::vec3& getDimensions() const { return _localBoundingBox.getDimensions(); } + void setDimensions(float value) { _localBoundingBox.setBox(glm::vec3(-value / 2.0f), value); } + void setDimensions(const glm::vec3& value) { _localBoundingBox.setBox(-value / 2.0f, value); } virtual void setProperties(const QScriptValue& properties); virtual QScriptValue getProperty(const QString& property); virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face); - + protected: - glm::vec3 _dimensions; + // Centered local bounding box + AABox _localBoundingBox; };