/usr/share/openscenegraph/examples/osgviewerWX/osgviewerWX.cpp is in openscenegraph-examples 3.2.0~rc1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | // For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
// For wxCURSOR_BLANK below, but isn't used a.t.m.
//#ifdef WIN32
//#include "wx/msw/wx.rc"
//#endif
#include "osgviewerWX.h"
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#include <wx/image.h>
#include <wx/menu.h>
#include <iostream>
// `Main program' equivalent, creating windows and returning main app frame
bool wxOsgApp::OnInit()
{
if (argc<2)
{
std::cout << wxString(argv[0]).mb_str() <<": requires filename argument." << std::endl;
return false;
}
int width = 800;
int height = 600;
// Create the main frame window
MainFrame *frame = new MainFrame(NULL, wxT("wxWidgets OSG Sample"),
wxDefaultPosition, wxSize(width, height));
// create osg canvas
// - initialize
int attributes[7];
attributes[0] = int(WX_GL_DOUBLEBUFFER);
attributes[1] = WX_GL_RGBA;
attributes[2] = WX_GL_DEPTH_SIZE;
attributes[3] = 8;
attributes[4] = WX_GL_STENCIL_SIZE;
attributes[5] = 8;
attributes[6] = 0;
OSGCanvas *canvas = new OSGCanvas(frame, wxID_ANY, wxDefaultPosition,
wxSize(width, height), wxSUNKEN_BORDER, wxT("osgviewerWX"), attributes);
GraphicsWindowWX* gw = new GraphicsWindowWX(canvas);
canvas->SetGraphicsWindow(gw);
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.
wxString fname(argv[1]);
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(std::string(fname.mb_str()));
if (!loadedModel)
{
std::cout << argv[0] <<": No data loaded." << std::endl;
return false;
}
viewer->setSceneData(loadedModel.get());
viewer->setCameraManipulator(new osgGA::TrackballManipulator);
frame->SetViewer(viewer);
/* Show the frame */
frame->Show(true);
return true;
}
IMPLEMENT_APP(wxOsgApp)
BEGIN_EVENT_TABLE(MainFrame, wxFrame)
EVT_IDLE(MainFrame::OnIdle)
END_EVENT_TABLE()
/* My frame constructor */
MainFrame::MainFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
const wxSize& size, long style)
: wxFrame(frame, wxID_ANY, title, pos, size, style)
{
}
void MainFrame::SetViewer(osgViewer::Viewer *viewer)
{
_viewer = viewer;
}
void MainFrame::OnIdle(wxIdleEvent &event)
{
if (!_viewer->isRealized())
return;
_viewer->frame();
event.RequestMore();
}
BEGIN_EVENT_TABLE(OSGCanvas, wxGLCanvas)
EVT_SIZE (OSGCanvas::OnSize)
EVT_PAINT (OSGCanvas::OnPaint)
EVT_ERASE_BACKGROUND (OSGCanvas::OnEraseBackground)
EVT_CHAR (OSGCanvas::OnChar)
EVT_KEY_UP (OSGCanvas::OnKeyUp)
EVT_ENTER_WINDOW (OSGCanvas::OnMouseEnter)
EVT_LEFT_DOWN (OSGCanvas::OnMouseDown)
EVT_MIDDLE_DOWN (OSGCanvas::OnMouseDown)
EVT_RIGHT_DOWN (OSGCanvas::OnMouseDown)
EVT_LEFT_UP (OSGCanvas::OnMouseUp)
EVT_MIDDLE_UP (OSGCanvas::OnMouseUp)
EVT_RIGHT_UP (OSGCanvas::OnMouseUp)
EVT_MOTION (OSGCanvas::OnMouseMotion)
EVT_MOUSEWHEEL (OSGCanvas::OnMouseWheel)
END_EVENT_TABLE()
OSGCanvas::OSGCanvas(wxWindow *parent, wxWindowID id,
const wxPoint& pos, const wxSize& size, long style, const wxString& name, int *attributes)
: wxGLCanvas(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name, attributes)
{
// default cursor to standard
_oldCursor = *wxSTANDARD_CURSOR;
}
OSGCanvas::~OSGCanvas()
{
}
void OSGCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
{
/* must always be here */
wxPaintDC dc(this);
}
void OSGCanvas::OnSize(wxSizeEvent& event)
{
// this is also necessary to update the context on some platforms
wxGLCanvas::OnSize(event);
// set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
int width, height;
GetClientSize(&width, &height);
if (_graphics_window.valid())
{
// update the window dimensions, in case the window has been resized.
_graphics_window->getEventQueue()->windowResize(0, 0, width, height);
_graphics_window->resized(0,0,width,height);
}
}
void OSGCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
{
/* Do nothing, to avoid flashing on MSW */
}
void OSGCanvas::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);
// If this key event is not processed here, we should call
// event.Skip() to allow processing to continue.
}
void OSGCanvas::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);
// If this key event is not processed here, we should call
// event.Skip() to allow processing to continue.
}
void OSGCanvas::OnMouseEnter(wxMouseEvent &event)
{
// Set focus to ourselves, so keyboard events get directed to us
SetFocus();
}
void OSGCanvas::OnMouseDown(wxMouseEvent &event)
{
if (_graphics_window.valid())
{
_graphics_window->getEventQueue()->mouseButtonPress(event.GetX(), event.GetY(),
event.GetButton());
}
}
void OSGCanvas::OnMouseUp(wxMouseEvent &event)
{
if (_graphics_window.valid())
{
_graphics_window->getEventQueue()->mouseButtonRelease(event.GetX(), event.GetY(),
event.GetButton());
}
}
void OSGCanvas::OnMouseMotion(wxMouseEvent &event)
{
if (_graphics_window.valid())
_graphics_window->getEventQueue()->mouseMotion(event.GetX(), event.GetY());
}
void OSGCanvas::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 OSGCanvas::UseCursor(bool value)
{
if (value)
{
// show the old cursor
SetCursor(_oldCursor);
}
else
{
// remember the old cursor
_oldCursor = GetCursor();
// hide the cursor
// - can't find a way to do this neatly, so create a 1x1, transparent image
wxImage image(1,1);
image.SetMask(true);
image.SetMaskColour(0, 0, 0);
wxCursor cursor(image);
SetCursor(cursor);
// On wxGTK, only works as of version 2.7.0
// (http://trac.wxwidgets.org/ticket/2946)
// SetCursor( wxStockCursor( wxCURSOR_BLANK ) );
}
}
GraphicsWindowWX::GraphicsWindowWX(OSGCanvas *canvas)
{
_canvas = canvas;
_traits = new GraphicsContext::Traits;
wxPoint pos = _canvas->GetPosition();
wxSize size = _canvas->GetSize();
_traits->x = pos.x;
_traits->y = pos.y;
_traits->width = size.x;
_traits->height = size.y;
init();
}
GraphicsWindowWX::~GraphicsWindowWX()
{
}
void GraphicsWindowWX::init()
{
if (valid())
{
setState( new osg::State );
getState()->setGraphicsContext(this);
if (_traits.valid() && _traits->sharedContext.valid())
{
getState()->setContextID( _traits->sharedContext->getState()->getContextID() );
incrementContextIDUsageCount( getState()->getContextID() );
}
else
{
getState()->setContextID( osg::GraphicsContext::createNewContextID() );
}
}
}
void GraphicsWindowWX::grabFocus()
{
// focus the canvas
_canvas->SetFocus();
}
void GraphicsWindowWX::grabFocusIfPointerInWindow()
{
// focus this window, if the pointer is in the window
wxPoint pos = wxGetMousePosition();
if (wxFindWindowAtPoint(pos) == _canvas)
_canvas->SetFocus();
}
void GraphicsWindowWX::useCursor(bool cursorOn)
{
_canvas->UseCursor(cursorOn);
}
bool GraphicsWindowWX::makeCurrentImplementation()
{
_canvas->SetCurrent();
return true;
}
void GraphicsWindowWX::swapBuffersImplementation()
{
_canvas->SwapBuffers();
}
|