Debian Codegraph @

vcg/wrap/gl/gl_surface.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23
24 /****************************************************************************
25 History
26
27 $Log: gl_surface.h,v $
28 Revision 1.2 2007/10/16 13:31:23 m_di_benedetto
29 Added a GLenum cast to suppress warning message.
30
31 Revision 1.1 2007/07/26 16:22:47 m_di_benedetto
32 First Commit.
33
34
35
36 ****************************************************************************/
37
38 #ifndef VCGLIB_GL_SURFACE_H
39 #define VCGLIB_GL_SURFACE_H
40
41 #include <vector>
42 #include <GL/glew.h>
43
44 namespace vcg
45 {
46
47 /****************************************************************************
48 The gl_surface class simplify the render-to-texture OpenGL functionality.
49
50 It provides a framebuffer composed of single or multiple color buffers and
51 a depth buffer; color-only and depth-only framebuffers can also be created.
52
53 Sample usage:
54
55 ****************************************************************
56 // *** declaration
57
58 gl_surface my_srf;
59
60
61 ***********************
62 // *** initialization: single color render target, with depth buffer
63 {
64 std::vector<GLenum> color_formats;
65 color_formats.push_back(GL_RGBA8);
66
67 my_srf.set(width, height, color_formats, GL_DEPTH_COMPONENT);
68 }
69 ***********************
70
71
72 ***********************
73 // *** initialization: two color render targets, without depth buffer
74 // NOTE: the maximum number of color targets is implementation dependent.
75 // NOTE: DX10 class hardware allows different formats for each color target.
76 {
77 std::vector<GLenum> color_formats;
78 color_formats.push_back(GL_RGBA8);
79 color_formats.push_back(GL_RGBA8);
80
81 my_srf.set(width, height, color_formats, GL_NONE);
82 }
83 ***********************
84
85
86 ***********************
87 // *** usage: render-to-targets
88 {
89 my_srf.begin_write();
90 application_draw_code();
91 my_srf.end_write();
92 }
93 ***********************
94
95
96 ***********************
97 // *** usage: using rendered textures
98 {
99 // 2 buffers
100
101 // bind the second
102 glActiveTexture(GL_TEXTURE1);
103 my_srf.begin_read_color(1); // actually does a glBindTexture();
104
105 // bind the first
106 glActiveTexture(GL_TEXTURE0);
107 my_srf.begin_read_color(0);
108
109 application_draw_code();
110
111 // unbind the second
112 glActiveTexture(GL_TEXTURE1);
113 my_srf.end_read_color(1);
114
115 // unbind first
116 glActiveTexture(GL_TEXTURE0);
117 my_srf.end_read_color(0);
118 }
119
120 ***********************
121 // *** usage: use depth map
122 {
123 my_srf.begin_read_depth();
124
125 // use depth map here
126 application_draw_code();
127
128 my_srf.end_read_depth();
129 }
130
131 ***********************
132 // *** usage: cleanup
133 {
134 my_srf.clear();
135 // clear() is also safely called in the destructor.
136 }
137 ***********************
138
139 Other commodity methods for getting/setting pixels are also provided.
140
141 ****************************************************************************/
142
143 class gl_surface
144 {
145 public:
146
147 typedef gl_surface this_type;
148
149 gl_surface(void) : width(0), height(0), depth_tex(0), fb(0)
150 {
151 ;
152 }
153
154 ~gl_surface(void)
155 {
156 this->clear();
157 }
158
159 bool set(int width, int height, const std::vector<GLenum> & color_formats, GLenum depth_format)
160 {
161 this->clear();
162
163 this->width = width;
164 this->height = height;
165
166 this->color_formats = color_formats;
167
168 this->color_texs.resize(color_formats.size());
169
170 for (size_t i=0; i<this->color_texs.size(); ++i)
171 {
172 glGenTextures (1, &(this->color_texs[i]));
173 glBindTexture (GL_TEXTURE_2D, this->color_texs[i]);
174 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
175 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
176 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
177 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
178 glTexImage2D (GL_TEXTURE_2D, 0, this->color_formats[i], this->width, this->height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, 0);
179 }
180
181 this->depth_format = depth_format;
182 if (this->depth_format != GL_NONE)
183 {
184 glGenTextures (1, &(this->depth_tex));
185 glBindTexture (GL_TEXTURE_2D, this->depth_tex);
186 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
187 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
188 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
189 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
190 glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
191 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
192 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
193 glTexImage2D (GL_TEXTURE_2D, 0, this->depth_format, this->width, this->height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0);
194 }
195
196 glGenFramebuffersEXT(1, &(this->fb));
197 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, this->fb);
198
199 std::vector<GLenum> sites(this->color_texs.size());
200 for (size_t i=0; i<this->color_texs.size(); ++i)
201 {
202 sites[i] = GL_COLOR_ATTACHMENT0_EXT + ((GLenum)i);
203 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, sites[i], GL_TEXTURE_2D, this->color_texs[i], 0);
204 }
205
206 if (this->depth_format != GL_NONE)
207 {
208 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, this->depth_tex, 0);
209 }
210
211 if (!sites.empty())
212 {
213 glDrawBuffers((GLsizei)(sites.size()), &(sites[0]));
214 }
215
216 const GLenum s = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
217 const bool res = (s == GL_FRAMEBUFFER_COMPLETE_EXT);
218
219 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
220
221 if (!res)
222 {
223 glDeleteFramebuffersEXT(1, &(this->fb));
224 this->fb = 0;
225
226 for (size_t i=0; i<this->color_texs.size(); ++i)
227 {
228 glDeleteTextures(1, &(this->color_texs[i]));
229 }
230 this->color_texs.clear();
231
232 this->color_formats.clear();
233
234 if (this->depth_tex != 0)
235 {
236 glDeleteTextures(1, &(this->depth_tex));
237 this->depth_tex = 0;
238 }
239
240 this->width = 0;
241 this->height = 0;
242
243 return false;
244 }
245
246 return true;
247 }
248
249 bool set_simple(int width, int height)
250 {
251 std::vector<GLenum> c_formats;
252 c_formats.push_back(GL_RGBA8);
253
254 return this->set(width, height, c_formats, GL_DEPTH_COMPONENT);
255 }
256
257 bool set_color_only(int width, int height, GLenum color_format)
258 {
259 std::vector<GLenum> c_formats;
260 c_formats.push_back(color_format);
261
262 return this->set(width, height, c_formats, GL_NONE);
263 }
264
265 bool set_color_only(int width, int height, const std::vector<GLenum> & color_formats)
266 {
267 return this->set(width, height, color_formats, GL_NONE);
268 }
269
270 bool set_depth_only(int width, int height, GLenum depth_format)
271 {
272 std::vector<GLenum> c_formats;
273
274 return this->set(width, height, c_formats, depth_format);
275 }
276
277 bool clear(void)
278 {
279 if (!this->is_valid()) return false;
280
281 glDeleteFramebuffersEXT(1, &(this->fb));
282 this->fb = 0;
283
284 for (size_t i=0; i<this->color_texs.size(); ++i)
285 {
286 glDeleteTextures(1, &(this->color_texs[i]));
287 }
288 this->color_texs.clear();
289
290 this->color_formats.clear();
291
292 if (this->depth_tex != 0)
293 {
294 glDeleteTextures(1, &(this->depth_tex));
295 this->depth_tex = 0;
296 }
297
298 this->width = 0;
299 this->height = 0;
300
301 return true;
302 }
303
304 bool is_valid(void) const
305 {
306 return (this->fb != 0);
307 }
308
309 int get_width(void) const
310 {
311 return this->width;
312 }
313
314 int get_height(void) const
315 {
316 return this->height;
317 }
318
319 int color_attachments_count(void) const
320 {
321 return ((int)(this->color_texs.size()));
322 }
323
324 GLenum get_color_attachment_format(int attachment) const
325 {
326 if (!this->is_valid()) return GL_NONE;
327 if ((attachment < 0) || (attachment >= this->color_attachments_count())) return GL_NONE;
328
329 return this->color_formats[attachment];
330 }
331
332 bool has_depth_attachment(void) const
333 {
334 return (this->depth_tex != 0);
335 }
336
337 GLenum get_depth_attachment_format(void) const
338 {
339 if (!this->is_valid()) return GL_NONE;
340 if (!this->has_depth_attachment()) return GL_NONE;
341
342 return this->depth_format;
343 }
344
345 bool set_color_pixels(int attachment, GLenum format, GLenum type, const void * pixels)
346 {
347 if (!this->begin_read_color(attachment)) return false;
348
349 glTexImage2D(GL_TEXTURE_2D, 0, this->color_formats[attachment], this->width, this->height, 0, format, type, pixels);
350
351 this->end_read_color(attachment);
352
353 return true;
354 }
355
356 bool get_color_pixels(int attachment, GLenum format, GLenum type, void * pixels)
357 {
358 if (!this->begin_read_color(attachment)) return false;
359
360 glGetTexImage(GL_TEXTURE_2D, 0, format, type, pixels);
361
362 this->end_read_color(attachment);
363
364 return true;
365 }
366
367 bool set_depth_pixels(GLenum format, GLenum type, const void * pixels)
368 {
369 if (!this->is_valid()) return false;
370 if (!this->has_depth_attachment()) return false;
371
372 glTexImage2D(GL_TEXTURE_2D, 0, this->depth_format, this->width, this->height, 0, format, type, pixels);
373
374 return true;
375 }
376
377 bool get_depth_pixels(GLenum format, GLenum type, void * pixels)
378 {
379 if (!this->begin_read_depth()) return false;
380
381 glGetTexImage(GL_TEXTURE_2D, 0, format, type, pixels);
382
383 this->end_read_depth();
384
385 return true;
386 }
387
388 bool begin_read_color(int attachment)
389 {
390 if (!this->is_valid()) return false;
391 if ((attachment < 0) || (attachment >= this->color_attachments_count())) return false;
392
393 glBindTexture(GL_TEXTURE_2D, this->color_texs[attachment]);
394
395 return true;
396 }
397
398 bool end_read_color(int attachment)
399 {
400 if (!this->is_valid()) return false;
401 if ((attachment < 0) || (attachment >= this->color_attachments_count())) return false;
402
403 glBindTexture(GL_TEXTURE_2D, 0);
404
405 return true;
406 }
407
408 bool begin_read_depth(void)
409 {
410 if (!this->is_valid()) return false;
411 if (!this->has_depth_attachment()) return false;
412
413 glBindTexture(GL_TEXTURE_2D, this->depth_tex);
414
415 return true;
416 }
417
418 bool end_read_depth(void)
419 {
420 if (!this->is_valid()) return false;
421 if (!this->has_depth_attachment()) return false;
422
423 glBindTexture(GL_TEXTURE_2D, 0);
424
425 return true;
426 }
427
428 bool begin_write(void)
429 {
430 if (!this->is_valid()) return false;
431
432 glPushAttrib(GL_VIEWPORT_BIT);
433
434 glViewport(0, 0, this->width, this->height);
435
436 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, this->fb);
437
438 return true;
439 }
440
441 bool end_write(void)
442 {
443 if (!this->is_valid()) return false;
444
445 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
446
447 glPopAttrib();
448
449 return true;
450 }
451
452 bool draw_color_attachment(int x, int y, int width, int height, int attachment)
453 {
454 if (!this->is_valid()) return false;
455 if ((attachment < 0) || (attachment >= this->color_attachments_count())) return false;
456
457 glPushAttrib(GL_ALL_ATTRIB_BITS);
458
459 glViewport(x, y, width, height);
460
461 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
462
463 glMatrixMode(GL_PROJECTION);
464 glPushMatrix();
465 glLoadIdentity();
466
467 glMatrixMode(GL_MODELVIEW);
468 glPushMatrix();
469 glLoadIdentity();
470
471 this->begin_read_color(attachment);
472 glBegin(GL_QUADS);
473 glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
474 glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0f, -1.0f);
475 glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f, 1.0f);
476 glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f);
477 glEnd();
478 this->end_read_color(attachment);
479
480 glMatrixMode(GL_PROJECTION);
481 glPopMatrix();
482
483 glMatrixMode(GL_MODELVIEW);
484 glPopMatrix();
485
486 glPopAttrib();
487 }
488
489 bool draw_depth_attachment(int x, int y, int width, int height)
490 {
491 if (!this->is_valid()) return false;
492 if (!this->has_depth_attachment()) return false;
493
494 glPushAttrib(GL_ALL_ATTRIB_BITS);
495
496 glViewport(x, y, width, height);
497
498 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
499
500 glMatrixMode(GL_PROJECTION);
501 glPushMatrix();
502 glLoadIdentity();
503
504 glMatrixMode(GL_MODELVIEW);
505 glPushMatrix();
506 glLoadIdentity();
507
508 this->begin_read_depth();
509 glBegin(GL_QUADS);
510 glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
511 glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0f, -1.0f);
512 glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f, 1.0f);
513 glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f);
514 glEnd();
515 this->end_read_depth();
516
517 glMatrixMode(GL_PROJECTION);
518 glPopMatrix();
519
520 glMatrixMode(GL_MODELVIEW);
521 glPopMatrix();
522
523 glPopAttrib();
524 }
525
526 protected:
527
528 int width;
529 int height;
530 std::vector<GLenum> color_formats;
531 std::vector<GLuint> color_texs;
532 GLenum depth_format;
533 GLuint depth_tex;
534 GLuint fb;
535 };
536
537 } // end namespace vcg
538
539 #endif // VCGLIB_GL_SURFACE_H