root/OpenSceneGraph/trunk/examples/osgviewerSDL/osgviewerSDL.cpp @ 6815

Revision 6815, 4.8 kB (checked in by robert, 7 years ago)

Added osgViewer::GraphicsWindowEmbedded? and ported GLUT, SDL and WX examples across from SimpleViewer? to Viewer.

  • 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 <osgViewer/Viewer>
7#include <osgViewer/StatsHandler>
8#include <osgGA/TrackballManipulator>
9#include <osgDB/ReadFile>
10
11#include "SDL.h"
12
13#include <iostream>
14
15bool convertEvent(SDL_Event& event, osgGA::EventQueue& eventQueue)
16{
17    switch (event.type) {
18
19        case SDL_MOUSEMOTION:
20            eventQueue.mouseMotion(event.motion.x, event.motion.y);
21            return true;
22
23        case SDL_MOUSEBUTTONDOWN:
24            eventQueue.mouseButtonPress(event.button.x, event.button.y, event.button.button);
25            return true;
26
27        case SDL_MOUSEBUTTONUP:
28            eventQueue.mouseButtonRelease(event.button.x, event.button.y, event.button.button);
29            return true;
30
31        case SDL_KEYUP:
32            eventQueue.keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event.key.keysym.unicode);
33            return true;
34
35        case SDL_KEYDOWN:
36            eventQueue.keyPress( (osgGA::GUIEventAdapter::KeySymbol) event.key.keysym.unicode);
37            return true;
38
39        case SDL_VIDEORESIZE:
40            eventQueue.windowResize(0, 0, event.resize.w, event.resize.h );
41            return true;
42
43        default:
44            break;
45    }
46    return false;
47}
48
49int main( int argc, char **argv )
50{
51    if (argc<2)
52    {
53        std::cout << argv[0] <<": requires filename argument." << std::endl;
54        return 1;
55    }
56
57    // init SDL
58    if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
59    {
60        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
61        exit(1);
62    }
63    atexit(SDL_Quit);
64   
65
66    // load the scene.
67    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
68    if (!loadedModel)
69    {
70        std::cout << argv[0] <<": No data loaded." << std::endl;
71        return 1;
72    }
73
74    // Starting with SDL 1.2.10, passing in 0 will use the system's current resolution.
75    unsigned int windowWidth = 0;
76    unsigned int windowHeight = 0;
77
78    // Passing in 0 for bitdepth also uses the system's current bitdepth. This works before 1.2.10 too.
79    unsigned int bitDepth = 0;
80
81    // If not linked to SDL 1.2.10+, then we must use hardcoded values
82    const SDL_version* linked_version = SDL_Linked_Version();
83    if(linked_version->major == 1 && linked_version->minor == 2)
84    {
85        if(linked_version->patch < 10)
86        {
87            windowWidth = 1280;
88            windowHeight = 1024;
89        }
90    }
91
92    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
93    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
94    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
95    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
96    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
97   
98    // set up the surface to render to
99    SDL_Surface* screen = SDL_SetVideoMode(windowWidth, windowHeight, bitDepth, SDL_OPENGL | SDL_FULLSCREEN | SDL_RESIZABLE);
100    if ( screen == NULL )
101    {
102        std::cerr<<"Unable to set "<<windowWidth<<"x"<<windowHeight<<" video: %s\n"<< SDL_GetError()<<std::endl;
103        exit(1);
104    }
105
106    SDL_EnableUNICODE(1);
107   
108    // If we used 0 to set the fields, query the values so we can pass it to osgViewer
109    windowWidth = screen->w;
110    windowHeight = screen->h;
111    osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> gw = new osgViewer::GraphicsWindowEmbedded(0,0,windowWidth,windowHeight);
112   
113    osgViewer::Viewer viewer;
114    viewer.getCamera()->setGraphicsContext(gw.get());
115    viewer.getCamera()->setViewport(new osg::Viewport(0,0,windowWidth,windowHeight));
116    viewer.setSceneData(loadedModel.get());
117    viewer.setCameraManipulator(new osgGA::TrackballManipulator);
118    viewer.addEventHandler(new osgViewer::StatsHandler);
119    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
120    viewer.realize();
121
122    bool done = false;
123    while( !done )
124    {
125        SDL_Event event;
126
127        while ( SDL_PollEvent(&event) )
128        {
129            // pass the SDL event into the viewers event queue
130            convertEvent(event, *(gw->getEventQueue()));
131
132            switch (event.type) {
133
134                case SDL_VIDEORESIZE:
135                    gw->resized(0, 0, event.resize.w, event.resize.h );
136                    break;
137
138                case SDL_KEYUP:
139
140                    if (event.key.keysym.sym==SDLK_ESCAPE) done = true;
141                    if (event.key.keysym.sym=='f')
142                    {
143                        SDL_WM_ToggleFullScreen(screen);
144                        gw->resized(0, 0, screen->w, screen->h );
145                    }
146
147                    break;
148
149                case SDL_QUIT:
150                    done = true;
151            }
152        }
153
154        if (done) continue;
155
156
157        // draw the new frame
158        viewer.frame();
159
160        // Swap Buffers
161        SDL_GL_SwapBuffers();
162    }
163   
164    return 0;
165}
166
167/*EOF*/
Note: See TracBrowser for help on using the browser.