Add exposure HDR lighting
This commit is contained in:
@@ -13,6 +13,7 @@ add_executable(Fall-Fever
|
||||
Light.cpp
|
||||
World.cpp
|
||||
Framebuffer.cpp
|
||||
Shadows.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include "ShaderProgram.h"
|
||||
|
||||
class Camera {
|
||||
|
||||
@@ -17,6 +18,11 @@ public:
|
||||
void translate(glm::vec3 translateVector);
|
||||
|
||||
void setPosition(glm::vec3 position) { this->position = position; }
|
||||
void setExposure(ShaderProgram *shaderProgram) {
|
||||
shaderProgram->setUniform("exposure", exposure);
|
||||
this->exposure = exposure;
|
||||
}
|
||||
|
||||
void lookAtTarget(glm::vec3 target);
|
||||
void lookForward();
|
||||
|
||||
@@ -43,5 +49,6 @@ private:
|
||||
float speed = 2.0f;
|
||||
|
||||
float fov;
|
||||
float exposure = 1.0f;
|
||||
|
||||
};
|
||||
|
||||
@@ -81,6 +81,8 @@ void Controller::run() {
|
||||
ShaderProgram skyboxProgram("res/shaders/skybox.vert", "res/shaders/skybox.frag");
|
||||
ShaderProgram postProcessingProgram("res/shaders/postprocessing.vert", "res/shaders/postprocessing.frag");
|
||||
|
||||
updateExposure(&postProcessingProgram);
|
||||
|
||||
Model model_backpack("res/models/backpack.ffo");
|
||||
//Model model_plant("res/models/plant.ffo");
|
||||
//Model model_container("res/models/container.ffo");
|
||||
@@ -126,15 +128,15 @@ void Controller::run() {
|
||||
// ...
|
||||
static bool rotateLightSource = 0;
|
||||
if(rotateLightSource) {
|
||||
float radius = 25.0;
|
||||
float radius = 4.0;
|
||||
glm::vec3 newPos = glm::vec3(-cos(glfwGetTime()*0.5), 0.5f, sin(glfwGetTime()*0.5)) * radius;
|
||||
world.getEntities()->operator[](1).setPosition(newPos);
|
||||
}
|
||||
static glm::vec3 lightColor = glm::vec3(1.f);
|
||||
world.updatePointLight(0, true, world.getEntities()->operator[](1).getPosition(), lightColor);
|
||||
world.updateDirectionalLight(true, glm::vec3(-0.2f, -1.0f, -0.3f), lightColor * 0.001f);
|
||||
static glm::vec3 lightColor = glm::vec3(1.f); static float intensity = 1.0f;
|
||||
world.updatePointLight(0, true, world.getEntities()->operator[](1).getPosition(), lightColor * intensity);
|
||||
world.updateDirectionalLight(true, glm::vec3(-0.2f, -1.0f, -0.3f), lightColor * 0.1f);
|
||||
lightProgram.bind();
|
||||
lightProgram.setUniform("v_lightColor", lightColor);
|
||||
lightProgram.setUniform("v_lightColor", lightColor * 100.0f);
|
||||
lightProgram.unbind();
|
||||
|
||||
// Render and buffer swap
|
||||
@@ -146,6 +148,10 @@ void Controller::run() {
|
||||
camera->lookForward();
|
||||
camera->updateVPM();
|
||||
|
||||
// Calc shadows
|
||||
// ...
|
||||
|
||||
glViewport(0, 0, gameWindow->getWindowWidth(), gameWindow->getWindowHeight());
|
||||
skybox.draw(camera->getView(), camera->getProj());
|
||||
world.draw(camera->getViewProj(), camera->getPosition());
|
||||
pp_framebuffer->unbind();
|
||||
@@ -153,7 +159,7 @@ void Controller::run() {
|
||||
pp_framebuffer->render();
|
||||
|
||||
#ifdef _DEBUG
|
||||
renderImGui(world.getEntities(), &world.getPointLights()[0], &lightColor, &rotateLightSource);
|
||||
renderImGui(world.getEntities(), &world.getPointLights()[0], &lightColor, &rotateLightSource, &postProcessingProgram, &intensity);
|
||||
#endif
|
||||
|
||||
glfwSwapBuffers(gameWindow->getGLFWwindow());
|
||||
@@ -209,8 +215,14 @@ void Controller::updateWindowSize(ShaderProgram *pp_program) {
|
||||
pp_framebuffer = new Framebuffer(gameWindow->getWindowWidth(), gameWindow->getWindowHeight(), pp_program);
|
||||
}
|
||||
|
||||
void Controller::updateExposure(ShaderProgram *shaderProgram) {
|
||||
shaderProgram->bind();
|
||||
shaderProgram->setUniform("u_exposure", exposure);
|
||||
shaderProgram->unbind();
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
void Controller::renderImGui(std::vector<Entity> *entites, PointLight *pointLight, glm::vec3 *lightColor, bool *rotateLightSource) {
|
||||
void Controller::renderImGui(std::vector<Entity> *entites, PointLight *pointLight, glm::vec3 *lightColor, bool *rotateLightSource, ShaderProgram *postProcessingProgram, float *intensity) {
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
@@ -218,7 +230,7 @@ void Controller::renderImGui(std::vector<Entity> *entites, PointLight *pointLigh
|
||||
|
||||
// render your GUI
|
||||
ImGui::Begin("Debug Utils");
|
||||
ImGui::Text("Dragon");
|
||||
ImGui::Text("Object");
|
||||
static float rotation = 0.0;
|
||||
ImGui::SliderFloat("Rotation", &rotation, 0, 2 * M_PI);
|
||||
static float translation[] = {0.0f, 0.0f, 0.0f};
|
||||
@@ -232,21 +244,24 @@ void Controller::renderImGui(std::vector<Entity> *entites, PointLight *pointLigh
|
||||
|
||||
// color picker
|
||||
ImGui::Text("\nLight Source");
|
||||
ImGui::Text("Attenuation");
|
||||
static float K_c = 1.0f, K_l = 0.09f, K_q = 0.062f;
|
||||
ImGui::SliderFloat("Constant Term", &K_c, 0, 1.0f*5);
|
||||
ImGui::SliderFloat("Linear Term", &K_l, 0, 0.09f*5);
|
||||
ImGui::SliderFloat("Quadratic Term", &K_q, 0, 0.032f*10);
|
||||
static float K_q = 0.062f;
|
||||
ImGui::SliderFloat("Attenuation Parameter", &K_q, 0, 1.5f);
|
||||
|
||||
pointLight->setParameters(K_c, K_l, K_q);
|
||||
updateExposure(postProcessingProgram);
|
||||
pointLight->setParameters(K_q);
|
||||
|
||||
static float color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
|
||||
ImGui::Text("Color");
|
||||
|
||||
ImGui::SliderFloat("Intensity", intensity, 0, 50.f);
|
||||
|
||||
ImGui::ColorEdit3("Color", color);
|
||||
lightColor->x = color[0];
|
||||
lightColor->y = color[1];
|
||||
lightColor->z = color[2];
|
||||
|
||||
ImGui::Text("\nMiscellaneous");
|
||||
ImGui::SliderFloat("Exposure", &exposure, 0, 5.0f);
|
||||
|
||||
ImGui::Checkbox("Rotate Lightsource", rotateLightSource);
|
||||
|
||||
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
|
||||
|
||||
@@ -27,8 +27,9 @@ private:
|
||||
void limit_framerate();
|
||||
|
||||
void updateWindowSize(ShaderProgram *pp_program);
|
||||
void updateExposure(ShaderProgram *shaderProgram);
|
||||
|
||||
void renderImGui(std::vector<Entity> *entites, PointLight *pointLight, glm::vec3 *lightColor, bool *rotateLightSource);
|
||||
void renderImGui(std::vector<Entity> *entites, PointLight *pointLight, glm::vec3 *lightColor, bool *rotateLightSource, ShaderProgram *postProcessingProgram, float *intensity);
|
||||
|
||||
Window *gameWindow;
|
||||
EventHandler *gameEventHandler;
|
||||
@@ -41,4 +42,6 @@ private:
|
||||
|
||||
bool wireFrameMode = 0;
|
||||
|
||||
float exposure = 1.0f;
|
||||
|
||||
};
|
||||
|
||||
@@ -41,17 +41,56 @@ void Framebuffer::unbind() {
|
||||
}
|
||||
|
||||
void Framebuffer::render() {
|
||||
shaderProgram->bind();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, getTextureId());
|
||||
GLint location = glGetUniformLocation(shaderProgram->getShaderProgramId(), "u_texture");
|
||||
glUniform1i(location, 0);
|
||||
|
||||
// A VAO is necessary although no data is stored in it
|
||||
GLuint temp_vao;
|
||||
glGenVertexArrays(1, &temp_vao);
|
||||
glBindVertexArray(temp_vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glBindVertexArray(0);
|
||||
shaderProgram->unbind();
|
||||
// Disable wireframe mode
|
||||
GLint wireframe;
|
||||
glGetIntegerv(GL_POLYGON_MODE, &wireframe);
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
|
||||
shaderProgram->bind();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, getTextureId());
|
||||
GLint location = glGetUniformLocation(shaderProgram->getShaderProgramId(), "u_texture");
|
||||
glUniform1i(location, 0);
|
||||
|
||||
// A VAO is necessary although no data is stored in it
|
||||
GLuint temp_vao;
|
||||
glGenVertexArrays(1, &temp_vao);
|
||||
glBindVertexArray(temp_vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glBindVertexArray(0);
|
||||
|
||||
glPolygonMode(GL_FRONT_AND_BACK, wireframe);
|
||||
shaderProgram->unbind();
|
||||
}
|
||||
|
||||
|
||||
|
||||
DepthMap::DepthMap(int resolution) {
|
||||
|
||||
glGenFramebuffers(1, &depthMapFBO);
|
||||
|
||||
unsigned int depthMap;
|
||||
glGenTextures(1, &depthMap);
|
||||
glBindTexture(GL_TEXTURE_2D, depthMap);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, resolution, resolution, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
|
||||
glDrawBuffer(GL_NONE);
|
||||
glReadBuffer(GL_NONE);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
|
||||
}
|
||||
|
||||
void DepthMap::bind() {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
|
||||
}
|
||||
|
||||
void DepthMap::unbind() {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
@@ -26,4 +26,20 @@ private:
|
||||
|
||||
ShaderProgram *shaderProgram;
|
||||
|
||||
};
|
||||
|
||||
|
||||
class DepthMap {
|
||||
|
||||
public:
|
||||
|
||||
DepthMap(int resolution);
|
||||
|
||||
void bind();
|
||||
void unbind();
|
||||
|
||||
private:
|
||||
|
||||
GLuint depthMapFBO;
|
||||
|
||||
};
|
||||
@@ -18,8 +18,6 @@ void PointLight::update() {
|
||||
shaderProgram->setUniform((_getStructMemberName() + "ambient").c_str(), ambientColor);
|
||||
shaderProgram->setUniform((_getStructMemberName() + "diffuse").c_str(), diffuseColor);
|
||||
shaderProgram->setUniform((_getStructMemberName() + "specular").c_str(), specularColor);
|
||||
shaderProgram->setUniform((_getStructMemberName() + "K_c").c_str(), K_c);
|
||||
shaderProgram->setUniform((_getStructMemberName() + "K_l").c_str(), K_l);
|
||||
shaderProgram->setUniform((_getStructMemberName() + "K_q").c_str(), K_q);
|
||||
|
||||
shaderProgram->unbind();
|
||||
|
||||
@@ -60,7 +60,7 @@ public:
|
||||
update();
|
||||
}
|
||||
|
||||
void setParameters(float K_c, float K_l, float K_q) { this->K_c = K_c; this->K_l = K_l; this->K_q = K_q; }
|
||||
void setParameters(float K_q) { this->K_q = K_q; }
|
||||
|
||||
void setId(unsigned int id) { lightId = id; }
|
||||
|
||||
@@ -72,8 +72,7 @@ private:
|
||||
unsigned int lightId;
|
||||
|
||||
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
float K_c = 1.0f;
|
||||
float K_l = 0.09f;
|
||||
|
||||
float K_q = 0.032f;
|
||||
|
||||
};
|
||||
|
||||
24
src/Shadows.cpp
Normal file
24
src/Shadows.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "Shadows.h"
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
|
||||
Shadows::Shadows()
|
||||
: depthMap(SHADOW_RES) {
|
||||
|
||||
}
|
||||
|
||||
void Shadows::calculate() {
|
||||
|
||||
glViewport(0, 0, SHADOW_RES, SHADOW_RES);
|
||||
depthMap.bind();
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
// ConfigureShaderAndMatrices();
|
||||
float near_plane = 1.0f, far_plane = 7.5f;
|
||||
glm::mat4 lightProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane, far_plane);
|
||||
glm::mat4 lightView = glm::lookAt(glm::vec3(-0.2f, -1.0f, -0.3f), glm::vec3( 0.0f, 0.0f, 0.0f), glm::vec3( 0.0f, 1.0f, 0.0f));
|
||||
glm::mat4 lightSpaceMatrix = lightProjection * lightView;
|
||||
// RenderScene();
|
||||
depthMap.unbind();
|
||||
|
||||
}
|
||||
19
src/Shadows.h
Normal file
19
src/Shadows.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "Framebuffer.h"
|
||||
|
||||
class Shadows {
|
||||
|
||||
public:
|
||||
|
||||
Shadows();
|
||||
|
||||
void calculate();
|
||||
|
||||
private:
|
||||
|
||||
DepthMap depthMap;
|
||||
|
||||
const int SHADOW_RES = 1024;
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user