root/OpenSceneGraph/trunk/examples/osgpoints/osgpoints.cpp @ 5954

Revision 5954, 6.7 kB (checked in by robert, 7 years ago)

Ported following examples to osgViewer:

osgparticleeffects
osgphotoalbum
osgpick
osgpoints
osgpointsprite
osgprecipitation
osgprerender
osgprerendercubemap
osgreflect
osgscalarbar
osgscribe
osgsequence
osgplanets

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This application is open source and may be redistributed and/or modified   
4 * freely and without restriction, both in commericial and non commericial applications,
5 * as long as this copyright notice is maintained.
6 *
7 * This application is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10*/
11
12#include <osgDB/ReadFile>
13#include <osgUtil/Optimizer>
14#include <osgViewer/Viewer>
15
16#include <osg/Point>
17#include <osg/BlendFunc>
18#include <osg/Texture2D>
19#include <osg/PointSprite>
20
21#include <iostream>
22
23class KeyboardEventHandler : public osgGA::GUIEventHandler
24{
25public:
26   
27        KeyboardEventHandler(osg::StateSet* stateset):
28            _stateset(stateset)
29        {
30            _point = new osg::Point;
31            _point->setDistanceAttenuation(osg::Vec3(0.0,0.0000,0.05f));
32            _stateset->setAttribute(_point.get());
33        }
34   
35        virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
36        {
37            switch(ea.getEventType())
38            {
39                case(osgGA::GUIEventAdapter::KEYDOWN):
40                {
41                    if (ea.getKey()=='+' || ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Add)
42                    {
43                       changePointSize(1.0f);
44                       return true;
45                    }
46                    else if (ea.getKey()=='-' || ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Subtract)
47                    {
48                       changePointSize(-1.0f);
49                       return true;
50                    }
51                    else if (ea.getKey()=='<')
52                    {
53                       changePointAttenuation(1.1f);
54                       return true;
55                    }
56                    else if (ea.getKey()=='>')
57                    {
58                       changePointAttenuation(1.0f/1.1f);
59                       return true;
60                    }
61                    break;
62                }
63                default:
64                    break;
65            }
66            return false;
67        }
68       
69       
70        float getPointSize() const
71        {
72            return _point->getSize();
73        }
74       
75        void setPointSize(float psize)
76        {
77            if (psize>0.0)
78            {
79                _point->setSize(psize);
80            }
81            std::cout<<"Point size "<<psize<<std::endl;
82        }
83
84        void changePointSize(float delta)
85        {
86            setPointSize(getPointSize()+delta);
87        }
88
89        void changePointAttenuation(float scale)
90        {
91            _point->setDistanceAttenuation(_point->getDistanceAttenuation()*scale);
92        }
93       
94        osg::ref_ptr<osg::StateSet> _stateset;
95        osg::ref_ptr<osg::Point>    _point;
96       
97};
98
99int main( int argc, char **argv )
100{
101
102    // use an ArgumentParser object to manage the program arguments.
103    osg::ArgumentParser arguments(&argc,argv);
104   
105    // set up the usage document, in case we need to print out how to use this program.
106    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
107    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" example provides an interactive viewer for visualising point clouds..");
108    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
109    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
110    arguments.getApplicationUsage()->addCommandLineOption("--sprites","Point sprites.");
111   
112
113    // construct the viewer.
114    osgViewer::Viewer viewer;
115
116    bool shader = false;
117    while (arguments.read("--shader")) shader = true;
118
119    // if user request help write it out to cout.
120    if (arguments.read("-h") || arguments.read("--help"))
121    {
122        arguments.getApplicationUsage()->write(std::cout);
123        return 1;
124    }
125   
126    bool usePointSprites = false;
127    while (arguments.read("--sprites")) { usePointSprites = true; };
128
129    if (arguments.argc()<=1)
130    {
131        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
132        return 1;
133    }
134
135    // read the scene from the list of file specified commandline args.
136    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
137
138    // if no model has been successfully loaded report failure.
139    if (!loadedModel)
140    {
141        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
142        return 1;
143    }
144
145    // optimize the scene graph, remove rendundent nodes and state etc.
146    osgUtil::Optimizer optimizer;
147    optimizer.optimize(loadedModel.get());
148
149
150    // set the scene to render
151    viewer.setSceneData(loadedModel.get());
152   
153
154    if (usePointSprites)   
155    {
156        osg::StateSet* stateset = loadedModel->getOrCreateStateSet();
157
158        /// Setup cool blending
159        osg::BlendFunc *fn = new osg::BlendFunc();
160        stateset->setAttributeAndModes(fn, osg::StateAttribute::ON);
161
162        /// Setup the point sprites
163        osg::PointSprite *sprite = new osg::PointSprite();
164        stateset->setTextureAttributeAndModes(0, sprite, osg::StateAttribute::ON);
165
166        /// The texture for the sprites
167        osg::Texture2D *tex = new osg::Texture2D();
168        tex->setImage(osgDB::readImageFile("Images/particle.rgb"));
169        stateset->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON);
170    }
171   
172
173    // register the handler for modifying the point size
174    viewer.addEventHandler(new KeyboardEventHandler(viewer.getCamera()->getOrCreateStateSet()));
175
176
177    if (shader)
178    {
179        osg::StateSet* stateset = loadedModel->getOrCreateStateSet();
180   
181        ///////////////////////////////////////////////////////////////////
182        // vertex shader using just Vec4 coefficients
183        char vertexShaderSource[] =
184            "void main(void) \n"
185            "{ \n"
186            "\n"
187            "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
188            "}\n";
189
190
191
192        osg::Program* program = new osg::Program;
193        stateset->setAttribute(program);
194
195        osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource);
196        program->addShader(vertex_shader);
197
198#if 0
199        //////////////////////////////////////////////////////////////////
200        // fragment shader
201        //
202        char fragmentShaderSource[] =
203            "void main(void) \n"
204            "{ \n"
205            "    gl_FragColor = gl_Color; \n"
206            "}\n";
207
208        osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
209        program->addShader(fragment_shader);
210#endif
211    }
212
213    return viewer.run();
214}
Note: See TracBrowser for help on using the browser.