From 532382b1f998d9a2603ee6b067d89299846c0dde Mon Sep 17 00:00:00 2001 From: Warwick Date: Tue, 26 Jul 2022 15:25:28 +0100 Subject: [PATCH] not correct way of using normal maps but gives me some preliminary info. --- data/shaders/pbrFragment.glsl | 23 ++++++++++++++++++----- data/shaders/pbrVertex.glsl | 4 +++- src/main.cpp | 1 + 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/data/shaders/pbrFragment.glsl b/data/shaders/pbrFragment.glsl index de490f5..2fef9bd 100644 --- a/data/shaders/pbrFragment.glsl +++ b/data/shaders/pbrFragment.glsl @@ -4,6 +4,7 @@ out vec4 FragColor; in vec2 ourTexCoord; in vec3 ourNormCoord; in vec3 WorldPos; +in mat4 TBN; // TODO: make temporary hard coded world/camera pos dynamic //uniform vec3 WorldPos ; @@ -70,15 +71,31 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) return ggx1 * ggx2; } +vec3 normal(){ + // load and invert normal + vec3 normal = normalize(texture(texture_normal1, ourTexCoord).rgb * 2.0 - 1.0); + + normal = (TBN * vec4(normal, 1.0)).xyz; + + //TODO: Make the normal vector match the matrix of the rest of the model by + //actually calculating the TBN + + return normal; +} + + vec3 PBR(vec3 albedo, float roughness, float metallic, float ao) { // Establish a temporary hard coded light position - vec3 lightPosition = vec3( (sin(tick / 1000.0)*2), 1 + sin(tick / 600.0)*2, 2.0); + vec3 lightPosition = vec3( 1, 1, 2); + //vec3 lightPosition = vec3( (sin(tick / 1000.0)*2), 1 + sin(tick / 600.0)*2, 2.0); //vec3 lightColor = vec3(1.0, 1.0, 1.0) - sin(tick / 90); vec3 lightColor = vec3(13.47, 11.31, 10.79); vec3 N = normalize(ourNormCoord); vec3 V = normalize(CameraPos - WorldPos); + N = (N + normal()) / 2; + //N = normal(); For seeing if normal map tracks with light. vec3 F0 = vec3(0.04); F0 = mix(F0, albedo, metallic); @@ -126,9 +143,5 @@ void main() float metallic = texture(texture_rma1, ourTexCoord).g; float ao = texture(texture_rma1, ourTexCoord).b; - // Normals - // load and invert normal - vec3 normal = normalize(texture(texture_normal1, ourTexCoord).rgb * 2.0 - 1.0); - FragColor = vec4(PBR(albedo, roughness, metallic, ao), 1.0); } diff --git a/data/shaders/pbrVertex.glsl b/data/shaders/pbrVertex.glsl index 6ddd0af..2a166cf 100644 --- a/data/shaders/pbrVertex.glsl +++ b/data/shaders/pbrVertex.glsl @@ -13,7 +13,7 @@ out vec3 ourNormCoord; out vec3 WorldPos; //Normals -out mat3 TBN; +out mat4 TBN; void main() { @@ -21,6 +21,8 @@ void main() ourNormCoord = aNormal; ourTexCoord = aTexCoord; + TBN = Model; + // Calculate position of fragment WorldPos = vec3(Model * vec4(aPos, 1.0)); }; diff --git a/src/main.cpp b/src/main.cpp index e9740c6..f043b20 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -126,6 +126,7 @@ int main(int argc, char **argv) { shader.setMat4("MVP", camera.getMVP()); shader.setVec3("CameraPos", camera.getCameraPosition()); shader.setInt("tick", SDL_GetTicks()); + boxbarrel.rotate(0.001, glm::vec3(0, 1, 0)); // Draw Meshes // cube.draw(shader);