From 5921e7062544ae19c1d78fd8a1e5a98ddbdb0de2 Mon Sep 17 00:00:00 2001 From: scrawl Date: Tue, 9 Jun 2015 02:29:56 +0200 Subject: [PATCH] Build a kdtree for terrain geometry Improves intersection testing performance, shaving off ~2ms of frame time in exteriors. Also increases terrain loading time by ~1ms per cell, so will have to look into background loading soon. --- components/terrain/terraingrid.cpp | 5 +++++ components/terrain/terraingrid.hpp | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/components/terrain/terraingrid.cpp b/components/terrain/terraingrid.cpp index 322c15196d..c8f8caef8e 100644 --- a/components/terrain/terraingrid.cpp +++ b/components/terrain/terraingrid.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include @@ -66,6 +67,7 @@ namespace Terrain TerrainGrid::TerrainGrid(osg::Group* parent, Resource::ResourceSystem* resourceSystem, osgUtil::IncrementalCompileOperation* ico, Storage* storage, int nodeMask) : Terrain::World(parent, resourceSystem, ico, storage, nodeMask) + , mKdTreeBuilder(new osg::KdTreeBuilder) { } @@ -133,6 +135,9 @@ void TerrainGrid::loadCell(int x, int y) osg::ref_ptr geode (new osg::Geode); geode->addDrawable(geometry); + // build a kdtree to speed up intersection tests with the terrain + geode->accept(*mKdTreeBuilder); + std::vector layerList; std::vector > blendmaps; mStorage->getBlendmaps(1.f, center, false, blendmaps, layerList); diff --git a/components/terrain/terraingrid.hpp b/components/terrain/terraingrid.hpp index 3a6d717933..a697297b5a 100644 --- a/components/terrain/terraingrid.hpp +++ b/components/terrain/terraingrid.hpp @@ -25,6 +25,11 @@ #include "world.hpp" #include "material.hpp" +namespace osg +{ + class KdTreeBuilder; +} + namespace Terrain { @@ -44,6 +49,8 @@ namespace Terrain private: typedef std::map, GridElement*> Grid; Grid mGrid; + + osg::ref_ptr mKdTreeBuilder; }; }