Skip to content

Commit 168a647

Browse files
authored
Cleanup ofShadow and ofCubemap (#8422)
1 parent 57cabbb commit 168a647

File tree

10 files changed

+350
-211
lines changed

10 files changed

+350
-211
lines changed

examples/3d/ofxAssimpExample/src/ofApp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void ofApp::draw(){
175175
// // Shadows have the following gl culling enabled by default
176176
// // this helps reduce z fighting by only rendering the rear facing triangles to the depth map
177177
// // the culling can be disabled by calling
178-
// light.getShadow().setGlCullingEnabled(false);
178+
// light.getShadow().setCullingEnabled(false);
179179
// // or the culling winding order can be changed by calling
180180
// light.getShadow().setFrontFaceWindingOrder(GL_CCW); // default is GL_CW
181181
renderScene();

examples/gl/materialPBRAdvancedExample/src/ofApp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void ofApp::setup(){
1212
light.setPosition(100.1, 400, 600 );
1313
light.lookAt(glm::vec3(0,0,0));
1414
light.getShadow().setEnabled(true);
15-
light.getShadow().setGlCullingEnabled(true);
15+
light.getShadow().setCullingEnabled(true);
1616
light.getShadow().setDirectionalBounds(2000, 1000);
1717
light.getShadow().setNearClip(200);
1818
light.getShadow().setFarClip(2000);

examples/gl/shadowsExample/src/ofApp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void ofApp::draw(){
151151
//glCullFace(GL_FRONT);
152152

153153
// the culling can be disabled by calling
154-
//light->getShadow().setGlCullingEnabled(false);
154+
//light->getShadow().setCullingEnabled(false);
155155
// or the culling winding order can be changed by calling
156156
//light->getShadow().setFrontFaceWindingOrder(GL_CCW); // default is GL_CW
157157
renderScene();

examples/gl/textureBufferInstancedExample/src/ofApp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void ofApp::setup(){
6565
light.setPosition(0.0, 3600, 600 );
6666
light.lookAt( glm::vec3(0,0,0) );
6767
light.getShadow().setEnabled(true);
68-
light.getShadow().setGlCullingEnabled(true);
68+
light.getShadow().setCullingEnabled(true);
6969
light.getShadow().setStrength(0.2);
7070
light.getShadow().setNearClip(200);
7171
light.getShadow().setFarClip(7500);

libs/openFrameworks/gl/ofCubeMap.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
//
2-
// ofCubeMap.cpp
3-
//
4-
// Created by Nick Hardeman on 10/16/22.
5-
//
6-
71
#include "ofShader.h"
82
#include "ofCubeMap.h"
93
#include "ofImage.h"
@@ -15,6 +9,7 @@
159
#include "ofFbo.h"
1610
#include "ofTexture.h"
1711
#include "ofFileUtils.h"
12+
#include "ofMaterial.h"
1813

1914
#ifdef TARGET_ANDROID
2015
#include "ofAppAndroidWindow.h"
@@ -90,28 +85,26 @@ static void release(GLuint id){
9085
#ifdef TARGET_ANDROID
9186
// TODO: Hook this up to an event
9287
void ofCubeMap::regenerateAllTextures() {
93-
for(size_t i=0; i<ofCubeMapsData().size(); i++) {
94-
if(!ofCubeMapsData()[i].expired()) {
95-
std::shared_ptr<ofCubeMap::Data> cubeMap = ofCubeMapsData()[i].lock();
88+
for(size_t i=0;i<getCubeMapsData().size(); i++) {
89+
if(!getCubeMapsData()[i].expired()) {
90+
auto cubeMap = getCubeMapsData()[i].lock();
9691
ofCubeMap::clearTextureData(cubeMap);
9792
}
9893
}
9994
sBrdfLutTex.clear();
10095
}
10196
#endif
10297

103-
104-
105-
//----------------------------------------
106-
vector<weak_ptr<ofCubeMap::Data> > & ofCubeMapsData(){
107-
static vector<weak_ptr<ofCubeMap::Data> > * cubeMapsActive = new vector<weak_ptr<ofCubeMap::Data> >;
108-
return *cubeMapsActive;
98+
//--------------------------------------------------------------
99+
std::vector<std::weak_ptr<ofCubeMap::Data>>& ofCubeMap::getCubeMapsData() {
100+
static std::vector<std::weak_ptr<ofCubeMap::Data>> cubeMapsDataActive;
101+
return cubeMapsDataActive;
109102
}
110103

111104
//--------------------------------------------------------------
112105
bool ofCubeMap::hasActiveCubeMap() {
113-
for(size_t i=0;i< ofCubeMapsData().size();i++){
114-
std::shared_ptr<ofCubeMap::Data> cubeMap = ofCubeMapsData()[i].lock();
106+
for(size_t i=0;i<getCubeMapsData().size();i++){
107+
auto cubeMap = getCubeMapsData()[i].lock();
115108
if(cubeMap && cubeMap->isEnabled && cubeMap->index > -1 ){
116109
return true;
117110
break;
@@ -122,8 +115,8 @@ bool ofCubeMap::hasActiveCubeMap() {
122115

123116
//--------------------------------------------------------------
124117
std::shared_ptr<ofCubeMap::Data> ofCubeMap::getActiveData() {
125-
for(size_t i=0;i< ofCubeMapsData().size();i++){
126-
std::shared_ptr<ofCubeMap::Data> cubeMap = ofCubeMapsData()[i].lock();
118+
for(size_t i=0; i < getCubeMapsData().size();i++){
119+
auto cubeMap = getCubeMapsData()[i].lock();
127120
if(cubeMap && cubeMap->isEnabled && cubeMap->index > -1 ){
128121
return cubeMap;
129122
}
@@ -155,17 +148,17 @@ void ofCubeMap::_checkSetup() {
155148
if( data->index < 0 ) {
156149
bool bFound = false;
157150
// search for the first free block
158-
for(size_t i=0; i<ofCubeMapsData().size(); i++) {
159-
if(ofCubeMapsData()[i].expired()) {
151+
for(size_t i=0; i< getCubeMapsData().size(); i++) {
152+
if(getCubeMapsData()[i].expired()) {
160153
data->index = i;
161-
ofCubeMapsData()[i] = data;
154+
getCubeMapsData()[i] = data;
162155
bFound = true;
163156
break;
164157
}
165158
}
166159
if(!bFound && ofIsGLProgrammableRenderer()){
167-
ofCubeMapsData().push_back(data);
168-
data->index = ofCubeMapsData().size() - 1;
160+
getCubeMapsData().push_back(data);
161+
data->index = getCubeMapsData().size() - 1;
169162
bFound = true;
170163
}
171164
}

libs/openFrameworks/gl/ofCubeMap.h

Lines changed: 89 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
//
2-
// ofCubeMap.h
3-
//
4-
// Created by Nick Hardeman on 10/16/22.
5-
//
6-
71
#pragma once
82

93
#include "ofShader.h"
@@ -18,68 +12,97 @@
1812

1913
class ofVboMesh;
2014
class ofGLProgrammableRenderer;
21-
22-
class ofCubeMap {
23-
public:
24-
struct ofCubeMapSettings {
25-
of::filesystem::path filePath { "" };
26-
of::filesystem::path cacheDirectory { "" };
27-
28-
bool overwriteCache = false;
29-
bool useCache = false;
30-
bool useLutTex = false;
31-
bool flipVertically = true;
32-
bool useMaximumPrecision = false;
33-
34-
int resolution = 512;
15+
class ofMaterial;
16+
17+
18+
struct ofCubeMapSettings {
19+
of::filesystem::path filePath { "" }; ///< path to load the cube map, a single texture
20+
of::filesystem::path cacheDirectory { "" }; ///< directory to cache the irradiance and pre filter maps
21+
22+
bool overwriteCache = false; ///< overwrite the cache of the irradiance and pre filter maps
23+
bool useCache = false; ///< check for cached irradiance and pre filter images on disk
24+
bool useLutTex = false; ///< create a lut texture to pass to the shaders
25+
bool flipVertically = true; ///< flip the loaded cubemap image vertically
26+
bool useMaximumPrecision = false; ///< use 32 bit float (GL_RGB32F) precision, default is 16 bit (GL_RGB16F)
27+
28+
int resolution = 512; ///< resolution of each side of the cube map to be generated
3529
#ifdef TARGET_OPENGLES
36-
int irradianceRes = 16;
37-
int preFilterRes = 64;
30+
int irradianceRes = 16; ///< resolution of each side of the irradiance map to be generated
31+
int preFilterRes = 64;///< resolution of each side of the pre filter map to be generated
3832
#else
39-
int irradianceRes = 32;
40-
int preFilterRes = 128;
33+
int irradianceRes = 32; ///< resolution of each side of the irradiance map to be generated
34+
int preFilterRes = 128; ///< resolution of each side of the pre filter map to be generated
4135
#endif
42-
};
36+
};
4337

38+
class ofCubeMap {
39+
protected:
40+
41+
/// \class ofCubeMap::Data
42+
///
43+
/// \brief Data class structure for storing GL texture ids and other data regarding cube maps
44+
/// Used internally for access to cube map data in the rendering pipeline, ie. ofMaterial.
45+
/// A Data class is created for each instance of ofCubeMap and all of them can be accessed by ofCubeMapsData().
46+
/// Which is declared towards the bottom of this file.
4447
class Data {
4548
public:
46-
GLuint cubeMapId = 0;
49+
GLuint cubeMapId = 0; ///< GL texture id of the cube map
4750
bool bCubeMapAllocated = false;
48-
GLuint irradianceMapId = 0;
51+
GLuint irradianceMapId = 0; ///< GL texture id of the irradiance map
4952
bool bIrradianceAllocated = false;
50-
GLuint preFilteredMapId = 0;
53+
GLuint preFilteredMapId = 0; ///< GL texture id of the pre filter map
5154
bool bPreFilteredMapAllocated = false;
52-
55+
5356
int index = -1;
5457
bool isEnabled = true;
55-
56-
ofCubeMapSettings settings;
57-
58-
int maxMipLevels = 5;
59-
float exposure = 1.0;
58+
59+
ofCubeMapSettings settings; ///< settings associated with this cube map
60+
61+
int maxMipLevels = 5; ///< number of mip map levels for generating the prefilted map
62+
float exposure = 1.0; ///< used for storing the exposure of the cube map, which is set with ofCubeMap::setExposure()
6063
};
61-
62-
static GLenum getTextureTarget();
64+
65+
/// \brief Global storage for all cube maps created. The Data class holds infomation about textures and other information
66+
/// relevant for rendering. This function is utilized internally.
67+
/// More about the Data class above.
68+
/// \return std::vector<std::weak_ptr<ofCubeMap::Data>>&.
69+
static std::vector<std::weak_ptr<ofCubeMap::Data>>& getCubeMapsData();
70+
71+
/// \brief Is there any active cube map.
72+
/// \return true if there is any active cube map.
6373
static bool hasActiveCubeMap();
74+
/// \brief Get the first cube map data that is enabled.
75+
/// \return std::shared_ptr<ofCubeMap::Data> of the active cube map data.
6476
static std::shared_ptr<ofCubeMap::Data> getActiveData();
77+
/// \brief Internal function for releasing the GL texture memory.
78+
/// \param std::shared_ptr<ofCubeMap::Data> cube map data holding texture ids to clear.
6579
static void clearTextureData(std::shared_ptr<ofCubeMap::Data> adata);
6680
#ifdef TARGET_ANDROID
6781
static void regenerateAllTextures();
6882
#endif
6983
static const ofTexture & getBrdfLutTexture();
84+
85+
friend class ofMaterial;
86+
friend class ofShader;
87+
88+
public:
89+
90+
/// \brief Return the GL texture type.
91+
/// \return GLenum GL_TEXTURE_CUBE_MAP.
92+
static GLenum getTextureTarget();
7093

7194
ofCubeMap();
7295
ofCubeMap(const ofCubeMap & mom);
7396
ofCubeMap(ofCubeMap && mom);
7497

7598
~ofCubeMap();
7699

77-
/// \load an image and convert to cube map.
100+
/// \load An image and convert to cube map.
78101
/// \param apath path to the image to load.
79102
/// \param aFaceResolution resolution of the cube map image sides.
80103
/// \param aBFlipY flip the images upside down.
81104
bool load(const of::filesystem::path & apath, int aFaceResolution, bool aBFlipY = true);
82-
/// \load an image and convert to cube map.
105+
/// \load An image and convert to cube map.
83106
/// \param apath path to the image to load.
84107
/// \param aFaceResolution resolution of the cube map image sides.
85108
/// \param aBFlipY flip the images upside down.
@@ -94,14 +117,18 @@ class ofCubeMap {
94117
/// \param mom The ofCubeMap to copy from. Reuses internal GL texture IDs.
95118
ofCubeMap & operator=(const ofCubeMap & mom);
96119
ofCubeMap & operator=(ofCubeMap && mom);
97-
120+
/// \brief Clear the GL textures.
98121
void clear();
99-
122+
/// \brief Calls drawCubeMap();
100123
void draw();
124+
/// \brief Render the cube map.
101125
void drawCubeMap();
126+
/// \brief Render the irradiance map.
102127
void drawIrradiance();
128+
/// \brief Render prefilted map.
129+
/// \param aRoughness from 0 - 1.
103130
void drawPrefilteredCube(float aRoughness);
104-
131+
105132
bool isEnabled() { return data->isEnabled; }
106133
bool isEnabled() const { return data->isEnabled; }
107134
void setEnabled(bool ab) { data->isEnabled = ab; }
@@ -111,18 +138,36 @@ class ofCubeMap {
111138
bool hasIrradianceMap();
112139

113140
int getFaceResolution() { return data->settings.resolution; }
141+
/// \brief Get the GL texture id of the cube map.
142+
/// \return GLuint.
114143
GLuint getTextureId();
144+
/// \brief Does this platform support HDR.
145+
/// \return bool.
115146
bool doesSupportHdr();
147+
/// \brief Is this cube map HDR.
148+
/// \return bool.
116149
bool isHdr();
150+
/// \brief Are the textures GL_RGB16F.
151+
/// \return bool.
117152
bool isMediumPrecision();
118-
153+
/// \brief Set the exposure of the cube map.
154+
/// \param aExposure from 0 - 1.
119155
void setExposure(float aExposure);
156+
/// \brief Get the exposure of the cube map.
157+
/// \return float from 0 - 1.
120158
float getExposure() { return data->exposure; }
121-
159+
/// \brief Use as precalculated BRDF Lut texture.
160+
/// \param ab bool to use texture.
122161
void setUseBrdfLutTexture(bool ab);
162+
/// \brief Is Using BRDF Lut texture.
163+
/// \return bool.
123164
bool isUsingLutBrdfTexture() { return data->settings.useLutTex; }
124-
165+
166+
/// \brief Get the texture id of the irradiance map.
167+
/// \return GLuint, texture id.
125168
GLuint getIrradianceMapId() { return data->irradianceMapId; }
169+
/// \brief Get the texture id of the pre filtered map.
170+
/// \return GLuint, texture id.
126171
GLuint getPrefilterMapId() { return data->preFilteredMapId; }
127172

128173
protected:
@@ -178,4 +223,3 @@ class ofCubeMap {
178223
static ofShader shaderBrdfLUT;
179224
};
180225

181-
std::vector<std::weak_ptr<ofCubeMap::Data>> & ofCubeMapsData();

libs/openFrameworks/gl/ofMaterial.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ void ofMaterial::initShaders(ofGLProgrammableRenderer & renderer) const{
870870

871871
size_t numLights = ofLightsData().size();
872872
// only support for a single cube map at a time
873-
size_t numCubeMaps = ofCubeMapsData().size() > 0 ? 1 : 0;
873+
size_t numCubeMaps = ofCubeMap::getCubeMapsData().size() > 0 ? 1 : 0;
874874
const std::string shaderId = getShaderStringId();
875875

876876
if(rendererShaders == shaders.end() ||
@@ -1077,7 +1077,7 @@ void ofMaterial::updateMaterial(const ofShader & shader,ofGLProgrammableRenderer
10771077
shader.setUniform1f("mat_normal_mix", data.normalGeomToNormalMapMix );
10781078
}
10791079

1080-
std::shared_ptr<ofCubeMap::Data> cubeMapData = ofCubeMap::getActiveData();
1080+
auto cubeMapData = ofCubeMap::getActiveData();
10811081
if( cubeMapData ) {
10821082
shader.setUniform1f("mat_ibl_exposure", cubeMapData->exposure );
10831083
shader.setUniform1f("uCubeMapEnabled", 1.0f );
@@ -1446,15 +1446,15 @@ const std::string ofMaterial::getDefinesString() const {
14461446
#endif
14471447
}
14481448

1449-
if(isPBR() && ofCubeMapsData().size() > 0 && ofIsGLProgrammableRenderer() ) {
1449+
if(isPBR() && ofCubeMap::getCubeMapsData().size() > 0 && ofIsGLProgrammableRenderer() ) {
14501450
// const auto& cubeMapData = ofCubeMap::getActiveData();
14511451

14521452
definesString += "#define HAS_CUBE_MAP 1\n";
14531453

14541454
bool bHasIrradiance = false;
14551455
bool bPreFilteredMap = false;
14561456
bool bBrdfLutTex = false;
1457-
for( auto cmdWeak : ofCubeMapsData() ) {
1457+
for( auto cmdWeak : ofCubeMap::getCubeMapsData() ) {
14581458
auto cmd = cmdWeak.lock();
14591459
if( !cmd ) continue;
14601460
if( cmd->bIrradianceAllocated ) {

libs/openFrameworks/gl/ofShader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,9 +1469,9 @@ bool ofShader::setShadowUniforms(int textureLocation) const {
14691469
setUniformTexture("uShadowMapSpot", ofShadow::getTextureTarget(OF_LIGHT_SPOT), ofShadow::getSpotTexId(), textureLocation + 2);
14701470
setUniformTexture("uShadowMapArea", ofShadow::getTextureTarget(OF_LIGHT_AREA), ofShadow::getAreaTexId(), textureLocation + 3);
14711471

1472-
for (size_t i = 0; i < ofShadowsData().size(); i++) {
1472+
for (size_t i = 0; i < ofShadow::getShadowsData().size(); i++) {
14731473
std::string idx = ofToString(i, 0);
1474-
std::shared_ptr<ofShadow::Data> shadow = ofShadowsData()[i].lock();
1474+
auto shadow = ofShadow::getShadowsData()[i].lock();
14751475
std::string shadowAddress = "shadows[" + idx + "]";
14761476
if (!shadow || !shadow->isEnabled || shadow->index < 0) {
14771477
setUniform1f(shadowAddress + ".enabled", 0);
@@ -1508,7 +1508,7 @@ bool ofShader::setPbrEnvironmentMapUniforms(int textureLocation) const {
15081508
return false;
15091509
}
15101510

1511-
std::shared_ptr<ofCubeMap::Data> cubeMapData = ofCubeMap::getActiveData();
1511+
auto cubeMapData = ofCubeMap::getActiveData();
15121512
if (cubeMapData) {
15131513
if (cubeMapData->bIrradianceAllocated) {
15141514
setUniformTexture("tex_irradianceMap", GL_TEXTURE_CUBE_MAP, cubeMapData->irradianceMapId, textureLocation);

0 commit comments

Comments
 (0)