Zones now use Models instead of FBXGeometries

This commit is contained in:
Atlante45 2015-04-28 17:07:40 +02:00
parent b7263eeacf
commit fe0bd456e1
2 changed files with 65 additions and 18 deletions

View file

@ -18,39 +18,77 @@ EntityItem* RenderableZoneEntityItem::factory(const EntityItemID& entityID, cons
return new RenderableZoneEntityItem(entityID, properties); return new RenderableZoneEntityItem(entityID, properties);
} }
bool RenderableZoneEntityItem::setProperties(const EntityItemProperties& properties) { template<typename Lambda>
void RenderableZoneEntityItem::changeProperties(Lambda setNewProperties) {
QString oldShapeURL = getCompoundShapeURL(); QString oldShapeURL = getCompoundShapeURL();
bool somethingChanged = ZoneEntityItem::setProperties(properties); glm::vec3 oldPosition = getPosition(), oldDimensions = getDimensions();
if (somethingChanged && oldShapeURL != getCompoundShapeURL()) { glm::quat oldRotation = getRotation();
_compoundShapeModel = DependencyManager::get<GeometryCache>()->getGeometry(getCompoundShapeURL(), QUrl(), true);
setNewProperties();
if (oldShapeURL != getCompoundShapeURL()) {
if (!_model) {
_model = getModel();
_needsInitialSimulation = true;
}
_model->setURL(getCompoundShapeURL(), QUrl(), true, true);
} }
if (oldPosition != getPosition() ||
oldRotation != getRotation() ||
oldDimensions != getDimensions()) {
_needsInitialSimulation = true;
}
}
bool RenderableZoneEntityItem::setProperties(const EntityItemProperties& properties) {
bool somethingChanged = false;
changeProperties([&]() {
somethingChanged = this->ZoneEntityItem::setProperties(properties);
});
return somethingChanged; return somethingChanged;
} }
int RenderableZoneEntityItem::readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesLeftToRead, int RenderableZoneEntityItem::readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesLeftToRead,
ReadBitstreamToTreeParams& args, ReadBitstreamToTreeParams& args,
EntityPropertyFlags& propertyFlags, bool overwriteLocalData) { EntityPropertyFlags& propertyFlags, bool overwriteLocalData) {
QString oldShapeURL = getCompoundShapeURL(); int bytesRead = 0;
int bytesRead = ZoneEntityItem::readEntitySubclassDataFromBuffer(data, bytesLeftToRead, changeProperties([&]() {
args, propertyFlags, overwriteLocalData); bytesRead = ZoneEntityItem::readEntitySubclassDataFromBuffer(data, bytesLeftToRead,
if (oldShapeURL != getCompoundShapeURL()) { args, propertyFlags, overwriteLocalData);
_compoundShapeModel = DependencyManager::get<GeometryCache>()->getGeometry(getCompoundShapeURL(), QUrl(), true); });
}
return bytesRead; return bytesRead;
} }
Model* RenderableZoneEntityItem::getModel() {
Model* model = new Model();
model->init();
return model;
}
void RenderableZoneEntityItem::initialSimulation() {
_model->setScaleToFit(true, getDimensions());
_model->setSnapModelToRegistrationPoint(true, getRegistrationPoint());
_model->setRotation(getRotation());
_model->setTranslation(getPosition());
_model->simulate(0.0f);
_needsInitialSimulation = false;
}
bool RenderableZoneEntityItem::contains(const glm::vec3& point) const { bool RenderableZoneEntityItem::contains(const glm::vec3& point) const {
if (getShapeType() != SHAPE_TYPE_COMPOUND) { if (getShapeType() != SHAPE_TYPE_COMPOUND) {
return EntityItem::contains(point); return EntityItem::contains(point);
} }
if (!_compoundShapeModel && hasCompoundShapeURL()) {
const_cast<RenderableZoneEntityItem*>(this)->_compoundShapeModel = DependencyManager::get<GeometryCache>()->getGeometry(getCompoundShapeURL(), QUrl(), true); if (_model && !_model->isActive() && hasCompoundShapeURL()) {
// Since we have a delayload, we need to update the geometry if it has been downloaded
_model->setURL(getCompoundShapeURL(), QUrl(), true);
} }
if (EntityItem::contains(point) && _compoundShapeModel && _compoundShapeModel->isLoaded()) { if (_model && _model->isActive() && EntityItem::contains(point)) {
const FBXGeometry& geometry = _compoundShapeModel->getFBXGeometry(); if (_needsInitialSimulation) {
glm::vec3 meshDimensions = geometry.getUnscaledMeshExtents().maximum - geometry.getUnscaledMeshExtents().minimum; const_cast<RenderableZoneEntityItem*>(this)->initialSimulation();
return geometry.convexHullContains(worldToEntity(point) * meshDimensions); }
return _model->convexHullContains(point);
} }
return false; return false;

View file

@ -12,6 +12,7 @@
#ifndef hifi_RenderableZoneEntityItem_h #ifndef hifi_RenderableZoneEntityItem_h
#define hifi_RenderableZoneEntityItem_h #define hifi_RenderableZoneEntityItem_h
#include <Model.h>
#include <ZoneEntityItem.h> #include <ZoneEntityItem.h>
class NetworkGeometry; class NetworkGeometry;
@ -21,7 +22,9 @@ public:
static EntityItem* factory(const EntityItemID& entityID, const EntityItemProperties& properties); static EntityItem* factory(const EntityItemID& entityID, const EntityItemProperties& properties);
RenderableZoneEntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties) : RenderableZoneEntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties) :
ZoneEntityItem(entityItemID, properties) ZoneEntityItem(entityItemID, properties),
_model(NULL),
_needsInitialSimulation(true)
{ } { }
virtual bool setProperties(const EntityItemProperties& properties); virtual bool setProperties(const EntityItemProperties& properties);
@ -32,8 +35,14 @@ public:
virtual bool contains(const glm::vec3& point) const; virtual bool contains(const glm::vec3& point) const;
private: private:
Model* getModel();
void initialSimulation();
QSharedPointer<NetworkGeometry> _compoundShapeModel; template<typename Lambda>
void changeProperties(Lambda functor);
Model* _model;
bool _needsInitialSimulation;
}; };
#endif // hifi_RenderableZoneEntityItem_h #endif // hifi_RenderableZoneEntityItem_h