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

Revision 6941, 4.0 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

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