Go to the documentation of this file.00001 #ifndef OPENTISSUE_UTILITY_GL_GL_RENDER_BUFFER_H
00002 #define OPENTISSUE_UTILITY_GL_GL_RENDER_BUFFER_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/utility/gl/gl.h>
00013 #include <boost/shared_ptr.hpp>
00014
00015 namespace OpenTissue
00016 {
00017
00018 namespace gl
00019 {
00020
00051 class Renderbuffer
00052 {
00053 protected:
00054
00055 GLuint m_buffer_id;
00056
00057 protected:
00058
00064 static GLuint create_buffer_id()
00065 {
00066 GLuint id = 0;
00067 glGenRenderbuffersEXT(1, &id);
00068 gl::gl_check_errors("RenderBuffer::create_buffer_id(): glGenRenderbuffersEXT");
00069 return id;
00070 }
00071
00072 public:
00073
00074 Renderbuffer()
00075 : m_buffer_id( create_buffer_id() )
00076 {}
00077
00085 Renderbuffer(GLenum internal_format, int width, int height)
00086 : m_buffer_id( create_buffer_id() )
00087 {
00088 set(internal_format, width, height);
00089 }
00090
00091 ~Renderbuffer(){ glDeleteRenderbuffersEXT(1, &m_buffer_id); }
00092
00093 public:
00094
00095 void bind()
00096 {
00097 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_buffer_id);
00098 gl::gl_check_errors("RenderBuffer::bind(): glBindRenderbufferEXT");
00099 }
00100 void unbind()
00101 {
00102 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
00103 gl::gl_check_errors("RenderBuffer::unbind(): glBindRenderbufferEXT");
00104 }
00105
00113 void set(GLenum internal_format, int width, int height)
00114 {
00115 int max_size = Renderbuffer::get_max_size();
00116 if (width > max_size || height > max_size )
00117 {
00118 std::cerr << "Renderbuffer.set(): " << "Size too big (" << width << ", " << height << ")" << std::endl;
00119 return;
00120 }
00121
00122 GLint tmp = 0;
00123 glGetIntegerv( GL_RENDERBUFFER_BINDING_EXT, &tmp );
00124 gl::gl_check_errors("RenderBuffer::set(): glGetIntegerv");
00125 GLuint saved_id = static_cast<GLuint>(tmp);
00126 if (saved_id != m_buffer_id)
00127 {
00128 bind();
00129 }
00130
00131 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, internal_format, width, height );
00132 gl::gl_check_errors("RenderBuffer::set(): glRenderbufferStorageEXT");
00133
00134 if (saved_id != m_buffer_id)
00135 {
00136 unbind();
00137 }
00138 }
00139
00145 GLuint get_id() const { return m_buffer_id; }
00146
00147 public:
00148
00155 static GLint get_max_size()
00156 {
00157 GLint max_size = 0;
00158 glGetIntegerv( GL_MAX_RENDERBUFFER_SIZE_EXT, &max_size );
00159 gl::gl_check_errors("RenderBuffer::get_max_size(): glGetIntegerv");
00160 return max_size;
00161 }
00162
00163 };
00164
00165 typedef boost::shared_ptr<Renderbuffer> renderbuffer_pointer;
00166
00175 inline renderbuffer_pointer create_depth_buffer(unsigned int width, unsigned int height)
00176 {
00177 renderbuffer_pointer buffer;
00178 buffer.reset(
00179 new Renderbuffer()
00180 );
00181 buffer->set(GL_DEPTH_COMPONENT24,width, height);
00182 return buffer;
00183 }
00184
00193 inline renderbuffer_pointer create_stencil_buffer(unsigned int width, unsigned int height)
00194 {
00195 renderbuffer_pointer buffer;
00196 buffer.reset(
00197 new Renderbuffer()
00198 );
00199 buffer->set(GL_STENCIL_INDEX16_EXT,width, height);
00200 return buffer;
00201 }
00202
00203
00212 inline renderbuffer_pointer create_packed_depth_and_stencil_buffer(unsigned int width, unsigned int height)
00213 {
00214 renderbuffer_pointer buffer;
00215 buffer.reset(
00216 new Renderbuffer()
00217 );
00218
00219 buffer->set(GL_DEPTH24_STENCIL8_EXT,width, height);
00220 return buffer;
00221 }
00222
00223 }
00224
00225 }
00226
00227
00228 #endif