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 <osg/Config>
24 
25 #if defined(_MSC_VER) && defined(OSG_DISABLE_MSVC_WARNINGS)
26     // disable warning "glutCreateMenu_ATEXIT_HACK' : unreferenced local function has been removed"
27     #pragma warning( disable : 4505 )
28 #endif
29 
30 #include <iostream>
31 #ifdef WIN32
32 #include <windows.h>
33 #endif
34 
35 #ifdef __APPLE__
36 #  include <GLUT/glut.h>
37 #else
38 #  include <GL/glut.h>
39 #endif
40 
41 #include <osgViewer/Viewer>
42 #include <osgViewer/ViewerEventHandlers>
43 #include <osgGA/TrackballManipulator>
44 #include <osgDB/ReadFile>
45 
46 osg::ref_ptr<osgViewer::Viewer> viewer;
47 osg::observer_ptr<osgViewer::GraphicsWindow> window;
48 
display(void)49 void display(void)
50 {
51     // update and render the scene graph
52     if (viewer.valid()) viewer->frame();
53 
54     // Swap Buffers
55     glutSwapBuffers();
56     glutPostRedisplay();
57 }
58 
reshape(int w,int h)59 void reshape( int w, int h )
60 {
61     // update the window dimensions, in case the window has been resized.
62     if (window.valid())
63     {
64         window->resized(window->getTraits()->x, window->getTraits()->y, w, h);
65         window->getEventQueue()->windowResize(window->getTraits()->x, window->getTraits()->y, w, h );
66     }
67 }
68 
mousebutton(int button,int state,int x,int y)69 void mousebutton( int button, int state, int x, int y )
70 {
71     if (window.valid())
72     {
73         if (state==0) window->getEventQueue()->mouseButtonPress( x, y, button+1 );
74         else window->getEventQueue()->mouseButtonRelease( x, y, button+1 );
75     }
76 }
77 
mousemove(int x,int y)78 void mousemove( int x, int y )
79 {
80     if (window.valid())
81     {
82         window->getEventQueue()->mouseMotion( x, y );
83     }
84 }
85 
keyboard(unsigned char key,int,int)86 void keyboard( unsigned char key, int /*x*/, int /*y*/ )
87 {
88     switch( key )
89     {
90         case 27:
91             // clean up the viewer
92             if (viewer.valid()) viewer = 0;
93             glutDestroyWindow(glutGetWindow());
94             break;
95         default:
96             if (window.valid())
97             {
98                 window->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key );
99                 window->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key );
100             }
101             break;
102     }
103 }
104 
main(int argc,char ** argv)105 int main( int argc, char **argv )
106 {
107     glutInit(&argc, argv);
108 
109     if (argc<2)
110     {
111         std::cout << argv[0] <<": requires filename argument." << std::endl;
112         return 1;
113     }
114 
115     // load the scene.
116     osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFile(argv[1]);
117     if (!loadedModel)
118     {
119         std::cout << argv[0] <<": No data loaded." << std::endl;
120         return 1;
121     }
122 
123     glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA );
124     glutInitWindowPosition( 100, 100 );
125     glutInitWindowSize( 800, 600 );
126     glutCreateWindow( argv[0] );
127     glutDisplayFunc( display );
128     glutReshapeFunc( reshape );
129     glutMouseFunc( mousebutton );
130     glutMotionFunc( mousemove );
131     glutKeyboardFunc( keyboard );
132 
133     // create the view of the scene.
134     viewer = new osgViewer::Viewer;
135     window = viewer->setUpViewerAsEmbeddedInWindow(100,100,800,600);
136     viewer->setSceneData(loadedModel.get());
137     viewer->setCameraManipulator(new osgGA::TrackballManipulator);
138     viewer->addEventHandler(new osgViewer::StatsHandler);
139     viewer->realize();
140 
141     glutMainLoop();
142 
143     return 0;
144 }
145 
146 /*EOF*/
147