libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moFBManager.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moFBManager.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28  Andrés Colubri
29 
30 *******************************************************************************/
31 
32 #include "moFBManager.h"
33 
34 //===========================================
35 //
36 // moFBManager
37 //
38 //===========================================
39 
41 {
44  SetName("fbmanager");
45  SetLabelName("fbmanager");
46  m_glmanager = NULL;
47 }
48 
50 {
51  Finish();
52 }
53 
55 {
56  if ( m_pResourceManager ) {
57  m_glmanager = m_pResourceManager->GetGLMan();
58  }
59  m_fbo_array.Init(0, NULL);
60  GetScreenBuffers();
61  m_fbo_stack_index = -1;
62  m_current_fbo = -1;
63  return (m_glmanager!=NULL);
64 }
65 
67 {
68  m_fbo_array.Finish();
69  return true;
70 }
71 
73 {
74  moFBO* new_fbo = new moFBO();
75  new_fbo->Init(m_glmanager);
76  m_fbo_array.Add(new_fbo);
77  return m_fbo_array.Count() - 1;
78 }
79 
81 {
82  if ( p_fbo!=(MOuint)MO_UNDEFINED && p_fbo < m_fbo_array.Count())
83  {
84  delete m_fbo_array.GetRef(p_fbo);
85  m_fbo_array.Remove(p_fbo);
86  UnbindAllFBO();
87  return true;
88  }
89  else return false;
90 }
91 
92 MOint moFBManager::GetFBO(MOuint p_target, MOuint p_internal_format, MOuint p_width, MOuint p_height)
93 {
94  for (MOuint i = 0; i < m_fbo_array.Count(); i++)
95  {
96  if ((m_fbo_array.Get(i)->GetTarget() == p_target) &&
97  (m_fbo_array.Get(i)->GetTarget() == p_internal_format) &&
98  (m_fbo_array.Get(i)->GetTarget() == p_width) &&
99  (m_fbo_array.Get(i)->GetTarget() == p_height))
100  return i;
101  }
102  return -1;
103 }
104 
106 {
107  if (m_glmanager->GetCurrentFBO() != 0)
108  {
109  m_fbo_stack_index++;
110  m_fbo_stack[m_fbo_stack_index] = (MOuint)m_current_fbo;
111 
112  m_draw_buffer_stack[m_fbo_stack_index] = m_glmanager->GetCurrentDrawBuffer();
113  m_read_buffer_stack[m_fbo_stack_index] = m_glmanager->GetCurrentReadBuffer();
114 
115  m_glmanager->SetCurrentFBO(0);
116  m_glmanager->SetCurrentDrawBuffer(m_screen_draw_buffer);
117  m_glmanager->SetCurrentReadBuffer(m_screen_read_buffer);
118  m_current_fbo = -1;
119  }
120 }
121 
123 {
124  if (m_glmanager->GetCurrentFBO() == 0) UnbindFBO();
125 }
126 
128 {
129  if ( p_fbo!=(MOuint)MO_UNDEFINED && p_fbo < m_fbo_array.Count())
130  {
131  m_fbo_stack_index++;
132  m_fbo_stack[m_fbo_stack_index] = m_current_fbo;
133 
134  m_draw_buffer_stack[m_fbo_stack_index] = m_glmanager->GetCurrentDrawBuffer();
135  m_read_buffer_stack[m_fbo_stack_index] = m_glmanager->GetCurrentReadBuffer();
136 
137  m_fbo_array[p_fbo]->Bind();
138  m_current_fbo = p_fbo;
139  return true;
140  }
141  else return false; // Wrong FBO index.
142 }
143 
145 {
146  if (BindFBO(p_fbo))
147  {
148  m_fbo_array[p_fbo]->SetDrawTexture(p_attach_point);
149  return true;
150  }
151  else return false;
152 }
153 
154 MOboolean moFBManager::BindFBO(MOuint p_fbo, MOuint p_num_attach_points, MOuint *p_attach_point_list)
155 {
156  if (BindFBO(p_fbo))
157  {
158  SetDrawTextures(p_num_attach_points, p_attach_point_list);
159  return true;
160  }
161  else return false;
162 }
163 
164 MOboolean moFBManager::AddTexture(MOuint p_fbo, moTexture* p_pTex, MOuint &p_attach_point)
165 {
166  if ( p_fbo!=(MOuint)MO_UNDEFINED && p_fbo < m_fbo_array.Count())
167  {
168  m_fbo_array[p_fbo]->AddTexture(p_pTex->GetWidth(), p_pTex->GetHeight(), p_pTex->GetTexParam(), p_pTex->GetGLId(), p_attach_point);
169  return true;
170  }
171  else return false;
172 }
173 
175 {
176  if ( p_fbo!=(MOuint)MO_UNDEFINED && p_fbo < m_fbo_array.Count())
177  {
178  m_fbo_array[p_fbo]->AttachTexture(p_pTex->GetWidth(), p_pTex->GetHeight(), p_pTex->GetTexParam(), p_pTex->GetGLId(), p_attach_point);
179  return true;
180  }
181  else return false;
182 }
183 
185 {
186  if (-1 < m_current_fbo) m_fbo_array[m_current_fbo]->SetDrawTexture(p_attach_point);
187 }
188 
189 void moFBManager::SetDrawTextures(MOuint p_num_attach_points, MOuint *p_attach_point_list)
190 {
191  if (-1 < m_current_fbo)
192  {
193  GLsizei n = p_num_attach_points;
194 
195  for (int i = 0; i < n; i++) m_color_buffers[i] = m_fbo_array[m_current_fbo]->GetColorAttachPoint(p_attach_point_list[i]);
196 #ifndef OPENGLESV2
197  glDrawBuffers(n, m_color_buffers);
198 #endif
199  }
200 }
201 
203 {
204  if (-1 < m_current_fbo) m_fbo_array[m_current_fbo]->SetReadTexture(p_attach_point);
205 }
206 
207 void moFBManager::SetDrawTextureByGLId(MOuint p_glid, MOuint p_width, MOuint p_height, const moTexParam& p_param, MOuint p_def_attach_point)
208 {
209  if (-1 < m_current_fbo) m_fbo_array[m_current_fbo]->SetDrawTexture(p_glid, p_width, p_height, p_param, p_def_attach_point);
210 }
211 
212 void moFBManager::SetDrawTexturesByGLId(MOuint p_num_glids, MOuint *p_glid_list, MOuint p_width, MOuint p_height, const moTexParam& p_param)
213 {
214  if (-1 < m_current_fbo)
215  {
216  GLsizei n = p_num_glids;
217 
218  for (int i = 0; i < n; i++)
219  m_fbo_array[m_current_fbo]->SetDrawTexture(p_glid_list[i], p_width, p_height, p_param, i);
220 
221 
222  for (int i = 0; i < n; i++) m_color_buffers[i] = m_fbo_array[m_current_fbo]->GetColorAttachPoint(i);
223 #ifndef OPENGLESV2
224  glDrawBuffers(n, m_color_buffers);
225 #endif
226  }
227 }
228 
229 void moFBManager::SetReadTextureByGLId(MOuint p_glid, MOuint p_width, MOuint p_height, const moTexParam& p_param, MOuint p_def_attach_point)
230 {
231  if (-1 < m_current_fbo) m_fbo_array[m_current_fbo]->SetReadTexture(p_glid, p_width, p_height, p_param, p_def_attach_point);
232 }
233 
234 MOint moFBManager::SetDrawTexture(moTexture* p_pTex, MOuint p_def_attach_point)
235 {
236  MOint result = 1;
237  if ((-1 < m_current_fbo) && (p_pTex != NULL))
238  {
239  if (p_pTex->GetFBO() == NULL)
240  // Attaching by GL id to the currently binded fbo.
241  SetDrawTextureByGLId(p_pTex->GetGLId(), p_pTex->GetWidth(), p_pTex->GetHeight(), p_pTex->GetTexParam(), p_def_attach_point);
242  else
243  // The texture is associated to a particular fbo.
244  if (p_pTex->GetFBO() == m_fbo_array[m_current_fbo])
245  if ( (MOuint)MO_UNDEFINED != p_pTex->GetFBOAttachPoint() )
246  // The texture is associated to a particular attachement point.
248  else
249  // Attaching by GL id to the currently binded fbo.
250  SetDrawTextureByGLId(p_pTex->GetGLId(), p_pTex->GetWidth(), p_pTex->GetHeight(), p_pTex->GetTexParam(), p_def_attach_point);
251  else result = 0; // Failure the currently binded fbo is different to the fbo assigned to the texture.
252  }
253  else result = 0; // There is no fbo currently binded or the texture is NULL.
254  return result;
255 }
256 
257 MOint moFBManager::SetDrawTextures(moTextureArray &p_tex_array)
258 {
259  MOint result = 1;
260  if (-1 < m_current_fbo)
261  {
262  GLsizei n = p_tex_array.Count();
263 
264  for (int i = 0; i < n; i++)
265  if (p_tex_array[i] != NULL)
266  m_fbo_array[m_current_fbo]->SetDrawTexture(p_tex_array[i]->GetGLId(), p_tex_array[i]->GetWidth(), p_tex_array[i]->GetHeight(), p_tex_array[i]->GetTexParam(), i);
267 
268 
269  for (int i = 0; i < n; i++) m_color_buffers[i] = m_fbo_array[m_current_fbo]->GetColorAttachPoint(i);
270 #ifndef OPENGLESV2
271  glDrawBuffers(n, m_color_buffers);
272 #endif
273  }
274  return result;
275 }
276 
277 MOint moFBManager::SetReadTexture(moTexture* p_pTex, MOuint p_def_attach_point)
278 {
279  MOint result = 1;
280  if ((-1 < m_current_fbo) && (p_pTex != NULL))
281  {
282  if (p_pTex->GetFBO() == NULL)
283  // Attaching by GL id to the currently binded fbo.
284  SetReadTextureByGLId(p_pTex->GetGLId(), p_pTex->GetWidth(), p_pTex->GetHeight(), p_pTex->GetTexParam(), p_def_attach_point);
285  else
286  // The texture is associated to a particular fbo.
287  if (p_pTex->GetFBO() == m_fbo_array[m_current_fbo])
288  if ( (MOuint)MO_UNDEFINED != p_pTex->GetFBOAttachPoint())
289  // The texture is associated to a particular attachement point.
291  else
292  // Attaching by GL id to the currently binded fbo.
293  SetReadTextureByGLId(p_pTex->GetGLId(), p_pTex->GetWidth(), p_pTex->GetHeight(), p_pTex->GetTexParam(), p_def_attach_point);
294  else result = 0; // Failure the currently binded fbo is different to the fbo assigned to the texture.
295  }
296  else result = 0; // There is no fbo currently binded or the texture is NULL.
297  return result;
298 }
299 
301 {
302  if (0 < m_fbo_stack_index)
303  {
304  MOuint previous_fbo = m_fbo_stack[m_fbo_stack_index];
305  m_fbo_array[previous_fbo]->Bind();
306  m_current_fbo = previous_fbo;
307 
308  MOuint prev_draw_buffer = m_draw_buffer_stack[m_fbo_stack_index];
309  MOuint prev_read_buffer = m_read_buffer_stack[m_fbo_stack_index];
310 
311  m_glmanager->SetCurrentDrawBuffer(prev_draw_buffer);
312  m_glmanager->SetCurrentReadBuffer(prev_read_buffer);
313 
314  m_fbo_stack_index--;
315  }
316  else
317  {
318  m_glmanager->SetCurrentFBO(0);
319  m_glmanager->SetCurrentDrawBuffer(m_screen_draw_buffer);
320  m_glmanager->SetCurrentReadBuffer(m_screen_read_buffer);
321  m_fbo_stack_index = -1;
322  m_current_fbo = -1;
323  }
324 }
325 
327 {
328  m_glmanager->SetCurrentFBO(0);
329  m_glmanager->SetCurrentDrawBuffer(m_screen_draw_buffer);
330  m_glmanager->SetCurrentReadBuffer(m_screen_read_buffer);
331  m_fbo_stack_index = -1;
332  m_current_fbo = -1;
333 }
334 
335 void moFBManager::GetScreenBuffers()
336 {
337 #ifndef OPENGLESV2
338  glGetIntegerv(GL_DRAW_BUFFER, &m_screen_draw_buffer);
339  glGetIntegerv(GL_READ_BUFFER, &m_screen_read_buffer);
340 #endif
341 }
342 
Parámetros internos de una textura.
Definition: moTypes.h:543
MOuint GetCurrentFBO()
Definition: moGLManager.h:348
void BindScreenFB()
void SetDrawTexture(MOuint p_attach_point)
void SetName(const moText &p_name)
void UnbindScreenFB()
#define MO_UNDEFINED
Definition: moTypes.h:379
void SetDrawTextures(MOuint p_num_attach_points, MOuint *p_attach_point_list)
#define MOboolean
Definition: moTypes.h:385
virtual MOboolean Init(moGLManager *p_gl)
Definition: moFBO.cpp:58
void SetResourceType(moResourceType p_restype)
moFBO * GetFBO() const
Definition: moTexture.h:208
void SetReadTexture(MOuint p_attach_point)
moGLManager * GetGLMan()
void SetDrawTextureByGLId(MOuint p_glid, MOuint p_width, MOuint p_height, const moTexParam &p_param, MOuint p_def_attach_point=0)
Definition: moFBO.h:60
MOboolean DeleteFBO(MOuint p_fbo)
Definition: moFBManager.cpp:80
moResourceManager * m_pResourceManager
Puntero al administrador de recursos.
MOuint CreateFBO()
Definition: moFBManager.cpp:72
Dispositivo de entrada/salida, típicamente, interfaces humanas de IO y datos ( teclado, mouse, tableta, tcp, udp, serial )
Definition: moTypes.h:532
void SetCurrentDrawBuffer(MOint p_buffer)
void SetCurrentReadBuffer(MOint p_buffer)
void SetCurrentFBO(MOuint m_fbo)
#define MOint
Definition: moTypes.h:388
void SetDrawTexturesByGLId(MOuint p_num_glids, MOuint *p_glid_list, MOuint p_width, MOuint p_height, const moTexParam &p_param)
MOboolean AttachTexture(MOuint p_fbo, moTexture *p_pTex, MOuint p_attach_point)
MOuint GetGLId() const
Definition: moTexture.h:224
clase base para el manejo de una textura
Definition: moTexture.h:78
virtual MOboolean Init()
Definition: moFBManager.cpp:54
void SetLabelName(const moText &p_labelname)
void UnbindFBO()
moFBO * GetFBO(MOuint p_fbo)
Definition: moFBManager.h:93
MOboolean AddTexture(MOuint p_fbo, moTexture *p_pTex, MOuint &p_attach_point)
MOuint GetHeight() const
Definition: moTexture.h:261
MOuint GetFBOAttachPoint() const
Definition: moTexture.h:213
MOint GetCurrentReadBuffer()
Definition: moGLManager.h:359
#define MOuint
Definition: moTypes.h:387
void UnbindAllFBO()
virtual ~moFBManager()
Definition: moFBManager.cpp:49
MOuint GetWidth() const
Definition: moTexture.h:256
virtual MOboolean Finish()
Definition: moFBManager.cpp:66
MOint GetCurrentDrawBuffer()
Definition: moGLManager.h:369
void SetType(moMoldeoObjectType p_type)
void SetReadTextureByGLId(MOuint p_glid, MOuint p_width, MOuint p_height, const moTexParam &p_param, MOuint p_def_attach_point=0)
MOboolean BindFBO(MOuint p_fbo)
moTexParam GetTexParam() const
Definition: moTexture.h:307