Debian Codegraph @

vcg/wrap/gl/fbo.h

raw symbols SCIP
1 #ifndef __FBO_H__
2 #define __FBO_H__
3
4 #pragma warning(disable : 4250)
5
6 #include <map>
7 #include <vector>
8
9 #include <GL/glew.h>
10 #include <GL/glut.h>
11
12 #include "gl_object.h"
13
14 class FrameBufferSemantic
15 {
16 public:
17 typedef enum
18 {
19 COLOR,
20 DEPTH,
21 STENCIL
22 } FBSType;
23
24 virtual FBSType Semantic(void) const = 0;
25 virtual bool ValidateFormat(GLenum format) const = 0;
26
27 static bool ValidateFormat(FBSType type, GLenum format)
28 {
29 switch (type)
30 {
31 case COLOR : return FrameBufferSemantic::ValidateColor(format);
32 case DEPTH : return FrameBufferSemantic::ValidateDepth(format);
33 case STENCIL : return FrameBufferSemantic::ValidateStencil(format);
34 default : return false;
35 }
36 }
37
38 static bool ValidateColor(GLenum type)
39 {
40 return true;
41 }
42
43 static bool ValidateDepth(GLenum type)
44 {
45 return true;
46 }
47
48 static bool ValidateStencil(GLenum type)
49 {
50 return true;
51 }
52 };
53
54 class Texture : public GLObject, public Bindable, public FrameBufferSemantic
55 {
56 public:
57 Texture(void) : GLObject(), Bindable(), FrameBufferSemantic()
58 {
59 this->format = GL_NONE;
60 }
61
62 void Gen(void)
63 {
64 this->Del();
65 glGenTextures(1, &(this->objectID));
66 }
67
68 void Del(void)
69 {
70 if (this->objectID == 0) return;
71 glDeleteTextures(1, &(this->objectID));
72 this->objectID = 0;
73 }
74
75 GLenum Format(void) const
76 {
77 return this->format;
78 }
79
80 virtual GLint Dimensions(void) const = 0;
81
82 virtual GLsizei Size(const unsigned int i) const = 0;
83
84 virtual GLenum Target(void) const = 0;
85
86 protected:
87 GLenum format;
88
89 void DoBind(void)
90 {
91 glBindTexture(this->Target(), this->objectID);
92 }
93
94 void DoUnbind(void)
95 {
96 glBindTexture(this->Target(), 0);
97 }
98 };
99
100 class ColorTexture : public virtual Texture
101 {
102 public:
103 ColorTexture(void) : Texture()
104 {
105 }
106
107 FrameBufferSemantic::FBSType Semantic(void) const
108 {
109 return FrameBufferSemantic::COLOR;
110 }
111
112 bool ValidateFormat(GLenum format) const
113 {
114 return FrameBufferSemantic::ValidateColor(format);
115 }
116 };
117
118 class DepthTexture : public virtual Texture
119 {
120 public:
121 DepthTexture(void) : Texture()
122 {
123 }
124
125 FrameBufferSemantic::FBSType Semantic(void) const
126 {
127 return FrameBufferSemantic::DEPTH;
128 }
129
130 bool ValidateFormat(GLenum format) const
131 {
132 return FrameBufferSemantic::ValidateDepth(format);
133 }
134 };
135
136 class StencilTexture : public virtual Texture
137 {
138 public:
139 StencilTexture(void) : Texture()
140 {
141 }
142
143 FrameBufferSemantic::FBSType Semantic(void) const
144 {
145 return FrameBufferSemantic::STENCIL;
146 }
147
148 bool ValidateFormat(GLenum format) const
149 {
150 return FrameBufferSemantic::ValidateStencil(format);
151 }
152 };
153
154 class Texture1D : public virtual Texture
155 {
156 public:
157 Texture1D(void) : Texture()
158 {
159 this->dims[0] = 0;
160 this->wraps[0] = GL_CLAMP_TO_EDGE;
161 }
162
163 GLsizei Width(void) const
164 {
165 return this->dims[0];
166 }
167
168 GLint Dimensions(void) const
169 {
170 return 1;
171 }
172
173 GLsizei Size(const unsigned int i) const
174 {
175 if (i > 0) return 0;
176 return this->dims[0];
177 }
178
179 GLenum Target(void) const
180 {
181 return GL_TEXTURE_1D;
182 }
183
184 bool Set(GLint level, GLint internalFormat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid * pixels)
185 {
186 if (!this->ValidateFormat(internalFormat)) return false;
187
188 this->Bind();
189
190 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
191 glTexImage1D(GL_TEXTURE_1D, level, internalFormat, width, border, format, type, pixels);
192
193 this->Unbind();
194
195 this->format = internalFormat;
196 this->dims[0] = width;
197
198 return true;
199 }
200
201 protected:
202 GLsizei dims[1];
203 GLenum wraps[1];
204 };
205
206 class Texture2D : public virtual Texture
207 {
208 public:
209 Texture2D(void) : Texture()
210 {
211 this->dims[0] = 0;
212 this->dims[1] = 0;
213 }
214
215 GLsizei Width(void) const
216 {
217 return this->dims[0];
218 }
219
220 GLsizei Height(void) const
221 {
222 return this->dims[1];
223 }
224
225 GLint Dimensions(void) const
226 {
227 return 2;
228 }
229
230 GLsizei Size(const unsigned int i) const
231 {
232 if (i > 1) return 0;
233 return this->dims[i];
234 }
235
236 GLenum Target(void) const
237 {
238 return GL_TEXTURE_2D;
239 }
240
241 bool Set(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid * pixels)
242 {
243 if (!this->ValidateFormat(internalFormat)) return false;
244
245 this->Bind();
246
247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
248 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, border, format, type, pixels);
249
250 this->Unbind();
251
252 this->format = internalFormat;
253 this->dims[0] = width;
254 this->dims[1] = height;
255
256 return true;
257 }
258
259 protected:
260 GLsizei dims[2];
261
262 void DoBind(void)
263 {
264 glBindTexture(GL_TEXTURE_2D, this->objectID);
265 }
266
267 void DoUnbind(void)
268 {
269 glBindTexture(GL_TEXTURE_2D, 0);
270 }
271 };
272
273 class Texture3D : public virtual Texture
274 {
275 public:
276 Texture3D(void) : Texture()
277 {
278 this->dims[0] = 0;
279 this->dims[1] = 0;
280 this->dims[2] = 0;
281 }
282
283 GLsizei Width(void) const
284 {
285 return this->dims[0];
286 }
287
288 GLsizei Height(void) const
289 {
290 return this->dims[1];
291 }
292
293 GLsizei Depth(void) const
294 {
295 return this->dims[2];
296 }
297
298 GLint Dimensions(void) const
299 {
300 return 3;
301 }
302
303 GLsizei Size(const unsigned int i) const
304 {
305 if (i > 2) return 0;
306 return this->dims[i];
307 }
308
309 GLenum Target(void) const
310 {
311 return GL_TEXTURE_3D;
312 }
313
314 bool Set(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid * pixels)
315 {
316 if (!this->ValidateFormat(internalFormat)) return false;
317
318 this->Bind();
319
320 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
321 glTexImage3D(GL_TEXTURE_3D, 0, internalFormat, width, height, depth, border, format, type, pixels);
322
323 this->Unbind();
324
325 this->format = internalFormat;
326 this->dims[0] = width;
327 this->dims[1] = height;
328 this->dims[2] = depth;
329
330 return true;
331 }
332
333 protected:
334 GLsizei dims[3];
335
336 void DoBind(void)
337 {
338 glBindTexture(GL_TEXTURE_3D, this->objectID);
339 }
340
341 void DoUnbind(void)
342 {
343 glBindTexture(GL_TEXTURE_3D, 0);
344 }
345 };
346
347 class ColorTexture1D : public virtual ColorTexture, public virtual Texture1D
348 {
349 public:
350 ColorTexture1D(void) : ColorTexture(), Texture1D()
351 {
352 }
353 };
354
355 class ColorTexture2D : public virtual ColorTexture, public virtual Texture2D
356 {
357 public:
358 ColorTexture2D(void) : ColorTexture(), Texture2D()
359 {
360 }
361 };
362
363 class ColorTexture3D : public virtual ColorTexture, public virtual Texture3D
364 {
365 public:
366 ColorTexture3D(void) : ColorTexture(), Texture3D()
367 {
368 }
369 };
370
371 class DepthTexture2D : public virtual DepthTexture, public virtual Texture2D
372 {
373 public:
374 DepthTexture2D(void) : DepthTexture(), Texture2D()
375 {
376 }
377 };
378
379 class StencilTexture2D : public virtual StencilTexture, public virtual Texture2D
380 {
381 public:
382 StencilTexture2D(void) : StencilTexture(), Texture2D()
383 {
384 }
385 };
386
387 class FrameBuffer;
388
389 class RenderTarget : public GLObject, public Bindable, public FrameBufferSemantic
390 {
391 friend class FrameBuffer;
392
393 public:
394 typedef enum
395 {
396 BUFFER,
397 TEXTURE
398 } RTStorageType;
399
400 RenderTarget(void) : GLObject(), Bindable(), FrameBufferSemantic()
401 {
402 this->frameBuffer = 0;
403 }
404
405 bool Attach(FrameBuffer * fb);
406
407 bool Detach(void);
408
409 FrameBuffer * GetFrameBuffer(void)
410 {
411 return this->frameBuffer;
412 }
413
414 const FrameBuffer * GetFrameBuffer(void) const
415 {
416 return this->frameBuffer;
417 }
418
419 virtual GLsizei Width(void) const = 0;
420 virtual GLsizei Height(void) const = 0;
421 virtual GLenum Format(void) const = 0;
422
423 virtual GLenum Attachment(void) const = 0;
424
425 virtual bool ValidateAttachment(GLenum attachment) const = 0;
426
427 virtual RTStorageType StorageType(void) const = 0;
428
429 protected:
430 FrameBuffer * frameBuffer;
431
432 virtual bool BindToFB(void) = 0;
433 };
434
435 class BufferRenderTarget : public virtual RenderTarget
436 {
437 public:
438 BufferRenderTarget(void) : RenderTarget()
439 {
440 this->width = 0;
441 this->height = 0;
442 this->format = GL_NONE;
443 }
444
445 void Gen(void)
446 {
447 this->Del();
448 glGenRenderbuffersEXT(1, &(this->objectID));
449 }
450
451 void Del(void)
452 {
453 if (this->objectID == 0) return;
454 glDeleteRenderbuffersEXT(1, &(this->objectID));
455 this->objectID = 0;
456 }
457
458 GLsizei Width(void) const
459 {
460 return this->width;
461 }
462
463 GLsizei Height(void) const
464 {
465 return this->height;
466 }
467
468 GLenum Format(void) const
469 {
470 return this->format;
471 }
472
473 RTStorageType StorageType(void) const
474 {
475 return RenderTarget::BUFFER;
476 }
477
478 bool Set(GLenum format, GLsizei width, GLsizei height)
479 {
480 if (!this->ValidateFormat(format)) return false;
481
482 this->Bind();
483
484 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, format, width, height);
485
486 this->Unbind();
487
488 this->format = format;
489 this->width = width;
490 this->height = height;
491
492 return true;
493 }
494
495 bool BindToFB(void)
496 {
497 if (this->frameBuffer == 0) return false;
498 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, this->Attachment(), GL_RENDERBUFFER_EXT, this->objectID);
499
500 return true;
501 }
502
503 protected:
504 GLenum format;
505 GLsizei width;
506 GLsizei height;
507
508 void DoBind(void)
509 {
510 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, this->objectID);
511 }
512
513 void DoUnbind(void)
514 {
515 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
516 }
517 };
518
519 class TextureRenderTarget : public virtual RenderTarget
520 {
521 public:
522 TextureRenderTarget(void) : RenderTarget()
523 {
524 this->tex = 0;
525 this->level = 0;
526 }
527
528 void Gen(void)
529 {
530 }
531
532 void Del(void)
533 {
534 }
535
536 GLsizei Width(void) const
537 {
538 if (this->tex == 0) return 0;
539 return this->tex->Width();
540 }
541
542 GLsizei Height(void) const
543 {
544 if (this->tex == 0) return 0;
545 return this->tex->Height();
546 }
547
548 GLenum Format(void) const
549 {
550 if (this->tex == 0) return GL_NONE;
551 return this->tex->Format();
552 }
553
554 RTStorageType StorageType(void) const
555 {
556 return RenderTarget::TEXTURE;
557 }
558
559 void SetLevel(GLint level)
560 {
561 if (level < 0) level = 0;
562 this->level = level;
563 }
564
565 bool Set(Texture2D * tex)
566 {
567 this->Unset();
568
569 if (tex == 0) return true;
570 if (this->Semantic() != tex->Semantic()) return false;
571
572 this->tex = tex;
573
574 return true;
575 }
576
577 bool Unset(void)
578 {
579 this->tex = 0;
580
581 return true;
582 }
583
584 Texture2D * GetTexture(void)
585 {
586 return (this->tex);
587 }
588
589 bool BindToFB(void)
590 {
591 if (this->frameBuffer == 0) return false;
592 if (this->tex == 0) return false;
593
594 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, this->Attachment(), GL_TEXTURE_2D, this->tex->ObjectID(), this->level);
595
596 return true;
597 }
598
599 protected:
600
601 Texture2D * tex;
602 GLint level;
603
604 void DoBind(void)
605 {
606 }
607
608 void DoUnbind(void)
609 {
610 }
611 };
612
613 class ColorRenderTarget : public virtual RenderTarget
614 {
615 public:
616 ColorRenderTarget(void) : RenderTarget()
617 {
618 this->attachment = GL_COLOR_ATTACHMENT0_EXT;
619 }
620
621 FrameBufferSemantic::FBSType Semantic(void) const
622 {
623 return FrameBufferSemantic::COLOR;
624 }
625
626 bool ValidateFormat(GLenum format) const
627 {
628 return FrameBufferSemantic::ValidateColor(format);
629 }
630
631 bool ValidateAttachment(GLenum attachment) const
632 {
633 return (((GL_COLOR_ATTACHMENT0_EXT) <= attachment) && (attachment <= (GL_COLOR_ATTACHMENT0_EXT + 3)));
634 }
635
636 void SetAttachment(GLenum attachment)
637 {
638 if (!this->ValidateAttachment(attachment)) return;
639 this->attachment = attachment;
640 }
641
642 GLenum Attachment(void) const
643 {
644 return this->attachment;
645 }
646
647 protected:
648 GLenum attachment;
649 };
650
651 class DepthRenderTarget : public virtual RenderTarget
652 {
653 public:
654 DepthRenderTarget(void) : RenderTarget()
655 {
656 }
657
658 FrameBufferSemantic::FBSType Semantic(void) const
659 {
660 return FrameBufferSemantic::DEPTH;
661 }
662
663 bool ValidateFormat(GLenum format) const
664 {
665 return FrameBufferSemantic::ValidateDepth(format);
666 }
667
668 bool ValidateAttachment(GLenum attachment) const
669 {
670 return (attachment == GL_DEPTH_ATTACHMENT_EXT);
671 }
672
673 GLenum Attachment(void) const
674 {
675 return GL_DEPTH_ATTACHMENT_EXT;
676 }
677 };
678
679 class StencilRenderTarget : public virtual RenderTarget
680 {
681 public:
682 StencilRenderTarget(void) : RenderTarget()
683 {
684 }
685
686 FrameBufferSemantic::FBSType Semantic(void) const
687 {
688 return FrameBufferSemantic::STENCIL;
689 }
690
691 bool ValidateFormat(GLenum format) const
692 {
693 return FrameBufferSemantic::ValidateStencil(format);
694 }
695
696 bool ValidateAttachment(GLenum attachment) const
697 {
698 return (attachment == GL_STENCIL_ATTACHMENT_EXT);
699 }
700
701 GLenum Attachment(void) const
702 {
703 return GL_STENCIL_ATTACHMENT_EXT;
704 }
705 };
706
707 class ColorRenderBuffer : public virtual ColorRenderTarget, public virtual BufferRenderTarget
708 {
709 public:
710 ColorRenderBuffer(void) : ColorRenderTarget(), BufferRenderTarget()
711 {
712 }
713 };
714
715 class ColorRenderTexture : public virtual ColorRenderTarget, public virtual TextureRenderTarget
716 {
717 public:
718 ColorRenderTexture(void) : ColorRenderTarget(), TextureRenderTarget()
719 {
720 }
721
722 ColorRenderTexture(Texture2D * tex) : ColorRenderTarget(), TextureRenderTarget()
723 {
724 this->Set(tex);
725 }
726 };
727
728 class DepthRenderBuffer : public virtual DepthRenderTarget, public virtual BufferRenderTarget
729 {
730 public:
731 DepthRenderBuffer(void) : DepthRenderTarget(), BufferRenderTarget()
732 {
733 }
734 };
735
736 class DepthRenderTexture : public virtual DepthRenderTarget, public virtual TextureRenderTarget
737 {
738 public:
739 DepthRenderTexture(void) : DepthRenderTarget(), TextureRenderTarget()
740 {
741 }
742
743 DepthRenderTexture(Texture2D * tex) : DepthRenderTarget(), TextureRenderTarget()
744 {
745 this->Set(tex);
746 }
747 };
748
749 class StencilRenderBuffer : public virtual StencilRenderTarget, public virtual BufferRenderTarget
750 {
751 public:
752 StencilRenderBuffer(void) : StencilRenderTarget(), BufferRenderTarget()
753 {
754 }
755 };
756
757 class StencilRenderTexture : public virtual StencilRenderTarget, public virtual TextureRenderTarget
758 {
759 public:
760 StencilRenderTexture(void) : StencilRenderTarget(), TextureRenderTarget()
761 {
762 }
763
764 StencilRenderTexture(Texture2D * tex) : StencilRenderTarget(), TextureRenderTarget()
765 {
766 this->Set(tex);
767 }
768 };
769
770 class FrameBuffer : public GLObject, public Bindable
771 {
772 friend class RenderTarget;
773
774 public:
775 FrameBuffer(void) : GLObject(), Bindable()
776 {
777 }
778
779 void Gen(void)
780 {
781 this->Del();
782 glGenFramebuffersEXT(1, &(this->objectID));
783 }
784
785 void Del(void)
786 {
787 if (this->objectID == 0) return;
788 glDeleteFramebuffersEXT(1, &(this->objectID));
789 this->objectID = 0;
790 }
791
792 bool DetachAll(void)
793 {
794 return false;
795 }
796
797 bool IsValid(void) const
798 {
799 const GLenum s = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
800 //return (s == GL_FRAMEBUFFER_COMPLETE_EXT);
801
802 switch (s)
803 {
804 case GL_FRAMEBUFFER_COMPLETE_EXT:
805 printf("ok\n");
806 break;
807 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
808 printf("i a\n");
809 break;
810 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
811 printf("i m a\n");
812 break;
813 case GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT:
814 printf("i d a\n");
815 break;
816 case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
817 printf("i d\n");
818 break;
819 case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
820 printf("i f\n");
821 break;
822 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
823 printf("i d b\n");
824 break;
825 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
826 printf("i r b\n");
827 break;
828 case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
829 printf("u\n");
830 break;
831 default:
832 printf("def\n");
833 break;
834 }
835
836 return (s == GL_FRAMEBUFFER_COMPLETE_EXT);
837 }
838
839 protected:
840 typedef std::map<GLenum, RenderTarget *> RTMap;
841 typedef RTMap::iterator RTMap_i;
842 typedef RTMap::const_iterator RTMap_ci;
843
844 RTMap renderTargets;
845
846 void DoBind(void)
847 {
848 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, this->objectID);
849
850 std::vector<GLenum> colorDrawBuffers;
851 colorDrawBuffers.reserve(this->renderTargets.size());
852
853 for (RTMap_i rt=this->renderTargets.begin(); rt!=this->renderTargets.end(); ++rt)
854 {
855 RenderTarget * prt = (*rt).second;
856 if (prt->Semantic() == FrameBufferSemantic::COLOR)
857 {
858 colorDrawBuffers.push_back(prt->Attachment());
859 }
860 prt->BindToFB();
861 }
862
863 const GLsizei sz = (GLsizei)(colorDrawBuffers.size());
864 if (sz > 0)
865 {
866 glDrawBuffers(sz, &(colorDrawBuffers[0]));
867 }
868 }
869
870 void DoUnbind(void)
871 {
872 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
873 }
874
875 bool AddRT(RenderTarget * rt)
876 {
877 if (rt == 0) return false;
878 RTMap_i it = this->renderTargets.find(rt->Attachment());
879 if (it == this->renderTargets.end())
880 {
881 this->renderTargets.insert(std::make_pair(rt->Attachment(), rt));
882 return true;
883 }
884 return false;
885 }
886
887 bool RemoveRT(RenderTarget * rt)
888 {
889 if (rt == 0) return false;
890 RTMap_i it = this->renderTargets.find(rt->Attachment());
891 if ((*it).second == rt)
892 {
893 this->renderTargets.erase(it);
894 return true;
895 }
896 return false;
897 }
898 };
899
900
901
902 bool RenderTarget::Attach(FrameBuffer * fb)
903 {
904 this->Detach();
905 if (fb == 0) return true;
906
907 if (fb->AddRT(this))
908 {
909 this->frameBuffer = fb;
910 return true;
911 }
912
913 return false;
914 }
915
916 bool RenderTarget::Detach(void)
917 {
918 if (this->frameBuffer == 0) return false;
919 this->frameBuffer->RemoveRT(this);
920 this->frameBuffer = 0;
921
922 return true;
923 }
924
925 #endif // __FBO_H__