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();
246 #endif // Wx + OpenGL 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 );
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 );
405 #endif // MRPT_HAS_WXWIDGETS 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);
An event sent by a window upon resize.
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. ...
mrpt::utils::CImagePtr m_last_captured_img
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)
A class acquiring a CCriticalSection at its constructor, and releasing it at destructor.
An event sent by a window upon a mouse click, giving the (x,y) pixel coordinates. ...
mrpt::opengl::COpenGLScenePtr m_3Dscene
Internal OpenGL object (see general discussion in about usage of this object)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
void enter() const MRPT_OVERRIDE
Enter.
void OnClose(wxCloseEvent &event)
mrpt::gui::CDisplayWindow3D * source3D
Only one of source* can be non-NULL, indicating the class that generated the request.
The data structure for each inter-thread request:
bool isCapturingImgs() const
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
#define IMPLEMENTS_MRPT_OBJECT(class_name, base, NameSpace)
This must be inserted in all CObject classes implementation files.
std::string m_caption
The caption of the window.
mrpt::opengl::COpenGLViewportPtr getDefaultViewport()
A short cut for getting the "main" viewport of the scene object, it is equivalent to: ...
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.
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...
virtual bool getLastMousePosition(int &x, int &y) const MRPT_OVERRIDE
Gets the last x,y pixel coordinates of the mouse.
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
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:
mrpt::synch::CCriticalSectionRecursive m_csAccess3DScene
Critical section for accesing m_3Dscene.
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 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.
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...
mrpt::utils::CImagePtr getLastWindowImagePtr() const
Retrieve the last captured image from the window, as a smart pointer.
mrpt::opengl::COpenGLScenePtr & get3DSceneAndLock()
Gets a reference to the smart shared pointer that holds the internal scene (carefuly read introductio...
This base provides a set of functions for maths stuff.
static CDisplayWindow3DPtr Create()
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. The window is not updated with this m...
#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)
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.
virtual void setCursorCross(bool cursorIsCross) MRPT_OVERRIDE
Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) ...
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
unsigned int m_grab_imgs_idx
void release(unsigned int increaseCount=1)
Increments the count of the semaphore by a given amount.
std::string m_grab_imgs_prefix
void setPos(int x, int y) MRPT_OVERRIDE
Changes the position of the window on the screen.
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.
volatile mrptKeyModifier m_keyPushedModifier
void forceRepaint()
Repaints the window. forceRepaint, repaint and updateWindow are all aliases of the same method...
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.
void setWindowTitle(const std::string &str) MRPT_OVERRIDE
Changes the window title.
mrpt::system::TTimeStamp m_lastFullScreen
This class implements the GUI thread required for the wxWidgets-based GUI.
synch::CCriticalSection m_last_captured_img_cs
void grabImagesStop()
Stops image grabbing started by grabImagesStart.
volatile bool m_keyPushed
synch::CSemaphore m_semWindowDestroyed
This semaphore will be signaled when the wx window is destroyed.
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].
void destroyWxWindow()
Must be called by child classes in their destructors. The code cannot be put into this class' destruc...
void setMinRange(double new_min)
Changes the camera min clip range (z) (used for gluPerspective). The window is not updated with this ...
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 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...
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) ...
void clearTextMessages()
Clear all text messages created with addTextMessage().
void resize(unsigned int width, unsigned int height) MRPT_OVERRIDE
Resizes the window, stretching the image to fit into the display area.
mrpt::utils::void_ptr_noncopy m_hwnd
The window handle.
void leave() const MRPT_OVERRIDE
Leave.
The base class for GUI window classes.
void setFOV(float v)
Changes the camera field of view (in degrees) (used for gluPerspective). The window is not updated wi...
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.