diff --git a/assignment-client/src/metavoxels/MetavoxelServer.cpp b/assignment-client/src/metavoxels/MetavoxelServer.cpp index 98c91d44e7..23c8f2189c 100644 --- a/assignment-client/src/metavoxels/MetavoxelServer.cpp +++ b/assignment-client/src/metavoxels/MetavoxelServer.cpp @@ -95,6 +95,8 @@ MetavoxelSession::MetavoxelSession(MetavoxelServer* server, const SharedNodePoin connect(&_sequencer, SIGNAL(readyToRead(Bitstream&)), SLOT(readPacket(Bitstream&))); connect(&_sequencer, SIGNAL(sendAcknowledged(int)), SLOT(clearSendRecordsBefore(int))); connect(&_sequencer, SIGNAL(receivedHighPriorityMessage(const QVariant&)), SLOT(handleMessage(const QVariant&))); + connect(_sequencer.getReliableInputChannel(), SIGNAL(receivedMessage(const QVariant&)), + SLOT(handleMessage(const QVariant&))); // insert the baseline send record SendRecord record = { 0 }; diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 15d45b614d..9788b88163 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2229,8 +2229,7 @@ void Application::updateShadowMap() { glRotatef(glm::degrees(glm::angle(inverseRotation)), axis.x, axis.y, axis.z); // store view matrix without translation, which we'll use for precision-sensitive objects - glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)&_untranslatedViewMatrix); - _viewMatrixTranslation = glm::vec3(); + updateUntranslatedViewMatrix(); _avatarManager.renderAvatars(Avatar::SHADOW_RENDER_MODE); _particles.render(); @@ -2315,8 +2314,7 @@ void Application::displaySide(Camera& whichCamera, bool selfAvatarOnly) { glRotatef(-glm::degrees(glm::angle(rotation)), axis.x, axis.y, axis.z); // store view matrix without translation, which we'll use for precision-sensitive objects - glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)&_untranslatedViewMatrix); - _viewMatrixTranslation = -whichCamera.getPosition(); + updateUntranslatedViewMatrix(-whichCamera.getPosition()); glTranslatef(_viewMatrixTranslation.x, _viewMatrixTranslation.y, _viewMatrixTranslation.z); @@ -2447,6 +2445,11 @@ void Application::displaySide(Camera& whichCamera, bool selfAvatarOnly) { } } +void Application::updateUntranslatedViewMatrix(const glm::vec3& viewMatrixTranslation) { + glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)&_untranslatedViewMatrix); + _viewMatrixTranslation = viewMatrixTranslation; +} + void Application::loadTranslatedViewMatrix(const glm::vec3& translation) { glLoadMatrixf((const GLfloat*)&_untranslatedViewMatrix); glTranslatef(translation.x + _viewMatrixTranslation.x, translation.y + _viewMatrixTranslation.y, diff --git a/interface/src/Application.h b/interface/src/Application.h index fd2d6f2dfa..625974d0bd 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -203,6 +203,10 @@ public: void displaySide(Camera& whichCamera, bool selfAvatarOnly = false); + /// Stores the current modelview matrix as the untranslated view matrix to use for transforms and the supplied vector as + /// the view matrix translation. + void updateUntranslatedViewMatrix(const glm::vec3& viewMatrixTranslation = glm::vec3()); + /// Loads a view matrix that incorporates the specified model translation without the precision issues that can /// result from matrix multiplication at high translation magnitudes. void loadTranslatedViewMatrix(const glm::vec3& translation); diff --git a/interface/src/Menu.cpp b/interface/src/Menu.cpp index 5deb9474cf..06fe89f955 100644 --- a/interface/src/Menu.cpp +++ b/interface/src/Menu.cpp @@ -183,7 +183,7 @@ Menu::Menu() : #ifdef HAVE_QXMPP _chatAction = addActionToQMenuAndActionHash(toolsMenu, MenuOption::Chat, - Qt::Key_Return, + 0, this, SLOT(showChat())); diff --git a/interface/src/MetavoxelSystem.cpp b/interface/src/MetavoxelSystem.cpp index 042c9329f2..6b4d817f7f 100644 --- a/interface/src/MetavoxelSystem.cpp +++ b/interface/src/MetavoxelSystem.cpp @@ -69,13 +69,13 @@ SharedObjectPointer MetavoxelSystem::findFirstRaySpannerIntersection( return closestSpanner; } -void MetavoxelSystem::applyEdit(const MetavoxelEditMessage& edit) { +void MetavoxelSystem::applyEdit(const MetavoxelEditMessage& edit, bool reliable) { foreach (const SharedNodePointer& node, NodeList::getInstance()->getNodeHash()) { if (node->getType() == NodeType::MetavoxelServer) { QMutexLocker locker(&node->getMutex()); MetavoxelClient* client = static_cast(node->getLinkedData()); if (client) { - client->applyEdit(edit); + client->applyEdit(edit, reliable); } } } @@ -228,7 +228,7 @@ MetavoxelSystem::RenderVisitor::RenderVisitor() : } bool MetavoxelSystem::RenderVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) { - spanner->getRenderer()->render(1.0f, clipMinimum, clipSize); + spanner->getRenderer()->render(1.0f, SpannerRenderer::DEFAULT_MODE, clipMinimum, clipSize); return true; } @@ -267,12 +267,17 @@ void MetavoxelClient::guide(MetavoxelVisitor& visitor) { _data.guide(visitor); } -void MetavoxelClient::applyEdit(const MetavoxelEditMessage& edit) { - // apply immediately to local tree - edit.apply(_data, _sequencer.getWeakSharedObjectHash()); +void MetavoxelClient::applyEdit(const MetavoxelEditMessage& edit, bool reliable) { + if (reliable) { + _sequencer.getReliableOutputChannel()->sendMessage(QVariant::fromValue(edit)); + + } else { + // apply immediately to local tree + edit.apply(_data, _sequencer.getWeakSharedObjectHash()); - // start sending it out - _sequencer.sendHighPriorityMessage(QVariant::fromValue(edit)); + // start sending it out + _sequencer.sendHighPriorityMessage(QVariant::fromValue(edit)); + } } void MetavoxelClient::simulate(float deltaTime) { @@ -290,11 +295,13 @@ void MetavoxelClient::simulate(float deltaTime) { int MetavoxelClient::parseData(const QByteArray& packet) { // process through sequencer QMetaObject::invokeMethod(&_sequencer, "receivedDatagram", Q_ARG(const QByteArray&, packet)); + Application::getInstance()->getBandwidthMeter()->inputStream(BandwidthMeter::METAVOXELS).updateValue(packet.size()); return packet.size(); } void MetavoxelClient::sendData(const QByteArray& data) { NodeList::getInstance()->writeDatagram(data, _node); + Application::getInstance()->getBandwidthMeter()->outputStream(BandwidthMeter::METAVOXELS).updateValue(data.size()); } void MetavoxelClient::readPacket(Bitstream& in) { @@ -340,9 +347,9 @@ static void enableClipPlane(GLenum plane, float x, float y, float z, float w) { glEnable(plane); } -void ClippedRenderer::render(float alpha, const glm::vec3& clipMinimum, float clipSize) { +void ClippedRenderer::render(float alpha, Mode mode, const glm::vec3& clipMinimum, float clipSize) { if (clipSize == 0.0f) { - renderUnclipped(alpha); + renderUnclipped(alpha, mode); return; } enableClipPlane(GL_CLIP_PLANE0, -1.0f, 0.0f, 0.0f, clipMinimum.x + clipSize); @@ -352,7 +359,7 @@ void ClippedRenderer::render(float alpha, const glm::vec3& clipMinimum, float cl enableClipPlane(GL_CLIP_PLANE4, 0.0f, 0.0f, -1.0f, clipMinimum.z + clipSize); enableClipPlane(GL_CLIP_PLANE5, 0.0f, 0.0f, 1.0f, -clipMinimum.z); - renderUnclipped(alpha); + renderUnclipped(alpha, mode); glDisable(GL_CLIP_PLANE0); glDisable(GL_CLIP_PLANE1); @@ -365,9 +372,9 @@ void ClippedRenderer::render(float alpha, const glm::vec3& clipMinimum, float cl SphereRenderer::SphereRenderer() { } -void SphereRenderer::render(float alpha, const glm::vec3& clipMinimum, float clipSize) { +void SphereRenderer::render(float alpha, Mode mode, const glm::vec3& clipMinimum, float clipSize) { if (clipSize == 0.0f) { - renderUnclipped(alpha); + renderUnclipped(alpha, mode); return; } // slight performance optimization: don't render if clip bounds are entirely within sphere @@ -376,13 +383,13 @@ void SphereRenderer::render(float alpha, const glm::vec3& clipMinimum, float cli for (int i = 0; i < Box::VERTEX_COUNT; i++) { const float CLIP_PROPORTION = 0.95f; if (glm::distance(sphere->getTranslation(), clipBox.getVertex(i)) >= sphere->getScale() * CLIP_PROPORTION) { - ClippedRenderer::render(alpha, clipMinimum, clipSize); + ClippedRenderer::render(alpha, mode, clipMinimum, clipSize); return; } } } -void SphereRenderer::renderUnclipped(float alpha) { +void SphereRenderer::renderUnclipped(float alpha, Mode mode) { Sphere* sphere = static_cast(parent()); const QColor& color = sphere->getColor(); glColor4f(color.redF(), color.greenF(), color.blueF(), color.alphaF() * alpha); @@ -390,7 +397,7 @@ void SphereRenderer::renderUnclipped(float alpha) { glPushMatrix(); const glm::vec3& translation = sphere->getTranslation(); glTranslatef(translation.x, translation.y, translation.z); - glm::quat rotation = glm::quat(glm::radians(sphere->getRotation())); + glm::quat rotation = sphere->getRotation(); glm::vec3 axis = glm::axis(rotation); glRotatef(glm::angle(rotation), axis.x, axis.y, axis.z); @@ -413,7 +420,7 @@ void StaticModelRenderer::init(Spanner* spanner) { applyURL(staticModel->getURL()); connect(spanner, SIGNAL(translationChanged(const glm::vec3&)), SLOT(applyTranslation(const glm::vec3&))); - connect(spanner, SIGNAL(rotationChanged(const glm::vec3&)), SLOT(applyRotation(const glm::vec3&))); + connect(spanner, SIGNAL(rotationChanged(const glm::quat&)), SLOT(applyRotation(const glm::quat&))); connect(spanner, SIGNAL(scaleChanged(float)), SLOT(applyScale(float))); connect(spanner, SIGNAL(urlChanged(const QUrl&)), SLOT(applyURL(const QUrl&))); } @@ -430,7 +437,20 @@ void StaticModelRenderer::simulate(float deltaTime) { _model->simulate(deltaTime); } -void StaticModelRenderer::renderUnclipped(float alpha) { +void StaticModelRenderer::renderUnclipped(float alpha, Mode mode) { + switch (mode) { + case DIFFUSE_MODE: + _model->render(alpha, Model::DIFFUSE_RENDER_MODE); + break; + + case NORMAL_MODE: + _model->render(alpha, Model::NORMAL_RENDER_MODE); + break; + + default: + _model->render(alpha); + break; + } _model->render(alpha); } @@ -443,8 +463,8 @@ void StaticModelRenderer::applyTranslation(const glm::vec3& translation) { _model->setTranslation(translation); } -void StaticModelRenderer::applyRotation(const glm::vec3& rotation) { - _model->setRotation(glm::quat(glm::radians(rotation))); +void StaticModelRenderer::applyRotation(const glm::quat& rotation) { + _model->setRotation(rotation); } void StaticModelRenderer::applyScale(float scale) { diff --git a/interface/src/MetavoxelSystem.h b/interface/src/MetavoxelSystem.h index 58f40dad37..ecab30d535 100644 --- a/interface/src/MetavoxelSystem.h +++ b/interface/src/MetavoxelSystem.h @@ -38,7 +38,7 @@ public: SharedObjectPointer findFirstRaySpannerIntersection(const glm::vec3& origin, const glm::vec3& direction, const AttributePointer& attribute, float& distance); - void applyEdit(const MetavoxelEditMessage& edit); + Q_INVOKABLE void applyEdit(const MetavoxelEditMessage& edit, bool reliable = false); void simulate(float deltaTime); void render(); @@ -98,7 +98,7 @@ public: void guide(MetavoxelVisitor& visitor); - void applyEdit(const MetavoxelEditMessage& edit); + void applyEdit(const MetavoxelEditMessage& edit, bool reliable = false); void simulate(float deltaTime); @@ -147,11 +147,11 @@ class ClippedRenderer : public SpannerRenderer { public: - virtual void render(float alpha, const glm::vec3& clipMinimum, float clipSize); + virtual void render(float alpha, Mode mode, const glm::vec3& clipMinimum, float clipSize); protected: - virtual void renderUnclipped(float alpha) = 0; + virtual void renderUnclipped(float alpha, Mode mode) = 0; }; /// Renders spheres. @@ -162,11 +162,11 @@ public: Q_INVOKABLE SphereRenderer(); - virtual void render(float alpha, const glm::vec3& clipMinimum, float clipSize); + virtual void render(float alpha, Mode mode, const glm::vec3& clipMinimum, float clipSize); protected: - virtual void renderUnclipped(float alpha); + virtual void renderUnclipped(float alpha, Mode mode); }; /// Renders static models. @@ -184,12 +184,12 @@ public: protected: - virtual void renderUnclipped(float alpha); + virtual void renderUnclipped(float alpha, Mode mode); private slots: void applyTranslation(const glm::vec3& translation); - void applyRotation(const glm::vec3& eulerAngles); // eulerAngles are in degrees + void applyRotation(const glm::quat& rotation); void applyScale(float scale); void applyURL(const QUrl& url); diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index 41f26258ff..05f74755df 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -326,8 +326,10 @@ void Avatar::renderBody(RenderMode renderMode) { renderBillboard(); return; } - _skeletonModel.render(1.0f, renderMode == SHADOW_RENDER_MODE); - getHead()->render(1.0f, renderMode == SHADOW_RENDER_MODE); + Model::RenderMode modelRenderMode = (renderMode == SHADOW_RENDER_MODE) ? + Model::SHADOW_RENDER_MODE : Model::DEFAULT_RENDER_MODE; + _skeletonModel.render(1.0f, modelRenderMode); + getHead()->render(1.0f, modelRenderMode); getHand()->render(false); } diff --git a/interface/src/avatar/Head.cpp b/interface/src/avatar/Head.cpp index 8a25c14574..f42220c0f4 100644 --- a/interface/src/avatar/Head.cpp +++ b/interface/src/avatar/Head.cpp @@ -180,8 +180,8 @@ void Head::relaxLean(float deltaTime) { _deltaLeanForward *= relaxationFactor; } -void Head::render(float alpha, bool forShadowMap) { - if (_faceModel.render(alpha, forShadowMap) && _renderLookatVectors) { +void Head::render(float alpha, Model::RenderMode mode) { + if (_faceModel.render(alpha, mode) && _renderLookatVectors) { renderLookatVectors(_leftEyePosition, _rightEyePosition, _lookAtPosition); } } diff --git a/interface/src/avatar/Head.h b/interface/src/avatar/Head.h index 8a03cfc7ad..36ab6a9229 100644 --- a/interface/src/avatar/Head.h +++ b/interface/src/avatar/Head.h @@ -37,7 +37,7 @@ public: void init(); void reset(); void simulate(float deltaTime, bool isMine, bool billboard = false); - void render(float alpha, bool forShadowMap); + void render(float alpha, Model::RenderMode mode); void setScale(float scale); void setPosition(glm::vec3 position) { _position = position; } void setGravity(glm::vec3 gravity) { _gravity = gravity; } diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 05636166e2..73d2a84eb7 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -614,14 +614,16 @@ void MyAvatar::renderBody(RenderMode renderMode) { } // Render the body's voxels and head - _skeletonModel.render(1.0f, renderMode == SHADOW_RENDER_MODE); + Model::RenderMode modelRenderMode = (renderMode == SHADOW_RENDER_MODE) ? + Model::SHADOW_RENDER_MODE : Model::DEFAULT_RENDER_MODE; + _skeletonModel.render(1.0f, modelRenderMode); // Render head so long as the camera isn't inside it const float RENDER_HEAD_CUTOFF_DISTANCE = 0.40f; Camera* myCamera = Application::getInstance()->getCamera(); if (renderMode != NORMAL_RENDER_MODE || (glm::length(myCamera->getPosition() - getHead()->calculateAverageEyePosition()) > RENDER_HEAD_CUTOFF_DISTANCE * _scale)) { - getHead()->render(1.0f, renderMode == SHADOW_RENDER_MODE); + getHead()->render(1.0f, modelRenderMode); } getHand()->render(true); } diff --git a/interface/src/renderer/Model.cpp b/interface/src/renderer/Model.cpp index 9d2a031a6e..36fbbe3833 100644 --- a/interface/src/renderer/Model.cpp +++ b/interface/src/renderer/Model.cpp @@ -58,7 +58,10 @@ Model::SkinLocations Model::_skinShadowLocations; void Model::setScale(const glm::vec3& scale) { glm::vec3 deltaScale = _scale - scale; - if (glm::length2(deltaScale) > EPSILON) { + + // decreased epsilon because this wasn't handling scale changes of 0.01 + const float SMALLER_EPSILON = EPSILON * 0.0001f; + if (glm::length2(deltaScale) > SMALLER_EPSILON) { _scale = scale; rebuildShapes(); } @@ -281,10 +284,10 @@ bool Model::updateGeometry() { return needFullUpdate; } -bool Model::render(float alpha, bool forShadowMap) { +bool Model::render(float alpha, RenderMode mode) { // render the attachments foreach (Model* attachment, _attachments) { - attachment->render(alpha); + attachment->render(alpha, mode); } if (_meshStates.isEmpty()) { return false; @@ -305,20 +308,24 @@ bool Model::render(float alpha, bool forShadowMap) { glDisable(GL_COLOR_MATERIAL); - glEnable(GL_CULL_FACE); + if (mode == DIFFUSE_RENDER_MODE || mode == NORMAL_RENDER_MODE) { + glDisable(GL_CULL_FACE); + } else { + glEnable(GL_CULL_FACE); + } // render opaque meshes with alpha testing glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.5f * alpha); - renderMeshes(alpha, forShadowMap, false); + renderMeshes(alpha, mode, false); glDisable(GL_ALPHA_TEST); - // render translucent meshes afterwards, with back face culling + // render translucent meshes afterwards - renderMeshes(alpha, forShadowMap, true); + renderMeshes(alpha, mode, true); glDisable(GL_CULL_FACE); @@ -1112,7 +1119,7 @@ void Model::deleteGeometry() { } } -void Model::renderMeshes(float alpha, bool forShadowMap, bool translucent) { +void Model::renderMeshes(float alpha, RenderMode mode, bool translucent) { const FBXGeometry& geometry = _geometry->getFBXGeometry(); const QVector& networkMeshes = _geometry->getMeshes(); @@ -1137,7 +1144,7 @@ void Model::renderMeshes(float alpha, bool forShadowMap, bool translucent) { ProgramObject* program = &_program; ProgramObject* skinProgram = &_skinProgram; SkinLocations* skinLocations = &_skinLocations; - if (forShadowMap) { + if (mode == SHADOW_RENDER_MODE) { program = &_shadowProgram; skinProgram = &_skinShadowProgram; skinLocations = &_skinShadowLocations; @@ -1175,7 +1182,7 @@ void Model::renderMeshes(float alpha, bool forShadowMap, bool translucent) { } if (mesh.blendshapes.isEmpty()) { - if (!(mesh.tangents.isEmpty() || forShadowMap)) { + if (!(mesh.tangents.isEmpty() || mode == SHADOW_RENDER_MODE)) { activeProgram->setAttributeBuffer(tangentLocation, GL_FLOAT, vertexCount * 2 * sizeof(glm::vec3), 3); activeProgram->enableAttributeArray(tangentLocation); } @@ -1185,7 +1192,7 @@ void Model::renderMeshes(float alpha, bool forShadowMap, bool translucent) { (mesh.tangents.size() + mesh.colors.size()) * sizeof(glm::vec3))); } else { - if (!(mesh.tangents.isEmpty() || forShadowMap)) { + if (!(mesh.tangents.isEmpty() || mode == SHADOW_RENDER_MODE)) { activeProgram->setAttributeBuffer(tangentLocation, GL_FLOAT, 0, 3); activeProgram->enableAttributeArray(tangentLocation); } @@ -1214,7 +1221,7 @@ void Model::renderMeshes(float alpha, bool forShadowMap, bool translucent) { continue; } // apply material properties - if (forShadowMap) { + if (mode == SHADOW_RENDER_MODE) { glBindTexture(GL_TEXTURE_2D, 0); } else { @@ -1255,7 +1262,7 @@ void Model::renderMeshes(float alpha, bool forShadowMap, bool translucent) { glDisableClientState(GL_TEXTURE_COORD_ARRAY); } - if (!(mesh.tangents.isEmpty() || forShadowMap)) { + if (!(mesh.tangents.isEmpty() || mode == SHADOW_RENDER_MODE)) { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE0); diff --git a/interface/src/renderer/Model.h b/interface/src/renderer/Model.h index 11374a6369..134a3e2172 100644 --- a/interface/src/renderer/Model.h +++ b/interface/src/renderer/Model.h @@ -57,7 +57,10 @@ public: void init(); void reset(); virtual void simulate(float deltaTime, bool fullUpdate = true); - bool render(float alpha = 1.0f, bool forShadowMap = false); + + enum RenderMode { DEFAULT_RENDER_MODE, SHADOW_RENDER_MODE, DIFFUSE_RENDER_MODE, NORMAL_RENDER_MODE }; + + bool render(float alpha = 1.0f, RenderMode mode = DEFAULT_RENDER_MODE); /// Sets the URL of the model to render. /// \param fallback the URL of a fallback model to render if the requested model fails to load @@ -254,7 +257,7 @@ private: void applyNextGeometry(); void deleteGeometry(); - void renderMeshes(float alpha, bool forShadowMap, bool translucent); + void renderMeshes(float alpha, RenderMode mode, bool translucent); QVector createJointStates(const FBXGeometry& geometry); QSharedPointer _baseGeometry; ///< reference required to prevent collection of base diff --git a/interface/src/ui/BandwidthMeter.cpp b/interface/src/ui/BandwidthMeter.cpp index 64ff74b846..962e8a857b 100644 --- a/interface/src/ui/BandwidthMeter.cpp +++ b/interface/src/ui/BandwidthMeter.cpp @@ -42,7 +42,8 @@ namespace { // .cpp-local BandwidthMeter::ChannelInfo BandwidthMeter::_CHANNELS[] = { { "Audio" , "Kbps", 8000.0 / 1024.0, 0x33cc99ff }, { "Avatars" , "Kbps", 8000.0 / 1024.0, 0xffef40c0 }, - { "Voxels" , "Kbps", 8000.0 / 1024.0, 0xd0d0d0a0 } + { "Voxels" , "Kbps", 8000.0 / 1024.0, 0xd0d0d0a0 }, + { "Metavoxels", "Kbps", 8000.0 / 1024.0, 0xd0d0d0a0 } }; BandwidthMeter::BandwidthMeter() : diff --git a/interface/src/ui/BandwidthMeter.h b/interface/src/ui/BandwidthMeter.h index 5879b1546d..c1577da8e4 100644 --- a/interface/src/ui/BandwidthMeter.h +++ b/interface/src/ui/BandwidthMeter.h @@ -30,11 +30,11 @@ public: bool isWithinArea(int x, int y, int screenWidth, int screenHeight); // Number of channels / streams. - static size_t const N_CHANNELS = 3; + static size_t const N_CHANNELS = 4; static size_t const N_STREAMS = N_CHANNELS * 2; // Channel usage. - enum ChannelIndex { AUDIO, AVATARS, VOXELS }; + enum ChannelIndex { AUDIO, AVATARS, VOXELS, METAVOXELS }; // Meta information held for a communication channel (bidirectional). struct ChannelInfo { diff --git a/interface/src/ui/MetavoxelEditor.cpp b/interface/src/ui/MetavoxelEditor.cpp index 19f5902336..ee1b4b0460 100644 --- a/interface/src/ui/MetavoxelEditor.cpp +++ b/interface/src/ui/MetavoxelEditor.cpp @@ -5,6 +5,9 @@ // Created by Andrzej Kapolka on 1/21/14. // Copyright (c) 2014 High Fidelity, Inc. All rights reserved. +// include this before QOpenGLFramebufferObject, which includes an earlier version of OpenGL +#include "InterfaceConfig.h" + #include #include #include @@ -13,8 +16,12 @@ #include #include #include +#include +#include #include +#include #include +#include #include #include @@ -565,7 +572,7 @@ void PlaceSpannerTool::render() { } Spanner* spanner = static_cast(_editor->getValue().value().data()); const float SPANNER_ALPHA = 0.25f; - spanner->getRenderer()->render(SPANNER_ALPHA, glm::vec3(), 0.0f); + spanner->getRenderer()->render(SPANNER_ALPHA, SpannerRenderer::DEFAULT_MODE, glm::vec3(), 0.0f); } bool PlaceSpannerTool::appliesTo(const AttributePointer& attribute) const { @@ -582,20 +589,18 @@ bool PlaceSpannerTool::eventFilter(QObject* watched, QEvent* event) { void PlaceSpannerTool::place() { AttributePointer attribute = AttributeRegistry::getInstance()->getAttribute(_editor->getSelectedAttribute()); - if (!attribute) { - return; + if (attribute) { + applyEdit(attribute, _editor->getValue().value()); } - SharedObjectPointer spanner = _editor->getValue().value(); - MetavoxelEditMessage message = { createEdit(attribute, spanner) }; - Application::getInstance()->getMetavoxels()->applyEdit(message); } InsertSpannerTool::InsertSpannerTool(MetavoxelEditor* editor) : PlaceSpannerTool(editor, "Insert Spanner", "Insert") { } -QVariant InsertSpannerTool::createEdit(const AttributePointer& attribute, const SharedObjectPointer& spanner) { - return QVariant::fromValue(InsertSpannerEdit(attribute, spanner)); +void InsertSpannerTool::applyEdit(const AttributePointer& attribute, const SharedObjectPointer& spanner) { + MetavoxelEditMessage message = { QVariant::fromValue(InsertSpannerEdit(attribute, spanner)) }; + Application::getInstance()->getMetavoxels()->applyEdit(message); } RemoveSpannerTool::RemoveSpannerTool(MetavoxelEditor* editor) : @@ -654,6 +659,230 @@ bool SetSpannerTool::appliesTo(const AttributePointer& attribute) const { return attribute == AttributeRegistry::getInstance()->getSpannersAttribute(); } -QVariant SetSpannerTool::createEdit(const AttributePointer& attribute, const SharedObjectPointer& spanner) { - return QVariant::fromValue(SetSpannerEdit(spanner)); +glm::quat DIRECTION_ROTATIONS[] = { + rotationBetween(glm::vec3(-1.0f, 0.0f, 0.0f), IDENTITY_FRONT), + rotationBetween(glm::vec3(1.0f, 0.0f, 0.0f), IDENTITY_FRONT), + rotationBetween(glm::vec3(0.0f, -1.0f, 0.0f), IDENTITY_FRONT), + rotationBetween(glm::vec3(0.0f, 1.0f, 0.0f), IDENTITY_FRONT), + rotationBetween(glm::vec3(0.0f, 0.0f, -1.0f), IDENTITY_FRONT), + rotationBetween(glm::vec3(0.0f, 0.0f, 1.0f), IDENTITY_FRONT) }; + +/// Represents a view from one direction of the spanner to be voxelized. +class DirectionImages { +public: + QImage color; + QVector depth; + glm::vec3 minima; + glm::vec3 maxima; + glm::vec3 scale; +}; + +class Voxelizer : public QRunnable { +public: + + Voxelizer(float size, const Box& bounds, float granularity, const QVector& directionImages); + + virtual void run(); + +private: + + void voxelize(const glm::vec3& center); + + float _size; + Box _bounds; + float _granularity; + QVector _directionImages; +}; + +Voxelizer::Voxelizer(float size, const Box& bounds, float granularity, const QVector& directionImages) : + _size(size), + _bounds(bounds), + _granularity(granularity), + _directionImages(directionImages) { +} + +void Voxelizer::run() { + // voxelize separately each cell within the bounds + float halfSize = _size * 0.5f; + for (float x = _bounds.minimum.x + halfSize; x < _bounds.maximum.x; x += _size) { + for (float y = _bounds.minimum.y + halfSize; y < _bounds.maximum.y; y += _size) { + for (float z = _bounds.minimum.z + halfSize; z < _bounds.maximum.z; z += _size) { + voxelize(glm::vec3(x, y, z)); + } + } + } +} + +class VoxelizationVisitor : public MetavoxelVisitor { +public: + + VoxelizationVisitor(const QVector& directionImages, const glm::vec3& center, float granularity); + + virtual int visit(MetavoxelInfo& info); + +private: + + QVector _directionImages; + glm::vec3 _center; + float _granularity; +}; + +VoxelizationVisitor::VoxelizationVisitor(const QVector& directionImages, + const glm::vec3& center, float granularity) : + MetavoxelVisitor(QVector(), QVector() << + AttributeRegistry::getInstance()->getColorAttribute()), + _directionImages(directionImages), + _center(center), + _granularity(granularity) { +} + +bool checkDisjoint(const DirectionImages& images, const glm::vec3& minimum, const glm::vec3& maximum, float extent) { + for (int x = qMax(0, (int)minimum.x), xmax = qMin(images.color.width(), (int)maximum.x); x < xmax; x++) { + for (int y = qMax(0, (int)minimum.y), ymax = qMin(images.color.height(), (int)maximum.y); y < ymax; y++) { + float depth = 1.0f - images.depth.at(y * images.color.width() + x); + if (depth - minimum.z >= -extent - EPSILON) { + return false; + } + } + } + return true; +} + +int VoxelizationVisitor::visit(MetavoxelInfo& info) { + float halfSize = info.size * 0.5f; + glm::vec3 center = info.minimum + _center + glm::vec3(halfSize, halfSize, halfSize); + const float EXTENT_SCALE = 2.0f; + if (info.size > _granularity) { + for (unsigned int i = 0; i < sizeof(DIRECTION_ROTATIONS) / sizeof(DIRECTION_ROTATIONS[0]); i++) { + glm::vec3 rotated = DIRECTION_ROTATIONS[i] * center; + const DirectionImages& images = _directionImages.at(i); + glm::vec3 relative = (rotated - images.minima) * images.scale; + glm::vec3 extents = images.scale * halfSize; + glm::vec3 minimum = relative - extents; + glm::vec3 maximum = relative + extents; + if (checkDisjoint(images, minimum, maximum, extents.z * EXTENT_SCALE)) { + info.outputValues[0] = AttributeValue(_outputs.at(0)); + return STOP_RECURSION; + } + } + return DEFAULT_ORDER; + } + QRgb closestColor; + float closestDistance = FLT_MAX; + for (unsigned int i = 0; i < sizeof(DIRECTION_ROTATIONS) / sizeof(DIRECTION_ROTATIONS[0]); i++) { + glm::vec3 rotated = DIRECTION_ROTATIONS[i] * center; + const DirectionImages& images = _directionImages.at(i); + glm::vec3 relative = (rotated - images.minima) * images.scale; + int x = qMax(qMin((int)glm::round(relative.x), images.color.width() - 1), 0); + int y = qMax(qMin((int)glm::round(relative.y), images.color.height() - 1), 0); + float depth = 1.0f - images.depth.at(y * images.color.width() + x); + float distance = depth - relative.z; + float extent = images.scale.z * halfSize * EXTENT_SCALE; + if (distance < -extent - EPSILON) { + info.outputValues[0] = AttributeValue(_outputs.at(0)); + return STOP_RECURSION; + } + QRgb color = images.color.pixel(x, y); + if (distance < extent + EPSILON) { + info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(color)); + return STOP_RECURSION; + } + if (distance < closestDistance) { + closestColor = color; + closestDistance = distance; + } + } + info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(closestColor)); + return STOP_RECURSION; +} + +void Voxelizer::voxelize(const glm::vec3& center) { + MetavoxelData data; + data.setSize(_size); + + VoxelizationVisitor visitor(_directionImages, center, _granularity); + data.guide(visitor); + + MetavoxelEditMessage edit = { QVariant::fromValue(SetDataEdit( + center - glm::vec3(_size, _size, _size) * 0.5f, data, true)) }; + QMetaObject::invokeMethod(Application::getInstance()->getMetavoxels(), "applyEdit", + Q_ARG(const MetavoxelEditMessage&, edit), Q_ARG(bool, true)); +} + +void SetSpannerTool::applyEdit(const AttributePointer& attribute, const SharedObjectPointer& spanner) { + Spanner* spannerData = static_cast(spanner.data()); + Box bounds = spannerData->getBounds(); + float longestSide(qMax(bounds.getLongestSide(), spannerData->getPlacementGranularity())); + float size = powf(2.0f, floorf(logf(longestSide) / logf(2.0f))); + Box cellBounds(glm::floor(bounds.minimum / size) * size, glm::ceil(bounds.maximum / size) * size); + + Application::getInstance()->getTextureCache()->getPrimaryFramebufferObject()->bind(); + + glEnable(GL_SCISSOR_TEST); + glEnable(GL_LIGHTING); + glEnable(GL_DEPTH_TEST); + glDisable(GL_BLEND); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + + QVector directionImages; + + for (unsigned int i = 0; i < sizeof(DIRECTION_ROTATIONS) / sizeof(DIRECTION_ROTATIONS[0]); i++) { + glm::vec3 minima(FLT_MAX, FLT_MAX, FLT_MAX); + glm::vec3 maxima(-FLT_MAX, -FLT_MAX, -FLT_MAX); + for (int j = 0; j < Box::VERTEX_COUNT; j++) { + glm::vec3 rotated = DIRECTION_ROTATIONS[i] * cellBounds.getVertex(j); + minima = glm::min(minima, rotated); + maxima = glm::max(maxima, rotated); + } + float renderGranularity = spannerData->getVoxelizationGranularity() / 4.0f; + int width = glm::round((maxima.x - minima.x) / renderGranularity); + int height = glm::round((maxima.y - minima.y) / renderGranularity); + + glViewport(0, 0, width, height); + glScissor(0, 0, width, height); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glLoadIdentity(); + glOrtho(minima.x, maxima.x, minima.y, maxima.y, -maxima.z, -minima.z); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glm::vec3 axis = glm::axis(DIRECTION_ROTATIONS[i]); + glRotatef(glm::degrees(glm::angle(DIRECTION_ROTATIONS[i])), axis.x, axis.y, axis.z); + + Application::getInstance()->setupWorldLight(); + + Application::getInstance()->updateUntranslatedViewMatrix(); + + spannerData->getRenderer()->render(1.0f, SpannerRenderer::DIFFUSE_MODE, glm::vec3(), 0.0f); + + DirectionImages images = { QImage(width, height, QImage::Format_ARGB32), + QVector(width * height), minima, maxima, glm::vec3(width / (maxima.x - minima.x), + height / (maxima.y - minima.y), 1.0f / (maxima.z - minima.z)) }; + glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, images.color.bits()); + glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, images.depth.data()); + directionImages.append(images); + + glMatrixMode(GL_PROJECTION); + } + glPopMatrix(); + + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + + glEnable(GL_BLEND); + glDisable(GL_SCISSOR_TEST); + + Application::getInstance()->getTextureCache()->getPrimaryFramebufferObject()->release(); + + glViewport(0, 0, Application::getInstance()->getGLWidget()->width(), Application::getInstance()->getGLWidget()->height()); + + // send the images off to the lab for processing + QThreadPool::globalInstance()->start(new Voxelizer(size, cellBounds, + spannerData->getVoxelizationGranularity(), directionImages)); } diff --git a/interface/src/ui/MetavoxelEditor.h b/interface/src/ui/MetavoxelEditor.h index 76ef8baf6f..7f3521bd1a 100644 --- a/interface/src/ui/MetavoxelEditor.h +++ b/interface/src/ui/MetavoxelEditor.h @@ -157,7 +157,7 @@ public: protected: - virtual QVariant createEdit(const AttributePointer& attribute, const SharedObjectPointer& spanner) = 0; + virtual void applyEdit(const AttributePointer& attribute, const SharedObjectPointer& spanner) = 0; private slots: @@ -174,7 +174,7 @@ public: protected: - virtual QVariant createEdit(const AttributePointer& attribute, const SharedObjectPointer& spanner); + virtual void applyEdit(const AttributePointer& attribute, const SharedObjectPointer& spanner); }; /// Allows removing a spanner from the scene. @@ -217,7 +217,7 @@ public: protected: - virtual QVariant createEdit(const AttributePointer& attribute, const SharedObjectPointer& spanner); + virtual void applyEdit(const AttributePointer& attribute, const SharedObjectPointer& spanner); }; #endif /* defined(__interface__MetavoxelEditor__) */ diff --git a/libraries/metavoxels/src/AttributeRegistry.cpp b/libraries/metavoxels/src/AttributeRegistry.cpp index fc448613f9..8914a9e3c9 100644 --- a/libraries/metavoxels/src/AttributeRegistry.cpp +++ b/libraries/metavoxels/src/AttributeRegistry.cpp @@ -144,6 +144,14 @@ void OwnedAttributeValue::mix(const AttributeValue& first, const AttributeValue& _value = _attribute->mix(first.getValue(), second.getValue(), alpha); } +void OwnedAttributeValue::blend(const AttributeValue& source, const AttributeValue& dest) { + if (_attribute) { + _attribute->destroy(_value); + } + _attribute = source.getAttribute(); + _value = _attribute->blend(source.getValue(), dest.getValue()); +} + OwnedAttributeValue& OwnedAttributeValue::operator=(const AttributeValue& other) { if (_attribute) { _attribute->destroy(_value); @@ -243,6 +251,19 @@ void* QRgbAttribute::mix(void* first, void* second, float alpha) const { glm::mix((float)qAlpha(firstValue), (float)qAlpha(secondValue), alpha))); } +const float EIGHT_BIT_MAXIMUM = 255.0f; + +void* QRgbAttribute::blend(void* source, void* dest) const { + QRgb sourceValue = decodeInline(source); + QRgb destValue = decodeInline(dest); + float alpha = qAlpha(sourceValue) / EIGHT_BIT_MAXIMUM; + return encodeInline(qRgba( + glm::mix((float)qRed(destValue), (float)qRed(sourceValue), alpha), + glm::mix((float)qGreen(destValue), (float)qGreen(sourceValue), alpha), + glm::mix((float)qBlue(destValue), (float)qBlue(sourceValue), alpha), + glm::mix((float)qAlpha(destValue), (float)qAlpha(sourceValue), alpha))); +} + void* QRgbAttribute::createFromScript(const QScriptValue& value, QScriptEngine* engine) const { return encodeInline((QRgb)value.toUInt32()); } @@ -287,6 +308,13 @@ void* PackedNormalAttribute::mix(void* first, void* second, float alpha) const { return encodeInline(packNormal(glm::normalize(glm::mix(firstNormal, secondNormal, alpha)))); } +void* PackedNormalAttribute::blend(void* source, void* dest) const { + QRgb sourceValue = decodeInline(source); + QRgb destValue = decodeInline(dest); + float alpha = qAlpha(sourceValue) / EIGHT_BIT_MAXIMUM; + return encodeInline(packNormal(glm::normalize(glm::mix(unpackNormal(destValue), unpackNormal(sourceValue), alpha)))); +} + const float CHAR_SCALE = 127.0f; const float INVERSE_CHAR_SCALE = 1.0f / CHAR_SCALE; diff --git a/libraries/metavoxels/src/AttributeRegistry.h b/libraries/metavoxels/src/AttributeRegistry.h index f7d8d955a5..db13ea9f4e 100644 --- a/libraries/metavoxels/src/AttributeRegistry.h +++ b/libraries/metavoxels/src/AttributeRegistry.h @@ -158,6 +158,9 @@ public: /// Sets this attribute to a mix of the first and second provided. void mix(const AttributeValue& first, const AttributeValue& second, float alpha); + /// Sets this attribute to a blend of the source and destination. + void blend(const AttributeValue& source, const AttributeValue& dest); + /// Destroys the current value, if any, and copies the specified other value. OwnedAttributeValue& operator=(const AttributeValue& other); @@ -218,6 +221,9 @@ public: /// Mixes the first and the second, returning a new value with the result. virtual void* mix(void* first, void* second, float alpha) const = 0; + /// Blends the source with the destination, returning a new value with the result. + virtual void* blend(void* source, void* dest) const = 0; + virtual void* getDefaultValue() const = 0; virtual void* createFromScript(const QScriptValue& value, QScriptEngine* engine) const { return create(); } @@ -249,6 +255,8 @@ public: virtual void* mix(void* first, void* second, float alpha) const { return create(alpha < 0.5f ? first : second); } + virtual void* blend(void* source, void* dest) const { return create(source); } + virtual void* getDefaultValue() const { return encodeInline(_defaultValue); } protected: @@ -315,6 +323,8 @@ public: virtual void* mix(void* first, void* second, float alpha) const; + virtual void* blend(void* source, void* dest) const; + virtual void* createFromScript(const QScriptValue& value, QScriptEngine* engine) const; virtual void* createFromVariant(const QVariant& value) const; @@ -333,6 +343,8 @@ public: virtual bool merge(void*& parent, void* children[], bool postRead = false) const; virtual void* mix(void* first, void* second, float alpha) const; + + virtual void* blend(void* source, void* dest) const; }; /// Packs a normal into an RGB value. diff --git a/libraries/metavoxels/src/Bitstream.cpp b/libraries/metavoxels/src/Bitstream.cpp index 077e6c1c69..3f3d6e5423 100644 --- a/libraries/metavoxels/src/Bitstream.cpp +++ b/libraries/metavoxels/src/Bitstream.cpp @@ -35,6 +35,7 @@ REGISTER_SIMPLE_TYPE_STREAMER(SharedObjectPointer) // some types don't quite work with our macro static int vec3Streamer = Bitstream::registerTypeStreamer(qMetaTypeId(), new SimpleTypeStreamer()); +static int quatStreamer = Bitstream::registerTypeStreamer(qMetaTypeId(), new SimpleTypeStreamer()); static int metaObjectStreamer = Bitstream::registerTypeStreamer(qMetaTypeId(), new SimpleTypeStreamer()); @@ -352,6 +353,14 @@ Bitstream& Bitstream::operator>>(glm::vec3& value) { return *this >> value.x >> value.y >> value.z; } +Bitstream& Bitstream::operator<<(const glm::quat& value) { + return *this << value.w << value.x << value.y << value.z; +} + +Bitstream& Bitstream::operator>>(glm::quat& value) { + return *this >> value.w >> value.x >> value.y >> value.z; +} + Bitstream& Bitstream::operator<<(const QByteArray& string) { *this << string.size(); return write(string.constData(), string.size() * BITS_IN_BYTE); @@ -869,7 +878,11 @@ Bitstream& Bitstream::operator>(SharedObjectPointer& object) { *this >> rawObject; } pointer = static_cast(rawObject); - pointer->setRemoteID(id); + if (pointer) { + pointer->setRemoteID(id); + } else { + qDebug() << "Null object" << pointer << reference; + } } object = static_cast(pointer.data()); return *this; diff --git a/libraries/metavoxels/src/Bitstream.h b/libraries/metavoxels/src/Bitstream.h index 760154a928..d7b16fcc4a 100644 --- a/libraries/metavoxels/src/Bitstream.h +++ b/libraries/metavoxels/src/Bitstream.h @@ -312,6 +312,9 @@ public: Bitstream& operator<<(const glm::vec3& value); Bitstream& operator>>(glm::vec3& value); + Bitstream& operator<<(const glm::quat& value); + Bitstream& operator>>(glm::quat& value); + Bitstream& operator<<(const QByteArray& string); Bitstream& operator>>(QByteArray& string); @@ -920,6 +923,13 @@ public: _Pragma(STRINGIFY(unused(_TypePtr##X))) #endif +/// Registers a simple type and its streamer. +template int registerSimpleMetaType() { + int type = qRegisterMetaType(); + Bitstream::registerTypeStreamer(type, new SimpleTypeStreamer()); + return type; +} + /// Registers a streamable type and its streamer. template int registerStreamableMetaType() { int type = qRegisterMetaType(); diff --git a/libraries/metavoxels/src/MetavoxelData.cpp b/libraries/metavoxels/src/MetavoxelData.cpp index e548de46c7..6ae847cea3 100644 --- a/libraries/metavoxels/src/MetavoxelData.cpp +++ b/libraries/metavoxels/src/MetavoxelData.cpp @@ -24,6 +24,8 @@ REGISTER_META_OBJECT(Spanner) REGISTER_META_OBJECT(Sphere) REGISTER_META_OBJECT(StaticModel) +static int metavoxelDataTypeId = registerSimpleMetaType(); + MetavoxelLOD::MetavoxelLOD(const glm::vec3& position, float threshold) : position(position), threshold(threshold) { @@ -327,6 +329,100 @@ const int Y_MAXIMUM_FLAG = 2; const int Z_MAXIMUM_FLAG = 4; const int MAXIMUM_FLAG_MASK = X_MAXIMUM_FLAG | Y_MAXIMUM_FLAG | Z_MAXIMUM_FLAG; +static glm::vec3 getNextMinimum(const glm::vec3& minimum, float nextSize, int index) { + return minimum + glm::vec3( + (index & X_MAXIMUM_FLAG) ? nextSize : 0.0f, + (index & Y_MAXIMUM_FLAG) ? nextSize : 0.0f, + (index & Z_MAXIMUM_FLAG) ? nextSize : 0.0f); +} + +static void setNode(const AttributeValue& value, MetavoxelNode*& node, MetavoxelNode* other, bool blend) { + if (!blend) { + // if we're not blending, we can just make a shallow copy + if (node) { + node->decrementReferenceCount(value.getAttribute()); + } + (node = other)->incrementReferenceCount(); + return; + } + if (node) { + MetavoxelNode* oldNode = node; + node = new MetavoxelNode(value.getAttribute(), oldNode); + oldNode->decrementReferenceCount(value.getAttribute()); + + } else { + node = new MetavoxelNode(value); + } + OwnedAttributeValue oldValue = node->getAttributeValue(value.getAttribute()); + node->blendAttributeValues(other->getAttributeValue(value.getAttribute()), oldValue); + if (!other->isLeaf()) { + for (int i = 0; i < MetavoxelNode::CHILD_COUNT; i++) { + MetavoxelNode* child = node->getChild(i); + setNode(oldValue, child, other->getChild(i), true); + node->setChild(i, child); + } + } + node->mergeChildren(value.getAttribute()); +} + +static void setNode(const AttributeValue& value, MetavoxelNode*& node, const glm::vec3& minimum, float size, + MetavoxelNode* other, const glm::vec3& otherMinimum, float otherSize, bool blend) { + if (otherSize >= size) { + setNode(value, node, other, blend); + return; + } + if (node) { + MetavoxelNode* oldNode = node; + node = new MetavoxelNode(value.getAttribute(), oldNode); + oldNode->decrementReferenceCount(value.getAttribute()); + + } else { + node = new MetavoxelNode(value); + } + int index = 0; + float otherHalfSize = otherSize * 0.5f; + float nextSize = size * 0.5f; + if (otherMinimum.x + otherHalfSize >= minimum.x + nextSize) { + index |= X_MAXIMUM_FLAG; + } + if (otherMinimum.y + otherHalfSize >= minimum.y + nextSize) { + index |= Y_MAXIMUM_FLAG; + } + if (otherMinimum.z + otherHalfSize >= minimum.z + nextSize) { + index |= Z_MAXIMUM_FLAG; + } + if (node->isLeaf()) { + for (int i = 1; i < MetavoxelNode::CHILD_COUNT; i++) { + node->setChild((index + i) % MetavoxelNode::CHILD_COUNT, new MetavoxelNode( + node->getAttributeValue(value.getAttribute()))); + } + } + MetavoxelNode* nextNode = node->getChild(index); + setNode(node->getAttributeValue(value.getAttribute()), nextNode, getNextMinimum(minimum, nextSize, index), + nextSize, other, otherMinimum, otherSize, blend); + node->setChild(index, nextNode); + node->mergeChildren(value.getAttribute()); +} + +void MetavoxelData::set(const glm::vec3& minimum, const MetavoxelData& data, bool blend) { + // expand to fit the entire data + Box bounds(minimum, minimum + glm::vec3(data.getSize(), data.getSize(), data.getSize())); + while (!getBounds().contains(bounds)) { + expand(); + } + + // set/mix each attribute separately + for (QHash::const_iterator it = data._roots.constBegin(); + it != data._roots.constEnd(); it++) { + MetavoxelNode*& root = _roots[it.key()]; + setNode(it.key(), root, getMinimum(), getSize(), it.value(), minimum, data.getSize(), blend); + if (root->isLeaf() && root->getAttributeValue(it.key()).isDefault()) { + _roots.remove(it.key()); + root->decrementReferenceCount(it.key()); + } + } +} + static int getOppositeIndex(int index) { return index ^ MAXIMUM_FLAG_MASK; } @@ -511,6 +607,14 @@ MetavoxelNode* MetavoxelData::createRoot(const AttributePointer& attribute) { return root = new MetavoxelNode(attribute); } +bool MetavoxelData::operator==(const MetavoxelData& other) const { + return _size == other._size && _roots == other._roots; +} + +bool MetavoxelData::operator!=(const MetavoxelData& other) const { + return _size != other._size || _roots != other._roots; +} + void MetavoxelData::incrementRootReferenceCounts() { for (QHash::const_iterator it = _roots.constBegin(); it != _roots.constEnd(); it++) { it.value()->incrementReferenceCount(); @@ -523,11 +627,22 @@ void MetavoxelData::decrementRootReferenceCounts() { } } -static glm::vec3 getNextMinimum(const glm::vec3& minimum, float nextSize, int index) { - return minimum + glm::vec3( - (index & X_MAXIMUM_FLAG) ? nextSize : 0.0f, - (index & Y_MAXIMUM_FLAG) ? nextSize : 0.0f, - (index & Z_MAXIMUM_FLAG) ? nextSize : 0.0f); +Bitstream& operator<<(Bitstream& out, const MetavoxelData& data) { + data.write(out); + return out; +} + +Bitstream& operator>>(Bitstream& in, MetavoxelData& data) { + data.read(in); + return in; +} + +template<> void Bitstream::writeDelta(const MetavoxelData& value, const MetavoxelData& reference) { + value.writeDelta(reference, MetavoxelLOD(), *this, MetavoxelLOD()); +} + +template<> void Bitstream::readDelta(MetavoxelData& value, const MetavoxelData& reference) { + value.readDelta(reference, MetavoxelLOD(), *this, MetavoxelLOD()); } bool MetavoxelStreamState::shouldSubdivide() const { @@ -577,7 +692,11 @@ MetavoxelNode::MetavoxelNode(const AttributePointer& attribute, const MetavoxelN void MetavoxelNode::setAttributeValue(const AttributeValue& attributeValue) { attributeValue.getAttribute()->destroy(_attributeValue); _attributeValue = attributeValue.copy(); - clearChildren(attributeValue.getAttribute()); +} + +void MetavoxelNode::blendAttributeValues(const AttributeValue& source, const AttributeValue& dest) { + source.getAttribute()->destroy(_attributeValue); + _attributeValue = source.getAttribute()->blend(source.getValue(), dest.getValue()); } AttributeValue MetavoxelNode::getAttributeValue(const AttributePointer& attribute) const { @@ -1408,7 +1527,7 @@ void SpannerRenderer::simulate(float deltaTime) { // nothing by default } -void SpannerRenderer::render(float alpha, const glm::vec3& clipMinimum, float clipSize) { +void SpannerRenderer::render(float alpha, Mode mode, const glm::vec3& clipMinimum, float clipSize) { // nothing by default } @@ -1426,7 +1545,7 @@ void Transformable::setTranslation(const glm::vec3& translation) { } } -void Transformable::setRotation(const glm::vec3& rotation) { +void Transformable::setRotation(const glm::quat& rotation) { if (_rotation != rotation) { emit rotationChanged(_rotation = rotation); } diff --git a/libraries/metavoxels/src/MetavoxelData.h b/libraries/metavoxels/src/MetavoxelData.h index fa408aafb7..7a53598042 100644 --- a/libraries/metavoxels/src/MetavoxelData.h +++ b/libraries/metavoxels/src/MetavoxelData.h @@ -61,6 +61,7 @@ public: MetavoxelData& operator=(const MetavoxelData& other); + void setSize(float size) { _size = size; } float getSize() const { return _size; } glm::vec3 getMinimum() const { return glm::vec3(_size, _size, _size) * -0.5f; } @@ -90,6 +91,9 @@ public: SharedObjectPointer findFirstRaySpannerIntersection(const glm::vec3& origin, const glm::vec3& direction, const AttributePointer& attribute, float& distance, const MetavoxelLOD& lod = MetavoxelLOD()); + /// Sets part of the data. + void set(const glm::vec3& minimum, const MetavoxelData& data, bool blend = false); + /// Expands the tree, increasing its capacity in all dimensions. void expand(); @@ -103,6 +107,9 @@ public: MetavoxelNode* getRoot(const AttributePointer& attribute) const { return _roots.value(attribute); } MetavoxelNode* createRoot(const AttributePointer& attribute); + bool operator==(const MetavoxelData& other) const; + bool operator!=(const MetavoxelData& other) const; + private: friend class MetavoxelVisitation; @@ -114,6 +121,16 @@ private: QHash _roots; }; +Bitstream& operator<<(Bitstream& out, const MetavoxelData& data); + +Bitstream& operator>>(Bitstream& in, MetavoxelData& data); + +template<> void Bitstream::writeDelta(const MetavoxelData& value, const MetavoxelData& reference); + +template<> void Bitstream::readDelta(MetavoxelData& value, const MetavoxelData& reference); + +Q_DECLARE_METATYPE(MetavoxelData) + /// Holds the state used in streaming metavoxel data. class MetavoxelStreamState { public: @@ -142,6 +159,8 @@ public: void setAttributeValue(const AttributeValue& attributeValue); + void blendAttributeValues(const AttributeValue& source, const AttributeValue& dest); + AttributeValue getAttributeValue(const AttributePointer& attribute) const; void* getAttributeValue() const { return _attributeValue; } @@ -174,13 +193,13 @@ public: void destroy(const AttributePointer& attribute); + void clearChildren(const AttributePointer& attribute); + private: Q_DISABLE_COPY(MetavoxelNode) friend class MetavoxelVisitation; - void clearChildren(const AttributePointer& attribute); - int _referenceCount; void* _attributeValue; MetavoxelNode* _children[CHILD_COUNT]; @@ -499,11 +518,13 @@ class SpannerRenderer : public QObject { public: + enum Mode { DEFAULT_MODE, DIFFUSE_MODE, NORMAL_MODE }; + Q_INVOKABLE SpannerRenderer(); virtual void init(Spanner* spanner); virtual void simulate(float deltaTime); - virtual void render(float alpha, const glm::vec3& clipMinimum, float clipSize); + virtual void render(float alpha, Mode mode, const glm::vec3& clipMinimum, float clipSize); virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, const glm::vec3& clipMinimum, float clipSize, float& distance) const; }; @@ -512,7 +533,7 @@ public: class Transformable : public Spanner { Q_OBJECT Q_PROPERTY(glm::vec3 translation MEMBER _translation WRITE setTranslation NOTIFY translationChanged) - Q_PROPERTY(glm::vec3 rotation MEMBER _rotation WRITE setRotation NOTIFY rotationChanged) + Q_PROPERTY(glm::quat rotation MEMBER _rotation WRITE setRotation NOTIFY rotationChanged) Q_PROPERTY(float scale MEMBER _scale WRITE setScale NOTIFY scaleChanged) public: @@ -522,8 +543,8 @@ public: void setTranslation(const glm::vec3& translation); const glm::vec3& getTranslation() const { return _translation; } - void setRotation(const glm::vec3& rotation); - const glm::vec3& getRotation() const { return _rotation; } + void setRotation(const glm::quat& rotation); + const glm::quat& getRotation() const { return _rotation; } void setScale(float scale); float getScale() const { return _scale; } @@ -531,13 +552,13 @@ public: signals: void translationChanged(const glm::vec3& translation); - void rotationChanged(const glm::vec3& rotation); + void rotationChanged(const glm::quat& rotation); void scaleChanged(float scale); private: glm::vec3 _translation; - glm::vec3 _rotation; // Euler Angles in degrees + glm::quat _rotation; float _scale; }; @@ -592,7 +613,7 @@ public: const QUrl& getURL() const { return _url; } virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, - const glm::vec3& clipMinimum, float clipSize,float& distance) const; + const glm::vec3& clipMinimum, float clipSize, float& distance) const; signals: diff --git a/libraries/metavoxels/src/MetavoxelMessages.cpp b/libraries/metavoxels/src/MetavoxelMessages.cpp index ce0d01ccf2..c930688cbd 100644 --- a/libraries/metavoxels/src/MetavoxelMessages.cpp +++ b/libraries/metavoxels/src/MetavoxelMessages.cpp @@ -305,3 +305,13 @@ void SetSpannerEdit::apply(MetavoxelData& data, const WeakSharedObjectHash& obje setIntersectingMasked(spanner->getBounds(), data); } + +SetDataEdit::SetDataEdit(const glm::vec3& minimum, const MetavoxelData& data, bool blend) : + minimum(minimum), + data(data), + blend(blend) { +} + +void SetDataEdit::apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const { + data.set(minimum, this->data, blend); +} diff --git a/libraries/metavoxels/src/MetavoxelMessages.h b/libraries/metavoxels/src/MetavoxelMessages.h index 25db8e5464..4f90a52e4c 100644 --- a/libraries/metavoxels/src/MetavoxelMessages.h +++ b/libraries/metavoxels/src/MetavoxelMessages.h @@ -176,4 +176,21 @@ public: DECLARE_STREAMABLE_METATYPE(SetSpannerEdit) +/// An edit that directly sets part of the metavoxel data. +class SetDataEdit : public MetavoxelEdit { + STREAMABLE + +public: + + STREAM glm::vec3 minimum; + STREAM MetavoxelData data; + STREAM bool blend; + + SetDataEdit(const glm::vec3& minimum = glm::vec3(), const MetavoxelData& data = MetavoxelData(), bool blend = false); + + virtual void apply(MetavoxelData& data, const WeakSharedObjectHash& objects) const; +}; + +DECLARE_STREAMABLE_METATYPE(SetDataEdit) + #endif /* defined(__interface__MetavoxelMessages__) */ diff --git a/libraries/metavoxels/src/MetavoxelUtil.cpp b/libraries/metavoxels/src/MetavoxelUtil.cpp index 7995809f1c..2ec1c3cb57 100644 --- a/libraries/metavoxels/src/MetavoxelUtil.cpp +++ b/libraries/metavoxels/src/MetavoxelUtil.cpp @@ -52,6 +52,7 @@ public: DoubleEditor::DoubleEditor(QWidget* parent) : QDoubleSpinBox(parent) { setMinimum(-FLT_MAX); setMaximum(FLT_MAX); + setSingleStep(0.01); } DelegatingItemEditorFactory::DelegatingItemEditorFactory() : @@ -127,6 +128,12 @@ static QItemEditorCreatorBase* createVec3EditorCreator() { return creator; } +static QItemEditorCreatorBase* createQuatEditorCreator() { + QItemEditorCreatorBase* creator = new LazyItemEditorCreator(); + getItemEditorFactory()->registerEditor(qMetaTypeId(), creator); + return creator; +} + static QItemEditorCreatorBase* createParameterizedURLEditorCreator() { QItemEditorCreatorBase* creator = new LazyItemEditorCreator(); getItemEditorFactory()->registerEditor(qMetaTypeId(), creator); @@ -138,6 +145,7 @@ static QItemEditorCreatorBase* qMetaObjectEditorCreator = createQMetaObjectEdito static QItemEditorCreatorBase* qColorEditorCreator = createQColorEditorCreator(); static QItemEditorCreatorBase* qUrlEditorCreator = createQUrlEditorCreator(); static QItemEditorCreatorBase* vec3EditorCreator = createVec3EditorCreator(); +static QItemEditorCreatorBase* quatEditorCreator = createQuatEditorCreator(); static QItemEditorCreatorBase* parameterizedURLEditorCreator = createParameterizedURLEditorCreator(); QByteArray signal(const char* signature) { @@ -380,7 +388,7 @@ void QUrlEditor::updateSettings() { QSettings().setValue("editorURLs", urls); } -Vec3Editor::Vec3Editor(QWidget* parent) : QWidget(parent) { +BaseVec3Editor::BaseVec3Editor(QWidget* parent) : QWidget(parent) { QHBoxLayout* layout = new QHBoxLayout(); layout->setContentsMargins(QMargins()); setLayout(layout); @@ -390,26 +398,64 @@ Vec3Editor::Vec3Editor(QWidget* parent) : QWidget(parent) { layout->addWidget(_z = createComponentBox()); } -void Vec3Editor::setVector(const glm::vec3& vector) { - _vector = vector; - _x->setValue(vector.x); - _y->setValue(vector.y); - _z->setValue(vector.z); -} - -void Vec3Editor::updateVector() { - emit vectorChanged(_vector = glm::vec3(_x->value(), _y->value(), _z->value())); -} - -QDoubleSpinBox* Vec3Editor::createComponentBox() { +QDoubleSpinBox* BaseVec3Editor::createComponentBox() { QDoubleSpinBox* box = new QDoubleSpinBox(); box->setMinimum(-FLT_MAX); box->setMaximum(FLT_MAX); box->setMinimumWidth(50); - connect(box, SIGNAL(valueChanged(double)), SLOT(updateVector())); + connect(box, SIGNAL(valueChanged(double)), SLOT(updateValue())); return box; } +Vec3Editor::Vec3Editor(QWidget* parent) : BaseVec3Editor(parent) { + _x->setSingleStep(0.01); + _y->setSingleStep(0.01); + _z->setSingleStep(0.01); +} + +static void setComponentValue(QDoubleSpinBox* box, double value) { + box->blockSignals(true); + box->setValue(value); + box->blockSignals(false); +} + +void Vec3Editor::setValue(const glm::vec3& value) { + _value = value; + setComponentValue(_x, value.x); + setComponentValue(_y, value.y); + setComponentValue(_z, value.z); +} + +void Vec3Editor::updateValue() { + emit valueChanged(_value = glm::vec3(_x->value(), _y->value(), _z->value())); +} + +QuatEditor::QuatEditor(QWidget* parent) : BaseVec3Editor(parent) { + _x->setRange(-179.0, 180.0); + _y->setRange(-179.0, 180.0); + _z->setRange(-179.0, 180.0); + + _x->setWrapping(true); + _y->setWrapping(true); + _z->setWrapping(true); +} + +void QuatEditor::setValue(const glm::quat& value) { + if (_value != value) { + glm::vec3 eulers = glm::degrees(safeEulerAngles(_value = value)); + setComponentValue(_x, eulers.x); + setComponentValue(_y, eulers.y); + setComponentValue(_z, eulers.z); + } +} + +void QuatEditor::updateValue() { + glm::quat value(glm::radians(glm::vec3(_x->value(), _y->value(), _z->value()))); + if (_value != value) { + emit valueChanged(_value = value); + } +} + ParameterizedURL::ParameterizedURL(const QUrl& url, const ScriptHash& parameters) : _url(url), _parameters(parameters) { diff --git a/libraries/metavoxels/src/MetavoxelUtil.h b/libraries/metavoxels/src/MetavoxelUtil.h index 9972981bc7..cc6c540151 100644 --- a/libraries/metavoxels/src/MetavoxelUtil.h +++ b/libraries/metavoxels/src/MetavoxelUtil.h @@ -142,10 +142,31 @@ private: QUrl _url; }; -/// Editor for vector values. -class Vec3Editor : public QWidget { +/// Base class for Vec3Editor and QuatEditor. +class BaseVec3Editor : public QWidget { Q_OBJECT - Q_PROPERTY(glm::vec3 vector MEMBER _vector WRITE setVector NOTIFY vectorChanged USER true) + +public: + + BaseVec3Editor(QWidget* parent); + +protected slots: + + virtual void updateValue() = 0; + +protected: + + QDoubleSpinBox* createComponentBox(); + + QDoubleSpinBox* _x; + QDoubleSpinBox* _y; + QDoubleSpinBox* _z; +}; + +/// Editor for vector values. +class Vec3Editor : public BaseVec3Editor { + Q_OBJECT + Q_PROPERTY(glm::vec3 value MEMBER _value WRITE setValue NOTIFY valueChanged USER true) public: @@ -153,24 +174,45 @@ public: signals: - void vectorChanged(const glm::vec3& vector); + void valueChanged(const glm::vec3& vector); public slots: - void setVector(const glm::vec3& vector); + void setValue(const glm::vec3& vector); -private slots: +protected: - void updateVector(); + virtual void updateValue(); private: - QDoubleSpinBox* createComponentBox(); + glm::vec3 _value; +}; + +/// Editor for quaternion values. +class QuatEditor : public BaseVec3Editor { + Q_OBJECT + Q_PROPERTY(glm::quat value MEMBER _value WRITE setValue NOTIFY valueChanged USER true) + +public: - QDoubleSpinBox* _x; - QDoubleSpinBox* _y; - QDoubleSpinBox* _z; - glm::vec3 _vector; + QuatEditor(QWidget* parent); + +signals: + + void valueChanged(const glm::quat& value); + +public slots: + + void setValue(const glm::quat& value); + +protected: + + virtual void updateValue(); + +private: + + glm::quat _value; }; typedef QHash ScriptHash; diff --git a/libraries/metavoxels/src/SharedObject.cpp b/libraries/metavoxels/src/SharedObject.cpp index 36257a740f..64ea48e016 100644 --- a/libraries/metavoxels/src/SharedObject.cpp +++ b/libraries/metavoxels/src/SharedObject.cpp @@ -20,18 +20,17 @@ REGISTER_META_OBJECT(SharedObject) SharedObject::SharedObject() : _id(++_lastID), - _remoteID(0), - _referenceCount(0) { + _remoteID(0) { _weakHash.insert(_id, this); } void SharedObject::incrementReferenceCount() { - _referenceCount++; + _referenceCount.ref(); } void SharedObject::decrementReferenceCount() { - if (--_referenceCount == 0) { + if (!_referenceCount.deref()) { _weakHash.remove(_id); delete this; } diff --git a/libraries/metavoxels/src/SharedObject.h b/libraries/metavoxels/src/SharedObject.h index 435127fffd..2751a66db3 100644 --- a/libraries/metavoxels/src/SharedObject.h +++ b/libraries/metavoxels/src/SharedObject.h @@ -9,6 +9,7 @@ #ifndef __interface__SharedObject__ #define __interface__SharedObject__ +#include #include #include #include @@ -42,7 +43,7 @@ public: void setRemoteID(int remoteID) { _remoteID = remoteID; } - int getReferenceCount() const { return _referenceCount; } + int getReferenceCount() const { return _referenceCount.load(); } void incrementReferenceCount(); void decrementReferenceCount(); @@ -62,7 +63,7 @@ private: int _id; int _remoteID; - int _referenceCount; + QAtomicInt _referenceCount; static int _lastID; static WeakSharedObjectHash _weakHash;