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

Revision 4360, 6.1 kB (checked in by robert, 9 years ago)

Added / and * controls of the distance attentuations of the point size extension

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 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
18class KeyboardEventHandler : public osgGA::GUIEventHandler
19{
20public:
21   
22        KeyboardEventHandler(osg::StateSet* stateset):
23            _stateset(stateset)
24        {
25            _point = new osg::Point;
26            _point->setDistanceAttenuation(osg::Vec3(0.0,0.0005,0.0f));
27            _point->setDistanceAttenuation(osg::Vec3(0.0,0.0000,0.05f));
28            _stateset->setAttribute(_point.get());
29        }
30   
31        virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
32        {
33            switch(ea.getEventType())
34            {
35                case(osgGA::GUIEventAdapter::KEYDOWN):
36                {
37                    if (ea.getKey()=='+' || ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Add)
38                    {
39                       changePointSize(1.0f);
40                       return true;
41                    }
42                    else if (ea.getKey()=='-' || ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Subtract)
43                    {
44                       changePointSize(-1.0f);
45                       return true;
46                    }
47                    else if (ea.getKey()=='*')
48                    {
49                       changePointAttenuation(1.1f);
50                       return true;
51                    }
52                    else if (ea.getKey()=='/')
53                    {
54                       changePointAttenuation(1.0f/1.1f);
55                       return true;
56                    }
57                    break;
58                }
59                default:
60                    break;
61            }
62            return false;
63        }
64
65        virtual void accept(osgGA::GUIEventHandlerVisitor& v)
66        {
67            v.visit(*this);
68        }
69       
70       
71        float getPointSize() const
72        {
73            return _point->getSize();
74        }
75       
76        void setPointSize(float psize)
77        {
78            if (psize>0.0)
79            {
80                _point->setSize(psize);
81            }
82            std::cout<<"Point size "<<psize<<std::endl;
83        }
84
85        void changePointSize(float delta)
86        {
87            setPointSize(getPointSize()+delta);
88        }
89
90        void changePointAttenuation(float scale)
91        {
92            _point->setDistanceAttenuation(_point->getDistanceAttenuation()*scale);
93        }
94       
95        osg::ref_ptr<osg::StateSet> _stateset;
96        osg::ref_ptr<osg::Point>    _point;
97       
98};
99
100int main( int argc, char **argv )
101{
102
103    // use an ArgumentParser object to manage the program arguments.
104    osg::ArgumentParser arguments(&argc,argv);
105   
106    // set up the usage document, in case we need to print out how to use this program.
107    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
108    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
109    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
110    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
111   
112
113    // construct the viewer.
114    osgProducer::Viewer viewer(arguments);
115
116    // set up the value with sensible default event handlers.
117    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
118
119    // get details on keyboard and mouse bindings used by the viewer.
120    viewer.getUsage(*arguments.getApplicationUsage());
121
122    // if user request help write it out to cout.
123    if (arguments.read("-h") || arguments.read("--help"))
124    {
125        arguments.getApplicationUsage()->write(std::cout);
126        return 1;
127    }
128
129    // any option left unread are converted into errors to write out later.
130    arguments.reportRemainingOptionsAsUnrecognized();
131
132    // report any errors if they have occured when parsing the program aguments.
133    if (arguments.errors())
134    {
135        arguments.writeErrorMessages(std::cout);
136        return 1;
137    }
138   
139    if (arguments.argc()<=1)
140    {
141        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
142        return 1;
143    }
144
145    osg::Timer timer;
146    osg::Timer_t start_tick = timer.tick();
147
148    // read the scene from the list of file specified commandline args.
149    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
150
151    // if no model has been successfully loaded report failure.
152    if (!loadedModel)
153    {
154        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
155        return 1;
156    }
157
158    osg::Timer_t end_tick = timer.tick();
159
160    std::cout << "Time to load = "<<timer.delta_s(start_tick,end_tick)<<std::endl;
161
162    // optimize the scene graph, remove rendundent nodes and state etc.
163    osgUtil::Optimizer optimizer;
164    optimizer.optimize(loadedModel.get());
165
166
167    // set the scene to render
168    viewer.setSceneData(loadedModel.get());
169
170    // register the handler for modifying the point size
171    viewer.getEventHandlerList().push_front(new KeyboardEventHandler(viewer.getGlobalStateSet()));
172
173    // create the windows and run the threads.
174    viewer.realize();
175
176    while( !viewer.done() )
177    {
178        // wait for all cull and draw threads to complete.
179        viewer.sync();
180
181        // update the scene by traversing it with the the update visitor which will
182        // call all node update callbacks and animations.
183        viewer.update();
184         
185        // fire off the cull and draw traversals of the scene.
186        viewer.frame();
187       
188    }
189   
190    // wait for all cull and draw threads to complete before exit.
191    viewer.sync();
192
193    return 0;
194}
Note: See TracBrowser for help on using the browser.