Main MRPT website > C++ reference for MRPT 1.9.9
CFBORender.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "opengl-precomp.h" // Precompiled header
11 
12 #include <mrpt/opengl/CFBORender.h>
13 #include "opengl_internals.h"
14 
15 using namespace std;
16 using namespace mrpt;
17 using namespace mrpt::utils;
18 using namespace mrpt::opengl;
19 
20 /*---------------------------------------------------------------
21  Constructor
22 ---------------------------------------------------------------*/
23 CFBORender::CFBORender(
24  unsigned int width, unsigned int height, const bool skip_glut_window)
25  : m_width(width),
26  m_height(height),
27  m_win_used(!skip_glut_window),
28  m_default_bk_color(.6f, .6f, .6f, 1)
29 {
30 #if MRPT_HAS_OPENCV && MRPT_HAS_OPENGL_GLUT
31 
33 
34  if (m_win_used)
35  {
36  // check a previous initialization of the GLUT
37  if (!glutGet(GLUT_INIT_STATE))
38  {
39  // create the context (a little trick)
40  int argc = 1;
41  char* argv[1] = {nullptr};
42  glutInit(&argc, argv);
43  }
44 
45  // create a hidden window
46  m_win = glutCreateWindow("CFBORender");
47  glutHideWindow();
48  }
49 
50  // call after creating the hidden window
51  if (!isExtensionSupported("GL_EXT_framebuffer_object"))
52  THROW_EXCEPTION("Framebuffer Object extension unsupported");
53 
54 // In win32 we have to load the pointers to the functions:
55 #ifdef MRPT_OS_WINDOWS
57  (PFNGLGENFRAMEBUFFERSEXTPROC)wglGetProcAddress("glGenFramebuffersEXT");
59  "glDeleteFramebuffersEXT");
61  (PFNGLBINDFRAMEBUFFEREXTPROC)wglGetProcAddress("glBindFramebufferEXT");
63  (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)wglGetProcAddress(
64  "glFramebufferTexture2DEXT");
65 
66  ASSERT_(glGenFramebuffersEXT != nullptr)
68  ASSERT_(glBindFramebufferEXT != nullptr)
70 #endif
71 
72  // gen the frambuffer object (FBO), similar manner as a texture
74 
75  // bind the framebuffer, fbo, so operations will now occur on it
77 
78  // change viewport size (in pixels)
79  glViewport(0, 0, m_width, m_height);
80 
81  // make a texture
82  glGenTextures(1, &m_tex);
83 
84  // initialize texture that will store the framebuffer image
85  const GLenum texTarget =
86 #if defined(GL_TEXTURE_RECTANGLE_NV)
88 #elif defined(GL_TEXTURE_RECTANGLE_ARB)
90 #else
92 #endif
93 
94  glBindTexture(texTarget, m_tex);
96  texTarget, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE,
97  nullptr);
98 
99  // bind this texture to the current framebuffer obj. as color_attachement_0
102 
103  //'unbind' the frambuffer object, so subsequent drawing ops are not drawn
104  // into the FBO.
105  // '0' means "windowing system provided framebuffer
107 
108  MRPT_END
109 
110 //#else
111 // THROW_EXCEPTION("MRPT compiled without OpenCV and/or OpenGL support!!")
112 #endif
113 }
114 
115 /*---------------------------------------------------------------
116  Destructor:
117  ---------------------------------------------------------------*/
119 {
120 #if MRPT_HAS_OPENGL_GLUT
121  // delete the current texture, the framebuffer object and the GLUT window
122  glDeleteTextures(1, &m_tex);
124  if (m_win_used) glutDestroyWindow(m_win);
125 #endif
126 }
127 
128 /*---------------------------------------------------------------
129  Set the scene camera
130  ---------------------------------------------------------------*/
131 void CFBORender::setCamera(const COpenGLScene& scene, const CCamera& camera)
132 {
133  MRPT_START
134 
135  scene.getViewport("main")->getCamera() = camera;
136 
137  MRPT_END
138 }
139 
140 /*---------------------------------------------------------------
141  Get the scene camera
142  ---------------------------------------------------------------*/
144 {
145  MRPT_START
146 
147  return scene.getViewport("main")->getCamera();
148 
149  MRPT_END
150 }
151 
152 /*---------------------------------------------------------------
153  Render the scene and get the rendered rgb image. This
154  function resizes the image buffer if it is necessary
155  ---------------------------------------------------------------*/
157 {
158 #if MRPT_HAS_OPENCV && MRPT_HAS_OPENGL_GLUT
159 
160  MRPT_START
161 
162  // resize the buffer if it is necessary
163  if (buffer.getWidth() != static_cast<size_t>(m_width) ||
164  buffer.getHeight() != static_cast<size_t>(m_height) ||
165  buffer.getChannelCount() != 3 || buffer.isOriginTopLeft() != false)
166  {
167  buffer.resize(m_width, m_height, 3, false);
168  }
169 
170  // Go on.
171  getFrame2(scene, buffer);
172  ;
173 
174  MRPT_END
175 #else
176  MRPT_UNUSED_PARAM(scene);
178 #endif
179 }
180 
181 /*---------------------------------------------------------------
182  Render the scene and get the rendered rgb image. This
183  function does not resize the image buffer.
184  ---------------------------------------------------------------*/
186 {
187 #if MRPT_HAS_OPENGL_GLUT
188 
189  MRPT_START
190 
191  // check the buffer size
192  ASSERT_EQUAL_(buffer.getWidth(), static_cast<size_t>(m_width))
193  ASSERT_EQUAL_(buffer.getHeight(), static_cast<size_t>(m_height))
194  ASSERT_EQUAL_(buffer.getChannelCount(), 3)
195  ASSERT_EQUAL_(buffer.isOriginTopLeft(), false)
196 
197  // bind the framebuffer, fbo, so operations will now occur on it
199 
200  glClearColor(
203 
204  // Render opengl objects:
205  // ---------------------------
206  scene.render();
207 
208  // If any, draw the 2D text messages:
209  // ----------------------------------
211 
212  // TODO NOTE: This should fail if the image has padding bytes. See
213  // glPixelStore() etc.
214  glReadPixels(
216 
217  //'unbind' the frambuffer object, so subsequent drawing ops are not drawn
218  // into the FBO.
219  // '0' means "windowing system provided framebuffer
221 
222  MRPT_END
223 #else
224  MRPT_UNUSED_PARAM(scene);
226 #endif
227 }
228 
229 /*---------------------------------------------------------------
230  Resize the image size
231  ---------------------------------------------------------------*/
232 void CFBORender::resize(unsigned int width, unsigned int height)
233 {
234 #if MRPT_HAS_OPENCV && MRPT_HAS_OPENGL_GLUT
235 
236  MRPT_START
237 
238  // update members
239  m_width = width;
240  m_height = height;
241 
242  // bind the framebuffer, fbo, so operations will now occur on it
244 
245  // change viewport size (in pixels)
246  glViewport(0, 0, m_width, m_height);
247 
248  // change texture size
249  const GLenum texTarget =
250 #if defined(GL_TEXTURE_RECTANGLE_NV)
252 #elif defined(GL_TEXTURE_RECTANGLE_ARB)
254 #else
256 #endif
257 
258  glBindTexture(texTarget, m_tex);
259  glTexImage2D(
260  texTarget, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE,
261  nullptr);
262 
263  //'unbind' the frambuffer object, so subsequent drawing ops are not drawn
264  // into the FBO.
265  // '0' means "windowing system provided framebuffer
267 
268  MRPT_END
269 
270 //#else
271 // THROW_EXCEPTION("MRPT compiled without OpenCV and/or OpenGL support!!")
272 #else
275 #endif
276 }
277 
278 /*---------------------------------------------------------------
279  Provide information on Framebuffer object extension
280  ---------------------------------------------------------------*/
281 int CFBORender::isExtensionSupported(const char* extension)
282 {
283 #if MRPT_HAS_OPENGL_GLUT
284 
285  MRPT_START
286 
287  const GLubyte* extensions = nullptr;
288  const GLubyte* start;
289  GLubyte *where, *terminator;
290 
291  /* Extension names should not have spaces. */
292  where = (GLubyte*)strchr(extension, ' ');
293  if (where || *extension == '\0') return 0;
294  extensions = glGetString(GL_EXTENSIONS);
295 
296  /* It takes a bit of care to be fool-proof about parsing the
297  OpenGL extensions string. Don't be fooled by sub-strings,
298  etc. */
299  start = extensions;
300  for (;;)
301  {
302  where = (GLubyte*)strstr((const char*)start, extension);
303  if (!where) break;
304  terminator = where + strlen(extension);
305  if (where == start || *(where - 1) == ' ')
306  if (*terminator == ' ' || *terminator == '\0') return 1;
307  start = terminator;
308  }
309 
310  MRPT_END
311 
312 //#else
313 // THROW_EXCEPTION("MRPT compiled without OpenGL support!!")
314 #else
315  MRPT_UNUSED_PARAM(extension);
316 #endif
317 
318  return 0;
319 }
#define ASSERT_EQUAL_(__A, __B)
GLAPI void GLAPIENTRY glDeleteTextures(GLsizei n, const GLuint *textures)
GLAPI void GLAPIENTRY glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
int isExtensionSupported(const char *extension)
Provide information on Framebuffer object extension.
Definition: CFBORender.cpp:281
#define GL_EXTENSIONS
Definition: glew.h:643
GLAPI void GLAPIENTRY glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:118
GLuint buffer
Definition: glext.h:3917
#define THROW_EXCEPTION(msg)
void(GLAPIENTRY * PFNGLGENFRAMEBUFFERSEXTPROC)(GLsizei n, GLuint *framebuffers)
Definition: glew.h:6744
void getFrame2(const COpenGLScene &scene, mrpt::utils::CImage &image)
Render the scene and get the rendered rgb image.
Definition: CFBORender.cpp:185
#define GL_TEXTURE_RECTANGLE_NV
Definition: glew.h:9954
STL namespace.
#define GL_UNSIGNED_BYTE
Definition: glew.h:302
mrpt::utils::TColorf m_default_bk_color
Definition: CFBORender.h:86
void(GLAPIENTRY * PFNGLDELETEFRAMEBUFFERSEXTPROC)(GLsizei n, const GLuint *framebuffers)
Definition: glew.h:6728
GLenum GLsizei width
Definition: glext.h:3531
GLAPI void GLAPIENTRY glBindTexture(GLenum target, GLuint texture)
#define GL_BGR_EXT
Definition: glew.h:5354
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
CCamera & getCamera(const COpenGLScene &scene)
Get a reference to the scene camera.
Definition: CFBORender.cpp:143
#define GL_RGB
Definition: glew.h:623
virtual ~CFBORender()
Destructor.
Definition: CFBORender.cpp:118
#define glDeleteFramebuffersEXT
Definition: glew.h:6762
void render_text_messages(const int w, const int h) const
Renders the messages to the current opengl rendering context (to be called OUT of MRPT mrpt::opengl r...
#define GL_COLOR_ATTACHMENT0_EXT
Definition: glew.h:6689
unsigned int GLenum
Definition: glew.h:206
#define MRPT_START
#define GL_TEXTURE_RECTANGLE_EXT
Definition: glew.h:8033
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLAPI void GLAPIENTRY glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
COpenGLViewport::Ptr getViewport(const std::string &viewportName=std::string("main")) const
Returns the viewport with the given name, or nullptr if it does not exist; note that the default view...
void setCamera(const COpenGLScene &scene, const CCamera &camera)
Change the scene camera.
Definition: CFBORender.cpp:131
void resize(unsigned int width, unsigned int height)
Resize the rendering canvas size.
Definition: CFBORender.cpp:232
GLAPI const GLubyte *GLAPIENTRY glGetString(GLenum name)
GLAPI void GLAPIENTRY glGenTextures(GLsizei n, GLuint *textures)
#define glFramebufferTexture2DEXT
Definition: glew.h:6767
#define glGenFramebuffersEXT
Definition: glew.h:6769
void render() const
Render this scene.
#define GL_FRAMEBUFFER_EXT
Definition: glew.h:6707
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives...
Definition: COpenGLScene.h:60
#define ASSERT_(f)
void(GLAPIENTRY * PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
Definition: glew.h:6738
#define glBindFramebufferEXT
Definition: glew.h:6758
#define GL_TEXTURE_RECTANGLE_ARB
Definition: glew.h:4092
GLuint start
Definition: glext.h:3528
A camera: if added to a scene, the viewpoint defined by this camera will be used instead of the camer...
Definition: CCamera.h:30
GLenum GLsizei GLsizei height
Definition: glext.h:3554
void(GLAPIENTRY * PFNGLBINDFRAMEBUFFEREXTPROC)(GLenum target, GLuint framebuffer)
Definition: glew.h:6723
unsigned char GLubyte
Definition: glew.h:214
GLAPI void GLAPIENTRY glTexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
void getFrame(const COpenGLScene &scene, mrpt::utils::CImage &image)
Render the scene and get the rendered rgb image.
Definition: CFBORender.cpp:156



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019