From cdef502e2e07d43caeb8b0e2494e8fa28841a667 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Tue, 26 May 2015 14:26:05 -0700 Subject: [PATCH] expose setVoxelSphere to js --- .../src/RenderablePolyVoxEntityItem.cpp | 34 +++---------------- .../src/RenderablePolyVoxEntityItem.h | 10 +++--- .../entities/src/EntityScriptingInterface.cpp | 17 ++++++++++ .../entities/src/EntityScriptingInterface.h | 2 ++ libraries/entities/src/PolyVoxEntityItem.h | 6 ++++ 5 files changed, 33 insertions(+), 36 deletions(-) diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp index 29d8c6ebf3..c3bad8a5e4 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp @@ -80,29 +80,16 @@ void RenderablePolyVoxEntityItem::setSphereInVolume(glm::vec3 center, float radi glm::vec3 pos(x, y, z); // And compute how far the current position is from the center of the volume float fDistToCenter = glm::distance(pos, center); - uint8_t uVoxelValue = _volData->getVoxelAt(x, y, z); // If the current voxel is less than 'radius' units from the center then we make it solid. if (fDistToCenter <= radius) { - // Our new voxel value - uVoxelValue = toValue; + _volData->setVoxelAt(x, y, z, toValue); } - - // Wrte the voxel value into the volume - _volData->setVoxelAt(x, y, z, uVoxelValue); } } } compressVolumeData(); } -void RenderablePolyVoxEntityItem::createSphereInVolume(glm::vec3 centerVoxelCoords, float radiusVoxelCoords) { - setSphereInVolume(centerVoxelCoords, radiusVoxelCoords, 255); -} - -void RenderablePolyVoxEntityItem::eraseSphereInVolume(glm::vec3 centerVoxelCoords, float radiusVoxelCoords) { - setSphereInVolume(centerVoxelCoords, radiusVoxelCoords, 0); -} - void RenderablePolyVoxEntityItem::setSphere(glm::vec3 centerWorldCoords, float radiusWorldCoords, uint8_t toValue) { // glm::vec3 centerVoxelCoords = worldToVoxelCoordinates(centerWorldCoords); glm::vec4 centerVoxelCoords = worldToVoxelMatrix() * glm::vec4(centerWorldCoords, 1.0f); @@ -112,37 +99,24 @@ void RenderablePolyVoxEntityItem::setSphere(glm::vec3 centerWorldCoords, float r setSphereInVolume(glm::vec3(centerVoxelCoords), radiusVoxelCoords, toValue); } -void RenderablePolyVoxEntityItem::createSphere(glm::vec3 centerWorldCoords, float radiusWorldCoords) { - setSphere(centerWorldCoords, radiusWorldCoords, 255); -} - -void RenderablePolyVoxEntityItem::eraseSphere(glm::vec3 centerWorldCoords, float radiusWorldCoords) { - setSphere(centerWorldCoords, radiusWorldCoords, 0); -} - void RenderablePolyVoxEntityItem::getModel() { if (!_volData) { // this will cause the allocation of _volData setVoxelVolumeSize(_voxelVolumeSize); } - glm::vec3 center = getCenter(); - createSphere(center, 4); - createSphere(center + glm::vec3(6.0f, 0.0f, 0.0f), 2); - // A mesh object to hold the result of surface extraction PolyVox::SurfaceMesh polyVoxMesh; //Create a surface extractor. Comment out one of the following two lines to decide which type gets created. - PolyVox::CubicSurfaceExtractorWithNormals> surfaceExtractor - (_volData, _volData->getEnclosingRegion(), &polyVoxMesh); - // PolyVox::MarchingCubesSurfaceExtractor> surfaceExtractor + // PolyVox::CubicSurfaceExtractorWithNormals> surfaceExtractor // (_volData, _volData->getEnclosingRegion(), &polyVoxMesh); + PolyVox::MarchingCubesSurfaceExtractor> surfaceExtractor + (_volData, _volData->getEnclosingRegion(), &polyVoxMesh); //Execute the surface extractor. surfaceExtractor.execute(); - // convert PolyVox mesh to a Sam mesh model::Mesh* mesh = new model::Mesh(); model::MeshPointer meshPtr(mesh); diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h index 1bbdc2e20a..633d02386e 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h @@ -47,13 +47,11 @@ public: glm::mat4 voxelToWorldMatrix() const; glm::mat4 worldToVoxelMatrix() const; - void setSphere(glm::vec3 center, float radius, uint8_t toValue); - void createSphere(glm::vec3 centerWorldCoords, float radiusWorldCoords); - void eraseSphere(glm::vec3 centerWorldCoords, float radiusWorldCoords); + // coords are in voxel-volume space + virtual void setSphereInVolume(glm::vec3 center, float radius, uint8_t toValue); - void setSphereInVolume(glm::vec3 center, float radius, uint8_t toValue); - void createSphereInVolume(glm::vec3 center, float radius); - void eraseSphereInVolume(glm::vec3 center, float radius); + // coords are in world-space + virtual void setSphere(glm::vec3 center, float radius, uint8_t toValue); private: void compressVolumeData(); diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index 351bbc3643..b4f532cab9 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -17,6 +17,7 @@ #include "ModelEntityItem.h" #include "ZoneEntityItem.h" #include "EntitiesLogging.h" +#include "PolyVoxEntityItem.h" EntityScriptingInterface::EntityScriptingInterface() : @@ -380,3 +381,19 @@ void RayToEntityIntersectionResultFromScriptValue(const QScriptValue& object, Ra vec3FromScriptValue(intersection, value.intersection); } } + + +bool EntityScriptingInterface::setVoxelSphere(QUuid entityID, const glm::vec3& center, float radius, int value) const { + EntityItem* entity = const_cast(_entityTree->findEntityByEntityItemID(entityID)); + if (!entity) { + return false; + } + EntityTypes::EntityType entityType = entity->getType(); + if (entityType != EntityTypes::PolyVox) { + return false; + } + + PolyVoxEntityItem* polyVoxEntity = static_cast(entity); + polyVoxEntity->setSphere(center, radius, value); + return true; +} diff --git a/libraries/entities/src/EntityScriptingInterface.h b/libraries/entities/src/EntityScriptingInterface.h index f1876a836b..b2292b75eb 100644 --- a/libraries/entities/src/EntityScriptingInterface.h +++ b/libraries/entities/src/EntityScriptingInterface.h @@ -117,6 +117,8 @@ public slots: Q_INVOKABLE void setSendPhysicsUpdates(bool value); Q_INVOKABLE bool getSendPhysicsUpdates() const; + Q_INVOKABLE bool setVoxelSphere(QUuid entityID, const glm::vec3& center, float radius, int value) const; + Q_INVOKABLE void dumpTree() const; signals: diff --git a/libraries/entities/src/PolyVoxEntityItem.h b/libraries/entities/src/PolyVoxEntityItem.h index 387bb86225..2039f44a34 100644 --- a/libraries/entities/src/PolyVoxEntityItem.h +++ b/libraries/entities/src/PolyVoxEntityItem.h @@ -70,6 +70,12 @@ class PolyVoxEntityItem : public EntityItem { static const glm::vec3 DEFAULT_VOXEL_VOLUME_SIZE; static const QByteArray DEFAULT_VOXEL_DATA; + // coords are in voxel-volume space + virtual void setSphereInVolume(glm::vec3 center, float radius, uint8_t toValue) {} + + // coords are in world-space + virtual void setSphere(glm::vec3 center, float radius, uint8_t toValue) {} + protected: rgbColor _color; glm::vec3 _voxelVolumeSize; // this is always 3 bytes