| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #include <osg/Group> |
|---|
| 13 | #include <osg/Geometry> |
|---|
| 14 | |
|---|
| 15 | #include <osgDB/ReadFile> |
|---|
| 16 | #include <osgUtil/Optimizer> |
|---|
| 17 | #include <osgProducer/Viewer> |
|---|
| 18 | |
|---|
| 19 | float random(float min,float max) { return min + (max-min)*(float)rand()/(float)RAND_MAX; } |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | struct DrawCallback : public osg::Drawable::DrawCallback |
|---|
| 23 | { |
|---|
| 24 | |
|---|
| 25 | DrawCallback(): |
|---|
| 26 | _firstTime(true) {} |
|---|
| 27 | |
|---|
| 28 | virtual void drawImplementation(osg::State& state,const osg::Drawable* drawable) const |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | if (!_firstTime) |
|---|
| 32 | { |
|---|
| 33 | _previousModelViewMatrix = _currentModelViewMatrix; |
|---|
| 34 | _currentModelViewMatrix = state.getModelViewMatrix(); |
|---|
| 35 | _inverseModelViewMatrix.invert(_currentModelViewMatrix); |
|---|
| 36 | |
|---|
| 37 | osg::Matrix T(_previousModelViewMatrix*_inverseModelViewMatrix); |
|---|
| 38 | |
|---|
| 39 | osg::Geometry* geometry = dynamic_cast<osg::Geometry*>(const_cast<osg::Drawable*>(drawable)); |
|---|
| 40 | osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()); |
|---|
| 41 | for(unsigned int i=0;i+1<vertices->size();i+=2) |
|---|
| 42 | { |
|---|
| 43 | (*vertices)[i+1] = (*vertices)[i]*T; |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | else |
|---|
| 47 | { |
|---|
| 48 | _currentModelViewMatrix = state.getModelViewMatrix(); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | _firstTime = false; |
|---|
| 52 | |
|---|
| 53 | drawable->drawImplementation(state); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | mutable bool _firstTime; |
|---|
| 57 | mutable osg::Matrix _currentModelViewMatrix; |
|---|
| 58 | mutable osg::Matrix _inverseModelViewMatrix; |
|---|
| 59 | mutable osg::Matrix _previousModelViewMatrix; |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | osg::Node* createScene(unsigned int noStars) |
|---|
| 66 | { |
|---|
| 67 | |
|---|
| 68 | osg::Geometry* geometry = new osg::Geometry; |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | osg::Vec3Array* vertices = new osg::Vec3Array(noStars*2); |
|---|
| 72 | geometry->setVertexArray(vertices); |
|---|
| 73 | |
|---|
| 74 | float min = -1.0f; |
|---|
| 75 | float max = 1.0f; |
|---|
| 76 | unsigned int j = 0; |
|---|
| 77 | unsigned int i = 0; |
|---|
| 78 | for(i=0;i<noStars;++i,j+=2) |
|---|
| 79 | { |
|---|
| 80 | (*vertices)[j].set(random(min,max),random(min,max),random(min,max)); |
|---|
| 81 | (*vertices)[j+1] = (*vertices)[j]+osg::Vec3(0.0f,0.0f,0.001f); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | osg::Vec4Array* colours = new osg::Vec4Array(1); |
|---|
| 86 | geometry->setColorArray(colours); |
|---|
| 87 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 88 | (*colours)[0].set(1.0f,1.0f,1.0f,1.0f); |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | geometry->addPrimitiveSet(new osg::DrawArrays(GL_LINES,0,noStars*2)); |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | osg::DrawElementsUShort* points = new osg::DrawElementsUShort(GL_POINTS,noStars); |
|---|
| 95 | geometry->addPrimitiveSet(points); |
|---|
| 96 | for(i=0;i<noStars;++i) |
|---|
| 97 | { |
|---|
| 98 | (*points)[i] = i*2; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | geometry->setUseDisplayList(false); |
|---|
| 102 | geometry->setDrawCallback(new DrawCallback); |
|---|
| 103 | |
|---|
| 104 | osg::Geode* geode = new osg::Geode; |
|---|
| 105 | geode->addDrawable(geometry); |
|---|
| 106 | geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 107 | |
|---|
| 108 | osg::Group* group = new osg::Group; |
|---|
| 109 | group->addChild(geode); |
|---|
| 110 | |
|---|
| 111 | return group; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | int main( int argc, char **argv ) |
|---|
| 115 | { |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 122 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models."); |
|---|
| 123 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 124 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | osgProducer::Viewer viewer(arguments); |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 138 | { |
|---|
| 139 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 140 | return 1; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | if (arguments.errors()) |
|---|
| 145 | { |
|---|
| 146 | arguments.writeErrorMessages(std::cout); |
|---|
| 147 | return 1; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | osg::ref_ptr<osg::Node> loadedModel = createScene(50000); |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | if (!loadedModel) |
|---|
| 156 | { |
|---|
| 157 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 158 | return 1; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | if (arguments.errors()) |
|---|
| 166 | { |
|---|
| 167 | arguments.writeErrorMessages(std::cout); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | viewer.setSceneData(loadedModel.get()); |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | viewer.realize(); |
|---|
| 175 | |
|---|
| 176 | while( !viewer.done() ) |
|---|
| 177 | { |
|---|
| 178 | |
|---|
| 179 | viewer.sync(); |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | viewer.update(); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | viewer.frame(); |
|---|
| 187 | |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | viewer.sync(); |
|---|
| 192 | |
|---|
| 193 | return 0; |
|---|
| 194 | } |
|---|