Allolib  1.0
C++ Components For Interactive Multimedia
al_FBO.hpp
1 #ifndef INCLUDE_AL_GRAPHICS_FBO_HPP
2 #define INCLUDE_AL_GRAPHICS_FBO_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology, UCSB.
8  Copyright (C) 2012. The Regents of the University of California.
9  All rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13 
14  Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16 
17  Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 
21  Neither the name of the University of California nor the names of its
22  contributors may be used to endorse or promote products derived from
23  this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36 
37 
38  File description:
39  Render and frame buffer object abstractions
40 
41  File author(s):
42  Lance Putnam, 2012, putnam.lance@gmail.com
43  Keehong Youn, 2017, younkeehong@gmail.com
44 
45 */
46 
47 #include "al/graphics/al_GPUObject.hpp"
48 #include "al/graphics/al_OpenGL.hpp"
49 #include "al/graphics/al_Texture.hpp"
50 
51 namespace al {
52 
55 
56 /* supported format for renderbuffer
57 (not every format can be renderbuffer format)
58 GL_RGBA8, GL_RG8, GL_RED8
59 GL_RGBA16, GL_RG16, GL_RED16
60 
61 GL_RGBA16F, GL_RG16F, GL_RED16F
62 GL_RGBA32F, GL_RG32F, GL_RED32F
63 
64 GL_RGBA8I, GL_RG8I, GL_RED8I
65 GL_RGBA16I, GL_RG16I, GL_RED16I
66 GL_RGBA32I, GL_RG32I, GL_RED32I
67 
68 GL_RGBA8UI, GL_RG8UI, GL_RED8UI
69 GL_RGBA16UI, GL_RG16UI, GL_RED16UI
70 GL_RGBA32UI, GL_RG32UI, GL_RED32UI
71 
72 GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT32F
73 GL_DEPTH24_STENCIL8, GL_DEPTH32F_STENCIL8
74 */
75 
83 class RBO : public GPUObject {
84 public:
86  RBO(unsigned int format = GL_DEPTH_COMPONENT16);
87 
88  ~RBO() { destroy(); }
89 
91  unsigned int format() const;
92 
94  RBO &format(unsigned int v);
95 
97  void bind();
98 
100  void unbind();
101 
103 
107  bool resize(unsigned width, unsigned height);
108 
109  void create(unsigned int width, unsigned int height,
110  unsigned int format = GL_DEPTH_COMPONENT16) {
111  mFormat = format;
112  resize(width, height);
113  }
114 
116  static unsigned maxSize();
117 
118  static void bind(unsigned id);
119  static bool resize(unsigned int format, unsigned width, unsigned height);
120 
121 protected:
122  unsigned int mFormat;
123 
124  virtual void onCreate();
125  virtual void onDestroy();
126 };
127 
129 
133 
150 class FBO : public GPUObject {
151 public:
153  enum Attachment {
154  COLOR_ATTACHMENT0 = GL_COLOR_ATTACHMENT0,
155  COLOR_ATTACHMENT1 = GL_COLOR_ATTACHMENT1,
156  COLOR_ATTACHMENT2 = GL_COLOR_ATTACHMENT2,
157  COLOR_ATTACHMENT3 = GL_COLOR_ATTACHMENT3,
158  DEPTH_ATTACHMENT = GL_DEPTH_ATTACHMENT,
159  STENCIL_ATTACHMENT = GL_STENCIL_ATTACHMENT,
160  DEPTH_STENCIL_ATTACHMENT = GL_DEPTH_STENCIL_ATTACHMENT
161  };
162 
163  ~FBO() { destroy(); }
164 
166  FBO &attachRBO(const RBO &rbo, unsigned int attachment = GL_DEPTH_ATTACHMENT);
167 
169  FBO &detachRBO(unsigned int attachment);
170 
172 
177  unsigned int attach = GL_COLOR_ATTACHMENT0,
178  int level = 0);
179 
181  FBO &detachTexture2D(unsigned int attachment, int level = 0);
182 
183  FBO &attachCubemapFace(Texture const &tex, unsigned int target_face,
184  unsigned int attachment = GL_COLOR_ATTACHMENT0,
185  int level = 0);
186 
187  FBO &detachCubemapFace(unsigned int target_face, unsigned int attachment,
188  int level = 0);
189 
191  void bind();
192 
194  void unbind();
195 
196  void begin() { bind(); }
197  void end() { unbind(); }
198 
200  GLenum status();
201  const char *statusString();
202  const char *statusString(GLenum stat);
203 
204  static void bind(unsigned fboID);
205  static void renderBuffer(unsigned rboID, unsigned int attachment);
206  static void texture2D(unsigned texID,
207  unsigned int attachment = GL_COLOR_ATTACHMENT0,
208  int level = 0);
209  static void textureCubemapFace(unsigned int texID, unsigned int target_face,
210  unsigned int attachment = GL_COLOR_ATTACHMENT0,
211  int level = 0);
212 
213 protected:
214  virtual void onCreate();
215  virtual void onDestroy();
216 
217 public:
218  static unsigned int const DEFAULT{0};
219 };
220 
221 } // namespace al
222 #endif
Frame buffer object class.
Definition: al_FBO.hpp:150
FBO & detachTexture2D(unsigned int attachment, int level=0)
Detach texture at a specified attachment point and mipmap level.
virtual void onCreate()
Called when currently assigned context is created.
virtual void onDestroy()
Called when currently assigned context is destroyed.
FBO & attachTexture2D(Texture const &tex, unsigned int attach=GL_COLOR_ATTACHMENT0, int level=0)
Attach a texture.
FBO & attachRBO(const RBO &rbo, unsigned int attachment=GL_DEPTH_ATTACHMENT)
Attach RBO at specified attachment point.
GLenum status()
Get status of frame buffer object.
FBO & detachRBO(unsigned int attachment)
Detach RBO at specified attachment point.
void bind()
Bind object (start rendering to attached objects)
void unbind()
Unbind object.
Attachment
Attachment type.
Definition: al_FBO.hpp:153
void destroy()
Destroys object on GPU.
void create()
Creates object on GPU.
Render buffer object class.
Definition: al_FBO.hpp:83
RBO(unsigned int format=GL_DEPTH_COMPONENT16)
unsigned int format() const
Get internal pixel format.
RBO & format(unsigned int v)
Set internal pixel format.
void bind()
Bind object.
virtual void onCreate()
Called when currently assigned context is created.
bool resize(unsigned width, unsigned height)
Set dimensions, in pixels.
virtual void onDestroy()
Called when currently assigned context is destroyed.
void unbind()
Unbind object.
static unsigned maxSize()
Get maximum buffer size.
Definition: al_App.hpp:23