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

Texture object generated in run time from another texture. More...

#include <SLGLTextureIBL.h>

Inheritance diagram for SLGLTextureIBL:
[legend]

Public Member Functions

 SLGLTextureIBL ()
 Default constructor. More...
 
 SLGLTextureIBL (SLAssetManager *assetMgr, SLstring shaderPath, SLGLTexture *sourceTexture, SLVec2i size, SLTextureType texType, SLenum target, SLbool readBackPixels=true, SLint min_filter=GL_LINEAR, SLint mag_filter=GL_LINEAR)
 ctor for generated textures More...
 
virtual ~SLGLTextureIBL ()
 
virtual void build (SLint texID=0)
 
void logFramebufferStatus ()
 
- Public Member Functions inherited from SLGLTexture
 SLGLTexture ()
 Default ctor for all stack instances (not created with new) More...
 
 SLGLTexture (SLAssetManager *assetMgr, const SLVCol4f &colors, SLint min_filter=GL_LINEAR, SLint mag_filter=GL_LINEAR, SLint wrapS=GL_REPEAT, const SLstring &name="2D-Texture")
 ctor for 1D texture with internal image allocation More...
 
 SLGLTexture (SLAssetManager *assetMgr, SLint min_filter, SLint mag_filter, SLint wrapS, SLint wrapT, SLenum target=GL_TEXTURE_2D)
 ctor for empty 2D textures More...
 
 SLGLTexture (SLAssetManager *assetMgr, unsigned char *data, int width, int height, int cvtype, SLint min_filter, SLint mag_filter, SLTextureType type, SLint wrapS, SLint wrapT)
 ctor for 2D textures from byte pointer More...
 
 SLGLTexture (SLAssetManager *assetMgr, const SLstring &imageFilename, SLint min_filter=GL_LINEAR_MIPMAP_LINEAR, SLint mag_filter=GL_LINEAR, SLTextureType type=TT_unknown, SLint wrapS=GL_REPEAT, SLint wrapT=GL_REPEAT)
 ctor for 2D textures with internal image allocation More...
 
 SLGLTexture (SLAssetManager *assetMgr, const SLVstring &imageFilenames, SLint min_filter=GL_LINEAR, SLint mag_filter=GL_LINEAR, SLint wrapS=GL_REPEAT, SLint wrapT=GL_REPEAT, const SLstring &name="3D-Texture", SLbool loadGrayscaleIntoAlpha=false)
 ctor for 3D texture with internal image allocation More...
 
 SLGLTexture (SLAssetManager *assetMgr, SLint depth, const SLstring &imageFilename, SLint min_filter=GL_LINEAR, SLint mag_filter=GL_LINEAR, SLint wrapS=GL_REPEAT, SLint wrapT=GL_REPEAT, const SLstring &name="3D-Texture", SLbool loadGrayscaleIntoAlpha=false)
 ctor for 3D texture from a single file with depth as 3rd dimension More...
 
 SLGLTexture (SLAssetManager *assetMgr, const SLstring &imageFilenameXPos, const SLstring &imageFilenameXNeg, const SLstring &imageFilenameYPos, const SLstring &imageFilenameYNeg, const SLstring &imageFilenameZPos, const SLstring &imageFilenameZNeg, SLint min_filter=GL_LINEAR, SLint mag_filter=GL_LINEAR, SLTextureType type=TT_unknown)
 ctor for cube mapping with internal image allocation More...
 
 ~SLGLTexture () override
 
void deleteData (SLbool deleteAlsoOnGPU)
 Delete all data (CVImages and GPU textures) More...
 
void deleteDataGpu ()
 Deletes the OpenGL texture objects and releases the memory on the GPU. More...
 
void deleteImages ()
 Deletes the CVImages in _images. No more texture mapping in ray tracing. More...
 
void bindActive (SLuint texUnit=0)
 
void fullUpdate ()
 
void drawSprite (SLbool doUpdate, SLfloat x, SLfloat y, SLfloat w, SLfloat h)
 Draws the texture as 2D sprite with OpenGL buffers. More...
 
void cubeUV2XYZ (SLint index, SLfloat u, SLfloat v, SLfloat &x, SLfloat &y, SLfloat &z)
 Computes the unnormalised vector x,y,z from tex. coords. uv with cubemap index. More...
 
void cubeXYZ2UV (SLfloat x, SLfloat y, SLfloat z, SLint &index, SLfloat &u, SLfloat &v)
 Computes the uv and cubemap image index from a unnormalised vector x,y,z. More...
 
SLstring filterString (SLint glFilter)
 Returns OpenGL texture filter as string. More...
 
void texType (SLTextureType bt)
 
void uvIndex (SLbyte i)
 
void bumpScale (SLfloat bs)
 
void minFiler (SLint minF)
 
void magFiler (SLint magF)
 
void needsUpdate (SLbool update)
 
void textureSize (int width, int height)
 
void deleteImageAfterBuild (SLbool delImg)
 If deleteImageAfterBuild is set to true you won't be able to ray trace the scene. More...
 
SLuint width ()
 
SLuint height ()
 
SLuint depth ()
 
SLbyte uvIndex ()
 
SLint bytesPerPixel ()
 
SLint bytesOnGPU ()
 
SLint bytesInFile ()
 
CVVImageimages ()
 
SLenum target () const
 
SLuint texID () const
 
SLTextureType texType ()
 
SLfloat bumpScale () const
 
SLCol4f getTexelf (SLfloat u, SLfloat v, SLuint imgIndex=0)
 SLGLTexture::getTexelf returns a pixel color from u & v texture coordinates. More...
 
SLCol4f getTexelf (const SLVec3f &cubemapDir)
 SLGLTexture::getTexelf returns a pixel color at the specified cubemap direction. More...
 
SLbool hasAlpha ()
 
SLMat4f tm ()
 
SLbool autoCalcTM3D () const
 
SLbool needsUpdate ()
 
SLstring typeName ()
 Returns the texture type as string. More...
 
SLstring typeShortName ()
 Returns the texture type short. More...
 
bool isTexture ()
 
SLstring minificationFilterName ()
 
SLstring magnificationFilterName ()
 
void build2DMipmaps (SLint target, SLuint index)
 
SLbool copyVideoImage (SLint camWidth, SLint camHeight, CVPixelFormatGL glFormat, SLuchar *data, SLbool isContinuous, SLbool isTopLeft)
 Copies the image data from a video camera into the current video image. More...
 
SLbool copyVideoImage (SLint camWidth, SLint camHeight, CVPixelFormatGL srcFormat, CVPixelFormatGL dstFormat, SLuchar *data, SLbool isContinuous, SLbool isTopLeft)
 
void calc3DGradients (SLint sampleRadius, const function< void(int)> &onUpdateProgress=nullptr)
 
void smooth3DGradients (SLint smoothRadius, function< void(int)> onUpdateProgress=nullptr)
 
SLVec2f dudv (SLfloat u, SLfloat v)
 
- Public 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
 

Protected Member Functions

void renderCube ()
 Renders 2x2 cube, used to project a texture to a cube texture with 6 sides. More...
 
void renderQuad ()
 Renders a 2x2 XY quad, used for rendering and capturing the BRDF integral. More...
 
void readPixels (int width, int height, string name, bool savePNG)
 Reads back the pixels into an image. More...
 
- Protected Member Functions inherited from SLGLTexture
void load (const SLstring &filename, SLbool flipVertical=true, SLbool loadGrayscaleIntoAlpha=false)
 Loads the texture, converts color depth & applies vertical mirroring. More...
 
void load (const SLVCol4f &colors)
 Loads the 1D color data into an image of height 1. More...
 

Protected Attributes

SLuint _cubeVAO = 0
 
SLuint _cubeVBO = 0
 
SLuint _quadVAO = 0
 
SLuint _quadVBO = 0
 
SLGLTexture_sourceTexture
 2D Texture from the HDR Image More...
 
SLGLProgram_shaderProgram
 Shader program to render the texture. More...
 
SLMat4f _captureProjection
 Projection matrix for capturing the textures. More...
 
SLVMat4f _captureViews
 All 6 positions of the views that represent the 6 sides of the cube map. More...
 
SLbool _readBackPixels
 Flag if generated texture should be read back from GPU into cvMat. More...
 
- Protected Attributes inherited from SLGLTexture
CVVImage _images
 Vector of CVImage pointers. More...
 
SLuint _texID
 OpenGL texture ID. More...
 
SLTextureType _texType
 See SLTextureType. More...
 
SLint _width
 Texture image width in pixels (images exist either in _images or on the GPU or on both) More...
 
SLint _height
 Texture image height in pixels (images exist either in _images or on the GPU or on both) More...
 
SLint _depth
 3D Texture image depth (images exist either in _images or on the GPU or on both) More...
 
SLbyte _uvIndex
 Texture coordinate index in SLMesh (0 = default) More...
 
SLint _internalFormat
 Internal OpenGL format. More...
 
SLint _bytesPerPixel
 Bytes per texture image pixel (images exist either in _images or on the GPU or on both) More...
 
SLint _min_filter
 Minification filter. More...
 
SLint _mag_filter
 Magnification filter. More...
 
SLint _wrap_s
 Wrapping in s direction. More...
 
SLint _wrap_t
 Wrapping in t direction. More...
 
SLenum _target
 texture target More...
 
SLMat4f _tm
 texture matrix More...
 
SLuint _bytesOnGPU
 NO. of bytes on GPU. More...
 
SLuint _bytesInFile
 NO. of bytes in file. More...
 
SLbool _autoCalcTM3D
 Flag if texture matrix should be calculated from AABB for 3D mapping. More...
 
SLfloat _bumpScale
 Bump mapping scale factor. More...
 
SLbool _resizeToPow2
 Flag if image should be resized to n^2. More...
 
SLGLVertexArray _vaoSprite
 Vertex array object for sprite rendering. More...
 
std::atomic< bool > _needsUpdate {}
 Flag if image needs an single update. More...
 
std::mutex _mutex
 Mutex to protect parallel access (used in ray tracing) More...
 
SLbool _deleteImageAfterBuild
 Flag if images should be deleted after build on GPU. More...
 
SLbool _compressedTexture = false
 True for compressed texture format on GPU. More...
 
- Protected Attributes inherited from SLObject
SLstring _name
 name of an object More...
 
SLstring _url
 uniform resource locator More...
 

Additional Inherited Members

- Static Public Member Functions inherited from SLGLTexture
static SLTextureType detectType (const SLstring &filename)
 Detects the texture type from the filename appendix (See SLTexType def.) More...
 
static string internalFormatStr (int internalFormat)
 Returns the internal pixel format from OpenGL. More...
 
- Static Public Attributes inherited from SLGLTexture
static SLfloat maxAnisotropy = -1.0f
 Returns the derivation as [s,t]. More...
 
static SLuint totalNumBytesOnGPU = 0
 Total NO. of bytes used for textures on GPU. More...
 

Detailed Description

Texture object generated in run time from another texture.

This class is mainly used to generate the textures used for reflections in the Image Base Lighting (IBL) techniques. It takes a source texture and projects it into a cube map with different rendering techniques to produces the textures needed in IBL like the irradiance or the prefilter map. This textures can be given to the SLMaterial with IBL. It uses the SLGLFrameBuffer class to render the scene into a cube map. These generated textures only exist on the GPU. There are no images in the SLGLTexture::_images vector.

Definition at line 31 of file SLGLTextureIBL.h.

Constructor & Destructor Documentation

◆ SLGLTextureIBL() [1/2]

SLGLTextureIBL::SLGLTextureIBL ( )
inline

Default constructor.

Definition at line 35 of file SLGLTextureIBL.h.

35 : SLGLTexture() { ; }
SLGLTexture()
Default ctor for all stack instances (not created with new)
Definition: SLGLTexture.cpp:37

◆ SLGLTextureIBL() [2/2]

SLGLTextureIBL::SLGLTextureIBL ( SLAssetManager am,
SLstring  shaderPath,
SLGLTexture sourceTexture,
SLVec2i  size,
SLTextureType  texType,
SLenum  target,
SLbool  readBackPixels = true,
SLint  min_filter = GL_LINEAR,
SLint  mag_filter = GL_LINEAR 
)

ctor for generated textures

Number of mip levels for roughness cubemaps.

ctor for generated textures from hdr textures

Definition at line 18 of file SLGLTextureIBL.cpp.

27 {
28  if (sourceTexture != nullptr)
29  {
30  assert(sourceTexture->texType() >= TT_hdr);
31  assert(texType > TT_hdr);
32  }
33 
34  _sourceTexture = sourceTexture;
35  _texType = texType;
36  _min_filter = min_filter;
37  _mag_filter = mag_filter;
38  _wrap_s = GL_CLAMP_TO_EDGE;
39  _wrap_t = GL_CLAMP_TO_EDGE;
40  _target = target;
41  _texID = 0;
42  _bumpScale = 1.0f;
43  _resizeToPow2 = false;
44  _autoCalcTM3D = false;
45  _needsUpdate = false;
46  _bytesOnGPU = 0;
47  _width = size.x;
48  _height = size.y;
49  _bytesPerPixel = 0;
50  _deleteImageAfterBuild = false;
51  _readBackPixels = readBackPixels;
52 
53  name("Generated " + typeName());
54 
57  shaderPath + "PBR_CubeMap.vert",
58  shaderPath + "PBR_CylinderToCubeMap.frag");
59  else if (texType == TT_irradianceCubemap)
61  shaderPath + "PBR_CubeMap.vert",
62  shaderPath + "PBR_IrradianceConvolution.frag");
63  else if (texType == TT_roughnessCubemap)
65  shaderPath + "PBR_CubeMap.vert",
66  shaderPath + "PBR_PrefilterRoughness.frag");
67  else if (texType == TT_brdfLUT)
69  shaderPath + "PBR_BRDFIntegration.vert",
70  shaderPath + "PBR_BRDFIntegration.frag");
71 
72  // perspective projection with field of view of 90 degrees
73  _captureProjection.perspective(90.0f, 1.0f, 0.1f, 10.0f);
74 
75  // initialize capture views: 6 views each at all 6 directions of the faces of a cube map
76  // clang-format off
77  SLMat4f mat;
78  mat.lookAt(0, 0, 0, 1, 0, 0, 0,-1, 0); _captureViews.push_back(mat);
79  mat.lookAt(0, 0, 0,-1, 0, 0, 0,-1, 0); _captureViews.push_back(mat);
80  mat.lookAt(0, 0, 0, 0, 1, 0, 0, 0, 1); _captureViews.push_back(mat);
81  mat.lookAt(0, 0, 0, 0,-1, 0, 0, 0,-1); _captureViews.push_back(mat);
82  mat.lookAt(0, 0, 0, 0, 0, 1, 0,-1, 0); _captureViews.push_back(mat);
83  mat.lookAt(0, 0, 0, 0, 0,-1, 0,-1, 0); _captureViews.push_back(mat);
84  // clang-format on
85 
86  // Add pointer to the global resource vectors for deallocation
87  if (am)
88  am->textures().push_back(this);
89 }
@ TT_brdfLUT
Definition: SLGLTexture.h:93
@ TT_irradianceCubemap
Definition: SLGLTexture.h:91
@ TT_environmentCubemap
Definition: SLGLTexture.h:90
@ TT_roughnessCubemap
Definition: SLGLTexture.h:92
@ TT_hdr
Definition: SLGLTexture.h:89
SLVGLTexture & textures()
Generic Shader Program class inherited from SLGLProgram.
SLbool _resizeToPow2
Flag if image should be resized to n^2.
Definition: SLGLTexture.h:321
SLint _bytesPerPixel
Bytes per texture image pixel (images exist either in _images or on the GPU or on both)
Definition: SLGLTexture.h:310
SLTextureType texType()
Definition: SLGLTexture.h:228
SLint _width
Texture image width in pixels (images exist either in _images or on the GPU or on both)
Definition: SLGLTexture.h:305
SLfloat _bumpScale
Bump mapping scale factor.
Definition: SLGLTexture.h:320
SLuint _bytesOnGPU
NO. of bytes on GPU.
Definition: SLGLTexture.h:317
SLenum target() const
Definition: SLGLTexture.h:226
SLint _wrap_t
Wrapping in t direction.
Definition: SLGLTexture.h:314
SLint _min_filter
Minification filter.
Definition: SLGLTexture.h:311
SLint _wrap_s
Wrapping in s direction.
Definition: SLGLTexture.h:313
SLbool _deleteImageAfterBuild
Flag if images should be deleted after build on GPU.
Definition: SLGLTexture.h:326
SLint _height
Texture image height in pixels (images exist either in _images or on the GPU or on both)
Definition: SLGLTexture.h:306
SLTextureType _texType
See SLTextureType.
Definition: SLGLTexture.h:304
SLenum _target
texture target
Definition: SLGLTexture.h:315
SLstring typeName()
Returns the texture type as string.
std::atomic< bool > _needsUpdate
Flag if image needs an single update.
Definition: SLGLTexture.h:323
void texType(SLTextureType bt)
Definition: SLGLTexture.h:200
SLint _mag_filter
Magnification filter.
Definition: SLGLTexture.h:312
SLbool _autoCalcTM3D
Flag if texture matrix should be calculated from AABB for 3D mapping.
Definition: SLGLTexture.h:319
SLuint _texID
OpenGL texture ID.
Definition: SLGLTexture.h:303
SLGLProgram * _shaderProgram
Shader program to render the texture.
SLVMat4f _captureViews
All 6 positions of the views that represent the 6 sides of the cube map.
SLGLTexture * _sourceTexture
2D Texture from the HDR Image
SLMat4f _captureProjection
Projection matrix for capturing the textures.
SLbool _readBackPixels
Flag if generated texture should be read back from GPU into cvMat.
void lookAt(T EyeX, T EyeY, T EyeZ, T AtX=0, T AtY=0, T AtZ=0, T UpX=0, T UpY=0, T UpZ=0)
Defines the a view matrix as the corresponding gluLookAt function.
Definition: SLMat4.h:708
void perspective(T fov, T aspect, T n, T f)
Defines a perspective projection matrix with a field of view angle.
Definition: SLMat4.h:874
const SLstring & name() const
Definition: SLObject.h:38
T y
Definition: SLVec2.h:30
T x
Definition: SLVec2.h:30

◆ ~SLGLTextureIBL()

SLGLTextureIBL::~SLGLTextureIBL ( )
virtual

Definition at line 91 of file SLGLTextureIBL.cpp.

92 {
93  deleteData(true);
94 
95  glDeleteVertexArrays(1, &_cubeVAO);
96  _cubeVAO = 0;
97  glDeleteBuffers(1, &_cubeVBO);
98  _cubeVBO = 0;
99  glDeleteVertexArrays(1, &_quadVAO);
100  _quadVAO = 0;
101  glDeleteBuffers(1, &_quadVBO);
102  _quadVBO = 0;
103  GET_GL_ERROR;
104 }
#define GET_GL_ERROR
Definition: SLGLState.h:56
void deleteData(SLbool deleteAlsoOnGPU)
Delete all data (CVImages and GPU textures)

Member Function Documentation

◆ build()

void SLGLTextureIBL::build ( SLint  texUnit = 0)
virtual

Build the texture into a cube map, rendering the texture 6 times and capturing each time one side of the cube (except for the BRDF LUT texture, which is completely generated by calculations directly with the shader). This is probably the most difficult OpenGL code in the project.

Reimplemented from SLGLTexture.

Definition at line 112 of file SLGLTextureIBL.cpp.

113 {
114  // Assure to delete all images if we read the pixels back
115  if (_readBackPixels)
116  deleteImages();
117  bool saveReadbackTextures = false;
118 
119  glGenTextures(1, &_texID);
120  glBindTexture(_target, _texID);
121 
122  GLuint fboID;
123  glGenFramebuffers(1, &fboID);
124 
125  GLuint rboID;
126  glGenRenderbuffers(1, &rboID);
127 
130  {
133 
134  // Build the textures for the 6 faces of a cubemap with no data
135  for (SLuint i = 0; i < 6; i++)
136  {
137  glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
138  0,
140  _width,
141  _height,
142  0,
145  nullptr);
146  }
147 
148  glTexParameteri(_target, GL_TEXTURE_WRAP_S, _wrap_s);
149  glTexParameteri(_target, GL_TEXTURE_WRAP_T, _wrap_t);
150  glTexParameteri(_target, GL_TEXTURE_WRAP_R, _wrap_t);
151  glTexParameteri(_target, GL_TEXTURE_MIN_FILTER, _min_filter);
152  glTexParameteri(_target, GL_TEXTURE_MAG_FILTER, _mag_filter);
153 
155 
156  if (_sourceTexture != nullptr)
158 
159  _shaderProgram->uniform1i("u_environmentMap", texUnit);
160 
161  glActiveTexture(GL_TEXTURE0 + texUnit);
162  glBindTexture(_sourceTexture->target(),
163  _sourceTexture->texID());
164  glViewport(0, 0, _width, _height);
165  glBindFramebuffer(GL_FRAMEBUFFER, fboID);
166  glBindRenderbuffer(GL_RENDERBUFFER, rboID);
167  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, _width, _height);
168  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboID);
169 
171 
172  // Set the list of draw buffers.
173  GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
174  glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
175 
176  for (SLuint i = 0; i < 6; i++)
177  {
179  _shaderProgram->uniformMatrix4fv("u_mvpMatrix", 1, mvp.m());
180 
181  glFramebufferTexture2D(GL_FRAMEBUFFER,
182  GL_COLOR_ATTACHMENT0,
183  GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
184  _texID,
185  0);
186 
187  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
188  renderCube();
189 
190  if (_readBackPixels)
191  {
192  string name = (_texType == TT_environmentCubemap)
193  ? "environmentCubemap_side"
194  : "irradianceCubemap_side";
195  name += std::to_string(i) + ".png";
197  _height,
198  name,
199  saveReadbackTextures);
200  }
201  }
202 
203  glBindFramebuffer(GL_FRAMEBUFFER, 0);
204  glBindRenderbuffer(GL_RENDERBUFFER, 0);
205 
207  {
208  glBindTexture(GL_TEXTURE_CUBE_MAP, _texID);
209  glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
210  }
211  }
212  else if (_texType == TT_roughnessCubemap)
213  {
215  "the source texture is not an environment map");
216 
219 
220  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
221  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
222  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
223  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
224  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
225  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
226  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, ROUGHNESS_NUM_MIP_LEVELS - 1);
227 
228  // Define textures for all mip levels of all six sides of the cube map
229  for (SLint mipLevel = 0; mipLevel < ROUGHNESS_NUM_MIP_LEVELS; mipLevel++)
230  {
231  // Calculate the size of this mip level
232  SLint mipWidth = _width / (1 << mipLevel);
233  SLint mipHeight = _height / (1 << mipLevel);
234 
235  for (SLint i = 0; i < 6; i++)
236  {
237  glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
238  mipLevel,
240  mipWidth,
241  mipHeight,
242  0,
245  nullptr);
246  }
247  }
248 
250 
251  if (_sourceTexture != nullptr)
253 
254  _shaderProgram->uniform1i("u_environmentMap", texUnit);
255  _shaderProgram->uniform1f("u_environmentMapSize", (float)_sourceTexture->width());
256 
257  glActiveTexture(GL_TEXTURE0 + texUnit);
258  glBindTexture(_sourceTexture->target(), _sourceTexture->texID());
259 
260  for (SLint mipLevel = 0; mipLevel < ROUGHNESS_NUM_MIP_LEVELS; ++mipLevel)
261  {
262  // Resize framebuffer according to mip level size
263  SLint mipWidth = _width / (1 << mipLevel);
264  SLint mipHeight = _height / (1 << mipLevel);
265  glViewport(0, 0, mipWidth, mipHeight);
266 
267  glBindFramebuffer(GL_FRAMEBUFFER, fboID);
268 
269  SLfloat roughness = (SLfloat)mipLevel / (SLfloat)(ROUGHNESS_NUM_MIP_LEVELS - 1);
270  _shaderProgram->uniform1f("u_roughness", roughness);
271 
272  for (SLuint i = 0; i < 6; ++i)
273  {
275  _shaderProgram->uniformMatrix4fv("u_mvpMatrix", 1, mvp.m());
276  glFramebufferTexture2D(GL_FRAMEBUFFER,
277  GL_COLOR_ATTACHMENT0,
278  GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
279  _texID,
280  mipLevel);
281 
282  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
283 
285 
286  renderCube();
287 
288  if (_readBackPixels)
289  {
290  string name = "roughnessCubemap_mip" +
291  std::to_string(mipLevel) + "_side" +
292  std::to_string(i) + ".png";
293  readPixels((SLint)mipWidth,
294  (SLint)mipHeight,
295  name,
296  saveReadbackTextures);
297  }
298  }
299 
300  glBindFramebuffer(GL_FRAMEBUFFER, 0);
301  }
302  }
303  else if (_texType == TT_brdfLUT)
304  {
307 
308  glTexImage2D(_target,
309  0,
311  _width,
312  _height,
313  0,
316  0);
317  glTexParameteri(_target, GL_TEXTURE_WRAP_S, _wrap_s);
318  glTexParameteri(_target, GL_TEXTURE_WRAP_T, _wrap_t);
319  glTexParameteri(_target, GL_TEXTURE_MIN_FILTER, _min_filter);
320  glTexParameteri(_target, GL_TEXTURE_MAG_FILTER, _mag_filter);
321 
322  glBindFramebuffer(GL_FRAMEBUFFER, fboID);
323 
324  glFramebufferTexture2D(GL_FRAMEBUFFER,
325  GL_COLOR_ATTACHMENT0,
326  GL_TEXTURE_2D,
327  _texID,
328  0);
329 
330  glViewport(0, 0, _width, _height);
332 
333  if (_sourceTexture != nullptr)
335  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
336 
338 
339  renderQuad();
340 
341  if (_readBackPixels)
342  {
343  string name = "brdfLUT.png";
345  _height,
346  name,
347  saveReadbackTextures);
348  }
349 
350  glBindFramebuffer(GL_FRAMEBUFFER, 0);
351  }
352 
353  glDeleteFramebuffers(1, &fboID);
354  glDeleteRenderbuffers(1, &rboID);
355 
356  // Reset the viewport
357  SLGLState* state = SLGLState::instance();
358  auto vp = state->viewport();
359  glViewport(vp.x, vp.y, vp.z, vp.w);
360  glFinish();
361  GET_GL_ERROR;
362 }
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
int SLint
Definition: SL.h:170
#define SL_BRDF_LUT_GL_INTERNAL_FORMAT
Definition: SLGLTexture.h:55
#define SL_HDR_PIXEL_BYTES
Definition: SLGLTexture.h:54
#define SL_BRDF_LUT_GL_FORMAT
Definition: SLGLTexture.h:56
#define SL_HDR_GL_INTERNAL_FORMAT
Definition: SLGLTexture.h:51
#define SL_BRDF_LUT_GL_TYPE
Definition: SLGLTexture.h:57
#define SL_HDR_GL_FORMAT
Definition: SLGLTexture.h:52
#define SL_HDR_GL_TYPE
Definition: SLGLTexture.h:53
#define SL_BRDF_LUT_PIXEL_BYTES
Definition: SLGLTexture.h:58
const SLint ROUGHNESS_NUM_MIP_LEVELS
SLint uniformMatrix4fv(const SLchar *name, SLsizei count, const SLfloat *value, GLboolean transpose=false) const
Passes a 4x4 float matrix values py pointer to the uniform variable "name".
SLint uniform1f(const SLchar *name, SLfloat v0) const
Passes the float value v0 to the uniform variable "name".
void useProgram()
SLint uniform1i(const SLchar *name, SLint v0) const
Passes the int values v0 to the uniform variable "name".
Singleton class holding all OpenGL states.
Definition: SLGLState.h:71
void viewport(SLint x, SLint y, SLsizei width, SLsizei height)
Definition: SLGLState.cpp:378
static SLGLState * instance()
Public static instance getter for singleton pattern.
Definition: SLGLState.h:74
void deleteImages()
Deletes the CVImages in _images. No more texture mapping in ray tracing.
SLuint width()
Definition: SLGLTexture.h:218
SLint _internalFormat
Internal OpenGL format.
Definition: SLGLTexture.h:309
SLuint texID() const
Definition: SLGLTexture.h:227
void bindActive(SLuint texUnit=0)
void renderQuad()
Renders a 2x2 XY quad, used for rendering and capturing the BRDF integral.
void logFramebufferStatus()
void renderCube()
Renders 2x2 cube, used to project a texture to a cube texture with 6 sides.
void readPixels(int width, int height, string name, bool savePNG)
Reads back the pixels into an image.
void m(int i, T val)
Definition: SLMat4.h:93

◆ logFramebufferStatus()

void SLGLTextureIBL::logFramebufferStatus ( )

Definition at line 503 of file SLGLTextureIBL.cpp.

504 {
505 #if defined(DEBUG) || defined(_DEBUG)
506  GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
507  switch (fbStatus)
508  {
509  case GL_FRAMEBUFFER_COMPLETE: break;
510  case GL_FRAMEBUFFER_UNDEFINED: SL_LOG("**** GL_FRAMEBUFFER_UNDEFINED ****"); break;
511  case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: SL_LOG("**** GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT ****"); break;
512  case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: SL_LOG("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT ****"); break;
513  case GL_FRAMEBUFFER_UNSUPPORTED: SL_LOG("**** GL_FRAMEBUFFER_UNSUPPORTED ****"); break;
514  case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: SL_LOG("**** GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE ****"); break;
515  // case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: SL_LOG("**** GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER ****"); break;
516  // case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: SL_LOG("**** GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER ****"); break;
517  // case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS: SL_LOG("**** GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS ****"); break;
518  default: SL_LOG("Unknown framebuffer status!!!");
519  }
520 #endif
521 }
#define SL_LOG(...)
Definition: SL.h:233

◆ readPixels()

void SLGLTextureIBL::readPixels ( int  width,
int  height,
string  name,
bool  savePNG 
)
protected

Reads back the pixels into an image.

Definition at line 482 of file SLGLTextureIBL.cpp.

483 {
484  CVImage* image = new CVImage(width,
485  height,
487  name);
488  glReadPixels(0,
489  0,
490  width,
491  height,
493  GL_UNSIGNED_BYTE,
494  image->data());
495  GET_GL_ERROR;
496 
497  if (savePNG)
498  image->savePNG(name, 9, false);
499 
500  _images.push_back(image);
501 }
#define SL_READ_PIXELS_CV_FORMAT
Definition: SLGLTexture.h:60
#define SL_READ_PIXELS_GL_FORMAT
Definition: SLGLTexture.h:59
OpenCV image class with the same interface as the former SLImage class.
Definition: CVImage.h:64
uchar * data()
Definition: CVImage.h:123
void savePNG(const string &filename, int compressionLevel=6, bool flipY=true, bool convertBGR2RGB=true)
Save as PNG at a certain compression level (0-9)
Definition: CVImage.cpp:637
SLuint height()
Definition: SLGLTexture.h:219
CVVImage _images
Vector of CVImage pointers.
Definition: SLGLTexture.h:302

◆ renderCube()

void SLGLTextureIBL::renderCube ( )
protected

Renders 2x2 cube, used to project a texture to a cube texture with 6 sides.

Definition at line 365 of file SLGLTextureIBL.cpp.

366 {
367  // initialize (if necessary)
368  if (_cubeVAO == 0)
369  {
370  // clang-format off
371  // Define vertex positions of an inward facing cube
372  // The original code from learnopengl.com used an outwards facing cube
373  // with disabled face culling. With our default enabled face culling this
374  // lead to a horrible error that cost us about 10kFr. of debugging.
375  // Thanks to the tip of Michael Goettlicher we finally got it correct.
376  float vertices[] = {
377  // back face
378  -1.0f, -1.0f, -1.0f, // bottom-left
379  1.0f, -1.0f, -1.0f, // bottom-right
380  1.0f, 1.0f, -1.0f, // top-right
381  1.0f, 1.0f, -1.0f, // top-right
382  -1.0f, 1.0f, -1.0f, // top-left
383  -1.0f, -1.0f, -1.0f, // bottom-left
384  // front face
385  -1.0f, -1.0f, 1.0f, // bottom-left
386  1.0f, 1.0f, 1.0f, // top-right
387  1.0f, -1.0f, 1.0f, // bottom-right
388  1.0f, 1.0f, 1.0f, // top-right
389  -1.0f, -1.0f, 1.0f, // bottom-left
390  -1.0f, 1.0f, 1.0f, // top-left
391  // left face
392  -1.0f, 1.0f, 1.0f, // top-right
393  -1.0f, -1.0f, -1.0f, // bottom-left
394  -1.0f, 1.0f, -1.0f, // top-left
395  -1.0f, -1.0f, -1.0f, // bottom-left
396  -1.0f, 1.0f, 1.0f, // top-right
397  -1.0f, -1.0f, 1.0f, // bottom-right
398  // right face
399  1.0f, 1.0f, 1.0f, // top-left
400  1.0f, 1.0f, -1.0f, // top-right
401  1.0f, -1.0f, -1.0f, // bottom-right
402  1.0f, -1.0f, -1.0f, // bottom-right
403  1.0f, -1.0f, 1.0f, // bottom-left
404  1.0f, 1.0f, 1.0f, // top-left
405  // bottom face
406  -1.0f, -1.0f, -1.0f, // top-right
407  1.0f, -1.0f, 1.0f, // bottom-left
408  1.0f, -1.0f, -1.0f, // top-left
409  1.0f, -1.0f, 1.0f, // bottom-left
410  -1.0f, -1.0f, -1.0f, // top-right
411  -1.0f, -1.0f, 1.0f, // bottom-right
412  // top face
413  1.0f, 1.0f, 1.0f, // right-top-front
414  -1.0f, 1.0f, 1.0f, // left-top-front
415  -1.0f, 1.0f, -1.0f, // left-top-back
416  -1.0f, 1.0f, -1.0f, // left-top-back
417  1.0f, 1.0f, -1.0f, // right-top-back
418  1.0f, 1.0f , 1.0f // right-top-front
419  };
420 
421  // clang-format on
422  glGenVertexArrays(1, &_cubeVAO);
423  glGenBuffers(1, &_cubeVBO);
424 
425  // fill buffer
426  glBindBuffer(GL_ARRAY_BUFFER, _cubeVBO);
427  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
428 
429  // link vertex attributes
430  glBindVertexArray(_cubeVAO);
431  glEnableVertexAttribArray(0);
432  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
433  glBindBuffer(GL_ARRAY_BUFFER, 0);
434  glBindVertexArray(0);
435  }
436 
437  // render inwards facing cube with face culling enabled
438  glBindVertexArray(_cubeVAO);
439  glDrawArrays(GL_TRIANGLES, 0, 36);
440  glBindVertexArray(0);
441 }

◆ renderQuad()

void SLGLTextureIBL::renderQuad ( )
protected

Renders a 2x2 XY quad, used for rendering and capturing the BRDF integral.

Definition at line 444 of file SLGLTextureIBL.cpp.

445 {
446  if (_quadVAO == 0)
447  {
448  // clang-format off
449  // Define a front (+Z) facing quad with texture coordinates
450  float quadVertices[] = {
451  // positions // texture Coords
452  -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
453  -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
454  1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
455  1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
456  };
457  // clang-format on
458 
459  // setup plane VAO
460  glGenVertexArrays(1, &_quadVAO);
461  glGenBuffers(1, &_quadVBO);
462  glBindVertexArray(_quadVAO);
463  glBindBuffer(GL_ARRAY_BUFFER, _quadVBO);
464  glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
465  glEnableVertexAttribArray(0);
466  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
467  glEnableVertexAttribArray(1);
468  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
469  }
470 
471  // Render quad
472  glBindVertexArray(_quadVAO);
473  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
474  glBindVertexArray(0);
475 }

Member Data Documentation

◆ _captureProjection

SLMat4f SLGLTextureIBL::_captureProjection
protected

Projection matrix for capturing the textures.

Definition at line 69 of file SLGLTextureIBL.h.

◆ _captureViews

SLVMat4f SLGLTextureIBL::_captureViews
protected

All 6 positions of the views that represent the 6 sides of the cube map.

Definition at line 70 of file SLGLTextureIBL.h.

◆ _cubeVAO

SLuint SLGLTextureIBL::_cubeVAO = 0
protected

Definition at line 62 of file SLGLTextureIBL.h.

◆ _cubeVBO

SLuint SLGLTextureIBL::_cubeVBO = 0
protected

Definition at line 63 of file SLGLTextureIBL.h.

◆ _quadVAO

SLuint SLGLTextureIBL::_quadVAO = 0
protected

Definition at line 64 of file SLGLTextureIBL.h.

◆ _quadVBO

SLuint SLGLTextureIBL::_quadVBO = 0
protected

Definition at line 65 of file SLGLTextureIBL.h.

◆ _readBackPixels

SLbool SLGLTextureIBL::_readBackPixels
protected

Flag if generated texture should be read back from GPU into cvMat.

Definition at line 71 of file SLGLTextureIBL.h.

◆ _shaderProgram

SLGLProgram* SLGLTextureIBL::_shaderProgram
protected

Shader program to render the texture.

Definition at line 68 of file SLGLTextureIBL.h.

◆ _sourceTexture

SLGLTexture* SLGLTextureIBL::_sourceTexture
protected

2D Texture from the HDR Image

Definition at line 67 of file SLGLTextureIBL.h.


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