| [6941] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| [2697] | 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osg/Group> |
|---|
| 20 | #include <osg/Geometry> |
|---|
| 21 | |
|---|
| 22 | #include <osgDB/ReadFile> |
|---|
| 23 | #include <osgUtil/Optimizer> |
|---|
| [5959] | 24 | #include <osgViewer/Viewer> |
|---|
| [2697] | 25 | |
|---|
| 26 | float random(float min,float max) { return min + (max-min)*(float)rand()/(float)RAND_MAX; } |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | struct DrawCallback : public osg::Drawable::DrawCallback |
|---|
| 30 | { |
|---|
| 31 | |
|---|
| 32 | DrawCallback(): |
|---|
| 33 | _firstTime(true) {} |
|---|
| 34 | |
|---|
| [6422] | 35 | virtual void drawImplementation(osg::RenderInfo& renderInfo,const osg::Drawable* drawable) const |
|---|
| [2697] | 36 | { |
|---|
| [6422] | 37 | osg::State& state = *renderInfo.getState(); |
|---|
| [2697] | 38 | |
|---|
| 39 | if (!_firstTime) |
|---|
| 40 | { |
|---|
| 41 | _previousModelViewMatrix = _currentModelViewMatrix; |
|---|
| 42 | _currentModelViewMatrix = state.getModelViewMatrix(); |
|---|
| 43 | _inverseModelViewMatrix.invert(_currentModelViewMatrix); |
|---|
| 44 | |
|---|
| 45 | osg::Matrix T(_previousModelViewMatrix*_inverseModelViewMatrix); |
|---|
| 46 | |
|---|
| 47 | osg::Geometry* geometry = dynamic_cast<osg::Geometry*>(const_cast<osg::Drawable*>(drawable)); |
|---|
| 48 | osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()); |
|---|
| 49 | for(unsigned int i=0;i+1<vertices->size();i+=2) |
|---|
| 50 | { |
|---|
| 51 | (*vertices)[i+1] = (*vertices)[i]*T; |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | else |
|---|
| 55 | { |
|---|
| 56 | _currentModelViewMatrix = state.getModelViewMatrix(); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | _firstTime = false; |
|---|
| 60 | |
|---|
| [6422] | 61 | drawable->drawImplementation(renderInfo); |
|---|
| [2697] | 62 | } |
|---|
| 63 | |
|---|
| 64 | mutable bool _firstTime; |
|---|
| 65 | mutable osg::Matrix _currentModelViewMatrix; |
|---|
| 66 | mutable osg::Matrix _inverseModelViewMatrix; |
|---|
| 67 | mutable osg::Matrix _previousModelViewMatrix; |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | osg::Node* createScene(unsigned int noStars) |
|---|
| 74 | { |
|---|
| 75 | |
|---|
| 76 | osg::Geometry* geometry = new osg::Geometry; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | osg::Vec3Array* vertices = new osg::Vec3Array(noStars*2); |
|---|
| 80 | geometry->setVertexArray(vertices); |
|---|
| 81 | |
|---|
| 82 | float min = -1.0f; |
|---|
| 83 | float max = 1.0f; |
|---|
| 84 | unsigned int j = 0; |
|---|
| 85 | unsigned int i = 0; |
|---|
| 86 | for(i=0;i<noStars;++i,j+=2) |
|---|
| 87 | { |
|---|
| 88 | (*vertices)[j].set(random(min,max),random(min,max),random(min,max)); |
|---|
| 89 | (*vertices)[j+1] = (*vertices)[j]+osg::Vec3(0.0f,0.0f,0.001f); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | osg::Vec4Array* colours = new osg::Vec4Array(1); |
|---|
| 94 | geometry->setColorArray(colours); |
|---|
| 95 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 96 | (*colours)[0].set(1.0f,1.0f,1.0f,1.0f); |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | geometry->addPrimitiveSet(new osg::DrawArrays(GL_LINES,0,noStars*2)); |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | osg::DrawElementsUShort* points = new osg::DrawElementsUShort(GL_POINTS,noStars); |
|---|
| 103 | geometry->addPrimitiveSet(points); |
|---|
| 104 | for(i=0;i<noStars;++i) |
|---|
| 105 | { |
|---|
| 106 | (*points)[i] = i*2; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | geometry->setUseDisplayList(false); |
|---|
| 110 | geometry->setDrawCallback(new DrawCallback); |
|---|
| 111 | |
|---|
| 112 | osg::Geode* geode = new osg::Geode; |
|---|
| 113 | geode->addDrawable(geometry); |
|---|
| [4767] | 114 | geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| [2697] | 115 | |
|---|
| 116 | osg::Group* group = new osg::Group; |
|---|
| 117 | group->addChild(geode); |
|---|
| 118 | |
|---|
| 119 | return group; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| [6180] | 122 | int main(int , char **) |
|---|
| [2697] | 123 | { |
|---|
| 124 | |
|---|
| [5959] | 125 | osgViewer::Viewer viewer; |
|---|
| [2697] | 126 | |
|---|
| 127 | |
|---|
| [5959] | 128 | viewer.setSceneData(createScene(50000)); |
|---|
| [2697] | 129 | |
|---|
| [5959] | 130 | return viewer.run(); |
|---|
| [2697] | 131 | } |
|---|