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

Revision 4718, 7.4 kB (checked in by robert, 8 years ago)

Moved the blink sequence light points down so they are visible in the default
camera position.

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