2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgviewerSDL.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2006-10-01 04:06:26 +08:00
|
|
|
// (C) 2005 Mike Weiblen http://mew.cx/ released under the OSGPL.
|
|
|
|
// Simple example using GLUT to create an OpenGL window and OSG for rendering.
|
|
|
|
// Derived from osgGLUTsimple.cpp and osgkeyboardmouse.cpp
|
|
|
|
|
2007-06-02 23:31:21 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2007-06-07 00:23:20 +08:00
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
2006-10-01 04:06:26 +08:00
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
2006-11-15 00:59:00 +08:00
|
|
|
#include "SDL.h"
|
2006-10-01 04:06:26 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2006-10-01 19:08:40 +08:00
|
|
|
bool convertEvent(SDL_Event& event, osgGA::EventQueue& eventQueue)
|
|
|
|
{
|
|
|
|
switch (event.type) {
|
|
|
|
|
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
eventQueue.mouseMotion(event.motion.x, event.motion.y);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
eventQueue.mouseButtonPress(event.button.x, event.button.y, event.button.button);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
|
|
|
eventQueue.mouseButtonRelease(event.button.x, event.button.y, event.button.button);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case SDL_KEYUP:
|
|
|
|
eventQueue.keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event.key.keysym.unicode);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
eventQueue.keyPress( (osgGA::GUIEventAdapter::KeySymbol) event.key.keysym.unicode);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case SDL_VIDEORESIZE:
|
|
|
|
eventQueue.windowResize(0, 0, event.resize.w, event.resize.h );
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-10-01 04:06:26 +08:00
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
if (argc<2)
|
|
|
|
{
|
|
|
|
std::cout << argv[0] <<": requires filename argument." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// init SDL
|
|
|
|
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
atexit(SDL_Quit);
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2006-10-01 04:06:26 +08:00
|
|
|
|
|
|
|
// load the scene.
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFile(argv[1]);
|
2006-10-01 04:06:26 +08:00
|
|
|
if (!loadedModel)
|
|
|
|
{
|
|
|
|
std::cout << argv[0] <<": No data loaded." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-11-15 00:59:00 +08:00
|
|
|
// Starting with SDL 1.2.10, passing in 0 will use the system's current resolution.
|
|
|
|
unsigned int windowWidth = 0;
|
|
|
|
unsigned int windowHeight = 0;
|
|
|
|
|
2006-11-15 19:21:53 +08:00
|
|
|
// Passing in 0 for bitdepth also uses the system's current bitdepth. This works before 1.2.10 too.
|
2006-11-15 00:59:00 +08:00
|
|
|
unsigned int bitDepth = 0;
|
2006-10-01 04:06:26 +08:00
|
|
|
|
2006-11-15 19:21:53 +08:00
|
|
|
// If not linked to SDL 1.2.10+, then we must use hardcoded values
|
|
|
|
const SDL_version* linked_version = SDL_Linked_Version();
|
|
|
|
if(linked_version->major == 1 && linked_version->minor == 2)
|
|
|
|
{
|
2007-12-08 23:04:47 +08:00
|
|
|
if(linked_version->patch < 10)
|
|
|
|
{
|
|
|
|
windowWidth = 1280;
|
|
|
|
windowHeight = 1024;
|
|
|
|
}
|
2006-11-15 19:21:53 +08:00
|
|
|
}
|
|
|
|
|
2006-10-01 04:06:26 +08:00
|
|
|
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
|
|
|
|
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
|
|
|
|
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
|
|
|
|
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
|
|
|
|
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2006-11-15 00:59:00 +08:00
|
|
|
// set up the surface to render to
|
|
|
|
SDL_Surface* screen = SDL_SetVideoMode(windowWidth, windowHeight, bitDepth, SDL_OPENGL | SDL_FULLSCREEN | SDL_RESIZABLE);
|
2006-10-01 04:06:26 +08:00
|
|
|
if ( screen == NULL )
|
|
|
|
{
|
|
|
|
std::cerr<<"Unable to set "<<windowWidth<<"x"<<windowHeight<<" video: %s\n"<< SDL_GetError()<<std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2006-11-15 00:59:00 +08:00
|
|
|
|
2006-10-01 04:06:26 +08:00
|
|
|
SDL_EnableUNICODE(1);
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2006-11-15 00:59:00 +08:00
|
|
|
// If we used 0 to set the fields, query the values so we can pass it to osgViewer
|
|
|
|
windowWidth = screen->w;
|
|
|
|
windowHeight = screen->h;
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2007-06-02 23:31:21 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2007-06-03 17:34:28 +08:00
|
|
|
osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> gw = viewer.setUpViewerAsEmbeddedInWindow(0,0,windowWidth,windowHeight);
|
2007-06-02 23:31:21 +08:00
|
|
|
viewer.setSceneData(loadedModel.get());
|
|
|
|
viewer.setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
viewer.addEventHandler(new osgViewer::StatsHandler);
|
|
|
|
viewer.realize();
|
2006-10-01 04:06:26 +08:00
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
while( !done )
|
|
|
|
{
|
|
|
|
SDL_Event event;
|
|
|
|
|
|
|
|
while ( SDL_PollEvent(&event) )
|
|
|
|
{
|
2006-10-01 19:08:40 +08:00
|
|
|
// pass the SDL event into the viewers event queue
|
2007-06-02 23:31:21 +08:00
|
|
|
convertEvent(event, *(gw->getEventQueue()));
|
2006-10-01 04:06:26 +08:00
|
|
|
|
2006-10-01 19:08:40 +08:00
|
|
|
switch (event.type) {
|
2006-10-01 04:06:26 +08:00
|
|
|
|
2007-06-02 23:31:21 +08:00
|
|
|
case SDL_VIDEORESIZE:
|
2007-12-08 23:04:47 +08:00
|
|
|
SDL_SetVideoMode(event.resize.w, event.resize.h, bitDepth, SDL_OPENGL | SDL_RESIZABLE);
|
2007-06-02 23:31:21 +08:00
|
|
|
gw->resized(0, 0, event.resize.w, event.resize.h );
|
|
|
|
break;
|
|
|
|
|
2006-10-01 04:06:26 +08:00
|
|
|
case SDL_KEYUP:
|
|
|
|
|
|
|
|
if (event.key.keysym.sym==SDLK_ESCAPE) done = true;
|
2015-10-22 22:14:53 +08:00
|
|
|
if (event.key.keysym.sym=='f')
|
2007-06-02 23:31:21 +08:00
|
|
|
{
|
|
|
|
SDL_WM_ToggleFullScreen(screen);
|
|
|
|
gw->resized(0, 0, screen->w, screen->h );
|
|
|
|
}
|
2006-10-01 04:06:26 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_QUIT:
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (done) continue;
|
|
|
|
|
|
|
|
|
|
|
|
// draw the new frame
|
|
|
|
viewer.frame();
|
|
|
|
|
2006-11-15 00:59:00 +08:00
|
|
|
// Swap Buffers
|
2006-10-01 04:06:26 +08:00
|
|
|
SDL_GL_SwapBuffers();
|
|
|
|
}
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2006-10-01 04:06:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*EOF*/
|