Main MRPT website > C++ reference for MRPT 1.5.7
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( unsigned int width, unsigned int height, const bool skip_glut_window ) :
24  m_width(width),
25  m_height(height),
26  m_win_used(!skip_glut_window),
27  m_default_bk_color(.6f,.6f,.6f,1)
28 {
29 #if MRPT_HAS_OPENCV && MRPT_HAS_OPENGL_GLUT
30 
32 
33  if (m_win_used)
34  {
35  // check a previous initialization of the GLUT
36  if(!glutGet(GLUT_INIT_STATE))
37  {
38  // create the context (a little trick)
39  int argc = 1;
40  char *argv[1] = { NULL };
41  glutInit(&argc, argv);
42  }
43 
44  // create a hidden window
45  m_win = glutCreateWindow("CFBORender");
46  glutHideWindow();
47  }
48 
49  // call after creating the hidden window
50  if(!isExtensionSupported("GL_EXT_framebuffer_object"))
51  THROW_EXCEPTION("Framebuffer Object extension unsupported");
52 
53  // In win32 we have to load the pointers to the functions:
54 #ifdef MRPT_OS_WINDOWS
55  glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)wglGetProcAddress("glGenFramebuffersEXT");
56  glDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC)wglGetProcAddress("glDeleteFramebuffersEXT");
57  glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)wglGetProcAddress("glBindFramebufferEXT");
58  glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC) wglGetProcAddress("glFramebufferTexture2DEXT");
59 
64 #endif
65 
66  // gen the frambuffer object (FBO), similar manner as a texture
68 
69  // bind the framebuffer, fbo, so operations will now occur on it
71 
72  // change viewport size (in pixels)
73  glViewport(0, 0, m_width, m_height);
74 
75  // make a texture
76  glGenTextures(1, &m_tex);
77 
78  // initialize texture that will store the framebuffer image
79  const GLenum texTarget =
80 # if defined(GL_TEXTURE_RECTANGLE_NV)
82 # elif defined(GL_TEXTURE_RECTANGLE_ARB)
84 # else
86 # endif
87 
88  glBindTexture(texTarget, m_tex);
89  glTexImage2D(texTarget, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
90 
91  // bind this texture to the current framebuffer obj. as color_attachement_0
93 
94  //'unbind' the frambuffer object, so subsequent drawing ops are not drawn into the FBO.
95  // '0' means "windowing system provided framebuffer
97 
98  MRPT_END
99 
100 //#else
101 // THROW_EXCEPTION("MRPT compiled without OpenCV and/or OpenGL support!!")
102 #endif
103 }
104 
105 /*---------------------------------------------------------------
106  Destructor:
107  ---------------------------------------------------------------*/
109 {
110 #if MRPT_HAS_OPENGL_GLUT
111  // delete the current texture, the framebuffer object and the GLUT window
112  glDeleteTextures(1, &m_tex);
114  if (m_win_used) glutDestroyWindow(m_win);
115 #endif
116 }
117 
118 /*---------------------------------------------------------------
119  Set the scene camera
120  ---------------------------------------------------------------*/
121 void CFBORender::setCamera( const COpenGLScene& scene, const CCamera& camera )
122 {
123  MRPT_START
124 
125  scene.getViewport("main")->getCamera() = camera;
126 
127  MRPT_END
128 }
129 
130 /*---------------------------------------------------------------
131  Get the scene camera
132  ---------------------------------------------------------------*/
134 {
135  MRPT_START
136 
137  return scene.getViewport("main")->getCamera();
138 
139  MRPT_END
140 }
141 
142 /*---------------------------------------------------------------
143  Render the scene and get the rendered rgb image. This
144  function resizes the image buffer if it is necessary
145  ---------------------------------------------------------------*/
147 {
148 #if MRPT_HAS_OPENCV && MRPT_HAS_OPENGL_GLUT
149 
150  MRPT_START
151 
152  // resize the buffer if it is necessary
153  if( buffer.getWidth() != static_cast<size_t>(m_width) ||
154  buffer.getHeight() != static_cast<size_t>(m_height) ||
155  buffer.getChannelCount() != 3 ||
156  buffer.isOriginTopLeft() != false )
157  {
158  buffer.resize(m_width, m_height, 3, false);
159  }
160 
161  // Go on.
162  getFrame2(scene,buffer);;
163 
164  MRPT_END
165 #else
167 #endif
168 }
169 
170 /*---------------------------------------------------------------
171  Render the scene and get the rendered rgb image. This
172  function does not resize the image buffer.
173  ---------------------------------------------------------------*/
175 {
176 #if MRPT_HAS_OPENGL_GLUT
177 
178  MRPT_START
179 
180  // check the buffer size
181  ASSERT_EQUAL_( buffer.getWidth(), static_cast<size_t>(m_width) )
182  ASSERT_EQUAL_( buffer.getHeight(),static_cast<size_t>(m_height) )
183  ASSERT_EQUAL_( buffer.getChannelCount(), 3 )
184  ASSERT_EQUAL_( buffer.isOriginTopLeft(), false )
185 
186  // bind the framebuffer, fbo, so operations will now occur on it
188 
190 
191  // Render opengl objects:
192  // ---------------------------
193  scene.render();
194 
195  // If any, draw the 2D text messages:
196  // ----------------------------------
198 
199 
200  // TODO NOTE: This should fail if the image has padding bytes. See glPixelStore() etc.
202 
203  //'unbind' the frambuffer object, so subsequent drawing ops are not drawn into the FBO.
204  // '0' means "windowing system provided framebuffer
206 
207  MRPT_END
208 #else
210 #endif
211 }
212 
213 /*---------------------------------------------------------------
214  Resize the image size
215  ---------------------------------------------------------------*/
216 void CFBORender::resize( unsigned int width, unsigned int height )
217 {
218 #if MRPT_HAS_OPENCV && MRPT_HAS_OPENGL_GLUT
219 
220  MRPT_START
221 
222  // update members
223  m_width = width;
224  m_height = height;
225 
226  // bind the framebuffer, fbo, so operations will now occur on it
228 
229  // change viewport size (in pixels)
230  glViewport(0, 0, m_width, m_height);
231 
232  // change texture size
233  const GLenum texTarget =
234 # if defined(GL_TEXTURE_RECTANGLE_NV)
236 # elif defined(GL_TEXTURE_RECTANGLE_ARB)
238 # else
240 # endif
241 
242  glBindTexture(texTarget, m_tex);
243  glTexImage2D(texTarget, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
244 
245  //'unbind' the frambuffer object, so subsequent drawing ops are not drawn into the FBO.
246  // '0' means "windowing system provided framebuffer
248 
249  MRPT_END
250 
251 //#else
252 // THROW_EXCEPTION("MRPT compiled without OpenCV and/or OpenGL support!!")
253 #else
255 #endif
256 }
257 
258 /*---------------------------------------------------------------
259  Provide information on Framebuffer object extension
260  ---------------------------------------------------------------*/
261 int CFBORender::isExtensionSupported( const char* extension )
262 {
263 #if MRPT_HAS_OPENGL_GLUT
264 
265  MRPT_START
266 
267  const GLubyte *extensions = NULL;
268  const GLubyte *start;
269  GLubyte *where, *terminator;
270 
271  /* Extension names should not have spaces. */
272  where = (GLubyte *) strchr(extension, ' ');
273  if (where || *extension == '\0')
274  return 0;
275  extensions = glGetString(GL_EXTENSIONS);
276 
277  /* It takes a bit of care to be fool-proof about parsing the
278  OpenGL extensions string. Don't be fooled by sub-strings,
279  etc. */
280  start = extensions;
281  for (;;) {
282  where = (GLubyte *) strstr((const char *) start, extension);
283  if (!where)
284  break;
285  terminator = where + strlen(extension);
286  if (where == start || *(where - 1) == ' ')
287  if (*terminator == ' ' || *terminator == '\0')
288  return 1;
289  start = terminator;
290  }
291 
292  MRPT_END
293 
294 //#else
295 // THROW_EXCEPTION("MRPT compiled without OpenGL support!!")
296 #else
297  MRPT_UNUSED_PARAM(extension);
298 #endif
299 
300  return 0;
301 }
#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.
Definition: zip.h:16
int isExtensionSupported(const char *extension)
Provide information on Framebuffer object extension.
Definition: CFBORender.cpp:261
#define GL_EXTENSIONS
Definition: glew.h:639
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:101
GLuint buffer
Definition: glext.h:3775
#define THROW_EXCEPTION(msg)
void getFrame2(const COpenGLScene &scene, mrpt::utils::CImage &image)
Render the scene and get the rendered rgb image.
Definition: CFBORender.cpp:174
#define GL_TEXTURE_RECTANGLE_NV
Definition: glew.h:8472
STL namespace.
#define GL_UNSIGNED_BYTE
Definition: glew.h:298
mrpt::utils::TColorf m_default_bk_color
Definition: CFBORender.h:71
void(GLAPIENTRY * PFNGLDELETEFRAMEBUFFERSEXTPROC)(GLsizei n, const GLuint *framebuffers)
Definition: glew.h:5642
GLenum GLsizei width
Definition: glext.h:3513
GLAPI void GLAPIENTRY glBindTexture(GLenum target, GLuint texture)
#define GL_BGR_EXT
Definition: glew.h:4693
#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:133
void(GLAPIENTRY * PFNGLBINDFRAMEBUFFEREXTPROC)(GLenum target, GLuint framebuffer)
Definition: glew.h:5639
#define GL_RGB
Definition: glew.h:619
virtual ~CFBORender()
Destructor.
Definition: CFBORender.cpp:108
COpenGLViewportPtr getViewport(const std::string &viewportName=std::string("main")) const
Returns the viewport with the given name, or NULL if it does not exist; note that the default viewpor...
#define glDeleteFramebuffersEXT
Definition: glew.h:5660
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:5605
unsigned int GLenum
Definition: glew.h:202
#define MRPT_START
#define GL_TEXTURE_RECTANGLE_EXT
Definition: glew.h:6804
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)
void setCamera(const COpenGLScene &scene, const CCamera &camera)
Change the scene camera.
Definition: CFBORender.cpp:121
void resize(unsigned int width, unsigned int height)
Resize the rendering canvas size.
Definition: CFBORender.cpp:216
GLAPI const GLubyte *GLAPIENTRY glGetString(GLenum name)
GLAPI void GLAPIENTRY glGenTextures(GLsizei n, GLuint *textures)
#define glFramebufferTexture2DEXT
Definition: glew.h:5664
#define glGenFramebuffersEXT
Definition: glew.h:5666
void render() const
Render this scene.
#define GL_FRAMEBUFFER_EXT
Definition: glew.h:5623
The namespace for 3D scene representation and rendering.
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives...
Definition: COpenGLScene.h:49
#define ASSERT_(f)
void(GLAPIENTRY * PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
Definition: glew.h:5646
#define glBindFramebufferEXT
Definition: glew.h:5657
#define GL_TEXTURE_RECTANGLE_ARB
Definition: glew.h:3615
GLuint start
Definition: glext.h:3512
A camera: if added to a scene, the viewpoint defined by this camera will be used instead of the camer...
Definition: CCamera.h:31
GLenum GLsizei GLsizei height
Definition: glext.h:3523
void(GLAPIENTRY * PFNGLGENFRAMEBUFFERSEXTPROC)(GLsizei n, GLuint *framebuffers)
Definition: glew.h:5648
unsigned char GLubyte
Definition: glew.h:210
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:146



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019