root/OpenSceneGraph/trunk/examples/osgviewerGLUT/osgsimpleviewerGLUT.cpp @ 6812

Revision 5679, 2.5 kB (checked in by robert, 7 years ago)

Moved SimpleViewer? and GraphicsWindow? into their own osgViewer library, updated simpleviewer examples to reflect this change

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