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