gl_renderer.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Suat Gedikli */
36 
37 #pragma once
38 
40 #include <GL/glew.h>
41 #ifdef __APPLE__
42 #include <OpenGL/gl.h>
43 #else
44 #include <GL/gl.h>
45 #endif
46 #include <string>
47 #include <thread>
48 #include <mutex>
49 #include <map>
50 
51 namespace mesh_filter
52 {
53 MOVEIT_CLASS_FORWARD(GLRenderer); // Defines GLRendererPtr, ConstPtr, WeakPtr... etc
54 
59 class GLRenderer
60 {
61 public:
70  GLRenderer(unsigned width, unsigned height, float near = 0.1, float far = 10.0);
71 
73  ~GLRenderer();
74 
79  void begin() const;
80 
85  void end() const;
86 
92  void callList(GLuint list) const;
93 
99  void getColorBuffer(unsigned char* buffer) const;
100 
106  void getDepthBuffer(float* buffer) const;
107 
116  GLuint setShadersFromFile(const std::string& vertex_filename, const std::string& fragment_filename);
117 
125  GLuint setShadersFromString(const std::string& vertex_shader, const std::string& fragment_shader);
126 
135  void setCameraParameters(float fx, float fy, float cx, float cy);
136 
143  void setClippingRange(float near, float far);
144 
150  const float& getNearClippingDistance() const;
151 
157  const float& getFarClippingDistance() const;
158 
164  unsigned getWidth() const;
165 
171  unsigned getHeight() const;
172 
179  void setBufferSize(unsigned width, unsigned height);
180 
186  const GLuint& getProgramID() const;
187 
193  GLuint getDepthTexture() const;
194 
200  GLuint getColorTexture() const;
201 
202 private:
207  void setCameraParameters() const;
208 
215  void readShaderCodeFromFile(const std::string& filename, std::string& source) const;
216 
224  GLuint loadShaders(const std::string& vertex_source, const std::string& fragment_source) const;
225 
233  GLuint createShader(GLuint shaderID, const std::string& source) const;
234 
239  void initFrameBuffers();
240 
245  void deleteFrameBuffers();
246 
251  static void createGLContext();
252 
257  static void deleteGLContext();
258 
260  unsigned width_;
261 
263  unsigned height_;
264 
266  GLuint fbo_id_;
267 
269  GLuint rbo_id_;
270 
272  GLuint rgb_id_;
273 
275  GLuint depth_id_;
276 
278  GLuint program_;
279 
281  float near_;
282 
284  float far_;
285 
287  float fx_;
288 
290  float fy_;
291 
293  float cx_;
294 
296  float cy_;
297 
299  static std::map<std::thread::id, std::pair<unsigned, GLuint> > context_;
300 
301  /* \brief lock for context map */
302  static std::mutex context_lock_;
303 
304  static bool glutInitialized_;
305 };
306 } // namespace mesh_filter
mesh_filter::GLRenderer::createGLContext
static void createGLContext()
create the OpenGL context if required. Only on context is created for each thread
Definition: gl_renderer.cpp:363
mesh_filter::GLRenderer::getColorBuffer
void getColorBuffer(unsigned char *buffer) const
retrieves the color buffer from OpenGL
Definition: gl_renderer.cpp:205
mesh_filter::GLRenderer::initFrameBuffers
void initFrameBuffers()
initializes the frame buffer objects
Definition: gl_renderer.cpp:125
mesh_filter::GLRenderer::fy_
float fy_
focal length in y-direction of camera in pixels
Definition: gl_renderer.h:322
mesh_filter::GLRenderer::setClippingRange
void setClippingRange(float near, float far)
sets the near and far clipping plane distances in meters
Definition: gl_renderer.cpp:91
mesh_filter::GLRenderer::glutInitialized_
static bool glutInitialized_
Definition: gl_renderer.h:336
mesh_filter::GLRenderer::getDepthBuffer
void getDepthBuffer(float *buffer) const
retrieves the depth buffer from OpenGL
Definition: gl_renderer.cpp:213
mesh_filter::GLRenderer::callList
void callList(GLuint list) const
executes a OpenGL list
Definition: gl_renderer.cpp:191
mesh_filter::GLRenderer::program_
GLuint program_
handle to program that is currently used
Definition: gl_renderer.h:310
mesh_filter::GLRenderer::getColorTexture
GLuint getColorTexture() const
returns the handle of the color buffer as an OpenGL texture object
Definition: gl_renderer.cpp:429
mesh_filter::GLRenderer::depth_id_
GLuint depth_id_
handle to depth buffer
Definition: gl_renderer.h:307
mesh_filter::GLRenderer::end
void end() const
finalizes the frame buffers after rendering and/or manipulating
Definition: gl_renderer.cpp:198
mesh_filter::GLRenderer::setShadersFromString
GLuint setShadersFromString(const std::string &vertex_shader, const std::string &fragment_shader)
loads, compiles, links and adds GLSL shaders from string to the current OpenGL context.
Definition: gl_renderer.cpp:234
mesh_filter::GLRenderer::getDepthTexture
GLuint getDepthTexture() const
returns the handle of the depth buffer as an OpenGL texture object
Definition: gl_renderer.cpp:434
mesh_filter::GLRenderer::fbo_id_
GLuint fbo_id_
handle to frame buffer object
Definition: gl_renderer.h:298
mesh_filter::GLRenderer::rgb_id_
GLuint rgb_id_
handle to color buffer
Definition: gl_renderer.h:304
mesh_filter::GLRenderer::getNearClippingDistance
const float & getNearClippingDistance() const
returns the distance of the near clipping plane in meters
Definition: gl_renderer.cpp:245
mesh_filter::GLRenderer::setShadersFromFile
GLuint setShadersFromFile(const std::string &vertex_filename, const std::string &fragment_filename)
loads, compiles, links and adds GLSL shaders from files to the current OpenGL context.
Definition: gl_renderer.cpp:221
mesh_filter::GLRenderer::setBufferSize
void setBufferSize(unsigned width, unsigned height)
set the size of fram buffers
Definition: gl_renderer.cpp:80
mesh_filter::GLRenderer::deleteGLContext
static void deleteGLContext()
deletes OpenGL context for the current thread
Definition: gl_renderer.cpp:410
mesh_filter::GLRenderer::cx_
float cx_
x-coordinate of principal point of camera in pixels
Definition: gl_renderer.h:325
mesh_filter::GLRenderer::rbo_id_
GLuint rbo_id_
handle to render buffer object
Definition: gl_renderer.h:301
mesh_filter::GLRenderer::cy_
float cy_
y-coordinate of principal point of camera in pixels
Definition: gl_renderer.h:328
mesh_filter::GLRenderer::createShader
GLuint createShader(GLuint shaderID, const std::string &source) const
create a OpenGL shader object from the shader source code
Definition: gl_renderer.cpp:255
mesh_filter::GLRenderer::begin
void begin() const
initializes the frame buffers for rendering and or manipulating
Definition: gl_renderer.cpp:181
mesh_filter::GLRenderer::~GLRenderer
~GLRenderer()
destructor, destroys frame buffer objects and OpenGL context
Definition: gl_renderer.cpp:73
mesh_filter::MOVEIT_CLASS_FORWARD
MOVEIT_CLASS_FORWARD(Job)
mesh_filter::GLRenderer::readShaderCodeFromFile
void readShaderCodeFromFile(const std::string &filename, std::string &source) const
reads shader source code from file to a string
Definition: gl_renderer.cpp:285
mesh_filter::GLRenderer::height_
unsigned height_
height of frame buffer objects in pixels
Definition: gl_renderer.h:295
mesh_filter::GLRenderer::GLRenderer
GLRenderer(unsigned width, unsigned height, float near=0.1, float far=10.0)
constructs the frame buffer object in a new OpenGL context.
Definition: gl_renderer.cpp:54
mesh_filter::GLRenderer::width_
unsigned width_
width of frame buffer objects in pixels
Definition: gl_renderer.h:292
mesh_filter::GLRenderer::far_
float far_
distance of far clipping plane in meters
Definition: gl_renderer.h:316
mesh_filter::GLRenderer::deleteFrameBuffers
void deleteFrameBuffers()
deletes the frame buffer objects
Definition: gl_renderer.cpp:167
class_forward.h
mesh_filter::GLRenderer::setCameraParameters
void setCameraParameters() const
sets the OpenGL camera parameters
Definition: gl_renderer.cpp:109
mesh_filter::GLRenderer::fx_
float fx_
focal length in x-direction of camera in pixels
Definition: gl_renderer.h:319
mesh_filter::GLRenderer::getWidth
unsigned getWidth() const
returns the width of the frame buffer objectsin pixels
Definition: gl_renderer.cpp:439
mesh_filter::GLRenderer::context_
static std::map< std::thread::id, std::pair< unsigned, GLuint > > context_
map from thread id to OpenGL context
Definition: gl_renderer.h:331
mesh_filter::GLRenderer::getFarClippingDistance
const float & getFarClippingDistance() const
returns the distance of the far clipping plane in meters
Definition: gl_renderer.cpp:250
mesh_filter::GLRenderer::loadShaders
GLuint loadShaders(const std::string &vertex_source, const std::string &fragment_source) const
Compiles, Links and adds the GLSL shaders from strings containing the source codes.
Definition: gl_renderer.cpp:308
mesh_filter::GLRenderer::getProgramID
const GLuint & getProgramID() const
Definition: gl_renderer.cpp:240
mesh_filter::GLRenderer::getHeight
unsigned getHeight() const
returns the height of the frame buffer objects in pixels
Definition: gl_renderer.cpp:444
mesh_filter::GLRenderer::near_
float near_
distance of near clipping plane in meters
Definition: gl_renderer.h:313
mesh_filter
Definition: depth_self_filter_nodelet.h:47
mesh_filter::GLRenderer::context_lock_
static std::mutex context_lock_
Definition: gl_renderer.h:334


perception
Author(s): Ioan Sucan , Jon Binney , Suat Gedikli
autogenerated on Fri Jun 21 2024 02:26:30