Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
FramebufferSetupPool.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 <OpenGLFramework/Utils/FramebufferSetupPool.hh>
26 #include <algorithm>
27 
28 using namespace BIAS;
29 using namespace std;
30 
32 {
33  ClearAll();
34 }
35 
36 void
38 {
39  texturePoolP_ = &texturePoolP;
40  fbo_.Create();
41 }
42 
43 /** Create a setup and the required 2D textures.
44  * \param setupName is the name used to identify the setup.
45  * \param width horizontal resolution.
46  * \param width vertical resolution.
47  * \param colorAttachmentName name of the glfTexture2D attached to COLOR_ATTACHMENT_0, identifying it in the internal
48  * Texture2DPool, this name must be unique across all setups.
49  * \param internalFormat is the internal format of the target glfTexture2D.
50  * \param clearColor if true the target texture is cleared when the setup is activated.
51  * \param depthAttachmentName if you want to render the depth output to a texture, give it a name for the internal Texture2DPool,
52  * if left empty to texture for the depth is attached, this name must be unique across all setups.
53  * \param clearDepth if true the target 'depth textur'e is cleared when the setup is activated.
54  * \param *Clear values the different channels are cleared, if clearing is enabled.
55  **/
57 FramebufferSetupPool::CreateSetup(const std::string& setupName,
58  unsigned int width, unsigned int height,
59  const std::string& colorAttachmentName, GLenum internalFormat,
60  const bool clearColors, const std::string& depthAttachmentName,
61  const bool clearDepth, const float rClear, const float gClear,
62  const float bClear, const float aClear, const float zClear)
63 {
64  map<std::string, pair<FramebufferSetup*, vector<string> > >::iterator it =
65  fboSetupPool_.find(setupName);
66  if (it != fboSetupPool_.end())
67  {
68  BIASERR("already used setup name : " << setupName
69  << ". use clear before reusing the name!");
70  ClearAll();
71  BIASABORT;
72  }
73 
74  //create attachments
75  glfTexture2D* colorAttachment = texturePoolP_->Create(colorAttachmentName,
76  width, height, internalFormat);
77 
78  FramebufferSetup* newSetup(NULL);
79  newSetup = new FramebufferSetup;
80  pair<FramebufferSetup*, vector<string> > newPair = make_pair(newSetup,
81  vector<string> ());
82  newPair.second.push_back(colorAttachmentName); // save attachment name
83 // cout<<"adding "<<colorAttachmentName<<endl;
84 // for(unsigned int i=0; i<newPair.second.size(); i++){
85 // cout<<"appended "<<newPair.second[i]<<endl;
86 // }
87 
88  glfTexture2D* depthAttachment = NULL;
89  if (depthAttachmentName.size() > 0) {
90  depthAttachment = texturePoolP_->Create(depthAttachmentName, width, height,
91  GL_DEPTH_COMPONENT);
92  newPair.second.push_back(depthAttachmentName); // save attachment name
93  }
94 
95 
96  fboSetupPool_[setupName] = newPair;
97 
98  newSetup->LinkFBO(&fbo_);
99  newSetup->SetColorAttachment(colorAttachment);
100  newSetup->SetDepthAttachment(depthAttachment);
101 
102  if (clearColors)
103  {
104  newSetup->EnableColorClearance(rClear, gClear, bClear, aClear);
105  }
106  else
107  {
108  newSetup->DisableColorClearance();
109  }
110  if (clearDepth)
111  {
112  newSetup->EnableDepthClearance(zClear);
113  }
114  else
115  {
116  newSetup->DisableDepthClearance();
117  }
118  return newSetup;
119 }
120 
122 FramebufferSetupPool::CreateSetup(const std::string& setupName,
123  unsigned int width, unsigned int height,
124  const std::vector<std::string>& colorAttachmentNames,
125  GLenum internalFormat, const bool clearColors,
126  const std::string& depthAttachmentName, const bool clearDepth,
127  const float rClear, const float gClear, const float bClear,
128  const float aClear, const float zClear)
129 {
130  map<std::string, pair<FramebufferSetup*, vector<string> > >::iterator it =
131  fboSetupPool_.find(setupName);
132  if (it != fboSetupPool_.end())
133  {
134  BIASERR("already used setup name : " << setupName
135  << ". use clear before reusing the name!");
136  ClearAll();
137  BIASABORT;
138  }
139 
140  //create attachments
141  FramebufferSetup* newSetup(NULL);
142  newSetup = new FramebufferSetup;
143  pair<FramebufferSetup*, vector<string> > newPair = make_pair(newSetup,
144  vector<string> ());
145 
146  newSetup->LinkFBO(&fbo_);
147  for (unsigned int i = 0; i < colorAttachmentNames.size(); i++)
148  {
149  glfTexture2D* colorAttachment = texturePoolP_->Create(
150  colorAttachmentNames[i], width, height, internalFormat);
151  newSetup->SetColorAttachment(colorAttachment, i);
152  newPair.second.push_back(colorAttachmentNames[i]); // save attachment name
153  }
154  fboSetupPool_[setupName] = newPair;
155 
156  //depth attachment
157  glfTexture2D* depthAttachment = NULL;
158  if (depthAttachmentName.size() > 0) {
159  depthAttachment = texturePoolP_->Create(depthAttachmentName, width, height,
160  GL_DEPTH_COMPONENT);
161  newPair.second.push_back(depthAttachmentName); // save attachment name
162  }
163  newSetup->SetDepthAttachment(depthAttachment);
164  if (clearColors)
165  {
166  newSetup->EnableColorClearance(rClear, gClear, bClear, aClear);
167  }
168  else
169  {
170  newSetup->DisableColorClearance();
171  }
172  if (clearDepth)
173  {
174  newSetup->EnableDepthClearance(zClear);
175  }
176  else
177  {
178  newSetup->DisableDepthClearance();
179  }
180  return newSetup;
181 }
182 
183 void
185  const vector<string>& colorAttachmentNames)
186 {
187  if (texturePoolP_ != NULL)
188  {
189  map<string, pair<FramebufferSetup*, vector<string> > >::iterator iter =
190  fboSetupPool_.find(setupName);
191  if (iter != fboSetupPool_.end())
192  {
193  FramebufferSetup* fbsP = (iter->second).first;
194  (iter->second).second = colorAttachmentNames;
195  SetColorAttachments_(*fbsP, colorAttachmentNames);
196  }
197  else
198  {
199  BIASERR("no frame buffer setup with name (" << setupName
200  << ") found !");
201  ClearAll();
202  BIASABORT;
203  }
204  }
205  else
206  {
207  BIASERR("call create first ! " << endl);
208  BIASABORT;
209  }
210 }
211 
212 void
213 FramebufferSetupPool::SetColorAttachments_(FramebufferSetup& frameBufferSetup,
214  const vector<string>& colorAttachmentNames)
215 {
216  vector<glfTexture*> textureVector(colorAttachmentNames.size());
217  for (unsigned int i = 0; i < colorAttachmentNames.size(); i++)
218  {
219  glfTexture2D* textureP = (*texturePoolP_)[colorAttachmentNames[i]];
220  if (textureP == NULL)
221  {
222  BIASERR("unable to attach texture "
223  << colorAttachmentNames[i] << " no such texture found "
224  << endl);
225  BIASABORT;
226  }
227  else
228  {
229  textureVector[i] = textureP;
230  }
231  }
232 
233  frameBufferSetup.SetColorAttachments(textureVector);
234 }
235 
237 FramebufferSetupPool::CreateSetup(const std::string& setupName)
238 {
239  FramebufferSetup* newSetup(NULL);
240  map<string, pair<FramebufferSetup*, vector<string> > >::iterator iter =
241  fboSetupPool_.find(setupName);
242  if (iter != fboSetupPool_.end())
243  {
244  BIASERR("already used setup name : " << setupName
245  << ". use clear before reusing the name!");
246  ClearAll();
247  BIASABORT;
248  }
249  else
250  {
251  newSetup = new FramebufferSetup;
252  newSetup->LinkFBO(&fbo_);
253  fboSetupPool_[setupName] = make_pair(newSetup, vector<string> ());
254  }
255 
256  return newSetup;
257 }
258 
259 void
261 {
262  map<std::string, pair<FramebufferSetup*, vector<string> > >::iterator it =
263  fboSetupPool_.begin();
264  for (; it != fboSetupPool_.end(); it++)
265  {
266  delete (it->second).first;
267  }
268  fboSetupPool_.clear();
269  //texturePoolP_->ClearAll();
270 }
271 
272 void
273 FramebufferSetupPool::ClearSetup(const std::string& setupName)
274 {
275  map<std::string, pair<FramebufferSetup*, vector<string> > >::iterator it =
276  fboSetupPool_.find(setupName);
277  if (it != fboSetupPool_.end())
278  {
279  delete (it->second).first;
280  fboSetupPool_.erase(it);
281  }
282 }
283 
284 void
285 FramebufferSetupPool::Activate(const std::string& setupName)
286 {
287  map<std::string, pair<FramebufferSetup*, vector<string> > >::iterator it =
288  fboSetupPool_.find(setupName);
289  if (it != fboSetupPool_.end())
290  {
291  ((it->second).first)->Execute();
292  }
293 }
294 
295 ///** Returns the pointer to a texture attached during a setup.
296 // * \attention it is not possible to read from and write into the same texture.
297 // **/
298 //glfTexture2D*
299 //FramebufferSetupPool::GetAttachment(const std::string& attachmentName)
300 //{
301 // return (*texturePoolP_)[attachmentName];
302 //}
303 
305 FramebufferSetupPool::GetSetup(const std::string& setupName)
306 {
307  map<std::string, pair<FramebufferSetup*, vector<string> > >::iterator it =
308  fboSetupPool_.find(setupName);
309  if (it != fboSetupPool_.end())
310  {
311  return (it->second).first;
312  }
313  else
314  {
315  BIASWARN("A setup with this name : " << setupName << " does not exist");
316  return NULL;
317  }
318 }
319 
322 {
323  return &fbo_;
324 }
325 
328 {
329  return texturePoolP_;
330 }
331 
333 FramebufferSetupPool::GetAttachment(const string& setupName,
334  const string& attachmentName)
335 {
336  map<string, pair<FramebufferSetup*, vector<string> > >::iterator iter =
337  fboSetupPool_.find(setupName);
338  if (iter != fboSetupPool_.end())
339  {
340  vector<string>& attachmentNames = (iter->second).second;
341  vector<string>::iterator name_iter = find(attachmentNames.begin(),
342  attachmentNames.end(), attachmentName);
343  if (name_iter != attachmentNames.end())
344  {
345  return (*texturePoolP_)[attachmentName];
346  }
347  else
348  {
349  return NULL;
350  }
351  }
352  else
353  {
354  return NULL;
355  }
356 }
357 
358 void
360  std::vector<std::string>& attachmentNames)
361 {
362  map<string, pair<FramebufferSetup*, vector<string> > >::iterator iter =
363  fboSetupPool_.find(setupName);
364  if (iter != fboSetupPool_.end())
365  {
366  attachmentNames = (iter->second).second;
367 
368  }
369  else {
370  BIASWARN("no such setup : "<<setupName);
371  }
372 }
void SetColorAttachments(const std::string &setupName, const std::vector< std::string > &colorAttachmentNames)
void GetSetupAttachmentNames(const std::string &setupName, std::vector< std::string > &attachmentNames)
A 2D texture.
Definition: glfTexture2D.hh:40
FramebufferSetup * CreateSetup(const std::string &setupName)
void Create()
Creates the texture but does not upload any data yet.
Definition: glfTexture.cpp:80
void SetDepthAttachment(BIAS::glfTexture *texture)
Convenience container for managing 2D textures.
glfTexture2D * GetAttachment(const std::string &setupName, const std::string &attachmentName)
Returns the pointer to a texture attached during a setup.
void SetColorAttachments(const std::vector< BIAS::glfTexture * > &textures)
replaces all previous color attachments and through given
FramebufferSetup * GetSetup(const std::string &setupName)
Returns the pointer to a texture attached during a setup.
void Activate(const std::string &setupName)
void EnableColorClearance(float red=0.0f, float green=0.0f, float blue=0.0f, float alpha=0.0f)
void LinkFBO(BIAS::glfFramebufferObject *fbo)
void SetColorAttachment(BIAS::glfTexture *texture, int AttachmentPoint=0)
glfFramebufferObject * GetFBO()
void Create(Texture2DPool &texturePoolP)
Call this once the GLContext is valid.
void ClearSetup(const std::string &setupName)
Does not remove the attachment.
BIAS::glfTexture2D * Create(const std::string &name, int width, int height, GLenum internalFormat, GLenum minFilter=GL_NEAREST, GLenum magFilter=GL_NEAREST, GLenum wrapS=GL_CLAMP, GLenum wrapT=GL_CLAMP, int mipmap=0)
Allocates an empty texture.
void EnableDepthClearance(float depth=1.0f)