2007-05-11 17:07:00 +08:00
|
|
|
// 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
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
// For wxCURSOR_BLANK below, but isn't used a.t.m.
|
|
|
|
//#ifdef WIN32
|
|
|
|
//#include "wx/msw/wx.rc"
|
|
|
|
//#endif
|
|
|
|
|
2007-06-02 06:41:11 +08:00
|
|
|
#include "osgviewerWX.h"
|
2007-06-02 23:31:21 +08:00
|
|
|
|
|
|
|
|
2007-06-07 00:23:20 +08:00
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
2007-05-11 17:07:00 +08:00
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <wx/image.h>
|
|
|
|
#include <wx/menu.h>
|
|
|
|
|
2008-03-14 22:35:15 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2007-05-11 17:07:00 +08:00
|
|
|
// `Main program' equivalent, creating windows and returning main app frame
|
|
|
|
bool wxOsgApp::OnInit()
|
|
|
|
{
|
2008-03-14 00:22:07 +08:00
|
|
|
if (argc<2)
|
|
|
|
{
|
|
|
|
std::cout << wxString(argv[0]).mb_str() <<": requires filename argument." << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int width = 800;
|
|
|
|
int height = 600;
|
|
|
|
|
2007-05-11 17:07:00 +08:00
|
|
|
// Create the main frame window
|
2008-03-14 00:22:07 +08:00
|
|
|
|
2007-05-21 20:28:25 +08:00
|
|
|
MainFrame *frame = new MainFrame(NULL, wxT("wxWidgets OSG Sample"),
|
2008-03-14 00:22:07 +08:00
|
|
|
wxDefaultPosition, wxSize(width, height));
|
2007-05-11 17:07:00 +08:00
|
|
|
|
2007-05-21 20:28:25 +08:00
|
|
|
// create osg canvas
|
|
|
|
// - initialize
|
2007-06-02 23:31:21 +08:00
|
|
|
|
2011-09-14 18:45:11 +08:00
|
|
|
int attributes[7];
|
2007-08-27 21:33:58 +08:00
|
|
|
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;
|
2008-06-19 21:49:36 +08:00
|
|
|
attributes[6] = 0;
|
|
|
|
|
|
|
|
OSGCanvas *canvas = new OSGCanvas(frame, wxID_ANY, wxDefaultPosition,
|
|
|
|
wxSize(width, height), wxSUNKEN_BORDER, wxT("osgviewerWX"), attributes);
|
2007-08-24 17:42:11 +08:00
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
GraphicsWindowWX* gw = new GraphicsWindowWX(canvas);
|
|
|
|
|
|
|
|
canvas->SetGraphicsWindow(gw);
|
2008-03-14 00:22:07 +08:00
|
|
|
|
2007-06-02 23:31:21 +08:00
|
|
|
osgViewer::Viewer *viewer = new osgViewer::Viewer;
|
|
|
|
viewer->getCamera()->setGraphicsContext(gw);
|
|
|
|
viewer->getCamera()->setViewport(0,0,width,height);
|
2018-03-07 00:54:57 +08:00
|
|
|
|
|
|
|
// set the draw and read buffers up for a double buffered window with rendering going to back buffer
|
|
|
|
viewer.getCamera()->setDrawBuffer(GL_BACK);
|
|
|
|
viewer.getCamera()->setReadBuffer(GL_BACK);
|
|
|
|
|
2007-06-02 23:31:21 +08:00
|
|
|
viewer->addEventHandler(new osgViewer::StatsHandler);
|
|
|
|
viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
|
2007-08-24 17:42:11 +08:00
|
|
|
|
2007-05-21 20:28:25 +08:00
|
|
|
// load the scene.
|
2008-03-14 00:22:07 +08:00
|
|
|
wxString fname(argv[1]);
|
2017-11-27 19:10:48 +08:00
|
|
|
osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFile(std::string(fname.mb_str()));
|
2007-05-21 20:28:25 +08:00
|
|
|
if (!loadedModel)
|
|
|
|
{
|
2008-03-14 00:22:07 +08:00
|
|
|
std::cout << argv[0] <<": No data loaded." << std::endl;
|
2007-05-21 20:28:25 +08:00
|
|
|
return false;
|
|
|
|
}
|
2007-08-24 17:42:11 +08:00
|
|
|
|
2007-06-02 23:31:21 +08:00
|
|
|
viewer->setSceneData(loadedModel.get());
|
|
|
|
viewer->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
frame->SetViewer(viewer);
|
2007-05-11 17:07:00 +08:00
|
|
|
|
|
|
|
/* Show the frame */
|
2007-05-21 20:28:25 +08:00
|
|
|
frame->Show(true);
|
2007-05-11 17:07:00 +08:00
|
|
|
|
2007-05-21 20:28:25 +08:00
|
|
|
return true;
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IMPLEMENT_APP(wxOsgApp)
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MainFrame, wxFrame)
|
2007-05-21 20:28:25 +08:00
|
|
|
EVT_IDLE(MainFrame::OnIdle)
|
2007-05-11 17:07:00 +08:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
/* My frame constructor */
|
|
|
|
MainFrame::MainFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
|
2007-05-21 20:28:25 +08:00
|
|
|
const wxSize& size, long style)
|
|
|
|
: wxFrame(frame, wxID_ANY, title, pos, size, style)
|
2007-05-11 17:07:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-06-02 23:31:21 +08:00
|
|
|
void MainFrame::SetViewer(osgViewer::Viewer *viewer)
|
2007-05-11 17:07:00 +08:00
|
|
|
{
|
2007-06-02 23:31:21 +08:00
|
|
|
_viewer = viewer;
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainFrame::OnIdle(wxIdleEvent &event)
|
|
|
|
{
|
2010-12-13 18:46:18 +08:00
|
|
|
if (!_viewer->isRealized())
|
|
|
|
return;
|
|
|
|
|
2007-06-02 23:31:21 +08:00
|
|
|
_viewer->frame();
|
2007-08-24 17:42:11 +08:00
|
|
|
|
2007-05-21 20:28:25 +08:00
|
|
|
event.RequestMore();
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
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)
|
2009-11-19 19:21:05 +08:00
|
|
|
EVT_MOUSEWHEEL (OSGCanvas::OnMouseWheel)
|
2007-05-11 17:07:00 +08:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
OSGCanvas::OSGCanvas(wxWindow *parent, wxWindowID id,
|
2007-08-27 21:33:58 +08:00
|
|
|
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)
|
2007-05-11 17:07:00 +08:00
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
// default cursor to standard
|
|
|
|
_oldCursor = *wxSTANDARD_CURSOR;
|
2007-06-02 23:31:21 +08:00
|
|
|
}
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
OSGCanvas::~OSGCanvas()
|
2007-06-02 23:31:21 +08:00
|
|
|
{
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
void OSGCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
|
2007-05-11 17:07:00 +08:00
|
|
|
{
|
|
|
|
/* must always be here */
|
2007-05-21 20:28:25 +08:00
|
|
|
wxPaintDC dc(this);
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
void OSGCanvas::OnSize(wxSizeEvent& event)
|
2007-05-11 17:07:00 +08:00
|
|
|
{
|
|
|
|
// this is also necessary to update the context on some platforms
|
2007-05-21 20:28:25 +08:00
|
|
|
wxGLCanvas::OnSize(event);
|
2007-05-11 17:07:00 +08:00
|
|
|
|
|
|
|
// set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
|
2007-05-21 20:28:25 +08:00
|
|
|
int width, height;
|
|
|
|
GetClientSize(&width, &height);
|
2007-08-24 17:42:11 +08:00
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
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);
|
|
|
|
}
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
void OSGCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
|
2007-05-11 17:07:00 +08:00
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
/* Do nothing, to avoid flashing on MSW */
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
void OSGCanvas::OnChar(wxKeyEvent &event)
|
2007-05-11 17:07:00 +08:00
|
|
|
{
|
2007-08-24 17:42:11 +08:00
|
|
|
#if wxUSE_UNICODE
|
2007-06-02 23:31:21 +08:00
|
|
|
int key = event.GetUnicodeKey();
|
|
|
|
#else
|
2007-05-21 20:28:25 +08:00
|
|
|
int key = event.GetKeyCode();
|
2007-06-02 23:31:21 +08:00
|
|
|
#endif
|
2008-06-19 21:49:36 +08:00
|
|
|
|
|
|
|
if (_graphics_window.valid())
|
|
|
|
_graphics_window->getEventQueue()->keyPress(key);
|
2007-05-11 17:07:00 +08:00
|
|
|
|
2008-05-27 05:09:54 +08:00
|
|
|
// If this key event is not processed here, we should call
|
|
|
|
// event.Skip() to allow processing to continue.
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
void OSGCanvas::OnKeyUp(wxKeyEvent &event)
|
2007-05-11 17:07:00 +08:00
|
|
|
{
|
2007-08-24 17:42:11 +08:00
|
|
|
#if wxUSE_UNICODE
|
2007-06-02 23:31:21 +08:00
|
|
|
int key = event.GetUnicodeKey();
|
|
|
|
#else
|
2007-05-21 20:28:25 +08:00
|
|
|
int key = event.GetKeyCode();
|
2007-06-02 23:31:21 +08:00
|
|
|
#endif
|
2008-06-19 21:49:36 +08:00
|
|
|
|
|
|
|
if (_graphics_window.valid())
|
|
|
|
_graphics_window->getEventQueue()->keyRelease(key);
|
2007-05-11 17:07:00 +08:00
|
|
|
|
2008-05-27 05:09:54 +08:00
|
|
|
// If this key event is not processed here, we should call
|
|
|
|
// event.Skip() to allow processing to continue.
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
void OSGCanvas::OnMouseEnter(wxMouseEvent &event)
|
2007-05-11 17:07:00 +08:00
|
|
|
{
|
2008-05-27 05:09:54 +08:00
|
|
|
// Set focus to ourselves, so keyboard events get directed to us
|
|
|
|
SetFocus();
|
|
|
|
}
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
void OSGCanvas::OnMouseDown(wxMouseEvent &event)
|
2008-05-27 05:09:54 +08:00
|
|
|
{
|
2008-06-19 21:49:36 +08:00
|
|
|
if (_graphics_window.valid())
|
|
|
|
{
|
|
|
|
_graphics_window->getEventQueue()->mouseButtonPress(event.GetX(), event.GetY(),
|
|
|
|
event.GetButton());
|
|
|
|
}
|
2008-05-27 05:09:54 +08:00
|
|
|
}
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
void OSGCanvas::OnMouseUp(wxMouseEvent &event)
|
2007-05-11 17:07:00 +08:00
|
|
|
{
|
2008-06-19 21:49:36 +08:00
|
|
|
if (_graphics_window.valid())
|
|
|
|
{
|
|
|
|
_graphics_window->getEventQueue()->mouseButtonRelease(event.GetX(), event.GetY(),
|
|
|
|
event.GetButton());
|
|
|
|
}
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
void OSGCanvas::OnMouseMotion(wxMouseEvent &event)
|
2007-05-11 17:07:00 +08:00
|
|
|
{
|
2008-06-19 21:49:36 +08:00
|
|
|
if (_graphics_window.valid())
|
|
|
|
_graphics_window->getEventQueue()->mouseMotion(event.GetX(), event.GetY());
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
2009-11-19 19:21:05 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
void OSGCanvas::UseCursor(bool value)
|
2007-05-11 17:07:00 +08:00
|
|
|
{
|
2008-06-19 21:49:36 +08:00
|
|
|
if (value)
|
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
// show the old cursor
|
|
|
|
SetCursor(_oldCursor);
|
|
|
|
}
|
2008-06-19 21:49:36 +08:00
|
|
|
else
|
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
// remember the old cursor
|
|
|
|
_oldCursor = GetCursor();
|
2007-05-11 17:07:00 +08:00
|
|
|
|
2007-05-21 20:28:25 +08:00
|
|
|
// 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);
|
2008-06-19 21:49:36 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2012-09-06 05:03:41 +08:00
|
|
|
if (_traits.valid() && _traits->sharedContext.valid())
|
2008-06-19 21:49:36 +08:00
|
|
|
{
|
|
|
|
getState()->setContextID( _traits->sharedContext->getState()->getContextID() );
|
|
|
|
incrementContextIDUsageCount( getState()->getContextID() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
getState()->setContextID( osg::GraphicsContext::createNewContextID() );
|
|
|
|
}
|
2007-05-21 20:28:25 +08:00
|
|
|
}
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
2008-06-19 21:49:36 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-05-11 17:07:00 +08:00
|
|
|
bool GraphicsWindowWX::makeCurrentImplementation()
|
|
|
|
{
|
2008-06-19 21:49:36 +08:00
|
|
|
_canvas->SetCurrent();
|
2007-05-21 20:28:25 +08:00
|
|
|
return true;
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindowWX::swapBuffersImplementation()
|
|
|
|
{
|
2008-06-19 21:49:36 +08:00
|
|
|
_canvas->SwapBuffers();
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|