rayIntersection uses meters instead of tree units

This commit is contained in:
Andrew Meadows 2015-02-26 17:42:17 -08:00
parent 64c28fe925
commit 7836bb4dcd
14 changed files with 32 additions and 43 deletions

View file

@ -80,7 +80,7 @@ void RenderableLightEntityItem::render(RenderArgs* args) {
#endif
};
bool RenderableLightEntityItem::findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
bool RenderableLightEntityItem::findDetailedRayIntersectionInMeters(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject, bool precisionPicking) const {

View file

@ -24,7 +24,7 @@ public:
virtual void render(RenderArgs* args);
virtual bool supportsDetailedRayIntersection() const { return true; }
virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
virtual bool findDetailedRayIntersectionInMeters(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject, bool precisionPicking) const;
};

View file

@ -254,29 +254,16 @@ EntityItemProperties RenderableModelEntityItem::getProperties() const {
return properties;
}
bool RenderableModelEntityItem::findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
bool RenderableModelEntityItem::findDetailedRayIntersectionInMeters(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject, bool precisionPicking) const {
if (!_model) {
return true;
}
//qDebug() << "RenderableModelEntityItem::findDetailedRayIntersectionInMeters() precisionPicking:" << precisionPicking;
glm::vec3 originInMeters = origin * (float)TREE_SCALE;
QString extraInfo;
float localDistance;
//qDebug() << "RenderableModelEntityItem::findDetailedRayIntersection() precisionPicking:" << precisionPicking;
bool intersectsModel = _model->findRayIntersectionAgainstSubMeshes(originInMeters, direction,
localDistance, face, extraInfo, precisionPicking);
if (intersectsModel) {
// NOTE: findRayIntersectionAgainstSubMeshes() does work in meters, but we're expected to return
// results in tree scale.
distance = localDistance / (float)TREE_SCALE;
}
return intersectsModel; // we only got here if we intersected our non-aabox
return _model->findRayIntersectionAgainstSubMeshes(origin, direction, distance, face, extraInfo, precisionPicking);
}

View file

@ -44,7 +44,7 @@ public:
virtual void render(RenderArgs* args);
virtual bool supportsDetailedRayIntersection() const { return true; }
virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
virtual bool findDetailedRayIntersectionInMeters(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject, bool precisionPicking) const;

View file

@ -139,7 +139,7 @@ public:
virtual void debugDump() const;
virtual bool supportsDetailedRayIntersection() const { return false; }
virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
virtual bool findDetailedRayIntersectionInMeters(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject, bool precisionPicking) const { return true; }

View file

@ -469,7 +469,7 @@ bool EntityTreeElement::bestFitBounds(const glm::vec3& minPoint, const glm::vec3
return false;
}
bool EntityTreeElement::findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
bool EntityTreeElement::findDetailedRayIntersectionInMeters(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject, bool precisionPicking, float distanceToElementCube) {
@ -485,7 +485,7 @@ bool EntityTreeElement::findDetailedRayIntersection(const glm::vec3& origin, con
while(entityItr != entityEnd) {
EntityItem* entity = (*entityItr);
AABox entityBox = entity->getAABox();
AABox entityBox = entity->getAABoxInMeters();
float localDistance;
BoxFace localFace;
@ -494,11 +494,11 @@ bool EntityTreeElement::findDetailedRayIntersection(const glm::vec3& origin, con
// extents is the entity relative, scaled, centered extents of the entity
glm::mat4 rotation = glm::mat4_cast(entity->getRotation());
glm::mat4 translation = glm::translate(entity->getPositionInDomainUnits());
glm::mat4 translation = glm::translate(entity->getPositionInMeters());
glm::mat4 entityToWorldMatrix = translation * rotation;
glm::mat4 worldToEntityMatrix = glm::inverse(entityToWorldMatrix);
glm::vec3 dimensions = entity->getDimensionsInDomainUnits();
glm::vec3 dimensions = entity->getDimensionsInMeters();
glm::vec3 registrationPoint = entity->getRegistrationPoint();
glm::vec3 corner = -(dimensions * registrationPoint);
@ -513,7 +513,7 @@ bool EntityTreeElement::findDetailedRayIntersection(const glm::vec3& origin, con
if (localDistance < distance) {
// now ask the entity if we actually intersect
if (entity->supportsDetailedRayIntersection()) {
if (entity->findDetailedRayIntersection(origin, direction, keepSearching, element, localDistance,
if (entity->findDetailedRayIntersectionInMeters(origin, direction, keepSearching, element, localDistance,
localFace, intersectedObject, precisionPicking)) {
if (localDistance < distance) {

View file

@ -135,7 +135,7 @@ public:
virtual bool deleteApproved() const { return !hasEntities(); }
virtual bool canRayIntersect() const { return hasEntities(); }
virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
virtual bool findDetailedRayIntersectionInMeters(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject, bool precisionPicking, float distanceToElementCube);

View file

@ -94,13 +94,13 @@ void SphereEntityItem::appendSubclassData(OctreePacketData* packetData, EncodeBi
APPEND_ENTITY_PROPERTY(PROP_COLOR, appendColor, getColor());
}
bool SphereEntityItem::findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
bool SphereEntityItem::findDetailedRayIntersectionInMeters(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject, bool precisionPicking) const {
// determine the ray in the frame of the entity transformed from a unit sphere
glm::mat4 translation = glm::translate(getPositionInDomainUnits());
glm::mat4 translation = glm::translate(getPositionInMeters());
glm::mat4 rotation = glm::mat4_cast(getRotation());
glm::mat4 scale = glm::scale(getDimensionsInDomainUnits());
glm::mat4 scale = glm::scale(getDimensionsInMeters());
glm::mat4 registration = glm::translate(glm::vec3(0.5f, 0.5f, 0.5f) - getRegistrationPoint());
glm::mat4 entityToWorldMatrix = translation * rotation * scale * registration;
glm::mat4 worldToEntityMatrix = glm::inverse(entityToWorldMatrix);

View file

@ -56,7 +56,7 @@ public:
virtual ShapeType getShapeType() const { return SHAPE_TYPE_SPHERE; }
virtual bool supportsDetailedRayIntersection() const { return true; }
virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
virtual bool findDetailedRayIntersectionInMeters(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject, bool precisionPicking) const;

View file

@ -122,7 +122,7 @@ void TextEntityItem::appendSubclassData(OctreePacketData* packetData, EncodeBits
}
bool TextEntityItem::findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
bool TextEntityItem::findDetailedRayIntersectionInMeters(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject, bool precisionPicking) const {
@ -136,7 +136,7 @@ bool TextEntityItem::findDetailedRayIntersection(const glm::vec3& origin, const
const glm::vec3 UNROTATED_NORMAL(0.0f, 0.0f, -1.0f);
glm::vec3 normal = _rotation * UNROTATED_NORMAL;
plane.setNormal(normal);
plane.setPoint(_position); // the position is definitely a point on our plane
plane.setPoint(getPositionInMeters()); // the position is definitely a point on our plane
bool intersects = plane.findRayIntersection(rayInfo);
@ -145,11 +145,11 @@ bool TextEntityItem::findDetailedRayIntersection(const glm::vec3& origin, const
// now we know the point the ray hit our plane
glm::mat4 rotation = glm::mat4_cast(getRotation());
glm::mat4 translation = glm::translate(getPositionInDomainUnits());
glm::mat4 translation = glm::translate(getPositionInMeters());
glm::mat4 entityToWorldMatrix = translation * rotation;
glm::mat4 worldToEntityMatrix = glm::inverse(entityToWorldMatrix);
glm::vec3 dimensions = getDimensionsInDomainUnits();
glm::vec3 dimensions = getDimensionsInMeters();
glm::vec3 registrationPoint = getRegistrationPoint();
glm::vec3 corner = -(dimensions * registrationPoint);
AABox entityFrameBox(corner, dimensions);

View file

@ -46,7 +46,7 @@ public:
EntityPropertyFlags& propertyFlags, bool overwriteLocalData);
virtual bool supportsDetailedRayIntersection() const { return true; }
virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
virtual bool findDetailedRayIntersectionInMeters(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject, bool precisionPicking) const;

View file

@ -706,7 +706,7 @@ bool findRayIntersectionOp(OctreeElement* element, void* extraData) {
bool Octree::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
OctreeElement*& element, float& distance, BoxFace& face, void** intersectedObject,
Octree::lockType lockType, bool* accurateResult, bool precisionPicking) {
RayArgs args = { origin / (float)(TREE_SCALE), direction, element, distance, face,
RayArgs args = { origin, direction, element, distance, face,
intersectedObject, false, precisionPicking};
distance = FLT_MAX;
@ -726,10 +726,6 @@ bool Octree::findRayIntersection(const glm::vec3& origin, const glm::vec3& direc
recurseTreeWithOperation(findRayIntersectionOp, &args);
if (args.found) {
args.distance *= (float)(TREE_SCALE); // scale back up to meters
}
if (gotLock) {
unlock();
}

View file

@ -1338,13 +1338,11 @@ bool OctreeElement::findRayIntersection(const glm::vec3& origin, const glm::vec3
keepSearching = true; // assume that we will continue searching after this.
AACube cube = getAACube();
cube *= (float)TREE_SCALE;
float distanceToElementCube = std::numeric_limits<float>::max();
float distanceToElementDetails = distance;
BoxFace localFace;
AACube debugCube = cube;
debugCube.scale((float)TREE_SCALE);
// if the ray doesn't intersect with our cube, we can stop searching!
if (!cube.findRayIntersection(origin, direction, distanceToElementCube, localFace)) {
keepSearching = false; // no point in continuing to search

View file

@ -63,6 +63,8 @@ public:
AABox clamp(const glm::vec3& min, const glm::vec3& max) const;
AABox clamp(float min, float max) const;
AACube& operator *= (float scale);
private:
glm::vec3 getClosestPointOnFace(const glm::vec3& point, BoxFace face) const;
glm::vec3 getClosestPointOnFace(const glm::vec4& origin, const glm::vec4& direction, BoxFace face) const;
@ -74,6 +76,12 @@ private:
float _scale;
};
inline AACube& AACube::operator *= (float scale) {
_corner *= scale;
_scale *= scale;
return *this;
}
inline bool operator==(const AACube& a, const AACube& b) {
return a.getCorner() == b.getCorner() && a.getScale() == b.getScale();
}