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(
272 #endif // Wx + OpenGL 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);
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);
429 #endif // MRPT_HAS_WXWIDGETS 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);
An event sent by a window upon resize.
virtual bool getLastMousePosition(int &x, int &y) const override
Gets the last x,y pixel coordinates of the mouse.
virtual ~CDisplayWindow3D()
Destructor.
float getCameraAzimuthDeg() const
Get camera parameters programmatically.
bool getLastMousePositionRay(mrpt::math::TLine3D &ray) const
Gets the 3D ray for the direction line of the pixel where the mouse cursor is at. ...
static void pushPendingWxRequest(TRequestToWxMainThread *data)
Thread-safe method to insert a new pending request (The memory must be dinamically allocated with "ne...
An event sent by a CDisplayWindow3D window when an image is saved after enabling this feature with CD...
void OnMenuAbout(wxCommandEvent &event)
void unlockAccess3DScene()
Unlocks the access to the internal 3D scene.
GLAPI void GLAPIENTRY glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels)
An event sent by a window upon a mouse click, giving the (x,y) pixel coordinates. ...
std::recursive_mutex m_csAccess3DScene
Critical section for accesing m_3Dscene.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
std::shared_ptr< COpenGLViewport > Ptr
void OnClose(wxCloseEvent &event)
mrpt::gui::CDisplayWindow3D * source3D
Only one of source* can be non-nullptr, indicating the class that generated the request.
mrpt::utils::CImage::Ptr getLastWindowImagePtr() const
Retrieve the last captured image from the window, as a smart pointer.
The data structure for each inter-thread request:
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...
bool isCapturingImgs() const
void setWindowTitle(const std::string &str) override
Changes the window title.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
std::string m_caption
The caption of the window.
mrpt::opengl::COpenGLScene::Ptr & get3DSceneAndLock()
Gets a reference to the smart shared pointer that holds the internal scene (carefuly read introductio...
A class for storing images as grayscale or RGB bitmaps.
void grabImagesStart(const std::string &grab_imgs_prefix=std::string("video_"))
Start to save rendered images to disk.
static int notifyWindowCreation()
Atomically increments the number of windows created with the main frame as parent.
void resize(unsigned int width, unsigned int height) override
Resizes the window, stretching the image to fit into the display area.
Keeps a list of text messages which can be rendered to OpenGL contexts by graphic classes...
#define THROW_EXCEPTION(msg)
A pair (x,y) of pixel coordinates (integer resolution).
void setCameraPointingToPoint(float x, float y, float z)
Changes the camera parameters programmatically.
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
void OnMenuClose(wxCommandEvent &event)
static wxBitmap getMRPTDefaultIcon()
~CDisplayWindow3DLocker()
virtual ~C3DWindowDialog()
GLAPI void GLAPIENTRY glReadBuffer(GLenum mode)
CDisplayWindow3D * m_win3D
static CDisplayWindow3D::Ptr Create(const std::string &windowCaption, unsigned int initialWindowWidth=400, unsigned int initialWindowHeight=300)
Class factory returning a smart pointer.
void internal_emitGrabImageEvent(const std::string &fil)
called by CMyGLCanvas_DisplayWindow3D::OnPostRenderSwapBuffers
bool hasSubscribers() const
Can be called by a derived class before preparing an event for publishing with publishEvent to determ...
void setCameraProjective(bool isProjective)
Sets the camera as projective, or orthogonal.
float getCameraZoom() const
Get camera parameters programmatically.
void setImageView_fast(mrpt::utils::CImage &img)
Just like setImageView but moves the internal image memory instead of making a copy, so it's faster but empties the input image.
void OnResize(wxSizeEvent &event)
int OPCODE
Valid codes are: For CDisplayWindow:
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 when the mouse is moved over it.
GLubyte GLubyte GLubyte GLubyte w
void OnChar(wxKeyEvent &event)
void setImageView(const mrpt::utils::CImage &img)
Set the "main" viewport into "image view"-mode, where an image is efficiently drawn (fitting the view...
void notifyChildWindowDestruction()
Called by wx main thread to set m_hwnd to NULL.
mrpt::opengl::COpenGLViewport::Ptr getDefaultViewport()
A short cut for getting the "main" viewport of the scene object, it is equivalent to: ...
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...
This base provides a set of functions for maths stuff.
void getCameraPointingToPoint(float &x, float &y, float &z) const
Get camera parameters programmatically.
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.
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
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)
void setPos(int x, int y) override
Changes the position of the window on the screen.
std::string grabImageGetNextFile()
Increments by one the image counter and return the next image file name (Users normally don't want to...
bool getLastWindowImage(mrpt::utils::CImage &out_img) const
Retrieve the last captured image from the window.
TOpenGLFontStyle
Different style for vectorized font rendering.
bool isOpen()
Returns false if the user has already closed the window.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
unsigned int m_grab_imgs_idx
std::shared_ptr< CImage > Ptr
std::shared_ptr< CDisplayWindow3D > Ptr
std::string m_grab_imgs_prefix
An event sent by a window upon a char pressed by the user.
GLsizei const GLchar ** string
mrptKeyModifier keyEventToMrptKeyModifier(const wxKeyEvent &ev)
Extracts the key modifiers from a wxKeyEvent.
mrpt::utils::CImage::Ptr m_last_captured_img
volatile mrptKeyModifier m_keyPushedModifier
void forceRepaint()
Repaints the window.
float getFOV() const
Return the camera field of view (in degrees) (used for gluPerspective)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::system::TTimeStamp m_lastFullScreen
This class implements the GUI thread required for the wxWidgets-based GUI.
void grabImagesStop()
Stops image grabbing started by grabImagesStart.
volatile bool m_keyPushed
std::shared_ptr< COpenGLScene > Ptr
bool isCameraProjective() const
Sets the camera as projective, or orthogonal.
void setCameraZoom(float zoom)
Changes the camera parameters programmatically.
The namespace for 3D scene representation and rendering.
GLuint const GLchar * name
A RGB color - floats in the range [0,1].
virtual void setCursorCross(bool cursorIsCross) override
Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) ...
void destroyWxWindow()
Must be called by child classes in their destructors.
void setMinRange(double new_min)
Changes the camera min clip range (z) (used for gluPerspective).
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
Classes for creating GUI windows for 2D and 3D visualization.
void createWxWindow(unsigned int initialWidth, unsigned int initialHeight)
Must be called by child classes just within the constructor.
mrpt::math::CVectorFloat vector_x
void captureImagesStop()
Stop image grabbing.
CDisplayWindow3D(const std::string &windowCaption=std::string(), unsigned int initialWindowWidth=400, unsigned int initialWindowHeight=300)
Constructor.
volatile int m_keyPushedCode
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...
GLenum GLsizei GLsizei height
std::string str
Parameters, depending on OPCODE.
void internal_setRenderingFPS(double FPS)
Set the rendering FPS (users don't call this, the method is for internal MRPT objects only) ...
std::promise< void > m_windowDestroyed
This semaphore will be signaled when the wx window is destroyed.
void clearTextMessages()
Clear all text messages created with addTextMessage().
std::mutex m_last_captured_img_cs
mrpt::utils::void_ptr_noncopy m_hwnd
The window handle.
mrpt::opengl::COpenGLScene::Ptr m_3Dscene
Internal OpenGL object (see general discussion in about usage of this object)
The base class for GUI window classes.
void setFOV(float v)
Changes the camera field of view (in degrees) (used for gluPerspective).
static int notifyWindowDestruction()
Atomically decrements the number of windows created with the main frame as parent.
void setCameraAzimuthDeg(float deg)
Changes the camera parameters programmatically.
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.
void setCameraElevationDeg(float deg)
Changes the camera parameters programmatically.
CMyGLCanvas_DisplayWindow3D * m_canvas
void useCameraFromScene(bool useIt=true)
If set to true (default = false), the mouse-based scene navigation will be disabled and the camera po...
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
void captureImagesStart()
Enables the grabbing of CImage objects from screenshots of the window.
3D line, represented by a base point and a director vector.
float getCameraElevationDeg() const
Get camera parameters programmatically.