Skip to content

Commit

Permalink
Check intersection with all relevant nodes to find the closest inters…
Browse files Browse the repository at this point in the history
…ection
  • Loading branch information
wonder-sk committed Jun 28, 2018
1 parent 255aa9e commit 1002a14
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/3d/terrain/qgsterrainentity_p.cpp
Expand Up @@ -102,21 +102,31 @@ bool QgsTerrainEntity::rayIntersection( const QgsRayCastingUtils::Ray3D &ray, QV
if ( mMap.terrainGenerator()->type() != QgsTerrainGenerator::Dem )
return false; // currently only working with DEM terrain

float minDist = -1;

QList<QgsChunkNode *> lst = activeNodes();
for ( QgsChunkNode *n : lst )
{
if ( n->entity() && QgsRayCastingUtils::rayBoxIntersection( ray, n->bbox() ) )
if ( n->entity() && ( minDist < 0 || n->bbox().distanceFromPoint( ray.origin() ) < minDist ) && QgsRayCastingUtils::rayBoxIntersection( ray, n->bbox() ) )
{
Qt3DRender::QGeometryRenderer *rend = n->entity()->findChild<Qt3DRender::QGeometryRenderer *>();
Qt3DRender::QGeometry *geom = rend->geometry();
DemTerrainTileGeometry *demGeom = static_cast<DemTerrainTileGeometry *>( geom );
Qt3DCore::QTransform *tr = n->entity()->findChild<Qt3DCore::QTransform *>();
if ( demGeom->rayIntersection( ray, tr->matrix(), intersectionPoint ) )
return true;
QVector3D nodeIntPoint;
if ( demGeom->rayIntersection( ray, tr->matrix(), nodeIntPoint ) )
{
float dist = ( ray.origin() - intersectionPoint ).length();
if ( minDist < 0 || dist < minDist )
{
minDist = dist;
intersectionPoint = nodeIntPoint;
}
}
}
}

return false;
return minDist >= 0;
}

void QgsTerrainEntity::onShowBoundingBoxesChanged()
Expand Down

0 comments on commit 1002a14

Please sign in to comment.