|
- #include <iostream>
- #include <string>
- #include <wx/wx.h>
- #include <wx/glcanvas.h>
- #include <wx/cursor.h>
- #include <osgViewer/Viewer>
- #include <osg/GraphicsContext>
- #include <osgDB/ReadFile>
- #include <osgViewer/ViewerEventHandlers>
- #include <osgGA/TrackballManipulator>
- class MyGraphicsWindow;
- class MyCanvas : public wxGLCanvas
- {
- public:
- MyCanvas(wxWindow * parent, wxWindowID id = wxID_ANY, int * attributeList = 0);
- virtual ~MyCanvas();
- public:
- DECLARE_EVENT_TABLE()
- void OnPaint(wxPaintEvent& event);
- void OnSize(wxSizeEvent& event);
- void OnEraseBackground(wxEraseEvent& event);
- void OnChar(wxKeyEvent &event);
- void OnKeyUp(wxKeyEvent &event);
- void OnMouseEnter(wxMouseEvent &event);
- void OnMouseDown(wxMouseEvent &event);
- void OnMouseUp(wxMouseEvent &event);
- void OnMouseMotion(wxMouseEvent &event);
- void OnMouseWheel(wxMouseEvent &event);
- public:
- void SetGraphicsWindow(osgViewer::GraphicsWindow * gw);
- void UseCursor(bool value);
- private:
- osg::ref_ptr<osgViewer::GraphicsWindow> _graphics_window;
- wxCursor _oldCursor;
- };
- MyCanvas::MyCanvas(wxWindow * parent, wxWindowID id, int * attributeList)
- :wxGLCanvas(parent, id, attributeList)
- {
- _oldCursor = *wxSTANDARD_CURSOR;
- }
- MyCanvas::~MyCanvas()
- {
- }
- BEGIN_EVENT_TABLE(MyCanvas, wxGLCanvas)
- EVT_SIZE (MyCanvas::OnSize)
- EVT_PAINT (MyCanvas::OnPaint)
- EVT_ERASE_BACKGROUND (MyCanvas::OnEraseBackground)
- EVT_CHAR (MyCanvas::OnChar)
- EVT_KEY_UP (MyCanvas::OnKeyUp)
- EVT_ENTER_WINDOW (MyCanvas::OnMouseEnter)
- EVT_LEFT_DOWN (MyCanvas::OnMouseDown)
- EVT_MIDDLE_DOWN (MyCanvas::OnMouseDown)
- EVT_RIGHT_DOWN (MyCanvas::OnMouseDown)
- EVT_LEFT_UP (MyCanvas::OnMouseUp)
- EVT_MIDDLE_UP (MyCanvas::OnMouseUp)
- EVT_RIGHT_UP (MyCanvas::OnMouseUp)
- EVT_MOTION (MyCanvas::OnMouseMotion)
- EVT_MOUSEWHEEL (MyCanvas::OnMouseWheel)
- END_EVENT_TABLE()
- void MyCanvas::OnPaint(wxPaintEvent & evnet)
- {
- wxPaintDC dc(this);
- }
- void MyCanvas::OnSize(wxSizeEvent& event)
- {
- wxGLCanvas::OnSize(event);
- int width, height;
- GetClientSize(&width, &height);
- if (_graphics_window.valid())
- {
- _graphics_window->getEventQueue()->windowResize(0, 0, width, height);
- _graphics_window->resized(0,0,width,height);
- }
- }
- void MyCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
- {
- }
- void MyCanvas::OnChar(wxKeyEvent &event)
- {
- #if wxUSE_UNICODE
- int key = event.GetUnicodeKey();
- #else
- int key = event.GetKeyCode();
- #endif
- if (_graphics_window.valid())
- _graphics_window->getEventQueue()->keyPress(key);
- }
- void MyCanvas::OnKeyUp(wxKeyEvent &event)
- {
- #if wxUSE_UNICODE
- int key = event.GetUnicodeKey();
- #else
- int key = event.GetKeyCode();
- #endif
- if (_graphics_window.valid())
- _graphics_window->getEventQueue()->keyRelease(key);
- }
- void MyCanvas::OnMouseEnter(wxMouseEvent &event)
- {
- SetFocus();
- }
- void MyCanvas::OnMouseDown(wxMouseEvent &event)
- {
- if (_graphics_window.valid())
- {
- _graphics_window->getEventQueue()->mouseButtonPress(event.GetX(), event.GetY(),
- event.GetButton());
- }
- }
- void MyCanvas::OnMouseUp(wxMouseEvent &event)
- {
- if (_graphics_window.valid())
- {
- _graphics_window->getEventQueue()->mouseButtonRelease(event.GetX(), event.GetY(),
- event.GetButton());
- }
- }
- void MyCanvas::OnMouseMotion(wxMouseEvent &event)
- {
- if (_graphics_window.valid())
- _graphics_window->getEventQueue()->mouseMotion(event.GetX(), event.GetY());
- }
- void MyCanvas::OnMouseWheel(wxMouseEvent &event)
- {
- int delta = event.GetWheelRotation() / event.GetWheelDelta() * event.GetLinesPerAction();
- if (_graphics_window.valid()) {
- _graphics_window->getEventQueue()->mouseScroll(
- delta>0 ?
- osgGA::GUIEventAdapter::SCROLL_UP :
- osgGA::GUIEventAdapter::SCROLL_DOWN);
- }
- }
- void MyCanvas::UseCursor(bool value)
- {
- if (value) {
- SetCursor(_oldCursor);
- } else {
- _oldCursor = GetCursor();
- wxImage image(1, 1);
- image.SetMask(true);
- image.SetMaskColour(0, 0, 0);
- wxCursor cursor(image);
- SetCursor(cursor);
- }
- }
- void MyCanvas::SetGraphicsWindow(osgViewer::GraphicsWindow * gw)
- {
- _graphics_window = gw;
- }
- class MyGraphicsWindow : public osgViewer::GraphicsWindow
- {
- public:
- MyGraphicsWindow(MyCanvas * canvas);
- virtual ~MyGraphicsWindow();
- void init();
- void grabFocus();
- void grabFocusIfPointerInWindow();
- void useCursor(bool cursorOn);
- bool makeCurrentImplementation();
- void swapBuffersImplementation();
- virtual bool valid() const { return true; }
- virtual bool realizeImplementation() { return true; }
- virtual bool isRealizedImplementation() const { return _canvas->IsShownOnScreen(); }
- virtual void closeImplementation() {}
- virtual bool releaseContextImplementation() { return true; }
- private:
- MyCanvas * _canvas;
- wxGLContext * _glContext;
- };
- MyGraphicsWindow::MyGraphicsWindow(MyCanvas * canvas)
- {
- _canvas = canvas;
- this->_traits = new GraphicsContext::Traits;
- _glContext = new wxGLContext(_canvas);
- wxPoint pos = _canvas->GetPosition();
- wxSize size = _canvas->GetSize();
- this->_traits->x = pos.x;
- this->_traits->y = pos.y;
- this->_traits->width = size.x;
- this->_traits->height = size.y;
- init();
- }
- MyGraphicsWindow::~MyGraphicsWindow()
- {
- delete _glContext;
- }
- void MyGraphicsWindow::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 MyGraphicsWindow::grabFocus()
- {
- _canvas->SetFocus();
- }
- void MyGraphicsWindow::grabFocusIfPointerInWindow()
- {
- wxPoint pos = wxGetMousePosition();
- if (wxFindWindowAtPoint(pos) == _canvas)
- _canvas->SetFocus();
- }
- void MyGraphicsWindow::useCursor(bool cursorOn)
- {
- _canvas->UseCursor(cursorOn);
- }
- bool MyGraphicsWindow::makeCurrentImplementation()
- {
- _canvas->SetCurrent(*_glContext);
-
- return true;
- }
- void MyGraphicsWindow::swapBuffersImplementation()
- {
- _canvas->SwapBuffers();
- }
- class MyFrame : public wxFrame
- {
- public:
- MyFrame(const wxString& title = wxT("我的窗口"));
- virtual ~MyFrame();
- private:
- DECLARE_EVENT_TABLE()
- void OnIdle(wxIdleEvent &event);
- public:
- osg::ref_ptr<osgViewer::Viewer> _viewer;
- MyCanvas * _canvas;
- MyGraphicsWindow * _myGraphicsWindow;
- osg::ref_ptr<osg::Node> loadedModel;
- };
- MyFrame::MyFrame(const wxString & title)
- :wxFrame(NULL, wxID_ANY, title, wxPoint(0, 0), wxSize(1024, 768))
- {
-
- _canvas = new MyCanvas(this);
- _myGraphicsWindow = new MyGraphicsWindow(_canvas);
- _canvas->SetGraphicsWindow(_myGraphicsWindow);
-
- _viewer = new osgViewer::Viewer;
- _viewer->getCamera()->setGraphicsContext(_myGraphicsWindow);
- _viewer->getCamera()->setViewport(0,0,1024,768);
- _viewer->addEventHandler(new osgViewer::StatsHandler);
- _viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
- loadedModel= osgDB::readNodeFile(std::string("cow.osg"));
-
- _viewer->setSceneData(loadedModel.get());
- _viewer->setCameraManipulator(new osgGA::TrackballManipulator);
- }
- MyFrame::~MyFrame()
- {
- }
- BEGIN_EVENT_TABLE(MyFrame, wxFrame)
- EVT_IDLE(MyFrame::OnIdle)
- END_EVENT_TABLE()
- void MyFrame::OnIdle(wxIdleEvent &event)
- {
- if (!_viewer->isRealized())
- return;
- _viewer->frame();
- event.RequestMore();
- }
- class MyApp : public wxApp
- {
- private:
- virtual bool OnInit()
- {
- MyFrame * f = new MyFrame;
- f->Show(true);
- return true;
- }
- };
- DECLARE_APP(MyApp)
- IMPLEMENT_APP(MyApp)
复制代码 wxWidgets-2.9.3对于OPENGL提供的接口略有改动,经过几天的研究终于弄好了。。。 |
|