Visual Computing Library  devel
Loading...
Searching...
No Matches
drawable_environment.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2026 *
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 Mozilla Public License Version 2.0 as published *
13 * by the Mozilla 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 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_BGFX_DRAWABLE_DRAWABLE_ENVIRONMENT_H
24#define VCL_BGFX_DRAWABLE_DRAWABLE_ENVIRONMENT_H
25
26#include <vclib/bgfx/buffers.h>
27#include <vclib/bgfx/texture.h>
28#include <vclib/bgfx/uniform.h>
29#include <vclib/render/settings/pbr_viewer_settings.h>
30
31#include <vclib/base.h>
32#include <vclib/io.h>
33
34#include <bgfx/bgfx.h>
35#include <bx/allocator.h>
36
37namespace vcl {
38
46{
47 // Supported file formats for environment maps
48 enum class FileFormat { UNKNOWN, HDR, EXR, KTX, DDS };
49
50 // Size of the BRDF lookup texture
51 static const uint BRDF_LU_TEXTURE_SIZE = 1024;
52
53 // Sampler of mHdrTexture
54 const Uniform mHdrSamplerUniform =
55 Uniform("s_hdr", bgfx::UniformType::Sampler);
56 // Sampler of mCubeMapTexture
57 const Uniform mEnvCubeSamplerUniform =
58 Uniform("s_env0", bgfx::UniformType::Sampler);
59 // Sampler of mIrradianceTexture
60 const Uniform mIrradianceCubeSamplerUniform =
61 Uniform("s_irradiance", bgfx::UniformType::Sampler);
62 // Sampler of mSpecularTexture
63 const Uniform mSpecularCubeSamplerUniform =
64 Uniform("s_specular", bgfx::UniformType::Sampler);
65 // Sampler of mBrdfLuTexture
66 const Uniform mBrdfLutSamplerUniform =
67 Uniform("s_brdf_lut", bgfx::UniformType::Sampler);
68
69 // The path of the environment image file provided as input.
70 std::string mImagePath;
71
72 // The number of mip levels in the specular cubemap,
73 // this value is needed by the shader to correctly sample the specular map.
74 uint8_t mSpecularMipLevels = 0;
75
76 // The HDR environment map,
77 // this is used if the input file is either in the HDR or EXR format
78 // and it is an equirectangular image.
79 Texture mHdrTexture;
80
81 // The cubemap environment map,
82 // this is either used directly if the input file is in the KTX or DDS
83 // format or contains the cubemap generated from an equirectangular image.
84 // It is used for display in the background and for caluclating the
85 // irradiance and specular maps for the image-based lighting.
86 Texture mCubeMapTexture;
87
88 // The irradiance cubemap, generated from the environment cubemap.
89 // It contains the diffuse part of the image-based lighting and
90 // it is used in the shader to compute the diffuse contribution of the
91 // environment lighting.
92 Texture mIrradianceTexture;
93
94 // The specular cubemap, generated from the environment cubemap.
95 // It contains the specular part of the image-based lighting and
96 // it is used in the shader to compute the specular contribution of the
97 // environment lighting.
98 Texture mSpecularTexture;
99
100 // The BRDF lookup texture, generated independently from the environment
101 // map. It is used in the shader to compute the BRDF part of the image-based
102 // lighting.
103 Texture mBrdfLuTexture;
104
105 // Vertex buffer for rendering a full-screen triangle, used for drawing the
106 // background.
107 const vcl::VertexBuffer mVertexBuffer = fullScreenTriangle();
108
109public:
114 enum class TextureType { RAW_CUBE, IRRADIANCE, SPECULAR, BRDF_LUT };
115
116 DrawableEnvironment() = default;
117
118 DrawableEnvironment(const std::string& imagePath, uint viewId = UINT_NULL);
119
121
123
124 ~DrawableEnvironment() = default;
125
126 DrawableEnvironment& operator=(const DrawableEnvironment& other) = delete;
127
128 DrawableEnvironment& operator=(DrawableEnvironment&& other)
129 {
130 swap(other);
131 return *this;
132 }
133
134 void swap(DrawableEnvironment& other)
135 {
136 using std::swap;
137 swap(mImagePath, other.mImagePath);
138 swap(mSpecularMipLevels, other.mSpecularMipLevels);
139 swap(mHdrTexture, other.mHdrTexture);
140 swap(mCubeMapTexture, other.mCubeMapTexture);
141 swap(mIrradianceTexture, other.mIrradianceTexture);
142 swap(mSpecularTexture, other.mSpecularTexture);
143 swap(mBrdfLuTexture, other.mBrdfLuTexture);
144 }
145
146 friend void swap(DrawableEnvironment& first, DrawableEnvironment& second)
147 {
148 first.swap(second);
149 }
150
151 const std::string& imagePath() const { return mImagePath; }
152
153 std::string imageFileName() const
154 {
155 return FileInfo::fileNameWithExtension(mImagePath);
156 }
157
158 uint8_t specularMipLevels() const { return mSpecularMipLevels; }
159
160 void drawBackground(uint viewId, const PBRViewerSettings& settings) const;
161
162 void bindTexture(
163 TextureType type,
164 uint stage,
165 uint samplerFlags = BGFX_SAMPLER_UVW_CLAMP) const;
166
171 bool canDraw() const { return mCubeMapTexture.isValid(); }
172
173private:
174 FileFormat getFileFormat(const std::string& imagePath);
175
176 bimg::ImageContainer* loadImage(std::string imagePath);
177
178 void setAndGenerateTextures(const bimg::ImageContainer& image, uint viewId);
179
180 void generateTextures(
181 const bimg::ImageContainer& image,
182 uint cubeSide,
184 uint viewId);
185
187};
188
189} // namespace vcl
190
191#endif // VCL_BGFX_DRAWABLE_DRAWABLE_ENVIRONMENT_H
A class representing an environment for PBR rendering.
Definition drawable_environment.h:46
FileFormat getFileFormat(const std::string &imagePath)
Determines the file format of the given image based on its extension.
Definition drawable_environment.cpp:126
void setAndGenerateTextures(const bimg::ImageContainer &image, uint viewId)
Sets up the environment textures based on the given image.
Definition drawable_environment.cpp:215
void generateTextures(const bimg::ImageContainer &image, uint cubeSide, uint8_t cubeMips, uint viewId)
Generates the necessary environment textures (cubemap, irradiance map, specular map,...
Definition drawable_environment.cpp:302
static vcl::VertexBuffer fullScreenTriangle()
Sets and returns the buffer for the full-screen triangle for background drawing.
Definition drawable_environment.cpp:438
void bindTexture(TextureType type, uint stage, uint samplerFlags=BGFX_SAMPLER_UVW_CLAMP) const
Binds the specified environment texture to the given texture stage.
Definition drawable_environment.cpp:91
TextureType
Types of environment textures managed by the DrawableEnvironment class.
Definition drawable_environment.h:114
void drawBackground(uint viewId, const PBRViewerSettings &settings) const
Draws the environment in the background.
Definition drawable_environment.cpp:65
bimg::ImageContainer * loadImage(std::string imagePath)
Loads the image from the specified file path.
Definition drawable_environment.cpp:148
bool canDraw() const
Checks if the environment is ready to be drawn.
Definition drawable_environment.h:171
The FileFormat class represents a file format.
Definition file_format.h:40
static std::string fileNameWithExtension(const std::string &fullpath)
Get the filename with extension of a file.
Definition file_info.h:240
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:41
Manages a BGFX texture.
Definition texture.h:45
bool isValid() const
Checks if the Texture holds valid BGFX texture handle.
Definition texture.h:133
The Uniform class wraps a bgfx::UniformHandle and provides a simple interface to set the uniform data...
Definition uniform.h:43
The VertexBuffer manages the lifetime of a bgfx::VertexBufferHandle.
Definition vertex_buffer.h:43
constexpr uint UINT_NULL
The UINT_NULL value represent a null value of uint that is the maximum value that can be represented ...
Definition base.h:49