| [6941] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| [2771] | 19 | #include <osg/PointSprite> |
|---|
| 20 | #include <osg/BlendFunc> |
|---|
| 21 | #include <osg/StateAttribute> |
|---|
| 22 | #include <osg/Point> |
|---|
| 23 | #include <osg/Geometry> |
|---|
| 24 | #include <osg/Texture2D> |
|---|
| 25 | #include <osg/TexEnv> |
|---|
| 26 | #include <osg/GLExtensions> |
|---|
| 27 | #include <osg/TexEnv> |
|---|
| 28 | |
|---|
| [5954] | 29 | #include <osgDB/ReadFile> |
|---|
| 30 | |
|---|
| 31 | #include <osgViewer/Viewer> |
|---|
| 32 | |
|---|
| [2771] | 33 | osg::Geode *makeGalaxy(unsigned nvertices) |
|---|
| 34 | { |
|---|
| 35 | osg::Geode *geode = new osg::Geode(); |
|---|
| 36 | osg::Geometry *galaxy = new osg::Geometry(); |
|---|
| 37 | osg::Vec3Array *vertices = new osg::Vec3Array(); |
|---|
| 38 | osg::Vec4Array *colors = new osg::Vec4Array(); |
|---|
| 39 | osg::Vec4 ini(1,1,0,1); |
|---|
| 40 | osg::Vec4 fin(0,0,1,1); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | for (unsigned i=0;i<nvertices/2;i++) { |
|---|
| 44 | float val = (i*2/(float)nvertices * 2 * 3.14159265359); |
|---|
| 45 | float modx1 = rand() / (float)RAND_MAX*2; |
|---|
| 46 | float mody1 = rand() / (float)RAND_MAX*2; |
|---|
| 47 | float modx2 = rand() / (float)RAND_MAX*2; |
|---|
| 48 | float mody2 = rand() / (float)RAND_MAX*2; |
|---|
| 49 | float modz1 = ((rand()-RAND_MAX/2) / (float)(RAND_MAX))*3/(val+1); |
|---|
| 50 | float modz2 = ((rand()-RAND_MAX/2) / (float)(RAND_MAX))*3/(val+1); |
|---|
| 51 | vertices->push_back(osg::Vec3(cos(val)*val+modx1, sin(val)*val+mody1, modz1)); |
|---|
| 52 | vertices->push_back(osg::Vec3(-cos(val)*val+modx2, -sin(val)*val+mody2, modz2)); |
|---|
| 53 | |
|---|
| 54 | colors->push_back(ini+(fin-ini)*(i*2/(float)nvertices)); |
|---|
| 55 | colors->push_back(ini+(fin-ini)*(i*2/(float)nvertices)); |
|---|
| 56 | } |
|---|
| 57 | galaxy->setVertexArray(vertices); |
|---|
| 58 | galaxy->setColorArray(colors); |
|---|
| 59 | galaxy->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 60 | galaxy->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, nvertices)); |
|---|
| 61 | geode->addDrawable(galaxy); |
|---|
| 62 | return geode; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | osg::StateSet* makeStateSet(float size) |
|---|
| 66 | { |
|---|
| 67 | osg::StateSet *set = new osg::StateSet(); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | set->setMode(GL_BLEND, osg::StateAttribute::ON); |
|---|
| 71 | osg::BlendFunc *fn = new osg::BlendFunc(); |
|---|
| 72 | fn->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::DST_ALPHA); |
|---|
| 73 | set->setAttributeAndModes(fn, osg::StateAttribute::ON); |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | osg::PointSprite *sprite = new osg::PointSprite(); |
|---|
| 77 | set->setTextureAttributeAndModes(0, sprite, osg::StateAttribute::ON); |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | osg::Point *point = new osg::Point(); |
|---|
| 81 | point->setSize(size); |
|---|
| [4069] | 82 | set->setAttribute(point); |
|---|
| [2771] | 83 | |
|---|
| 84 | |
|---|
| 85 | set->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); |
|---|
| 86 | set->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | osg::Texture2D *tex = new osg::Texture2D(); |
|---|
| 90 | tex->setImage(osgDB::readImageFile("Images/particle.rgb")); |
|---|
| 91 | set->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON); |
|---|
| 92 | |
|---|
| 93 | return set; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | int main(int, char *[]) |
|---|
| 97 | { |
|---|
| [5954] | 98 | osgViewer::Viewer viewer; |
|---|
| [2771] | 99 | |
|---|
| 100 | |
|---|
| 101 | osg::Node *node = makeGalaxy(5000); |
|---|
| 102 | |
|---|
| 103 | node->setStateSet(makeStateSet(10.0f)); |
|---|
| 104 | |
|---|
| 105 | viewer.setSceneData(node); |
|---|
| 106 | |
|---|
| [5954] | 107 | return viewer.run(); |
|---|
| [2771] | 108 | } |
|---|