OSG載入傾斜攝影數據
1. 概述
ContextCapture(Smart3D)生成的傾斜攝影模型數據一般都形如如下組織結構:
在Data目錄下包含了分塊的瓦片數據,每個瓦片都是一個LOD文件夾。osg能夠直接讀取osgb格式,理論上只需要依次載入每個LOD的金字塔層級最高的osgb,整個傾斜攝影模型數據就載入進來了。不過有點麻煩的是這類數據缺乏一個整體載入的入口,如果每次載入都遍歷整個文件夾載入的話,會影響載入的效率。所以一般的數據查看軟體都會為其增加一個索引。
這裡就給傾斜攝影數據添加一個osgb格式的索引文件,生成後就可以通過OSG直接載入整個傾斜攝影模型數據。
2. 實例
2.1. 程式碼
具體的實現程式碼如下:
#include <iostream>
#include <string>
#include <QDir>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
using namespace std;
//查找目錄下所有的文件夾
static void findDir(string dir, vector<string>& subDirs)
{
//
subDirs.clear();
QDir fromDir(QString::fromLocal8Bit(dir.c_str()));
QStringList filters;
//
QFileInfoList fileInfoList = fromDir.entryInfoList(filters, QDir::AllDirs | QDir::Files);
foreach(QFileInfo fileInfo, fileInfoList)
{
if (fileInfo.fileName() == "." || fileInfo.fileName() == "..")
{
continue;
}
if (fileInfo.isDir())
{
QByteArray dir = fileInfo.filePath().toLocal8Bit();
subDirs.push_back(dir.data());
}
}
}
//得到文件路徑的文件名 C:\\b\\a(.txt) -> a
static std::string DirOrPathGetName(std::string filePath)
{
size_t m = filePath.find_last_of('/');
if (m == string::npos)
{
return filePath;
}
size_t p = filePath.find_last_of('.');
if (p != string::npos && p > m) //沒有點號或者
{
filePath.erase(p);
}
std::string dirPath = filePath;
dirPath.erase(0, m + 1);
return dirPath;
}
void createObliqueIndexes(std::string fileDir)
{
string dataDir = fileDir + "/Data";
osg::ref_ptr<osg::Group> group = new osg::Group();
vector<string> subDirs;
findDir(dataDir, subDirs);
for (size_t i = 0; i < subDirs.size(); i++)
{
string name = DirOrPathGetName(subDirs[i]);
string path = subDirs[i] + "/" + name + ".osgb";
osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(path);
osg::ref_ptr<osg::PagedLOD> lod = new osg::PagedLOD();
auto bs = node->getBound();
auto c = bs.center();
auto r = bs.radius();
lod->setCenter(c);
lod->setRadius(r);
lod->setRangeMode(osg::LOD::RangeMode::PIXEL_SIZE_ON_SCREEN);
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->getOrCreateStateSet();
lod->addChild(geode.get());
std::string relativeFilePath = "./Data/" + name + "/" + name + ".osgb"; //相對路徑
lod->setFileName(0, "");
lod->setFileName(1, relativeFilePath);
lod->setRange(0, 0, 1.0); //第一層不可見
lod->setRange(1, 1.0, FLT_MAX);
lod->setDatabasePath("");
group->addChild(lod);
}
std::string outputLodFile = fileDir + "/Data.osgb";
osgDB::writeNodeFile(*group, outputLodFile);
}
int main(int argc, char *argv[])
{
string fileDir = "D:/Data/scene/city";
std::string outputLodFile = fileDir + "/Data.osgb";
createObliqueIndexes(fileDir);
osgViewer::Viewer viewer;
osg::Node * node = new osg::Node;
node = osgDB::readNodeFile(outputLodFile);
viewer.setSceneData(node);
return viewer.run();
}
2.2. 解析
如果直接讀取每一塊的LOD然後通過osgDB::writeNodeFile寫入到一個osgb文件,這個文件就會保存所有塊的LOD第一層資訊。這樣在第二冊載入的時候還是會比較慢,所以這裡就創建了一個空的節點,形成了索引所有LOD塊的數據結構。對於每一塊數據,新建兩層LOD,第一層為自身的空白節點,第二層為分塊LOD的第一層數據:
osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(path);
osg::ref_ptr<osg::PagedLOD> lod = new osg::PagedLOD();
auto bs = node->getBound();
auto c = bs.center();
auto r = bs.radius();
lod->setCenter(c);
lod->setRadius(r);
lod->setRangeMode(osg::LOD::RangeMode::PIXEL_SIZE_ON_SCREEN);
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->getOrCreateStateSet();
lod->addChild(geode.get());
std::string relativeFilePath = "./Data/" + name + "/" + name + ".osgb"; //相對路徑
lod->setFileName(0, "");
lod->setFileName(1, relativeFilePath);
lod->setRange(0, 0, 1.0); //第一層不可見
lod->setRange(1, 1.0, FLT_MAX);
lod->setDatabasePath("");
group->addChild(lod);
LOD的Center和Radius都非常重要,需要預先設置好;setRangeMode設置了細節層級調度的模式,一般都為PIXEL_SIZE_ON_SCREEN;setFileName設置了每一層的數據路徑,setRange確定了當前層級的範圍。由於這個LOD只是個索引文件,所以會設置第二層為極大的可見範圍值。
3. 結果
可以像載入普通OSGB文件一樣載入這個索引文件,通過osgviewer載入的效果如下: