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

Revision 7648, 7.2 kB (checked in by robert, 6 years ago)

From Roland Smeenk, "Attached you will find a large set of small typo fixes (mainly in the comments)."

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