mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-02 04:37:54 +08:00
Merge pull request #332 from alexpanter/master
Code samples for area light article.
This commit is contained in:
@@ -184,6 +184,8 @@ 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/1.area_light
|
||||
8.guest/2022/7.area_lights/2.multiple_area_lights
|
||||
)
|
||||
|
||||
configure_file(configuration/root_directory.h.in configuration/root_directory.h)
|
||||
|
||||
@@ -109,7 +109,7 @@ public:
|
||||
if (Zoom < 1.0f)
|
||||
Zoom = 1.0f;
|
||||
if (Zoom > 45.0f)
|
||||
Zoom = 45.0f;
|
||||
Zoom = 45.0f;
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -127,4 +127,4 @@ private:
|
||||
Up = glm::normalize(glm::cross(Right, Front));
|
||||
}
|
||||
};
|
||||
#endif
|
||||
#endif
|
||||
|
||||
BIN
resources/textures/concreteTexture.png
Normal file
BIN
resources/textures/concreteTexture.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 638 KiB |
178
src/8.guest/2022/7.area_lights/1.area_light/7.area_light.fs
Normal file
178
src/8.guest/2022/7.area_lights/1.area_light/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/1.area_light/7.area_light.vs
Normal file
24
src/8.guest/2022/7.area_lights/1.area_light/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;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 color;
|
||||
uniform vec3 lightColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
color = vec4(lightColor, 1.0f);
|
||||
}
|
||||
14
src/8.guest/2022/7.area_lights/1.area_light/7.light_plane.vs
Normal file
14
src/8.guest/2022/7.area_lights/1.area_light/7.light_plane.vs
Normal file
@@ -0,0 +1,14 @@
|
||||
#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 mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPosition, 1.0f);
|
||||
}
|
||||
554
src/8.guest/2022/7.area_lights/1.area_light/area_lights.cpp
Normal file
554
src/8.guest/2022/7.area_lights/1.area_light/area_lights.cpp
Normal file
@@ -0,0 +1,554 @@
|
||||
//
|
||||
// Implementing Areal Lights with Linearly Transformed Cosines.
|
||||
//
|
||||
// Inspiration:
|
||||
// https://advances.realtimerendering.com/s2016/s2016_ltc_rnd.pdf
|
||||
// https://eheitzresearch.wordpress.com/415-2/
|
||||
|
||||
// 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"
|
||||
#include "../colors.hpp" // LOOK FOR DIFFERENT COLORS!
|
||||
|
||||
// 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 do_movement(GLfloat deltaTime);
|
||||
unsigned int loadTexture(const char *path, bool gammaCorrection);
|
||||
void renderQuad();
|
||||
void renderCube();
|
||||
|
||||
// SETTINGS AND GLOBALS
|
||||
const unsigned int SCR_WIDTH = 800;
|
||||
const unsigned int SCR_HEIGHT = 600;
|
||||
const glm::vec3 LIGHT_COLOR = Color::BurlyWood; // CHANGE AREA LIGHT COLOR HERE!
|
||||
bool keys[1024]; // activated keys
|
||||
glm::vec3 areaLightTranslate;
|
||||
Shader* ltcShaderPtr;
|
||||
|
||||
// camera
|
||||
Camera camera(glm::vec3(0.0f, 1.0f, 0.5f), glm::vec3(0.0f, 1.0f, 0.0f), 180.0f, 0.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 VertexAL {
|
||||
glm::vec3 position;
|
||||
glm::vec3 normal;
|
||||
glm::vec2 texcoord;
|
||||
};
|
||||
|
||||
const GLfloat psize = 10.0f;
|
||||
VertexAL planeVertices[6] = {
|
||||
{ {-psize, 0.0f, -psize}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f} },
|
||||
{ {-psize, 0.0f, psize}, {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f} },
|
||||
{ { psize, 0.0f, psize}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f} },
|
||||
{ {-psize, 0.0f, -psize}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f} },
|
||||
{ { psize, 0.0f, psize}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f} },
|
||||
{ { psize, 0.0f, -psize}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f} }
|
||||
};
|
||||
VertexAL 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';
|
||||
ltcShaderPtr->use();
|
||||
ltcShaderPtr->setVec4("material.albedoRoughness", glm::vec4(color, roughness));
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
void incrementLightIntensity(float step)
|
||||
{
|
||||
static float intensity = 4.0f;
|
||||
intensity += step;
|
||||
intensity = glm::clamp(intensity, 0.0f, 10.0f);
|
||||
//std::cout << "intensity: " << intensity << '\n';
|
||||
ltcShaderPtr->use();
|
||||
ltcShaderPtr->setFloat("areaLight.intensity", intensity);
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
void switchTwoSided(bool doSwitch)
|
||||
{
|
||||
static bool twoSided = true;
|
||||
if (doSwitch) twoSided = !twoSided;
|
||||
//std::cout << "twoSided: " << std::boolalpha << twoSided << '\n';
|
||||
ltcShaderPtr->use();
|
||||
ltcShaderPtr->setFloat("areaLight.twoSided", twoSided);
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
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);
|
||||
|
||||
// LUT textures
|
||||
LTC_matrices mLTC;
|
||||
mLTC.mat1 = loadMTexture();
|
||||
mLTC.mat2 = loadLUTTexture();
|
||||
|
||||
// SHADERS
|
||||
Shader shaderLTC("7.area_light.vs", "7.area_light.fs");
|
||||
ltcShaderPtr = &shaderLTC;
|
||||
Shader shaderLightPlane("7.light_plane.vs", "7.light_plane.fs");
|
||||
|
||||
// TEXTURES
|
||||
unsigned int concreteTexture = loadTexture(
|
||||
FileSystem::getPath("resources/textures/concreteTexture.png").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", LIGHT_COLOR);
|
||||
shaderLTC.setInt("LTC1", 0);
|
||||
shaderLTC.setInt("LTC2", 1);
|
||||
shaderLTC.setInt("material.diffuse", 2);
|
||||
incrementRoughness(0.0f);
|
||||
incrementLightIntensity(0.0f);
|
||||
switchTwoSided(false);
|
||||
glUseProgram(0);
|
||||
|
||||
shaderLightPlane.use();
|
||||
{
|
||||
glm::mat4 model(1.0f);
|
||||
shaderLightPlane.setMat4("model", model);
|
||||
}
|
||||
shaderLightPlane.setVec3("lightColor", LIGHT_COLOR);
|
||||
glUseProgram(0);
|
||||
|
||||
// 3D OBJECTS
|
||||
configureMockupData();
|
||||
areaLightTranslate = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
|
||||
// RENDER LOOP
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
float currentFrame = static_cast<float>(glfwGetTime());
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
|
||||
glfwPollEvents();
|
||||
do_movement(deltaTime);
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
shaderLTC.use();
|
||||
glm::mat4 model(1.0f);
|
||||
glm::mat3 normalMatrix = glm::mat3(model);
|
||||
shaderLTC.setMat4("model", model);
|
||||
shaderLTC.setMat3("normalMatrix", normalMatrix);
|
||||
glm::mat4 view = camera.GetViewMatrix();
|
||||
shaderLTC.setMat4("view", view);
|
||||
glm::mat4 projection = glm::perspective(
|
||||
glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
shaderLTC.setMat4("projection", projection);
|
||||
shaderLTC.setVec3("viewPosition", camera.Position);
|
||||
shaderLTC.setVec3("areaLightTranslate", areaLightTranslate);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, mLTC.mat1);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, mLTC.mat2);
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
glBindTexture(GL_TEXTURE_2D, concreteTexture);
|
||||
renderPlane();
|
||||
glUseProgram(0);
|
||||
|
||||
shaderLightPlane.use();
|
||||
model = glm::translate(model, areaLightTranslate);
|
||||
shaderLightPlane.setMat4("model", model);
|
||||
shaderLightPlane.setMat4("view", view);
|
||||
shaderLightPlane.setMat4("projection", projection);
|
||||
renderAreaLight();
|
||||
glUseProgram(0);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &planeVAO);
|
||||
glDeleteBuffers(1, &planeVBO);
|
||||
glDeleteVertexArrays(1, &areaLightVAO);
|
||||
glDeleteBuffers(1, &areaLightVBO);
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
void do_movement(GLfloat deltaTime)
|
||||
{
|
||||
float cameraSpeed = deltaTime * 3.0f;
|
||||
|
||||
if(keys[GLFW_KEY_W]) {
|
||||
camera.ProcessKeyboard(FORWARD, cameraSpeed);
|
||||
}
|
||||
else if(keys[GLFW_KEY_S]) {
|
||||
camera.ProcessKeyboard(BACKWARD, cameraSpeed);
|
||||
}
|
||||
if(keys[GLFW_KEY_A]) {
|
||||
camera.ProcessKeyboard(LEFT, cameraSpeed);
|
||||
}
|
||||
else if(keys[GLFW_KEY_D]) {
|
||||
camera.ProcessKeyboard(RIGHT, 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_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;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 color;
|
||||
uniform vec3 lightColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
color = vec4(lightColor, 1.0f);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#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 mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPosition, 1.0f);
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
in vec3 worldPosition;
|
||||
in vec3 worldNormal;
|
||||
in vec2 texcoord;
|
||||
|
||||
struct AreaLight
|
||||
{
|
||||
float intensity;
|
||||
vec3 color;
|
||||
vec3 points[4];
|
||||
bool twoSided;
|
||||
};
|
||||
uniform AreaLight areaLights[32];
|
||||
uniform int numAreaLights;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 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));
|
||||
//Minv = Minv * transpose(mat3(N, T2, T1));
|
||||
|
||||
// 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)
|
||||
);
|
||||
|
||||
// iterate through all area lights
|
||||
for (int i = 0; i < numAreaLights; i++)
|
||||
{
|
||||
// Evaluate LTC shading
|
||||
vec3 diffuse = LTC_Evaluate(N, V, P, mat3(1), areaLights[i].points, areaLights[i].twoSided);
|
||||
vec3 specular = LTC_Evaluate(N, V, P, Minv, areaLights[i].points, areaLights[i].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;
|
||||
|
||||
// Add contribution
|
||||
result += areaLights[i].color * areaLights[i].intensity * (specular + mDiffuse * diffuse);
|
||||
//result += vec3(0.5, 0.5, 0.5);
|
||||
}
|
||||
|
||||
fragColor = vec4(ToSRGB(result), 1.0f);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,625 @@
|
||||
//
|
||||
// Implementing Areal Lights with Linearly Transformed Cosines.
|
||||
//
|
||||
// Inspiration:
|
||||
// https://advances.realtimerendering.com/s2016/s2016_ltc_rnd.pdf
|
||||
// https://eheitzresearch.wordpress.com/415-2/
|
||||
|
||||
// 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>
|
||||
#include <functional>
|
||||
#include <chrono>
|
||||
#include <random>
|
||||
|
||||
// CUSTOM
|
||||
#include "../ltc_matrix.hpp"
|
||||
#include "../colors.hpp" // LOOK FOR DIFFERENT COLORS!
|
||||
|
||||
// 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 do_movement(GLfloat deltaTime);
|
||||
unsigned int loadTexture(const char *path, bool gammaCorrection);
|
||||
void renderQuad();
|
||||
void renderCube();
|
||||
|
||||
// SETTINGS AND GLOBALS
|
||||
const unsigned int SCR_WIDTH = 800;
|
||||
const unsigned int SCR_HEIGHT = 600;
|
||||
const glm::vec3 LIGHT_COLOR = Color::BurlyWood; // CHANGE AREA LIGHT COLOR HERE!
|
||||
bool keys[1024]; // activated keys
|
||||
const int NUM_AREA_LIGHTS = 16;
|
||||
Shader* ltcShaderPtr;
|
||||
|
||||
// camera
|
||||
Camera camera(glm::vec3(-0.224556, 10.4038, -18.9259), glm::vec3(0.0f, 1.0f, 0.0f), 89.3999, -34.3001);
|
||||
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;
|
||||
|
||||
|
||||
struct VertexAL {
|
||||
glm::vec3 position;
|
||||
glm::vec3 normal;
|
||||
glm::vec2 texcoord;
|
||||
};
|
||||
|
||||
struct AreaLight {
|
||||
glm::vec3 offset;
|
||||
float yRotation;
|
||||
|
||||
glm::vec3 color;
|
||||
float intensity = 4.0f;
|
||||
bool twoSided = true;
|
||||
};
|
||||
|
||||
AreaLight areaLights[NUM_AREA_LIGHTS];
|
||||
|
||||
|
||||
//
|
||||
// 2---3-5
|
||||
// | / /|
|
||||
// | / / |
|
||||
// |/ / |
|
||||
// 1-4---6
|
||||
//
|
||||
const GLfloat psize = 10.0f;
|
||||
VertexAL planeVertices[6] = {
|
||||
{ {-psize, 0.0f, -psize}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f} },
|
||||
{ {-psize, 0.0f, psize}, {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f} },
|
||||
{ { psize, 0.0f, psize}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f} },
|
||||
{ {-psize, 0.0f, -psize}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f} },
|
||||
{ { psize, 0.0f, psize}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f} },
|
||||
{ { psize, 0.0f, -psize}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f} }
|
||||
};
|
||||
VertexAL 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 configureAreaLights()
|
||||
{
|
||||
// CONFIGURE AREA LIGHTS
|
||||
std::uniform_real_distribution<GLfloat> random_floats(0.0f, 1.0f);
|
||||
typedef std::chrono::high_resolution_clock myclock;
|
||||
unsigned seed = myclock::now().time_since_epoch().count();
|
||||
std::default_random_engine generator(seed);
|
||||
std::function<float(void)> fn =
|
||||
[&random_floats, &generator]{ return random_floats(generator); };
|
||||
for (int i = 0; i < NUM_AREA_LIGHTS; i++)
|
||||
{
|
||||
float x = fn(); x = (x > 0.5f) ? x : -x;
|
||||
float z = fn(); z = (z > 0.5f) ? z : -z;
|
||||
areaLights[i].offset = glm::vec3(x, 0.0f, z) * 8.f;
|
||||
areaLights[i].yRotation = fn() * glm::two_pi<float>();
|
||||
areaLights[i].color = glm::vec3(fn(), fn(), fn());
|
||||
// color
|
||||
// intensity
|
||||
}
|
||||
|
||||
// SEND TO GPU
|
||||
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 configurePlane()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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';
|
||||
ltcShaderPtr->use();
|
||||
ltcShaderPtr->setVec4("material.albedoRoughness", glm::vec4(color, roughness));
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
void incrementLightIntensity(float step)
|
||||
{
|
||||
static float intensity = 4.0f;
|
||||
intensity += step;
|
||||
intensity = glm::clamp(intensity, 0.0f, 10.0f);
|
||||
//std::cout << "intensity: " << intensity << '\n';
|
||||
ltcShaderPtr->use();
|
||||
ltcShaderPtr->setFloat("areaLight.intensity", intensity);
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
void switchTwoSided(bool doSwitch)
|
||||
{
|
||||
static bool twoSided = true;
|
||||
if (doSwitch) twoSided = !twoSided;
|
||||
//std::cout << "twoSided: " << std::boolalpha << twoSided << '\n';
|
||||
ltcShaderPtr->use();
|
||||
ltcShaderPtr->setFloat("areaLight.twoSided", twoSided);
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
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: Multiple 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);
|
||||
|
||||
// LUT textures
|
||||
LTC_matrices mLTC;
|
||||
mLTC.mat1 = loadMTexture();
|
||||
mLTC.mat2 = loadLUTTexture();
|
||||
|
||||
// SHADERS
|
||||
Shader shaderLTC("7.multi_area_light.vs", "7.multi_area_light.fs");
|
||||
ltcShaderPtr = &shaderLTC;
|
||||
Shader shaderLightPlane("7.light_plane.vs", "7.light_plane.fs");
|
||||
|
||||
// TEXTURES
|
||||
unsigned int concreteTexture = loadTexture(
|
||||
FileSystem::getPath("resources/textures/concreteTexture.png").c_str(), true);
|
||||
|
||||
// 3D OBJECTS
|
||||
configurePlane();
|
||||
configureAreaLights();
|
||||
|
||||
// SHADER CONFIGURATION
|
||||
shaderLTC.use();
|
||||
for (int i = 0; i < NUM_AREA_LIGHTS; i++)
|
||||
{
|
||||
glm::mat4 model(1.0f);
|
||||
model = glm::translate(model, areaLights[i].offset);
|
||||
model = glm::rotate(model, areaLights[i].yRotation, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
|
||||
glm::vec3 p0 = glm::vec3(model * glm::vec4(areaLightVertices[0].position, 1.0f));
|
||||
glm::vec3 p1 = glm::vec3(model * glm::vec4(areaLightVertices[1].position, 1.0f));
|
||||
glm::vec3 p2 = glm::vec3(model * glm::vec4(areaLightVertices[4].position, 1.0f));
|
||||
glm::vec3 p3 = glm::vec3(model * glm::vec4(areaLightVertices[5].position, 1.0f));
|
||||
|
||||
std::string str_pos = "areaLights[" + std::to_string(i) + "].points";
|
||||
std::string str_col = "areaLights[" + std::to_string(i) + "].color";
|
||||
std::string str_int = "areaLights[" + std::to_string(i) + "].intensity";
|
||||
std::string str_two = "areaLights[" + std::to_string(i) + "].twoSided";
|
||||
shaderLTC.setVec3((str_pos + "[0]").c_str(), p0);
|
||||
shaderLTC.setVec3((str_pos + "[1]").c_str(), p1);
|
||||
shaderLTC.setVec3((str_pos + "[2]").c_str(), p2);
|
||||
shaderLTC.setVec3((str_pos + "[3]").c_str(), p3);
|
||||
shaderLTC.setVec3(str_col.c_str(), areaLights[i].color);
|
||||
shaderLTC.setFloat(str_int.c_str(), 2.0f);
|
||||
shaderLTC.setInt(str_two.c_str(), 1);
|
||||
}
|
||||
shaderLTC.setInt("numAreaLights", NUM_AREA_LIGHTS);
|
||||
shaderLTC.setInt("LTC1", 0);
|
||||
shaderLTC.setInt("LTC2", 1);
|
||||
shaderLTC.setInt("material.diffuse", 2);
|
||||
incrementRoughness(0.0f);
|
||||
//incrementLightIntensity(0.0f);
|
||||
//switchTwoSided(false);
|
||||
glUseProgram(0);
|
||||
|
||||
shaderLightPlane.use();
|
||||
{
|
||||
glm::mat4 model(1.0f);
|
||||
shaderLightPlane.setMat4("model", model);
|
||||
}
|
||||
shaderLightPlane.setVec3("lightColor", LIGHT_COLOR);
|
||||
glUseProgram(0);
|
||||
|
||||
// TIME MEASUREMENT
|
||||
GLuint timeQuery;
|
||||
glGenQueries(1, &timeQuery);
|
||||
|
||||
GLuint64 totalQueryTimeNs = 0;
|
||||
GLuint64 numQueries = 0;
|
||||
|
||||
|
||||
// RENDER LOOP
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
float currentFrame = static_cast<float>(glfwGetTime());
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
|
||||
glfwPollEvents();
|
||||
do_movement(deltaTime);
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
shaderLTC.use();
|
||||
glm::mat4 model(1.0f);
|
||||
glm::mat3 normalMatrix = glm::mat3(model);
|
||||
shaderLTC.setMat4("model", model);
|
||||
shaderLTC.setMat3("normalMatrix", normalMatrix);
|
||||
glm::mat4 view = camera.GetViewMatrix();
|
||||
shaderLTC.setMat4("view", view);
|
||||
glm::mat4 projection = glm::perspective(
|
||||
glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
shaderLTC.setMat4("projection", projection);
|
||||
shaderLTC.setVec3("viewPosition", camera.Position);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, mLTC.mat1);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, mLTC.mat2);
|
||||
glActiveTexture(GL_TEXTURE2);
|
||||
glBindTexture(GL_TEXTURE_2D, concreteTexture);
|
||||
|
||||
// measure time
|
||||
glBeginQuery(GL_TIME_ELAPSED, timeQuery);
|
||||
renderPlane();
|
||||
glEndQuery(GL_TIME_ELAPSED);
|
||||
|
||||
glUseProgram(0);
|
||||
|
||||
// draw area light planes
|
||||
shaderLightPlane.use();
|
||||
shaderLightPlane.setMat4("view", view);
|
||||
shaderLightPlane.setMat4("projection", projection);
|
||||
float sinNowTime = glm::sin(currentFrame);
|
||||
for (int i = 0; i < NUM_AREA_LIGHTS; i++)
|
||||
{
|
||||
model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, areaLights[i].offset);
|
||||
model = glm::rotate(model, areaLights[i].yRotation, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
shaderLightPlane.setMat4("model", model);
|
||||
shaderLightPlane.setVec3("lightColor", areaLights[i].color);
|
||||
renderAreaLight();
|
||||
}
|
||||
glUseProgram(0);
|
||||
|
||||
// fetch timestamp
|
||||
GLuint64 elapsed = 0; // will be in nanoseconds
|
||||
glGetQueryObjectui64v(timeQuery, GL_QUERY_RESULT, &elapsed);
|
||||
numQueries++;
|
||||
totalQueryTimeNs += elapsed;
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
||||
// compute average frame time
|
||||
double measuredAverageNs = (double)totalQueryTimeNs / (double)numQueries;
|
||||
double measuredAverageMs = measuredAverageNs * 1.0e-6;
|
||||
std::cout << "Total average time(ms) = " << measuredAverageMs << '\n';
|
||||
|
||||
glDeleteQueries(1, &timeQuery);
|
||||
glDeleteVertexArrays(1, &planeVAO);
|
||||
glDeleteBuffers(1, &planeVBO);
|
||||
glDeleteVertexArrays(1, &areaLightVAO);
|
||||
glDeleteBuffers(1, &areaLightVBO);
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
void do_movement(GLfloat deltaTime)
|
||||
{
|
||||
float cameraSpeed = deltaTime * 3.0f;
|
||||
|
||||
if(keys[GLFW_KEY_W]) {
|
||||
camera.ProcessKeyboard(FORWARD, cameraSpeed);
|
||||
}
|
||||
else if(keys[GLFW_KEY_S]) {
|
||||
camera.ProcessKeyboard(BACKWARD, cameraSpeed);
|
||||
}
|
||||
if(keys[GLFW_KEY_A]) {
|
||||
camera.ProcessKeyboard(LEFT, cameraSpeed);
|
||||
}
|
||||
else if(keys[GLFW_KEY_D]) {
|
||||
camera.ProcessKeyboard(RIGHT, 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);
|
||||
// }
|
||||
}
|
||||
|
||||
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_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;
|
||||
}
|
||||
152
src/8.guest/2022/7.area_lights/colors.hpp
Normal file
152
src/8.guest/2022/7.area_lights/colors.hpp
Normal file
@@ -0,0 +1,152 @@
|
||||
#pragma once
|
||||
|
||||
// GLM
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
|
||||
// Color values found at:
|
||||
// https://www.rapidtables.com/web/color/RGB_Color.html
|
||||
|
||||
class Color
|
||||
{
|
||||
public:
|
||||
static inline glm::vec3 Maroon = glm::vec3(0.501961f, 0.0f, 0.0f);
|
||||
static inline glm::vec3 DarkRed = glm::vec3(0.545098f, 0.0f, 0.0f);
|
||||
static inline glm::vec3 Brown = glm::vec3(0.647059f, 0.164706f, 0.164706f);
|
||||
static inline glm::vec3 Firebrick = glm::vec3(0.698039f, 0.133333f, 0.133333f);
|
||||
static inline glm::vec3 Crimson = glm::vec3(0.862745f, 0.0784314f, 0.235294f);
|
||||
static inline glm::vec3 Red = glm::vec3(1.0f, 0.0f, 0.0f);
|
||||
static inline glm::vec3 Tomato = glm::vec3(1.0f, 0.388235f, 0.278431f);
|
||||
static inline glm::vec3 Coral = glm::vec3(1.0f, 0.498039f, 0.313726f);
|
||||
static inline glm::vec3 IndianRed = glm::vec3(0.803922f, 0.360784f, 0.360784f);
|
||||
static inline glm::vec3 LightCoral = glm::vec3(0.941176f, 0.501961f, 0.501961f);
|
||||
static inline glm::vec3 DarkSalmon = glm::vec3(0.913725f, 0.588235f, 0.478431f);
|
||||
static inline glm::vec3 Salmon = glm::vec3(0.980392f, 0.501961f, 0.447059f);
|
||||
static inline glm::vec3 LightSalmon = glm::vec3(1.0f, 0.627451f, 0.478431f);
|
||||
static inline glm::vec3 OrangeRed = glm::vec3(1.0f, 0.270588f, 0.0f);
|
||||
static inline glm::vec3 DarkOrange = glm::vec3(1.0f, 0.54902f, 0.0f);
|
||||
static inline glm::vec3 Orange = glm::vec3(1.0f, 0.647059f, 0.0f);
|
||||
static inline glm::vec3 Gold = glm::vec3(1.0f, 0.843137f, 0.0f);
|
||||
static inline glm::vec3 DarkGoldenRod = glm::vec3(0.721569f, 0.52549f, 0.0431373f);
|
||||
static inline glm::vec3 GoldenRod = glm::vec3(0.854902f, 0.647059f, 0.12549f);
|
||||
static inline glm::vec3 PaleGoldenRod = glm::vec3(0.933333f, 0.909804f, 0.666667f);
|
||||
static inline glm::vec3 DarkKhaki = glm::vec3(0.741176f, 0.717647f, 0.419608f);
|
||||
static inline glm::vec3 Khaki = glm::vec3(0.941176f, 0.901961f, 0.54902f);
|
||||
static inline glm::vec3 Olive = glm::vec3(0.501961f, 0.501961f, 0.0f);
|
||||
static inline glm::vec3 Yellow = glm::vec3(1.0f, 1.0f, 0.0f);
|
||||
static inline glm::vec3 YellowGreen = glm::vec3(0.603922f, 0.803922f, 0.196078f);
|
||||
static inline glm::vec3 DarkOliveGreen = glm::vec3(0.333333f, 0.419608f, 0.184314f);
|
||||
static inline glm::vec3 OliveDrab = glm::vec3(0.419608f, 0.556863f, 0.137255f);
|
||||
static inline glm::vec3 LawnGreen = glm::vec3(0.486275f, 0.988235f, 0.0f);
|
||||
static inline glm::vec3 ChartReuse = glm::vec3(0.498039f, 1.0f, 0.0f);
|
||||
static inline glm::vec3 GreenYellow = glm::vec3(0.678431f, 1.0f, 0.184314f);
|
||||
static inline glm::vec3 DarkGreen = glm::vec3(0.0f, 0.392157f, 0.0f);
|
||||
static inline glm::vec3 Green = glm::vec3(0.0f, 0.501961f, 0.0f);
|
||||
static inline glm::vec3 ForestGreen = glm::vec3(0.133333f, 0.545098f, 0.133333f);
|
||||
static inline glm::vec3 Lime = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||
static inline glm::vec3 LimeGreen = glm::vec3(0.196078f, 0.803922f, 0.196078f);
|
||||
static inline glm::vec3 LightGreen = glm::vec3(0.564706f, 0.933333f, 0.564706f);
|
||||
static inline glm::vec3 PaleGreen = glm::vec3(0.596078f, 0.984314f, 0.596078f);
|
||||
static inline glm::vec3 DarkSeaGreen = glm::vec3(0.560784f, 0.737255f, 0.560784f);
|
||||
static inline glm::vec3 MediumSpringGreen = glm::vec3(0.0f, 0.980392f, 0.603922f);
|
||||
static inline glm::vec3 SpringGreen = glm::vec3(0.0f, 1.0f, 0.498039f);
|
||||
static inline glm::vec3 SeaGreen = glm::vec3(0.180392f, 0.545098f, 0.341176f);
|
||||
static inline glm::vec3 MediumAquaMarine = glm::vec3(0.4f, 0.803922f, 0.666667f);
|
||||
static inline glm::vec3 MediumSeaGreen = glm::vec3(0.235294f, 0.701961f, 0.443137f);
|
||||
static inline glm::vec3 LightSeaGreen = glm::vec3(0.12549f, 0.698039f, 0.666667f);
|
||||
static inline glm::vec3 DarkSlateGray = glm::vec3(0.184314f, 0.309804f, 0.309804f);
|
||||
static inline glm::vec3 Teal = glm::vec3(0.0f, 0.501961f, 0.501961f);
|
||||
static inline glm::vec3 DarkCyan = glm::vec3(0.0f, 0.545098f, 0.545098f);
|
||||
static inline glm::vec3 Aqua = glm::vec3(0.0f, 1.0f, 1.0f);
|
||||
static inline glm::vec3 Cyan = glm::vec3(0.0f, 1.0f, 1.0f);
|
||||
static inline glm::vec3 LightCyan = glm::vec3(0.878431f, 1.0f, 1.0f);
|
||||
static inline glm::vec3 DarkTurquoise = glm::vec3(0.0f, 0.807843f, 0.819608f);
|
||||
static inline glm::vec3 Turquoise = glm::vec3(0.25098f, 0.878431f, 0.815686f);
|
||||
static inline glm::vec3 MediumTurquoise = glm::vec3(0.282353f, 0.819608f, 0.8f);
|
||||
static inline glm::vec3 PaleTurquoise = glm::vec3(0.686275f, 0.933333f, 0.933333f);
|
||||
static inline glm::vec3 Aquamarine = glm::vec3(0.498039f, 1.0f, 0.831373f);
|
||||
static inline glm::vec3 PowderBlue = glm::vec3(0.690196f, 0.878431f, 0.901961f);
|
||||
static inline glm::vec3 CadetBlue = glm::vec3(0.372549f, 0.619608f, 0.627451f);
|
||||
static inline glm::vec3 SteelBlue = glm::vec3(0.27451f, 0.509804f, 0.705882f);
|
||||
static inline glm::vec3 CornflowerBlue = glm::vec3(0.392157f, 0.584314f, 0.929412f);
|
||||
static inline glm::vec3 DeepSkyBlue = glm::vec3(0.0f, 0.74902f, 1.0f);
|
||||
static inline glm::vec3 DodgerBlue = glm::vec3(0.117647f, 0.564706f, 1.0f);
|
||||
static inline glm::vec3 LightBlue = glm::vec3(0.678431f, 0.847059f, 0.901961f);
|
||||
static inline glm::vec3 SkyBlue = glm::vec3(0.529412f, 0.807843f, 0.921569f);
|
||||
static inline glm::vec3 LightSkyBlue = glm::vec3(0.529412f, 0.807843f, 0.980392f);
|
||||
static inline glm::vec3 MidnightBlue = glm::vec3(0.0980392f, 0.0980392f, 0.439216f);
|
||||
static inline glm::vec3 Navy = glm::vec3(0.0f, 0.0f, 0.501961f);
|
||||
static inline glm::vec3 DarkBlue = glm::vec3(0.0f, 0.0f, 0.545098f);
|
||||
static inline glm::vec3 MediumBlue = glm::vec3(0.0f, 0.0f, 0.803922f);
|
||||
static inline glm::vec3 Blue = glm::vec3(0.0f, 0.0f, 1.0f);
|
||||
static inline glm::vec3 RoyalBlue = glm::vec3(0.254902f, 0.411765f, 0.882353f);
|
||||
static inline glm::vec3 BlueViolet = glm::vec3(0.541176f, 0.168627f, 0.886275f);
|
||||
static inline glm::vec3 Indigo = glm::vec3(0.294118f, 0.0f, 0.509804f);
|
||||
static inline glm::vec3 DarkSlateBlue = glm::vec3(0.282353f, 0.239216f, 0.545098f);
|
||||
static inline glm::vec3 SlateBlue = glm::vec3(0.415686f, 0.352941f, 0.803922f);
|
||||
static inline glm::vec3 MediumSlateBlue = glm::vec3(0.482353f, 0.407843f, 0.933333f);
|
||||
static inline glm::vec3 MediumPurple = glm::vec3(0.576471f, 0.439216f, 0.858824f);
|
||||
static inline glm::vec3 DarkMagenta = glm::vec3(0.545098f, 0.0f, 0.545098f);
|
||||
static inline glm::vec3 DarkViolet = glm::vec3(0.580392f, 0.0f, 0.827451f);
|
||||
static inline glm::vec3 DarkOrchid = glm::vec3(0.6f, 0.196078f, 0.8f);
|
||||
static inline glm::vec3 MediumOrchid = glm::vec3(0.729412f, 0.333333f, 0.827451f);
|
||||
static inline glm::vec3 Purple = glm::vec3(0.501961f, 0.0f, 0.501961f);
|
||||
static inline glm::vec3 Thistle = glm::vec3(0.847059f, 0.74902f, 0.847059f);
|
||||
static inline glm::vec3 Plum = glm::vec3(0.866667f, 0.627451f, 0.866667f);
|
||||
static inline glm::vec3 Violet = glm::vec3(0.933333f, 0.509804f, 0.933333f);
|
||||
static inline glm::vec3 Magenta = glm::vec3(1.0f, 0.0f, 1.0f);
|
||||
static inline glm::vec3 Orchid = glm::vec3(0.854902f, 0.439216f, 0.839216f);
|
||||
static inline glm::vec3 MediumVioletRed = glm::vec3(0.780392f, 0.0823529f, 0.521569f);
|
||||
static inline glm::vec3 PaleVioletRed = glm::vec3(0.858824f, 0.439216f, 0.576471f);
|
||||
static inline glm::vec3 DeepPink = glm::vec3(1.0f, 0.0784314f, 0.576471f);
|
||||
static inline glm::vec3 HotPink = glm::vec3(1.0f, 0.411765f, 0.705882f);
|
||||
static inline glm::vec3 LightPink = glm::vec3(1.0f, 0.713726f, 0.756863f);
|
||||
static inline glm::vec3 Pink = glm::vec3(1.0f, 0.752941f, 0.796078f);
|
||||
static inline glm::vec3 AntiqueWhite = glm::vec3(0.980392f, 0.921569f, 0.843137f);
|
||||
static inline glm::vec3 Beige = glm::vec3(0.960784f, 0.960784f, 0.862745f);
|
||||
static inline glm::vec3 Bisque = glm::vec3(1.0f, 0.894118f, 0.768627f);
|
||||
static inline glm::vec3 BlanchedAlmond = glm::vec3(1.0f, 0.921569f, 0.803922f);
|
||||
static inline glm::vec3 Wheat = glm::vec3(0.960784f, 0.870588f, 0.701961f);
|
||||
static inline glm::vec3 CornSilk = glm::vec3(1.0f, 0.972549f, 0.862745f);
|
||||
static inline glm::vec3 LemonChiffon = glm::vec3(1.0f, 0.980392f, 0.803922f);
|
||||
static inline glm::vec3 LightGoldenRodYellow = glm::vec3(0.980392f, 0.980392f, 0.823529f);
|
||||
static inline glm::vec3 LightYellow = glm::vec3(1.0f, 1.0f, 0.878431f);
|
||||
static inline glm::vec3 SaddleBrown = glm::vec3(0.545098f, 0.270588f, 0.0745098f);
|
||||
static inline glm::vec3 Sienna = glm::vec3(0.627451f, 0.321569f, 0.176471f);
|
||||
static inline glm::vec3 Chocolate = glm::vec3(0.823529f, 0.411765f, 0.117647f);
|
||||
static inline glm::vec3 Peru = glm::vec3(0.803922f, 0.521569f, 0.247059f);
|
||||
static inline glm::vec3 SandyBrown = glm::vec3(0.956863f, 0.643137f, 0.376471f);
|
||||
static inline glm::vec3 BurlyWood = glm::vec3(0.870588f, 0.721569f, 0.529412f);
|
||||
static inline glm::vec3 Tan = glm::vec3(0.823529f, 0.705882f, 0.54902f);
|
||||
static inline glm::vec3 RosyBrown = glm::vec3(0.737255f, 0.560784f, 0.560784f);
|
||||
static inline glm::vec3 Moccasin = glm::vec3(1.0f, 0.894118f, 0.709804f);
|
||||
static inline glm::vec3 NavajoWhite = glm::vec3(1.0f, 0.870588f, 0.678431f);
|
||||
static inline glm::vec3 PeachPuff = glm::vec3(1.0f, 0.854902f, 0.72549f);
|
||||
static inline glm::vec3 MistyRose = glm::vec3(1.0f, 0.894118f, 0.882353f);
|
||||
static inline glm::vec3 LavenderBlush = glm::vec3(1.0f, 0.941176f, 0.960784f);
|
||||
static inline glm::vec3 Linen = glm::vec3(0.980392f, 0.941176f, 0.901961f);
|
||||
static inline glm::vec3 OldLace = glm::vec3(0.992157f, 0.960784f, 0.901961f);
|
||||
static inline glm::vec3 PapayaWhip = glm::vec3(1.0f, 0.937255f, 0.835294f);
|
||||
static inline glm::vec3 SeaShell = glm::vec3(1.0f, 0.960784f, 0.933333f);
|
||||
static inline glm::vec3 MintCream = glm::vec3(0.960784f, 1.0f, 0.980392f);
|
||||
static inline glm::vec3 SlateGray = glm::vec3(0.439216f, 0.501961f, 0.564706f);
|
||||
static inline glm::vec3 LightSlateGray = glm::vec3(0.466667f, 0.533333f, 0.6f);
|
||||
static inline glm::vec3 LightSteelBlue = glm::vec3(0.690196f, 0.768627f, 0.870588f);
|
||||
static inline glm::vec3 Lavender = glm::vec3(0.901961f, 0.901961f, 0.980392f);
|
||||
static inline glm::vec3 FloralWhite = glm::vec3(1.0f, 0.980392f, 0.941176f);
|
||||
static inline glm::vec3 AliceBlue = glm::vec3(0.941176f, 0.972549f, 1.0f);
|
||||
static inline glm::vec3 GhostWhite = glm::vec3(0.972549f, 0.972549f, 1.0f);
|
||||
static inline glm::vec3 Honeydew = glm::vec3(0.941176f, 1.0f, 0.941176f);
|
||||
static inline glm::vec3 Ivory = glm::vec3(1.0f, 1.0f, 0.941176f);
|
||||
static inline glm::vec3 Azure = glm::vec3(0.941176f, 1.0f, 1.0f);
|
||||
static inline glm::vec3 Snow = glm::vec3(1.0f, 0.980392f, 0.980392f);
|
||||
static inline glm::vec3 Black = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
static inline glm::vec3 DimGrey = glm::vec3(0.411765f, 0.411765f, 0.411765f);
|
||||
static inline glm::vec3 Grey = glm::vec3(0.501961f, 0.501961f, 0.501961f);
|
||||
static inline glm::vec3 DarkGrey = glm::vec3(0.662745f, 0.662745f, 0.662745f);
|
||||
static inline glm::vec3 Silver = glm::vec3(0.752941f, 0.752941f, 0.752941f);
|
||||
static inline glm::vec3 LightGrey = glm::vec3(0.827451f, 0.827451f, 0.827451f);
|
||||
static inline glm::vec3 Gainsboro = glm::vec3(0.862745f, 0.862745f, 0.862745f);
|
||||
static inline glm::vec3 WhiteSmoke = glm::vec3(0.960784f, 0.960784f, 0.960784f);
|
||||
static inline glm::vec3 White = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
};
|
||||
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