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

Revision 6814, 4.4 kB (checked in by robert, 7 years ago)

Ported GLUT example to using osgViewer::Viewer rather than SimpleViewer? by using
a local GraphicsWindowEmbedded? class to help the adaption.

  • 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
66class GraphicsWindowEmbedded : public osgViewer::GraphicsWindow
67{
68public:
69   
70    GraphicsWindowEmbedded(osg::GraphicsContext::Traits* traits=0)
71    {
72            _traits = traits;
73
74            init();
75           
76            if (valid())
77            {
78                setState( new osg::State );
79                getState()->setGraphicsContext(this);
80
81                if (_traits.valid() && _traits->sharedContext)
82                {
83                    getState()->setContextID( _traits->sharedContext->getState()->getContextID() );
84                    incrementContextIDUsageCount( getState()->getContextID() );   
85                }
86                else
87                {
88                    getState()->setContextID( osg::GraphicsContext::createNewContextID() );
89                }
90            }
91    }
92
93    void init() {}
94   
95    virtual bool valid() const { return true; }
96    virtual bool realizeImplementation() { return true; }
97    virtual bool isRealizedImplementation() const  { return true; }
98    virtual void closeImplementation() {}
99    virtual bool makeCurrentImplementation() {}
100    virtual bool releaseContextImplementation() {}
101    virtual void swapBuffersImplementation() {}
102    virtual void grabFocus() {}
103    virtual void grabFocusIfPointerInWindow() {}
104};
105
106int main( int argc, char **argv )
107{
108    glutInit(&argc, argv);
109
110    if (argc<2)
111    {
112        std::cout << argv[0] <<": requires filename argument." << std::endl;
113        return 1;
114    }
115
116    // load the scene.
117    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
118    if (!loadedModel)
119    {
120        std::cout << argv[0] <<": No data loaded." << std::endl;
121        return 1;
122    }
123
124    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA );
125    glutInitWindowPosition( 100, 100 );
126    glutInitWindowSize( 800, 600 );
127    glutCreateWindow( argv[0] );
128    glutDisplayFunc( display );
129    glutReshapeFunc( reshape );
130    glutMouseFunc( mousebutton );
131    glutMotionFunc( mousemove );
132    glutKeyboardFunc( keyboard );
133
134    osg::GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits;
135    traits->x = 100;
136    traits->y = 100;
137    traits->width = 800;
138    traits->height = 600;
139    window = new GraphicsWindowEmbedded(traits);
140
141    // create the view of the scene.
142    viewer = new osgViewer::Viewer;
143    viewer->getCamera()->setGraphicsContext(window.get());
144    viewer->getCamera()->setViewport(new osg::Viewport(0,0,800,600));
145    viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
146
147    viewer->setSceneData(loadedModel.get());
148    viewer->setCameraManipulator(new osgGA::TrackballManipulator);
149    viewer->addEventHandler(new osgViewer::StatsHandler);
150    viewer->realize();
151
152    glutMainLoop();
153   
154    return 0;
155}
156
157/*EOF*/
Note: See TracBrowser for help on using the browser.