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

Revision 11574, 3.9 kB (checked in by robert, 4 years ago)

Fixed permissions

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgpointsprite.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
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
29#include <osgDB/ReadFile>
30
31#include <osgViewer/Viewer>
32
33osg::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    /** Formula for the two spirals */
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
65osg::StateSet* makeStateSet(float size)
66{
67    osg::StateSet *set = new osg::StateSet();
68
69    /// Setup cool blending
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    /// Setup the point sprites
76    osg::PointSprite *sprite = new osg::PointSprite();
77    set->setTextureAttributeAndModes(0, sprite, osg::StateAttribute::ON);
78
79    /// Give some size to the points to be able to see the sprite
80    osg::Point *point = new osg::Point();
81    point->setSize(size);
82    set->setAttribute(point);
83
84    /// Disable depth test to avoid sort problems and Lighting
85    set->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
86    set->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
87
88    /// The texture for the sprites
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
96int main(int, char *[])
97{
98    osgViewer::Viewer viewer;
99
100    /// Make the galaxy of points
101    osg::Node *node = makeGalaxy(5000);
102
103    node->setStateSet(makeStateSet(10.0f));
104
105    viewer.setSceneData(node);
106
107    return viewer.run();
108}
Note: See TracBrowser for help on using the browser.