Skip to content

Commit

Permalink
resource_manager: better texture loader
Browse files Browse the repository at this point in the history
pbr_manager: environment texture
  • Loading branch information
furkansarihan committed Feb 11, 2024
1 parent b669bf9 commit 7571c92
Show file tree
Hide file tree
Showing 16 changed files with 401 additions and 229 deletions.
7 changes: 7 additions & 0 deletions dev/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ int main()
// terrain.m_playerPos = character.m_position;
renderManager->addRenderable(&terrain);

// environment map
std::string path = resourceManager->m_executablePath + "/assets/hdr_skybox/skybox-7.hdr";
TextureParams textureParams;
textureParams.dataType = TextureDataType::Float32;
Texture envTexture = resourceManager->textureFromFile(textureParams, path, path);
renderManager->updateEnvironmentTexture(envTexture);

enigine.start();

return 0;
Expand Down
4 changes: 2 additions & 2 deletions src/assets/shaders/hdr-to-cubemap.vs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ uniform mat4 model;

void main()
{
localPos = aPos;
gl_Position = projection * view * model * vec4(localPos, 1.0);
localPos = aPos;
gl_Position = projection * view * model * vec4(localPos, 1.0);
}
2 changes: 1 addition & 1 deletion src/assets/shaders/terrain-pbr-deferred-pre.fs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ void main()
return;
}

// TODO: should allign with base renderer
// TODO: should align with base renderer
vec3 tangentNormal = sampleTex(texture_normal1, hs, pTexCoords) * 2.0 - 1.0;
vec3 Q1 = dFdx(pWorldPos);
vec3 Q2 = dFdy(pWorldPos);
Expand Down
2 changes: 2 additions & 0 deletions src/enigine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ int Enigine::init()
rootUI->m_uiList.push_back(renderUI);
rootUI->m_uiList.push_back(physicsWorldUI);

updateManager->add(renderUI);

return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions src/mesh/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ struct Vertex

struct Texture
{
unsigned int id, width, height;
int nrComponents;
unsigned int id;
int width, height, nrComponents;
std::string type; // TODO: change
};

Expand Down
28 changes: 25 additions & 3 deletions src/model/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void Model::loadModel(std::string const &path)
return;
}
// retrieve the directory path of the filepath
directory = path.substr(0, path.find_last_of('/'));
m_directory = path.substr(0, path.find_last_of('/'));

for (unsigned int i = 0; i < m_scene->mNumTextures; i++)
{
Expand Down Expand Up @@ -328,8 +328,30 @@ std::vector<Texture> Model::loadMaterialTextures(aiMaterial *mat, aiTextureType
aiString str;
mat->GetTexture(type, i, &str);

std::string path = std::string(str.C_Str());
Texture texture = m_resourceManager->getTexture(*this, path, typeName);
std::string texturePath = std::string(str.C_Str());
Texture texture;

TextureParams textureParams;
textureParams.generateMipmaps = true;
textureParams.anisotropicFiltering = true;
textureParams.maxAnisotropy = 4.f;

const aiTexture *embeddedTexture = m_scene->GetEmbeddedTexture(str.C_Str());
if (embeddedTexture != nullptr)
{
std::string key = m_path + "/" + texturePath;
void *buffer = embeddedTexture->pcData;
unsigned int bufferSize = embeddedTexture->mWidth;
texture = m_resourceManager->textureFromMemory(textureParams, key, buffer, bufferSize);
}
else
{
// TODO: check relative or absolute path
std::string path = m_directory + "/" + texturePath;
texture = m_resourceManager->textureFromFile(textureParams, path, path);
}

texture.type = typeName;
textures.push_back(texture);
}
return textures;
Expand Down
2 changes: 1 addition & 1 deletion src/model/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Model
glm::vec3 aabbMin;
glm::vec3 aabbMax;
std::string m_path;
std::string directory;
std::string m_directory;
bool gammaCorrection;

std::map<std::string, BoneInfo> m_boneInfoMap;
Expand Down
95 changes: 36 additions & 59 deletions src/pbr_manager/pbr_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,39 @@

#include "../external/stb_image/stb_image.h"

PbrManager::PbrManager(std::string executablePath)
: m_executablePath(executablePath),
m_cubemapFaceSize(1024)
PbrManager::PbrManager()
: m_cubemapFaceSize(1024),
m_irradianceSize(32),
m_prefilterSize(128),
m_brdfLutSize(512)
{
glGenFramebuffers(1, &captureFBO);
glGenRenderbuffers(1, &captureRBO);

glGenTextures(1, &envCubemap);
glGenTextures(1, &irradianceMap);
glGenTextures(1, &prefilterMap);
glGenTextures(1, &brdfLUTTexture);
}

PbrManager::~PbrManager()
{
// TODO: destruction
}

void PbrManager::setupCubemap(Model *cube, Shader hdrToCubemapShader)
void PbrManager::setupCubemap(Model *cube, Shader &hdrToCubemapShader)
{
stbi_set_flip_vertically_on_load(true);
int faceSize = m_cubemapFaceSize;
int width, height, nrComponents;
std::string filename = "skybox-7.hdr";
float *data = stbi_loadf((m_executablePath + "/assets/hdr_skybox/" + filename).c_str(), &width, &height, &nrComponents, 0);

if (data)
{
glGenTextures(1, &m_skyboxTexture);
glBindTexture(GL_TEXTURE_2D, m_skyboxTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, data);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

stbi_image_free(data);
}
else
{
std::cout << "Failed to load HDR image." << std::endl;
}
stbi_set_flip_vertically_on_load(false);

// TODO: extract
glGenFramebuffers(1, &captureFBO);
glGenRenderbuffers(1, &captureRBO);

glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
glBindRenderbuffer(GL_RENDERBUFFER, captureRBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, faceSize, faceSize);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, m_cubemapFaceSize, m_cubemapFaceSize);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, captureRBO);

glGenTextures(1, &envCubemap);
glBindTexture(GL_TEXTURE_CUBE_MAP, envCubemap);
for (unsigned int i = 0; i < 6; ++i)
{
// note that we store each face with 16 bit floating point values
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB32F, faceSize, faceSize, 0, GL_RGB, GL_FLOAT, nullptr);
// TODO: internal type from texture?
// note that we store each face with 32? bit floating point values
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB32F, m_cubemapFaceSize, m_cubemapFaceSize, 0, GL_RGB, GL_FLOAT, nullptr);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Expand All @@ -67,13 +47,14 @@ void PbrManager::setupCubemap(Model *cube, Shader hdrToCubemapShader)
hdrToCubemapShader.use();

hdrToCubemapShader.setMat4("projection", captureProjection);
hdrToCubemapShader.setMat4("model", glm::mat4(1));
// vertical flip
hdrToCubemapShader.setMat4("model", glm::scale(glm::mat4(1.0), glm::vec3(1.f, -1.f, 1.f)));
hdrToCubemapShader.setInt("equirectangularMap", 0);
glActiveTexture(GL_TEXTURE0);
glUniform1i(glGetUniformLocation(hdrToCubemapShader.id, "equirectangularMap"), 0);
glBindTexture(GL_TEXTURE_2D, m_skyboxTexture);
glBindTexture(GL_TEXTURE_2D, m_environmentTexture.id);

glViewport(0, 0, faceSize, faceSize); // don't forget to configure the viewport to the capture dimensions.
glViewport(0, 0, m_cubemapFaceSize, m_cubemapFaceSize);
glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
for (unsigned int i = 0; i < 6; ++i)
{
Expand All @@ -86,14 +67,12 @@ void PbrManager::setupCubemap(Model *cube, Shader hdrToCubemapShader)
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void PbrManager::setupIrradianceMap(Model *cube, Shader irradianceShader)
void PbrManager::setupIrradianceMap(Model *cube, Shader &irradianceShader)
{
int irradianceSize = 32;
glGenTextures(1, &irradianceMap);
glBindTexture(GL_TEXTURE_CUBE_MAP, irradianceMap);
for (unsigned int i = 0; i < 6; ++i)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB32F, irradianceSize, irradianceSize, 0, GL_RGB, GL_FLOAT, nullptr);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB32F, m_irradianceSize, m_irradianceSize, 0, GL_RGB, GL_FLOAT, nullptr);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Expand All @@ -106,7 +85,7 @@ void PbrManager::setupIrradianceMap(Model *cube, Shader irradianceShader)

glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
glBindRenderbuffer(GL_RENDERBUFFER, captureRBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, irradianceSize, irradianceSize);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, m_irradianceSize, m_irradianceSize);

irradianceShader.use();

Expand All @@ -116,7 +95,7 @@ void PbrManager::setupIrradianceMap(Model *cube, Shader irradianceShader)
glUniform1i(glGetUniformLocation(irradianceShader.id, "environmentMap"), 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, envCubemap);

glViewport(0, 0, irradianceSize, irradianceSize); // don't forget to configure the viewport to the capture dimensions.
glViewport(0, 0, m_irradianceSize, m_irradianceSize);
glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
for (unsigned int i = 0; i < 6; ++i)
{
Expand All @@ -129,14 +108,12 @@ void PbrManager::setupIrradianceMap(Model *cube, Shader irradianceShader)
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void PbrManager::setupPrefilterMap(Model *cube, Shader prefilterShader)
void PbrManager::setupPrefilterMap(Model *cube, Shader &prefilterShader)
{
int prefilterSize = 1024;
glGenTextures(1, &prefilterMap);
glBindTexture(GL_TEXTURE_CUBE_MAP, prefilterMap);
for (unsigned int i = 0; i < 6; ++i)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB32F, prefilterSize, prefilterSize, 0, GL_RGB, GL_FLOAT, nullptr);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB32F, m_prefilterSize, m_prefilterSize, 0, GL_RGB, GL_FLOAT, nullptr);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Expand All @@ -148,7 +125,7 @@ void PbrManager::setupPrefilterMap(Model *cube, Shader prefilterShader)

glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
glBindRenderbuffer(GL_RENDERBUFFER, captureRBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, prefilterSize, prefilterSize);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, m_prefilterSize, m_prefilterSize);

prefilterShader.use();

Expand All @@ -160,12 +137,13 @@ void PbrManager::setupPrefilterMap(Model *cube, Shader prefilterShader)
glBindTexture(GL_TEXTURE_CUBE_MAP, envCubemap);

glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
// TODO: variable?
unsigned int maxMipLevels = 5;
for (unsigned int mip = 0; mip < maxMipLevels; ++mip)
{
// resize framebuffer according to mip-level size.
unsigned int mipWidth = static_cast<unsigned int>(prefilterSize * std::pow(0.5, mip));
unsigned int mipHeight = static_cast<unsigned int>(prefilterSize * std::pow(0.5, mip));
unsigned int mipWidth = static_cast<unsigned int>(m_prefilterSize * std::pow(0.5, mip));
unsigned int mipHeight = static_cast<unsigned int>(m_prefilterSize * std::pow(0.5, mip));
glBindRenderbuffer(GL_RENDERBUFFER, captureRBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, mipWidth, mipHeight);
glViewport(0, 0, mipWidth, mipHeight);
Expand All @@ -184,24 +162,23 @@ void PbrManager::setupPrefilterMap(Model *cube, Shader prefilterShader)
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void PbrManager::setupBrdfLUTTexture(unsigned int quadVAO, Shader brdfShader)
// setup only once
void PbrManager::setupBrdfLUTTexture(unsigned int quadVAO, Shader &brdfShader)
{
glGenTextures(1, &brdfLUTTexture);

// pre-allocate enough memory for the LUT texture.
glBindTexture(GL_TEXTURE_2D, brdfLUTTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RG16F, 512, 512, 0, GL_RG, GL_FLOAT, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RG16F, m_brdfLutSize, m_brdfLutSize, 0, GL_RG, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
glBindRenderbuffer(GL_RENDERBUFFER, captureRBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 512, 512);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, m_brdfLutSize, m_brdfLutSize);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, brdfLUTTexture, 0);

glViewport(0, 0, 512, 512);
glViewport(0, 0, m_brdfLutSize, m_brdfLutSize);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

brdfShader.use();
Expand Down
20 changes: 10 additions & 10 deletions src/pbr_manager/pbr_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
class PbrManager
{
public:
PbrManager(std::string executablePath);
PbrManager();
~PbrManager();

std::string m_executablePath;
int m_cubemapFaceSize;
int m_irradianceSize;
int m_prefilterSize;
int m_brdfLutSize;

unsigned int m_skyboxTexture;
Texture m_environmentTexture;

unsigned int captureFBO, captureRBO;

Expand All @@ -39,13 +42,10 @@ class PbrManager
glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
};

void setupCubemap(Model *cube, Shader hdrToCubemapShader);
void setupIrradianceMap(Model *cube, Shader irradianceShader);
void setupPrefilterMap(Model *cube, Shader prefilterShader);
void setupBrdfLUTTexture(unsigned int quadVAO, Shader brdfShader);

private:
unsigned int m_cubemapFaceSize;
void setupCubemap(Model *cube, Shader &hdrToCubemapShader);
void setupIrradianceMap(Model *cube, Shader &irradianceShader);
void setupPrefilterMap(Model *cube, Shader &prefilterShader);
void setupBrdfLUTTexture(unsigned int quadVAO, Shader &brdfShader);
};

#endif /* pbr_manager_hpp */
18 changes: 9 additions & 9 deletions src/post_process/post_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
PostProcess::PostProcess(int width, int height)
: m_width(width),
m_height(height),
m_contrastBright(0.25f),
m_contrastDark(0.2f),
m_contrastBright(0.f),
m_contrastDark(0.f),
m_bloomIntensity(0.06f),
m_A(0.221f),
m_B(0.486f),
m_C(0.083f),
m_D(0.074f),
m_E(0.026f),
m_F(0.309f),
m_W(6.f),
m_exposure(2.25f),
m_B(0.18f),
m_C(0.1f),
m_D(0.003f),
m_E(0.03f),
m_F(0.3f),
m_W(11.2f),
m_exposure(1.f),
m_gamma(2.2f)
{
glGenFramebuffers(1, &m_framebufferObject);
Expand Down
Loading

0 comments on commit 7571c92

Please sign in to comment.