12 #include <mrpt/config.h>
32 #if MRPT_HAS_OPENGL_GLUT
33 #ifdef MRPT_OS_WINDOWS
39 #include <OpenGL/gl.h>
40 #include <OpenGL/glu.h>
41 #include <GLUT/glut.h>
46 #ifdef HAVE_FREEGLUT_EXT_H
47 #include <GL/freeglut_ext.h>
52 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
55 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild wxWidgets"
65 class CMyGLCanvas_DisplayWindow3D :
public mrpt::gui::CMyGLCanvasBase
69 wxWindow *parent, wxWindowID
id = wxID_ANY,
70 const wxPoint& pos = wxDefaultPosition,
71 const wxSize&
size = wxDefaultSize,
72 long style = 0,
const wxString&
name = _T(
"CMyGLCanvas_DisplayWindow3D") );
74 virtual ~CMyGLCanvas_DisplayWindow3D();
83 void render_text_messages_public(
const int w,
const int h)
const { 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(
CDisplayWindow3D *m_win3D, wxKeyEvent&ev);
100 CMyGLCanvas_DisplayWindow3D::CMyGLCanvas_DisplayWindow3D(
102 wxWindow *parent, wxWindowID
id,
103 const wxPoint& pos,
const wxSize&
size,
long style,
const wxString&
name)
104 : CMyGLCanvasBase(parent,
id,pos,
size,style,
name)
107 Connect(wxEVT_CHAR,(wxObjectEventFunction)&CMyGLCanvas_DisplayWindow3D::OnCharCustom);
108 Connect(wxEVT_CHAR_HOOK,(wxObjectEventFunction)&CMyGLCanvas_DisplayWindow3D::OnCharCustom);
110 Connect(wxEVT_LEFT_DOWN,(wxObjectEventFunction)&CMyGLCanvas_DisplayWindow3D::OnMouseDown);
111 Connect(wxEVT_RIGHT_DOWN,(wxObjectEventFunction)&CMyGLCanvas_DisplayWindow3D::OnMouseDown);
112 Connect(wxEVT_MOTION, (wxObjectEventFunction)&CMyGLCanvas_DisplayWindow3D::OnMouseMove);
115 void CMyGLCanvas_DisplayWindow3D::display3D_processKeyEvent(
CDisplayWindow3D *m_win3D, wxKeyEvent&ev)
124 cout <<
"[CDisplayWindow3D] Switching fullscreen...\n";
128 win->ShowFullScreen( !win->IsFullScreen() );
137 const int code = ev.GetKeyCode();
152 void CMyGLCanvas_DisplayWindow3D::OnCharCustom( wxKeyEvent& ev )
154 CMyGLCanvas_DisplayWindow3D::display3D_processKeyEvent(m_win3D, ev);
157 void CMyGLCanvas_DisplayWindow3D::OnMouseDown(wxMouseEvent& event)
170 void CMyGLCanvas_DisplayWindow3D::OnMouseMove(wxMouseEvent& event)
184 CMyGLCanvas_DisplayWindow3D::~CMyGLCanvas_DisplayWindow3D()
186 m_openGLScene.clear_unique();
189 void CMyGLCanvas_DisplayWindow3D::OnPreRender()
191 if (m_openGLScene) m_openGLScene.clear_unique();
195 m_openGLScene = ptrScene;
198 void CMyGLCanvas_DisplayWindow3D::OnPostRender()
201 m_openGLScene.clear_unique();
206 this->GetSize(&
w,&h);
208 m_text_msgs.render_text_messages_public(
w,h);
211 void CMyGLCanvas_DisplayWindow3D::OnPostRenderSwapBuffers(
double At, wxPaintDC &dc)
225 CImagePtr frame(
new CImage(
w, h, 3,
false));
229 if (!grabFile.empty())
231 frame->saveToFile(grabFile);
240 frame.clear_unique();
248 #if MRPT_HAS_WXWIDGETS
262 const std::
string &caption,
263 wxSize initialSize ) :
267 #if MRPT_HAS_OPENGL_GLUT
269 Create(parent,
id,
_U(caption.c_str()), wxDefaultPosition, initialSize, wxDEFAULT_FRAME_STYLE, _T(
"id"));
276 m_canvas =
new CMyGLCanvas_DisplayWindow3D( win3D,
this, wxID_ANY, wxDefaultPosition, wxDefaultSize );
280 Connect(wxID_ANY,wxEVT_CLOSE_WINDOW,(wxObjectEventFunction)&C3DWindowDialog::OnClose);
281 Connect(
ID_MENUITEM1,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&C3DWindowDialog::OnMenuClose);
282 Connect(
ID_MENUITEM2,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&C3DWindowDialog::OnMenuAbout);
284 Connect(wxID_ANY,wxEVT_CHAR,(wxObjectEventFunction)&C3DWindowDialog::OnChar);
286 Connect(wxID_ANY,wxEVT_SIZE,(wxObjectEventFunction)&C3DWindowDialog::OnResize);
290 WxSubsystem::CWXMainFrame::notifyWindowCreation();
299 C3DWindowDialog::~C3DWindowDialog()
308 bool allow_close=
true;
314 if (!allow_close)
return;
337 ::wxMessageBox(_(
"3D Scene viewer\n Class gui::CDisplayWindow3D\n MRPT C++ library"),_(
"About..."));
342 #if MRPT_HAS_OPENGL_GLUT
343 CMyGLCanvas_DisplayWindow3D::display3D_processKeyEvent(
m_win3D, ev);
349 #if MRPT_HAS_OPENGL_GLUT
364 #if MRPT_HAS_OPENGL_GLUT
365 m_canvas->m_text_msgs.clearTextMessages();
374 const size_t unique_index,
378 #if MRPT_HAS_OPENGL_GLUT
379 m_canvas->m_text_msgs.addTextMessage( x_frac, y_frac, text,
color,unique_index,font );
389 const double font_size,
391 const size_t unique_index,
392 const double font_spacing,
393 const double font_kerning,
394 const bool has_shadow,
398 #if MRPT_HAS_OPENGL_GLUT
399 m_canvas->m_text_msgs.addTextMessage( x_frac, y_frac, text,
color, font_name, font_size, font_style, unique_index, font_spacing, font_kerning,has_shadow,shadow_color );
412 unsigned int initialWindowWidth,
413 unsigned int initialWindowHeight )
416 m_grab_imgs_prefix(),
418 m_is_capturing_imgs(false),
419 m_last_captured_img_cs(
"m_last_captured_img_cs"),
420 m_lastFullScreen (
mrpt::system::
now()),
432 unsigned int initialWindowWidth,
433 unsigned int initialWindowHeight )
435 return CDisplayWindow3DPtr(
new CDisplayWindow3D(windowCaption,initialWindowWidth,initialWindowHeight));
457 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
460 cerr <<
"[CDisplayWindow3D::setPos] Window closed!: " <<
m_caption << endl;
482 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
485 cerr <<
"[CDisplayWindow3D::setPos] Window closed!: " <<
m_caption << endl;
506 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
509 cerr <<
"[CDisplayWindow3D::setWindowTitle] Window closed!: " <<
m_caption << endl;
547 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
568 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
571 win->
m_canvas->cameraElevationDeg = deg;
579 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
582 win->
m_canvas->useCameraFromScene = useIt;
593 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
596 win->
m_canvas->cameraAzimuthDeg = deg;
607 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
625 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
628 win->
m_canvas->cameraZoomDistance = zoom;
639 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
642 win->
m_canvas->cameraIsProjective = isProjective;
652 mrpt::opengl::COpenGLViewportPtr gl_view =
m_3Dscene->getViewport(
"main");
656 gl_view->getViewportClipDistances(m,M);
657 gl_view->setViewportClipDistances(new_min,M);
665 mrpt::opengl::COpenGLViewportPtr gl_view =
m_3Dscene->getViewport(
"main");
669 gl_view->getViewportClipDistances(m,M);
670 gl_view->setViewportClipDistances(m,new_max);
677 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
687 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
699 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
701 return win ? win->
m_canvas->cameraElevationDeg : 0;
712 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
714 return win ? win->
m_canvas->cameraAzimuthDeg : 0;
725 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
745 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
747 return win ? win->
m_canvas->cameraZoomDistance : 0;
758 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
760 return win ? win->
m_canvas->cameraIsProjective :
true;
771 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
773 if (!win)
return false;
791 m_3Dscene->getViewport(
"main")->get3DRayForPixelCoord(
x,
y,ray);
804 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
807 win->
m_canvas->SetCursor( *(cursorIsCross ? wxCROSS_CURSOR : wxSTANDARD_CURSOR) );
894 const size_t unique_index,
898 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
916 REQ->
y = int(unique_index);
935 const double font_size,
937 const size_t unique_index,
938 const double font_spacing,
939 const double font_kerning,
940 const bool draw_shadow,
944 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
964 REQ->
vector_x[8] = draw_shadow ? 1:0;
969 REQ->
x = int(font_style);
970 REQ->
y = int(unique_index);
995 #if MRPT_HAS_WXWIDGETS && MRPT_HAS_OPENGL_GLUT
1010 const double ALPHA = 0.99;
1025 mrpt::opengl::COpenGLViewportPtr view =
m_3Dscene->getViewport(
"main");
1033 mrpt::opengl::COpenGLViewportPtr view =
m_3Dscene->getViewport(
"main");
1034 view->setImageView(
img);
1041 mrpt::opengl::COpenGLViewportPtr view =
m_3Dscene->getViewport(
"main");
1042 view->setImageView_fast(
img);
#define IMPLEMENTS_MRPT_OBJECT(class_name, base, NameSpace)
This must be inserted in all CObject classes implementation files.
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.
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. The code cannot be put into this class' destruc...
synch::CSemaphore m_semWindowDestroyed
This semaphore will be signaled when the wx window is destroyed.
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.
virtual bool getLastMousePosition(int &x, int &y) const MRPT_OVERRIDE
Gets the last x,y pixel coordinates of the mouse.
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 setWindowTitle(const std::string &str) MRPT_OVERRIDE
Changes the window title.
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,...
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. forceRepaint, repaint and updateWindow are all aliases of the same method.
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.
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). The window is not updated wi...
void setCameraProjective(bool isProjective)
Sets the camera as projective, or orthogonal.
void resize(unsigned int width, unsigned int height) MRPT_OVERRIDE
Resizes the window, stretching the image to fit into the display area.
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.
void setCameraAzimuthDeg(float deg)
Changes the camera parameters programmatically.
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.
mrpt::opengl::COpenGLViewportPtr getDefaultViewport()
A short cut for getting the "main" viewport of the scene object, it is equivalent to:
synch::CCriticalSection m_last_captured_img_cs
void setMaxRange(double new_max)
Changes the camera max clip range (z) (used for gluPerspective. The window is not updated with this m...
float getFOV() const
Return the camera field of view (in degrees) (used for gluPerspective)
mrpt::opengl::COpenGLScenePtr & get3DSceneAndLock()
Gets a reference to the smart shared pointer that holds the internal scene (carefuly read introductio...
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...
virtual void setCursorCross(bool cursorIsCross) MRPT_OVERRIDE
Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true)
static CDisplayWindow3DPtr Create()
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). The window is not updated with this ...
float getCameraZoom() const
Get camera parameters programmatically.
mrpt::utils::CImagePtr m_last_captured_img
void clearTextMessages()
Clear all text messages created with addTextMessage().
void setPos(int x, int y) MRPT_OVERRIDE
Changes the position of the window on the screen.
mrpt::opengl::COpenGLScenePtr m_3Dscene
Internal OpenGL object (see general discussion in about usage of this object)
mrpt::synch::CCriticalSectionRecursive m_csAccess3DScene
Critical section for accesing m_3Dscene.
void grabImagesStop()
Stops image grabbing started by grabImagesStart.
mrpt::utils::CImagePtr getLastWindowImagePtr() const
Retrieve the last captured image from the window, as a smart pointer.
void setCameraElevationDeg(float deg)
Changes the camera parameters programmatically.
void captureImagesStop()
Stop image grabbing.
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::COpenGLScenePtr &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 programat...
An event sent by a window upon resize.
Keeps a list of text messages which can be rendered to OpenGL contexts by graphic classes.
A class acquiring a CCriticalSection at its constructor, and releasing it at destructor.
void leave() const MRPT_OVERRIDE
Leave.
void enter() const MRPT_OVERRIDE
Enter.
void release(unsigned int increaseCount=1)
Increments the count of the semaphore by a given amount.
A class for storing images as grayscale or RGB bitmaps.
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 BASE_IMPEXP 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 BASE_IMPEXP 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-NULL, 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).