MRPT  1.9.9
CFBORender.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://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::opengl;
18 using mrpt::img::CImage;
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 _WIN32
57  (PFNGLGENFRAMEBUFFERSEXTPROC)wglGetProcAddress("glGenFramebuffersEXT");
59  "glDeleteFramebuffersEXT");
61  (PFNGLBINDFRAMEBUFFEREXTPROC)wglGetProcAddress("glBindFramebufferEXT");
63  (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)wglGetProcAddress(
64  "glFramebufferTexture2DEXT");
65 
66  ASSERT_(glGenFramebuffersEXT != nullptr);
67  ASSERT_(glDeleteFramebuffersEXT != 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  {
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  // bind the framebuffer, fbo, so operations will now occur on it
198 
199  glClearColor(
202 
203  // Render opengl objects:
204  // ---------------------------
205  scene.render();
206 
207  // If any, draw the 2D text messages:
208  // ----------------------------------
210 
211  // TODO NOTE: This should fail if the image has padding bytes. See
212  // glPixelStore() etc.
213  glReadPixels(
215 
216  //'unbind' the frambuffer object, so subsequent drawing ops are not drawn
217  // into the FBO.
218  // '0' means "windowing system provided framebuffer
220 
221  MRPT_END
222 #else
223  MRPT_UNUSED_PARAM(scene);
225 #endif
226 }
227 
228 /*---------------------------------------------------------------
229  Resize the image size
230  ---------------------------------------------------------------*/
231 void CFBORender::resize(unsigned int width, unsigned int height)
232 {
233 #if MRPT_HAS_OPENCV && MRPT_HAS_OPENGL_GLUT
234 
235  MRPT_START
236 
237  // update members
238  m_width = width;
239  m_height = height;
240 
241  // bind the framebuffer, fbo, so operations will now occur on it
243 
244  // change viewport size (in pixels)
245  glViewport(0, 0, m_width, m_height);
246 
247  // change texture size
248  const GLenum texTarget =
249 #if defined(GL_TEXTURE_RECTANGLE_NV)
251 #elif defined(GL_TEXTURE_RECTANGLE_ARB)
253 #else
255 #endif
256 
257  glBindTexture(texTarget, m_tex);
258  glTexImage2D(
259  texTarget, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE,
260  nullptr);
261 
262  //'unbind' the frambuffer object, so subsequent drawing ops are not drawn
263  // into the FBO.
264  // '0' means "windowing system provided framebuffer
266 
267  MRPT_END
268 
269 //#else
270 // THROW_EXCEPTION("MRPT compiled without OpenCV and/or OpenGL support!!");
271 #else
274 #endif
275 }
276 
277 /*---------------------------------------------------------------
278  Provide information on Framebuffer object extension
279  ---------------------------------------------------------------*/
280 int CFBORender::isExtensionSupported(const char* extension)
281 {
282 #if MRPT_HAS_OPENGL_GLUT
283 
284  MRPT_START
285 
286  /* Extension names should not have spaces. */
287  auto where = strchr(extension, ' ');
288  if (where || *extension == '\0') return 0;
289  const auto extensions = glGetString(GL_EXTENSIONS);
290 
291  /* It takes a bit of care to be fool-proof about parsing the
292  OpenGL extensions string. Don't be fooled by sub-strings,
293  etc. */
294  auto start = reinterpret_cast<const char*>(extensions);
295  for (;;)
296  {
297  where = strstr(start, extension);
298  if (!where) break;
299  auto terminator = where + strlen(extension);
300  if (where == start || *(where - 1) == ' ')
301  if (*terminator == ' ' || *terminator == '\0') return 1;
302  start = terminator;
303  }
304 
305  MRPT_END
306 #else
307  MRPT_UNUSED_PARAM(extension);
308 #endif
309 
310  return 0;
311 }
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)
#define MRPT_START
Definition: exceptions.h:241
int isExtensionSupported(const char *extension)
Provide information on Framebuffer object extension.
Definition: CFBORender.cpp:280
#define GL_EXTENSIONS
Definition: glew.h:644
GLAPI void GLAPIENTRY glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
GLuint buffer
Definition: glext.h:3928
void(GLAPIENTRY * PFNGLGENFRAMEBUFFERSEXTPROC)(GLsizei n, GLuint *framebuffers)
Definition: glew.h:6984
#define GL_TEXTURE_RECTANGLE_NV
Definition: glew.h:10341
void getFrame(const COpenGLScene &scene, mrpt::img::CImage &image)
Render the scene and get the rendered rgb image.
Definition: CFBORender.cpp:156
STL namespace.
#define GL_UNSIGNED_BYTE
Definition: glew.h:303
void(GLAPIENTRY * PFNGLDELETEFRAMEBUFFERSEXTPROC)(GLsizei n, const GLuint *framebuffers)
Definition: glew.h:6968
GLenum GLsizei width
Definition: glext.h:3535
GLAPI void GLAPIENTRY glBindTexture(GLenum target, GLuint texture)
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
mrpt::img::CImage CImage
Definition: utils/CImage.h:5
#define GL_BGR_EXT
Definition: glew.h:5555
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
Definition: exceptions.h:137
CCamera & getCamera(const COpenGLScene &scene)
Get a reference to the scene camera.
Definition: CFBORender.cpp:143
#define GL_RGB
Definition: glew.h:624
virtual ~CFBORender()
Destructor.
Definition: CFBORender.cpp:118
#define glDeleteFramebuffersEXT
Definition: glew.h:7004
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:6928
unsigned int GLenum
Definition: glew.h:207
#define GL_TEXTURE_RECTANGLE_EXT
Definition: glew.h:8337
void getFrame2(const COpenGLScene &scene, mrpt::img::CImage &image)
Render the scene and get the rendered rgb image.
Definition: CFBORender.cpp:185
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)
const char * argv[]
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:231
GLAPI const GLubyte *GLAPIENTRY glGetString(GLenum name)
GLAPI void GLAPIENTRY glGenTextures(GLsizei n, GLuint *textures)
#define glFramebufferTexture2DEXT
Definition: glew.h:7009
#define glGenFramebuffersEXT
Definition: glew.h:7011
void render() const
Render this scene.
#define MRPT_END
Definition: exceptions.h:245
#define GL_FRAMEBUFFER_EXT
Definition: glew.h:6946
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:58
void(GLAPIENTRY * PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
Definition: glew.h:6978
const int argc
#define glBindFramebufferEXT
Definition: glew.h:7000
#define GL_TEXTURE_RECTANGLE_ARB
Definition: glew.h:4240
GLuint start
Definition: glext.h:3532
mrpt::img::TColorf m_default_bk_color
Definition: CFBORender.h:83
A camera: if added to a scene, the viewpoint defined by this camera will be used instead of the camer...
Definition: CCamera.h:27
GLenum GLsizei GLsizei height
Definition: glext.h:3558
void(GLAPIENTRY * PFNGLBINDFRAMEBUFFEREXTPROC)(GLenum target, GLuint framebuffer)
Definition: glew.h:6962
GLAPI void GLAPIENTRY glTexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:147
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 9b18308f3 Mon Nov 18 23:39:25 2019 +0100 at lun nov 18 23:45:12 CET 2019