root/OpenSceneGraph/trunk/examples/osgpointsprite/osgpointsprite.cpp @ 4069

Revision 4069, 3.1 kB (checked in by robert, 10 years ago)

Removed the enabling of the point smooth mode.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
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 <osgDB/ReadFile>
9#include <osgProducer/Viewer>
10#include <osg/GLExtensions>
11#include <osg/TexEnv>
12
13osg::Geode *makeGalaxy(unsigned nvertices)
14{
15    osg::Geode *geode = new osg::Geode();
16    osg::Geometry *galaxy = new osg::Geometry();
17    osg::Vec3Array *vertices = new osg::Vec3Array();
18    osg::Vec4Array *colors = new osg::Vec4Array();
19    osg::Vec4 ini(1,1,0,1);
20    osg::Vec4 fin(0,0,1,1);
21
22    /** Formula for the two spirals */
23    for (unsigned i=0;i<nvertices/2;i++) {
24        float val = (i*2/(float)nvertices * 2 * 3.14159265359);
25        float modx1 = rand() / (float)RAND_MAX*2;
26        float mody1 = rand() / (float)RAND_MAX*2;
27        float modx2 = rand() / (float)RAND_MAX*2;
28        float mody2 = rand() / (float)RAND_MAX*2;
29        float modz1 = ((rand()-RAND_MAX/2) / (float)(RAND_MAX))*3/(val+1);
30        float modz2 = ((rand()-RAND_MAX/2) / (float)(RAND_MAX))*3/(val+1);
31        vertices->push_back(osg::Vec3(cos(val)*val+modx1, sin(val)*val+mody1, modz1));
32        vertices->push_back(osg::Vec3(-cos(val)*val+modx2, -sin(val)*val+mody2, modz2));
33
34        colors->push_back(ini+(fin-ini)*(i*2/(float)nvertices));
35        colors->push_back(ini+(fin-ini)*(i*2/(float)nvertices));
36    }
37    galaxy->setVertexArray(vertices);
38    galaxy->setColorArray(colors);
39    galaxy->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
40    galaxy->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, nvertices));
41    geode->addDrawable(galaxy);
42    return geode;
43}
44
45osg::StateSet* makeStateSet(float size)
46{
47    osg::StateSet *set = new osg::StateSet();
48
49    /// Setup cool blending
50    set->setMode(GL_BLEND, osg::StateAttribute::ON);
51    osg::BlendFunc *fn = new osg::BlendFunc();
52    fn->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::DST_ALPHA);
53    set->setAttributeAndModes(fn, osg::StateAttribute::ON);
54
55    /// Setup the point sprites
56    osg::PointSprite *sprite = new osg::PointSprite();
57    set->setTextureAttributeAndModes(0, sprite, osg::StateAttribute::ON);
58
59    /// Give some size to the points to be able to see the sprite
60    osg::Point *point = new osg::Point();
61    point->setSize(size);
62    set->setAttribute(point);
63
64    /// Disable depth test to avoid sort problems and Lighting
65    set->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
66    set->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
67
68    /// The texture for the sprites
69    osg::Texture2D *tex = new osg::Texture2D();
70    tex->setImage(osgDB::readImageFile("Images/particle.rgb"));
71    set->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON);
72
73    return set;
74}
75
76int main(int, char *[])
77{
78    osgProducer::Viewer viewer;
79
80    /// Make the galaxy of points
81    osg::Node *node = makeGalaxy(5000);
82
83    node->setStateSet(makeStateSet(10.0f));
84
85    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
86    viewer.setSceneData(node);
87
88    viewer.realize();
89    while (!viewer.done())
90    {
91        viewer.sync();
92        viewer.update();
93        viewer.frame();
94    }
95
96    return(0);
97}
Note: See TracBrowser for help on using the browser.