osg解析系列-[osgSim::Impostors] 提升渲染性能的快捷通道

本文详细展示了如何在C++中利用osgSim的Impostor功能优化大型MFC模型,并对相关库函数和优化过程进行了代码演示。
// M23.12.W7.osgSim.Impostor.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>
#include <iostream>

//osg
#include <osg/Texture2D>
#include <osg/ShapeDrawable>
#include <osg/MatrixTransform>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/PolygonMode>
#include <osg/PolygonOffset>
#include <osg/OccluderNode>
#include <osg/LineWidth>
#include <osg/io_utils>
#include <osg/CullFace>
#include <osg/Stencil>

#include <osg/PositionAttitudeTransform>
#include <osg/MatrixTransform>
#include <osg/CoordinateSystemNode>

// osgviewer
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
// osgDB
#include <osgDB/FileUtils>
#include <osgDB/fstream>
#include <osgDB/ReadFile>

// osgGA
#include <osgGA/NodeTrackerManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/SphericalManipulator>


//osgUtil
#include <osgUtil/Optimizer>

//osgManipulator
#include <osgManipulator/TabBoxDragger>
#include <osgManipulator/TabBoxTrackballDragger>
#include <osgManipulator/TabPlaneDragger>
#include <osgManipulator/TabPlaneTrackballDragger>
#include <osgManipulator/Scale1DDragger>
#include <osgManipulator/Scale2DDragger>
#include <osgManipulator/TrackballDragger>
#include <osgManipulator/Translate1DDragger>
#include <osgManipulator/Translate2DDragger>
#include <osgManipulator/TranslateAxisDragger>
#include <osgManipulator/TranslatePlaneDragger>
#include <osgManipulator/RotateCylinderDragger>
#include <osgManipulator/AntiSquish>

//osgsim
#include <osgSim/OverlayNode>
#include <osgSim/SphereSegment>
#include <osgSim/Impostor>
#include <osgSim/InsertImpostorsVisitor>
#include "TestManipulator.h"

//osglib
#ifdef _DEBUG
#pragma comment(lib,"OpenThreadsd.lib")
#pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgUtild.lib")
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"osgFXd.lib")
#pragma comment(lib,"osgGAd.lib")
#pragma comment(lib,"osgTextd.lib")
#pragma comment(lib,"osgViewerd.lib")

#pragma comment(lib,"osgShadowd.lib")
#pragma comment(lib,"osgManipulatord.lib")
#pragma comment(lib,"osgParticled.lib")
#pragma comment(lib,"osgSimd.lib")
#else
#pragma comment(lib,"OpenThreads.lib")
#pragma comment(lib,"osg.lib")
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"osgFX.lib")
#pragma comment(lib,"osgGA.lib")
#pragma comment(lib,"osgUtil.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgShadow.lib")
#pragma comment(lib,"osgManipulator.lib")
#pragma comment(lib,"osgSim.lib")
#endif // DEBUG

int main(int argc, char **argv)
{
	// use an ArgumentParser object to manage the program arguments.
	osg::ArgumentParser arguments(&argc, argv);

	// construct the viewer.
	osgViewer::Viewer viewer(arguments);
	// load the nodes from the commandline arguments.
	osg::ref_ptr<osg::Node> model = osgDB::readRefNodeFiles(arguments);
	if (!model)
	{
		OSG_NOTICE << "No model loaded, please specify and model on the the command line" << std::endl;
		return 0;
	}

	// the osgSim::InsertImpostorsVisitor used lower down to insert impostors
	// only operators on subclass of Group's, if the model top node is not
	// a group then it won't be able to insert an impostor.  We therefore
	// manually insert an impostor above the model.
	if (dynamic_cast<osg::Group*>(model.get()) == 0)
	{
		const osg::BoundingSphere& bs = model->getBound();
		if (bs.valid())
		{

			osgSim::Impostor* impostor = new osgSim::Impostor;

			// standard LOD settings
			impostor->addChild(model.get());
			impostor->setRange(0, 0.0f, 1e7f);
			impostor->setCenter(bs.center());

			// impostor specific settings.
			impostor->setImpostorThresholdToBound(5.0f);

			model = impostor;

		}
	}

	osg::ref_ptr<osg::Group> rootnode = new osg::Group;
	rootnode->addChild(model);

	// now insert impostors in the model using the InsertImpostorsVisitor.
	osgSim::InsertImpostorsVisitor ov;
	model->accept(ov);

	// insert the Impostors above groups and LOD's
	ov.insertImpostors();

	viewer.setSceneData(rootnode);

	// optimize the scene graph, remove redundant nodes and state etc.
	osgUtil::Optimizer optimizer;
	optimizer.optimize(rootnode);

	viewer.run();

    return 0;
}


运行结果:用较大的模型(MFC)做测试
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值