| 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 | |
|---|
| 25 | void 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 | |
|---|
| 54 | bool usePointSprites; |
|---|
| 55 | |
|---|
| 56 | osg::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 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | for(int i=0;i<noStepsY;++i) |
|---|
| 95 | { |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 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 | |
|---|
| 139 | static 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 | |
|---|
| 190 | int main( int argc, char **argv ) |
|---|
| 191 | { |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 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 | |
|---|
| 203 | osgProducer::Viewer viewer(arguments); |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 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 | |
|---|
| 222 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | if (arguments.errors()) |
|---|
| 226 | { |
|---|
| 227 | arguments.writeErrorMessages(std::cout); |
|---|
| 228 | return 1; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | osg::Group* rootnode = new osg::Group; |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | rootnode->addChild(osgDB::readNodeFiles(arguments)); |
|---|
| 235 | rootnode->addChild(createLightPointsDatabase()); |
|---|
| 236 | rootnode->addChild(CreateBlinkSequenceLightNode()); |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | osgUtil::Optimizer optimzer; |
|---|
| 240 | optimzer.optimize(rootnode); |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | viewer.setSceneData( rootnode ); |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | viewer.realize(); |
|---|
| 247 | |
|---|
| 248 | while( !viewer.done() ) |
|---|
| 249 | { |
|---|
| 250 | |
|---|
| 251 | viewer.sync(); |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | viewer.update(); |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | viewer.frame(); |
|---|
| 259 | |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | viewer.sync(); |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | viewer.cleanup_frame(); |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | viewer.sync(); |
|---|
| 270 | |
|---|
| 271 | return 0; |
|---|
| 272 | } |
|---|