root/OpenSceneGraph/trunk/examples/osgpick/osgpick.cpp @ 5381

Revision 5381, 9.5 kB (checked in by robert, 8 years ago)

Added viewer.cleanup_frame() to all examples.

  • 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 library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13/* osgpick sample
14* demonstrate use of osgUtil/PickVisitor for picking in a HUD or
15* in a 3d scene,
16*/
17
18#include <osgUtil/Optimizer>
19#include <osgDB/ReadFile>
20#include <osgProducer/Viewer>
21
22#include <osg/Material>
23#include <osg/Geode>
24#include <osg/BlendFunc>
25#include <osg/Depth>
26#include <osg/Projection>
27#include <osg/MatrixTransform>
28#include <osg/CameraNode>
29#include <osg/io_utils>
30
31#include <osgText/Text>
32
33#include <sstream>
34
35// class to handle events with a pick
36class PickHandler : public osgGA::GUIEventHandler {
37public:
38
39    PickHandler(osgProducer::Viewer* viewer,osgText::Text* updateText):
40        _viewer(viewer),
41        _updateText(updateText) {}
42       
43    ~PickHandler() {}
44   
45    bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us);
46
47    virtual void pick(const osgGA::GUIEventAdapter& ea);
48
49    void setLabel(const std::string& name)
50    {
51        if (_updateText.get()) _updateText->setText(name);
52    }
53   
54protected:
55
56    osgProducer::Viewer* _viewer;
57    osg::ref_ptr<osgText::Text>  _updateText;
58};
59
60bool PickHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
61{
62    switch(ea.getEventType())
63    {
64    case(osgGA::GUIEventAdapter::FRAME):
65        {
66            pick(ea);
67        }
68        return false;
69       
70    default:
71        return false;
72    }
73}
74
75void PickHandler::pick(const osgGA::GUIEventAdapter& ea)
76{
77    osgUtil::IntersectVisitor::HitList hlist;
78   
79    std::string gdlist="";
80    if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
81    {
82        for(osgUtil::IntersectVisitor::HitList::iterator hitr=hlist.begin();
83            hitr!=hlist.end();
84            ++hitr)
85        {
86            std::ostringstream os;
87            if (hitr->_geode.valid() && !hitr->_geode->getName().empty())
88            {
89                // the geodes are identified by name.
90                os<<"Object \""<<hitr->_geode->getName()<<"\""<<std::endl;
91            }
92            else if (hitr->_drawable.valid())
93            {
94                os<<"Object \""<<hitr->_drawable->className()<<"\""<<std::endl;
95            }
96
97            os<<"        local coords vertex("<< hitr->getLocalIntersectPoint()<<")"<<"  normal("<<hitr->getLocalIntersectNormal()<<")"<<std::endl;
98            os<<"        world coords vertex("<< hitr->getWorldIntersectPoint()<<")"<<"  normal("<<hitr->getWorldIntersectNormal()<<")"<<std::endl;
99            osgUtil::Hit::VecIndexList& vil = hitr->_vecIndexList;
100            for(unsigned int i=0;i<vil.size();++i)
101            {
102                os<<"        vertex indices ["<<i<<"] = "<<vil[i]<<std::endl;
103            }
104           
105            gdlist += os.str();
106        }
107    }
108    setLabel(gdlist);
109}
110
111osg::Node* createHUD(osgText::Text* updateText)
112{
113
114    // create the hud. derived from osgHud.cpp
115    // adds a set of quads, each in a separate Geode - which can be picked individually
116    // eg to be used as a menuing/help system!
117    // Can pick texts too!
118
119    osg::CameraNode* hudCamera = new osg::CameraNode;
120    hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
121    hudCamera->setProjectionMatrixAsOrtho2D(0,1280,0,1024);
122    hudCamera->setViewMatrix(osg::Matrix::identity());
123    hudCamera->setRenderOrder(osg::CameraNode::POST_RENDER);
124    hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
125   
126    std::string timesFont("fonts/times.ttf");
127   
128    // turn lighting off for the text and disable depth test to ensure its always ontop.
129    osg::Vec3 position(150.0f,800.0f,0.0f);
130    osg::Vec3 delta(0.0f,-60.0f,0.0f);
131   
132    {
133        osg::Geode* geode = new osg::Geode();
134        osg::StateSet* stateset = geode->getOrCreateStateSet();
135        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
136        stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
137        geode->setName("simple");
138        hudCamera->addChild(geode);
139       
140        osgText::Text* text = new  osgText::Text;
141        geode->addDrawable( text );
142       
143        text->setFont(timesFont);
144        text->setText("Picking in Head Up Displays is simple!");
145        text->setPosition(position);
146       
147        position += delta;
148    }   
149   
150   
151    for (int i=0; i<5; i++) {
152        osg::Vec3 dy(0.0f,-30.0f,0.0f);
153        osg::Vec3 dx(120.0f,0.0f,0.0f);
154        osg::Geode* geode = new osg::Geode();
155        osg::StateSet* stateset = geode->getOrCreateStateSet();
156        const char *opts[]={"One", "Two", "Three", "January", "Feb", "2003"};
157        osg::Geometry *quad=new osg::Geometry;
158        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
159        stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
160        std::string name="subOption";
161        name += " ";
162        name += std::string(opts[i]);
163        geode->setName(name);
164        osg::Vec3Array* vertices = new osg::Vec3Array(4); // 1 quad
165        osg::Vec4Array* colors = new osg::Vec4Array;
166        colors = new osg::Vec4Array;
167        colors->push_back(osg::Vec4(0.8-0.1*i,0.1*i,0.2*i, 1.0));
168        quad->setColorArray(colors);
169        quad->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);
170        (*vertices)[0]=position;
171        (*vertices)[1]=position+dx;
172        (*vertices)[2]=position+dx+dy;
173        (*vertices)[3]=position+dy;
174        quad->setVertexArray(vertices);
175        quad->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
176        geode->addDrawable(quad);
177        hudCamera->addChild(geode);
178       
179        position += delta;
180    }   
181   
182   
183   
184    { // this displays what has been selected
185        osg::Geode* geode = new osg::Geode();
186        osg::StateSet* stateset = geode->getOrCreateStateSet();
187        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
188        stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
189        geode->setName("The text label");
190        geode->addDrawable( updateText );
191        hudCamera->addChild(geode);
192       
193        updateText->setCharacterSize(20.0f);
194        updateText->setFont(timesFont);
195        updateText->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
196        updateText->setText("");
197        updateText->setPosition(position);
198       
199        position += delta;
200    }   
201   
202    return hudCamera;
203
204}
205
206int main( int argc, char **argv )
207{
208
209    // use an ArgumentParser object to manage the program arguments.
210    osg::ArgumentParser arguments(&argc,argv);
211   
212    // set up the usage document, in case we need to print out how to use this program.
213    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to do Head Up Displays.");
214    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] [filename] ...");
215    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
216   
217
218    // construct the viewer.
219    osgProducer::Viewer viewer(arguments);
220
221    // set up the value with sensible default event handlers.
222    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
223
224    // get details on keyboard and mouse bindings used by the viewer.
225    viewer.getUsage(*arguments.getApplicationUsage());
226
227    // if user request help write it out to cout.
228    if (arguments.read("-h") || arguments.read("--help"))
229    {
230        arguments.getApplicationUsage()->write(std::cout);
231        return 1;
232    }
233
234    // any option left unread are converted into errors to write out later.
235    arguments.reportRemainingOptionsAsUnrecognized();
236
237    // report any errors if they have occured when parsing the program aguments.
238    if (arguments.errors())
239    {
240        arguments.writeErrorMessages(std::cout);
241        return 1;
242    }
243   
244    // read the scene from the list of file specified commandline args.
245    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
246
247    osg::ref_ptr<osg::Group> group = dynamic_cast<osg::Group*>(scene.get());
248    if (!group)
249    {
250        group = new osg::Group;
251        group->addChild(scene.get());
252    }
253
254    osg::ref_ptr<osgText::Text> updateText = new osgText::Text;
255
256    // add the HUD subgraph.   
257    group->addChild(createHUD(updateText.get()));
258
259    // add the handler for doing the picking
260    viewer.getEventHandlerList().push_front(new PickHandler(&viewer,updateText.get()));
261
262    // set the scene to render
263    viewer.setSceneData(group.get());
264
265    // create the windows and run the threads.
266    viewer.realize();
267
268    while( !viewer.done() )
269    {
270        // wait for all cull and draw threads to complete.
271        viewer.sync();
272
273        // update the scene by traversing it with the the update visitor which will
274        // call all node update callbacks and animations.
275        viewer.update();
276         
277        // fire off the cull and draw traversals of the scene.
278        viewer.frame();
279       
280    }
281   
282    // wait for all cull and draw threads to complete.
283    viewer.sync();
284
285    // run a clean up frame to delete all OpenGL objects.
286    viewer.cleanup_frame();
287
288    // wait for all the clean up frame to complete.
289    viewer.sync();
290   
291    return 0;
292}
Note: See TracBrowser for help on using the browser.