From 7c350b3acbda71c7dcb39cb7ae3e6c6c074971da Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Sun, 16 Feb 2014 12:30:15 -0800 Subject: [PATCH] add 3D sphere overlay support --- examples/overlaysExample.js | 22 +++++++++ interface/src/ui/Base3DOverlay.cpp | 70 ++++++++++++++++++++++++++++ interface/src/ui/Base3DOverlay.h | 56 ++++++++++++++++++++++ interface/src/ui/Cube3DOverlay.cpp | 56 +--------------------- interface/src/ui/Cube3DOverlay.h | 24 +--------- interface/src/ui/Overlays.cpp | 7 +++ interface/src/ui/Sphere3DOverlay.cpp | 45 ++++++++++++++++++ interface/src/ui/Sphere3DOverlay.h | 37 +++++++++++++++ 8 files changed, 240 insertions(+), 77 deletions(-) create mode 100644 interface/src/ui/Base3DOverlay.cpp create mode 100644 interface/src/ui/Base3DOverlay.h create mode 100644 interface/src/ui/Sphere3DOverlay.cpp create mode 100644 interface/src/ui/Sphere3DOverlay.h diff --git a/examples/overlaysExample.js b/examples/overlaysExample.js index 501f9a248e..01eff71188 100644 --- a/examples/overlaysExample.js +++ b/examples/overlaysExample.js @@ -117,6 +117,20 @@ var solidCube = Overlays.addOverlay("cube", { solid: true }); +var spherePosition = { x: 5, y: 5, z: 5 }; +var sphereSize = 1; +var minSphereSize = 0.5; +var maxSphereSize = 10; +var sphereSizeChange = 0.05; + +var sphere = Overlays.addOverlay("sphere", { + position: spherePosition, + size: sphereSize, + color: { red: 0, green: 0, blue: 255}, + alpha: 1, + solid: false + }); + function scriptEnding() { Overlays.deleteOverlay(toolA); @@ -151,12 +165,20 @@ function update() { } Overlays.editOverlay(cube, { position: cubePosition } ); + // move our solid 3D cube solidCubePosition.x += solidCubeMove; solidCubePosition.z += solidCubeMove; if (solidCubePosition.x > maxSolidCubeX || solidCubePosition.x < minSolidCubeX) { solidCubeMove = solidCubeMove * -1; } Overlays.editOverlay(solidCube, { position: solidCubePosition } ); + + // adjust our 3D sphere + sphereSize += sphereSizeChange; + if (sphereSize > maxSphereSize || sphereSize < minSphereSize) { + sphereSizeChange = sphereSizeChange * -1; + } + Overlays.editOverlay(sphere, { size: sphereSize, solid: (sphereSizeChange < 0) } ); } Script.willSendVisualDataCallback.connect(update); diff --git a/interface/src/ui/Base3DOverlay.cpp b/interface/src/ui/Base3DOverlay.cpp new file mode 100644 index 0000000000..23b02a6ac6 --- /dev/null +++ b/interface/src/ui/Base3DOverlay.cpp @@ -0,0 +1,70 @@ +// +// Base3DOverlay.cpp +// interface +// +// Copyright (c) 2014 High Fidelity, Inc. All rights reserved. +// + +// include this before QGLWidget, which includes an earlier version of OpenGL +#include "InterfaceConfig.h" + +#include +#include + +#include "Base3DOverlay.h" +#include "TextRenderer.h" + +const glm::vec3 DEFAULT_POSITION = glm::vec3(0.0f, 0.0f, 0.0f); +const float DEFAULT_SIZE = 1.0f; +const float DEFAULT_LINE_WIDTH = 1.0f; +const bool DEFAULT_isSolid = false; + +Base3DOverlay::Base3DOverlay() : + _position(DEFAULT_POSITION), + _size(DEFAULT_SIZE), + _lineWidth(DEFAULT_LINE_WIDTH), + _isSolid(DEFAULT_isSolid) +{ +} + +Base3DOverlay::~Base3DOverlay() { +} + +void Base3DOverlay::setProperties(const QScriptValue& properties) { + Overlay::setProperties(properties); + + QScriptValue position = properties.property("position"); + if (position.isValid()) { + QScriptValue x = position.property("x"); + QScriptValue y = position.property("y"); + QScriptValue z = position.property("z"); + if (x.isValid() && y.isValid() && z.isValid()) { + glm::vec3 newPosition; + newPosition.x = x.toVariant().toFloat(); + newPosition.y = y.toVariant().toFloat(); + newPosition.z = z.toVariant().toFloat(); + setPosition(newPosition); + } + } + + if (properties.property("size").isValid()) { + setSize(properties.property("size").toVariant().toFloat()); + } + + if (properties.property("lineWidth").isValid()) { + setLineWidth(properties.property("lineWidth").toVariant().toFloat()); + } + + if (properties.property("isSolid").isValid()) { + setIsSolid(properties.property("isSolid").toVariant().toBool()); + } + if (properties.property("isWire").isValid()) { + setIsSolid(!properties.property("isWire").toVariant().toBool()); + } + if (properties.property("solid").isValid()) { + setIsSolid(properties.property("solid").toVariant().toBool()); + } + if (properties.property("wire").isValid()) { + setIsSolid(!properties.property("wire").toVariant().toBool()); + } +} diff --git a/interface/src/ui/Base3DOverlay.h b/interface/src/ui/Base3DOverlay.h new file mode 100644 index 0000000000..264ca74326 --- /dev/null +++ b/interface/src/ui/Base3DOverlay.h @@ -0,0 +1,56 @@ +// +// Base3DOverlay.h +// interface +// +// Copyright (c) 2014 High Fidelity, Inc. All rights reserved. +// + +#ifndef __interface__Base3DOverlay__ +#define __interface__Base3DOverlay__ + +// 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" + +class Base3DOverlay : public Overlay { + Q_OBJECT + +public: + Base3DOverlay(); + ~Base3DOverlay(); + + // getters + const glm::vec3& getPosition() const { return _position; } + float getSize() const { return _size; } + float getLineWidth() const { return _lineWidth; } + bool getIsSolid() const { return _isSolid; } + + // setters + void setPosition(const glm::vec3& position) { _position = position; } + void setSize(float size) { _size = size; } + void setLineWidth(float lineWidth) { _lineWidth = lineWidth; } + void setIsSolid(bool isSolid) { _isSolid = isSolid; } + + virtual void setProperties(const QScriptValue& properties); + +protected: + glm::vec3 _position; + float _size; + float _lineWidth; + bool _isSolid; +}; + + +#endif /* defined(__interface__Base3DOverlay__) */ diff --git a/interface/src/ui/Cube3DOverlay.cpp b/interface/src/ui/Cube3DOverlay.cpp index dda1f64295..992a18e451 100644 --- a/interface/src/ui/Cube3DOverlay.cpp +++ b/interface/src/ui/Cube3DOverlay.cpp @@ -12,19 +12,8 @@ #include #include "Cube3DOverlay.h" -#include "TextRenderer.h" -const glm::vec3 DEFAULT_POSITION = glm::vec3(0.0f, 0.0f, 0.0f); -const float DEFAULT_SIZE = 1.0f; -const float DEFAULT_LINE_WIDTH = 1.0f; -const bool DEFAULT_isSolid = false; - -Cube3DOverlay::Cube3DOverlay() : - _position(DEFAULT_POSITION), - _size(DEFAULT_SIZE), - _lineWidth(DEFAULT_LINE_WIDTH), - _isSolid(DEFAULT_isSolid) -{ +Cube3DOverlay::Cube3DOverlay() { } Cube3DOverlay::~Cube3DOverlay() { @@ -53,46 +42,3 @@ void Cube3DOverlay::render() { glPopMatrix(); } - -void Cube3DOverlay::setProperties(const QScriptValue& properties) { - Overlay::setProperties(properties); - - QScriptValue position = properties.property("position"); - if (position.isValid()) { - QScriptValue x = position.property("x"); - QScriptValue y = position.property("y"); - QScriptValue z = position.property("z"); - if (x.isValid() && y.isValid() && z.isValid()) { - glm::vec3 newPosition; - newPosition.x = x.toVariant().toFloat(); - newPosition.y = y.toVariant().toFloat(); - newPosition.z = z.toVariant().toFloat(); - setPosition(newPosition); - } - } - - if (properties.property("size").isValid()) { - setSize(properties.property("size").toVariant().toFloat()); - } - - if (properties.property("lineWidth").isValid()) { - setLineWidth(properties.property("lineWidth").toVariant().toFloat()); - } - - if (properties.property("isSolid").isValid()) { - setIsSolid(properties.property("isSolid").toVariant().toBool()); - } - if (properties.property("isWire").isValid()) { - setIsSolid(!properties.property("isWire").toVariant().toBool()); - } - if (properties.property("solid").isValid()) { - setIsSolid(properties.property("solid").toVariant().toBool()); - } - if (properties.property("wire").isValid()) { - setIsSolid(!properties.property("wire").toVariant().toBool()); - } - - -} - - diff --git a/interface/src/ui/Cube3DOverlay.h b/interface/src/ui/Cube3DOverlay.h index ad6ba92d02..0033b9c43f 100644 --- a/interface/src/ui/Cube3DOverlay.h +++ b/interface/src/ui/Cube3DOverlay.h @@ -22,35 +22,15 @@ #include -#include "Overlay.h" +#include "Base3DOverlay.h" -class Cube3DOverlay : public Overlay { +class Cube3DOverlay : public Base3DOverlay { Q_OBJECT public: Cube3DOverlay(); ~Cube3DOverlay(); virtual void render(); - - // getters - const glm::vec3& getPosition() const { return _position; } - float getSize() const { return _size; } - float getLineWidth() const { return _lineWidth; } - bool getIsSolid() const { return _isSolid; } - - // setters - void setPosition(const glm::vec3& position) { _position = position; } - void setSize(float size) { _size = size; } - void setLineWidth(float lineWidth) { _lineWidth = lineWidth; } - void setIsSolid(bool isSolid) { _isSolid = isSolid; } - - virtual void setProperties(const QScriptValue& properties); - -private: - glm::vec3 _position; - float _size; - float _lineWidth; - bool _isSolid; }; diff --git a/interface/src/ui/Overlays.cpp b/interface/src/ui/Overlays.cpp index b731d46b16..902e850fc6 100644 --- a/interface/src/ui/Overlays.cpp +++ b/interface/src/ui/Overlays.cpp @@ -9,6 +9,7 @@ #include "Cube3DOverlay.h" #include "ImageOverlay.h" #include "Overlays.h" +#include "Sphere3DOverlay.h" #include "TextOverlay.h" @@ -59,6 +60,12 @@ unsigned int Overlays::addOverlay(const QString& type, const QScriptValue& prope thisOverlay->setProperties(properties); created = true; is3D = true; + } else if (type == "sphere") { + thisOverlay = new Sphere3DOverlay(); + thisOverlay->init(_parent); + thisOverlay->setProperties(properties); + created = true; + is3D = true; } if (created) { diff --git a/interface/src/ui/Sphere3DOverlay.cpp b/interface/src/ui/Sphere3DOverlay.cpp new file mode 100644 index 0000000000..7fded5bedb --- /dev/null +++ b/interface/src/ui/Sphere3DOverlay.cpp @@ -0,0 +1,45 @@ +// +// Sphere3DOverlay.cpp +// interface +// +// Copyright (c) 2014 High Fidelity, Inc. All rights reserved. +// + +// include this before QGLWidget, which includes an earlier version of OpenGL +#include "InterfaceConfig.h" + +#include +#include + +#include "Sphere3DOverlay.h" + +Sphere3DOverlay::Sphere3DOverlay() { +} + +Sphere3DOverlay::~Sphere3DOverlay() { +} + +void Sphere3DOverlay::render() { + if (!_visible) { + return; // do nothing if we're not visible + } + + const float MAX_COLOR = 255; + glColor4f(_color.red / MAX_COLOR, _color.green / MAX_COLOR, _color.blue / MAX_COLOR, _alpha); + + + glDisable(GL_LIGHTING); + glPushMatrix(); + glTranslatef(_position.x + _size * 0.5f, + _position.y + _size * 0.5f, + _position.z + _size * 0.5f); + glLineWidth(_lineWidth); + const int slices = 15; + if (_isSolid) { + glutSolidSphere(_size, slices, slices); + } else { + glutWireSphere(_size, slices, slices); + } + glPopMatrix(); + +} diff --git a/interface/src/ui/Sphere3DOverlay.h b/interface/src/ui/Sphere3DOverlay.h new file mode 100644 index 0000000000..03210866e8 --- /dev/null +++ b/interface/src/ui/Sphere3DOverlay.h @@ -0,0 +1,37 @@ +// +// Sphere3DOverlay.h +// interface +// +// Copyright (c) 2014 High Fidelity, Inc. All rights reserved. +// + +#ifndef __interface__Sphere3DOverlay__ +#define __interface__Sphere3DOverlay__ + +// include this before QGLWidget, which includes an earlier version of OpenGL +#include "InterfaceConfig.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "Base3DOverlay.h" + +class Sphere3DOverlay : public Base3DOverlay { + Q_OBJECT + +public: + Sphere3DOverlay(); + ~Sphere3DOverlay(); + virtual void render(); +}; + + +#endif /* defined(__interface__Sphere3DOverlay__) */