mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-02 04:37:54 +08:00
Initial commit for area lights guest article
This commit is contained in:
@@ -184,6 +184,7 @@ set(GUEST_ARTICLES
|
||||
8.guest/2021/4.dsa
|
||||
8.guest/2022/5.computeshader_helloworld
|
||||
8.guest/2022/6.physically_based_bloom
|
||||
8.guest/2022/7.area_lights
|
||||
)
|
||||
|
||||
configure_file(configuration/root_directory.h.in configuration/root_directory.h)
|
||||
|
||||
BIN
resources/textures/concreteTexture.jpg
Normal file
BIN
resources/textures/concreteTexture.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 101 KiB |
178
src/8.guest/2022/7.area_lights/7.area_light.fs
Normal file
178
src/8.guest/2022/7.area_lights/7.area_light.fs
Normal file
@@ -0,0 +1,178 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
in vec3 worldPosition;
|
||||
in vec3 worldNormal;
|
||||
in vec2 texcoord;
|
||||
|
||||
struct Light
|
||||
{
|
||||
float intensity;
|
||||
vec3 color;
|
||||
vec3 points[4];
|
||||
bool twoSided;
|
||||
};
|
||||
uniform Light areaLight;
|
||||
uniform vec3 areaLightTranslate;
|
||||
|
||||
struct Material
|
||||
{
|
||||
sampler2D diffuse;
|
||||
vec4 albedoRoughness; // (x,y,z) = color, w = roughness
|
||||
};
|
||||
uniform Material material;
|
||||
|
||||
uniform vec3 viewPosition;
|
||||
uniform sampler2D LTC1; // for inverse M
|
||||
uniform sampler2D LTC2; // GGX norm, fresnel, 0(unused), sphere
|
||||
|
||||
const float LUT_SIZE = 64.0; // ltc_texture size
|
||||
const float LUT_SCALE = (LUT_SIZE - 1.0)/LUT_SIZE;
|
||||
const float LUT_BIAS = 0.5/LUT_SIZE;
|
||||
|
||||
|
||||
// Vector form without project to the plane (dot with the normal)
|
||||
// Use for proxy sphere clipping
|
||||
vec3 IntegrateEdgeVec(vec3 v1, vec3 v2)
|
||||
{
|
||||
// Using built-in acos() function will result flaws
|
||||
// Using fitting result for calculating acos()
|
||||
float x = dot(v1, v2);
|
||||
float y = abs(x);
|
||||
|
||||
float a = 0.8543985 + (0.4965155 + 0.0145206*y)*y;
|
||||
float b = 3.4175940 + (4.1616724 + y)*y;
|
||||
float v = a / b;
|
||||
|
||||
float theta_sintheta = (x > 0.0) ? v : 0.5*inversesqrt(max(1.0 - x*x, 1e-7)) - v;
|
||||
|
||||
return cross(v1, v2)*theta_sintheta;
|
||||
}
|
||||
|
||||
float IntegrateEdge(vec3 v1, vec3 v2)
|
||||
{
|
||||
return IntegrateEdgeVec(v1, v2).z;
|
||||
}
|
||||
|
||||
// P is fragPos in world space (LTC distribution)
|
||||
vec3 LTC_Evaluate(vec3 N, vec3 V, vec3 P, mat3 Minv, vec3 points[4], bool twoSided)
|
||||
{
|
||||
// construct orthonormal basis around N
|
||||
vec3 T1, T2;
|
||||
T1 = normalize(V - N * dot(V, N));
|
||||
T2 = cross(N, T1);
|
||||
|
||||
// rotate area light in (T1, T2, N) basis
|
||||
Minv = Minv * transpose(mat3(T1, T2, N));
|
||||
|
||||
// polygon (allocate 4 vertices for clipping)
|
||||
vec3 L[4];
|
||||
// transform polygon from LTC back to origin Do (cosine weighted)
|
||||
L[0] = Minv * (points[0] - P);
|
||||
L[1] = Minv * (points[1] - P);
|
||||
L[2] = Minv * (points[2] - P);
|
||||
L[3] = Minv * (points[3] - P);
|
||||
|
||||
// use tabulated horizon-clipped sphere
|
||||
// check if the shading point is behind the light
|
||||
vec3 dir = points[0] - P; // LTC space
|
||||
vec3 lightNormal = cross(points[1] - points[0], points[3] - points[0]);
|
||||
bool behind = (dot(dir, lightNormal) < 0.0);
|
||||
|
||||
// cos weighted space
|
||||
L[0] = normalize(L[0]);
|
||||
L[1] = normalize(L[1]);
|
||||
L[2] = normalize(L[2]);
|
||||
L[3] = normalize(L[3]);
|
||||
|
||||
// integrate
|
||||
vec3 vsum = vec3(0.0);
|
||||
vsum += IntegrateEdgeVec(L[0], L[1]);
|
||||
vsum += IntegrateEdgeVec(L[1], L[2]);
|
||||
vsum += IntegrateEdgeVec(L[2], L[3]);
|
||||
vsum += IntegrateEdgeVec(L[3], L[0]);
|
||||
|
||||
// form factor of the polygon in direction vsum
|
||||
float len = length(vsum);
|
||||
|
||||
float z = vsum.z/len;
|
||||
if (behind)
|
||||
z = -z;
|
||||
|
||||
vec2 uv = vec2(z*0.5f + 0.5f, len); // range [0, 1]
|
||||
uv = uv*LUT_SCALE + LUT_BIAS;
|
||||
|
||||
// Fetch the form factor for horizon clipping
|
||||
float scale = texture(LTC2, uv).w;
|
||||
|
||||
float sum = len*scale;
|
||||
if (!behind && !twoSided)
|
||||
sum = 0.0;
|
||||
|
||||
// Outgoing radiance (solid angle) for the entire polygon
|
||||
vec3 Lo_i = vec3(sum, sum, sum);
|
||||
return Lo_i;
|
||||
}
|
||||
|
||||
// PBR-maps for roughness (and metallic) are usually stored in non-linear
|
||||
// color space (sRGB), so we use these functions to convert into linear RGB.
|
||||
vec3 PowVec3(vec3 v, float p)
|
||||
{
|
||||
return vec3(pow(v.x, p), pow(v.y, p), pow(v.z, p));
|
||||
}
|
||||
|
||||
const float gamma = 2.2;
|
||||
vec3 ToLinear(vec3 v) { return PowVec3(v, gamma); }
|
||||
vec3 ToSRGB(vec3 v) { return PowVec3(v, 1.0/gamma); }
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
// gamma correction
|
||||
vec3 mDiffuse = texture(material.diffuse, texcoord).xyz;// * vec3(0.7f, 0.8f, 0.96f);
|
||||
vec3 mSpecular = ToLinear(vec3(0.23f, 0.23f, 0.23f)); // mDiffuse
|
||||
|
||||
vec3 result = vec3(0.0f);
|
||||
|
||||
vec3 N = normalize(worldNormal);
|
||||
vec3 V = normalize(viewPosition - worldPosition);
|
||||
vec3 P = worldPosition;
|
||||
float dotNV = clamp(dot(N, V), 0.0f, 1.0f);
|
||||
|
||||
// use roughness and sqrt(1-cos_theta) to sample M_texture
|
||||
vec2 uv = vec2(material.albedoRoughness.w, sqrt(1.0f - dotNV));
|
||||
uv = uv*LUT_SCALE + LUT_BIAS;
|
||||
|
||||
// get 4 parameters for inverse_M
|
||||
vec4 t1 = texture(LTC1, uv);
|
||||
|
||||
// Get 2 parameters for Fresnel calculation
|
||||
vec4 t2 = texture(LTC2, uv);
|
||||
|
||||
mat3 Minv = mat3(
|
||||
vec3(t1.x, 0, t1.y),
|
||||
vec3( 0, 1, 0),
|
||||
vec3(t1.z, 0, t1.w)
|
||||
);
|
||||
|
||||
// translate light source for testing
|
||||
vec3 translatedPoints[4];
|
||||
translatedPoints[0] = areaLight.points[0] + areaLightTranslate;
|
||||
translatedPoints[1] = areaLight.points[1] + areaLightTranslate;
|
||||
translatedPoints[2] = areaLight.points[2] + areaLightTranslate;
|
||||
translatedPoints[3] = areaLight.points[3] + areaLightTranslate;
|
||||
|
||||
// Evaluate LTC shading
|
||||
vec3 diffuse = LTC_Evaluate(N, V, P, mat3(1), translatedPoints, areaLight.twoSided);
|
||||
vec3 specular = LTC_Evaluate(N, V, P, Minv, translatedPoints, areaLight.twoSided);
|
||||
|
||||
// GGX BRDF shadowing and Fresnel
|
||||
// t2.x: shadowedF90 ??? (F90 normally it should be 1.0)
|
||||
// t2.y: Smith function for Geometric Attenuation Term, it is dot(V or L, H).
|
||||
specular *= mSpecular*t2.x + (1.0f - mSpecular) * t2.y;
|
||||
|
||||
result = areaLight.color * areaLight.intensity * (specular + mDiffuse * diffuse);
|
||||
|
||||
fragColor = vec4(ToSRGB(result), 1.0f);
|
||||
}
|
||||
24
src/8.guest/2022/7.area_lights/7.area_light.vs
Normal file
24
src/8.guest/2022/7.area_lights/7.area_light.vs
Normal file
@@ -0,0 +1,24 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPosition;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
layout (location = 2) in vec2 aTexcoord;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat3 normalMatrix;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
out vec3 worldPosition;
|
||||
out vec3 worldNormal;
|
||||
out vec2 texcoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 worldpos = model * vec4(aPosition, 1.0f);
|
||||
worldPosition = worldpos.xyz;
|
||||
worldNormal = normalMatrix * aNormal;
|
||||
texcoord = aTexcoord;
|
||||
|
||||
gl_Position = projection * view * worldpos;
|
||||
}
|
||||
666
src/8.guest/2022/7.area_lights/area_lights.cpp
Normal file
666
src/8.guest/2022/7.area_lights/area_lights.cpp
Normal file
@@ -0,0 +1,666 @@
|
||||
// GLAD, GLFW, STB-IMAGE
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <stb_image.h>
|
||||
|
||||
// GLM
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
// LEARNOPENGL
|
||||
#include <learnopengl/filesystem.h>
|
||||
#include <learnopengl/shader.h>
|
||||
#include <learnopengl/camera.h>
|
||||
#include <learnopengl/model.h>
|
||||
|
||||
// STANDARD
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
// CUSTOM
|
||||
#include "ltc_matrix.hpp"
|
||||
|
||||
// FUNCTION PROTOTYPES
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
|
||||
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
||||
void processInput(GLFWwindow *window);
|
||||
unsigned int loadTexture(const char *path, bool gammaCorrection);
|
||||
void renderQuad();
|
||||
void renderCube();
|
||||
|
||||
// settings
|
||||
const unsigned int SCR_WIDTH = 800;
|
||||
const unsigned int SCR_HEIGHT = 600;
|
||||
bool bloom = true;
|
||||
float exposure = 1.0f;
|
||||
int programChoice = 1;
|
||||
float bloomFilterRadius = 0.005f;
|
||||
|
||||
// camera
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 5.0f));
|
||||
float lastX = (float)SCR_WIDTH / 2.0;
|
||||
float lastY = (float)SCR_HEIGHT / 2.0;
|
||||
bool firstMouse = true;
|
||||
|
||||
// timing
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
|
||||
|
||||
//
|
||||
// 2---3-5
|
||||
// | / /|
|
||||
// | / / |
|
||||
// |/ / |
|
||||
// 1-4---6
|
||||
//
|
||||
struct Vertex {
|
||||
glm::vec3 position;
|
||||
glm::vec3 normal;
|
||||
glm::vec2 texcoord;
|
||||
};
|
||||
|
||||
const GLfloat size = 10.0f;
|
||||
Vertex planeVertices[6] = {
|
||||
{ {-size, 0.0f, -size}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f} },
|
||||
{ {-size, 0.0f, size}, {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f} },
|
||||
{ { size, 0.0f, size}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f} },
|
||||
{ {-size, 0.0f, -size}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f} },
|
||||
{ { size, 0.0f, size}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f} },
|
||||
{ { size, 0.0f, -size}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f} }
|
||||
};
|
||||
Vertex areaLightVertices[6] = {
|
||||
{ {-8.0f, 2.4f, -1.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f} }, // 0 1 5 4
|
||||
{ {-8.0f, 2.4f, 1.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f} },
|
||||
{ {-8.0f, 0.4f, 1.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 1.0f} },
|
||||
{ {-8.0f, 2.4f, -1.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f} },
|
||||
{ {-8.0f, 0.4f, 1.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 1.0f} },
|
||||
{ {-8.0f, 0.4f, -1.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f} }
|
||||
};
|
||||
|
||||
GLuint planeVBO, planeVAO;
|
||||
GLuint areaLightVBO, areaLightVAO;
|
||||
|
||||
void configureMockupData()
|
||||
{
|
||||
// PLANE
|
||||
glGenVertexArrays(1, &planeVAO);
|
||||
glGenBuffers(1, &planeVBO);
|
||||
|
||||
glBindVertexArray(planeVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, planeVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), planeVertices, GL_STATIC_DRAW);
|
||||
|
||||
// position
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat),
|
||||
(GLvoid*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// normal
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat),
|
||||
(GLvoid*)(3 * sizeof(GLfloat)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// texcoord
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat),
|
||||
(GLvoid*)(6 * sizeof(GLfloat)));
|
||||
glEnableVertexAttribArray(2);
|
||||
glBindVertexArray(0);
|
||||
|
||||
// AREA LIGHT
|
||||
glGenVertexArrays(1, &areaLightVAO);
|
||||
glBindVertexArray(areaLightVAO);
|
||||
|
||||
glGenBuffers(1, &areaLightVBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, areaLightVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(areaLightVertices), areaLightVertices, GL_STATIC_DRAW);
|
||||
|
||||
// position
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat),
|
||||
(GLvoid*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// normal
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat),
|
||||
(GLvoid*)(3 * sizeof(GLfloat)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// texcoord
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat),
|
||||
(GLvoid*)(6 * sizeof(GLfloat)));
|
||||
glEnableVertexAttribArray(2);
|
||||
glBindVertexArray(0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void renderPlane()
|
||||
{
|
||||
glBindVertexArray(planeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void renderAreaLight()
|
||||
{
|
||||
glBindVertexArray(areaLightVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct LTC_matrices {
|
||||
GLuint mat1;
|
||||
GLuint mat2;
|
||||
};
|
||||
|
||||
GLuint loadMTexture()
|
||||
{
|
||||
GLuint texture = 0;
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 64, 64,
|
||||
0, GL_RGBA, GL_FLOAT, LTC1);
|
||||
|
||||
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_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
return texture;
|
||||
}
|
||||
|
||||
GLuint loadLUTTexture()
|
||||
{
|
||||
GLuint texture = 0;
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 64, 64,
|
||||
0, GL_RGBA, GL_FLOAT, LTC2);
|
||||
|
||||
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_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
return texture;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void incrementRoughness(float step)
|
||||
{
|
||||
static glm::vec3 color = Color::SlateGray;
|
||||
static float roughness = 0.5f;
|
||||
roughness += step;
|
||||
roughness = glm::clamp(roughness, 0.0f, 1.0f);
|
||||
//std::cout << "roughness: " << roughness << '\n';
|
||||
lightingShader->Activate();
|
||||
lightingShader->SetUniformVec4("material.albedoRoughness", glm::vec4(color, roughness));
|
||||
lightingShader->Deactivate();
|
||||
}
|
||||
|
||||
void incrementLightIntensity(float step)
|
||||
{
|
||||
static float intensity = 4.0f;
|
||||
intensity += step;
|
||||
intensity = glm::clamp(intensity, 0.0f, 10.0f);
|
||||
//std::cout << "intensity: " << intensity << '\n';
|
||||
lightingShader->Activate();
|
||||
lightingShader->SetUniformFloat("areaLight.intensity", intensity);
|
||||
lightingShader->Deactivate();
|
||||
}
|
||||
|
||||
void switchTwoSided(bool doSwitch)
|
||||
{
|
||||
static bool twoSided = true;
|
||||
if (doSwitch) twoSided = !twoSided;
|
||||
//std::cout << "twoSided: " << std::boolalpha << twoSided << '\n';
|
||||
lightingShader->Activate();
|
||||
lightingShader->SetUniformBool("areaLight.twoSided", twoSided);
|
||||
lightingShader->Deactivate();
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// glfw: initialize and configure
|
||||
// ------------------------------
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
#ifdef __APPLE__
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
#endif
|
||||
|
||||
// glfw window creation
|
||||
// --------------------
|
||||
GLFWwindow* window = glfwCreateWindow(
|
||||
SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL: Area Lights", NULL, NULL);
|
||||
if (window == NULL)
|
||||
{
|
||||
std::cout << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
|
||||
// tell GLFW to capture our mouse
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
// glad: load all OpenGL function pointers
|
||||
// ---------------------------------------
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
{
|
||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// configure global opengl state
|
||||
// -----------------------------
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
// build and compile shaders
|
||||
// -------------------------
|
||||
Shader shaderLTC("7.area_light.vs", "7.area_light.fs");
|
||||
// Shader shaderLight("6.bloom.vs", "6.light_box.fs");
|
||||
// Shader shaderBlur("6.old_blur.vs", "6.old_blur.fs");
|
||||
// Shader shaderBloomFinal("6.bloom_final.vs", "6.bloom_final.fs");
|
||||
|
||||
// load textures
|
||||
// -------------
|
||||
unsigned int concreteTexture = loadTexture(FileSystem::getPath("resources/textures/concreteTexture.jpg").c_str(), true);
|
||||
|
||||
// shader configuration
|
||||
// --------------------
|
||||
shaderLTC.use();
|
||||
shaderLTC.setVec3("areaLight.points[0]", areaLightVertices[0].position);
|
||||
shaderLTC.setVec3("areaLight.points[1]", areaLightVertices[1].position);
|
||||
shaderLTC.setVec3("areaLight.points[2]", areaLightVertices[4].position);
|
||||
shaderLTC.setVec3("areaLight.points[3]", areaLightVertices[5].position);
|
||||
shaderLTC.setVec3("areaLight.color", Color::White);
|
||||
shaderLTC.setInt("LTC1", 0);
|
||||
shaderLTC.setInt("LTC2", 1);
|
||||
shaderLTC.setInt("material.diffuse", 2);
|
||||
incrementRoughness(0.0f);
|
||||
incrementLightIntensity(0.0f);
|
||||
switchTwoSided(false);
|
||||
glUseProgram(0);
|
||||
|
||||
|
||||
// shader.setInt("diffuseTexture", 0);
|
||||
// shaderBlur.use();
|
||||
// shaderBlur.setInt("image", 0);
|
||||
// shaderBloomFinal.use();
|
||||
// shaderBloomFinal.setInt("scene", 0);
|
||||
// shaderBloomFinal.setInt("bloomBlur", 1);
|
||||
|
||||
// render loop
|
||||
// -----------
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
// per-frame time logic
|
||||
// --------------------
|
||||
float currentFrame = static_cast<float>(glfwGetTime());
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
|
||||
// input
|
||||
// -----
|
||||
processInput(window);
|
||||
|
||||
// render
|
||||
// ------
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// 1. render scene into floating point framebuffer
|
||||
// -----------------------------------------------
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, hdrFBO);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
glm::mat4 view = camera.GetViewMatrix();
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
shader.use();
|
||||
shader.setMat4("projection", projection);
|
||||
shader.setMat4("view", view);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, woodTexture);
|
||||
// set lighting uniforms
|
||||
for (unsigned int i = 0; i < lightPositions.size(); i++)
|
||||
{
|
||||
shader.setVec3("lights[" + std::to_string(i) + "].Position", lightPositions[i]);
|
||||
shader.setVec3("lights[" + std::to_string(i) + "].Color", lightColors[i]);
|
||||
}
|
||||
shader.setVec3("viewPos", camera.Position);
|
||||
// create one large cube that acts as the floor
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, glm::vec3(0.0f, -1.0f, 0.0));
|
||||
model = glm::scale(model, glm::vec3(12.5f, 0.5f, 12.5f));
|
||||
shader.setMat4("model", model);
|
||||
renderCube();
|
||||
// then create multiple cubes as the scenery
|
||||
glBindTexture(GL_TEXTURE_2D, containerTexture);
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, glm::vec3(0.0f, 1.5f, 0.0));
|
||||
model = glm::scale(model, glm::vec3(0.5f));
|
||||
shader.setMat4("model", model);
|
||||
renderCube();
|
||||
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, glm::vec3(2.0f, 0.0f, 1.0));
|
||||
model = glm::scale(model, glm::vec3(0.5f));
|
||||
shader.setMat4("model", model);
|
||||
renderCube();
|
||||
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, glm::vec3(-1.0f, -1.0f, 2.0));
|
||||
model = glm::rotate(model, glm::radians(60.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0)));
|
||||
shader.setMat4("model", model);
|
||||
renderCube();
|
||||
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, glm::vec3(0.0f, 2.7f, 4.0));
|
||||
model = glm::rotate(model, glm::radians(23.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0)));
|
||||
model = glm::scale(model, glm::vec3(1.25));
|
||||
shader.setMat4("model", model);
|
||||
renderCube();
|
||||
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, glm::vec3(-2.0f, 1.0f, -3.0));
|
||||
model = glm::rotate(model, glm::radians(124.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0)));
|
||||
shader.setMat4("model", model);
|
||||
renderCube();
|
||||
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, glm::vec3(-3.0f, 0.0f, 0.0));
|
||||
model = glm::scale(model, glm::vec3(0.5f));
|
||||
shader.setMat4("model", model);
|
||||
renderCube();
|
||||
|
||||
// finally show all the light sources as bright cubes
|
||||
shaderLight.use();
|
||||
shaderLight.setMat4("projection", projection);
|
||||
shaderLight.setMat4("view", view);
|
||||
|
||||
for (unsigned int i = 0; i < lightPositions.size(); i++)
|
||||
{
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, glm::vec3(lightPositions[i]));
|
||||
model = glm::scale(model, glm::vec3(0.25f));
|
||||
shaderLight.setMat4("model", model);
|
||||
shaderLight.setVec3("lightColor", lightColors[i]);
|
||||
renderCube();
|
||||
}
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
|
||||
if (programChoice < 1 || programChoice > 3) { programChoice = 1; }
|
||||
bloom = (programChoice == 1) ? false : true;
|
||||
bool horizontal = true;
|
||||
|
||||
// 2.A) bloom is disabled
|
||||
// ----------------------
|
||||
if (programChoice == 1)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// 2.B) blur bright fragments with two-pass Gaussian Blur
|
||||
// ------------------------------------------------------
|
||||
else if (programChoice == 2)
|
||||
{
|
||||
bool first_iteration = true;
|
||||
unsigned int amount = 10;
|
||||
shaderBlur.use();
|
||||
for (unsigned int i = 0; i < amount; i++)
|
||||
{
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[horizontal]);
|
||||
shaderBlur.setInt("horizontal", horizontal);
|
||||
glBindTexture(GL_TEXTURE_2D, first_iteration ? colorBuffers[1] : pingpongColorbuffers[!horizontal]); // bind texture of other framebuffer (or scene if first iteration)
|
||||
renderQuad();
|
||||
horizontal = !horizontal;
|
||||
if (first_iteration)
|
||||
first_iteration = false;
|
||||
}
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
// 2.C) use unthresholded bloom with progressive downsample/upsampling
|
||||
// -------------------------------------------------------------------
|
||||
else if (programChoice == 3)
|
||||
{
|
||||
bloomRenderer.RenderBloomTexture(colorBuffers[1], bloomFilterRadius);
|
||||
}
|
||||
|
||||
// 3. now render floating point color buffer to 2D quad and tonemap HDR colors to default framebuffer's (clamped) color range
|
||||
// --------------------------------------------------------------------------------------------------------------------------
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
shaderBloomFinal.use();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, colorBuffers[0]);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
if (programChoice == 1) {
|
||||
glBindTexture(GL_TEXTURE_2D, 0); // trick to bind invalid texture "0", we don't care either way!
|
||||
}
|
||||
if (programChoice == 2) {
|
||||
glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[!horizontal]);
|
||||
}
|
||||
else if (programChoice == 3) {
|
||||
glBindTexture(GL_TEXTURE_2D, bloomRenderer.BloomTexture());
|
||||
}
|
||||
shaderBloomFinal.setInt("programChoice", programChoice);
|
||||
shaderBloomFinal.setFloat("exposure", exposure);
|
||||
renderQuad();
|
||||
|
||||
//std::cout << "bloom: " << (bloom ? "on" : "off") << "| exposure: " << exposure << std::endl;
|
||||
|
||||
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
||||
// -------------------------------------------------------------------------------
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
bloomRenderer.Destroy();
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
void do_movement(GLfloat deltaTime)
|
||||
{
|
||||
GLfloat cameraSpeed = 10.0f * deltaTime;
|
||||
|
||||
if(keys[GLFW_KEY_W]) {
|
||||
cam->MoveForwards(cameraSpeed);
|
||||
}
|
||||
else if(keys[GLFW_KEY_S]) {
|
||||
cam->MoveBackwards(cameraSpeed);
|
||||
}
|
||||
if(keys[GLFW_KEY_A]) {
|
||||
cam->StrafeLeft(cameraSpeed);
|
||||
}
|
||||
else if(keys[GLFW_KEY_D]) {
|
||||
cam->StrafeRight(cameraSpeed);
|
||||
}
|
||||
|
||||
if (keys[GLFW_KEY_Z]) {
|
||||
if (keys[GLFW_KEY_LEFT_SHIFT]) cam->MoveDown(cameraSpeed);
|
||||
else cam->MoveUp(cameraSpeed);
|
||||
}
|
||||
|
||||
if (keys[GLFW_KEY_R]) {
|
||||
if (keys[GLFW_KEY_LEFT_SHIFT]) incrementRoughness(0.01f);
|
||||
else incrementRoughness(-0.01f);
|
||||
}
|
||||
|
||||
if (keys[GLFW_KEY_I]) {
|
||||
if (keys[GLFW_KEY_LEFT_SHIFT]) incrementLightIntensity(0.025f);
|
||||
else incrementLightIntensity(-0.025f);
|
||||
}
|
||||
|
||||
if (keys[GLFW_KEY_LEFT]) {
|
||||
areaLightTranslate.z += 0.01f;
|
||||
}
|
||||
if (keys[GLFW_KEY_RIGHT]) {
|
||||
areaLightTranslate.z -= 0.01f;
|
||||
}
|
||||
if (keys[GLFW_KEY_UP]) {
|
||||
areaLightTranslate.y += 0.01f;
|
||||
}
|
||||
if (keys[GLFW_KEY_DOWN]) {
|
||||
areaLightTranslate.y -= 0.01f;
|
||||
}
|
||||
}
|
||||
|
||||
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
|
||||
{
|
||||
static unsigned short wireframe = 0;
|
||||
|
||||
if(action == GLFW_PRESS)
|
||||
{
|
||||
switch(key)
|
||||
{
|
||||
case GLFW_KEY_ESCAPE:
|
||||
glfwSetWindowShouldClose(window, GL_TRUE);
|
||||
return;
|
||||
case GLFW_KEY_L:
|
||||
screen_lock = !screen_lock;
|
||||
break;
|
||||
case GLFW_KEY_C:
|
||||
lightingShader = new Shaders::ShaderWrapper(shadername, Shaders::SHADERS_VF);
|
||||
recompileShader = true;
|
||||
break;
|
||||
case GLFW_KEY_B:
|
||||
switchTwoSided(true);
|
||||
break;
|
||||
default:
|
||||
keys[key] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(action == GLFW_RELEASE)
|
||||
{
|
||||
if(key == GLFW_KEY_SPACE) {
|
||||
switch(wireframe)
|
||||
{
|
||||
case 0:
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
wireframe = 1;
|
||||
break;
|
||||
default:
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
wireframe = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
keys[key] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
// make sure the viewport matches the new window dimensions; note that width and
|
||||
// height will be significantly larger than specified on retina displays.
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
// glfw: whenever the mouse moves, this callback is called
|
||||
// -------------------------------------------------------
|
||||
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
|
||||
{
|
||||
float xpos = static_cast<float>(xposIn);
|
||||
float ypos = static_cast<float>(yposIn);
|
||||
if (firstMouse)
|
||||
{
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
firstMouse = false;
|
||||
}
|
||||
|
||||
float xoffset = xpos - lastX;
|
||||
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
|
||||
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
camera.ProcessMouseMovement(xoffset, yoffset);
|
||||
}
|
||||
|
||||
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
|
||||
// ----------------------------------------------------------------------
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
camera.ProcessMouseScroll(static_cast<float>(yoffset));
|
||||
}
|
||||
|
||||
// utility function for loading a 2D texture from file
|
||||
// ---------------------------------------------------
|
||||
unsigned int loadTexture(char const * path, bool gammaCorrection)
|
||||
{
|
||||
unsigned int textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
|
||||
int width, height, nrComponents;
|
||||
unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);
|
||||
if (data)
|
||||
{
|
||||
GLenum internalFormat;
|
||||
GLenum dataFormat;
|
||||
if (nrComponents == 1)
|
||||
{
|
||||
internalFormat = dataFormat = GL_RED;
|
||||
}
|
||||
else if (nrComponents == 3)
|
||||
{
|
||||
internalFormat = gammaCorrection ? GL_SRGB : GL_RGB;
|
||||
dataFormat = GL_RGB;
|
||||
}
|
||||
else if (nrComponents == 4)
|
||||
{
|
||||
internalFormat = gammaCorrection ? GL_SRGB_ALPHA : GL_RGBA;
|
||||
dataFormat = GL_RGBA;
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, dataFormat, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
stbi_image_free(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Texture failed to load at path: " << path << std::endl;
|
||||
stbi_image_free(data);
|
||||
}
|
||||
|
||||
return textureID;
|
||||
}
|
||||
8203
src/8.guest/2022/7.area_lights/ltc_matrix.hpp
Normal file
8203
src/8.guest/2022/7.area_lights/ltc_matrix.hpp
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user