| 1 | |
|---|
| 2 | #include "wx/wxprec.h" |
|---|
| 3 | |
|---|
| 4 | #ifdef __BORLANDC__ |
|---|
| 5 | #pragma hdrstop |
|---|
| 6 | #endif |
|---|
| 7 | |
|---|
| 8 | #ifndef WX_PRECOMP |
|---|
| 9 | #include "wx/wx.h" |
|---|
| 10 | #endif |
|---|
| 11 | |
|---|
| 12 | #include "osgviewerWX.h" |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 16 | #include <osgGA/TrackballManipulator> |
|---|
| 17 | #include <osgDB/ReadFile> |
|---|
| 18 | #include <wx/image.h> |
|---|
| 19 | #include <wx/menu.h> |
|---|
| 20 | |
|---|
| 21 | #include <iostream> |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | bool wxOsgApp::OnInit() |
|---|
| 25 | { |
|---|
| 26 | if (argc<2) |
|---|
| 27 | { |
|---|
| 28 | std::cout << wxString(argv[0]).mb_str() <<": requires filename argument." << std::endl; |
|---|
| 29 | return false; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | int width = 800; |
|---|
| 33 | int height = 600; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | MainFrame *frame = new MainFrame(NULL, wxT("wxWidgets OSG Sample"), |
|---|
| 38 | wxDefaultPosition, wxSize(width, height)); |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | int *attributes = new int[6]; |
|---|
| 44 | attributes[0] = int(WX_GL_DOUBLEBUFFER); |
|---|
| 45 | attributes[1] = WX_GL_RGBA; |
|---|
| 46 | attributes[2] = WX_GL_DEPTH_SIZE; |
|---|
| 47 | attributes[3] = 8; |
|---|
| 48 | attributes[4] = WX_GL_STENCIL_SIZE; |
|---|
| 49 | attributes[5] = 8; |
|---|
| 50 | |
|---|
| 51 | GraphicsWindowWX* gw = new GraphicsWindowWX(frame, wxID_ANY, wxDefaultPosition, |
|---|
| 52 | wxSize(width, height), wxSUNKEN_BORDER, wxT("osgviewerWX"), attributes); |
|---|
| 53 | |
|---|
| 54 | osgViewer::Viewer *viewer = new osgViewer::Viewer; |
|---|
| 55 | viewer->getCamera()->setGraphicsContext(gw); |
|---|
| 56 | viewer->getCamera()->setViewport(0,0,width,height); |
|---|
| 57 | viewer->addEventHandler(new osgViewer::StatsHandler); |
|---|
| 58 | viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | wxString fname(argv[1]); |
|---|
| 62 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(std::string(fname.mb_str())); |
|---|
| 63 | if (!loadedModel) |
|---|
| 64 | { |
|---|
| 65 | std::cout << argv[0] <<": No data loaded." << std::endl; |
|---|
| 66 | return false; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | viewer->setSceneData(loadedModel.get()); |
|---|
| 70 | viewer->setCameraManipulator(new osgGA::TrackballManipulator); |
|---|
| 71 | |
|---|
| 72 | frame->SetViewer(viewer); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | frame->Show(true); |
|---|
| 76 | |
|---|
| 77 | return true; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | IMPLEMENT_APP(wxOsgApp) |
|---|
| 81 | |
|---|
| 82 | BEGIN_EVENT_TABLE(MainFrame, wxFrame) |
|---|
| 83 | EVT_IDLE(MainFrame::OnIdle) |
|---|
| 84 | END_EVENT_TABLE() |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | MainFrame::MainFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, |
|---|
| 88 | const wxSize& size, long style) |
|---|
| 89 | : wxFrame(frame, wxID_ANY, title, pos, size, style) |
|---|
| 90 | { |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | void MainFrame::SetViewer(osgViewer::Viewer *viewer) |
|---|
| 94 | { |
|---|
| 95 | _viewer = viewer; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | void MainFrame::OnIdle(wxIdleEvent &event) |
|---|
| 99 | { |
|---|
| 100 | _viewer->frame(); |
|---|
| 101 | |
|---|
| 102 | event.RequestMore(); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | BEGIN_EVENT_TABLE(GraphicsWindowWX, wxGLCanvas) |
|---|
| 106 | EVT_SIZE (GraphicsWindowWX::OnSize ) |
|---|
| 107 | EVT_PAINT (GraphicsWindowWX::OnPaint ) |
|---|
| 108 | EVT_ERASE_BACKGROUND(GraphicsWindowWX::OnEraseBackground) |
|---|
| 109 | EVT_KEY_DOWN (GraphicsWindowWX::OnKeyDown ) |
|---|
| 110 | EVT_KEY_UP (GraphicsWindowWX::OnKeyUp ) |
|---|
| 111 | EVT_MOUSE_EVENTS (GraphicsWindowWX::OnMouse ) |
|---|
| 112 | END_EVENT_TABLE() |
|---|
| 113 | |
|---|
| 114 | GraphicsWindowWX::GraphicsWindowWX(wxWindow *parent, wxWindowID id, |
|---|
| 115 | const wxPoint& pos, const wxSize& size, long style, const wxString& name, int *attributes) |
|---|
| 116 | : wxGLCanvas(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name, attributes) |
|---|
| 117 | { |
|---|
| 118 | |
|---|
| 119 | _oldCursor = *wxSTANDARD_CURSOR; |
|---|
| 120 | |
|---|
| 121 | _traits = new GraphicsContext::Traits; |
|---|
| 122 | _traits->x = pos.x; |
|---|
| 123 | _traits->y = pos.y; |
|---|
| 124 | _traits->width = size.x; |
|---|
| 125 | _traits->height = size.y; |
|---|
| 126 | |
|---|
| 127 | init(); |
|---|
| 128 | |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | void GraphicsWindowWX::init() |
|---|
| 132 | { |
|---|
| 133 | if (valid()) |
|---|
| 134 | { |
|---|
| 135 | setState( new osg::State ); |
|---|
| 136 | getState()->setGraphicsContext(this); |
|---|
| 137 | |
|---|
| 138 | if (_traits.valid() && _traits->sharedContext) |
|---|
| 139 | { |
|---|
| 140 | getState()->setContextID( _traits->sharedContext->getState()->getContextID() ); |
|---|
| 141 | incrementContextIDUsageCount( getState()->getContextID() ); |
|---|
| 142 | } |
|---|
| 143 | else |
|---|
| 144 | { |
|---|
| 145 | getState()->setContextID( osg::GraphicsContext::createNewContextID() ); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | GraphicsWindowWX::~GraphicsWindowWX() |
|---|
| 151 | { |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | void GraphicsWindowWX::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
|---|
| 155 | { |
|---|
| 156 | |
|---|
| 157 | wxPaintDC dc(this); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | void GraphicsWindowWX::OnSize(wxSizeEvent& event) |
|---|
| 161 | { |
|---|
| 162 | |
|---|
| 163 | wxGLCanvas::OnSize(event); |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | int width, height; |
|---|
| 167 | GetClientSize(&width, &height); |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | getEventQueue()->windowResize(0, 0, width, height); |
|---|
| 171 | resized(0,0,width,height); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | void GraphicsWindowWX::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) |
|---|
| 175 | { |
|---|
| 176 | |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | void GraphicsWindowWX::OnKeyDown(wxKeyEvent &event) |
|---|
| 180 | { |
|---|
| 181 | #if wxUSE_UNICODE |
|---|
| 182 | int key = event.GetUnicodeKey(); |
|---|
| 183 | #else |
|---|
| 184 | int key = event.GetKeyCode(); |
|---|
| 185 | #endif |
|---|
| 186 | getEventQueue()->keyPress(key); |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | event.Skip(); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | void GraphicsWindowWX::OnKeyUp(wxKeyEvent &event) |
|---|
| 193 | { |
|---|
| 194 | #if wxUSE_UNICODE |
|---|
| 195 | int key = event.GetUnicodeKey(); |
|---|
| 196 | #else |
|---|
| 197 | int key = event.GetKeyCode(); |
|---|
| 198 | #endif |
|---|
| 199 | getEventQueue()->keyRelease(key); |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | event.Skip(); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | void GraphicsWindowWX::OnMouse(wxMouseEvent& event) |
|---|
| 206 | { |
|---|
| 207 | if (event.ButtonDown()) { |
|---|
| 208 | int button = event.GetButton(); |
|---|
| 209 | getEventQueue()->mouseButtonPress(event.GetX(), event.GetY(), button); |
|---|
| 210 | } |
|---|
| 211 | else if (event.ButtonUp()) { |
|---|
| 212 | int button = event.GetButton(); |
|---|
| 213 | getEventQueue()->mouseButtonRelease(event.GetX(), event.GetY(), button); |
|---|
| 214 | } |
|---|
| 215 | else if (event.Dragging()) { |
|---|
| 216 | getEventQueue()->mouseMotion(event.GetX(), event.GetY()); |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | void GraphicsWindowWX::grabFocus() |
|---|
| 221 | { |
|---|
| 222 | |
|---|
| 223 | SetFocus(); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | void GraphicsWindowWX::grabFocusIfPointerInWindow() |
|---|
| 227 | { |
|---|
| 228 | |
|---|
| 229 | wxPoint pos = wxGetMousePosition(); |
|---|
| 230 | if (this == wxFindWindowAtPoint(pos)) { |
|---|
| 231 | SetFocus(); |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | void GraphicsWindowWX::useCursor(bool cursorOn) |
|---|
| 236 | { |
|---|
| 237 | if (cursorOn) { |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | SetCursor(_oldCursor); |
|---|
| 241 | } |
|---|
| 242 | else { |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | _oldCursor = GetCursor(); |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | wxImage image(1,1); |
|---|
| 250 | image.SetMask(true); |
|---|
| 251 | image.SetMaskColour(0, 0, 0); |
|---|
| 252 | wxCursor cursor(image); |
|---|
| 253 | SetCursor(cursor); |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | bool GraphicsWindowWX::makeCurrentImplementation() |
|---|
| 258 | { |
|---|
| 259 | SetCurrent(); |
|---|
| 260 | return true; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | void GraphicsWindowWX::swapBuffersImplementation() |
|---|
| 264 | { |
|---|
| 265 | SwapBuffers(); |
|---|
| 266 | } |
|---|