Go to the documentation of this file.
12 #include <mrpt/config.h>
28 #if MRPT_HAS_OPENGL_GLUT
35 #include <OpenGL/gl.h>
36 #include <OpenGL/glu.h>
37 #include <GLUT/glut.h>
42 #ifdef HAVE_FREEGLUT_EXT_H
43 #include <GL/freeglut_ext.h>
48 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
51 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild wxWidgets"
61 class CMyGLCanvas_DisplayWindow3D :
public mrpt::gui::CWxGLCanvasBase
64 CMyGLCanvas_DisplayWindow3D(
66 const wxPoint& pos = wxDefaultPosition,
67 const wxSize&
size = wxDefaultSize,
long style = 0,
68 const wxString&
name = _T(
"CMyGLCanvas_DisplayWindow3D"));
70 virtual ~CMyGLCanvas_DisplayWindow3D();
80 void render_text_messages_public(
const int w,
const int h)
const
82 render_text_messages(
w, h);
85 THubClass m_text_msgs;
87 void OnCharCustom(wxKeyEvent& event);
88 void OnMouseDown(wxMouseEvent& event);
89 void OnMouseMove(wxMouseEvent& event);
93 void OnPostRenderSwapBuffers(
double At, wxPaintDC& dc);
95 static void display3D_processKeyEvent(
101 CMyGLCanvas_DisplayWindow3D::CMyGLCanvas_DisplayWindow3D(
103 const wxPoint& pos,
const wxSize&
size,
long style,
const wxString&
name)
104 : CWxGLCanvasBase(parent,
id, pos,
size, style,
name), m_win3D(win3D)
106 this->Bind(wxEVT_CHAR, &CMyGLCanvas_DisplayWindow3D::OnCharCustom,
this);
108 wxEVT_CHAR_HOOK, &CMyGLCanvas_DisplayWindow3D::OnCharCustom,
this);
110 wxEVT_LEFT_DOWN, &CMyGLCanvas_DisplayWindow3D::OnMouseDown,
this);
112 wxEVT_RIGHT_DOWN, &CMyGLCanvas_DisplayWindow3D::OnMouseDown,
this);
113 this->Bind(wxEVT_MOTION, &CMyGLCanvas_DisplayWindow3D::OnMouseMove,
this);
116 void CMyGLCanvas_DisplayWindow3D::display3D_processKeyEvent(
127 cout <<
"[CDisplayWindow3D] Switching fullscreen...\n";
131 win->ShowFullScreen(!
win->IsFullScreen());
142 const int code = ev.GetKeyCode();
161 void CMyGLCanvas_DisplayWindow3D::OnCharCustom(wxKeyEvent& ev)
163 CMyGLCanvas_DisplayWindow3D::display3D_processKeyEvent(m_win3D, ev);
166 void CMyGLCanvas_DisplayWindow3D::OnMouseDown(wxMouseEvent& event)
176 event.LeftDown(), event.RightDown()));
186 void CMyGLCanvas_DisplayWindow3D::OnMouseMove(wxMouseEvent& event)
196 event.LeftDown(), event.RightDown()));
206 CMyGLCanvas_DisplayWindow3D::~CMyGLCanvas_DisplayWindow3D()
208 getOpenGLSceneRef().reset();
212 void CMyGLCanvas_DisplayWindow3D::OnPreRender()
214 auto& openGLSceneRef = getOpenGLSceneRef();
215 if (openGLSceneRef) openGLSceneRef.reset();
218 if (ptrScene) openGLSceneRef = ptrScene;
221 void CMyGLCanvas_DisplayWindow3D::OnPostRender()
224 getOpenGLSceneRef().reset();
229 this->GetSize(&
w, &h);
231 m_text_msgs.render_text_messages_public(
w, h);
234 void CMyGLCanvas_DisplayWindow3D::OnPostRenderSwapBuffers(
235 double At, wxPaintDC& dc)
253 if (!grabFile.empty())
255 frame->saveToFile(grabFile);
262 std::lock_guard<std::mutex> lock(
271 #endif // Wx + OpenGL
273 #if MRPT_HAS_WXWIDGETS
284 const std::
string& caption, wxSize initialSize)
285 : m_win3D(win3D), m_mainFrame(parent)
287 #if MRPT_HAS_OPENGL_GLUT
290 parent,
id,
_U(caption.c_str()), wxDefaultPosition, initialSize,
291 wxDEFAULT_FRAME_STYLE, _T(
"id"));
298 m_canvas =
new CMyGLCanvas_DisplayWindow3D(
299 win3D,
this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
332 bool allow_close =
true;
342 if (!allow_close)
return;
363 _(
"3D Scene viewer\n Class gui::CDisplayWindow3D\n MRPT C++ library"),
369 #if MRPT_HAS_OPENGL_GLUT
370 CMyGLCanvas_DisplayWindow3D::display3D_processKeyEvent(
m_win3D, ev);
376 #if MRPT_HAS_OPENGL_GLUT
384 m_win3D, event.GetSize().GetWidth(),
385 event.GetSize().GetHeight()));
397 #if MRPT_HAS_OPENGL_GLUT
398 m_canvas->m_text_msgs.clearTextMessages();
403 const double x_frac,
const double y_frac,
const std::string& text,
407 #if MRPT_HAS_OPENGL_GLUT
408 m_canvas->m_text_msgs.addTextMessage(
409 x_frac, y_frac, text,
color, unique_index, font);
414 const double x_frac,
const double y_frac,
const std::string& text,
417 const size_t unique_index,
const double font_spacing,
418 const double font_kerning,
const bool has_shadow,
421 #if MRPT_HAS_OPENGL_GLUT
422 m_canvas->m_text_msgs.addTextMessage(
423 x_frac, y_frac, text,
color, font_name, font_size, font_style,
424 unique_index, font_spacing, font_kerning, has_shadow, shadow_color);
428 #endif // MRPT_HAS_WXWIDGETS
434 const std::string& windowCaption,
unsigned int initialWindowWidth,
435 unsigned int initialWindowHeight)
437 m_grab_imgs_prefix(),
439 m_is_capturing_imgs(false),
440 m_lastFullScreen(
mrpt::system::
now()),
451 const std::string& windowCaption,
unsigned int initialWindowWidth,
452 unsigned int initialWindowHeight)
456 windowCaption, initialWindowWidth, initialWindowHeight));
475 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
478 cerr <<
"[CDisplayWindow3D::setPos] Window closed!: " <<
m_caption
502 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
505 cerr <<
"[CDisplayWindow3D::setPos] Window closed!: " <<
m_caption
529 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
532 cerr <<
"[CDisplayWindow3D::setWindowTitle] Window closed!: "
567 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
589 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
591 if (
win)
win->m_canvas->setElevationDegrees(deg);
599 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
601 if (
win)
win->m_canvas->setUseCameraFromScene(useIt);
612 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
614 if (
win)
win->m_canvas->setAzimuthDegrees(deg);
625 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
629 win->m_canvas->setCameraPointing(
x,
y,
z);
643 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
645 if (
win)
win->m_canvas->setZoomDistance(zoom);
656 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
658 if (
win)
win->m_canvas->setCameraProjective(isProjective);
673 gl_view->getViewportClipDistances(m, M);
674 gl_view->setViewportClipDistances(new_min, M);
687 gl_view->getViewportClipDistances(m, M);
688 gl_view->setViewportClipDistances(m, new_max);
695 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
697 if (
win)
return win->m_canvas->cameraFOV();
704 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
706 if (
win)
win->m_canvas->setCameraFOV(
v);
715 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
717 return win ?
win->m_canvas->getElevationDegrees() : 0;
728 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
730 return win ?
win->m_canvas->getAzimuthDegrees() : 0;
740 float&
x,
float&
y,
float&
z)
const
742 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
746 x =
win->m_canvas->getCameraPointingX();
747 y =
win->m_canvas->getCameraPointingY();
748 z =
win->m_canvas->getCameraPointingZ();
764 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
766 return win ?
win->m_canvas->getZoomDistance() : 0;
777 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
779 return win ?
win->m_canvas->isCameraProjective() :
true;
790 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
792 if (!
win)
return false;
793 win->m_canvas->getLastMousePosition(
x,
y);
811 m_3Dscene->getViewport(
"main")->get3DRayForPixelCoord(
x,
y, ray);
824 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
827 win->m_canvas->SetCursor(
828 *(cursorIsCross ? wxCROSS_CURSOR : wxSTANDARD_CURSOR));
900 const double x_frac,
const double y_frac,
const std::string& text,
904 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
924 REQ->
y = int(unique_index);
942 const double x_frac,
const double y_frac,
const std::string& text,
945 const size_t unique_index,
const double font_spacing,
946 const double font_kerning,
const bool draw_shadow,
949 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
970 REQ->
vector_x[8] = draw_shadow ? 1 : 0;
975 REQ->
x = int(font_style);
976 REQ->
y = int(unique_index);
1001 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
1017 const double ALPHA = 0.99;
1041 view->setImageView(
img);
1049 view->setImageView_fast(
img);
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
void setMaxRange(double new_max)
Changes the camera max clip range (z) (used for gluPerspective.
void setFOV(float v)
Changes the camera field of view (in degrees) (used for gluPerspective).
void internal_emitGrabImageEvent(const std::string &fil)
called by CMyGLCanvas_DisplayWindow3D::OnPostRenderSwapBuffers
volatile int m_keyPushedCode
virtual void setCursorCross(bool cursorIsCross) override
Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true)
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.
bool isCameraProjective() const
Sets the camera as projective, or orthogonal.
mrptKeyModifier keyEventToMrptKeyModifier(const wxKeyEvent &ev)
Extracts the key modifiers from a wxKeyEvent.
std::recursive_mutex m_csAccess3DScene
Critical section for accesing m_3Dscene.
float getFOV() const
Return the camera field of view (in degrees) (used for gluPerspective)
float getCameraElevationDeg() const
Get camera parameters programmatically.
void createWxWindow(unsigned int initialWidth, unsigned int initialHeight)
Must be called by child classes just within the constructor.
virtual ~CDisplayWindow3D()
Destructor.
mrpt::opengl::COpenGLScene::Ptr m_3Dscene
Internal OpenGL object (see general discussion in about usage of this object)
void grabImagesStart(const std::string &grab_imgs_prefix=std::string("video_"))
Start to save rendered images to disk.
bool isCapturingImgs() const
static CDisplayWindow3D::Ptr Create(const std::string &windowCaption, unsigned int initialWindowWidth=400, unsigned int initialWindowHeight=300)
Class factory returning a smart pointer.
CDisplayWindow3D(const std::string &windowCaption=std::string(), unsigned int initialWindowWidth=400, unsigned int initialWindowHeight=300)
Constructor.
TOpenGLFontStyle
Different style for vectorized font rendering.
void setWindowTitle(const std::string &str) override
Changes the window title.
static void pushPendingWxRequest(TRequestToWxMainThread *data)
Thread-safe method to insert a new pending request (The memory must be dinamically allocated with "ne...
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...
void OnMenuAbout(wxCommandEvent &event)
volatile mrptKeyModifier m_keyPushedModifier
void OnClose(wxCloseEvent &event)
void getCameraPointingToPoint(float &x, float &y, float &z) const
Get camera parameters programmatically.
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
std::string m_grab_imgs_prefix
void setCameraZoom(float zoom)
Changes the camera parameters programmatically.
An event sent by a window when the mouse is moved over it.
void captureImagesStart()
Enables the grabbing of CImage objects from screenshots of the window.
virtual bool getLastMousePosition(int &x, int &y) const override
Gets the last x,y pixel coordinates of the mouse.
mrpt::opengl::COpenGLScene::Ptr & get3DSceneAndLock()
Gets a reference to the smart shared pointer that holds the internal scene (carefuly read introductio...
An event sent by a window upon a char pressed by the user.
CMyGLCanvas_DisplayWindow3D * m_canvas
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLubyte GLubyte GLubyte GLubyte w
void OnResize(wxSizeEvent &event)
void addTextMessage(const double x, const double y, const std::string &text, const mrpt::img::TColorf &color=mrpt::img::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.
#define THROW_EXCEPTION(msg)
mrpt::opengl::COpenGLViewport::Ptr getDefaultViewport()
A short cut for getting the "main" viewport of the scene object, it is equivalent to:
void resize(unsigned int width, unsigned int height) override
Resizes the window, stretching the image to fit into the display area.
void clearTextMessages()
Clear all text messages created with addTextMessage().
An event sent by a window upon resize.
An event sent by a CDisplayWindow3D window when an image is saved after enabling this feature with CD...
This class implements the GUI thread required for the wxWidgets-based GUI.
std::promise< void > m_windowDestroyed
This semaphore will be signaled when the wx window is destroyed.
GLuint const GLchar * name
GLAPI void GLAPIENTRY glReadBuffer(GLenum mode)
mrpt::math::CVectorFloat vector_x
void addTextMessage(const double x_frac, const double y_frac, const std::string &text, const mrpt::img::TColorf &color, const size_t unique_index, const mrpt::opengl::TOpenGLFont font)
Keeps a list of text messages which can be rendered to OpenGL contexts by graphic classes.
void useCameraFromScene(bool useIt=true)
If set to true (default = false), the mouse-based scene navigation will be disabled and the camera po...
static wxBitmap getMRPTDefaultIcon()
static int notifyWindowCreation()
Atomically increments the number of windows created with the main frame as parent.
void OnMenuClose(wxCommandEvent &event)
float getCameraAzimuthDeg() const
Get camera parameters programmatically.
static int notifyWindowDestruction()
Atomically decrements the number of windows created with the main frame as parent.
The base class for GUI window classes.
An event sent by a window upon a mouse click, giving the (x,y) pixel coordinates.
std::string str
Parameters, depending on OPCODE.
void setCameraProjective(bool isProjective)
Sets the camera as projective, or orthogonal.
bool isOpen()
Returns false if the user has already closed the window.
The data structure for each inter-thread request:
void setCameraPointingToPoint(float x, float y, float z)
Changes the camera parameters programmatically.
void unlockAccess3DScene()
Unlocks the access to the internal 3D scene.
bool getLastMousePositionRay(mrpt::math::TLine3D &ray) const
Gets the 3D ray for the direction line of the pixel where the mouse cursor is at.
float getCameraZoom() const
Get camera parameters programmatically.
std::string m_caption
The caption of the window.
mrpt::gui::CDisplayWindow3D::Ptr win
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
void captureImagesStop()
Stop image grabbing.
bool hasSubscribers() const
Can be called by a derived class before preparing an event for publishing with publishEvent to determ...
mrpt::void_ptr_noncopy m_hwnd
The window handle.
std::string grabImageGetNextFile()
Increments by one the image counter and return the next image file name (Users normally don't want to...
int OPCODE
Valid codes are: For CDisplayWindow:
void setCameraElevationDeg(float deg)
Changes the camera parameters programmatically.
mrpt::system::TTimeStamp m_lastFullScreen
A RGB color - floats in the range [0,1].
std::shared_ptr< CDisplayWindow3D > Ptr
void forceRepaint()
Repaints the window.
void setImageView_fast(mrpt::img::CImage &img)
Just like setImageView but moves the internal image memory instead of making a copy,...
GLAPI void GLAPIENTRY glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels)
Classes for creating GUI windows for 2D and 3D visualization.
std::mutex m_last_captured_img_cs
void notifyChildWindowDestruction()
Called by wx main thread to set m_hwnd to NULL.
GLenum GLsizei GLsizei height
A class for storing images as grayscale or RGB bitmaps.
CDisplayWindow3D * m_win3D
virtual ~C3DWindowDialog()
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...
unsigned int m_grab_imgs_idx
void internal_setRenderingFPS(double FPS)
Set the rendering FPS (users don't call this, the method is for internal MRPT objects only)
std::shared_ptr< CImage > Ptr
bool getLastWindowImage(mrpt::img::CImage &out_img) const
Retrieve the last captured image from the window.
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
This base provides a set of functions for maths stuff.
volatile bool m_keyPushed
3D line, represented by a base point and a director vector.
GLsizei const GLchar ** string
std::shared_ptr< COpenGLScene > Ptr
void OnChar(wxKeyEvent &event)
void setPos(int x, int y) override
Changes the position of the window on the screen.
void setCameraAzimuthDeg(float deg)
Changes the camera parameters programmatically.
A pair (x,y) of pixel coordinates (integer resolution).
void destroyWxWindow()
Must be called by child classes in their destructors.
mrpt::gui::CDisplayWindow3D * source3D
Only one of source* can be non-nullptr, indicating the class that generated the request.
An event sent by a window upon when it's about to be closed, either manually by the user or programma...
void setImageView(const mrpt::img::CImage &img)
Set the "main" viewport into "image view"-mode, where an image is efficiently drawn (fitting the view...
mrpt::img::CImage::Ptr getLastWindowImagePtr() const
Retrieve the last captured image from the window, as a smart pointer.
The namespace for 3D scene representation and rendering.
std::shared_ptr< COpenGLViewport > Ptr
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
void setMinRange(double new_min)
Changes the camera min clip range (z) (used for gluPerspective).
mrpt::img::CImage::Ptr m_last_captured_img
void grabImagesStop()
Stops image grabbing started by grabImagesStart.
~CDisplayWindow3DLocker()
Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST | |