Main MRPT website > C++ reference for MRPT 1.9.9
CWxGLCanvasBase.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "gui-precomp.h" // Precompiled headers
11 
13 #include <mrpt/gui/WxSubsystem.h>
14 #include <mrpt/utils/CTicTac.h>
15 
16 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
17 
18 using namespace mrpt;
19 using namespace mrpt::utils;
20 using namespace mrpt::gui;
21 using namespace mrpt::opengl;
22 using namespace std;
23 
24 #if MRPT_HAS_OPENGL_GLUT
25 #ifdef MRPT_OS_WINDOWS
26 // Windows:
27 #include <windows.h>
28 #endif
29 
30 #ifdef MRPT_OS_APPLE
31 #include <OpenGL/gl.h>
32 #include <OpenGL/glu.h>
33 #include <GLUT/glut.h>
34 #else
35 #include <GL/gl.h>
36 #include <GL/glu.h>
37 #include <GL/glut.h>
38 #ifdef HAVE_FREEGLUT_EXT_H
39 #include <GL/freeglut_ext.h>
40 #endif
41 #endif
42 
43 #endif
44 
45 #if !wxUSE_GLCANVAS
46 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild wxWidgets"
47 #endif
48 
49 /*----------------------------------------------------------------
50  Implementation of Test-GLCanvas
51 -----------------------------------------------------------------*/
52 
53 void CWxGLCanvasBase::OnWindowCreation(wxWindowCreateEvent& ev)
54 {
55  if (!m_gl_context) m_gl_context = new wxGLContext(this);
56 }
57 
58 void CWxGLCanvasBase::swapBuffers() { SwapBuffers(); }
59 void CWxGLCanvasBase::preRender() { OnPreRender(); }
60 void CWxGLCanvasBase::postRender() { OnPostRender(); }
61 void CWxGLCanvasBase::renderError(const string& err_msg)
62 {
63  OnRenderError(_U(err_msg.c_str()));
64 }
65 
66 void CWxGLCanvasBase::OnMouseDown(wxMouseEvent& event)
67 {
68  setMousePos(event.GetX(), event.GetY());
69  setMouseClicked(true);
70 }
71 void CWxGLCanvasBase::OnMouseUp(wxMouseEvent& /*event*/)
72 {
73  setMouseClicked(false);
74 }
75 
76 void CWxGLCanvasBase::OnMouseMove(wxMouseEvent& event)
77 {
78  bool leftIsDown = event.LeftIsDown();
79 
80  if (leftIsDown || event.RightIsDown())
81  {
82  int X = event.GetX();
83  int Y = event.GetY();
84  updateLastPos(X, Y);
85 
86  // Proxy variables to cache the changes:
87  CamaraParams params = cameraParams();
88 
89  if (leftIsDown)
90  {
91  if (event.ShiftDown())
92  updateZoom(params, X, Y);
93 
94  else if (event.ControlDown())
95  updateRotate(params, X, Y);
96 
97  else
98  updateOrbitCamera(params, X, Y);
99  }
100  else
101  updatePan(params, X, Y);
102 
103  setMousePos(X, Y);
104  setCameraParams(params);
105 
106 #if wxCHECK_VERSION(2, 9, 5)
107  wxTheApp->SafeYieldFor(nullptr, wxEVT_CATEGORY_TIMER);
108 #endif
109  Refresh(false);
110  }
111 
112  // ensure we have the focus so we get keyboard events:
113  this->SetFocus();
114 }
115 
116 void CWxGLCanvasBase::OnMouseWheel(wxMouseEvent& event)
117 {
118  CamaraParams params = cameraParams();
119  updateZoom(params, event.GetWheelRotation());
120  setCameraParams(params);
121 
122  Refresh(false);
123  // ensure we have the focus so we get keyboard events:
124  this->SetFocus();
125 }
126 
127 static int WX_GL_ATTR_LIST[] = {WX_GL_DOUBLEBUFFER, WX_GL_RGBA,
128  WX_GL_DEPTH_SIZE, 24, 0};
129 
130 CWxGLCanvasBase::CWxGLCanvasBase(
131  wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
132  long style, const wxString& name)
133  : CGlCanvasBase(),
134  wxGLCanvas(
135  parent, id, WX_GL_ATTR_LIST, pos, size,
136  style | wxFULL_REPAINT_ON_RESIZE, name)
137 {
138  this->Bind(wxEVT_LEFT_DOWN, &CWxGLCanvasBase::OnMouseDown, this);
139  this->Bind(wxEVT_RIGHT_DOWN, &CWxGLCanvasBase::OnMouseDown, this);
140  this->Bind(wxEVT_LEFT_UP, &CWxGLCanvasBase::OnMouseUp, this);
141  this->Bind(wxEVT_RIGHT_UP, &CWxGLCanvasBase::OnMouseUp, this);
142  this->Bind(wxEVT_MOTION, &CWxGLCanvasBase::OnMouseMove, this);
143  this->Bind(wxEVT_MOUSEWHEEL, &CWxGLCanvasBase::OnMouseWheel, this);
144  this->Bind(wxEVT_CHAR, &CWxGLCanvasBase::OnChar, this);
145  this->Bind(wxEVT_CHAR_HOOK, &CWxGLCanvasBase::OnChar, this);
146  this->Bind(wxEVT_CREATE, &CWxGLCanvasBase::OnWindowCreation, this);
147 
148  this->Bind(wxEVT_SIZE, &CWxGLCanvasBase::OnSize, this);
149  this->Bind(wxEVT_PAINT, &CWxGLCanvasBase::OnPaint, this);
150  this->Bind(
151  wxEVT_ERASE_BACKGROUND, &CWxGLCanvasBase::OnEraseBackground, this);
152  this->Bind(wxEVT_ENTER_WINDOW, &CWxGLCanvasBase::OnEnterWindow, this);
153 
154 // JL: There seems to be a problem in MSW we don't receive this event, but
155 // in GTK we do and at the right moment to avoid an X server crash.
156 #ifdef _WIN32
157  wxWindowCreateEvent dum;
158  OnWindowCreation(dum);
159 #endif
160 }
161 
162 CWxGLCanvasBase::~CWxGLCanvasBase() { delete_safe(m_gl_context); }
163 void CWxGLCanvasBase::OnChar(wxKeyEvent& event) { OnCharCustom(event); }
164 void CWxGLCanvasBase::Render()
165 {
166  wxPaintDC dc(this);
167 
168  if (!m_gl_context)
169  { /*cerr << "[CWxGLCanvasBase::Render] No GL Context!" << endl;*/
170  return;
171  }
172  else
173  SetCurrent(*m_gl_context);
174 
175  // Init OpenGL once, but after SetCurrent
176  if (!m_init)
177  {
178  InitGL();
179  m_init = true;
180  }
181 
182  int width, height;
183  GetClientSize(&width, &height);
184  double At = renderCanvas(width, height);
185 
186  OnPostRenderSwapBuffers(At, dc);
187 }
188 
189 void CWxGLCanvasBase::OnEnterWindow(wxMouseEvent& WXUNUSED(event))
190 {
191  SetFocus();
192 }
193 
194 void CWxGLCanvasBase::OnPaint(wxPaintEvent& WXUNUSED(event)) { Render(); }
195 void CWxGLCanvasBase::OnSize(wxSizeEvent& event)
196 {
197  if (!m_parent->IsShown()) return;
198 
199  // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
200  int w, h;
201  GetClientSize(&w, &h);
202 
203  if (this->IsShownOnScreen())
204  {
205  if (!m_gl_context)
206  { /*cerr << "[CWxGLCanvasBase::Render] No GL Context!" << endl;*/
207  return;
208  }
209  else
210  SetCurrent(*m_gl_context);
211 
212  resizeViewport(w, h);
213  }
214 }
215 
216 void CWxGLCanvasBase::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
217 {
218  // Do nothing, to avoid flashing.
219 }
220 
221 void CWxGLCanvasBase::InitGL()
222 {
223  if (!m_gl_context)
224  { /*cerr << "[CWxGLCanvasBase::Render] No GL Context!" << endl;*/
225  return;
226  }
227  else
228  SetCurrent(*m_gl_context);
229 
230  static bool GLUT_INIT_DONE = false;
231 
232  if (!GLUT_INIT_DONE)
233  {
234  GLUT_INIT_DONE = true;
235 
236  int argc = 1;
237  char* argv[1] = {nullptr};
238  glutInit(&argc, argv);
239  }
240 }
241 
242 void CWxGLCanvasBase::setCameraPose(const mrpt::poses::CPose3D& camPose)
243 {
244  THROW_EXCEPTION("todo")
245 }
246 
247 #endif // MRPT_HAS_WXWIDGETS
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
#define _U(x)
Definition: WxSubsystem.h:506
#define THROW_EXCEPTION(msg)
STL namespace.
GLenum GLsizei width
Definition: glext.h:3531
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
void delete_safe(T *&ptr)
Calls "delete" to free an object only if the pointer is not nullptr, then set the pointer to NULL...
Definition: bits.h:253
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
GLuint id
Definition: glext.h:3909
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLuint const GLchar * name
Definition: glext.h:4054
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
GLsizeiptr size
Definition: glext.h:3923
This base class implements a working with opengl::Camera and a OpenGL canvas, and it&#39;s used in gui::C...
Definition: CGlCanvasBase.h:28
GLenum GLsizei GLsizei height
Definition: glext.h:3554
GLenum const GLfloat * params
Definition: glext.h:3534



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019