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

Revision 9640, 4.2 kB (checked in by robert, 6 years ago)

Disabled warning

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