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
|
|
|
|
|
2007-05-11 20:46:38 +08:00
|
|
|
#include "osgsimpleviewerWX.h"
|
2007-05-11 17:07:00 +08:00
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <wx/image.h>
|
|
|
|
#include <wx/menu.h>
|
|
|
|
|
|
|
|
// `Main program' equivalent, creating windows and returning main app frame
|
|
|
|
bool wxOsgApp::OnInit()
|
|
|
|
{
|
|
|
|
// Create the main frame window
|
2007-05-21 20:28:25 +08:00
|
|
|
MainFrame *frame = new MainFrame(NULL, wxT("wxWidgets OSG Sample"),
|
|
|
|
wxDefaultPosition, wxDefaultSize);
|
2007-05-11 17:07:00 +08:00
|
|
|
|
2007-05-21 20:28:25 +08:00
|
|
|
// create osg canvas
|
|
|
|
// - initialize
|
|
|
|
SimpleViewerWX *viewerWindow = new SimpleViewerWX(frame, wxID_ANY, wxDefaultPosition,
|
2007-05-11 17:07:00 +08:00
|
|
|
wxSize(200, 200), wxSUNKEN_BORDER);
|
2007-05-21 20:28:25 +08:00
|
|
|
|
|
|
|
// load the scene.
|
2007-05-21 20:30:14 +08:00
|
|
|
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
|
2007-05-21 20:28:25 +08:00
|
|
|
if (!loadedModel)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
viewerWindow->setSceneData(loadedModel.get());
|
|
|
|
viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
|
|
|
|
frame->SetSimpleViewer(viewerWindow);
|
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-05-21 20:28:25 +08:00
|
|
|
_viewerWindow = NULL;
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainFrame::SetSimpleViewer(SimpleViewerWX *viewer)
|
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
_viewerWindow = viewer;
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainFrame::OnIdle(wxIdleEvent &event)
|
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
_viewerWindow->render();
|
|
|
|
event.RequestMore();
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(GraphicsWindowWX, wxGLCanvas)
|
2007-05-21 20:28:25 +08:00
|
|
|
EVT_SIZE (GraphicsWindowWX::OnSize )
|
|
|
|
EVT_PAINT (GraphicsWindowWX::OnPaint )
|
|
|
|
EVT_ERASE_BACKGROUND(GraphicsWindowWX::OnEraseBackground)
|
|
|
|
EVT_KEY_DOWN (GraphicsWindowWX::OnKeyDown )
|
|
|
|
EVT_KEY_UP (GraphicsWindowWX::OnKeyUp )
|
|
|
|
EVT_MOUSE_EVENTS (GraphicsWindowWX::OnMouse )
|
2007-05-11 17:07:00 +08:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
GraphicsWindowWX::GraphicsWindowWX(wxWindow *parent, wxWindowID id,
|
2007-05-21 20:28:25 +08:00
|
|
|
const wxPoint& pos, const wxSize& size, long style, const wxString& name)
|
|
|
|
: wxGLCanvas(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name)
|
2007-05-11 17:07:00 +08:00
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
// default cursor to standard
|
|
|
|
_oldCursor = *wxSTANDARD_CURSOR;
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsWindowWX::~GraphicsWindowWX()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindowWX::OnPaint( wxPaintEvent& WXUNUSED(event) )
|
|
|
|
{
|
|
|
|
/* must always be here */
|
2007-05-21 20:28:25 +08:00
|
|
|
wxPaintDC dc(this);
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindowWX::OnSize(wxSizeEvent& event)
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// update the window dimensions, in case the window has been resized.
|
|
|
|
getEventQueue()->windowResize(0, 0, width, height);
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindowWX::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
|
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
/* Do nothing, to avoid flashing on MSW */
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindowWX::OnKeyDown(wxKeyEvent &event)
|
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
int key = event.GetKeyCode();
|
|
|
|
getEventQueue()->keyPress(key);
|
2007-05-11 17:07:00 +08:00
|
|
|
|
2007-05-21 20:28:25 +08:00
|
|
|
// propagate event
|
|
|
|
event.Skip();
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindowWX::OnKeyUp(wxKeyEvent &event)
|
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
int key = event.GetKeyCode();
|
|
|
|
getEventQueue()->keyRelease(key);
|
2007-05-11 17:07:00 +08:00
|
|
|
|
2007-05-21 20:28:25 +08:00
|
|
|
// propagate event
|
|
|
|
event.Skip();
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindowWX::OnMouse(wxMouseEvent& event)
|
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
if (event.ButtonDown()) {
|
|
|
|
int button = event.GetButton();
|
|
|
|
getEventQueue()->mouseButtonPress(event.GetX(), event.GetY(), button);
|
|
|
|
}
|
|
|
|
else if (event.ButtonUp()) {
|
|
|
|
int button = event.GetButton();
|
|
|
|
getEventQueue()->mouseButtonRelease(event.GetX(), event.GetY(), button);
|
|
|
|
}
|
|
|
|
else if (event.Dragging()) {
|
|
|
|
getEventQueue()->mouseMotion(event.GetX(), event.GetY());
|
|
|
|
}
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindowWX::grabFocus()
|
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
// focus this window
|
|
|
|
SetFocus();
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindowWX::grabFocusIfPointerInWindow()
|
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
// focus this window, if the pointer is in the window
|
|
|
|
wxPoint pos = wxGetMousePosition();
|
|
|
|
if (this == wxFindWindowAtPoint(pos)) {
|
|
|
|
SetFocus();
|
|
|
|
}
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindowWX::useCursor(bool cursorOn)
|
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
if (cursorOn) {
|
2007-05-11 17:07:00 +08:00
|
|
|
|
2007-05-21 20:28:25 +08:00
|
|
|
// show the old cursor
|
|
|
|
SetCursor(_oldCursor);
|
|
|
|
}
|
|
|
|
else {
|
2007-05-11 17:07:00 +08:00
|
|
|
|
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);
|
|
|
|
}
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GraphicsWindowWX::makeCurrentImplementation()
|
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
SetCurrent();
|
|
|
|
return true;
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindowWX::swapBuffersImplementation()
|
|
|
|
{
|
2007-05-21 20:28:25 +08:00
|
|
|
SwapBuffers();
|
2007-05-11 17:07:00 +08:00
|
|
|
}
|