root/OpenSceneGraph/trunk/examples/osgviewerGLUT/osgviewerGLUT.cpp @ 6899

Revision 6899, 3.1 kB (checked in by robert, 8 years ago)

Added clean up of the view before destruction of the window

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL.
2// (C) 2005 Mike Weiblen http://mew.cx/ released under the OSGPL.
3// Simple example using GLUT to create an OpenGL window and OSG for rendering.
4// Derived from osgGLUTsimple.cpp and osgkeyboardmouse.cpp
5
6#include <iostream>
7#ifdef WIN32
8#include <windows.h>
9#endif
10#ifdef __APPLE__
11include <GLUT/glut.h>
12#else
13include <GL/glut.h>
14#endif
15
16#include <osgViewer/Viewer>
17#include <osgViewer/ViewerEventHandlers>
18#include <osgGA/TrackballManipulator>
19#include <osgDB/ReadFile>
20
21osg::ref_ptr<osgViewer::Viewer> viewer;
22osg::observer_ptr<osgViewer::GraphicsWindow> window;
23
24void display(void)
25{
26    // update and render the scene graph
27    if (viewer.valid()) viewer->frame();
28
29    // Swap Buffers
30    glutSwapBuffers();
31    glutPostRedisplay();
32}
33
34void reshape( int w, int h )
35{
36    // update the window dimensions, in case the window has been resized.
37    if (window.valid())
38    {
39        window->resized(window->getTraits()->x, window->getTraits()->y, w, h);
40        window->getEventQueue()->windowResize(window->getTraits()->x, window->getTraits()->y, w, h );
41    }
42}
43
44void mousebutton( int button, int state, int x, int y )
45{
46    if (window.valid())
47    {
48        if (state==0) window->getEventQueue()->mouseButtonPress( x, y, button+1 );
49        else window->getEventQueue()->mouseButtonRelease( x, y, button+1 );
50    }
51}
52
53void mousemove( int x, int y )
54{
55    if (window.valid())
56    {
57        window->getEventQueue()->mouseMotion( x, y );
58    }
59}
60
61void keyboard( unsigned char key, int /*x*/, int /*y*/ )
62{
63    switch( key )
64    {
65        case 27:
66            // clean up the viewer
67            if (viewer.valid()) viewer = 0;
68            glutDestroyWindow(glutGetWindow());
69            break;
70        default:
71            if (window.valid())
72            {
73                window->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key );
74                window->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key );
75            }
76            break;
77    }
78}
79
80int main( int argc, char **argv )
81{
82    glutInit(&argc, argv);
83
84    if (argc<2)
85    {
86        std::cout << argv[0] <<": requires filename argument." << std::endl;
87        return 1;
88    }
89
90    // load the scene.
91    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
92    if (!loadedModel)
93    {
94        std::cout << argv[0] <<": No data loaded." << std::endl;
95        return 1;
96    }
97
98    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA );
99    glutInitWindowPosition( 100, 100 );
100    glutInitWindowSize( 800, 600 );
101    glutCreateWindow( argv[0] );
102    glutDisplayFunc( display );
103    glutReshapeFunc( reshape );
104    glutMouseFunc( mousebutton );
105    glutMotionFunc( mousemove );
106    glutKeyboardFunc( keyboard );
107
108    // create the view of the scene.
109    viewer = new osgViewer::Viewer;
110    window = viewer->setUpViewerAsEmbeddedInWindow(100,100,800,600);
111    viewer->setSceneData(loadedModel.get());
112    viewer->setCameraManipulator(new osgGA::TrackballManipulator);
113    viewer->addEventHandler(new osgViewer::StatsHandler);
114    viewer->realize();
115
116    glutMainLoop();
117   
118    return 0;
119}
120
121/*EOF*/
Note: See TracBrowser for help on using the browser.