Merge pull request #4712 from Atlante45/compound_zones_fix

Compound zones fix
This commit is contained in:
Brad Hefta-Gaub 2015-04-28 09:48:05 -07:00
commit 50eecb15b3
5 changed files with 123 additions and 20 deletions

View file

@ -18,39 +18,77 @@ EntityItem* RenderableZoneEntityItem::factory(const EntityItemID& entityID, cons
return new RenderableZoneEntityItem(entityID, properties);
}
bool RenderableZoneEntityItem::setProperties(const EntityItemProperties& properties) {
template<typename Lambda>
void RenderableZoneEntityItem::changeProperties(Lambda setNewProperties) {
QString oldShapeURL = getCompoundShapeURL();
bool somethingChanged = ZoneEntityItem::setProperties(properties);
if (somethingChanged && oldShapeURL != getCompoundShapeURL()) {
_compoundShapeModel = DependencyManager::get<GeometryCache>()->getGeometry(getCompoundShapeURL(), QUrl(), true);
glm::vec3 oldPosition = getPosition(), oldDimensions = getDimensions();
glm::quat oldRotation = getRotation();
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;
}
int RenderableZoneEntityItem::readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesLeftToRead,
ReadBitstreamToTreeParams& args,
EntityPropertyFlags& propertyFlags, bool overwriteLocalData) {
QString oldShapeURL = getCompoundShapeURL();
int bytesRead = ZoneEntityItem::readEntitySubclassDataFromBuffer(data, bytesLeftToRead,
args, propertyFlags, overwriteLocalData);
if (oldShapeURL != getCompoundShapeURL()) {
_compoundShapeModel = DependencyManager::get<GeometryCache>()->getGeometry(getCompoundShapeURL(), QUrl(), true);
}
int bytesRead = 0;
changeProperties([&]() {
bytesRead = ZoneEntityItem::readEntitySubclassDataFromBuffer(data, bytesLeftToRead,
args, propertyFlags, overwriteLocalData);
});
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 {
if (getShapeType() != SHAPE_TYPE_COMPOUND) {
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()) {
const FBXGeometry& geometry = _compoundShapeModel->getFBXGeometry();
glm::vec3 meshDimensions = geometry.getUnscaledMeshExtents().maximum - geometry.getUnscaledMeshExtents().minimum;
return geometry.convexHullContains(worldToEntity(point) * meshDimensions);
if (_model && _model->isActive() && EntityItem::contains(point)) {
if (_needsInitialSimulation) {
const_cast<RenderableZoneEntityItem*>(this)->initialSimulation();
}
return _model->convexHullContains(point);
}
return false;

View file

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

View file

@ -324,7 +324,7 @@ public:
const FBXGeometry& getFBXGeometry() const { return _geometry; }
const QVector<NetworkMesh>& getMeshes() const { return _meshes; }
//
QVector<int> getJointMappings(const AnimationPointer& animation);
virtual void setLoadPriority(const QPointer<QObject>& owner, float priority);

View file

@ -573,6 +573,61 @@ bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const g
return intersectedSomething;
}
bool Model::convexHullContains(glm::vec3 point) {
// if we aren't active, we can't compute that yet...
if (!isActive()) {
return false;
}
// extents is the entity relative, scaled, centered extents of the entity
glm::vec3 position = _translation;
glm::mat4 rotation = glm::mat4_cast(_rotation);
glm::mat4 translation = glm::translate(position);
glm::mat4 modelToWorldMatrix = translation * rotation;
glm::mat4 worldToModelMatrix = glm::inverse(modelToWorldMatrix);
Extents modelExtents = getMeshExtents(); // NOTE: unrotated
glm::vec3 dimensions = modelExtents.maximum - modelExtents.minimum;
glm::vec3 corner = -(dimensions * _registrationPoint);
AABox modelFrameBox(corner, dimensions);
glm::vec3 modelFramePoint = glm::vec3(worldToModelMatrix * glm::vec4(point, 1.0f));
// we can use the AABox's contains() by mapping our point into the model frame
// and testing there.
if (modelFrameBox.contains(modelFramePoint)){
if (!_calculatedMeshTrianglesValid) {
recalculateMeshBoxes(true);
}
// If we are inside the models box, then consider the submeshes...
int subMeshIndex = 0;
foreach(const AABox& subMeshBox, _calculatedMeshBoxes) {
if (subMeshBox.contains(point)) {
bool insideMesh = true;
// To be inside the sub mesh, we need to be behind every triangles' planes
const QVector<Triangle>& meshTriangles = _calculatedMeshTriangles[subMeshIndex];
foreach (const Triangle& triangle, meshTriangles) {
if (!isPointBehindTrianglesPlane(point, triangle.v0, triangle.v1, triangle.v2)) {
// it's not behind at least one so we bail
insideMesh = false;
break;
}
}
if (insideMesh) {
// It's inside this mesh, return true.
return true;
}
}
subMeshIndex++;
}
}
// It wasn't in any mesh, return false.
return false;
}
// TODO: we seem to call this too often when things haven't actually changed... look into optimizing this
void Model::recalculateMeshBoxes(bool pickAgainstTriangles) {
bool calculatedMeshTrianglesNeeded = pickAgainstTriangles && !_calculatedMeshTrianglesValid;
@ -1047,7 +1102,7 @@ int Model::getLastFreeJointIndex(int jointIndex) const {
void Model::setURL(const QUrl& url, const QUrl& fallback, bool retainCurrent, bool delayLoad) {
// don't recreate the geometry if it's the same URL
if (_url == url) {
if (_url == url && _geometry && _geometry->getURL() == url) {
return;
}
_url = url;

View file

@ -213,6 +213,7 @@ public:
bool findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const glm::vec3& direction, float& distance,
BoxFace& face, QString& extraInfo, bool pickAgainstTriangles = false);
bool convexHullContains(glm::vec3 point);
protected:
QSharedPointer<NetworkGeometry> _geometry;