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

Revision 2727, 7.2 kB (checked in by robert, 10 years ago)

From Tom Jolly, Fixes to osgSim::BlinkSequence? to enable proper saving and
loading.

  • 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/Transform>
5#include <osg/Billboard>
6#include <osg/Geode>
7#include <osg/Group>
8#include <osg/ShapeDrawable>
9#include <osg/Notify>
10
11#include <osgDB/Registry>
12#include <osgDB/ReadFile>
13
14#include <osgUtil/Optimizer>
15
16#include <osgSim/LightPointNode>
17
18
19
20#define INTERPOLATE(member) lp.member = start.member*rstart + end.member*rend;
21
22void addToLightPointNode(osgSim::LightPointNode& lpn,osgSim::LightPoint& start,osgSim::LightPoint& end,unsigned int noSteps)
23{
24    if (noSteps<=1)
25    {
26        lpn.addLightPoint(start);
27        return;
28    }
29   
30    float rend = 0.0f;
31    float rdelta = 1.0f/((float)noSteps-1.0f);
32   
33    lpn.getLightPointList().reserve(noSteps);
34   
35    for(unsigned int i=0;i<noSteps;++i,rend+=rdelta)
36    {
37        float rstart = 1.0f-rend;
38        osgSim::LightPoint lp(start);
39        INTERPOLATE(_position)
40        INTERPOLATE(_intensity);
41        INTERPOLATE(_color);
42        INTERPOLATE(_radius);
43
44        lpn.addLightPoint(lp);
45       
46   }
47}
48
49#undef INTERPOLATE
50
51osg::Node* createLightPointsDatabase()
52{
53    osgSim::LightPoint start;
54    osgSim::LightPoint end;
55
56    start._position.set(-500.0f,-500.0f,0.0f);
57    start._color.set(1.0f,0.0f,0.0f,1.0f);
58   
59    end._position.set(500.0f,-500.0f,0.0f);
60    end._color.set(1.0f,1.0f,1.0f,1.0f);
61   
62    osg::Transform* transform = new osg::Transform;
63
64    osg::Vec3 start_delta(0.0f,10.0f,0.0f);
65    osg::Vec3 end_delta(0.0f,10.0f,1.0f);
66
67    int noStepsX = 100;
68    int noStepsY = 100;
69
70//     osgSim::BlinkSequence* bs = new osgSim::BlinkSequence;
71//     bs->addPulse(1.0,osg::Vec4(1.0f,0.0f,0.0f,1.0f));
72//     bs->addPulse(0.5,osg::Vec4(0.0f,0.0f,0.0f,0.0f)); // off
73//     bs->addPulse(1.5,osg::Vec4(1.0f,1.0f,0.0f,1.0f));
74//     bs->addPulse(0.5,osg::Vec4(0.0f,0.0f,0.0f,0.0f)); // off
75//     bs->addPulse(1.0,osg::Vec4(1.0f,1.0f,1.0f,1.0f));
76//     bs->addPulse(0.5,osg::Vec4(0.0f,0.0f,0.0f,0.0f)); // off
77   
78
79//    osgSim::Sector* sector = new osgSim::ConeSector(osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0),osg::inDegrees(45.0));
80//    osgSim::Sector* sector = new osgSim::ElevationSector(-osg::inDegrees(45.0),osg::inDegrees(45.0),osg::inDegrees(45.0));
81//    osgSim::Sector* sector = new osgSim::AzimSector(-osg::inDegrees(45.0),osg::inDegrees(45.0),osg::inDegrees(90.0));
82//     osgSim::Sector* sector = new osgSim::AzimElevationSector(osg::inDegrees(180),osg::inDegrees(90), // azim range
83//                                                                 osg::inDegrees(0.0),osg::inDegrees(90.0), // elevation range
84//                                                                 osg::inDegrees(5.0));
85
86    for(int i=0;i<noStepsY;++i)
87    {
88
89//         osgSim::BlinkSequence* local_bs = new osgSim::BlinkSequence(*bs);
90//         local_bs->setSequenceGroup(new osgSim::BlinkSequence::SequenceGroup((double)i*0.1));       
91//         start._blinkSequence = local_bs;
92
93//        start._sector = sector;
94
95        osgSim::LightPointNode* lpn = new osgSim::LightPointNode;
96        addToLightPointNode(*lpn,start,end,noStepsX);
97       
98        start._position += start_delta;
99        end._position += end_delta;
100       
101        transform->addChild(lpn);   
102    }
103       
104    osg::Group* group = new osg::Group;
105    group->addChild(transform);
106   
107   
108    return group;
109}
110
111static osg::Node* CreateBlinkSequenceLightNode()
112{
113   osgSim::LightPointNode*      lightPointNode = new osgSim::LightPointNode;;
114
115   osgSim::LightPointNode::LightPointList       lpList;
116
117   osg::ref_ptr<osgSim::SequenceGroup>   seq_0;
118   seq_0 = new osgSim::SequenceGroup;
119   seq_0->_baseTime = 0.0;
120
121   osg::ref_ptr<osgSim::SequenceGroup>   seq_1;
122   seq_1 = new osgSim::SequenceGroup;
123   seq_1->_baseTime = 0.5;
124
125   const int max_points = 32;
126   for( int i = 0; i < max_points; ++i )
127   {
128      osgSim::LightPoint   lp;
129      double x = cos( (2.0*osg::PI*i)/max_points );
130      double z = sin( (2.0*osg::PI*i)/max_points );
131      lp._position.set( x, 0.0f, z + 100.0f );
132      lp._blinkSequence = new osgSim::BlinkSequence;
133      for( int j = 10; j > 0; --j )
134      {
135         float  intensity = j/10.0f;
136         lp._blinkSequence->addPulse( 1.0/max_points,
137                                     osg::Vec4( intensity, intensity, intensity, intensity ) );
138      }
139      if( max_points > 10 )
140      {
141         lp._blinkSequence->addPulse( 1.0 - 10.0/max_points,
142                                     osg::Vec4( 0.0f, 0.0f, 0.0f, 0.0f ) );
143      }
144
145      if( i & 1 )
146      {
147         lp._blinkSequence->setSequenceGroup( seq_1.get() );
148      }
149      else
150      {
151         lp._blinkSequence->setSequenceGroup( seq_0.get() );
152      }
153      lp._blinkSequence->setPhaseShift( i/(static_cast<double>(max_points)) );
154      lpList.push_back( lp );
155   }
156
157   lightPointNode->setLightPointList( lpList );
158
159   return lightPointNode;
160}
161
162int main( int argc, char **argv )
163{
164
165    // use an ArgumentParser object to manage the program arguments.
166    osg::ArgumentParser arguments(&argc,argv);
167
168    // set up the usage document, in case we need to print out how to use this program.
169    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use high quality light point, typically used for naviagional lights.");
170    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
171    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
172
173    // construct the viewer.
174    osgProducer::Viewer viewer(arguments);
175
176    // set up the value with sensible default event handlers.
177    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
178
179    // get details on keyboard and mouse bindings used by the viewer.
180    viewer.getUsage(*arguments.getApplicationUsage());
181
182    // if user request help write it out to cout.
183    if (arguments.read("-h") || arguments.read("--help"))
184    {
185        arguments.getApplicationUsage()->write(std::cout);
186        return 1;
187    }
188
189    // any option left unread are converted into errors to write out later.
190    arguments.reportRemainingOptionsAsUnrecognized();
191
192    // report any errors if they have occured when parsing the program aguments.
193    if (arguments.errors())
194    {
195        arguments.writeErrorMessages(std::cout);
196        return 1;
197    }
198   
199    osg::Group* rootnode = new osg::Group;
200
201    // load the nodes from the commandline arguments.
202    rootnode->addChild(osgDB::readNodeFiles(arguments));
203    rootnode->addChild(createLightPointsDatabase());
204    rootnode->addChild(CreateBlinkSequenceLightNode());
205   
206    // run optimization over the scene graph
207    osgUtil::Optimizer optimzer;
208    optimzer.optimize(rootnode);
209     
210    // add a viewport to the viewer and attach the scene graph.
211    viewer.setSceneData( rootnode );
212   
213    // create the windows and run the threads.
214    viewer.realize();
215
216    while( !viewer.done() )
217    {
218        // wait for all cull and draw threads to complete.
219        viewer.sync();
220
221        // update the scene by traversing it with the the update visitor which will
222        // call all node update callbacks and animations.
223        viewer.update();
224         
225        // fire off the cull and draw traversals of the scene.
226        viewer.frame();
227       
228    }
229   
230    // wait for all cull and draw threads to complete before exit.
231    viewer.sync();
232
233    return 0;
234}
Note: See TracBrowser for help on using the browser.