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

Revision 13574, 3.9 kB (checked in by robert, 14 minutes ago)

From Paul Martz, fixed placement of OpenGL header so that it gets generated and placed in the build directory as per the Config file

  • 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, osg::Array::BIND_PER_VERTEX);
59    galaxy->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, nvertices));
60    geode->addDrawable(galaxy);
61    return geode;
62}
63
64osg::StateSet* makeStateSet(float size)
65{
66    osg::StateSet *set = new osg::StateSet();
67
68    /// Setup cool blending
69    set->setMode(GL_BLEND, osg::StateAttribute::ON);
70    osg::BlendFunc *fn = new osg::BlendFunc();
71    fn->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::DST_ALPHA);
72    set->setAttributeAndModes(fn, osg::StateAttribute::ON);
73
74    /// Setup the point sprites
75    osg::PointSprite *sprite = new osg::PointSprite();
76    set->setTextureAttributeAndModes(0, sprite, osg::StateAttribute::ON);
77
78    /// Give some size to the points to be able to see the sprite
79    osg::Point *point = new osg::Point();
80    point->setSize(size);
81    set->setAttribute(point);
82
83    /// Disable depth test to avoid sort problems and Lighting
84    set->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
85    set->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
86
87    /// The texture for the sprites
88    osg::Texture2D *tex = new osg::Texture2D();
89    tex->setImage(osgDB::readImageFile("Images/particle.rgb"));
90    set->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON);
91
92    return set;
93}
94
95int main(int, char *[])
96{
97    osgViewer::Viewer viewer;
98
99    /// Make the galaxy of points
100    osg::Node *node = makeGalaxy(5000);
101
102    node->setStateSet(makeStateSet(10.0f));
103
104    viewer.setSceneData(node);
105
106    return viewer.run();
107}
Note: See TracBrowser for help on using the browser.