root/OpenSceneGraph/trunk/examples/osglightpoint/osglightpoint.cpp @ 5927

Revision 5927, 7.3 kB (checked in by robert, 8 years ago)

Ported following examples to osgViewer:

osggeodemo
osggeometry
osghud
osgimpostor
osgkeyboard
osglauncher
osglight
osglightpoint
osglogicop
osglogo
osgmovie
osgmultiplecameras
osgmultitexture
osgoccluder
osgparametric
osgparticle

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/GL>
2#include <osgViewer/Viewer>
3
4#include <osg/MatrixTransform>
5#include <osg/Billboard>
6#include <osg/Geode>
7#include <osg/Group>
8#include <osg/ShapeDrawable>
9#include <osg/Notify>
10#include <osg/PointSprite>
11#include <osg/Texture2D>
12#include <osg/BlendFunc>
13
14#include <osgDB/Registry>
15#include <osgDB/ReadFile>
16
17#include <osgUtil/Optimizer>
18
19#include <osgSim/LightPointNode>
20
21#include <iostream>
22
23#define INTERPOLATE(member) lp.member = start.member*rstart + end.member*rend;
24
25void addToLightPointNode(osgSim::LightPointNode& lpn,osgSim::LightPoint& start,osgSim::LightPoint& end,unsigned int noSteps)
26{
27    if (noSteps<=1)
28    {
29        lpn.addLightPoint(start);
30        return;
31    }
32   
33    float rend = 0.0f;
34    float rdelta = 1.0f/((float)noSteps-1.0f);
35   
36    lpn.getLightPointList().reserve(noSteps);
37   
38    for(unsigned int i=0;i<noSteps;++i,rend+=rdelta)
39    {
40        float rstart = 1.0f-rend;
41        osgSim::LightPoint lp(start);
42        INTERPOLATE(_position)
43        INTERPOLATE(_intensity);
44        INTERPOLATE(_color);
45        INTERPOLATE(_radius);
46
47        lpn.addLightPoint(lp);
48       
49   }
50}
51
52#undef INTERPOLATE
53
54bool usePointSprites;
55
56osg::Node* createLightPointsDatabase()
57{
58    osgSim::LightPoint start;
59    osgSim::LightPoint end;
60
61    start._position.set(-500.0f,-500.0f,0.0f);
62    start._color.set(1.0f,0.0f,0.0f,1.0f);
63   
64    end._position.set(500.0f,-500.0f,0.0f);
65    end._color.set(1.0f,1.0f,1.0f,1.0f);
66   
67    osg::MatrixTransform* transform = new osg::MatrixTransform;
68   
69    transform->setDataVariance(osg::Object::STATIC);
70    transform->setMatrix(osg::Matrix::scale(0.1,0.1,0.1));
71
72    osg::Vec3 start_delta(0.0f,10.0f,0.0f);
73    osg::Vec3 end_delta(0.0f,10.0f,1.0f);
74
75    int noStepsX = 100;
76    int noStepsY = 100;
77
78//     osgSim::BlinkSequence* bs = new osgSim::BlinkSequence;
79//     bs->addPulse(1.0,osg::Vec4(1.0f,0.0f,0.0f,1.0f));
80//     bs->addPulse(0.5,osg::Vec4(0.0f,0.0f,0.0f,0.0f)); // off
81//     bs->addPulse(1.5,osg::Vec4(1.0f,1.0f,0.0f,1.0f));
82//     bs->addPulse(0.5,osg::Vec4(0.0f,0.0f,0.0f,0.0f)); // off
83//     bs->addPulse(1.0,osg::Vec4(1.0f,1.0f,1.0f,1.0f));
84//     bs->addPulse(0.5,osg::Vec4(0.0f,0.0f,0.0f,0.0f)); // off
85   
86
87//    osgSim::Sector* sector = new osgSim::ConeSector(osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0),osg::inDegrees(45.0));
88//    osgSim::Sector* sector = new osgSim::ElevationSector(-osg::inDegrees(45.0),osg::inDegrees(45.0),osg::inDegrees(45.0));
89//    osgSim::Sector* sector = new osgSim::AzimSector(-osg::inDegrees(45.0),osg::inDegrees(45.0),osg::inDegrees(90.0));
90//     osgSim::Sector* sector = new osgSim::AzimElevationSector(osg::inDegrees(180),osg::inDegrees(90), // azim range
91//                                                                 osg::inDegrees(0.0),osg::inDegrees(90.0), // elevation range
92//                                                                 osg::inDegrees(5.0));
93
94    for(int i=0;i<noStepsY;++i)
95    {
96
97//         osgSim::BlinkSequence* local_bs = new osgSim::BlinkSequence(*bs);
98//         local_bs->setSequenceGroup(new osgSim::BlinkSequence::SequenceGroup((double)i*0.1));       
99//         start._blinkSequence = local_bs;
100
101//        start._sector = sector;
102
103        osgSim::LightPointNode* lpn = new osgSim::LightPointNode;
104
105        //
106        osg::StateSet* set = lpn->getOrCreateStateSet();
107
108        if (usePointSprites)
109        {
110            lpn->setPointSprite();
111
112            // Set point sprite texture in LightPointNode StateSet.
113            osg::Texture2D *tex = new osg::Texture2D();
114            tex->setImage(osgDB::readImageFile("Images/particle.rgb"));
115            set->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON);
116        }
117
118        //set->setMode(GL_BLEND, osg::StateAttribute::ON);
119        //osg::BlendFunc *fn = new osg::BlendFunc();
120        //fn->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::DST_ALPHA);
121        //set->setAttributeAndModes(fn, osg::StateAttribute::ON);
122        //
123
124        addToLightPointNode(*lpn,start,end,noStepsX);
125       
126        start._position += start_delta;
127        end._position += end_delta;
128       
129        transform->addChild(lpn);   
130    }
131       
132    osg::Group* group = new osg::Group;
133    group->addChild(transform);
134   
135   
136    return group;
137}
138
139static osg::Node* CreateBlinkSequenceLightNode()
140{
141   osgSim::LightPointNode*      lightPointNode = new osgSim::LightPointNode;;
142
143   osgSim::LightPointNode::LightPointList       lpList;
144
145   osg::ref_ptr<osgSim::SequenceGroup>   seq_0;
146   seq_0 = new osgSim::SequenceGroup;
147   seq_0->_baseTime = 0.0;
148
149   osg::ref_ptr<osgSim::SequenceGroup>   seq_1;
150   seq_1 = new osgSim::SequenceGroup;
151   seq_1->_baseTime = 0.5;
152
153   const int max_points = 32;
154   for( int i = 0; i < max_points; ++i )
155   {
156      osgSim::LightPoint   lp;
157      double x = cos( (2.0*osg::PI*i)/max_points );
158      double z = sin( (2.0*osg::PI*i)/max_points );
159      lp._position.set( x, 0.0f, z + 30.0f );
160      lp._blinkSequence = new osgSim::BlinkSequence;
161      for( int j = 10; j > 0; --j )
162      {
163         float  intensity = j/10.0f;
164         lp._blinkSequence->addPulse( 1.0/max_points,
165                                     osg::Vec4( intensity, intensity, intensity, intensity ) );
166      }
167      if( max_points > 10 )
168      {
169         lp._blinkSequence->addPulse( 1.0 - 10.0/max_points,
170                                     osg::Vec4( 0.0f, 0.0f, 0.0f, 0.0f ) );
171      }
172
173      if( i & 1 )
174      {
175         lp._blinkSequence->setSequenceGroup( seq_1.get() );
176      }
177      else
178      {
179         lp._blinkSequence->setSequenceGroup( seq_0.get() );
180      }
181      lp._blinkSequence->setPhaseShift( i/(static_cast<double>(max_points)) );
182      lpList.push_back( lp );
183   }
184
185   lightPointNode->setLightPointList( lpList );
186
187   return lightPointNode;
188}
189
190int main( int argc, char **argv )
191{
192    // use an ArgumentParser object to manage the program arguments.
193    osg::ArgumentParser arguments(&argc,argv);
194
195    // set up the usage document, in case we need to print out how to use this program.
196    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use high quality light point, typically used for naviagional lights.");
197    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
198    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
199    arguments.getApplicationUsage()->addCommandLineOption("--sprites","Point sprites.");
200
201    // construct the viewer.
202    osgViewer::Viewer viewer;
203
204    // if user request help write it out to cout.
205    if (arguments.read("-h") || arguments.read("--help"))
206    {
207        arguments.getApplicationUsage()->write(std::cout);
208        return 1;
209    }
210
211    usePointSprites = false;
212    while (arguments.read("--sprites")) { usePointSprites = true; };
213
214    osg::Group* rootnode = new osg::Group;
215
216    // load the nodes from the commandline arguments.
217    rootnode->addChild(osgDB::readNodeFiles(arguments));
218    rootnode->addChild(createLightPointsDatabase());
219    rootnode->addChild(CreateBlinkSequenceLightNode());
220   
221    // run optimization over the scene graph
222    osgUtil::Optimizer optimzer;
223    optimzer.optimize(rootnode);
224     
225    // add a viewport to the viewer and attach the scene graph.
226    viewer.setSceneData( rootnode );
227   
228    return viewer.run();
229}
Note: See TracBrowser for help on using the browser.