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

Revision 6941, 8.2 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

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