From 179f903c90678598d4be5b15f518233526352f80 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sat, 2 Jun 2007 15:31:21 +0000 Subject: [PATCH] Added osgViewer::GraphicsWindowEmbedded and ported GLUT, SDL and WX examples across from SimpleViewer to Viewer. --- examples/osgviewerGLUT/osgviewerGLUT.cpp | 47 +-------------- examples/osgviewerSDL/osgviewerSDL.cpp | 30 +++++++--- examples/osgviewerWX/osgviewerWX.cpp | 73 ++++++++++++++++++++---- examples/osgviewerWX/osgviewerWX.h | 39 +++++-------- include/osgViewer/GraphicsWindow | 54 ++++++++++++++++++ 5 files changed, 153 insertions(+), 90 deletions(-) diff --git a/examples/osgviewerGLUT/osgviewerGLUT.cpp b/examples/osgviewerGLUT/osgviewerGLUT.cpp index 90cf034e3..e81217311 100644 --- a/examples/osgviewerGLUT/osgviewerGLUT.cpp +++ b/examples/osgviewerGLUT/osgviewerGLUT.cpp @@ -63,46 +63,6 @@ void keyboard( unsigned char key, int /*x*/, int /*y*/ ) } } -class GraphicsWindowEmbedded : public osgViewer::GraphicsWindow -{ -public: - - GraphicsWindowEmbedded(osg::GraphicsContext::Traits* traits=0) - { - _traits = traits; - - init(); - - if (valid()) - { - setState( new osg::State ); - getState()->setGraphicsContext(this); - - if (_traits.valid() && _traits->sharedContext) - { - getState()->setContextID( _traits->sharedContext->getState()->getContextID() ); - incrementContextIDUsageCount( getState()->getContextID() ); - } - else - { - getState()->setContextID( osg::GraphicsContext::createNewContextID() ); - } - } - } - - void init() {} - - virtual bool valid() const { return true; } - virtual bool realizeImplementation() { return true; } - virtual bool isRealizedImplementation() const { return true; } - virtual void closeImplementation() {} - virtual bool makeCurrentImplementation() {} - virtual bool releaseContextImplementation() {} - virtual void swapBuffersImplementation() {} - virtual void grabFocus() {} - virtual void grabFocusIfPointerInWindow() {} -}; - int main( int argc, char **argv ) { glutInit(&argc, argv); @@ -131,12 +91,7 @@ int main( int argc, char **argv ) glutMotionFunc( mousemove ); glutKeyboardFunc( keyboard ); - osg::GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits; - traits->x = 100; - traits->y = 100; - traits->width = 800; - traits->height = 600; - window = new GraphicsWindowEmbedded(traits); + window = new osgViewer::GraphicsWindowEmbedded(100,100,800,600); // create the view of the scene. viewer = new osgViewer::Viewer; diff --git a/examples/osgviewerSDL/osgviewerSDL.cpp b/examples/osgviewerSDL/osgviewerSDL.cpp index 957b5348c..9b9d2f2f2 100644 --- a/examples/osgviewerSDL/osgviewerSDL.cpp +++ b/examples/osgviewerSDL/osgviewerSDL.cpp @@ -3,7 +3,8 @@ // Simple example using GLUT to create an OpenGL window and OSG for rendering. // Derived from osgGLUTsimple.cpp and osgkeyboardmouse.cpp -#include +#include +#include #include #include @@ -104,14 +105,19 @@ int main( int argc, char **argv ) SDL_EnableUNICODE(1); - osgViewer::SimpleViewer viewer; - viewer.setSceneData(loadedModel.get()); - viewer.setCameraManipulator(new osgGA::TrackballManipulator); - // If we used 0 to set the fields, query the values so we can pass it to osgViewer windowWidth = screen->w; windowHeight = screen->h; - viewer.getEventQueue()->windowResize(0, 0, windowWidth, windowHeight ); + osg::ref_ptr gw = new osgViewer::GraphicsWindowEmbedded(0,0,windowWidth,windowHeight); + + osgViewer::Viewer viewer; + viewer.getCamera()->setGraphicsContext(gw.get()); + viewer.getCamera()->setViewport(new osg::Viewport(0,0,windowWidth,windowHeight)); + viewer.setSceneData(loadedModel.get()); + viewer.setCameraManipulator(new osgGA::TrackballManipulator); + viewer.addEventHandler(new osgViewer::StatsHandler); + viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); + viewer.realize(); bool done = false; while( !done ) @@ -121,14 +127,22 @@ int main( int argc, char **argv ) while ( SDL_PollEvent(&event) ) { // pass the SDL event into the viewers event queue - convertEvent(event, *viewer.getEventQueue()); + convertEvent(event, *(gw->getEventQueue())); switch (event.type) { + case SDL_VIDEORESIZE: + gw->resized(0, 0, event.resize.w, event.resize.h ); + break; + case SDL_KEYUP: if (event.key.keysym.sym==SDLK_ESCAPE) done = true; - if (event.key.keysym.sym=='f') SDL_WM_ToggleFullScreen(screen); + if (event.key.keysym.sym=='f') + { + SDL_WM_ToggleFullScreen(screen); + gw->resized(0, 0, screen->w, screen->h ); + } break; diff --git a/examples/osgviewerWX/osgviewerWX.cpp b/examples/osgviewerWX/osgviewerWX.cpp index 8cafdc5e9..c450301d9 100644 --- a/examples/osgviewerWX/osgviewerWX.cpp +++ b/examples/osgviewerWX/osgviewerWX.cpp @@ -10,6 +10,9 @@ #endif #include "osgviewerWX.h" + + +#include #include #include #include @@ -24,8 +27,20 @@ bool wxOsgApp::OnInit() // create osg canvas // - initialize - SimpleViewerWX *viewerWindow = new SimpleViewerWX(frame, wxID_ANY, wxDefaultPosition, - wxSize(200, 200), wxSUNKEN_BORDER); + + + int width = 800; + int height = 600; + + GraphicsWindowWX* gw = new GraphicsWindowWX(frame, wxID_ANY, wxDefaultPosition, + wxSize(width, height), wxSUNKEN_BORDER); + + + osgViewer::Viewer *viewer = new osgViewer::Viewer; + viewer->getCamera()->setGraphicsContext(gw); + viewer->getCamera()->setViewport(0,0,width,height); + viewer->addEventHandler(new osgViewer::StatsHandler); + viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); // load the scene. osg::ref_ptr loadedModel = osgDB::readNodeFile("cow.osg"); @@ -33,10 +48,11 @@ bool wxOsgApp::OnInit() { return false; } - viewerWindow->setSceneData(loadedModel.get()); - viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator); + + viewer->setSceneData(loadedModel.get()); + viewer->setCameraManipulator(new osgGA::TrackballManipulator); - frame->SetSimpleViewer(viewerWindow); + frame->SetViewer(viewer); /* Show the frame */ frame->Show(true); @@ -55,17 +71,17 @@ MainFrame::MainFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, long style) : wxFrame(frame, wxID_ANY, title, pos, size, style) { - _viewerWindow = NULL; } -void MainFrame::SetSimpleViewer(SimpleViewerWX *viewer) +void MainFrame::SetViewer(osgViewer::Viewer *viewer) { - _viewerWindow = viewer; + _viewer = viewer; } void MainFrame::OnIdle(wxIdleEvent &event) { - _viewerWindow->render(); + _viewer->frame(); + event.RequestMore(); } @@ -84,6 +100,34 @@ GraphicsWindowWX::GraphicsWindowWX(wxWindow *parent, wxWindowID id, { // default cursor to standard _oldCursor = *wxSTANDARD_CURSOR; + + _traits = new GraphicsContext::Traits; + _traits->x = pos.x; + _traits->y = pos.y; + _traits->width = size.x; + _traits->height = size.y; + + init(); + +} + +void GraphicsWindowWX::init() +{ + if (valid()) + { + setState( new osg::State ); + getState()->setGraphicsContext(this); + + if (_traits.valid() && _traits->sharedContext) + { + getState()->setContextID( _traits->sharedContext->getState()->getContextID() ); + incrementContextIDUsageCount( getState()->getContextID() ); + } + else + { + getState()->setContextID( osg::GraphicsContext::createNewContextID() ); + } + } } GraphicsWindowWX::~GraphicsWindowWX() @@ -107,6 +151,7 @@ void GraphicsWindowWX::OnSize(wxSizeEvent& event) // update the window dimensions, in case the window has been resized. getEventQueue()->windowResize(0, 0, width, height); + resized(0,0,width,height); } void GraphicsWindowWX::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) @@ -116,8 +161,12 @@ void GraphicsWindowWX::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) void GraphicsWindowWX::OnKeyDown(wxKeyEvent &event) { +#if 1 + int key = event.GetUnicodeKey(); +#else int key = event.GetKeyCode(); - getEventQueue()->keyPress(key); +#endif + getEventQueue()->keyPress(key); // propagate event event.Skip(); @@ -125,7 +174,11 @@ void GraphicsWindowWX::OnKeyDown(wxKeyEvent &event) void GraphicsWindowWX::OnKeyUp(wxKeyEvent &event) { +#if 1 + int key = event.GetUnicodeKey(); +#else int key = event.GetKeyCode(); +#endif getEventQueue()->keyRelease(key); // propagate event diff --git a/examples/osgviewerWX/osgviewerWX.h b/examples/osgviewerWX/osgviewerWX.h index 954e2c19d..87a0607f6 100644 --- a/examples/osgviewerWX/osgviewerWX.h +++ b/examples/osgviewerWX/osgviewerWX.h @@ -5,10 +5,10 @@ #include "wx/app.h" #include "wx/cursor.h" #include "wx/glcanvas.h" -#include +#include #include -class GraphicsWindowWX: public wxGLCanvas, virtual osgViewer::GraphicsWindow +class GraphicsWindowWX: public wxGLCanvas, public osgViewer::GraphicsWindow { public: GraphicsWindowWX(wxWindow *parent, wxWindowID id = wxID_ANY, @@ -17,6 +17,8 @@ public: const wxString& name = wxT("TestGLCanvas")); ~GraphicsWindowWX(); + + void init(); void OnPaint(wxPaintEvent& event); void OnSize(wxSizeEvent& event); @@ -36,45 +38,30 @@ public: bool makeCurrentImplementation(); void swapBuffersImplementation(); + // note implemented yet...just use dummy implementation to get working. + virtual bool valid() const { return true; } + virtual bool realizeImplementation() { return true; } + virtual bool isRealizedImplementation() const { return true; } + virtual void closeImplementation() {} + virtual bool releaseContextImplementation() { return true; } + private: wxCursor _oldCursor; DECLARE_EVENT_TABLE() }; -class SimpleViewerWX : public osgViewer::SimpleViewer, public GraphicsWindowWX -{ -public: - SimpleViewerWX(wxWindow *parent, wxWindowID id = wxID_ANY, - const wxPoint& pos = wxDefaultPosition, - const wxSize& size = wxDefaultSize, long style = 0, - const wxString& name = wxT("TestGLCanvas")) : - GraphicsWindowWX(parent, id, pos, size, style, name) - { - } - - void render() - { - makeCurrentImplementation(); - - // update and render the scene graph - frame(); - - swapBuffersImplementation(); - } -}; - class MainFrame : public wxFrame { public: MainFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, long style = wxDEFAULT_FRAME_STYLE); - void SetSimpleViewer(SimpleViewerWX *viewer); + void SetViewer(osgViewer::Viewer *viewer); void OnIdle(wxIdleEvent& event); private: - SimpleViewerWX *_viewerWindow; + osg::ref_ptr _viewer; DECLARE_EVENT_TABLE() }; diff --git a/include/osgViewer/GraphicsWindow b/include/osgViewer/GraphicsWindow index 5557f3c64..e46e78c41 100644 --- a/include/osgViewer/GraphicsWindow +++ b/include/osgViewer/GraphicsWindow @@ -150,6 +150,60 @@ class OSGVIEWER_EXPORT GraphicsWindow : public osg::GraphicsContext, public osgG }; +class GraphicsWindowEmbedded : public GraphicsWindow +{ + public: + + GraphicsWindowEmbedded(osg::GraphicsContext::Traits* traits=0) + { + _traits = traits; + + init(); + + } + + GraphicsWindowEmbedded(int x, int y, int width, int height) + { + _traits = new GraphicsContext::Traits; + _traits->x = x; + _traits->y = y; + _traits->width = width; + _traits->height = height; + + init(); + } + + void init() + { + if (valid()) + { + setState( new osg::State ); + getState()->setGraphicsContext(this); + + if (_traits.valid() && _traits->sharedContext) + { + getState()->setContextID( _traits->sharedContext->getState()->getContextID() ); + incrementContextIDUsageCount( getState()->getContextID() ); + } + else + { + getState()->setContextID( osg::GraphicsContext::createNewContextID() ); + } + } + } + + // dumy implementations, assume that graphics context is *always* current and valid. + virtual bool valid() const { return true; } + virtual bool realizeImplementation() { return true; } + virtual bool isRealizedImplementation() const { return true; } + virtual void closeImplementation() {} + virtual bool makeCurrentImplementation() { return true; } + virtual bool releaseContextImplementation() { return true; } + virtual void swapBuffersImplementation() {} + virtual void grabFocus() {} + virtual void grabFocusIfPointerInWindow() {} +}; + } #endif