osg解析系列-[osgSim::LightPointNode]光点的应用示例(如:模拟街边的一排路灯)

osgSim是仿真计算库,osgSim::LightPointNode可用来模拟街边的一排路灯。代码如下:

// M23.12.W9.osgSim.LightPoint.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include 

//osg
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 

// osgviewer
#include 
#include 
// osgDB
#include 
#include 
#include 

// osgGA
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


//osgUtil
#include 

//osgManipulator
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//osgsim
#include 
#include 
#include 
#include 
#include 

//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 a ArgumentParser object to manage the program arguments
	osg::ArgumentParser Parser(&argc, argv);

	//construct the viewer
	osgViewer::Viewer viewer(Parser);

	//rootnode of the scene 
	osg::ref_ptr<osg::Group> rootnode = new osg::Group;

	auto CreateBlinkSequenceLightNode = []()->osg::Node*
	{
		osgSim::LightPointNode*      lightPointNode = new osgSim::LightPointNode;;

		osgSim::LightPointNode::LightPointList       lpList;

		osg::ref_ptr<osgSim::SequenceGroup>   seq_0;
		seq_0 = new osgSim::SequenceGroup;
		seq_0->_baseTime = 0.0;

		osg::ref_ptr<osgSim::SequenceGroup>   seq_1;
		seq_1 = new osgSim::SequenceGroup;
		seq_1->_baseTime = 0.5;

		const int max_points = 32;
		for (int i = 0; i < max_points; ++i)
		{
			osgSim::LightPoint   lp;
			double x = cos((2.0*osg::PI*i) / max_points);
			double z = sin((2.0*osg::PI*i) / max_points);
			lp._position.set(x, 0.0f, z + 30.0f);
			lp._blinkSequence = new osgSim::BlinkSequence;
			for (int j = 10; j > 0; --j)
			{
				float  intensity = j / 10.0f;
				lp._blinkSequence->addPulse(1.0 / max_points,
					osg::Vec4(intensity, intensity, intensity, intensity));
			}
			if (max_points > 10)
			{
				lp._blinkSequence->addPulse(1.0 - 10.0 / max_points,
					osg::Vec4(0.0f, 0.0f, 0.0f, 0.0f));
			}

			if (i & 1)
			{
				lp._blinkSequence->setSequenceGroup(seq_1.get());
			}
			else
			{
				lp._blinkSequence->setSequenceGroup(seq_0.get());
			}
			lp._blinkSequence->setPhaseShift(i / (static_cast<double>(max_points)));
			lpList.push_back(lp);
		}

		lightPointNode->setLightPointList(lpList);

		return lightPointNode;
	};

	//create lightpoint
	rootnode->addChild(CreateBlinkSequenceLightNode());

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

	viewer.run();

    return 0;
}


运行效果:
osg解析系列-[osgSim::LightPointNode]光点的应用示例(如:模拟街边的一排路灯)_第1张图片

你可能感兴趣的:(c++,图形渲染)