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

Revision 7950, 6.5 kB (checked in by robert, 6 years ago)

Added iostream include

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#include <iostream>
22
23// `Main program' equivalent, creating windows and returning main app frame
24bool 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    // Create the main frame window
36
37    MainFrame *frame = new MainFrame(NULL, wxT("wxWidgets OSG Sample"),
38        wxDefaultPosition, wxSize(width, height));
39
40    // create osg canvas
41    //    - initialize
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    // load the scene.
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    /* Show the frame */
75    frame->Show(true);
76
77    return true;
78}
79
80IMPLEMENT_APP(wxOsgApp)
81
82BEGIN_EVENT_TABLE(MainFrame, wxFrame)
83    EVT_IDLE(MainFrame::OnIdle)
84END_EVENT_TABLE()
85
86/* My frame constructor */
87MainFrame::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
93void MainFrame::SetViewer(osgViewer::Viewer *viewer)
94{
95    _viewer = viewer;
96}
97
98void MainFrame::OnIdle(wxIdleEvent &event)
99{
100    _viewer->frame();
101
102    event.RequestMore();
103}
104
105BEGIN_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            )
112END_EVENT_TABLE()
113
114GraphicsWindowWX::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    // default cursor to standard
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
131void 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
150GraphicsWindowWX::~GraphicsWindowWX()
151{
152}
153
154void GraphicsWindowWX::OnPaint( wxPaintEvent& WXUNUSED(event) )
155{
156    /* must always be here */
157    wxPaintDC dc(this);
158}
159
160void GraphicsWindowWX::OnSize(wxSizeEvent& event)
161{
162    // this is also necessary to update the context on some platforms
163    wxGLCanvas::OnSize(event);
164
165    // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
166    int width, height;
167    GetClientSize(&width, &height);
168
169    // update the window dimensions, in case the window has been resized.
170    getEventQueue()->windowResize(0, 0, width, height);
171    resized(0,0,width,height);
172}
173
174void GraphicsWindowWX::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
175{
176    /* Do nothing, to avoid flashing on MSW */
177}
178
179void 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    // propagate event
189    event.Skip();
190}
191
192void 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    // propagate event
202    event.Skip();
203}
204
205void 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
220void GraphicsWindowWX::grabFocus()
221{
222    // focus this window
223    SetFocus();
224}
225
226void GraphicsWindowWX::grabFocusIfPointerInWindow()
227{
228    // focus this window, if the pointer is in the window
229    wxPoint pos = wxGetMousePosition();
230    if (this == wxFindWindowAtPoint(pos)) {
231        SetFocus();
232    }
233}
234
235void GraphicsWindowWX::useCursor(bool cursorOn)
236{
237    if (cursorOn) {
238
239        // show the old cursor
240        SetCursor(_oldCursor);
241    }
242    else {
243
244        // remember the old cursor
245        _oldCursor = GetCursor();
246
247        // hide the cursor
248        //    - can't find a way to do this neatly, so create a 1x1, transparent image
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
257bool GraphicsWindowWX::makeCurrentImplementation()
258{
259    SetCurrent();
260    return true;
261}
262
263void GraphicsWindowWX::swapBuffersImplementation()
264{
265    SwapBuffers();
266}
Note: See TracBrowser for help on using the browser.