Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfRenderbuffer.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include <Base/Image/ImageBase.hh>
26 #include "glfRenderbuffer.hh"
27 #include "glfException.hh"
28 #include "glfFormatDB.hh"
29 
30 
31 using namespace BIAS;
32 
34 {
35  id_ = 0;
36 }
37 
39 {
40  if (id_ != 0) {
41  glDeleteRenderbuffersEXT(1, &id_);
42  }
43 }
44 
45 void glfRenderbuffer::Create(GLenum internalFormat, int width, int height, GLsizei samples)
46 {
47  if(!GLEW_EXT_framebuffer_object) {
48  GLF_THROW_EXCEPTION("GLEW_EXT_framebuffer_object:objects of this type can only be used"
49  " with EXT_framebuffer_object OpenGL extension");
50  }
51 
52  if(id_ == 0) {
53  glGenRenderbuffersEXT(1, &id_);
54  }
55 
56  Bind();
57  if(samples > 0 && !GLEW_EXT_framebuffer_multisample) {
58  BIASWARN("Multisampling is not supported.");
59  }
60  if(samples==0 || !GLEW_EXT_framebuffer_multisample) {
61  glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT,
62  internalFormat,
63  width, height);
64  } else {
65  GLsizei maxSamples;
66  glGetIntegerv(GL_MAX_SAMPLES_EXT, &maxSamples);
67  if(maxSamples<samples) {
68  samples = maxSamples;
69  BIASWARN("requested number samples are not support, reducing to "<<samples<<" samples");
70  }
71  glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT,
72  samples,
73  internalFormat,
74  width, height);
75  }
76 
77  GLF_THROW_ON_OPENGL_ERROR;
78 }
79 
81 {
82  BIASASSERT(id_ != 0);
83  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, id_);
84  GLF_THROW_ON_OPENGL_ERROR;
85 }
86 
88 {
89  return id_;
90 }
91 
93 GetSize(GLint& width, GLint& height) {
94  Bind();
95 
96  glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_WIDTH_EXT, &width);
97  glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_HEIGHT_EXT, &height);
98 
99  }
100 
103  Bind();
104 
105  GLint numSamples;
106  glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
107  GL_RENDERBUFFER_SAMPLES_EXT,
108  &numSamples);
109  if(numSamples>0) {
110  GLF_THROW_EXCEPTION("num samples must be zero to read out buffer.");
111  }
112 
113  GLint internalFormat;
114  glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
115  GL_RENDERBUFFER_INTERNAL_FORMAT_EXT,
116  &internalFormat);
117  int width, height;
118  GetSize(width, height);
119 
120  GLenum format;
121  int numChannels;
122  glfFormatDB::GetFormatForInternalFormat(internalFormat, format, numChannels);
123  ImageBase::EStorageType storageType = image.GetStorageType();
124  if (long(image.GetWidth()) != width && long(image.GetHeight()) != height &&
125  long(image.GetChannelCount()) != numChannels)
126  {
127  image.Init(width, height, numChannels, storageType);
128  }
129 
130  // get OpenGL pixel type
132 
133  glPixelStorei(GL_PACK_ALIGNMENT, 1);
134 
135  glReadPixels( 0, 0, width, height, format, type, image.GetImageData());
136  GLF_THROW_ON_OPENGL_ERROR;
137 }
static void GetFormatForInternalFormat(GLenum internalFormat, GLenum &format, int &numChannels)
Computes a pixel format that can be used with glGetTexImage and glReadPixels for an internal format o...
Definition: glfFormatDB.cpp:32
void Create(GLenum internalFormat, int width, int height, GLsizei samples=0)
Creates the renderbuffer with the given format and size.
unsigned int GetWidth() const
Definition: ImageBase.hh:312
const void * GetImageData() const
Definition: ImageBase.hh:280
void GetSize(GLint &width, GLint &height)
void CopyToImage(ImageBase &image)
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
void Init(unsigned int width, unsigned int height, unsigned int nChannels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
Initialize image size and channels.
Definition: ImageBase.cpp:229
static GLenum GetPixelTypeForStorageType(ImageBase::EStorageType storageType)
Returns the OpenGL pixel type (GL_UNSIGNED_BYTE etc.) for a BIAS storage type.
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
GLuint GetRenderbufferID() const
Returns the OpenGL id of the renderbuffer.
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
void Bind() const
Binds the renderbuffer.