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

Revision 1984, 5.6 kB (checked in by robert, 11 years ago)

Additions since the CVS back up was made.

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