Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
FramebufferSetup.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 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 <OpenGLFramework/Utils/FramebufferSetup.hh>
26 #include <OpenGLFramework/Base/glfFBOAttachmentReleaseException.hh>
27 
28 using namespace std;
29 using namespace BIAS;
30 
31 FramebufferSetup::FramebufferSetup() :
32  fbo_(NULL), clearColorBuffer_(false), clearDepthBuffer_(false), clearDepth_(
33  1.0f), depthAttachment_(NULL)
34 {
35  for (unsigned int i = 0; i < 4; i++)
36  clearColor_[i] = 0.0f;
37 }
38 
39 void
41 {
42  BIASASSERT(fbo != NULL);
43  fbo_ = fbo;
44 }
45 
46 void
48  int AttachmentPoint)
49 {
50  if (AttachmentPoint >= static_cast<int> (colorAttachments_.size()))
51  {
52  colorAttachments_.resize(AttachmentPoint + 1, NULL);
53  }
54  colorAttachments_[AttachmentPoint] = texture;
55 }
56 
57 
58 void
60 {
61  colorAttachments_.clear();
62 }
63 
64 
65 
66 void
67 FramebufferSetup::SetColorAttachments(const vector<glfTexture*>& textures)
68 {
69  colorAttachments_.clear();
70  colorAttachments_ = textures;
71 }
72 
73 void
75 {
76  depthAttachment_ = texture;
77 }
78 
81 {
82  if (AttachmentPoint >= static_cast<int> (colorAttachments_.size()))
83  {
84  return NULL;
85  }
86  else
87  {
88  return dynamic_cast<glfTexture2D*> (colorAttachments_[AttachmentPoint]);
89  }
90 }
91 
92 void
93 FramebufferSetup::EnableColorClearance(float red, float green, float blue,
94  float alpha)
95 {
96  clearColor_[0] = red;
97  clearColor_[1] = green;
98  clearColor_[2] = blue;
99  clearColor_[3] = alpha;
100  clearColorBuffer_ = true;
101 }
102 
103 void
105 {
106  clearDepth_ = depth;
107  clearDepthBuffer_ = true;
108 }
109 
110 void
112 {
113  clearColorBuffer_ = false;
114 }
115 
116 void
118 {
119  clearDepthBuffer_ = false;
120 }
121 
122 void
124 {
125  BIASASSERT(fbo_ != NULL);
126  const unsigned int numRT =
127  static_cast<GLuint> (glfFramebufferObject::MaxRenderTargets());
128  vector<GLenum> renderBuffers;
129  renderBuffers.reserve(numRT);
130  for (unsigned int i = 0; i < colorAttachments_.size(); i++)
131  {
132  if (colorAttachments_[i] != NULL)
133  {
134  fbo_->AttachTexture(*(colorAttachments_[i]), GL_COLOR_ATTACHMENT0_EXT
135  + i);
136  renderBuffers.push_back(GL_COLOR_ATTACHMENT0_EXT + i);
137  }
138  else
139  {
140  try
141  {
142  fbo_->ReleaseAttachment(GL_COLOR_ATTACHMENT0_EXT + i);
143  }
145  {
146  //we ignore this error, this is just fine for us
147  }
148  }
149  }
150  fbo_->SetDrawBuffers(renderBuffers);
151  //release all others
152  for (unsigned int i = colorAttachments_.size(); i < numRT; i++)
153  {
154  try
155  {
156  fbo_->ReleaseAttachment(GL_COLOR_ATTACHMENT0_EXT + i);
157  }
159  {
160  //we ignore this error, this is just fine for us
161  }
162  }
163 
164  if (depthAttachment_ != NULL)
165  {
166  fbo_->AttachTexture(*depthAttachment_, GL_DEPTH_ATTACHMENT_EXT);
167  }
168  else
169  {
170  try
171  {
172  fbo_->ReleaseAttachment(GL_DEPTH_ATTACHMENT_EXT);
173  }
175  {
176  //we ignore this error, this is just fine for us
177  }
178  }
179 
180 #ifdef BIAS_DEBUG
181  fbo_->CheckComplete();
182 #endif
183 
184  if (clearColorBuffer_)
185  {
186  fbo_->ClearColorBuffer(clearColor_[0], clearColor_[1], clearColor_[2],
187  clearColor_[3]);
188  }
189  if (clearDepthBuffer_)
190  {
191  fbo_->ClearDepthBuffer(clearDepth_);
192  }
193 
194 }
void AttachTexture(const glfTexture &texture, GLenum attachmentPoint, int attachedMipMapLevel=0, int zSlice=0, GLenum cubeMapSide=0)
Attaches a texture to an attachment point which can be.
A 2D texture.
Definition: glfTexture2D.hh:40
void SetDepthAttachment(BIAS::glfTexture *texture)
void SetDrawBuffers(const std::vector< GLenum > &drawbuffers)
void ReleaseAttachment(GLenum attachmentPoint)
Removes an attachment from the framebuffer object.
void SetColorAttachments(const std::vector< BIAS::glfTexture * > &textures)
replaces all previous color attachments and through given
Class is thrown when an release of some FBO attachment point is not possible.
void CheckComplete() const
Checks whether the framebuffer object is supported in its current state.
void EnableColorClearance(float red=0.0f, float green=0.0f, float blue=0.0f, float alpha=0.0f)
void ClearDepthBuffer(float depth=1.0f)
Clears the depth buffer of the render target with the given value.
void LinkFBO(BIAS::glfFramebufferObject *fbo)
void SetColorAttachment(BIAS::glfTexture *texture, int AttachmentPoint=0)
Base class for textures.
Definition: glfTexture.hh:42
void ClearColorBuffer(float red=0.0f, float green=0.0f, float blue=0.0f, float alpha=0.0f)
Clears the color buffer of the render target with the given color.
void EnableDepthClearance(float depth=1.0f)
glfTexture2D * GetColorAttachment2D(int AttachmentPoint)
Returns texture attached to AttachmentPoint if it is a glfTexture2D otherwise NULL.