From f2c1c43da3d8c86df2f9cffc5ee93b4f76343dd1 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 20 May 2015 08:44:41 -0700 Subject: [PATCH] polyvox code runs, no rendering yet --- .../src/RenderableModelEntityItem.cpp | 2 +- .../src/RenderableModelEntityItem.h | 2 +- .../src/RenderablePolyVoxEntityItem.cpp | 78 ++++++++++++- .../src/RenderablePolyVoxEntityItem.h | 17 ++- libraries/entities/src/PolyVoxEntityItem.cpp | 108 ++++++++++++++++++ libraries/entities/src/PolyVoxEntityItem.h | 68 +++++++++++ 6 files changed, 264 insertions(+), 11 deletions(-) create mode 100644 libraries/entities/src/PolyVoxEntityItem.cpp create mode 100644 libraries/entities/src/PolyVoxEntityItem.h diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index 50bd0c9d5c..f7e70b2b56 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -110,7 +110,7 @@ void RenderableModelEntityItem::remapTextures() { void RenderableModelEntityItem::render(RenderArgs* args) { PerformanceTimer perfTimer("RMEIrender"); - assert(getType() == EntityTypes::Model || getType() == EntityTypes::PolyVox); + assert(getType() == EntityTypes::Model); bool drawAsModel = hasModel(); diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.h b/libraries/entities-renderer/src/RenderableModelEntityItem.h index 74182b4753..8f55e67d09 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.h +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.h @@ -60,7 +60,7 @@ public: virtual bool contains(const glm::vec3& point) const; -protected: +private: void remapTextures(); Model* _model; diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp index 936a833ec8..a4df131cc3 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -58,7 +59,75 @@ void createSphereInVolume(PolyVox::SimpleVolume& volData, float fRadius } -// virtual + +void RenderablePolyVoxEntityItem::render(RenderArgs* args) { + PerformanceTimer perfTimer("PolyVoxrender"); + assert(getType() == EntityTypes::PolyVox); + + bool drawAsModel = hasModel(); + + glm::vec3 position = getPosition(); + glm::vec3 dimensions = getDimensions(); + + bool didDraw = false; + if (drawAsModel) { + glPushMatrix(); + { + float alpha = getLocalRenderAlpha(); + + if (!_model || _needsModelReload) { + // TODO: this getModel() appears to be about 3% of model render time. We should optimize + PerformanceTimer perfTimer("getModel"); + EntityTreeRenderer* renderer = static_cast(args->_renderer); + getModel(renderer); + } + + if (_model) { + glm::quat rotation = getRotation(); + bool movingOrAnimating = isMoving(); + if ((movingOrAnimating || _needsInitialSimulation) && _model->isActive()) { + _model->setScaleToFit(true, dimensions); + _model->setSnapModelToRegistrationPoint(true, getRegistrationPoint()); + _model->setRotation(rotation); + _model->setTranslation(position); + + // make sure to simulate so everything gets set up correctly for rendering + { + PerformanceTimer perfTimer("_model->simulate"); + _model->simulate(0.0f); + } + _needsInitialSimulation = false; + } + + if (_model->isActive()) { + // TODO: this is the majority of model render time. And rendering of a cube model vs the basic Box render + // is significantly more expensive. Is there a way to call this that doesn't cost us as much? + PerformanceTimer perfTimer("model->render"); + // filter out if not needed to render + if (args && (args->_renderMode == RenderArgs::SHADOW_RENDER_MODE)) { + if (movingOrAnimating) { + _model->renderInScene(alpha, args); + didDraw = true; + } + } else { + _model->renderInScene(alpha, args); + didDraw = true; + } + } + } + } + glPopMatrix(); + } + + if (!didDraw) { + glm::vec4 greenColor(0.0f, 1.0f, 0.0f, 1.0f); + RenderableDebugableEntityItem::renderBoundingBox(this, args, 0.0f, greenColor); + } + + RenderableDebugableEntityItem::render(this, args); +} + + Model* RenderablePolyVoxEntityItem::getModel(EntityTreeRenderer* renderer) { PolyVox::SimpleVolume volData(PolyVox::Region(PolyVox::Vector3DInt32(0,0,0), PolyVox::Vector3DInt32(63, 63, 63))); @@ -96,13 +165,10 @@ Model* RenderablePolyVoxEntityItem::getModel(EntityTreeRenderer* renderer) { } assert(_myRenderer == renderer); // you should only ever render on one renderer - result = _model = _myRenderer->allocateModel(getModelURL(), getCompoundShapeURL()); - assert(_model); + // result = _model = _myRenderer->allocateModel("", ""); + // assert(_model); _needsInitialSimulation = true; - - - return result; } diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h index 24359accf0..7c3a42ca4a 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h @@ -14,18 +14,29 @@ #include -#include "RenderableModelEntityItem.h" +#include "PolyVoxEntityItem.h" #include "RenderableDebugableEntityItem.h" -class RenderablePolyVoxEntityItem : public RenderableModelEntityItem { +class RenderablePolyVoxEntityItem : public PolyVoxEntityItem { public: static EntityItem* factory(const EntityItemID& entityID, const EntityItemProperties& properties); RenderablePolyVoxEntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties) : - RenderableModelEntityItem(entityItemID, properties) { } + PolyVoxEntityItem(entityItemID, properties) { } + void render(RenderArgs* args); virtual bool hasModel() const { return true; } virtual Model* getModel(EntityTreeRenderer* renderer); + +private: + Model* _model = nullptr; + bool _needsInitialSimulation = true; + bool _needsModelReload = true; + EntityTreeRenderer* _myRenderer = nullptr; + // QString _currentTextures; + // QStringList _originalTextures; + // bool _originalTexturesRead; + // QVector> _points; }; diff --git a/libraries/entities/src/PolyVoxEntityItem.cpp b/libraries/entities/src/PolyVoxEntityItem.cpp new file mode 100644 index 0000000000..145a4ec728 --- /dev/null +++ b/libraries/entities/src/PolyVoxEntityItem.cpp @@ -0,0 +1,108 @@ +// +// PolyVoxEntityItem.cpp +// libraries/entities/src +// +// Created by Seth Alves on 5/11/15. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + + +#include + +#include + +#include "PolyVoxEntityItem.h" +#include "EntityTree.h" +#include "EntitiesLogging.h" +#include "EntityTreeElement.h" + + +EntityItem* PolyVoxEntityItem::factory(const EntityItemID& entityID, const EntityItemProperties& properties) { + EntityItem* result = new PolyVoxEntityItem(entityID, properties); + return result; +} + +PolyVoxEntityItem::PolyVoxEntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties) : + EntityItem(entityItemID) +{ + _type = EntityTypes::PolyVox; + _created = properties.getCreated(); + setProperties(properties); +} + +EntityItemProperties PolyVoxEntityItem::getProperties() const { + EntityItemProperties properties = EntityItem::getProperties(); // get the properties from our base class + + properties._color = getXColor(); + properties._colorChanged = false; + + properties._glowLevel = getGlowLevel(); + properties._glowLevelChanged = false; + + return properties; +} + +bool PolyVoxEntityItem::setProperties(const EntityItemProperties& properties) { + bool somethingChanged = false; + somethingChanged = EntityItem::setProperties(properties); // set the properties in our base class + + SET_ENTITY_PROPERTY_FROM_PROPERTIES(color, setColor); + + if (somethingChanged) { + bool wantDebug = false; + if (wantDebug) { + uint64_t now = usecTimestampNow(); + int elapsed = now - getLastEdited(); + qCDebug(entities) << "PolyVoxEntityItem::setProperties() AFTER update... edited AGO=" << elapsed << + "now=" << now << " getLastEdited()=" << getLastEdited(); + } + setLastEdited(properties._lastEdited); + } + return somethingChanged; +} + +int PolyVoxEntityItem::readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesLeftToRead, + ReadBitstreamToTreeParams& args, + EntityPropertyFlags& propertyFlags, bool overwriteLocalData) { + + int bytesRead = 0; + const unsigned char* dataAt = data; + + READ_ENTITY_PROPERTY(PROP_COLOR, rgbColor, setColor); + + return bytesRead; +} + + +// TODO: eventually only include properties changed since the params.lastViewFrustumSent time +EntityPropertyFlags PolyVoxEntityItem::getEntityProperties(EncodeBitstreamParams& params) const { + EntityPropertyFlags requestedProperties = EntityItem::getEntityProperties(params); + requestedProperties += PROP_COLOR; + return requestedProperties; +} + +void PolyVoxEntityItem::appendSubclassData(OctreePacketData* packetData, EncodeBitstreamParams& params, + EntityTreeElementExtraEncodeData* modelTreeElementExtraEncodeData, + EntityPropertyFlags& requestedProperties, + EntityPropertyFlags& propertyFlags, + EntityPropertyFlags& propertiesDidntFit, + int& propertyCount, + OctreeElement::AppendState& appendState) const { + + bool successPropertyFits = true; + + APPEND_ENTITY_PROPERTY(PROP_COLOR, getColor()); +} + +void PolyVoxEntityItem::debugDump() const { + quint64 now = usecTimestampNow(); + qCDebug(entities) << " POLYVOX EntityItem id:" << getEntityItemID() << "---------------------------------------------"; + qCDebug(entities) << " color:" << _color[0] << "," << _color[1] << "," << _color[2]; + qCDebug(entities) << " position:" << debugTreeVector(_position); + qCDebug(entities) << " dimensions:" << debugTreeVector(_dimensions); + qCDebug(entities) << " getLastEdited:" << debugTime(getLastEdited(), now); +} + diff --git a/libraries/entities/src/PolyVoxEntityItem.h b/libraries/entities/src/PolyVoxEntityItem.h new file mode 100644 index 0000000000..5ddbd9ba7a --- /dev/null +++ b/libraries/entities/src/PolyVoxEntityItem.h @@ -0,0 +1,68 @@ +// +// PolyVoxEntityItem.h +// libraries/entities/src +// +// Created by Seth Alves on 5/11/15. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_PolyVoxEntityItem_h +#define hifi_PolyVoxEntityItem_h + +#include "EntityItem.h" + +class PolyVoxEntityItem : public EntityItem { + public: + static EntityItem* factory(const EntityItemID& entityID, const EntityItemProperties& properties); + + PolyVoxEntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties); + + ALLOW_INSTANTIATION // This class can be instantiated + + // methods for getting/setting all properties of an entity + virtual EntityItemProperties getProperties() const; + virtual bool setProperties(const EntityItemProperties& properties); + + // TODO: eventually only include properties changed since the params.lastViewFrustumSent time + virtual EntityPropertyFlags getEntityProperties(EncodeBitstreamParams& params) const; + + virtual void appendSubclassData(OctreePacketData* packetData, EncodeBitstreamParams& params, + EntityTreeElementExtraEncodeData* modelTreeElementExtraEncodeData, + EntityPropertyFlags& requestedProperties, + EntityPropertyFlags& propertyFlags, + EntityPropertyFlags& propertiesDidntFit, + int& propertyCount, + OctreeElement::AppendState& appendState) const; + + virtual int readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesLeftToRead, + ReadBitstreamToTreeParams& args, + EntityPropertyFlags& propertyFlags, bool overwriteLocalData); + + const rgbColor& getColor() const { return _color; } + xColor getXColor() const { xColor color = { _color[RED_INDEX], _color[GREEN_INDEX], _color[BLUE_INDEX] }; return color; } + + void setColor(const rgbColor& value) { memcpy(_color, value, sizeof(_color)); } + void setColor(const xColor& value) { + _color[RED_INDEX] = value.red; + _color[GREEN_INDEX] = value.green; + _color[BLUE_INDEX] = value.blue; + } + + virtual ShapeType getShapeType() const { return SHAPE_TYPE_POLYVOX; } + + // never have a ray intersection pick a PolyVoxEntityItem. + virtual bool supportsDetailedRayIntersection() const { return true; } + virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction, + bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face, + void** intersectedObject, bool precisionPicking) const { return false; } + + virtual void debugDump() const; + + protected: + rgbColor _color; +}; + +#endif // hifi_PolyVoxEntityItem_h