basics of triangle picking working for models

This commit is contained in:
ZappoMan 2014-12-05 11:31:27 -08:00
parent 0647211fb8
commit a6b86da47a
25 changed files with 298 additions and 39 deletions

View file

@ -24,6 +24,7 @@ function setupMenus() {
Menu.addMenuItem({ menuName: "Developer > Entities", menuItemName: "Disable Light Entities", isCheckable: true, isChecked: false });
Menu.addMenuItem({ menuName: "Developer > Entities", menuItemName: "Don't Attempt to Reduce Material Switches", isCheckable: true, isChecked: false });
Menu.addMenuItem({ menuName: "Developer > Entities", menuItemName: "Don't Attempt Render Entities as Scene", isCheckable: true, isChecked: false });
Menu.addMenuItem({ menuName: "Developer > Entities", menuItemName: "Pick Against Model Triangles", isCheckable: true, isChecked: false });
Menu.addMenu("Developer > Entities > Culling");
Menu.addMenuItem({ menuName: "Developer > Entities > Culling", menuItemName: "Don't Cull Out Of View Mesh Parts", isCheckable: true, isChecked: false });
Menu.addMenuItem({ menuName: "Developer > Entities > Culling", menuItemName: "Don't Cull Too Small Mesh Parts", isCheckable: true, isChecked: false });

View file

@ -432,6 +432,7 @@ namespace MenuOption {
const QString OldVoxelCullingMode = "Old Voxel Culling Mode";
const QString Pair = "Pair";
const QString PasteToVoxel = "Paste to Voxel...";
const QString PickAgainstModelTriangles = "Pick Against Model Triangles";
const QString PipelineWarnings = "Log Render Pipeline Warnings";
const QString Preferences = "Preferences...";
const QString Quit = "Quit";

View file

@ -133,9 +133,6 @@ void RenderableModelEntityItem::render(RenderArgs* args) {
getModel(renderer);
}
if (_model) {
// handle animations..
if (hasAnimation()) {
@ -175,7 +172,7 @@ void RenderableModelEntityItem::render(RenderArgs* args) {
// 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");
bool dontRenderAsScene = Menu::getInstance()->isOptionChecked(MenuOption::DontRenderEntitiesAsScene);
bool dontRenderAsScene = true; // Menu::getInstance()->isOptionChecked(MenuOption::DontRenderEntitiesAsScene);
if (dontRenderAsScene) {
_model->render(alpha, modelRenderMode, args);
} else {
@ -270,10 +267,14 @@ bool RenderableModelEntityItem::findDetailedRayIntersection(const glm::vec3& ori
qDebug() << " originInMeters:" << originInMeters;
QString extraInfo;
float localDistance;
bool intersectsModel = _model->findRayIntersectionAgainstSubMeshes(originInMeters, direction, localDistance, face, extraInfo);
if (intersectsModel) {
// NOTE: findRayIntersectionAgainstSubMeshes() does work in meters, but we're expected to return
// results in tree scale.
distance = localDistance / (float)TREE_SCALE;
qDebug() << " --hit this mode -- returning distance:" << distance;
}
return intersectsModel; // we only got here if we intersected our non-aabox

View file

@ -54,6 +54,7 @@ Model::Model(QObject* parent) :
_blendNumber(0),
_appliedBlendNumber(0),
_calculatedMeshBoxesValid(false),
_calculatedMeshTrianglesValid(false),
_meshGroupsKnown(false) {
// we may have been created in the network thread, but we live in the main thread
@ -516,7 +517,7 @@ void Model::setJointStates(QVector<JointState> states) {
}
bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const glm::vec3& direction,
float& distance, BoxFace& face, QString& extraInfo) const {
float& distance, BoxFace& face, QString& extraInfo) {
bool intersectedSomething = false;
@ -524,8 +525,12 @@ bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const g
if (!isActive()) {
return intersectedSomething;
}
bool pickAgainstTriangles = Menu::getInstance()->isOptionChecked(MenuOption::PickAgainstModelTriangles);
qDebug() << "Model::findRayIntersectionAgainstSubMeshes()...";
qDebug() << " origin:" << origin;
qDebug() << " direction:" << direction;
// extents is the entity relative, scaled, centered extents of the entity
glm::vec3 position = _translation;
@ -538,31 +543,58 @@ bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const g
qDebug() << " modelExtents:" << modelExtents;
glm::vec3 dimensions = modelExtents.maximum - modelExtents.minimum;
glm::vec3 corner = dimensions * -0.5f; // since we're going to do the ray picking in the model frame of reference
glm::vec3 corner = dimensions * _registrationPoint; // since we're going to do the ray picking in the model frame of reference
AABox overlayFrameBox(corner, dimensions);
qDebug() << " overlayFrameBox:" << overlayFrameBox;
glm::vec3 modelFrameOrigin = glm::vec3(worldToModelMatrix * glm::vec4(origin, 1.0f));
glm::vec3 modelFrameDirection = glm::vec3(worldToModelMatrix * glm::vec4(direction, 0.0f));
qDebug() << " modelFrameOrigin:" << modelFrameOrigin;
qDebug() << " modelFrameDirection:" << modelFrameDirection;
// we can use the AABox's ray intersection by mapping our origin and direction into the model frame
// and testing intersection there.
if (overlayFrameBox.findRayIntersection(modelFrameOrigin, modelFrameDirection, distance, face)) {
float bestDistance = std::numeric_limits<float>::max();
float bestTriangleDistance = std::numeric_limits<float>::max();
bool someTriangleHit = false;
float distanceToSubMesh;
BoxFace subMeshFace;
int subMeshIndex = 0;
const FBXGeometry& geometry = _geometry->getFBXGeometry();
// If we hit the models box, then consider the submeshes...
foreach(const AABox& subMeshBox, _calculatedMeshBoxes) {
const FBXGeometry& geometry = _geometry->getFBXGeometry();
qDebug() << "subMeshBox[" << subMeshIndex <<"]:" << subMeshBox;
if (subMeshBox.findRayIntersection(origin, direction, distanceToSubMesh, subMeshFace)) {
if (distanceToSubMesh < bestDistance) {
if (pickAgainstTriangles) {
if (!_calculatedMeshTrianglesValid) {
recalcuateMeshBoxes();
}
// check our triangles here....
const QVector<Triangle>& meshTriangles = _calculatedMeshTriangles[subMeshIndex];
int t = 0;
foreach (const Triangle& triangle, meshTriangles) {
//qDebug() << "triangle["<< t <<"] :" << triangle.v0 << ", "<< triangle.v1 << ", " << triangle.v2;
t++;
float thisTriangleDistance;
if (findRayTrianlgeIntersection(origin, direction, triangle, thisTriangleDistance)) {
if (thisTriangleDistance < bestTriangleDistance) {
bestTriangleDistance = thisTriangleDistance;
someTriangleHit = true;
}
}
}
}
bestDistance = distanceToSubMesh;
intersectedSomething = true;
face = subMeshFace;
@ -571,6 +603,27 @@ bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const g
}
subMeshIndex++;
}
// if we were asked to pick against triangles, and we didn't hit one, then we
// do not consider this model to be hit at all.
if (pickAgainstTriangles && !someTriangleHit) {
intersectedSomething = false;
}
qDebug() << "pickAgainstTriangles:" << pickAgainstTriangles;
qDebug() << "someTriangleHit:" << someTriangleHit;
qDebug() << "bestTriangleDistance:" << bestTriangleDistance;
qDebug() << "bestDistance:" << bestDistance;
if (intersectedSomething) {
qDebug() << " --- we hit this model --- ";
if (pickAgainstTriangles) {
distance = bestTriangleDistance;
} else {
distance = bestDistance;
}
qDebug() << "distance:" << distance;
}
return intersectedSomething;
}
@ -579,17 +632,92 @@ bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const g
}
void Model::recalcuateMeshBoxes() {
if (!_calculatedMeshBoxesValid) {
bool pickAgainstTriangles = Menu::getInstance()->isOptionChecked(MenuOption::PickAgainstModelTriangles);
bool calculatedMeshTrianglesNeeded = pickAgainstTriangles && !_calculatedMeshTrianglesValid;
if (!_calculatedMeshBoxesValid || calculatedMeshTrianglesNeeded) {
qDebug() << "Model::recalcuateMeshBoxes()";
PerformanceTimer perfTimer("calculatedMeshBoxes");
const FBXGeometry& geometry = _geometry->getFBXGeometry();
int numberOfMeshes = geometry.meshes.size();
_calculatedMeshBoxes.resize(numberOfMeshes);
_calculatedMeshTriangles.clear();
for (int i = 0; i < numberOfMeshes; i++) {
const FBXMesh& mesh = geometry.meshes.at(i);
Extents scaledMeshExtents = calculateScaledOffsetExtents(mesh.meshExtents);
qDebug() << "mesh.meshExtents["<<i<<"]:" << mesh.meshExtents;
qDebug() << "scaledMeshExtents["<<i<<"]:" << scaledMeshExtents;
_calculatedMeshBoxes[i] = AABox(scaledMeshExtents);
qDebug() << "_calculatedMeshBoxes["<<i<<"]:" << _calculatedMeshBoxes[i];
if (pickAgainstTriangles) {
qDebug() << "mesh.parts.size():" << mesh.parts.size();
qDebug() << "---- calculating triangles for mesh parts for mesh:" << i << " ----------";
QVector<Triangle> thisMeshTriangles;
for (int j = 0; j < mesh.parts.size(); j++) {
const FBXMeshPart& part = mesh.parts.at(j);
const int INDICES_PER_TRIANGLE = 3;
const int INDICES_PER_QUAD = 4;
if (part.quadIndices.size() > 0) {
int numberOfQuads = part.quadIndices.size() / INDICES_PER_QUAD;
qDebug() << "numberOfQuads:" << numberOfQuads;
int vIndex = 0;
for (int q = 0; q < numberOfQuads; q++) {
int i0 = part.quadIndices[vIndex++];
int i1 = part.quadIndices[vIndex++];
int i2 = part.quadIndices[vIndex++];
int i3 = part.quadIndices[vIndex++];
glm::vec3 v0 = calculateScaledOffsetPoint(glm::vec3(mesh.modelTransform * glm::vec4(mesh.vertices[i0], 1.0f)));
glm::vec3 v1 = calculateScaledOffsetPoint(glm::vec3(mesh.modelTransform * glm::vec4(mesh.vertices[i1], 1.0f)));
glm::vec3 v2 = calculateScaledOffsetPoint(glm::vec3(mesh.modelTransform * glm::vec4(mesh.vertices[i2], 1.0f)));
glm::vec3 v3 = calculateScaledOffsetPoint(glm::vec3(mesh.modelTransform * glm::vec4(mesh.vertices[i3], 1.0f)));
Triangle tri1 = { v0, v1, v3 };
Triangle tri2 = { v1, v2, v3 };
//qDebug() << "quad["<< q <<"].t1 :" << v0 << ", "<< v1 << ", " << v3;
//qDebug() << "quad["<< q <<"].t2 :" << v1 << ", "<< v2 << ", " << v3;
thisMeshTriangles.push_back(tri1);
thisMeshTriangles.push_back(tri2);
}
}
if (part.triangleIndices.size() > 0) {
int numberOfTris = part.triangleIndices.size() / INDICES_PER_TRIANGLE;
qDebug() << "numberOfTris:" << numberOfTris;
int vIndex = 0;
for (int t = 0; t < numberOfTris; t++) {
int i0 = part.triangleIndices[vIndex++];
int i1 = part.triangleIndices[vIndex++];
int i2 = part.triangleIndices[vIndex++];
glm::vec3 v0 = calculateScaledOffsetPoint(glm::vec3(mesh.modelTransform * glm::vec4(mesh.vertices[i0], 1.0f)));
glm::vec3 v1 = calculateScaledOffsetPoint(glm::vec3(mesh.modelTransform * glm::vec4(mesh.vertices[i1], 1.0f)));
glm::vec3 v2 = calculateScaledOffsetPoint(glm::vec3(mesh.modelTransform * glm::vec4(mesh.vertices[i2], 1.0f)));
Triangle tri = { v0, v1, v2 };
//qDebug() << "triangle["<< t <<"] :" << v0 << ", " << v1 << ", " << v2;
thisMeshTriangles.push_back(tri);
}
}
}
_calculatedMeshTriangles.push_back(thisMeshTriangles);
qDebug() << "------------------------------------------------------------------------------";
}
}
_calculatedMeshBoxesValid = true;
_calculatedMeshTrianglesValid = pickAgainstTriangles;
}
}
@ -851,6 +979,15 @@ Extents Model::calculateScaledOffsetExtents(const Extents& extents) const {
return translatedExtents;
}
glm::vec3 Model::calculateScaledOffsetPoint(const glm::vec3& point) const {
// we need to include any fst scaling, translation, and rotation, which is captured in the offset matrix
glm::vec3 offsetPoint = glm::vec3(_geometry->getFBXGeometry().offset * glm::vec4(point, 1.0f));
glm::vec3 scaledPoint = ((offsetPoint + _offset) * _scale);
glm::vec3 rotatedPoint = _rotation * scaledPoint;
glm::vec3 translatedPoint = rotatedPoint + _translation;
return translatedPoint;
}
bool Model::getJointState(int index, glm::quat& rotation) const {
if (index == -1 || index >= _jointStates.size()) {
@ -1149,6 +1286,7 @@ void Model::simulate(float deltaTime, bool fullUpdate) {
if (isActive() && fullUpdate) {
_calculatedMeshBoxesValid = false; // if we have to simulate, we need to assume our mesh boxes are all invalid
_calculatedMeshTrianglesValid = false;
// check for scale to fit
if (_scaleToFit && !_scaledToFit) {

View file

@ -19,6 +19,7 @@
#include "Transform.h"
#include <AABox.h>
#include <AnimationCache.h>
#include <GeometryUtil.h>
#include <PhysicsEntity.h>
#include "AnimationHandle.h"
@ -34,7 +35,6 @@ class Shape;
#include "RenderArgs.h"
class ViewFrustum;
#include "gpu/Stream.h"
#include "gpu/Batch.h"
@ -119,6 +119,9 @@ public:
/// Returns the scaled equivalent of some extents in model space.
Extents calculateScaledOffsetExtents(const Extents& extents) const;
/// Returns the scaled equivalent of a point in model space.
glm::vec3 calculateScaledOffsetPoint(const glm::vec3& point) const;
/// Returns a reference to the shared geometry.
const QSharedPointer<NetworkGeometry>& getGeometry() const { return _geometry; }
@ -194,7 +197,7 @@ public:
{ _geometry->setTextureWithNameToURL(name, url); }
bool findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const glm::vec3& direction,
float& distance, BoxFace& face, QString& extraInfo) const;
float& distance, BoxFace& face, QString& extraInfo);
protected:
QSharedPointer<NetworkGeometry> _geometry;
@ -361,8 +364,11 @@ private:
static void initSkinProgram(ProgramObject& program, SkinLocations& locations, int specularTextureUnit = 1);
QVector<AABox> _calculatedMeshBoxes;
QVector<AABox> _calculatedMeshBoxes; // world coordinate AABoxes for all sub mesh boxes
bool _calculatedMeshBoxesValid;
QVector< QVector<Triangle> > _calculatedMeshTriangles; // world coordinate triangles for all sub meshes
bool _calculatedMeshTrianglesValid;
void recalcuateMeshBoxes();

View file

@ -167,7 +167,7 @@ QScriptValue Base3DOverlay::getProperty(const QString& property) {
}
bool Base3DOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
float& distance, BoxFace& face) const {
float& distance, BoxFace& face) {
return false;
}

View file

@ -50,10 +50,10 @@ public:
virtual void setProperties(const QScriptValue& properties);
virtual QScriptValue getProperty(const QString& property);
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face) const;
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face);
virtual bool findRayIntersectionExtraInfo(const glm::vec3& origin, const glm::vec3& direction,
float& distance, BoxFace& face, QString& extraInfo) const {
float& distance, BoxFace& face, QString& extraInfo) {
return findRayIntersection(origin, direction, distance, face);
}

View file

@ -213,7 +213,7 @@ void BillboardOverlay::replyFinished() {
}
bool BillboardOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
float& distance, BoxFace& face) const {
float& distance, BoxFace& face) {
if (_billboardTexture) {
float maxSize = glm::max(_fromImage.width(), _fromImage.height());

View file

@ -35,7 +35,7 @@ public:
void setClipFromSource(const QRect& bounds) { _fromImage = bounds; }
virtual QScriptValue getProperty(const QString& property);
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face) const;
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face);
virtual BillboardOverlay* createClone() const;

View file

@ -355,7 +355,7 @@ QScriptValue Circle3DOverlay::getProperty(const QString& property) {
bool Circle3DOverlay::findRayIntersection(const glm::vec3& origin,
const glm::vec3& direction, float& distance, BoxFace& face) const {
const glm::vec3& direction, float& distance, BoxFace& face) {
bool intersects = Planar3DOverlay::findRayIntersection(origin, direction, distance, face);
if (intersects) {

View file

@ -48,7 +48,7 @@ public:
void setMajorTickMarksColor(const xColor& value) { _majorTickMarksColor = value; }
void setMinorTickMarksColor(const xColor& value) { _minorTickMarksColor = value; }
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face) const;
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face);
virtual Circle3DOverlay* createClone() const;

View file

@ -170,14 +170,14 @@ QScriptValue ModelOverlay::getProperty(const QString& property) {
}
bool ModelOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
float& distance, BoxFace& face) const {
float& distance, BoxFace& face) {
QString subMeshNameTemp;
return _model.findRayIntersectionAgainstSubMeshes(origin, direction, distance, face, subMeshNameTemp);
}
bool ModelOverlay::findRayIntersectionExtraInfo(const glm::vec3& origin, const glm::vec3& direction,
float& distance, BoxFace& face, QString& extraInfo) const {
float& distance, BoxFace& face, QString& extraInfo) {
return _model.findRayIntersectionAgainstSubMeshes(origin, direction, distance, face, extraInfo);
}

View file

@ -26,9 +26,9 @@ public:
virtual void render(RenderArgs* args);
virtual void setProperties(const QScriptValue& properties);
virtual QScriptValue getProperty(const QString& property);
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face) const;
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face);
virtual bool findRayIntersectionExtraInfo(const glm::vec3& origin, const glm::vec3& direction,
float& distance, BoxFace& face, QString& extraInfo) const;
float& distance, BoxFace& face, QString& extraInfo);
virtual ModelOverlay* createClone() const;

View file

@ -92,7 +92,7 @@ QScriptValue Planar3DOverlay::getProperty(const QString& property) {
}
bool Planar3DOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
float& distance, BoxFace& face) const {
float& distance, BoxFace& face) {
RayIntersectionInfo rayInfo;
rayInfo._rayStart = origin;

View file

@ -39,7 +39,7 @@ public:
virtual void setProperties(const QScriptValue& properties);
virtual QScriptValue getProperty(const QString& property);
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face) const;
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face);
protected:
glm::vec2 _dimensions;

View file

@ -100,7 +100,7 @@ QScriptValue Volume3DOverlay::getProperty(const QString& property) {
}
bool Volume3DOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
float& distance, BoxFace& face) const {
float& distance, BoxFace& face) {
// extents is the entity relative, scaled, centered extents of the entity
glm::vec3 position = getPosition();

View file

@ -41,7 +41,7 @@ public:
virtual void setProperties(const QScriptValue& properties);
virtual QScriptValue getProperty(const QString& property);
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face) const;
virtual bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance, BoxFace& face);
protected:
glm::vec3 _dimensions;

View file

@ -475,7 +475,7 @@ bool EntityTreeElement::bestFitBounds(const glm::vec3& minPoint, const glm::vec3
bool EntityTreeElement::findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject) {
void** intersectedObject, float distanceToElementCube) {
// only called if we do intersect our bounding cube, but find if we actually intersect with entities...
@ -509,15 +509,32 @@ bool EntityTreeElement::findDetailedRayIntersection(const glm::vec3& origin, con
// we can use the AABox's ray intersection by mapping our origin and direction into the entity frame
// and testing intersection there.
qDebug() << "EntityTreeElement::findDetailedRayIntersection()....";
qDebug() << " origin:" << origin;
qDebug() << " checking entity:" << entity->getEntityItemID() << "-" << qPrintable(EntityTypes::getEntityTypeName(entity->getType()));
qDebug() << " distance:" << distance;
if (entityFrameBox.findRayIntersection(entityFrameOrigin, entityFrameDirection, localDistance, localFace)) {
qDebug() << " localDistance:" << localDistance;
if (localDistance < distance) {
qDebug() << " localDistance < distance... continue...";
// now ask the entity if we actually intersect
if (entity->supportsDetailedRayIntersection()) {
qDebug() << " entity->supportsDetailedRayIntersection()....";
if (entity->findDetailedRayIntersection(origin, direction, keepSearching, element, localDistance,
localFace, intersectedObject)) {
qDebug() << " localDistance (detailed):" << localDistance;
if (localDistance < distance) {
qDebug() << " localDistance < distance...";
qDebug() << " CHOOSING THIS ONE ---> " << entity->getEntityItemID() << "-" << qPrintable(EntityTypes::getEntityTypeName(entity->getType()));
distance = localDistance;
face = localFace;
*intersectedObject = (void*)entity;
@ -527,6 +544,10 @@ bool EntityTreeElement::findDetailedRayIntersection(const glm::vec3& origin, con
} else {
// if the entity type doesn't support a detailed intersection, then just return the non-AABox results
if (localDistance < distance) {
qDebug() << " localDistance < distance...";
qDebug() << " CHOOSING THIS ONE ---> " << entity->getEntityItemID() << "-" << qPrintable(EntityTypes::getEntityTypeName(entity->getType()));
distance = localDistance;
face = localFace;
*intersectedObject = (void*)entity;

View file

@ -137,7 +137,7 @@ public:
virtual bool canRayIntersect() const { return hasEntities(); }
virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject);
void** intersectedObject, float distanceToElementCube);
virtual bool findSpherePenetration(const glm::vec3& center, float radius,
glm::vec3& penetration, void** penetratedObject) const;

View file

@ -2044,6 +2044,7 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping,
extracted.mesh.meshExtents.minimum = glm::min(extracted.mesh.meshExtents.minimum, transformedVertex);
extracted.mesh.meshExtents.maximum = glm::max(extracted.mesh.meshExtents.maximum, transformedVertex);
extracted.mesh.modelTransform = modelTransform;
}
// look for textures, material properties

View file

@ -149,6 +149,7 @@ public:
QVector<FBXCluster> clusters;
Extents meshExtents;
glm::mat4 modelTransform;
bool isEye;

View file

@ -1339,28 +1339,54 @@ bool OctreeElement::findRayIntersection(const glm::vec3& origin, const glm::vec3
keepSearching = true; // assume that we will continue searching after this.
AACube cube = getAACube();
float localDistance;
float distanceToElementCube = std::numeric_limits<float>::max();
float distanceToElementDetails = distance;
BoxFace localFace;
qDebug() << "OctreeElement::findRayIntersection()....";
qDebug() << " origin:" << origin;
qDebug() << " checking element:" << cube;
qDebug() << " distance:" << distance;
// if the ray doesn't intersect with our cube, we can stop searching!
if (!cube.findRayIntersection(origin, direction, localDistance, localFace)) {
if (!cube.findRayIntersection(origin, direction, distanceToElementCube, localFace)) {
qDebug() << " didn't intersect cube... done searching...";
keepSearching = false; // no point in continuing to search
return false; // we did not intersect
}
qDebug() << " distanceToElementCube:" << distanceToElementCube;
// by default, we only allow intersections with leaves with content
if (!canRayIntersect()) {
return false; // we don't intersect with non-leaves, and we keep searching
}
// we did hit this element, so calculate appropriate distances
localDistance *= TREE_SCALE;
if (localDistance < distance) {
//localDistance *= TREE_SCALE;
// if the distance to the element cube is not less than the current best distance, then it's not possible
// for any details inside the cube to be closer so we don't need to consider them.
if (distanceToElementCube < distance) {
qDebug() << " distanceToElementCube < distance:" << (distanceToElementCube < distance);
qDebug() << " continue.... call... findDetailedRayIntersection()...";
//qDebug() << " distanceToElementCube < distance -- continue.... call... findDetailedRayIntersection()...";
if (findDetailedRayIntersection(origin, direction, keepSearching,
element, distance, face, intersectedObject)) {
distance = localDistance;
face = localFace;
return true;
element, distanceToElementDetails, face, intersectedObject, distanceToElementCube)) {
qDebug() << " findDetailedRayIntersection() -- intersected something";
if (distanceToElementDetails < distance) {
qDebug() << " distanceToElementDetails < distance -- THIS ONE IS GOOD -------";
distance = distanceToElementDetails;
face = localFace;
qDebug() << " distance:" << distance << " -- THIS ONE IS GOOD -------";
return true;
}
}
}
return false;
@ -1368,11 +1394,12 @@ bool OctreeElement::findRayIntersection(const glm::vec3& origin, const glm::vec3
bool OctreeElement::findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject) {
void** intersectedObject, float distanceToElementCube) {
// we did hit this element, so calculate appropriate distances
if (hasContent()) {
element = this;
distance = distanceToElementCube;
if (intersectedObject) {
*intersectedObject = this;
}

View file

@ -123,7 +123,7 @@ public:
virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
bool& keepSearching, OctreeElement*& element, float& distance, BoxFace& face,
void** intersectedObject);
void** intersectedObject, float distanceToElementCube);
virtual bool findSpherePenetration(const glm::vec3& center, float radius,
glm::vec3& penetration, void** penetratedObject) const;

View file

@ -252,6 +252,52 @@ bool findRayCapsuleIntersection(const glm::vec3& origin, const glm::vec3& direct
return true;
}
bool findRayTrianlgeIntersection(const glm::vec3& origin, const glm::vec3& direction,
const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, float& distance) {
glm::vec3 e1, e2, h, s, q;
float a, f, u, v, t;
e1 = v1 - v0;
e2 = v2 - v0;
h = glm::cross(direction, e2);
a = glm::dot(e1, h);
if (a > -0.00001 && a < 0.00001) {
return false;
}
f = 1/a;
s = origin - v0;
u = f * glm::dot(s,h);
if (u < 0.0 || u > 1.0) {
return false;
}
q = glm::cross(s, e1);
v = f * glm::dot(direction, q);
if (v < 0.0 || u + v > 1.0) {
return false;
}
// at this stage we can compute t to find out where the intersection point is on the line
t = f * glm::dot(e2,q);
// ray intersection
if (t > 0.00001) {
distance = t;
return true;
} else {
// this means that there is a line intersection but not a ray intersection
return false;
}
return false;
}
// Do line segments (r1p1.x, r1p1.y)--(r1p2.x, r1p2.y) and (r2p1.x, r2p1.y)--(r2p2.x, r2p2.y) intersect?
// from: http://ptspts.blogspot.com/2010/06/how-to-determine-if-two-line-segments.html
bool doLineSegmentsIntersect(glm::vec2 r1p1, glm::vec2 r1p2, glm::vec2 r2p1, glm::vec2 r2p2) {

View file

@ -76,6 +76,22 @@ bool findRaySphereIntersection(const glm::vec3& origin, const glm::vec3& directi
bool findRayCapsuleIntersection(const glm::vec3& origin, const glm::vec3& direction,
const glm::vec3& start, const glm::vec3& end, float radius, float& distance);
bool findRayTrianlgeIntersection(const glm::vec3& origin, const glm::vec3& direction,
const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, float& distance);
class Triangle {
public:
glm::vec3 v0;
glm::vec3 v1;
glm::vec3 v2;
};
inline bool findRayTrianlgeIntersection(const glm::vec3& origin, const glm::vec3& direction,
const Triangle& triangle, float& distance) {
return findRayTrianlgeIntersection(origin, direction, triangle.v0, triangle.v1, triangle.v2, distance);
}
bool doLineSegmentsIntersect(glm::vec2 r1p1, glm::vec2 r1p2, glm::vec2 r2p1, glm::vec2 r2p2);
bool isOnSegment(float xi, float yi, float xj, float yj, float xk, float yk);
int computeDirection(float xi, float yi, float xj, float yj, float xk, float yk);