| 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 | |
|---|
| 22 | void 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 | |
|---|
| 51 | osg::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 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | for(int i=0;i<noStepsY;++i) |
|---|
| 90 | { |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 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 | |
|---|
| 114 | static 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 | |
|---|
| 165 | int main( int argc, char **argv ) |
|---|
| 166 | { |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 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 | |
|---|
| 177 | osgProducer::Viewer viewer(arguments); |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 187 | { |
|---|
| 188 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 189 | return 1; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | if (arguments.errors()) |
|---|
| 197 | { |
|---|
| 198 | arguments.writeErrorMessages(std::cout); |
|---|
| 199 | return 1; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | osg::Group* rootnode = new osg::Group; |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | rootnode->addChild(osgDB::readNodeFiles(arguments)); |
|---|
| 206 | rootnode->addChild(createLightPointsDatabase()); |
|---|
| 207 | rootnode->addChild(CreateBlinkSequenceLightNode()); |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | osgUtil::Optimizer optimzer; |
|---|
| 211 | optimzer.optimize(rootnode); |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | viewer.setSceneData( rootnode ); |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | viewer.realize(); |
|---|
| 218 | |
|---|
| 219 | while( !viewer.done() ) |
|---|
| 220 | { |
|---|
| 221 | |
|---|
| 222 | viewer.sync(); |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | viewer.update(); |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | viewer.frame(); |
|---|
| 230 | |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | viewer.sync(); |
|---|
| 235 | |
|---|
| 236 | return 0; |
|---|
| 237 | } |
|---|