Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleContextWx.cpp

Example for usage ContextWX, GLProjections and ThreeDOutscenes

, GLProjectionParametersPerspective, SceneThreeDOutWrapper

Author
MIP, sedlazeck 04/2012
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
@example ExampleContextWx.cpp
@relates ContextWX, GLProjectionParametersPerspective, SceneOpenSceneGraph
@brief Example for usage ContextWX, GLProjections and Scenes
@ingroup g_examples
@ingroup g_glviewer
@author MIP, ischiller 02/2010
*/
#include <Base/Common/BIASpragmaStart.hh>
#include <wx/wx.h>
#include <wx/aboutdlg.h>
#include <wx/filename.h>
#include <wx/artprov.h>
#include <Base/Common/BIASpragmaEnd.hh>
#include <Base/Image/Image.hh>
#include <Base/Image/ImageIO.hh>
#include <Base/Common/FileHandling.hh>
#include <Gui/StringConv.hh>
#include <GLviewer/ContextWX.hh>
#include <GLviewer/Scenes/SceneBGImage.hh>
#ifdef BIAS_HAVE_OPENSCENEGRAPH
#include <GLviewer/Scenes/SceneOpenSceneGraph.hh>
#endif
#include <GLviewer/GLProjectionParametersPerspective.hh>
#include <GLviewer/Controller/DistanceMeasureControl.hh>
using namespace BIAS;
using namespace std;
/////////////////////////////////////
////////// the window //////////
/////////////////////////////////////
/**
* \cond HIDDEN_SYMBOLS
*/
class CWxFrame : public wxFrame
{
public:
CWxFrame(wxWindow *parent, const wxString& title);
void OnOpen(wxCommandEvent &event);
void OnQuit(wxCommandEvent &event);
void OnScreenshot(wxCommandEvent& event);
void OnViewSize(wxCommandEvent& event);
void OnAbout(wxCommandEvent &event);
void OnScreenInfo(wxCommandEvent &event);
void OnResize(wxSizeEvent &event);
void LateInit();
protected:
ContextWX* DummyContext_;
ContextWX* Context_;
#ifdef BIAS_HAVE_OPENSCENEGRAPH
#endif
SceneBGImage BGImage_;
bool SizeLocked_;
enum {
ID_Whatever = wxID_HIGHEST + 42,
ID_Screenshot,
ID_VS_Free,
ID_VS_640,
ID_VS_800,
ID_VS_16_9,
ID_VS_16_10,
ID_ScreenInfo
};
private:
void DumpSizes_();
void FitToImageSize_();
wxMenuBar* Menubar_;
wxToolBar* Toolbar_;
wxStatusBar* Statusbar_;
DECLARE_EVENT_TABLE()
};
/** \endcond */
BEGIN_EVENT_TABLE(CWxFrame, wxFrame)
EVT_MENU(wxID_OPEN, CWxFrame::OnOpen)
EVT_MENU(wxID_EXIT, CWxFrame::OnQuit)
EVT_MENU(wxID_ABOUT, CWxFrame::OnAbout)
EVT_MENU(ID_Screenshot, CWxFrame::OnScreenshot)
EVT_MENU(ID_VS_Free, CWxFrame::OnViewSize)
EVT_MENU(ID_VS_640, CWxFrame::OnViewSize)
EVT_MENU(ID_VS_800, CWxFrame::OnViewSize)
EVT_MENU(ID_VS_16_9, CWxFrame::OnViewSize)
EVT_MENU(ID_VS_16_10, CWxFrame::OnViewSize)
EVT_MENU(ID_ScreenInfo, CWxFrame::OnScreenInfo)
EVT_SIZE(CWxFrame::OnResize)
END_EVENT_TABLE()
CWxFrame::CWxFrame(wxWindow *parent, const wxString& title)
: wxFrame(parent, wxID_ANY, title),
Context_(NULL), Proj_(NULL),
SizeLocked_(false)
{
#ifdef BIAS_HAVE_OPENSCENEGRAPH
Scene_ = NULL;
#endif
int ContextAttribs[] = { WX_GL_RGBA,
WX_GL_LEVEL, 0,
WX_GL_MIN_RED, 1,
WX_GL_MIN_GREEN, 1,
WX_GL_MIN_BLUE, 1,
WX_GL_MIN_ALPHA, 1,
WX_GL_DEPTH_SIZE, 24,
WX_GL_STENCIL_SIZE, 8,
WX_GL_DOUBLEBUFFER, 0 };
DummyContext_ = new ContextWX( this, wxID_ANY,
wxDefaultPosition, wxSize(640, 480), 0,
wxT("GLCanvas"),ContextAttribs);
DummyContext_->SetBlending(false);
DummyContext_->SetBackfaceCulling(false);
DummyContext_->SetMinSize( wxSize( 320,240 ) );
// create a status bar
Statusbar_ = this->CreateStatusBar( 2, wxST_SIZEGRIP, wxID_ANY );
const int numFields = 2;
Statusbar_->SetFieldsCount(numFields);
const int widths[numFields] = { -1, 170 };
Statusbar_->SetStatusWidths(numFields, widths);
// create a tool bar
Toolbar_ = this->CreateToolBar( wxTB_HORIZONTAL, wxID_ANY );
Toolbar_->SetToolBitmapSize( wxSize( 24,24 ) );
Toolbar_->SetToolSeparation( 0 );
Toolbar_->SetToolPacking( 0 );
Toolbar_->AddTool( ID_ScreenInfo, wxT("Screen Info"),
wxArtProvider::GetIcon(wxART_INFORMATION,wxART_TOOLBAR) );
Toolbar_->AddTool( wxID_OPEN, wxT("Open File"),
wxArtProvider::GetIcon(wxART_FILE_OPEN ,wxART_TOOLBAR) );
Toolbar_->Realize();
// create a menu bar
wxMenu *menuFile = new wxMenu;
menuFile->Append(wxID_OPEN);
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuEdit = new wxMenu;
menuEdit->Append(ID_Screenshot, wxString(wxT("&Screenshot")) + wxT('\t') + wxT("F3"));
wxMenu *menuView = new wxMenu;
menuView->AppendRadioItem(ID_VS_Free, wxString(wxT("View Size &Free")) + wxT('\t') + wxT("CTRL-SHIFT-0"));
menuView->AppendRadioItem(ID_VS_640, wxString(wxT("View Size 4:3 &small")) + wxT('\t') + wxT("CTRL-SHIFT-1"));
menuView->AppendRadioItem(ID_VS_800, wxString(wxT("View Size 4:3 &medium")) + wxT('\t') + wxT("CTRL-SHIFT-2"));
menuView->AppendRadioItem(ID_VS_16_9, wxString(wxT("View Size 16:&9 medium")) + wxT('\t') + wxT("CTRL-SHIFT-3"));
menuView->AppendRadioItem(ID_VS_16_10, wxString(wxT("View Size 16:&10 medium")) + wxT('\t') + wxT("CTRL-SHIFT-4"));
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
Menubar_ = new wxMenuBar;
Menubar_->Append(menuFile, wxT("&File"));
Menubar_->Append(menuEdit, wxT("&Edit"));
Menubar_->Append(menuView, wxT("&View"));
Menubar_->Append(menuHelp, wxT("&Help"));
SetMenuBar(Menubar_);
wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
mainSizer->Add(Context_, 1, wxEXPAND|wxALL|wxALIGN_CENTRE, 0);
/// this is the problem
//SetMinSize(wxSize(400,390));
SetSizer(mainSizer);
mainSizer->Fit(this);
Layout();
Proj_ = new GLProjectionParametersPerspective( /*offscreen*/);
}
void CWxFrame::
LateInit()
{
Context_ = DummyContext_;
//Context_->AppendAdditionalListener(&Handler_);
LogoBG_.Init(400, 300, 3);
LogoBG_.FillImageWithConstValue(120);
BGImage_.SetImage(LogoBG_);
Context_->MakeGLContextCurrent();
Context_->SetBlending(false);
// use special background image mode, which bypasses also fisheye rendering
// doesn't seem to work with distortion mapping
Context_->SetBackgroundImageScene(&BGImage_);
#ifdef BIAS_HAVE_OPENSCENEGRAPH
Scene_ = new SceneOpenSceneGraph;
BIASASSERT(Scene_);
if(Scene_->Init()!=0)
{
cerr<<"Scene_ init failed !!!"<<endl;
}
Scene_->SetActive(false);
Scene_->HeadlightSwitch(true);
Context_->AppendScene(Scene_);
#else
BIASWARN("Need OpenSceneGraph to show something")
#endif
Proj_->SetSimplePerspectiveCam(40.0);
Proj_->SetAutoReshapeBehaviour(GLProjectionParametersBase::AutoRescaleSimple);
Context_->SetGLProjectionParametersInterface(Proj_);
Context_->SetControl(&control_);
//DistanceMeaseureControl is also derived from Scenebase and draws itself
Context_->AppendScene(&control_);
Context_->PrintGLStatus();
Context_->InitializationComplete();
Context_->SetAutoClipping(true);
Context_->SetAutoClipMinZNear(0.1);
// background color (yellow: 1.0f, 1.0f, 0.0f, 0.0f)
Vector4<float> bg(1.0f, 1.0f, 0.0f, 0.0f);
Context_->SetClearColor(bg);
FitToImageSize_();
}
void CWxFrame::
OnResize(wxSizeEvent &event)
{
cout << "size event: window size \t("
<< GetSize().GetWidth() << " x " << GetSize().GetHeight() << ")\n";
event.Skip();
}
void CWxFrame::
OnOpen(wxCommandEvent &event)
{
#ifdef BIAS_HAVE_OPENSCENEGRAPH
wxString fileName;
wxFileDialog
dialog(
this,
wxT("Load Model"),
wxT(""),
wxT(""),
wxT("Model files (*.wrl;*.dae;*.osg;*.3ds;)|*.wrl;*.dae;*.osg;*.3ds;|All files (*.*)|*.*"),
wxFD_OPEN);
int ret = dialog.ShowModal();
if (ret == wxID_OK) {
ret = Scene_->AppendSubTreeFromFile(WxToAscii(dialog.GetPath()));
}
if (ret != 0){
stringstream text;
text<<"Error, model could not be loaded";
wxMessageBox(AsciiToWx(text.str()),wxT("Error"), wxOK | wxICON_ERROR, this);
}
else{
BIAS::Vector3<double> CameraCenter, radius, SceneCenter;
if (!Context_->GetSceneCenter(SceneCenter, radius))
BIASERR("could not retrieve bounding box from scene");
// move 10% outwards
CameraCenter = SceneCenter + 1.1 * radius;
cout<<"using scene center "<<SceneCenter<<" and new camera center "
<<CameraCenter <<endl;
// set point of interest only if not equal to camera center
if ((CameraCenter-SceneCenter).NormL2()>0.001) {
Proj_->GetMyselfAsProjectionParameterBase()->SetC(CameraCenter);
control_.SetPointOfInterest(SceneCenter);
} else {
cerr<<"could not set poi, empty scene ?"<<endl;
}
Context_->Refresh();
}
#else
stringstream text;
text<<"Error, BIAS does not have OpenSceneGraph to load models!";
wxMessageBox(AsciiToWx(text.str()),wxT("Error"), wxOK | wxICON_ERROR, this);
#endif
event.Skip();
}
void CWxFrame::
OnQuit(wxCommandEvent &event)
{
Close();
}
void CWxFrame::
OnViewSize(wxCommandEvent& event)
{
FitToImageSize_();
}
void CWxFrame::
OnScreenInfo(wxCommandEvent &event)
{
BIASASSERT(Context_);
BIASASSERT(Proj_);
Context_->MakeGLContextCurrent();
Proj_->GetImage(shot);
unsigned w,h;
Proj_->GetImageSize(w, h);
wxString msg = wxT("Context size is ");
msg << Context_->GetSize().GetWidth() << wxT(" x ")
<< Context_->GetSize().GetHeight();
msg << wxT("\n") << wxT("Projection size is ");
msg << w << wxT(" x ") << h;
msg << wxT("\n") << wxT("Screenshot size will be ");
msg << shot.GetWidth() << wxT(" x ") << shot.GetHeight();
wxMessageBox(msg, wxT("Example ContextWX - Screen Info"),
wxOK | wxICON_INFORMATION, this);
}
void CWxFrame::
OnScreenshot(wxCommandEvent& event)
{
BIASASSERT(Context_);
Context_->MakeGLContextCurrent();
Proj_->GetImage(shot);
wxFileDialog dlg(this, _("Example ContextWX - Save Screenshot"),
wxGetCwd(), wxT("ContextScreenshot"),
_("PNG (*.png)|*.png|JPEG (*.jpg)|*.jpg"),
wxFD_SAVE | wxFD_OVERWRITE_PROMPT | wxFD_CHANGE_DIR);
if ( dlg.ShowModal() != wxID_OK) return;
wxFileName fname( dlg.GetPath() );
if ( !fname.HasExt() ) {
int idx = dlg.GetFilterIndex();
switch (idx) {
case 1:
fname.SetExt(_("jpg"));
break;
default:
fname.SetExt(_("png"));
break;
}
}
int res = -1;
if ( fname.GetExt() == wxT("jpg") ) {
res = ImageIO::Save(WxToFilenames(fname.GetFullPath()), shot, ImageIO::FF_jpg);
}
if ( fname.GetExt() == wxT("png") ) {
res = ImageIO::Save(WxToFilenames(fname.GetFullPath()), shot, ImageIO::FF_png);
}
if ( res != 0 ) {
wxMessageBox(wxT("Saving screenshot failed!"), wxT("Error"),
wxOK | wxICON_ERROR, this);
}
}
void CWxFrame::
OnAbout(wxCommandEvent &event)
{
wxString desc;
desc << wxT(" This example is part of the\n");
desc << wxT(" BIAS library (Basic ImageAlgorithmS)\n");
wxString lic = wxT("BIAS is free software; you can redistribute it and/or modify\n \
it under the terms of the GNU Lesser General Public Licence as published by\n \
the Free Software Foundation; either version 2.1 of the Licence, or\n \
(at your option) any later version.\n \
\n \
BIAS is distributed in the hope that it will be useful,\n \
but WITHOUT ANY WARRANTY; without even the implied warranty of\n \
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n \
GNU Lesser General Public Licence for more details.\n \
\n \
You should have received a copy of the GNU Lesser General Public Licence\n \
along with BIAS; if not, write to the Free Software\n \
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n");
wxAboutDialogInfo aboutdlg;
aboutdlg.SetDescription( desc );
aboutdlg.SetLicence( lic );
aboutdlg.SetCopyright( wxT("Copyright (C) 2003-2010\n \
Multimediale Systeme der Informationsverarbeitung\n \
Institut fuer Informatik\n \
Christian-Albrechts-Universitaet Kiel") );
aboutdlg.SetWebSite( wxT("http://www.mip.informatik.uni-kiel.de/") );
wxAboutBox( aboutdlg );
}
void CWxFrame::
DumpSizes_()
{
static unsigned run = 0;
cout << "\n****** loads of sizes (no. " << run++ << ") *****\n";
cout << "size locked? " << boolalpha << SizeLocked_ << endl;
cout << "window size \t(" << GetSize().GetWidth() << " x " << GetSize().GetHeight() << ")\n";
cout << "client size \t(" << GetClientSize().GetWidth() << " x " << GetClientSize().GetHeight() << ")\n";
cout << "menubar size \t(" << Menubar_->GetSize().GetWidth() << " x " << Menubar_->GetSize().GetHeight() << ")\n";
cout << "toolbar size \t(" << Toolbar_->GetSize().GetWidth() << " x " << Toolbar_->GetSize().GetHeight() << ")\n";
cout << "statusbar size \t(" << Statusbar_->GetSize().GetWidth() << " x " << Statusbar_->GetSize().GetHeight() << ")\n";
cout << "context size \t(" << Context_->GetSize().GetWidth() << " x " << Context_->GetSize().GetHeight() << ")\n";
Context_->MakeGLContextCurrent();
Context_->Render();
if (Proj_ != NULL) {
unsigned w,h;
Proj_->GetImageSize(w,h);
cout << "projection image \t(" << w << " x " << h << ")\n";
}
Context_->GetImage(img);
cout << "context image \t(" << img.GetWidth() << " x " << img.GetHeight() << ")\n";
}
void CWxFrame::
FitToImageSize_()
{
cout << "++++ fitting ++++" << endl;
BIASASSERT(Context_);
DumpSizes_();
//wxSize windowSize = this->GetSize();
//wxSize clientSize = this->GetClientSize();
wxSize contextSize = Context_->GetSize();
SizeLocked_ = false;
//int barsHeight = 0; barsHeight = Menubar_->GetSize().GetHeight() + Toolbar_->GetSize().GetHeight()
// + Statusbar_->GetSize().GetHeight();
if ( Menubar_->IsChecked(ID_VS_640) ) {
SizeLocked_ = true;
contextSize.Set(640, 480);
//clientSize.Set(640, 480);
}
if ( Menubar_->IsChecked(ID_VS_800) ) {
SizeLocked_ = true;
contextSize.Set(800, 600);
//clientSize.Set(800, 600);
}
if ( Menubar_->IsChecked(ID_VS_16_9) ) {
SizeLocked_ = true;
contextSize.Set(800, 450);
//clientSize.Set(800, 450);
}
if ( Menubar_->IsChecked(ID_VS_16_10) ) {
SizeLocked_ = true;
contextSize.Set(800, 500);
//clientSize.Set(800, 500);
}
if ( Menubar_->IsChecked(ID_VS_Free) ) {
SizeLocked_ = false;
}
if ( SizeLocked_ ) {
Context_->SetSize(contextSize);
cout << "setting context size \t(" << Context_->GetSize().GetWidth() << " x " << Context_->GetSize().GetHeight() << ")\n";
this->SetClientSize(contextSize);
}
else {
wxSize minContextSize = Context_->GetMinSize();
if ( contextSize.GetWidth() < minContextSize.GetWidth()
|| contextSize.GetHeight() < minContextSize.GetHeight() ) {
contextSize = minContextSize;
Context_->SetSize(contextSize);
}
this->SetClientSize(contextSize);
}
Fit();
cout << "fit: context size \t(" << Context_->GetSize().GetWidth() << " x " << Context_->GetSize().GetHeight() << ")\n";
//Layout();
//Update();
//DumpSizes_();
wxString msg = wxT("Context size is ");
msg << Context_->GetSize().GetWidth() << wxT(" x ")
<< Context_->GetSize().GetHeight();
Statusbar_->SetStatusText(msg);
}
////////////////////////////////////////
////////// main routine //////////
////////////////////////////////////////
/** \cond HIDDEN_SYMBOLS */
class CWxApp : public wxApp
{
public:
bool OnInit();
protected:
};
/** \endcond */
bool CWxApp::OnInit()
{
wxInitAllImageHandlers();
CWxFrame* mainFrame =
new CWxFrame(NULL, wxT("Example ContextWX"));
mainFrame->Show();
mainFrame->LateInit();
return true;
}
IMPLEMENT_APP(CWxApp)