From f4e8a1a71e0eafd4bb247322782382e213c58b74 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Tue, 16 Nov 2021 07:37:34 +0200 Subject: [PATCH] improved normal map sampling --- pbr.frag | 10 ++++------ pbrSample.cpp | 40 +++++++++++----------------------------- texture.frag | 4 ++-- 3 files changed, 17 insertions(+), 37 deletions(-) diff --git a/pbr.frag b/pbr.frag index d1082b0..eddd5bb 100644 --- a/pbr.frag +++ b/pbr.frag @@ -49,7 +49,7 @@ float trowbridgeReitz(vec3 n, vec3 h, float a){ float schlickGGX(vec3 vec1, vec3 vec2, float k){ float dotProd = max(dot(vec1, vec2), 0); - return dotProd / (dotProd * (1 - k) + k); + return dotProd / max(dotProd * (1 - k) + k, .000001); } float geoSmith(vec3 n, vec3 v, vec3 l, float k){ @@ -72,7 +72,7 @@ void main() { if(normalMapEnabled) { mat3 normMat = mat3(tan, biTan, norm); - vec3 n = texture(textures[1].pastTexture, texCoords).rgb; + vec3 n = normalize(texture(textures[1].pastTexture, texCoords).rgb * 2 - 1); normal = normMat * n; } @@ -83,8 +83,6 @@ void main() { if(albedoMapEnabled) albedo = texture(textures[0].pastTexture, texCoords); - albedo.rgb = pow(albedo.rgb, vec3(1 / 2.2)); - float roughness = roughnessVal; if(roughnessMapEnabled) @@ -116,12 +114,12 @@ void main() { vec3 halfVec = normalize(lightDir + normal); float D = trowbridgeReitz(normal, halfVec, roughness * roughness); - float k = pow(roughness + 1, 2) / 8.0; + float k = pow(roughness * roughness + 1, 2) / 8.0; float G = geoSmith(normal, viewDir, lightDir, k); vec3 F = fresnelSchlick(viewDir, halfVec, f0); float nDotV = max(dot(viewDir, normal), 0); float nDotL = max(dot(lightDir, normal), 0); - vec3 cookTor = D * G * F / max(4 * nDotV * nDotL, .001); + vec3 cookTor = D * G * F / max(4 * nDotV * nDotL, .000001); vec3 fDiff = lambertDiffuse(albedo); vec3 kDiff = (vec3(1) - F) * (1 - metalness); diff --git a/pbrSample.cpp b/pbrSample.cpp index 4403b46..eda48ea 100644 --- a/pbrSample.cpp +++ b/pbrSample.cpp @@ -30,8 +30,8 @@ int main() { root->createSkybox(skyboxTextures); Camera *cam = root->getCamera(); - cam->setPosition(Vector3(1, 1, 0) * 5); - cam->lookAt(Vector3(-1, -1, 0).norm(), Vector3(-1, 1, 0).norm()); + cam->setPosition(Vector3(0, 1, 1) * 7); + cam->lookAt(Vector3(0, -1, -1).norm(), Vector3(0, 1, -1).norm()); Node *rootNode = root->getRootNode(); @@ -41,37 +41,22 @@ int main() { mat->addBoolUniform("normalMapEnabled", true); mat->addBoolUniform("roughnessMapEnabled", true); mat->addBoolUniform("metallnessMapEnabled", true); - 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"}; + 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->addTexUniform("textures[0]", new Texture(fr0, 1), true); mat->addTexUniform("textures[1]", new Texture(fr1, 1), true); mat->addTexUniform("textures[2]", new Texture(fr2, 1), true); mat->addTexUniform("textures[3]", new Texture(fr3, 1), true); - /* - mat->addVec4Uniform("albedoColor", Vector4(1, 0, 1, 1)); - mat->addFloatUniform("roughnessVal", .09); - mat->addFloatUniform("metalnessVal", .1); - mat->addFloatUniform("ambientOcclusion", 0); - */ - /* - mat = new Material(PATH + "texture"); - mat->addBoolUniform("texturingEnabled", true); - mat->addBoolUniform("lightingEnabled", true); - string fr[]{TEX_PATH + "bricks.jpg"}; - mat->addTexUniform("textures[0]", new Texture(fr, 1), true); - */ + mat->addTexUniform("textures[4]", new Texture(fr4, 1), true); model->setMaterial(mat); rootNode->attachChild(model); { Light *light = new Light(Light::POINT); - light->setAttenuationValues(.00001, .00001, 1); - light->setColor(Vector3(1, 1, 1)); - light->setInnerAngle(.1); - light->setOuterAngle(.1); + light->setColor(Vector3(10, 0, 0)); Node *lightNode = new Node(); Box *b = new Box(Vector3(1, 1, 1) * .1); Material *m = new Material(PATH + "texture"); @@ -82,15 +67,12 @@ int main() { lightNode->attachMesh(b); lightNode->addLight(light); rootNode->attachChild(lightNode); - lightNode->setPosition(Vector3(2, 3, 0)); + lightNode->setPosition(Vector3(2, 3, 2)); lightNode->setOrientation(Quaternion(1.57, Vector3::VEC_I)); } { Light *light = new Light(Light::POINT); - light->setAttenuationValues(.00001, .00001, 1); - light->setColor(Vector3(0, 0, 1)); - light->setInnerAngle(.1); - light->setOuterAngle(.1); + light->setColor(Vector3(0, 0, 10)); Node *lightNode = new Node(); Box *b = new Box(Vector3(1, 1, 1) * .1); Material *m = new Material(PATH + "texture"); diff --git a/texture.frag b/texture.frag index df9cc28..be11000 100755 --- a/texture.frag +++ b/texture.frag @@ -80,10 +80,10 @@ void main(){ if(lightingEnabled){ if(normalMapEnabled){ - vec3 n = vec3(texture(textures[1].pastTexture, texCoords)); + vec3 n = normalize(vec3(texture(textures[1].pastTexture, texCoords)) * 2 - 1); if(textures[1].animated) - n = mix(n, texture(textures[1].nextTexture, texCoords).rgb, textures[1].mixRatio); + n = mix(n, normalize(texture(textures[1].nextTexture, texCoords).rgb * 2 - 1), textures[1].mixRatio); normal = mat3(tan, biTan, norm) * n; }