Skip to content

Commit

Permalink
Minor fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
voldien committed Jan 27, 2025
1 parent dba9b1e commit 069322c
Show file tree
Hide file tree
Showing 41 changed files with 684 additions and 240 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/Samples/RayTracing)
ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/Samples/Sort)
ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/Samples/CirclePacking)


ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/Samples/Irradiance)

####################################
# Shader Source Files
####################################
Expand Down
1 change: 1 addition & 0 deletions Samples/Blending/Blending.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ namespace glsample {
/* Blending. */
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/* */
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->diffuse_texture);
Expand Down
10 changes: 10 additions & 0 deletions Samples/Irradiance/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FILE(GLOB IRRADIANCE_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
FILE(GLOB IRRADIANCE_HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.h)

ADD_EXECUTABLE(Irradiance ${IRRADIANCE_SOURCE_FILES} ${IRRADIANCE_HEADER_FILES} )
TARGET_LINK_LIBRARIES(Irradiance glCommon gl-sample-common-asset-importer)
ADD_DEPENDENCIES(Irradiance glCommon gl-sample-common-asset-importer)

TARGET_INCLUDE_DIRECTORIES(Irradiance PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} )

INSTALL(TARGETS Irradiance DESTINATION bin)
223 changes: 223 additions & 0 deletions Samples/Irradiance/Irradiance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
#include "GLUIComponent.h"
#include "Skybox.h"
#include <GL/glew.h>
#include <GLSample.h>
#include <GLSampleWindow.h>
#include <ImageImport.h>
#include <ShaderLoader.h>
#include <Util/CameraController.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>

namespace glsample {

/**
* @brief
*
*/
class Irradiance : public GLSampleWindow {

public:
Irradiance() : GLSampleWindow() {
this->setTitle("Irradiance");

this->skyboxSettingComponent =
std::make_shared<IrradianceSettingComponent>(*this);
this->addUIComponent(this->skyboxSettingComponent);

this->camera.setPosition(glm::vec3(25.0f));
this->camera.lookAt(glm::vec3(0.f));

this->camera.enableNavigation(true);
}

struct uniform_buffer_block {
glm::mat4 proj{};
glm::mat4 modelViewProjection{};
glm::vec4 tintColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
float exposure = 1.0f;
float gamma = 2.2;
} uniform_stage_buffer;

Skybox skybox;
MeshObject sphere;

unsigned int display_graphic_program{};

unsigned int irradiance_texture = 0;
unsigned int skybox_texture_panoramic = 0;

CameraController camera;

/* Uniform buffer. */
unsigned int uniform_buffer_binding = 0;
unsigned int uniform_buffer{};
const size_t nrUniformBuffer = 3;
size_t uniformSize = sizeof(uniform_buffer_block);

const std::string vertexSkyboxPanoramicShaderPath = "Shaders/irradiance/irradiance.vert.spv";
const std::string fragmentSkyboxPanoramicShaderPath = "Shaders/skybox/panoramic.frag.spv";

public:
class IrradianceSettingComponent : public GLUIComponent<Irradiance> {

public:
IrradianceSettingComponent(Irradiance &sample)
: GLUIComponent<Irradiance>(sample, "Irradiance Settings"), uniform(sample.uniform_stage_buffer) {}
void draw() override {
ImGui::ColorEdit4("Tint", &this->uniform.tintColor[0],
ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_Float);
ImGui::TextUnformatted("Debug");
ImGui::Checkbox("WireFrame", &this->showWireFrame);
ImGui::Image(static_cast<ImTextureID>(this->getRefSample().irradiance_texture), ImVec2(512, 256),
ImVec2(1, 1), ImVec2(0, 0));
}

bool showWireFrame = false;

private:
struct uniform_buffer_block &uniform;
};
std::shared_ptr<IrradianceSettingComponent> skyboxSettingComponent;

void Release() override {
glDeleteProgram(this->display_graphic_program);
glDeleteTextures(1, (const GLuint *)&this->skybox_texture_panoramic);
glDeleteTextures(1, (const GLuint *)&this->irradiance_texture);
}

void Initialize() override {

const std::string panoramicPath = this->getResult()["texture"].as<std::string>();

{
/* Load shader binaries. */
const std::vector<uint32_t> vertex_skybox_binary =
IOUtil::readFileData<uint32_t>(this->vertexSkyboxPanoramicShaderPath, this->getFileSystem());
const std::vector<uint32_t> fragment_skybox_binary =
IOUtil::readFileData<uint32_t>(this->fragmentSkyboxPanoramicShaderPath, this->getFileSystem());

/* */
fragcore::ShaderCompiler::CompilerConvertOption compilerOptions;
compilerOptions.target = fragcore::ShaderLanguage::GLSL;
compilerOptions.glslVersion = this->getShaderVersion();

/* Create skybox graphic pipeline program. */
this->display_graphic_program =
ShaderLoader::loadGraphicProgram(compilerOptions, &vertex_skybox_binary, &fragment_skybox_binary);
}

/* Setup graphic pipeline. */
glUseProgram(this->display_graphic_program);
unsigned int uniform_buffer_index =
glGetUniformBlockIndex(this->display_graphic_program, "UniformBufferBlock");
glUniformBlockBinding(this->display_graphic_program, uniform_buffer_index, 0);
glUniform1i(glGetUniformLocation(this->display_graphic_program, "PanoramaTexture"), 0);
glUseProgram(0);

/* Load panoramic texture. */
TextureImporter textureImporter(this->getFileSystem());
this->skybox_texture_panoramic = textureImporter.loadImage2D(panoramicPath, ColorSpace::SRGB);

ProcessData util(this->getFileSystem());
util.computeIrradiance(this->skybox_texture_panoramic, this->irradiance_texture, 256, 128);

skybox.Init(this->skybox_texture_panoramic, Skybox::loadDefaultProgram(this->getFileSystem()));

/* */
GLint minMapBufferSize = 0;
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &minMapBufferSize);
this->uniformSize = Math::align<size_t>(this->uniformSize, (size_t)minMapBufferSize);

/* Create uniform buffer. */
glGenBuffers(1, &this->uniform_buffer);
glBindBuffer(GL_UNIFORM_BUFFER, this->uniform_buffer);
glBufferData(GL_UNIFORM_BUFFER, this->uniformSize * this->nrUniformBuffer, nullptr, GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);

Common::loadSphere(this->sphere, 3, 16, 16);
}

void onResize(int width, int height) override { this->camera.setAspect((float)width / (float)height); }

void draw() override {

int width = 0, height = 0;
this->getSize(&width, &height);

/* */
glViewport(0, 0, width, height);
/* */
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/* Optional - to display wireframe. */
glPolygonMode(GL_FRONT_AND_BACK, skyboxSettingComponent->showWireFrame ? GL_LINE : GL_FILL);

{

glBindBufferRange(GL_UNIFORM_BUFFER, this->uniform_buffer_binding, this->uniform_buffer,
(this->getFrameCount() % this->nrUniformBuffer) * this->uniformSize,
this->uniformSize);

/* */
glUseProgram(this->display_graphic_program);

glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);

/* */
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->irradiance_texture);

/* Draw triangle. */
glBindVertexArray(this->sphere.vao);
glDrawElements(GL_TRIANGLES, this->sphere.nrIndicesElements, GL_UNSIGNED_INT, nullptr);
glBindVertexArray(0);
}

this->skybox.Render(this->camera);
}

void update() override {
/* */
this->camera.update(this->getTimer().deltaTime<float>());

/* */
this->uniform_stage_buffer.proj = this->camera.getProjectionMatrix();
this->uniform_stage_buffer.modelViewProjection =
(this->uniform_stage_buffer.proj * this->camera.getViewMatrix());

/* */
glBindBuffer(GL_UNIFORM_BUFFER, this->uniform_buffer);
void *uniformPointer = glMapBufferRange(
GL_UNIFORM_BUFFER, ((this->getFrameCount() + 1) % this->nrUniformBuffer) * this->uniformSize,
this->uniformSize, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT);
memcpy(uniformPointer, &this->uniform_stage_buffer, sizeof(this->uniform_stage_buffer));
glUnmapBuffer(GL_UNIFORM_BUFFER);
}
};

class IrradianceGLSample : public GLSample<Irradiance> {
public:
IrradianceGLSample() : GLSample<Irradiance>() {}

void customOptions(cxxopts::OptionAdder &options) override {
options("T,texture", "Texture Path",
cxxopts::value<std::string>()->default_value("asset/snowy_forest_4k.exr"));
}
};
} // namespace glsample

int main(int argc, const char **argv) {
try {
glsample::IrradianceGLSample sample;
sample.run(argc, argv);

} catch (const std::exception &ex) {
std::cerr << cxxexcept::getStackMessage(ex) << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
9 changes: 5 additions & 4 deletions Samples/MultiPass/multipass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ namespace glsample {
/* Setup graphic pipeline. */
glUseProgram(this->multipass_program);
int uniform_buffer_index = glGetUniformBlockIndex(this->multipass_program, "UniformBufferBlock");
glUniform1i(glGetUniformLocation(this->multipass_program, "DiffuseTexture"), 0);
glUniform1i(glGetUniformLocation(this->multipass_program, "NormalTexture"), 1);
glUniform1i(glGetUniformLocation(this->multipass_program, "AlphaMaskedTexture"), 2);
glUniform1i(glGetUniformLocation(this->multipass_program, "DiffuseTexture"), TextureType::Diffuse);
glUniform1i(glGetUniformLocation(this->multipass_program, "NormalTexture"), TextureType::Normal);
glUniform1i(glGetUniformLocation(this->multipass_program, "AlphaMaskedTexture"), TextureType::AlphaMask);
glUniformBlockBinding(this->multipass_program, uniform_buffer_index, this->uniform_buffer_binding);

//TODO: add
Expand Down Expand Up @@ -232,7 +232,7 @@ namespace glsample {
/* Optional - to display wireframe. */
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

this->scene.render();
this->scene.render(&this->camera);

this->skybox.Render(this->camera);

Expand Down Expand Up @@ -270,6 +270,7 @@ namespace glsample {
void update() override {

/* Update Camera. */
this->scene.update(this->getTimer().deltaTime<float>());
this->camera.update(this->getTimer().deltaTime<float>());

/* */
Expand Down
13 changes: 7 additions & 6 deletions Samples/Panoramic/Panoramic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace glsample {
this->setTitle("Panoramic View");

/* */
this->panoramicSettingComponent = std::make_shared<PanoramicSettingComponent>(this->uniformStageBuffer);
this->panoramicSettingComponent = std::make_shared<PanoramicSettingComponent>(*this);
this->addUIComponent(this->panoramicSettingComponent);

/* Default camera position and orientation. */
Expand Down Expand Up @@ -84,10 +84,10 @@ namespace glsample {

CameraController camera;

class PanoramicSettingComponent : public nekomimi::UIComponent {
class PanoramicSettingComponent : public GLUIComponent<Panoramic> {
public:
PanoramicSettingComponent(struct uniform_buffer_block &uniform) : uniform(uniform) {
this->setName("Panoramic Settings");
PanoramicSettingComponent(Panoramic &sample)
: GLUIComponent(sample, "Panoramic Settings"), uniform(sample.uniformStageBuffer) {
}
void draw() override {

Expand All @@ -104,7 +104,8 @@ namespace glsample {

ImGui::TextUnformatted("Debug");
ImGui::Checkbox("WireFrame", &this->showWireFrame);
ImGui::TextUnformatted("Depth Texture");

this->getRefSample().scene.renderUI();
}

bool showWireFrame = false;
Expand Down Expand Up @@ -324,7 +325,7 @@ namespace glsample {
glActiveTexture(GL_TEXTURE0 + TextureType::Irradiance);
glBindTexture(GL_TEXTURE_2D, this->irradiance_texture);

this->scene.render();
this->scene.render(&this->camera);
}

{
Expand Down
13 changes: 6 additions & 7 deletions Samples/ShadowMapping/BasicShadowMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,6 @@ namespace glsample {
const std::string fragmentShadowShaderPath = "Shaders/shadowmap/shadowmap.frag.spv";
const std::string fragmentClippingShadowShaderPath = "Shaders/shadowmap/shadowmap_alpha.frag.spv";

/* */
const std::string vertexSkyboxPanoramicShaderPath = "Shaders/skybox/skybox.vert.spv";
const std::string fragmentSkyboxPanoramicShaderPath = "Shaders/skybox/panoramic.frag.spv";

void Release() override {
glDeleteProgram(this->graphic_program);
glDeleteProgram(this->graphic_pfc_program);
Expand Down Expand Up @@ -198,8 +194,10 @@ namespace glsample {
glUseProgram(0);

glUseProgram(this->shadow_alpha_clip_program);
glUniform1i(glGetUniformLocation(this->shadow_alpha_clip_program, "DiffuseTexture"), TextureType::Diffuse);
glUniform1i(glGetUniformLocation(this->shadow_alpha_clip_program, "AlphaMaskedTexture"), TextureType::AlphaMask);
glUniform1i(glGetUniformLocation(this->shadow_alpha_clip_program, "DiffuseTexture"),
TextureType::Diffuse);
glUniform1i(glGetUniformLocation(this->shadow_alpha_clip_program, "AlphaMaskedTexture"),
TextureType::AlphaMask);
uniform_buffer_shadow_index =
glGetUniformBlockIndex(this->shadow_alpha_clip_program, "UniformBufferBlock");
glUniformBlockBinding(this->shadow_alpha_clip_program, uniform_buffer_shadow_index,
Expand All @@ -222,7 +220,8 @@ namespace glsample {
glUseProgram(this->graphic_pfc_program);
uniform_buffer_index = glGetUniformBlockIndex(this->graphic_pfc_program, "UniformBufferBlock");
glUniform1i(glGetUniformLocation(this->graphic_pfc_program, "DiffuseTexture"), TextureType::Diffuse);
glUniform1i(glGetUniformLocation(this->graphic_pfc_program, "AlphaMaskedTexture"), TextureType::AlphaMask);
glUniform1i(glGetUniformLocation(this->graphic_pfc_program, "AlphaMaskedTexture"),
TextureType::AlphaMask);
glUniform1i(glGetUniformLocation(this->graphic_pfc_program, "ShadowTexture"), shadowBinding);
glUniform1i(glGetUniformLocation(this->graphic_program, "IrradianceTexture"), TextureType::Irradiance);
glUniformBlockBinding(this->graphic_pfc_program, uniform_buffer_index,
Expand Down
Loading

0 comments on commit 069322c

Please sign in to comment.