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

Revision 5168, 8.3 kB (checked in by robert, 10 years ago)

From Paul Martz, added support to textured light points into osgSim and OpenFlight? loader, as part of the OpenFlight? v16.0 spec.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/GL>
2#include <osgProducer/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
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
193    // use an ArgumentParser object to manage the program arguments.
194    osg::ArgumentParser arguments(&argc,argv);
195
196    // set up the usage document, in case we need to print out how to use this program.
197    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use high quality light point, typically used for naviagional lights.");
198    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
199    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
200    arguments.getApplicationUsage()->addCommandLineOption("--sprites","Point sprites.");
201
202    // construct the viewer.
203    osgProducer::Viewer viewer(arguments);
204
205    // set up the value with sensible default event handlers.
206    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
207
208    // get details on keyboard and mouse bindings used by the viewer.
209    viewer.getUsage(*arguments.getApplicationUsage());
210
211    // if user request help write it out to cout.
212    if (arguments.read("-h") || arguments.read("--help"))
213    {
214        arguments.getApplicationUsage()->write(std::cout);
215        return 1;
216    }
217
218    usePointSprites = false;
219    while (arguments.read("--sprites")) { usePointSprites = true; };
220
221    // any option left unread are converted into errors to write out later.
222    arguments.reportRemainingOptionsAsUnrecognized();
223
224    // report any errors if they have occured when parsing the program aguments.
225    if (arguments.errors())
226    {
227        arguments.writeErrorMessages(std::cout);
228        return 1;
229    }
230   
231    osg::Group* rootnode = new osg::Group;
232
233    // load the nodes from the commandline arguments.
234    rootnode->addChild(osgDB::readNodeFiles(arguments));
235    rootnode->addChild(createLightPointsDatabase());
236    rootnode->addChild(CreateBlinkSequenceLightNode());
237   
238    // run optimization over the scene graph
239    osgUtil::Optimizer optimzer;
240    optimzer.optimize(rootnode);
241     
242    // add a viewport to the viewer and attach the scene graph.
243    viewer.setSceneData( rootnode );
244   
245    // create the windows and run the threads.
246    viewer.realize();
247
248    while( !viewer.done() )
249    {
250        // wait for all cull and draw threads to complete.
251        viewer.sync();
252
253        // update the scene by traversing it with the the update visitor which will
254        // call all node update callbacks and animations.
255        viewer.update();
256         
257        // fire off the cull and draw traversals of the scene.
258        viewer.frame();
259       
260    }
261   
262    // wait for all cull and draw threads to complete before exit.
263    viewer.sync();
264
265    return 0;
266}
Note: See TracBrowser for help on using the browser.