12 #include <mrpt/config.h>
29 #if MRPT_HAS_OPENGL_GLUT
30 #ifdef MRPT_OS_WINDOWS
36 #include <OpenGL/gl.h>
37 #include <OpenGL/glu.h>
38 #include <GLUT/glut.h>
43 #ifdef HAVE_FREEGLUT_EXT_H
44 #include <GL/freeglut_ext.h>
49 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
52 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild wxWidgets"
62 class CMyGLCanvas_DisplayWindow3D :
public mrpt::gui::CWxGLCanvasBase
65 CMyGLCanvas_DisplayWindow3D(
67 const wxPoint& pos = wxDefaultPosition,
68 const wxSize&
size = wxDefaultSize,
long style = 0,
69 const wxString&
name = _T(
"CMyGLCanvas_DisplayWindow3D"));
71 virtual ~CMyGLCanvas_DisplayWindow3D();
81 void render_text_messages_public(
const int w,
const int h)
const
83 render_text_messages(
w, h);
86 THubClass m_text_msgs;
88 void OnCharCustom(wxKeyEvent& event);
89 void OnMouseDown(wxMouseEvent& event);
90 void OnMouseMove(wxMouseEvent& event);
94 void OnPostRenderSwapBuffers(
double At, wxPaintDC& dc);
96 static void display3D_processKeyEvent(
102 CMyGLCanvas_DisplayWindow3D::CMyGLCanvas_DisplayWindow3D(
104 const wxPoint& pos,
const wxSize&
size,
long style,
const wxString&
name)
105 : CWxGLCanvasBase(parent,
id, pos,
size, style,
name), m_win3D(win3D)
107 this->Bind(wxEVT_CHAR, &CMyGLCanvas_DisplayWindow3D::OnCharCustom,
this);
109 wxEVT_CHAR_HOOK, &CMyGLCanvas_DisplayWindow3D::OnCharCustom,
this);
111 wxEVT_LEFT_DOWN, &CMyGLCanvas_DisplayWindow3D::OnMouseDown,
this);
113 wxEVT_RIGHT_DOWN, &CMyGLCanvas_DisplayWindow3D::OnMouseDown,
this);
114 this->Bind(wxEVT_MOTION, &CMyGLCanvas_DisplayWindow3D::OnMouseMove,
this);
117 void CMyGLCanvas_DisplayWindow3D::display3D_processKeyEvent(
128 cout <<
"[CDisplayWindow3D] Switching fullscreen...\n";
132 win->ShowFullScreen(!win->IsFullScreen());
143 const int code = ev.GetKeyCode();
162 void CMyGLCanvas_DisplayWindow3D::OnCharCustom(wxKeyEvent& ev)
164 CMyGLCanvas_DisplayWindow3D::display3D_processKeyEvent(m_win3D, ev);
167 void CMyGLCanvas_DisplayWindow3D::OnMouseDown(wxMouseEvent& event)
177 event.LeftDown(), event.RightDown()));
187 void CMyGLCanvas_DisplayWindow3D::OnMouseMove(wxMouseEvent& event)
197 event.LeftDown(), event.RightDown()));
207 CMyGLCanvas_DisplayWindow3D::~CMyGLCanvas_DisplayWindow3D()
209 getOpenGLSceneRef().reset();
213 void CMyGLCanvas_DisplayWindow3D::OnPreRender()
215 auto& openGLSceneRef = getOpenGLSceneRef();
216 if (openGLSceneRef) openGLSceneRef.reset();
219 if (ptrScene) openGLSceneRef = ptrScene;
222 void CMyGLCanvas_DisplayWindow3D::OnPostRender()
225 getOpenGLSceneRef().reset();
230 this->GetSize(&
w, &h);
232 m_text_msgs.render_text_messages_public(
w, h);
235 void CMyGLCanvas_DisplayWindow3D::OnPostRenderSwapBuffers(
236 double At, wxPaintDC& dc)
254 if (!grabFile.empty())
256 frame->saveToFile(grabFile);
263 std::lock_guard<std::mutex> lock(
274 #if MRPT_HAS_WXWIDGETS
285 const std::
string& caption, wxSize initialSize)
286 : m_win3D(win3D), m_mainFrame(parent)
288 #if MRPT_HAS_OPENGL_GLUT
291 parent,
id,
_U(caption.c_str()), wxDefaultPosition, initialSize,
292 wxDEFAULT_FRAME_STYLE, _T(
"id"));
299 m_canvas =
new CMyGLCanvas_DisplayWindow3D(
300 win3D,
this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
303 this->Bind(wxEVT_CLOSE_WINDOW, &C3DWindowDialog::OnClose,
this);
305 wxEVT_COMMAND_MENU_SELECTED, &C3DWindowDialog::OnMenuClose,
this,
308 wxEVT_COMMAND_MENU_SELECTED, &C3DWindowDialog::OnMenuAbout,
this,
310 this->Bind(wxEVT_CHAR, &C3DWindowDialog::OnChar,
this);
311 this->Bind(wxEVT_SIZE, &C3DWindowDialog::OnResize,
this);
315 WxSubsystem::CWXMainFrame::notifyWindowCreation();
324 C3DWindowDialog::~C3DWindowDialog()
333 bool allow_close =
true;
343 if (!allow_close)
return;
364 _(
"3D Scene viewer\n Class gui::CDisplayWindow3D\n MRPT C++ library"),
370 #if MRPT_HAS_OPENGL_GLUT
371 CMyGLCanvas_DisplayWindow3D::display3D_processKeyEvent(
m_win3D, ev);
377 #if MRPT_HAS_OPENGL_GLUT
385 m_win3D, event.GetSize().GetWidth(),
386 event.GetSize().GetHeight()));
398 #if MRPT_HAS_OPENGL_GLUT
399 m_canvas->m_text_msgs.clearTextMessages();
404 const double x_frac,
const double y_frac,
const std::string& text,
408 #if MRPT_HAS_OPENGL_GLUT
409 m_canvas->m_text_msgs.addTextMessage(
410 x_frac, y_frac, text,
color, unique_index, font);
415 const double x_frac,
const double y_frac,
const std::string& text,
418 const size_t unique_index,
const double font_spacing,
419 const double font_kerning,
const bool has_shadow,
422 #if MRPT_HAS_OPENGL_GLUT
423 m_canvas->m_text_msgs.addTextMessage(
424 x_frac, y_frac, text,
color, font_name, font_size, font_style,
425 unique_index, font_spacing, font_kerning, has_shadow, shadow_color);
435 const std::string& windowCaption,
unsigned int initialWindowWidth,
436 unsigned int initialWindowHeight)
438 m_grab_imgs_prefix(),
440 m_is_capturing_imgs(false),
441 m_lastFullScreen(
mrpt::system::
now()),
452 const std::string& windowCaption,
unsigned int initialWindowWidth,
453 unsigned int initialWindowHeight)
457 windowCaption, initialWindowWidth, initialWindowHeight));
476 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
479 cerr <<
"[CDisplayWindow3D::setPos] Window closed!: " <<
m_caption
503 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
506 cerr <<
"[CDisplayWindow3D::setPos] Window closed!: " <<
m_caption
530 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
533 cerr <<
"[CDisplayWindow3D::setWindowTitle] Window closed!: "
568 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
590 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
592 if (win) win->
m_canvas->setElevationDegrees(deg);
600 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
602 if (win) win->
m_canvas->setUseCameraFromScene(useIt);
613 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
615 if (win) win->
m_canvas->setAzimuthDegrees(deg);
626 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
644 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
646 if (win) win->
m_canvas->setZoomDistance(zoom);
657 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
659 if (win) win->
m_canvas->setCameraProjective(isProjective);
674 gl_view->getViewportClipDistances(m, M);
675 gl_view->setViewportClipDistances(new_min, M);
688 gl_view->getViewportClipDistances(m, M);
689 gl_view->setViewportClipDistances(m, new_max);
696 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
698 if (win)
return win->
m_canvas->cameraFOV();
705 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
716 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
718 return win ? win->
m_canvas->getElevationDegrees() : 0;
729 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
731 return win ? win->
m_canvas->getAzimuthDegrees() : 0;
741 float&
x,
float&
y,
float&
z)
const
743 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
765 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
767 return win ? win->
m_canvas->getZoomDistance() : 0;
778 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
780 return win ? win->
m_canvas->isCameraProjective() :
true;
791 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
793 if (!win)
return false;
812 m_3Dscene->getViewport(
"main")->get3DRayForPixelCoord(
x,
y, ray);
825 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
829 *(cursorIsCross ? wxCROSS_CURSOR : wxSTANDARD_CURSOR));
901 const double x_frac,
const double y_frac,
const std::string& text,
905 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
925 REQ->
y = int(unique_index);
943 const double x_frac,
const double y_frac,
const std::string& text,
946 const size_t unique_index,
const double font_spacing,
947 const double font_kerning,
const bool draw_shadow,
950 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
971 REQ->
vector_x[8] = draw_shadow ? 1 : 0;
976 REQ->
x = int(font_style);
977 REQ->
y = int(unique_index);
1002 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
1018 const double ALPHA = 0.99;
1042 view->setImageView(
img);
1050 view->setImageView_fast(
img);
void OnMenuClose(wxCommandEvent &event)
void OnClose(wxCloseEvent &event)
void addTextMessage(const double x_frac, const double y_frac, const std::string &text, const mrpt::utils::TColorf &color, const size_t unique_index, const mrpt::opengl::TOpenGLFont font)
CMyGLCanvas_DisplayWindow3D * m_canvas
void OnResize(wxSizeEvent &event)
void OnChar(wxKeyEvent &event)
CDisplayWindow3D * m_win3D
void OnMenuAbout(wxCommandEvent &event)
The base class for GUI window classes.
std::string m_caption
The caption of the window.
void notifyChildWindowDestruction()
Called by wx main thread to set m_hwnd to NULL.
bool isOpen()
Returns false if the user has already closed the window.
std::promise< void > m_windowDestroyed
This semaphore will be signaled when the wx window is destroyed.
void createWxWindow(unsigned int initialWidth, unsigned int initialHeight)
Must be called by child classes just within the constructor.
void destroyWxWindow()
Must be called by child classes in their destructors.
volatile int m_keyPushedCode
volatile mrptKeyModifier m_keyPushedModifier
volatile bool m_keyPushed
mrpt::utils::void_ptr_noncopy m_hwnd
The window handle.
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
void addTextMessage(const double x, const double y, const std::string &text, const mrpt::utils::TColorf &color=mrpt::utils::TColorf(1.0, 1.0, 1.0), const size_t unique_index=0, const mrpt::opengl::TOpenGLFont font=mrpt::opengl::MRPT_GLUT_BITMAP_TIMES_ROMAN_24)
Add 2D text messages overlapped to the 3D rendered scene.
static CDisplayWindow3D::Ptr Create(const std::string &windowCaption, unsigned int initialWindowWidth=400, unsigned int initialWindowHeight=300)
Class factory returning a smart pointer.
void setCameraPointingToPoint(float x, float y, float z)
Changes the camera parameters programmatically.
void setImageView_fast(mrpt::utils::CImage &img)
Just like setImageView but moves the internal image memory instead of making a copy,...
mrpt::utils::CImage::Ptr getLastWindowImagePtr() const
Retrieve the last captured image from the window, as a smart pointer.
void internal_setRenderingFPS(double FPS)
Set the rendering FPS (users don't call this, the method is for internal MRPT objects only)
unsigned int m_grab_imgs_idx
void forceRepaint()
Repaints the window.
void unlockAccess3DScene()
Unlocks the access to the internal 3D scene.
void setPos(int x, int y) override
Changes the position of the window on the screen.
virtual void setCursorCross(bool cursorIsCross) override
Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true)
std::recursive_mutex m_csAccess3DScene
Critical section for accesing m_3Dscene.
bool getLastMousePositionRay(mrpt::math::TLine3D &ray) const
Gets the 3D ray for the direction line of the pixel where the mouse cursor is at.
void setCameraZoom(float zoom)
Changes the camera parameters programmatically.
float getCameraElevationDeg() const
Get camera parameters programmatically.
bool isCapturingImgs() const
void getCameraPointingToPoint(float &x, float &y, float &z) const
Get camera parameters programmatically.
void setFOV(float v)
Changes the camera field of view (in degrees) (used for gluPerspective).
void setWindowTitle(const std::string &str) override
Changes the window title.
void setCameraProjective(bool isProjective)
Sets the camera as projective, or orthogonal.
void grabImagesStart(const std::string &grab_imgs_prefix=std::string("video_"))
Start to save rendered images to disk.
CDisplayWindow3D(const std::string &windowCaption=std::string(), unsigned int initialWindowWidth=400, unsigned int initialWindowHeight=300)
Constructor.
bool getLastWindowImage(mrpt::utils::CImage &out_img) const
Retrieve the last captured image from the window.
mrpt::opengl::COpenGLScene::Ptr & get3DSceneAndLock()
Gets a reference to the smart shared pointer that holds the internal scene (carefuly read introductio...
void setCameraAzimuthDeg(float deg)
Changes the camera parameters programmatically.
virtual bool getLastMousePosition(int &x, int &y) const override
Gets the last x,y pixel coordinates of the mouse.
virtual ~CDisplayWindow3D()
Destructor.
std::string m_grab_imgs_prefix
bool isCameraProjective() const
Sets the camera as projective, or orthogonal.
void captureImagesStart()
Enables the grabbing of CImage objects from screenshots of the window.
void setMaxRange(double new_max)
Changes the camera max clip range (z) (used for gluPerspective.
void resize(unsigned int width, unsigned int height) override
Resizes the window, stretching the image to fit into the display area.
float getFOV() const
Return the camera field of view (in degrees) (used for gluPerspective)
void internal_emitGrabImageEvent(const std::string &fil)
called by CMyGLCanvas_DisplayWindow3D::OnPostRenderSwapBuffers
void setImageView(const mrpt::utils::CImage &img)
Set the "main" viewport into "image view"-mode, where an image is efficiently drawn (fitting the view...
mrpt::opengl::COpenGLViewport::Ptr getDefaultViewport()
A short cut for getting the "main" viewport of the scene object, it is equivalent to:
mrpt::opengl::COpenGLScene::Ptr m_3Dscene
Internal OpenGL object (see general discussion in about usage of this object)
mrpt::system::TTimeStamp m_lastFullScreen
std::string grabImageGetNextFile()
Increments by one the image counter and return the next image file name (Users normally don't want to...
float getCameraAzimuthDeg() const
Get camera parameters programmatically.
void setMinRange(double new_min)
Changes the camera min clip range (z) (used for gluPerspective).
float getCameraZoom() const
Get camera parameters programmatically.
void clearTextMessages()
Clear all text messages created with addTextMessage().
std::mutex m_last_captured_img_cs
mrpt::utils::CImage::Ptr m_last_captured_img
void grabImagesStop()
Stops image grabbing started by grabImagesStart.
void setCameraElevationDeg(float deg)
Changes the camera parameters programmatically.
void captureImagesStop()
Stop image grabbing.
std::shared_ptr< CDisplayWindow3D > Ptr
void useCameraFromScene(bool useIt=true)
If set to true (default = false), the mouse-based scene navigation will be disabled and the camera po...
CDisplayWindow3DLocker(CDisplayWindow3D &win, mrpt::opengl::COpenGLScene::Ptr &out_scene_ptr)
Acquires the lock of the 3D scene of the referenced window, and returns a copy of the smart pointer t...
~CDisplayWindow3DLocker()
static int notifyWindowDestruction()
Atomically decrements the number of windows created with the main frame as parent.
This class implements the GUI thread required for the wxWidgets-based GUI.
static void pushPendingWxRequest(TRequestToWxMainThread *data)
Thread-safe method to insert a new pending request (The memory must be dinamically allocated with "ne...
static wxBitmap getMRPTDefaultIcon()
An event sent by a CDisplayWindow3D window when an image is saved after enabling this feature with CD...
An event sent by a window upon a mouse click, giving the (x,y) pixel coordinates.
An event sent by a window when the mouse is moved over it.
An event sent by a window upon a char pressed by the user.
An event sent by a window upon when it's about to be closed, either manually by the user or programma...
An event sent by a window upon resize.
std::shared_ptr< COpenGLScene > Ptr
std::shared_ptr< COpenGLViewport > Ptr
Keeps a list of text messages which can be rendered to OpenGL contexts by graphic classes.
A class for storing images as grayscale or RGB bitmaps.
std::shared_ptr< CImage > Ptr
bool hasSubscribers() const
Can be called by a derived class before preparing an event for publishing with publishEvent to determ...
void publishEvent(const mrptEvent &e) const
Called when you want this object to emit an event to all the observers currently subscribed to this o...
GLAPI void GLAPIENTRY glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels)
GLAPI void GLAPIENTRY glReadBuffer(GLenum mode)
GLubyte GLubyte GLubyte GLubyte w
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
GLuint const GLchar * name
GLenum GLsizei GLsizei height
GLsizei const GLchar ** string
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
double timeDifference(const mrpt::system::TTimeStamp t_first, const mrpt::system::TTimeStamp t_later)
Returns the time difference from t1 to t2 (positive if t2 is posterior to t1), in seconds.
#define THROW_EXCEPTION(msg)
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
Classes for creating GUI windows for 2D and 3D visualization.
mrptKeyModifier keyEventToMrptKeyModifier(const wxKeyEvent &ev)
Extracts the key modifiers from a wxKeyEvent.
This base provides a set of functions for maths stuff.
The namespace for 3D scene representation and rendering.
TOpenGLFontStyle
Different style for vectorized font rendering.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values,...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
The data structure for each inter-thread request:
int OPCODE
Valid codes are: For CDisplayWindow:
mrpt::math::CVectorFloat vector_x
mrpt::gui::CDisplayWindow3D * source3D
Only one of source* can be non-nullptr, indicating the class that generated the request.
std::string str
Parameters, depending on OPCODE.
3D line, represented by a base point and a director vector.
A RGB color - floats in the range [0,1].
A pair (x,y) of pixel coordinates (integer resolution).