Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/3d/ofxAssimpExample/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void ofApp::draw(){
// // Shadows have the following gl culling enabled by default
// // this helps reduce z fighting by only rendering the rear facing triangles to the depth map
// // the culling can be disabled by calling
// light.getShadow().setGlCullingEnabled(false);
// light.getShadow().setCullingEnabled(false);
// // or the culling winding order can be changed by calling
// light.getShadow().setFrontFaceWindingOrder(GL_CCW); // default is GL_CW
renderScene();
Expand Down
2 changes: 1 addition & 1 deletion examples/gl/materialPBRAdvancedExample/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void ofApp::setup(){
light.setPosition(100.1, 400, 600 );
light.lookAt(glm::vec3(0,0,0));
light.getShadow().setEnabled(true);
light.getShadow().setGlCullingEnabled(true);
light.getShadow().setCullingEnabled(true);
light.getShadow().setDirectionalBounds(2000, 1000);
light.getShadow().setNearClip(200);
light.getShadow().setFarClip(2000);
Expand Down
2 changes: 1 addition & 1 deletion examples/gl/shadowsExample/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void ofApp::draw(){
//glCullFace(GL_FRONT);

// the culling can be disabled by calling
//light->getShadow().setGlCullingEnabled(false);
//light->getShadow().setCullingEnabled(false);
// or the culling winding order can be changed by calling
//light->getShadow().setFrontFaceWindingOrder(GL_CCW); // default is GL_CW
renderScene();
Expand Down
2 changes: 1 addition & 1 deletion examples/gl/textureBufferInstancedExample/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void ofApp::setup(){
light.setPosition(0.0, 3600, 600 );
light.lookAt( glm::vec3(0,0,0) );
light.getShadow().setEnabled(true);
light.getShadow().setGlCullingEnabled(true);
light.getShadow().setCullingEnabled(true);
light.getShadow().setStrength(0.2);
light.getShadow().setNearClip(200);
light.getShadow().setFarClip(7500);
Expand Down
41 changes: 17 additions & 24 deletions libs/openFrameworks/gl/ofCubeMap.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
//
// ofCubeMap.cpp
//
// Created by Nick Hardeman on 10/16/22.
//

#include "ofShader.h"
#include "ofCubeMap.h"
#include "ofImage.h"
Expand All @@ -15,6 +9,7 @@
#include "ofFbo.h"
#include "ofTexture.h"
#include "ofFileUtils.h"
#include "ofMaterial.h"

#ifdef TARGET_ANDROID
#include "ofAppAndroidWindow.h"
Expand Down Expand Up @@ -90,28 +85,26 @@ static void release(GLuint id){
#ifdef TARGET_ANDROID
// TODO: Hook this up to an event
void ofCubeMap::regenerateAllTextures() {
for(size_t i=0; i<ofCubeMapsData().size(); i++) {
if(!ofCubeMapsData()[i].expired()) {
std::shared_ptr<ofCubeMap::Data> cubeMap = ofCubeMapsData()[i].lock();
for(size_t i=0;i<getCubeMapsData().size(); i++) {
if(!getCubeMapsData()[i].expired()) {
auto cubeMap = getCubeMapsData()[i].lock();
ofCubeMap::clearTextureData(cubeMap);
}
}
sBrdfLutTex.clear();
}
#endif



//----------------------------------------
vector<weak_ptr<ofCubeMap::Data> > & ofCubeMapsData(){
static vector<weak_ptr<ofCubeMap::Data> > * cubeMapsActive = new vector<weak_ptr<ofCubeMap::Data> >;
return *cubeMapsActive;
//--------------------------------------------------------------
std::vector<std::weak_ptr<ofCubeMap::Data>>& ofCubeMap::getCubeMapsData() {
static std::vector<std::weak_ptr<ofCubeMap::Data>> cubeMapsDataActive;
return cubeMapsDataActive;
}

//--------------------------------------------------------------
bool ofCubeMap::hasActiveCubeMap() {
for(size_t i=0;i< ofCubeMapsData().size();i++){
std::shared_ptr<ofCubeMap::Data> cubeMap = ofCubeMapsData()[i].lock();
for(size_t i=0;i<getCubeMapsData().size();i++){
auto cubeMap = getCubeMapsData()[i].lock();
if(cubeMap && cubeMap->isEnabled && cubeMap->index > -1 ){
return true;
break;
Expand All @@ -122,8 +115,8 @@ bool ofCubeMap::hasActiveCubeMap() {

//--------------------------------------------------------------
std::shared_ptr<ofCubeMap::Data> ofCubeMap::getActiveData() {
for(size_t i=0;i< ofCubeMapsData().size();i++){
std::shared_ptr<ofCubeMap::Data> cubeMap = ofCubeMapsData()[i].lock();
for(size_t i=0; i < getCubeMapsData().size();i++){
auto cubeMap = getCubeMapsData()[i].lock();
if(cubeMap && cubeMap->isEnabled && cubeMap->index > -1 ){
return cubeMap;
}
Expand Down Expand Up @@ -155,17 +148,17 @@ void ofCubeMap::_checkSetup() {
if( data->index < 0 ) {
bool bFound = false;
// search for the first free block
for(size_t i=0; i<ofCubeMapsData().size(); i++) {
if(ofCubeMapsData()[i].expired()) {
for(size_t i=0; i< getCubeMapsData().size(); i++) {
if(getCubeMapsData()[i].expired()) {
data->index = i;
ofCubeMapsData()[i] = data;
getCubeMapsData()[i] = data;
bFound = true;
break;
}
}
if(!bFound && ofIsGLProgrammableRenderer()){
ofCubeMapsData().push_back(data);
data->index = ofCubeMapsData().size() - 1;
getCubeMapsData().push_back(data);
data->index = getCubeMapsData().size() - 1;
bFound = true;
}
}
Expand Down
134 changes: 89 additions & 45 deletions libs/openFrameworks/gl/ofCubeMap.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
//
// ofCubeMap.h
//
// Created by Nick Hardeman on 10/16/22.
//

#pragma once

#include "ofShader.h"
Expand All @@ -18,68 +12,97 @@

class ofVboMesh;
class ofGLProgrammableRenderer;

class ofCubeMap {
public:
struct ofCubeMapSettings {
of::filesystem::path filePath { "" };
of::filesystem::path cacheDirectory { "" };

bool overwriteCache = false;
bool useCache = false;
bool useLutTex = false;
bool flipVertically = true;
bool useMaximumPrecision = false;

int resolution = 512;
class ofMaterial;


struct ofCubeMapSettings {
of::filesystem::path filePath { "" }; ///< path to load the cube map, a single texture
of::filesystem::path cacheDirectory { "" }; ///< directory to cache the irradiance and pre filter maps
bool overwriteCache = false; ///< overwrite the cache of the irradiance and pre filter maps
bool useCache = false; ///< check for cached irradiance and pre filter images on disk
bool useLutTex = false; ///< create a lut texture to pass to the shaders
bool flipVertically = true; ///< flip the loaded cubemap image vertically
bool useMaximumPrecision = false; ///< use 32 bit float (GL_RGB32F) precision, default is 16 bit (GL_RGB16F)
int resolution = 512; ///< resolution of each side of the cube map to be generated
#ifdef TARGET_OPENGLES
int irradianceRes = 16;
int preFilterRes = 64;
int irradianceRes = 16; ///< resolution of each side of the irradiance map to be generated
int preFilterRes = 64;///< resolution of each side of the pre filter map to be generated
#else
int irradianceRes = 32;
int preFilterRes = 128;
int irradianceRes = 32; ///< resolution of each side of the irradiance map to be generated
int preFilterRes = 128; ///< resolution of each side of the pre filter map to be generated
#endif
};
};

class ofCubeMap {
protected:

/// \class ofCubeMap::Data
///
/// \brief Data class structure for storing GL texture ids and other data regarding cube maps
/// Used internally for access to cube map data in the rendering pipeline, ie. ofMaterial.
/// A Data class is created for each instance of ofCubeMap and all of them can be accessed by ofCubeMapsData().
/// Which is declared towards the bottom of this file.
class Data {
public:
GLuint cubeMapId = 0;
GLuint cubeMapId = 0; ///< GL texture id of the cube map
bool bCubeMapAllocated = false;
GLuint irradianceMapId = 0;
GLuint irradianceMapId = 0; ///< GL texture id of the irradiance map
bool bIrradianceAllocated = false;
GLuint preFilteredMapId = 0;
GLuint preFilteredMapId = 0; ///< GL texture id of the pre filter map
bool bPreFilteredMapAllocated = false;

int index = -1;
bool isEnabled = true;

ofCubeMapSettings settings;

int maxMipLevels = 5;
float exposure = 1.0;
ofCubeMapSettings settings; ///< settings associated with this cube map
int maxMipLevels = 5; ///< number of mip map levels for generating the prefilted map
float exposure = 1.0; ///< used for storing the exposure of the cube map, which is set with ofCubeMap::setExposure()
};

static GLenum getTextureTarget();

/// \brief Global storage for all cube maps created. The Data class holds infomation about textures and other information
/// relevant for rendering. This function is utilized internally.
/// More about the Data class above.
/// \return std::vector<std::weak_ptr<ofCubeMap::Data>>&.
static std::vector<std::weak_ptr<ofCubeMap::Data>>& getCubeMapsData();

/// \brief Is there any active cube map.
/// \return true if there is any active cube map.
static bool hasActiveCubeMap();
/// \brief Get the first cube map data that is enabled.
/// \return std::shared_ptr<ofCubeMap::Data> of the active cube map data.
static std::shared_ptr<ofCubeMap::Data> getActiveData();
/// \brief Internal function for releasing the GL texture memory.
/// \param std::shared_ptr<ofCubeMap::Data> cube map data holding texture ids to clear.
static void clearTextureData(std::shared_ptr<ofCubeMap::Data> adata);
#ifdef TARGET_ANDROID
static void regenerateAllTextures();
#endif
static const ofTexture & getBrdfLutTexture();

friend class ofMaterial;
friend class ofShader;

public:

/// \brief Return the GL texture type.
/// \return GLenum GL_TEXTURE_CUBE_MAP.
static GLenum getTextureTarget();

ofCubeMap();
ofCubeMap(const ofCubeMap & mom);
ofCubeMap(ofCubeMap && mom);

~ofCubeMap();

/// \load an image and convert to cube map.
/// \load An image and convert to cube map.
/// \param apath path to the image to load.
/// \param aFaceResolution resolution of the cube map image sides.
/// \param aBFlipY flip the images upside down.
bool load(const of::filesystem::path & apath, int aFaceResolution, bool aBFlipY = true);
/// \load an image and convert to cube map.
/// \load An image and convert to cube map.
/// \param apath path to the image to load.
/// \param aFaceResolution resolution of the cube map image sides.
/// \param aBFlipY flip the images upside down.
Expand All @@ -94,14 +117,18 @@ class ofCubeMap {
/// \param mom The ofCubeMap to copy from. Reuses internal GL texture IDs.
ofCubeMap & operator=(const ofCubeMap & mom);
ofCubeMap & operator=(ofCubeMap && mom);

/// \brief Clear the GL textures.
void clear();

/// \brief Calls drawCubeMap();
void draw();
/// \brief Render the cube map.
void drawCubeMap();
/// \brief Render the irradiance map.
void drawIrradiance();
/// \brief Render prefilted map.
/// \param aRoughness from 0 - 1.
void drawPrefilteredCube(float aRoughness);

bool isEnabled() { return data->isEnabled; }
bool isEnabled() const { return data->isEnabled; }
void setEnabled(bool ab) { data->isEnabled = ab; }
Expand All @@ -111,18 +138,36 @@ class ofCubeMap {
bool hasIrradianceMap();

int getFaceResolution() { return data->settings.resolution; }
/// \brief Get the GL texture id of the cube map.
/// \return GLuint.
GLuint getTextureId();
/// \brief Does this platform support HDR.
/// \return bool.
bool doesSupportHdr();
/// \brief Is this cube map HDR.
/// \return bool.
bool isHdr();
/// \brief Are the textures GL_RGB16F.
/// \return bool.
bool isMediumPrecision();

/// \brief Set the exposure of the cube map.
/// \param aExposure from 0 - 1.
void setExposure(float aExposure);
/// \brief Get the exposure of the cube map.
/// \return float from 0 - 1.
float getExposure() { return data->exposure; }

/// \brief Use as precalculated BRDF Lut texture.
/// \param ab bool to use texture.
void setUseBrdfLutTexture(bool ab);
/// \brief Is Using BRDF Lut texture.
/// \return bool.
bool isUsingLutBrdfTexture() { return data->settings.useLutTex; }


/// \brief Get the texture id of the irradiance map.
/// \return GLuint, texture id.
GLuint getIrradianceMapId() { return data->irradianceMapId; }
/// \brief Get the texture id of the pre filtered map.
/// \return GLuint, texture id.
GLuint getPrefilterMapId() { return data->preFilteredMapId; }

protected:
Expand Down Expand Up @@ -178,4 +223,3 @@ class ofCubeMap {
static ofShader shaderBrdfLUT;
};

std::vector<std::weak_ptr<ofCubeMap::Data>> & ofCubeMapsData();
8 changes: 4 additions & 4 deletions libs/openFrameworks/gl/ofMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ void ofMaterial::initShaders(ofGLProgrammableRenderer & renderer) const{

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

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

std::shared_ptr<ofCubeMap::Data> cubeMapData = ofCubeMap::getActiveData();
auto cubeMapData = ofCubeMap::getActiveData();
if( cubeMapData ) {
shader.setUniform1f("mat_ibl_exposure", cubeMapData->exposure );
shader.setUniform1f("uCubeMapEnabled", 1.0f );
Expand Down Expand Up @@ -1446,15 +1446,15 @@ const std::string ofMaterial::getDefinesString() const {
#endif
}

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

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

bool bHasIrradiance = false;
bool bPreFilteredMap = false;
bool bBrdfLutTex = false;
for( auto cmdWeak : ofCubeMapsData() ) {
for( auto cmdWeak : ofCubeMap::getCubeMapsData() ) {
auto cmd = cmdWeak.lock();
if( !cmd ) continue;
if( cmd->bIrradianceAllocated ) {
Expand Down
6 changes: 3 additions & 3 deletions libs/openFrameworks/gl/ofShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1469,9 +1469,9 @@ bool ofShader::setShadowUniforms(int textureLocation) const {
setUniformTexture("uShadowMapSpot", ofShadow::getTextureTarget(OF_LIGHT_SPOT), ofShadow::getSpotTexId(), textureLocation + 2);
setUniformTexture("uShadowMapArea", ofShadow::getTextureTarget(OF_LIGHT_AREA), ofShadow::getAreaTexId(), textureLocation + 3);

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

std::shared_ptr<ofCubeMap::Data> cubeMapData = ofCubeMap::getActiveData();
auto cubeMapData = ofCubeMap::getActiveData();
if (cubeMapData) {
if (cubeMapData->bIrradianceAllocated) {
setUniformTexture("tex_irradianceMap", GL_TEXTURE_CUBE_MAP, cubeMapData->irradianceMapId, textureLocation);
Expand Down
Loading
Loading