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

Revision 5636, 8.2 kB (checked in by robert, 8 years ago)

Removed deprecated GUIEventHandler method

  • 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 <osgProducer/Viewer>
15
16#include <osg/Point>
17#include <osg/BlendFunc>
18#include <osg/Texture2D>
19#include <osg/PointSprite>
20
21class KeyboardEventHandler : public osgGA::GUIEventHandler
22{
23public:
24   
25        KeyboardEventHandler(osg::StateSet* stateset):
26            _stateset(stateset)
27        {
28            _point = new osg::Point;
29            _point->setDistanceAttenuation(osg::Vec3(0.0,0.0000,0.05f));
30            _stateset->setAttribute(_point.get());
31        }
32   
33        virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
34        {
35            switch(ea.getEventType())
36            {
37                case(osgGA::GUIEventAdapter::KEYDOWN):
38                {
39                    if (ea.getKey()=='+' || ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Add)
40                    {
41                       changePointSize(1.0f);
42                       return true;
43                    }
44                    else if (ea.getKey()=='-' || ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Subtract)
45                    {
46                       changePointSize(-1.0f);
47                       return true;
48                    }
49                    else if (ea.getKey()=='<')
50                    {
51                       changePointAttenuation(1.1f);
52                       return true;
53                    }
54                    else if (ea.getKey()=='>')
55                    {
56                       changePointAttenuation(1.0f/1.1f);
57                       return true;
58                    }
59                    break;
60                }
61                default:
62                    break;
63            }
64            return false;
65        }
66       
67       
68        float getPointSize() const
69        {
70            return _point->getSize();
71        }
72       
73        void setPointSize(float psize)
74        {
75            if (psize>0.0)
76            {
77                _point->setSize(psize);
78            }
79            std::cout<<"Point size "<<psize<<std::endl;
80        }
81
82        void changePointSize(float delta)
83        {
84            setPointSize(getPointSize()+delta);
85        }
86
87        void changePointAttenuation(float scale)
88        {
89            _point->setDistanceAttenuation(_point->getDistanceAttenuation()*scale);
90        }
91       
92        osg::ref_ptr<osg::StateSet> _stateset;
93        osg::ref_ptr<osg::Point>    _point;
94       
95};
96
97int main( int argc, char **argv )
98{
99
100    // use an ArgumentParser object to manage the program arguments.
101    osg::ArgumentParser arguments(&argc,argv);
102   
103    // set up the usage document, in case we need to print out how to use this program.
104    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
105    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" example provides an interactive viewer for visualising point clouds..");
106    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
107    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
108    arguments.getApplicationUsage()->addCommandLineOption("--sprites","Point sprites.");
109   
110
111    // construct the viewer.
112    osgProducer::Viewer viewer(arguments);
113
114    // set up the value with sensible default event handlers.
115    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
116
117    // get details on keyboard and mouse bindings used by the viewer.
118    viewer.getUsage(*arguments.getApplicationUsage());
119
120    bool shader = false;
121    while (arguments.read("--shader")) shader = true;
122
123    // if user request help write it out to cout.
124    if (arguments.read("-h") || arguments.read("--help"))
125    {
126        arguments.getApplicationUsage()->write(std::cout);
127        return 1;
128    }
129   
130    bool usePointSprites = false;
131    while (arguments.read("--sprites")) { usePointSprites = true; };
132
133    // any option left unread are converted into errors to write out later.
134    arguments.reportRemainingOptionsAsUnrecognized();
135
136    // report any errors if they have occured when parsing the program aguments.
137    if (arguments.errors())
138    {
139        arguments.writeErrorMessages(std::cout);
140        return 1;
141    }
142   
143    if (arguments.argc()<=1)
144    {
145        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
146        return 1;
147    }
148
149    osg::Timer timer;
150    osg::Timer_t start_tick = timer.tick();
151
152    // read the scene from the list of file specified commandline args.
153    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
154
155    // if no model has been successfully loaded report failure.
156    if (!loadedModel)
157    {
158        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
159        return 1;
160    }
161
162    osg::Timer_t end_tick = timer.tick();
163
164    std::cout << "Time to load = "<<timer.delta_s(start_tick,end_tick)<<std::endl;
165
166    // optimize the scene graph, remove rendundent nodes and state etc.
167    osgUtil::Optimizer optimizer;
168    optimizer.optimize(loadedModel.get());
169
170
171    // set the scene to render
172    viewer.setSceneData(loadedModel.get());
173   
174
175    if (usePointSprites)   
176    {
177        osg::StateSet* stateset = loadedModel->getOrCreateStateSet();
178
179        /// Setup cool blending
180        osg::BlendFunc *fn = new osg::BlendFunc();
181        stateset->setAttributeAndModes(fn, osg::StateAttribute::ON);
182
183        /// Setup the point sprites
184        osg::PointSprite *sprite = new osg::PointSprite();
185        stateset->setTextureAttributeAndModes(0, sprite, osg::StateAttribute::ON);
186
187        /// The texture for the sprites
188        osg::Texture2D *tex = new osg::Texture2D();
189        tex->setImage(osgDB::readImageFile("Images/particle.rgb"));
190        stateset->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON);
191    }
192   
193
194    // register the handler for modifying the point size
195    viewer.getEventHandlerList().push_front(new KeyboardEventHandler(viewer.getGlobalStateSet()));
196
197
198    if (shader)
199    {
200        osg::StateSet* stateset = loadedModel->getOrCreateStateSet();
201   
202        ///////////////////////////////////////////////////////////////////
203        // vertex shader using just Vec4 coefficients
204        char vertexShaderSource[] =
205            "void main(void) \n"
206            "{ \n"
207            "\n"
208            "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
209            "}\n";
210
211
212
213        osg::Program* program = new osg::Program;
214        stateset->setAttribute(program);
215
216        osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource);
217        program->addShader(vertex_shader);
218
219#if 0
220        //////////////////////////////////////////////////////////////////
221        // fragment shader
222        //
223        char fragmentShaderSource[] =
224            "void main(void) \n"
225            "{ \n"
226            "    gl_FragColor = gl_Color; \n"
227            "}\n";
228
229        osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
230        program->addShader(fragment_shader);
231#endif
232    }
233
234
235
236    // create the windows and run the threads.
237    viewer.realize();
238
239    while( !viewer.done() )
240    {
241        // wait for all cull and draw threads to complete.
242        viewer.sync();
243
244        // update the scene by traversing it with the the update visitor which will
245        // call all node update callbacks and animations.
246        viewer.update();
247         
248        // fire off the cull and draw traversals of the scene.
249        viewer.frame();
250       
251    }
252   
253    // wait for all cull and draw threads to complete.
254    viewer.sync();
255
256    // run a clean up frame to delete all OpenGL objects.
257    viewer.cleanup_frame();
258
259    // wait for all the clean up frame to complete.
260    viewer.sync();
261
262    return 0;
263}
Note: See TracBrowser for help on using the browser.