Program Listing for File CWxGLCanvasBase.h

↰ Return to documentation for file (include/mrpt/gui/CWxGLCanvasBase.h)

/*                    _
                     | |    Mobile Robot Programming Toolkit (MRPT)
 _ __ ___  _ __ _ __ | |_
| '_ ` _ \| '__| '_ \| __|          https://www.mrpt.org/
| | | | | | |  | |_) | |_
|_| |_| |_|_|  | .__/ \__|     https://github.com/MRPT/mrpt/
               | |
               |_|

 Copyright (c) 2005-2026, Individual contributors, see AUTHORS file
 See: https://www.mrpt.org/Authors - All rights reserved.
 SPDX-License-Identifier: BSD-3-Clause
*/

#pragma once

#include <mrpt/gui/CGlCanvasBase.h>
#include <mrpt/gui/config.h>
#include <mrpt/opengl/config.h>  // MRPT_HAS_OPENGL
#include <mrpt/viz/COrbitCameraController.h>
#include <mrpt/viz/Scene.h>
#include <mrpt/viz/opengl_fonts.h>

#include <atomic>
#include <mutex>

// have wxWidgets libs
#if MRPT_HAS_WXWIDGETS

#include <wx/artprov.h>
#include <wx/bitmap.h>
#include <wx/busyinfo.h>
#include <wx/colordlg.h>
#include <wx/dcmemory.h>
#include <wx/dirdlg.h>
#include <wx/filedlg.h>
#include <wx/icon.h>
#include <wx/image.h>
#include <wx/imaglist.h>
#include <wx/intl.h>
#include <wx/log.h>
#include <wx/msgdlg.h>
#include <wx/progdlg.h>
#include <wx/string.h>
#include <wx/textdlg.h>

#if wxUSE_GLCANVAS && MRPT_HAS_OPENGL

#include <wx/dcclient.h>
#include <wx/glcanvas.h>

// To avoid conflicts between Eigen & X11 headers
#ifdef Success
#undef Success
#endif

namespace mrpt::gui
{
class CWxGLCanvasBase : public CGlCanvasBase, public wxGLCanvas
{
 public:
  CWxGLCanvasBase(
      wxWindow* parent,
      wxWindowID id = wxID_ANY,
      const wxPoint& pos = wxDefaultPosition,
      const wxSize& size = wxDefaultSize,
      long style = 0,
      const wxString& name = _T("CWxGLCanvasBase"));

  void OnPaint(wxPaintEvent& event);
  void OnSize(wxSizeEvent& event);
  void OnEraseBackground(wxEraseEvent& event);
  void OnEnterWindow(wxMouseEvent& event);

  void OnChar(wxKeyEvent& event);

  void OnMouseDown(wxMouseEvent& event);
  void OnMouseMove(wxMouseEvent& event);
  void OnMouseUp(wxMouseEvent&);
  void OnMouseWheel(wxMouseEvent& event);

  void Render();
  void InitGL();

  void setCameraPose(const mrpt::poses::CPose3D& camPose);

  virtual void OnCharCustom([[maybe_unused]] wxKeyEvent& event) {}
  virtual void OnPreRender() {}
  virtual void OnPostRender() {}
  virtual void OnPostRenderSwapBuffers([[maybe_unused]] double At, [[maybe_unused]] wxPaintDC& dc)
  {
  }

  virtual void OnRenderError([[maybe_unused]] const wxString& str) {}

  [[nodiscard]] bool is_GL_context_created() const { return m_init; }

 protected:
  static std::unique_ptr<wxGLContext> m_gl_context;
  static std::recursive_mutex m_gl_context_mtx;

  std::atomic_bool m_init = false;

  long m_Key = 0;
  unsigned long m_StartTime = 0;
  unsigned long m_LastTime = 0;
  unsigned long m_LastRedraw = 0;

  // Used to create the gl context at startup.
  void OnWindowCreation(wxWindowCreateEvent& ev);
  void swapBuffers() override;
  void preRender() override;
  void postRender() override;
  void renderError(const std::string& err_msg) override;

};  // end of class

}  // namespace mrpt::gui

#endif  // wxUSE_GLCANVAS
#endif  // MRPT_HAS_WXWIDGETS