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

Revision 6821, 2.8 kB (checked in by robert, 8 years ago)

Added Viewer::setUpViewerAsEmbeddedInWindow convinience method

  • 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/StatsHandler>
18#include <osgGA/TrackballManipulator>
19#include <osgDB/ReadFile>
20
21osg::ref_ptr<osgViewer::Viewer> viewer;
22osg::ref_ptr<osgViewer::GraphicsWindow> window;
23
24void display(void)
25{
26    // update and render the scene graph
27    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    window->resized(window->getTraits()->x, window->getTraits()->y, w, h);
38    window->getEventQueue()->windowResize(window->getTraits()->x, window->getTraits()->y, w, h );
39}
40
41void mousebutton( int button, int state, int x, int y )
42{
43    if (state==0) window->getEventQueue()->mouseButtonPress( x, y, button+1 );
44    else window->getEventQueue()->mouseButtonRelease( x, y, button+1 );
45}
46
47void mousemove( int x, int y )
48{
49    window->getEventQueue()->mouseMotion( x, y );
50}
51
52void keyboard( unsigned char key, int /*x*/, int /*y*/ )
53{
54    switch( key )
55    {
56        case 27:
57            glutDestroyWindow(glutGetWindow());
58            break;
59        default:
60            window->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key );
61            window->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key );
62            break;
63    }
64}
65
66int main( int argc, char **argv )
67{
68    glutInit(&argc, argv);
69
70    if (argc<2)
71    {
72        std::cout << argv[0] <<": requires filename argument." << std::endl;
73        return 1;
74    }
75
76    // load the scene.
77    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
78    if (!loadedModel)
79    {
80        std::cout << argv[0] <<": No data loaded." << std::endl;
81        return 1;
82    }
83
84    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA );
85    glutInitWindowPosition( 100, 100 );
86    glutInitWindowSize( 800, 600 );
87    glutCreateWindow( argv[0] );
88    glutDisplayFunc( display );
89    glutReshapeFunc( reshape );
90    glutMouseFunc( mousebutton );
91    glutMotionFunc( mousemove );
92    glutKeyboardFunc( keyboard );
93
94    // create the view of the scene.
95    viewer = new osgViewer::Viewer;
96    window = viewer->setUpViewerAsEmbeddedInWindow(100,100,800,600);
97    viewer->setSceneData(loadedModel.get());
98    viewer->setCameraManipulator(new osgGA::TrackballManipulator);
99    viewer->addEventHandler(new osgViewer::StatsHandler);
100    viewer->realize();
101
102    glutMainLoop();
103   
104    return 0;
105}
106
107/*EOF*/
Note: See TracBrowser for help on using the browser.