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

This commit is contained in:
Robert Osfield 2007-06-02 15:31:21 +00:00
parent 8edb2f7284
commit 179f903c90
5 changed files with 153 additions and 90 deletions

View File

@ -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;

View File

@ -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 <osgViewer/SimpleViewer>
#include <osgViewer/Viewer>
#include <osgViewer/StatsHandler>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
@ -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<osgViewer::GraphicsWindowEmbedded> 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;

View File

@ -10,6 +10,9 @@
#endif
#include "osgviewerWX.h"
#include <osgViewer/StatsHandler>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#include <wx/image.h>
@ -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<osg::Node> 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

View File

@ -5,10 +5,10 @@
#include "wx/app.h"
#include "wx/cursor.h"
#include "wx/glcanvas.h"
#include <osgViewer/SimpleViewer>
#include <osgViewer/Viewer>
#include <string>
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<osgViewer::Viewer> _viewer;
DECLARE_EVENT_TABLE()
};

View File

@ -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