3d overlays can be children of entities or avatars

This commit is contained in:
Seth Alves 2016-07-31 14:11:56 -07:00
parent f71a59276c
commit e4e86c245e
14 changed files with 193 additions and 80 deletions

View file

@ -21,6 +21,7 @@ const bool DEFAULT_IS_SOLID = false;
const bool DEFAULT_IS_DASHED_LINE = false;
Base3DOverlay::Base3DOverlay() :
SpatiallyNestable(NestableType::Overlay, QUuid::createUuid()),
_lineWidth(DEFAULT_LINE_WIDTH),
_isSolid(DEFAULT_IS_SOLID),
_isDashedLine(DEFAULT_IS_DASHED_LINE),
@ -31,15 +32,68 @@ Base3DOverlay::Base3DOverlay() :
Base3DOverlay::Base3DOverlay(const Base3DOverlay* base3DOverlay) :
Overlay(base3DOverlay),
_transform(base3DOverlay->_transform),
SpatiallyNestable(NestableType::Overlay, QUuid::createUuid()),
_lineWidth(base3DOverlay->_lineWidth),
_isSolid(base3DOverlay->_isSolid),
_isDashedLine(base3DOverlay->_isDashedLine),
_ignoreRayIntersection(base3DOverlay->_ignoreRayIntersection),
_drawInFront(base3DOverlay->_drawInFront)
{
setTransform(base3DOverlay->getTransform());
}
void Base3DOverlay::setProperties(const QVariantMap& properties) {
QVariantMap convertOverlayLocationFromScriptSemantics(const QVariantMap& properties,
const QUuid& currentParentID,
int currentParentJointIndex) {
// the position and rotation in _transform are relative to the parent (aka local). The versions coming from
// scripts are in world-frame, unless localPosition or localRotation are used. Patch up the properties
// so that "position" and "rotation" are relative-to-parent values.
QVariantMap result = properties;
QUuid parentID = result["parentID"].isValid() ? QUuid(result["parentID"].toString()) : currentParentID;
int parentJointIndex =
result["parentJointIndex"].isValid() ? result["parentJointIndex"].toInt() : currentParentJointIndex;
bool success;
// carry over some legacy keys
if (!result["position"].isValid() && !result["localPosition"].isValid()) {
if (result["p1"].isValid()) {
result["position"] = result["p1"];
} else if (result["point"].isValid()) {
result["position"] = result["point"];
} else if (result["start"].isValid()) {
result["position"] = result["start"];
}
}
if (!result["orientation"].isValid() && result["rotation"].isValid()) {
result["orientation"] = result["rotation"];
}
if (!result["localOrientation"].isValid() && result["localRotation"].isValid()) {
result["localOrientation"] = result["localRotation"];
}
// make "position" and "orientation" be relative-to-parent
if (result["localPosition"].isValid()) {
result["position"] = result["localPosition"];
} else if (result["position"].isValid()) {
glm::vec3 localPosition = SpatiallyNestable::worldToLocal(vec3FromVariant(result["position"]),
parentID, parentJointIndex, success);
result["position"] = vec3toVariant(localPosition);
}
if (result["localOrientation"].isValid()) {
result["orientation"] = result["localOrientation"];
} else if (result["orientation"].isValid()) {
glm::quat localOrientation = SpatiallyNestable::worldToLocal(quatFromVariant(result["orientation"]),
parentID, parentJointIndex, success);
result["orientation"] = quatToVariant(localOrientation);
}
return result;
}
void Base3DOverlay::setProperties(const QVariantMap& originalProperties) {
QVariantMap properties =
convertOverlayLocationFromScriptSemantics(originalProperties, getParentID(), getParentJointIndex());
Overlay::setProperties(properties);
bool needRenderItemUpdate = false;
@ -52,17 +106,12 @@ void Base3DOverlay::setProperties(const QVariantMap& properties) {
needRenderItemUpdate = true;
}
auto position = properties["position"];
// if "position" property was not there, check to see if they included aliases: point, p1
if (!position.isValid()) {
position = properties["p1"];
if (!position.isValid()) {
position = properties["point"];
}
if (properties["position"].isValid()) {
setPosition(vec3FromVariant(properties["position"]));
needRenderItemUpdate = true;
}
if (position.isValid()) {
setPosition(vec3FromVariant(position));
if (properties["orientation"].isValid()) {
setOrientation(quatFromVariant(properties["orientation"]));
needRenderItemUpdate = true;
}
@ -71,13 +120,6 @@ void Base3DOverlay::setProperties(const QVariantMap& properties) {
needRenderItemUpdate = true;
}
auto rotation = properties["rotation"];
if (rotation.isValid()) {
setRotation(quatFromVariant(rotation));
needRenderItemUpdate = true;
}
if (properties["isSolid"].isValid()) {
setIsSolid(properties["isSolid"].toBool());
}
@ -107,6 +149,13 @@ void Base3DOverlay::setProperties(const QVariantMap& properties) {
setIgnoreRayIntersection(properties["ignoreRayIntersection"].toBool());
}
if (properties["parentID"].isValid()) {
setParentID(QUuid(properties["parentID"].toString()));
}
if (properties["parentJointIndex"].isValid()) {
setParentJointIndex(properties["parentJointIndex"].toInt());
}
// Communicate changes to the renderItem if needed
if (needRenderItemUpdate) {
auto itemID = getRenderItemID();
@ -123,12 +172,18 @@ QVariant Base3DOverlay::getProperty(const QString& property) {
if (property == "position" || property == "start" || property == "p1" || property == "point") {
return vec3toVariant(getPosition());
}
if (property == "localPosition") {
return vec3toVariant(getLocalPosition());
}
if (property == "rotation" || property == "orientation") {
return quatToVariant(getOrientation());
}
if (property == "localRotation" || property == "localOrientation") {
return quatToVariant(getLocalOrientation());
}
if (property == "lineWidth") {
return _lineWidth;
}
if (property == "rotation") {
return quatToVariant(getRotation());
}
if (property == "isSolid" || property == "isFilled" || property == "solid" || property == "filed") {
return _isSolid;
}
@ -144,6 +199,12 @@ QVariant Base3DOverlay::getProperty(const QString& property) {
if (property == "drawInFront") {
return _drawInFront;
}
if (property == "parentID") {
return getParentID();
}
if (property == "parentJointIndex") {
return getParentJointIndex();
}
return Overlay::getProperty(property);
}
@ -152,3 +213,15 @@ bool Base3DOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3
float& distance, BoxFace& face, glm::vec3& surfaceNormal) {
return false;
}
void Base3DOverlay::locationChanged(bool tellPhysics) {
auto itemID = getRenderItemID();
if (render::Item::isValidID(itemID)) {
render::ScenePointer scene = qApp->getMain3DScene();
render::PendingChanges pendingChanges;
pendingChanges.updateItem(itemID);
scene->enqueuePendingChanges(pendingChanges);
}
// Overlays can't currently have children.
// SpatiallyNestable::locationChanged(tellPhysics); // tell all the children, also
}

View file

@ -12,10 +12,11 @@
#define hifi_Base3DOverlay_h
#include <Transform.h>
#include <SpatiallyNestable.h>
#include "Overlay.h"
class Base3DOverlay : public Overlay {
class Base3DOverlay : public Overlay, public SpatiallyNestable {
Q_OBJECT
public:
@ -24,12 +25,9 @@ public:
// getters
virtual bool is3D() const override { 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(); }
glm::vec3 getCenter() const { return getPosition(); }
float getLineWidth() const { return _lineWidth; }
bool getIsSolid() const { return _isSolid; }
@ -38,12 +36,6 @@ public:
bool getIgnoreRayIntersection() const { return _ignoreRayIntersection; }
bool getDrawInFront() const { return _drawInFront; }
// 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; }
void setIsDashedLine(bool isDashedLine) { _isDashedLine = isDashedLine; }
@ -64,7 +56,7 @@ public:
}
protected:
Transform _transform;
virtual void locationChanged(bool tellPhysics = true) override;
float _lineWidth;
bool _isSolid;

View file

@ -69,7 +69,7 @@ void Circle3DOverlay::render(RenderArgs* args) {
// FIXME: THe line width of _lineWidth is not supported anymore, we ll need a workaround
auto transform = _transform;
auto transform = getTransform();
transform.postScale(glm::vec3(getDimensions(), 1.0f));
batch.setModelTransform(transform);

View file

@ -37,7 +37,9 @@ Image3DOverlay::Image3DOverlay(const Image3DOverlay* image3DOverlay) :
}
void Image3DOverlay::update(float deltatime) {
applyTransformTo(_transform);
Transform transform = getTransform();
applyTransformTo(transform);
setTransform(transform);
}
void Image3DOverlay::render(RenderArgs* args) {
@ -86,8 +88,9 @@ void Image3DOverlay::render(RenderArgs* args) {
xColor color = getColor();
float alpha = getAlpha();
applyTransformTo(_transform, true);
Transform transform = _transform;
Transform transform = getTransform();
applyTransformTo(transform, true);
setTransform(transform);
transform.postScale(glm::vec3(getDimensions(), 1.0f));
batch->setModelTransform(transform);
@ -187,7 +190,9 @@ bool Image3DOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec
float& distance, BoxFace& face, glm::vec3& surfaceNormal) {
if (_texture && _texture->isLoaded()) {
// Make sure position and rotation is updated.
applyTransformTo(_transform, true);
Transform transform;
applyTransformTo(transform, true);
setTransform(transform);
// Produce the dimensions of the overlay based on the image's aspect ratio and the overlay's scale.
bool isNull = _fromImage.isNull();

View file

@ -35,7 +35,7 @@ AABox Line3DOverlay::getBounds() const {
auto extents = Extents{};
extents.addPoint(_start);
extents.addPoint(_end);
extents.transform(_transform);
extents.transform(getTransform());
return AABox(extents);
}
@ -52,7 +52,7 @@ void Line3DOverlay::render(RenderArgs* args) {
auto batch = args->_batch;
if (batch) {
batch->setModelTransform(_transform);
batch->setModelTransform(getTransform());
auto geometryCache = DependencyManager::get<GeometryCache>();
if (getIsDashedLine()) {

View file

@ -30,7 +30,7 @@ AABox Planar3DOverlay::getBounds() const {
auto halfDimensions = glm::vec3{_dimensions / 2.0f, 0.01f};
auto extents = Extents{-halfDimensions, halfDimensions};
extents.transform(_transform);
extents.transform(getTransform());
return AABox(extents);
}

View file

@ -39,7 +39,7 @@ void Sphere3DOverlay::render(RenderArgs* args) {
auto batch = args->_batch;
if (batch) {
Transform transform = _transform;
Transform transform = getTransform();
transform.postScale(getDimensions() * SPHERE_OVERLAY_SCALE);
batch->setModelTransform(transform);

View file

@ -65,7 +65,9 @@ xColor Text3DOverlay::getBackgroundColor() {
}
void Text3DOverlay::update(float deltatime) {
applyTransformTo(_transform);
Transform transform = getTransform();
applyTransformTo(transform);
setTransform(transform);
}
void Text3DOverlay::render(RenderArgs* args) {
@ -76,8 +78,10 @@ void Text3DOverlay::render(RenderArgs* args) {
Q_ASSERT(args->_batch);
auto& batch = *args->_batch;
applyTransformTo(_transform, true);
batch.setModelTransform(_transform);
Transform transform = getTransform();
applyTransformTo(transform, true);
setTransform(transform);
batch.setModelTransform(transform);
const float MAX_COLOR = 255.0f;
xColor backgroundColor = getBackgroundColor();
@ -102,7 +106,6 @@ void Text3DOverlay::render(RenderArgs* args) {
glm::vec2 clipDimensions((dimensions.x - (_leftMargin + _rightMargin)) / scaleFactor,
(dimensions.y - (_topMargin + _bottomMargin)) / scaleFactor);
Transform transform = _transform;
transform.postTranslate(glm::vec3(-(halfDimensions.x - _leftMargin),
halfDimensions.y - _topMargin, 0.001f));
transform.setScale(scaleFactor);
@ -222,6 +225,8 @@ QSizeF Text3DOverlay::textSize(const QString& text) const {
bool Text3DOverlay::findRayIntersection(const glm::vec3 &origin, const glm::vec3 &direction, float &distance,
BoxFace &face, glm::vec3& surfaceNormal) {
applyTransformTo(_transform, true);
Transform transform = getTransform();
applyTransformTo(transform, true);
setTransform(transform);
return Billboard3DOverlay::findRayIntersection(origin, direction, distance, face, surfaceNormal);
}

View file

@ -56,7 +56,7 @@ bool Volume3DOverlay::findRayIntersection(const glm::vec3& origin, const glm::ve
float& distance, BoxFace& face, glm::vec3& surfaceNormal) {
// extents is the entity relative, scaled, centered extents of the entity
glm::mat4 worldToEntityMatrix;
_transform.getInverseMatrix(worldToEntityMatrix);
getTransform().getInverseMatrix(worldToEntityMatrix);
glm::vec3 overlayFrameOrigin = glm::vec3(worldToEntityMatrix * glm::vec4(origin, 1.0f));
glm::vec3 overlayFrameDirection = glm::vec3(worldToEntityMatrix * glm::vec4(direction, 0.0f));

View file

@ -57,7 +57,9 @@ Web3DOverlay::~Web3DOverlay() {
}
void Web3DOverlay::update(float deltatime) {
applyTransformTo(_transform);
Transform transform = getTransform();
applyTransformTo(transform);
setTransform(transform);
}
void Web3DOverlay::render(RenderArgs* args) {
@ -85,8 +87,9 @@ void Web3DOverlay::render(RenderArgs* args) {
vec2 halfSize = size / 2.0f;
vec4 color(toGlm(getColor()), getAlpha());
applyTransformTo(_transform, true);
Transform transform = _transform;
Transform transform = getTransform();
applyTransformTo(transform, true);
setTransform(transform);
if (glm::length2(getDimensions()) != 1.0f) {
transform.postScale(vec3(getDimensions(), 1.0f));
}
@ -165,7 +168,10 @@ bool Web3DOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3&
// FIXME - face and surfaceNormal not being returned
// Make sure position and rotation is updated.
applyTransformTo(_transform, true);
Transform transform;
applyTransformTo(transform, true);
setTransform(transform);
vec2 size = _resolution / _dpi * INCHES_TO_METERS * vec2(getDimensions());
// Produce the dimensions of the overlay based on the image's aspect ratio and the overlay's scale.
return findRayRectangleIntersection(origin, direction, getRotation(), getPosition(), size, distance);

View file

@ -321,10 +321,6 @@ public:
/// return preferred shape type (actual physical shape may differ)
virtual ShapeType getShapeType() const { return SHAPE_TYPE_NONE; }
// these are only needed because the names don't match
virtual const glm::quat getRotation() const { return getOrientation(); }
virtual void setRotation(glm::quat orientation) { setOrientation(orientation); }
// updateFoo() methods to be used when changes need to be accumulated in the _dirtyFlags
virtual void updateRegistrationPoint(const glm::vec3& value);
void updatePosition(const glm::vec3& value);

View file

@ -513,6 +513,15 @@ const Transform SpatiallyNestable::getTransform(bool& success, int depth) const
return result;
}
const Transform SpatiallyNestable::getTransform() const {
bool success;
Transform result = getTransform(success);
if (!success) {
qDebug() << "getTransform failed for" << getID();
}
return result;
}
const Transform SpatiallyNestable::getTransform(int jointIndex, bool& success, int depth) const {
// this returns the world-space transform for this object. It finds its parent's transform (which may
// cause this object's parent to query its parent, etc) and multiplies this object's local transform onto it.
@ -558,6 +567,12 @@ void SpatiallyNestable::setTransform(const Transform& transform, bool& success)
}
}
bool SpatiallyNestable::setTransform(const Transform& transform) {
bool success;
setTransform(transform, success);
return success;
}
glm::vec3 SpatiallyNestable::getScale() const {
// TODO: scale
glm::vec3 result;
@ -575,7 +590,7 @@ glm::vec3 SpatiallyNestable::getScale(int jointIndex) const {
void SpatiallyNestable::setScale(const glm::vec3& scale) {
// guard against introducing NaN into the transform
if (isNaN(scale)) {
qDebug() << "SpatiallyNestable::setLocalScale -- scale contains NaN";
qDebug() << "SpatiallyNestable::setScale -- scale contains NaN";
return;
}
// TODO: scale
@ -585,6 +600,19 @@ void SpatiallyNestable::setScale(const glm::vec3& scale) {
dimensionsChanged();
}
void SpatiallyNestable::setScale(float value) {
// guard against introducing NaN into the transform
if (value <= 0.0f) {
qDebug() << "SpatiallyNestable::setScale -- scale is zero or negative value";
return;
}
// TODO: scale
_transformLock.withWriteLock([&] {
_transform.setScale(value);
});
dimensionsChanged();
}
const Transform SpatiallyNestable::getLocalTransform() const {
Transform result;
_transformLock.withReadLock([&] {

View file

@ -28,7 +28,8 @@ using SpatiallyNestableConstPointer = std::shared_ptr<const SpatiallyNestable>;
enum class NestableType {
Entity,
Avatar
Avatar,
Overlay
};
class SpatiallyNestable : public std::enable_shared_from_this<SpatiallyNestable> {
@ -53,7 +54,9 @@ public:
// world frame
virtual const Transform getTransform(bool& success, int depth = 0) const;
virtual const Transform getTransform() const;
virtual void setTransform(const Transform& transform, bool& success);
virtual bool setTransform(const Transform& transform);
virtual Transform getParentTransform(bool& success, int depth = 0) const;
@ -68,6 +71,10 @@ public:
virtual void setOrientation(const glm::quat& orientation, bool& success, bool tellPhysics = true);
virtual void setOrientation(const glm::quat& orientation);
// these are here because some older code uses rotation rather than orientation
virtual const glm::quat getRotation() const { return getOrientation(); }
virtual void setRotation(glm::quat orientation) { setOrientation(orientation); }
virtual glm::vec3 getVelocity(bool& success) const;
virtual glm::vec3 getVelocity() const;
virtual void setVelocity(const glm::vec3& velocity, bool& success);
@ -91,6 +98,7 @@ public:
virtual glm::vec3 getScale() const;
virtual void setScale(const glm::vec3& scale);
virtual void setScale(float value);
// get world-frame values for a specific joint
virtual const Transform getTransform(int jointIndex, bool& success, int depth = 0) const;
@ -123,10 +131,10 @@ public:
// this object's frame
virtual const Transform getAbsoluteJointTransformInObjectFrame(int jointIndex) const;
virtual glm::quat getAbsoluteJointRotationInObjectFrame(int index) const = 0;
virtual glm::vec3 getAbsoluteJointTranslationInObjectFrame(int index) const = 0;
virtual bool setAbsoluteJointRotationInObjectFrame(int index, const glm::quat& rotation) = 0;
virtual bool setAbsoluteJointTranslationInObjectFrame(int index, const glm::vec3& translation) = 0;
virtual glm::quat getAbsoluteJointRotationInObjectFrame(int index) const { return glm::quat(); }
virtual glm::vec3 getAbsoluteJointTranslationInObjectFrame(int index) const { return glm::vec3(); }
virtual bool setAbsoluteJointRotationInObjectFrame(int index, const glm::quat& rotation) { return false; }
virtual bool setAbsoluteJointTranslationInObjectFrame(int index, const glm::vec3& translation) {return false; }
SpatiallyNestablePointer getThisPointer() const;