osg官方的四个程序:
osgViewer: 四个程序当中用的最多的 OSG 程序,用于查看模型;
osgVersion: 查看osg库版本;
osgArchive: 类似于zip,可将多个.osg模型打包成一个.osga的模型压缩包;
osgConv: 模型格式转换与压缩;
接下来进入正题:
前提条件是假设大家已经完成了osg的编译。
接下来自己新建一个项目,引用之类的基础操作也完成了,开始编写以下代码:
osgt1.cpp
// 1
// 加载osgb的测试
//#include <Windows.h> 添加之后解决一个报错的问题
#include <Windows.h>
#include <osg/Notify>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/ref_ptr>
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osgViewer/Viewer>
#include <iostream>
int main(int argc, char** argv)
{
// use an ArgumentParser object to manage the program arguments. 使用一个参数解析器对象用来管理程序参数
osg::ArgumentParser arguments(&argc, argv);
// osg读取一个节点的方法
// osg::ref_ptr<osg::Node> pNode = osgDB::readNodeFile("cow.osgt");
// Load default mode 加载默认节点
osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFile("E:\\GIS\\广州\\关于树的研究\\OSGB\\data\\Tile_+041_-005\\Tile_+041_-005.osgb");
// if no model has been successfully loaded report failure.模型没有正确加载的话,报告错误
if (!loadedModel)
{
std::cout << arguments.getApplicationName() << ": No data loaded" << std::endl;
return 1;
}/*
else
{
return 0;
}*/
// construct the viewer.构造视口
osgViewer::Viewer viewer;
// different thread model 不同线程模型
// enum ThreadingModel
/*{
SingleThreaded,单一线程
CullDrawThreadPerContext,每条内容精选绘制线程 每条内容线程
ThreadPerContext = CullDrawThreadPerContext,
DrawThreadPerContext,绘制线程
CullThreadPerCameraDrawThreadPerContext,每个相机精选线程每条内容绘制线程 每个相机线程
ThreadPerCamera = CullThreadPerCameraDrawThreadPerContext,
AutomaticSelection 自动选择
};*/
osgViewer::Viewer::ThreadingModel threadingModel = osgViewer::Viewer::AutomaticSelection;
while (arguments.read("-s")) { threadingModel = osgViewer::Viewer::SingleThreaded; }
while (arguments.read("-g")) { threadingModel = osgViewer::Viewer::CullDrawThreadPerContext; }
while (arguments.read("-d")) { threadingModel = osgViewer::Viewer::DrawThreadPerContext; }
while (arguments.read("-c")) { threadingModel = osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext; }
viewer.setThreadingModel(threadingModel);
int xoffset = 40;
int yoffset = 40;
// left window + left slave camera 左窗口+左控制相机
{
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->x = xoffset + 0;
traits->y = yoffset + 0;
traits->width = 600;
traits->height = 480;
traits->windowDecoration = true;
traits->doubleBuffer = true;
traits->sharedContext = 0;
traits->readDISPLAY();
traits->setUndefinedScreenDetailsToDefaultScreen();
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext(gc.get());
camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
camera->setDrawBuffer(buffer);
camera->setReadBuffer(buffer);
// add this slave camera to the viewer, with a shift left of the projection matrix 将这个从相机添加到查看器中,并将投影矩阵向左移位
//viewer.addSlave(camera.get(), osg::Matrixd::translate(1.0, 0.0, 0.0), osg::Matrixd());
viewer.addSlave(camera.get(), osg::Matrixd::translate(0.0, 0.0, 0.0), osg::Matrixd());
}
right window + right slave camera 右窗口+右控制相机
//{
// osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
// traits->x = xoffset + 600;
// traits->y = yoffset + 0;
// traits->width = 600;
// traits->height = 480;
// traits->windowDecoration = true;
// traits->doubleBuffer = true;
// traits->sharedContext = 0;
// traits->readDISPLAY();
// traits->setUndefinedScreenDetailsToDefaultScreen();
// osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
// osg::ref_ptr<osg::Camera> camera = new osg::Camera;
// camera->setGraphicsContext(gc.get());
// camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
// GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
// camera->setDrawBuffer(buffer);
// camera->setReadBuffer(buffer);
// // add this slave camera to the viewer, with a shift right of the projection matrix
// viewer.addSlave(camera.get(), osg::Matrixd::translate(-1.0, 0.0, 0.0), osg::Matrixd());
//}
// optimize the scene graph, remove redundant nodes and state etc.优化场景图,去除冗余节点和状态等
osgUtil::Optimizer optimizer;
optimizer.optimize(loadedModel);
// set the scene to render 渲染场景
viewer.setSceneData(loadedModel);
return viewer.run();
}
运行起来之后效果如下:
单个的osgb文件我们大概了解了,osgb的data全文件呢? 下篇继续。。。