SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLDepthBuffer Class Reference

#include <SLGLDepthBuffer.h>

Inheritance diagram for SLGLDepthBuffer:
[legend]

Public Member Functions

 SLGLDepthBuffer (const SLVec2i &dimensions, SLenum magFilter=GL_NEAREST, SLenum minFilter=GL_NEAREST, SLint wrap=GL_REPEAT, SLfloat borderColor[]=nullptr, SLenum target=GL_TEXTURE_2D, SLstring name="SM-DepthBuffer")
 
 ~SLGLDepthBuffer ()
 
SLint texID ()
 
SLint target ()
 
void bindActive (SLuint texUnit) const
 Sets the active texture unit within the shader and binds the texture. More...
 
void bind ()
 Binds the OpenGL frame buffer object for the depth buffer. More...
 
void unbind ()
 Ends the usage of the depth buffer frame buffer. More...
 
void bindFace (SLenum face) const
 Binds a specific texture face of a cube map depth buffer. More...
 
SLfloatreadPixels () const
 
SLVec2i dimensions ()
 

Private Attributes

SLVec2i _dimensions
 Size of the texture. More...
 
SLuint _fboID
 ID of the FB object. More...
 
SLint _prevFboID
 ID of the previously bound FB. More...
 
SLuint _texID
 ID of the texture. More...
 
SLenum _target
 GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP. More...
 
- Private Attributes inherited from SLObject
SLstring _name
 name of an object More...
 
SLstring _url
 uniform resource locator More...
 

Additional Inherited Members

- Private Member Functions inherited from SLObject
 SLObject (const SLstring &Name="", const SLstring &url="")
 
virtual ~SLObject ()
 
void name (const SLstring &Name)
 
void url (const SLstring &url)
 
const SLstringname () const
 
const SLstringurl () const
 

Detailed Description

Definition at line 19 of file SLGLDepthBuffer.h.

Constructor & Destructor Documentation

◆ SLGLDepthBuffer()

SLGLDepthBuffer::SLGLDepthBuffer ( const SLVec2i dimensions,
SLenum  magFilter = GL_NEAREST,
SLenum  minFilter = GL_NEAREST,
SLint  wrap = GL_REPEAT,
SLfloat  borderColor[] = nullptr,
SLenum  target = GL_TEXTURE_2D,
SLstring  name = "SM-DepthBuffer" 
)

Constructor for OpenGL depth buffer framebuffer used in shadow mapping

Parameters
dimensions2D vector pixel dimensions
magFilterOpenGL magnification filter enum
minFilterOpenGL minification filter enum
wrapOpenGL texture wrapping enum
borderColor
targetOpenGL texture target enum GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP
nameName of the depth buffer

Definition at line 26 of file SLGLDepthBuffer.cpp.

33  : SLObject(name),
36 {
38 
39  assert(target == GL_TEXTURE_2D || target == GL_TEXTURE_CUBE_MAP);
40  SLGLState* stateGL = SLGLState::instance();
41 
42  // Init framebuffer.
43  glGenFramebuffers(1, &_fboID);
45 
46  bind();
47 
48  glGenTextures(1, &_texID);
49  stateGL->bindTexture(target, _texID);
51 
52  // Texture parameters.
53  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter);
54  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilter);
55  glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap);
56  glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap);
57 
58 #ifndef SL_GLES
59  if (borderColor != nullptr)
60  glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, borderColor);
61 #endif
62 
64 
65  if (target == GL_TEXTURE_2D)
66  {
67  glTexImage2D(GL_TEXTURE_2D,
68  0,
69  GL_DEPTH_COMPONENT32F,
70  _dimensions.x,
71  _dimensions.y,
72  0,
73  GL_DEPTH_COMPONENT,
74  GL_FLOAT,
75  nullptr);
77 
78  // Attach texture to framebuffer.
79  glFramebufferTexture2D(GL_FRAMEBUFFER,
80  GL_DEPTH_ATTACHMENT,
81  GL_TEXTURE_2D,
82  _texID,
83  0);
85  }
86  else // target is GL_TEXTURE_CUBE_MAP
87  {
88  for (SLint i = 0; i < 6; ++i)
89  {
90  glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
91  0,
92  GL_DEPTH_COMPONENT32F,
93  _dimensions.x,
94  _dimensions.y,
95  0,
96  GL_DEPTH_COMPONENT,
97  GL_FLOAT,
98  nullptr);
100 
101  // Attach texture to framebuffer.
102  glFramebufferTexture2D(GL_FRAMEBUFFER,
103  GL_DEPTH_ATTACHMENT,
104  GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
105  _texID,
106  0);
107  GET_GL_ERROR;
108  }
109  }
110 
111 #ifndef SL_GLES
112  glDrawBuffer(GL_NONE);
113  GET_GL_ERROR;
114 #endif
115 
116  glReadBuffer(GL_NONE);
117  GET_GL_ERROR;
118 
119  unbind();
120 
121  if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
122  SL_LOG("FBO failed to initialize correctly.");
123  GET_GL_ERROR;
124 }
#define PROFILE_FUNCTION()
Definition: Instrumentor.h:41
#define SL_LOG(...)
Definition: SL.h:233
int SLint
Definition: SL.h:170
#define GET_GL_ERROR
Definition: SLGLState.h:56
SLuint _fboID
ID of the FB object.
SLVec2i dimensions()
SLenum _target
GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP.
SLVec2i _dimensions
Size of the texture.
void bind()
Binds the OpenGL frame buffer object for the depth buffer.
SLuint _texID
ID of the texture.
void unbind()
Ends the usage of the depth buffer frame buffer.
Singleton class holding all OpenGL states.
Definition: SLGLState.h:71
static SLGLState * instance()
Public static instance getter for singleton pattern.
Definition: SLGLState.h:74
void bindTexture(SLenum target, SLuint textureID)
Definition: SLGLState.cpp:423
SLObject(const SLstring &Name="", const SLstring &url="")
Definition: SLObject.h:25
const SLstring & name() const
Definition: SLObject.h:38
T y
Definition: SLVec2.h:30
T x
Definition: SLVec2.h:30

◆ ~SLGLDepthBuffer()

SLGLDepthBuffer::~SLGLDepthBuffer ( )

Definition at line 127 of file SLGLDepthBuffer.cpp.

128 {
129  glDeleteTextures(1, &_texID);
130  GET_GL_ERROR;
131 
132  glDeleteFramebuffers(1, &_fboID);
133  GET_GL_ERROR;
134 }

Member Function Documentation

◆ bind()

void SLGLDepthBuffer::bind ( )

Binds the OpenGL frame buffer object for the depth buffer.

Definition at line 168 of file SLGLDepthBuffer.cpp.

169 {
170  // Keep the previous FB ID for later unbinding
171  glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_prevFboID);
172  glBindFramebuffer(GL_FRAMEBUFFER, _fboID);
173  GET_GL_ERROR;
174 }
SLint _prevFboID
ID of the previously bound FB.

◆ bindActive()

void SLGLDepthBuffer::bindActive ( SLuint  texUnit) const

Sets the active texture unit within the shader and binds the texture.

The uniform location loc must be requested before with glUniformLocation. The texture unit value must correspond to the number that is set with glUniform1i(loc, texUnit).

Parameters
texUnitTexture Unit value

Definition at line 143 of file SLGLDepthBuffer.cpp.

144 {
145 
146  SLGLState* stateGL = SLGLState::instance();
147  stateGL->activeTexture(GL_TEXTURE0 + texUnit);
148  stateGL->bindTexture(_target, _texID);
149 }
void activeTexture(SLenum textureUnit)
Definition: SLGLState.cpp:445

◆ bindFace()

void SLGLDepthBuffer::bindFace ( SLenum  face) const

Binds a specific texture face of a cube map depth buffer.

Definition at line 187 of file SLGLDepthBuffer.cpp.

188 {
189  assert(_target == GL_TEXTURE_CUBE_MAP);
190  assert(face >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
191  face <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z);
192 
193  glFramebufferTexture2D(GL_FRAMEBUFFER,
194  GL_DEPTH_ATTACHMENT,
195  face,
196  _texID,
197  0);
198  GET_GL_ERROR;
199 }

◆ dimensions()

SLVec2i SLGLDepthBuffer::dimensions ( )
inline

Definition at line 38 of file SLGLDepthBuffer.h.

38 { return _dimensions; }

◆ readPixels()

SLfloat * SLGLDepthBuffer::readPixels ( ) const

Definition at line 152 of file SLGLDepthBuffer.cpp.

153 {
154  SLfloat* depth = new SLfloat[_dimensions.y * _dimensions.x];
155  glReadPixels(0,
156  0,
157  _dimensions.x,
158  _dimensions.y,
159  GL_DEPTH_COMPONENT,
160  GL_FLOAT,
161  depth);
162  GET_GL_ERROR;
163  return depth;
164 }
float SLfloat
Definition: SL.h:173

◆ target()

SLint SLGLDepthBuffer::target ( )
inline

Definition at line 32 of file SLGLDepthBuffer.h.

32 { return _target; }

◆ texID()

SLint SLGLDepthBuffer::texID ( )
inline

Definition at line 31 of file SLGLDepthBuffer.h.

31 { return _texID; }

◆ unbind()

void SLGLDepthBuffer::unbind ( )

Ends the usage of the depth buffer frame buffer.

Definition at line 178 of file SLGLDepthBuffer.cpp.

179 {
180  // iOS does not allow binding to 0. That's why we keep the previous FB ID
181  glBindFramebuffer(GL_FRAMEBUFFER, _prevFboID);
182  GET_GL_ERROR;
183 }

Member Data Documentation

◆ _dimensions

SLVec2i SLGLDepthBuffer::_dimensions
private

Size of the texture.

Definition at line 41 of file SLGLDepthBuffer.h.

◆ _fboID

SLuint SLGLDepthBuffer::_fboID
private

ID of the FB object.

Definition at line 42 of file SLGLDepthBuffer.h.

◆ _prevFboID

SLint SLGLDepthBuffer::_prevFboID
private

ID of the previously bound FB.

Definition at line 43 of file SLGLDepthBuffer.h.

◆ _target

SLenum SLGLDepthBuffer::_target
private

GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP.

Definition at line 45 of file SLGLDepthBuffer.h.

◆ _texID

SLuint SLGLDepthBuffer::_texID
private

ID of the texture.

Definition at line 44 of file SLGLDepthBuffer.h.


The documentation for this class was generated from the following files: