refactored shaders

This commit is contained in:
devZoGok
2021-12-01 19:58:44 +02:00
parent 56d4a631a2
commit d8cb09f097
11 changed files with 179 additions and 174 deletions
+50 -71
View File
@@ -1,14 +1,15 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
in vec2 texCoords;
uniform uint numSamples;
const float PI = 3.14159265359;
// ----------------------------------------------------------------------------
// http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html
// efficient VanDerCorpus calculation.
float RadicalInverse_VdC(uint bits)
{
float radicalInverseVdC(uint bits) {
bits = (bits << 16u) | (bits >> 16u);
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
@@ -16,99 +17,77 @@ float RadicalInverse_VdC(uint bits)
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
return float(bits) * 2.3283064365386963e-10; // / 0x100000000
}
// ----------------------------------------------------------------------------
vec2 Hammersley(uint i, uint N)
{
return vec2(float(i)/float(N), RadicalInverse_VdC(i));
}
// ----------------------------------------------------------------------------
vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness)
{
float a = roughness*roughness;
vec2 hammersley(uint i, uint normal) {
return vec2(float(i) / float(normal), radicalInverseVdC(i));
}
vec3 importanceSampleGGX(vec2 Xi, vec3 normal, float a) {
float phi = 2.0 * PI * Xi.x;
float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a * a - 1.0) * Xi.y));
float sinTheta = sqrt(1.0 - cosTheta * cosTheta);
// from spherical coordinates to cartesian coordinates - halfway vector
vec3 H;
H.x = cos(phi) * sinTheta;
H.y = sin(phi) * sinTheta;
H.z = cosTheta;
vec3 halfVec = vec3(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);
// from tangent-space H vector to world-space sample vector
vec3 up = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
vec3 tangent = normalize(cross(up, N));
vec3 bitangent = cross(N, tangent);
vec3 up = (abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0));
vec3 tan = normalize(cross(up, normal));
vec3 biTan = cross(normal, tan);
vec3 sampleVec = tangent * H.x + bitangent * H.y + N * H.z;
vec3 sampleVec = tan * halfVec.x + biTan * halfVec.y + normal * halfVec.z;
return normalize(sampleVec);
}
// ----------------------------------------------------------------------------
float GeometrySchlickGGX(float NdotV, float roughness)
{
// note that we use a different k for IBL
float a = roughness;
float k = (a * a) / 2.0;
float nom = NdotV;
float denom = NdotV * (1.0 - k) + k;
return nom / denom;
float schlickGGX(float nDotV, float k) {
return nDotV / (nDotV * (1.0 - k) + k);
}
// ----------------------------------------------------------------------------
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
float geoSmith(vec3 normal, vec3 viewDir, vec3 lightDir, float roughness) {
float nDotV = max(dot(normal, viewDir), 0.0);
float nDotL = max(dot(normal, lightDir), 0.0);
float k = pow(roughness, 4) / 2.0;
float ggx2 = schlickGGX(nDotV, k);
float ggx1 = schlickGGX(nDotL, k);
return ggx1 * ggx2;
}
// ----------------------------------------------------------------------------
vec2 IntegrateBRDF(float NdotV, float roughness)
{
vec3 V;
V.x = sqrt(1.0 - NdotV*NdotV);
V.y = 0.0;
V.z = NdotV;
float A = 0.0;
float B = 0.0;
vec2 integrateBRDF(float nDotV, float roughness) {
vec3 viewDir = vec3(sqrt(1.0 - nDotV * nDotV), 0.0, nDotV);
vec3 N = vec3(0.0, 0.0, 1.0);
float a = 0.0;
float b = 0.0;
const uint SAMPLE_COUNT = 1024u;
for(uint i = 0u; i < SAMPLE_COUNT; ++i)
{
vec3 normal = vec3(0.0, 0.0, 1.0);
for(uint i = 0u; i < numSamples; ++i) {
// generates a sample vector that's biased towards the
// preferred alignment direction (importance sampling).
vec2 Xi = Hammersley(i, SAMPLE_COUNT);
vec3 H = ImportanceSampleGGX(Xi, N, roughness);
vec3 L = normalize(2.0 * dot(V, H) * H - V);
vec2 Xi = hammersley(i, numSamples);
vec3 halfVec = importanceSampleGGX(Xi, normal, roughness);
vec3 lightDir = normalize(2.0 * dot(viewDir, halfVec) * halfVec - viewDir);
float NdotL = max(L.z, 0.0);
float NdotH = max(H.z, 0.0);
float VdotH = max(dot(V, H), 0.0);
float nDotL = max(lightDir.z, 0.0);
float nDotH = max(halfVec.z, 0.0);
float vDotH = max(dot(viewDir, halfVec), 0.0);
if(NdotL > 0.0)
{
float G = GeometrySmith(N, V, L, roughness);
float G_Vis = (G * VdotH) / (NdotH * NdotV);
float Fc = pow(1.0 - VdotH, 5.0);
if(nDotL > 0.0) {
float G = geoSmith(normal, viewDir, lightDir, roughness);
float gVis = (G * vDotH) / (nDotH * nDotV);
float fc = pow(1.0 - vDotH, 5.0);
A += (1.0 - Fc) * G_Vis;
B += Fc * G_Vis;
a += (1.0 - fc) * gVis;
b += fc * gVis;
}
}
A /= float(SAMPLE_COUNT);
B /= float(SAMPLE_COUNT);
return vec2(A, B);
a /= float(numSamples);
b /= float(numSamples);
return vec2(a, b);
}
// ----------------------------------------------------------------------------
void main()
{
vec2 integratedBRDF = IntegrateBRDF(TexCoords.x, TexCoords.y);
void main() {
vec2 integratedBRDF = integrateBRDF(texCoords.x, texCoords.y);
FragColor = vec4(integratedBRDF, 0, 1);
}
+2 -2
View File
@@ -4,9 +4,9 @@ layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
out vec2 TexCoords;
out vec2 texCoords;
void main(){
TexCoords = aTexCoords;
texCoords = aTexCoords;
gl_Position = vec4(aPos, 1);
}
+39 -58
View File
@@ -1,30 +1,23 @@
#version 330 core
#
out vec4 FragColor;
in vec3 WorldPos;
in vec3 fragPos;
uniform samplerCube environmentMap;
uniform float roughness;
uniform float resolution;
uniform uint numSamples;
const float PI = 3.14159265359;
// ----------------------------------------------------------------------------
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH;
float nom = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return nom / denom;
float trowbridgeReitz(vec3 normal, vec3 halfVec, float alpha) {
float nDotH = max(dot(normal, halfVec), 0.0);
return (alpha * alpha) / (PI * pow(nDotH * nDotH * (alpha * alpha - 1) + 1, 2));
}
// ----------------------------------------------------------------------------
// http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html
// efficient VanDerCorpus calculation.
float RadicalInverse_VdC(uint bits)
{
float radicalInverseVdC(uint bits) {
bits = (bits << 16u) | (bits >> 16u);
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
@@ -32,75 +25,63 @@ float RadicalInverse_VdC(uint bits)
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
return float(bits) * 2.3283064365386963e-10; // / 0x100000000
}
// ----------------------------------------------------------------------------
vec2 Hammersley(uint i, uint N)
{
return vec2(float(i)/float(N), RadicalInverse_VdC(i));
}
// ----------------------------------------------------------------------------
vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness)
{
float a = roughness*roughness;
vec2 hammersley(uint i, uint normal) {
return vec2(float(i) / float(normal), radicalInverseVdC(i));
}
vec3 importanceSampleGGX(vec2 Xi, vec3 normal, float a) {
float phi = 2.0 * PI * Xi.x;
float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a*a - 1.0) * Xi.y));
float sinTheta = sqrt(1.0 - cosTheta*cosTheta);
// from spherical coordinates to cartesian coordinates - halfway vector
vec3 H;
H.x = cos(phi) * sinTheta;
H.y = sin(phi) * sinTheta;
H.z = cosTheta;
vec3 halfVec = vec3(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);
// from tangent-space H vector to world-space sample vector
vec3 up = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
vec3 tangent = normalize(cross(up, N));
vec3 bitangent = cross(N, tangent);
vec3 up = (abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0));
vec3 tan = normalize(cross(up, normal));
vec3 bitan = cross(normal, tan);
vec3 sampleVec = tangent * H.x + bitangent * H.y + N * H.z;
vec3 sampleVec = tan * halfVec.x + bitan * halfVec.y + normal * halfVec.z;
return normalize(sampleVec);
}
// ----------------------------------------------------------------------------
void main()
{
vec3 N = normalize(WorldPos);
void main() {
vec3 normal = normalize(fragPos);
// make the simplyfying assumption that V equals R equals the normal
vec3 R = N;
vec3 V = R;
vec3 reflVec = normal;
vec3 viewVec = reflVec;
const uint SAMPLE_COUNT = 1024u;
vec3 prefilteredColor = vec3(0.0);
float totalWeight = 0.0;
for(uint i = 0u; i < SAMPLE_COUNT; ++i)
{
for(uint i = 0u; i < numSamples; ++i) {
// generates a sample vector that's biased towards the preferred alignment direction (importance sampling).
vec2 Xi = Hammersley(i, SAMPLE_COUNT);
vec3 H = ImportanceSampleGGX(Xi, N, roughness);
vec3 L = normalize(2.0 * dot(V, H) * H - V);
vec2 Xi = hammersley(i, numSamples);
vec3 halfVec = importanceSampleGGX(Xi, normal, roughness * roughness);
vec3 lightVec = normalize(2.0 * dot(viewVec, halfVec) * halfVec - viewVec);
float nDotL = max(dot(normal, lightVec), 0.0);
float NdotL = max(dot(N, L), 0.0);
if(NdotL > 0.0)
{
if(nDotL > 0.0) {
// sample from the environment's mip level based on roughness/pdf
float D = DistributionGGX(N, H, roughness);
float NdotH = max(dot(N, H), 0.0);
float HdotV = max(dot(H, V), 0.0);
float pdf = D * NdotH / (4.0 * HdotV) + 0.0001;
float D = trowbridgeReitz(normal, halfVec, roughness * roughness);
float nDotH = max(dot(normal, halfVec), 0.0);
float hDotV = max(dot(halfVec, viewVec), 0.0);
float pdf = max(D * nDotH / (4.0 * hDotV), 0.0001);
float resolution = 512.0; // resolution of source cubemap (per face)
float saTexel = 4.0 * PI / (6.0 * resolution * resolution);
float saSample = 1.0 / (float(SAMPLE_COUNT) * pdf + 0.0001);
float saSample = 1.0 / max(float(numSamples) * pdf, 0.0001);
float mipLevel = roughness == 0.0 ? 0.0 : 0.5 * log2(saSample / saTexel);
float mipLevel = (roughness == 0.0 ? 0.0 : 0.5 * log2(saSample / saTexel));
prefilteredColor += textureLod(environmentMap, L, mipLevel).rgb * NdotL;
totalWeight += NdotL;
prefilteredColor += textureLod(environmentMap, lightVec, mipLevel).rgb * nDotL;
totalWeight += nDotL;
}
}
prefilteredColor = prefilteredColor / totalWeight;
prefilteredColor /= totalWeight;
FragColor = vec4(prefilteredColor, 1.0);
}
+2 -2
View File
@@ -2,12 +2,12 @@
layout (location = 0) in vec3 aPos;
out vec3 WorldPos;
out vec3 fragPos;
uniform mat4 proj;
uniform mat4 view;
void main(){
WorldPos = aPos;
fragPos = aPos;
gl_Position = proj * view * vec4(aPos, 1);
}
+12 -12
View File
@@ -1,37 +1,37 @@
#version 330 core
out vec4 FragColor;
in vec3 WorldPos;
in vec3 fragPos;
uniform samplerCube environmentMap;
uniform float sampleDelta;
const float PI = 3.14159265359;
void main()
{
vec3 N = normalize(WorldPos);
void main() {
vec3 normal = normalize(fragPos);
vec3 irradiance = vec3(0.0);
// tangent space calculation from origin point
vec3 up = vec3(0.0, 1.0, 0.0);
vec3 right = cross(up, N);
up = cross(N, right);
vec3 right = cross(up, normal);
up = cross(normal, right);
float sampleDelta = 0.5;
float nrSamples = 0.0f;
for(float phi = 0.0; phi < 2.0 * PI; phi += sampleDelta)
{
for(float theta = 0.0; theta < 0.5 * PI; theta += sampleDelta)
{
for(float phi = 0.0; phi < 2.0 * PI; phi += sampleDelta) {
for(float theta = 0.0; theta < 0.5 * PI; theta += sampleDelta) {
// spherical to cartesian (in tangent space)
vec3 tangentSample = vec3(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
// tangent space to world
vec3 sampleVec = tangentSample.x * right + tangentSample.y * up + tangentSample.z * N;
vec3 sampleVec = tangentSample.x * right + tangentSample.y * up + tangentSample.z * normal;
irradiance += texture(environmentMap, sampleVec).rgb * cos(theta) * sin(theta);
nrSamples++;
}
}
irradiance = PI * irradiance * (1.0 / float(nrSamples));
FragColor = vec4(irradiance, 1.0);
+2 -2
View File
@@ -2,12 +2,12 @@
layout (location = 0) in vec3 aPos;
out vec3 WorldPos;
out vec3 fragPos;
uniform mat4 proj;
uniform mat4 view;
void main(){
WorldPos = aPos;
fragPos = aPos;
gl_Position = proj * view * vec4(aPos, 1);
}
+4
View File
@@ -320,6 +320,7 @@ namespace vb01{
irradianceShader->use();
irradianceShader->setMat4(proj, "proj");
irradianceShader->setInt(0, "environmentMap");
irradianceShader->setFloat(irradianceSampleDelta, "sampleDelta");
prefilterMap->select();
Root *root = Root::getSingleton();
@@ -349,6 +350,8 @@ namespace vb01{
environmentShader->use();
environmentShader->setMat4(proj, "proj");
environmentShader->setInt(0, "environmentMap");
environmentShader->setFloat(preFilterMapSize, "resolution");
environmentShader->setUnsignedInt(numSamples, "numSamples");
prefilterMap->select();
@@ -392,6 +395,7 @@ namespace vb01{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *brdfIntegrationMap->getTexture(), 0);
brdfIntegrationShader->use();
brdfIntegrationShader->setUnsignedInt(numSamples, "numSamples");
Root *root = Root::getSingleton();
root->getBrdfLutPlane()->render();
+2
View File
@@ -74,6 +74,8 @@ namespace vb01{
inline glm::vec3 getUpVec(glm::vec3 dir){return (fabs(dir.y) == 1 ? glm::vec3(0, 0, -1) : glm::vec3(0, 1, 0));}
int irradianceMapSize = 32, preFilterMapSize = 512, environmentMapSize = 128, brdfMapSize = 512;
u32 numSamples = 1024;
float irradianceSampleDelta = 0.5;
Texture *prefilterMap = nullptr, *irradianceMap = nullptr, *postfilterMap = nullptr, *brdfIntegrationMap = nullptr;
Shader *environmentShader = nullptr, *irradianceShader = nullptr, *brdfIntegrationShader = nullptr;
u32 preFilterFramebuffer, preFilterRenderbuffer, irrandianceFramebuffer, irradianceRenderbuffer, postFilterFramebuffer, postFilterRenderbuffer, brdfFramebuffer, brdfRenderbuffer;
+55 -22
View File
@@ -4,6 +4,7 @@
#include "texture.h"
#include "light.h"
#include "box.h"
#include "quad.h"
#include <string>
@@ -13,12 +14,12 @@ using namespace std;
int main() {
const string PATH = "../", TEX_PATH = PATH + "samples/textures/", MODEL_PATH = PATH + "samples/models/";
string skyboxTextures[] = {
TEX_PATH + "top.jpg",
TEX_PATH + "bottom.jpg",
TEX_PATH + "left.jpg",
TEX_PATH + "right.jpg",
TEX_PATH + "front.jpg",
TEX_PATH + "back.jpg"
TEX_PATH + "bricks.jpg",
TEX_PATH + "bricks.jpg",
TEX_PATH + "bricks.jpg",
TEX_PATH + "bricks.jpg",
TEX_PATH + "bricks.jpg",
TEX_PATH + "bricks.jpg"
};
/*
@@ -30,36 +31,68 @@ int main() {
root->createSkybox(skyboxTextures);
Camera *cam = root->getCamera();
cam->setPosition(Vector3(0, 1, 1) * 7);
cam->lookAt(Vector3(0, -1, -1).norm(), Vector3(0, 1, -1).norm());
cam->setPosition(Vector3(1, 1, 1) * 2);
cam->lookAt(Vector3(-1, -1, -1).norm(), Vector3(-1, 1, -1).norm());
Node *rootNode = root->getRootNode();
Model *model = new Model(MODEL_PATH + "teapot.vb");
Model *model = new Model(MODEL_PATH + "sphere.vb");
model->setReflect(true);
Material *mat = new Material(PATH + "pbr");
mat->addBoolUniform("albedoMapEnabled", true);
mat->addBoolUniform("normalMapEnabled", true);
mat->addBoolUniform("roughnessMapEnabled", true);
mat->addBoolUniform("metallnessMapEnabled", true);
mat->addBoolUniform("ambientOcclusionMapEnabled", true);
mat->addBoolUniform("environmentMapEnabled", false);
string fr0[]{TEX_PATH + "geyser-rock1_albedo.jpg"};
string fr1[]{TEX_PATH + "geyser-rock1_normal.jpg"};
string fr2[]{TEX_PATH + "geyser-rock1_roughness.jpg"};
string fr3[]{TEX_PATH + "geyser-rock1_metallic.jpg"};
string fr4[]{TEX_PATH + "geyser-rock1_ao.jpg"};
mat->addBoolUniform("albedoMapEnabled", false);
mat->addBoolUniform("normalMapEnabled", false);
mat->addBoolUniform("roughnessMapEnabled", false);
mat->addBoolUniform("metalnessMapEnabled", false);
mat->addBoolUniform("ambientOcclusionMapEnabled", false);
mat->addBoolUniform("environmentMapEnabled", true);
mat->addVec4Uniform("albedoColor", Vector4(1, .764, .03, 1));
mat->addFloatUniform("metalnessVal", .9);
mat->addFloatUniform("roughnessVal", .1);
mat->addFloatUniform("ambientOcclusion", .1);
/*
string fr0[]{TEX_PATH + "rustediron2_albedo.jpg"};
string fr1[]{TEX_PATH + "rustediron2_normal.jpg"};
string fr2[]{TEX_PATH + "rustediron2_roughness.jpg"};
string fr3[]{TEX_PATH + "rustediron2_metallic.jpg"};
string fr4[]{TEX_PATH + "rustediron2_ao.jpg"};
mat->addTexUniform("textures[0]", new Texture(fr0, 1, false), true);
mat->addTexUniform("textures[1]", new Texture(fr1, 1, false), true);
mat->addTexUniform("textures[2]", new Texture(fr2, 1, false), true);
mat->addTexUniform("textures[3]", new Texture(fr3, 1, false), true);
mat->addTexUniform("textures[4]", new Texture(fr4, 1, false), true);
*/
model->setMaterial(mat);
rootNode->attachChild(model);
model->setPosition(Vector3(0, 0, 0));
{
Node *lightNode = new Node();
Box *b = new Box(Vector3(1, 1, 1) * 1);
Material *m = new Material(PATH + "skybox");
m->addTexUniform("tex", model->getChild(0)->getMesh(0)->getPostfilterMap(), true);
b->setMaterial(m);
lightNode->attachMesh(b);
rootNode->attachChild(lightNode);
lightNode->setPosition(Vector3(0, 0, 0));
lightNode->setOrientation(Quaternion(1.57, Vector3::VEC_I));
}
{
Node *lightNode = new Node();
Quad *b = new Quad(Vector3(2, 2, 1), false);
Material *m = new Material(PATH + "texture");
m->addBoolUniform("texturingEnabled", true);
m->addBoolUniform("lightingEnabled", false);
//m->addVec4Uniform("diffuseColor", Vector4(0, 0, 1, 1));
m->addTexUniform("textures[0]", model->getChild(0)->getMesh(0)->getBrdfIntegrationMap(), true);
b->setMaterial(m);
lightNode->attachMesh(b);
rootNode->attachChild(lightNode);
lightNode->setPosition(Vector3(-4, 1, 0));
lightNode->setOrientation(Quaternion(1.57, Vector3::VEC_I));
}
{
Light *light = new Light(Light::POINT);
light->setColor(Vector3(10, 10, 10));
light->setColor(Vector3(1, 1, 1) * 10);
Node *lightNode = new Node();
Box *b = new Box(Vector3(1, 1, 1) * .1);
Material *m = new Material(PATH + "texture");
@@ -70,7 +103,7 @@ int main() {
lightNode->attachMesh(b);
lightNode->addLight(light);
rootNode->attachChild(lightNode);
lightNode->setPosition(Vector3(2, 3, 2));
lightNode->setPosition(Vector3(0, 4, 3));
lightNode->setOrientation(Quaternion(1.57, Vector3::VEC_I));
}
{
+5
View File
@@ -224,4 +224,9 @@ namespace vb01{
int uniformLoc = glGetUniformLocation(id, var.c_str());
glUniform1i(uniformLoc, i);
}
void Shader::setUnsignedInt(u32 i, string var){
int uniformLoc = glGetUniformLocation(id, var.c_str());
glUniform1ui(uniformLoc, i);
}
}
+1
View File
@@ -28,6 +28,7 @@ namespace vb01{
void setFloat(float, std::string);
void setBool(bool, std::string);
void setInt(int, std::string);
void setUnsignedInt(u32, std::string);
void editShader(ShaderType, int, std::string);
inline bool isGeometry(){return geometry;}
private: