root/OpenSceneGraph/trunk/examples/osgviewerWX/osgviewerWX.cpp @ 7307

Revision 7307, 6.3 kB (checked in by robert, 7 years ago)

From Brandon Hamm, changed WxWindows? example to use double buffering

Line 
1// For compilers that support precompilation, includes "wx.h".
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// `Main program' equivalent, creating windows and returning main app frame
22bool wxOsgApp::OnInit()
23{
24    // Create the main frame window
25    MainFrame *frame = new MainFrame(NULL, wxT("wxWidgets OSG Sample"),
26        wxDefaultPosition, wxDefaultSize);
27
28    // create osg canvas
29    //    - initialize
30   
31
32    int width = 800;
33    int height = 600;
34
35    int *attributes = new int[6];
36    attributes[0] = int(WX_GL_DOUBLEBUFFER);
37    attributes[1] = WX_GL_RGBA;
38    attributes[2] = WX_GL_DEPTH_SIZE;
39    attributes[3] = 8;
40    attributes[4] = WX_GL_STENCIL_SIZE;
41    attributes[5] = 8;
42
43    GraphicsWindowWX* gw = new GraphicsWindowWX(frame, wxID_ANY, wxDefaultPosition,
44                                                wxSize(width, height), wxSUNKEN_BORDER, wxT("osgviewerWX"), attributes);
45   
46    osgViewer::Viewer *viewer = new osgViewer::Viewer;
47    viewer->getCamera()->setGraphicsContext(gw);
48    viewer->getCamera()->setViewport(0,0,width,height);
49    viewer->addEventHandler(new osgViewer::StatsHandler);
50    viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
51
52    // load the scene.
53    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("cow.osg");
54    if (!loadedModel)
55    {
56        return false;
57    }
58
59    viewer->setSceneData(loadedModel.get());
60    viewer->setCameraManipulator(new osgGA::TrackballManipulator);
61
62    frame->SetViewer(viewer);
63
64    /* Show the frame */
65    frame->Show(true);
66
67    return true;
68}
69
70IMPLEMENT_APP(wxOsgApp)
71
72BEGIN_EVENT_TABLE(MainFrame, wxFrame)
73    EVT_IDLE(MainFrame::OnIdle)
74END_EVENT_TABLE()
75
76/* My frame constructor */
77MainFrame::MainFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
78    const wxSize& size, long style)
79    : wxFrame(frame, wxID_ANY, title, pos, size, style)
80{
81}
82
83void MainFrame::SetViewer(osgViewer::Viewer *viewer)
84{
85    _viewer = viewer;
86}
87
88void MainFrame::OnIdle(wxIdleEvent &event)
89{
90    _viewer->frame();
91
92    event.RequestMore();
93}
94
95BEGIN_EVENT_TABLE(GraphicsWindowWX, wxGLCanvas)
96    EVT_SIZE            (GraphicsWindowWX::OnSize            )
97    EVT_PAINT            (GraphicsWindowWX::OnPaint            )
98    EVT_ERASE_BACKGROUND(GraphicsWindowWX::OnEraseBackground)
99    EVT_KEY_DOWN        (GraphicsWindowWX::OnKeyDown        )
100    EVT_KEY_UP            (GraphicsWindowWX::OnKeyUp            )
101    EVT_MOUSE_EVENTS    (GraphicsWindowWX::OnMouse            )
102END_EVENT_TABLE()
103
104GraphicsWindowWX::GraphicsWindowWX(wxWindow *parent, wxWindowID id,
105    const wxPoint& pos, const wxSize& size, long style, const wxString& name, int *attributes)
106    : wxGLCanvas(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name, attributes)
107{
108    // default cursor to standard
109    _oldCursor = *wxSTANDARD_CURSOR;
110
111    _traits = new GraphicsContext::Traits;
112    _traits->x = pos.x;
113    _traits->y = pos.y;
114    _traits->width = size.x;
115    _traits->height = size.y;
116
117    init();
118
119}
120
121void GraphicsWindowWX::init()
122{
123    if (valid())
124    {
125        setState( new osg::State );
126        getState()->setGraphicsContext(this);
127
128        if (_traits.valid() && _traits->sharedContext)
129        {
130            getState()->setContextID( _traits->sharedContext->getState()->getContextID() );
131            incrementContextIDUsageCount( getState()->getContextID() );
132        }
133        else
134        {
135            getState()->setContextID( osg::GraphicsContext::createNewContextID() );
136        }
137    }
138}
139
140GraphicsWindowWX::~GraphicsWindowWX()
141{
142}
143
144void GraphicsWindowWX::OnPaint( wxPaintEvent& WXUNUSED(event) )
145{
146    /* must always be here */
147    wxPaintDC dc(this);
148}
149
150void GraphicsWindowWX::OnSize(wxSizeEvent& event)
151{
152    // this is also necessary to update the context on some platforms
153    wxGLCanvas::OnSize(event);
154
155    // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
156    int width, height;
157    GetClientSize(&width, &height);
158
159    // update the window dimensions, in case the window has been resized.
160    getEventQueue()->windowResize(0, 0, width, height);
161    resized(0,0,width,height);
162}
163
164void GraphicsWindowWX::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
165{
166    /* Do nothing, to avoid flashing on MSW */
167}
168
169void GraphicsWindowWX::OnKeyDown(wxKeyEvent &event)
170{
171#if wxUSE_UNICODE
172    int key = event.GetUnicodeKey();
173#else
174    int key = event.GetKeyCode();
175#endif
176    getEventQueue()->keyPress(key);
177
178    // propagate event
179    event.Skip();
180}
181
182void GraphicsWindowWX::OnKeyUp(wxKeyEvent &event)
183{
184#if wxUSE_UNICODE
185    int key = event.GetUnicodeKey();
186#else
187    int key = event.GetKeyCode();
188#endif
189    getEventQueue()->keyRelease(key);
190
191    // propagate event
192    event.Skip();
193}
194
195void GraphicsWindowWX::OnMouse(wxMouseEvent& event)
196{
197    if (event.ButtonDown()) {
198        int button = event.GetButton();
199        getEventQueue()->mouseButtonPress(event.GetX(), event.GetY(), button);
200    }
201    else if (event.ButtonUp()) {
202        int button = event.GetButton();
203        getEventQueue()->mouseButtonRelease(event.GetX(), event.GetY(), button);
204    }
205    else if (event.Dragging()) {
206        getEventQueue()->mouseMotion(event.GetX(), event.GetY());
207    }
208}
209
210void GraphicsWindowWX::grabFocus()
211{
212    // focus this window
213    SetFocus();
214}
215
216void GraphicsWindowWX::grabFocusIfPointerInWindow()
217{
218    // focus this window, if the pointer is in the window
219    wxPoint pos = wxGetMousePosition();
220    if (this == wxFindWindowAtPoint(pos)) {
221        SetFocus();
222    }
223}
224
225void GraphicsWindowWX::useCursor(bool cursorOn)
226{
227    if (cursorOn) {
228
229        // show the old cursor
230        SetCursor(_oldCursor);
231    }
232    else {
233
234        // remember the old cursor
235        _oldCursor = GetCursor();
236
237        // hide the cursor
238        //    - can't find a way to do this neatly, so create a 1x1, transparent image
239        wxImage image(1,1);
240        image.SetMask(true);
241        image.SetMaskColour(0, 0, 0);
242        wxCursor cursor(image);
243        SetCursor(cursor);
244    }
245}
246
247bool GraphicsWindowWX::makeCurrentImplementation()
248{
249    SetCurrent();
250    return true;
251}
252
253void GraphicsWindowWX::swapBuffersImplementation()
254{
255    SwapBuffers();
256}
Note: See TracBrowser for help on using the browser.