From 037ad47468bed1c1bb4eca3915f59548868d5858 Mon Sep 17 00:00:00 2001 From: "Grayson Riffe (Laptop)" Date: Tue, 28 Sep 2021 13:51:48 -0500 Subject: [PATCH] Normal maps now work as expected --- Game/src/MainState.cpp | 11 +++- .../base/shaders/gBufferFragment.shader | 7 ++- .../base/shaders/gBufferVertex.shader | 9 ++- NothinFancy/src/Renderer/GBuffer.cpp | 5 +- NothinFancy/src/Renderer/Model.cpp | 56 +++++++++++++++---- NothinFancy/src/Renderer/Renderer.cpp | 2 +- NothinFancy/src/include/Model.h | 2 +- 7 files changed, 69 insertions(+), 23 deletions(-) diff --git a/Game/src/MainState.cpp b/Game/src/MainState.cpp index 878fe6a..60d1384 100644 --- a/Game/src/MainState.cpp +++ b/Game/src/MainState.cpp @@ -52,6 +52,13 @@ void MainState::update(double deltaTime) { camera->moveLeft(speed * deltaTime); } + static double offset = 0.0; + if (app->isKeyHeld(NFI_UP)) + offset += 2.0 * deltaTime; + if (app->isKeyHeld(NFI_DOWN)) + offset -= 2.0 * deltaTime; + test.setRotation(offset * 10.0, 0.0, 0.0); + light.setPosition(nf::Vec3(std::sin(circle) * 10.0, 5.0, std::cos(circle) * 10.0)); circle += 2.0 * deltaTime; @@ -70,8 +77,8 @@ void MainState::render(nf::Renderer& renderer) { renderer.render(test); renderer.render(plane); renderer.render(light); - renderer.render(light2); - renderer.render(light3); + /*renderer.render(light2); + renderer.render(light3);*/ renderer.render(text); renderer.render(uiTex); renderer.render(button); diff --git a/NFPackCreator/AssetBuild/base/shaders/gBufferFragment.shader b/NFPackCreator/AssetBuild/base/shaders/gBufferFragment.shader index 1077c04..958fc73 100644 --- a/NFPackCreator/AssetBuild/base/shaders/gBufferFragment.shader +++ b/NFPackCreator/AssetBuild/base/shaders/gBufferFragment.shader @@ -3,6 +3,7 @@ in vec3 fragPos; in vec2 texCoord; in vec3 normal; +in mat3 tbn; struct Material { bool hasDiffuseTex; @@ -27,10 +28,12 @@ void main() { if (material.hasNormTex) { normals = texture(material.normalTexture, texCoord).xyz; - normals = normalize(normals * 2.0 - 1.0); + normals = normals * 2.0 - 1.0; + normals = normalize(tbn * normals); } else - normals = normalize(normal); + normals = normal; + if (material.hasDiffuseTex) diffuse = texture(material.diffuseTexture, texCoord).rgb; diff --git a/NFPackCreator/AssetBuild/base/shaders/gBufferVertex.shader b/NFPackCreator/AssetBuild/base/shaders/gBufferVertex.shader index 233da64..06a7c16 100644 --- a/NFPackCreator/AssetBuild/base/shaders/gBufferVertex.shader +++ b/NFPackCreator/AssetBuild/base/shaders/gBufferVertex.shader @@ -3,6 +3,7 @@ layout(location = 0) in vec3 pos; layout(location = 1) in vec2 texCoords; layout(location = 2) in vec3 normals; +layout(location = 3) in vec3 tangent; uniform mat4 model; uniform mat4 view; @@ -11,13 +12,19 @@ uniform mat4 proj; out vec3 fragPos; out vec2 texCoord; out vec3 normal; +out vec3 tang; +out mat3 tbn; void main() { vec4 world = model * vec4(pos, 1.0); fragPos = world.xyz; texCoord = texCoords; mat3 normalMat = transpose(inverse(mat3(model))); - normal = normalMat * normals; + vec3 t = normalize(normalMat * tangent); + vec3 n = normalize(normalMat * normals); + normal = n; + vec3 b = cross(n, t); + tbn = mat3(t, b, n); gl_Position = proj * view * world; } \ No newline at end of file diff --git a/NothinFancy/src/Renderer/GBuffer.cpp b/NothinFancy/src/Renderer/GBuffer.cpp index 109e50d..75d0c8c 100644 --- a/NothinFancy/src/Renderer/GBuffer.cpp +++ b/NothinFancy/src/Renderer/GBuffer.cpp @@ -18,10 +18,7 @@ namespace nf { m_width = Application::getApp()->getConfig().width; m_height = Application::getApp()->getConfig().height; - glGenTextures(1, &m_textures[0]); - glGenTextures(1, &m_textures[1]); - glGenTextures(1, &m_textures[2]); - glGenTextures(1, &m_textures[3]); + glGenTextures(m_textures.size(), &m_textures[0]); for (unsigned int i = 0; i < m_textures.size(); i++) { glBindTexture(GL_TEXTURE_2D, m_textures[i]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_width, m_height, 0, GL_RGB, GL_FLOAT, nullptr); diff --git a/NothinFancy/src/Renderer/Model.cpp b/NothinFancy/src/Renderer/Model.cpp index 1b8bbfd..76f200e 100644 --- a/NothinFancy/src/Renderer/Model.cpp +++ b/NothinFancy/src/Renderer/Model.cpp @@ -10,7 +10,7 @@ #include "Utility.h" namespace nf { - Material::Material(const void* vb, const size_t vbSize, const void* tc, const size_t tcSize, const void* vn, const size_t vnSize, const void* ib, const unsigned int ibCount, ATexture* diffTex, Vec3& diffColor, ATexture* specTex, float shininess, ATexture* normalTex) { + Material::Material(const void* vb, const size_t vbSize, const void* tc, const size_t tcSize, const void* vn, const size_t vnSize, const void* tan, const size_t tanSize, const void* ib, const unsigned int ibCount, ATexture* diffTex, Vec3& diffColor, ATexture* specTex, float shininess, ATexture* normalTex) { m_vao = new VertexArray(); m_vao->addBuffer(vb, vbSize); m_vao->push(3); @@ -21,6 +21,9 @@ namespace nf { m_vao->addBuffer(vn, vnSize); m_vao->push(3); m_vao->finishBufferLayout(); + m_vao->addBuffer(tan, tanSize); + m_vao->push(3); + m_vao->finishBufferLayout(); m_ib = new IndexBuffer(ib, ibCount); if (diffTex) { m_hasDiffuse = true; @@ -103,6 +106,8 @@ namespace nf { std::vector outVN; std::vector unindexedVN; std::vector vnIndices; + std::vector unindexedTan; + std::vector outTan; std::vector outIB; unsigned int ibCount = 0; @@ -243,6 +248,33 @@ namespace nf { mats[curr]->unindexedVN.push_back(vnY); mats[curr]->unindexedVN.push_back(vnZ); } + + for (unsigned int i = 0; i * 9 < mats[curr]->unindexedVB.size(); i++) { + glm::vec3 pos1(mats[curr]->unindexedVB[i * 9], mats[curr]->unindexedVB[i * 9 + 1], mats[curr]->unindexedVB[i * 9 + 2]); + glm::vec2 uv1(mats[curr]->unindexedTC[i * 6], mats[curr]->unindexedTC[i * 6 + 1]); + glm::vec3 pos2(mats[curr]->unindexedVB[i * 9 + 3], mats[curr]->unindexedVB[i * 9 + 4], mats[curr]->unindexedVB[i * 9 + 5]); + glm::vec2 uv2(mats[curr]->unindexedTC[i * 6 + 2], mats[curr]->unindexedTC[i * 6 + 3]); + glm::vec3 pos3(mats[curr]->unindexedVB[i * 9 + 6], mats[curr]->unindexedVB[i * 9 + 7], mats[curr]->unindexedVB[i * 9 + 8]); + glm::vec2 uv3(mats[curr]->unindexedTC[i * 6 + 4], mats[curr]->unindexedTC[i * 6 + 5]); + + glm::vec3 edge1 = pos2 - pos1; + glm::vec3 edge2 = pos3 - pos1; + glm::vec2 delta1 = uv2 - uv1; + glm::vec2 delta2 = uv3 - uv1; + float f = 1.0f / (delta1.x * delta2.y - delta2.x * delta1.y); + float x = f * (delta2.y * edge1.x - delta1.y * edge2.x); + float y = f * (delta2.y * edge1.y - delta1.y * edge2.y); + float z = f * (delta2.y * edge1.z - delta1.y * edge2.z); + mats[curr]->unindexedTan.push_back(x); + mats[curr]->unindexedTan.push_back(y); + mats[curr]->unindexedTan.push_back(z); + mats[curr]->unindexedTan.push_back(x); + mats[curr]->unindexedTan.push_back(y); + mats[curr]->unindexedTan.push_back(z); + mats[curr]->unindexedTan.push_back(x); + mats[curr]->unindexedTan.push_back(y); + mats[curr]->unindexedTan.push_back(z); + } } struct Vertex { @@ -282,27 +314,27 @@ namespace nf { mats[curr]->outVN.push_back(currVertex.vnX); mats[curr]->outVN.push_back(currVertex.vnY); mats[curr]->outVN.push_back(currVertex.vnZ); + mats[curr]->outTan.push_back(mats[curr]->unindexedTan[(i * 3)]); + mats[curr]->outTan.push_back(mats[curr]->unindexedTan[(i * 3 + 1)]); + mats[curr]->outTan.push_back(mats[curr]->unindexedTan[(i * 3 + 2)]); unsigned int index = (mats[curr]->outVB.size() / 3) - 1; mats[curr]->outIB.push_back(index); vertexMap[currVertex] = index; mats[curr]->ibCount++; } } - } - for (auto& m : mats) { - TempMaterial& curr = *m.second; + TempMaterial& curr2 = *m.second; ATexture* diff = nullptr; - if (curr.diffuseTextureName.size()) - diff = model->neededTextures[curr.diffuseTextureName]; + if (curr2.diffuseTextureName.size()) + diff = model->neededTextures[curr2.diffuseTextureName]; ATexture* spec = nullptr; - if (curr.specularTextureName.size()) - spec = model->neededTextures[curr.specularTextureName]; + if (curr2.specularTextureName.size()) + spec = model->neededTextures[curr2.specularTextureName]; ATexture* norm = nullptr; - if (curr.normalTextureName.size()) - norm = model->neededTextures[curr.normalTextureName]; - m_materials.push_back(new Material(&curr.outVB[0], curr.outVB.size() * sizeof(float), &curr.outTC[0], curr.outTC.size() * sizeof(float), &curr.outVN[0], curr.outVN.size() * sizeof(float), &curr.outIB[0], curr.ibCount, diff, curr.diffuseColor, spec, curr.shininess, norm)); - + if (curr2.normalTextureName.size()) + norm = model->neededTextures[curr2.normalTextureName]; + m_materials.push_back(new Material(&curr2.outVB[0], curr2.outVB.size() * sizeof(float), &curr2.outTC[0], curr2.outTC.size() * sizeof(float), &curr2.outVN[0], curr2.outVN.size() * sizeof(float), &curr2.outTan[0], curr2.outTan.size() * sizeof(float), &curr2.outIB[0], curr2.ibCount, diff, curr2.diffuseColor, spec, curr2.shininess, norm)); delete m.second; } } diff --git a/NothinFancy/src/Renderer/Renderer.cpp b/NothinFancy/src/Renderer/Renderer.cpp index 1d70110..28abc5a 100644 --- a/NothinFancy/src/Renderer/Renderer.cpp +++ b/NothinFancy/src/Renderer/Renderer.cpp @@ -80,7 +80,7 @@ namespace nf { m_gBuffer = new GBuffer; m_directionalDepthTexSize = 4096; - m_pointDepthTexSize = 1024; + m_pointDepthTexSize = 2048; createShadowMaps(); if (!m_app->isCustomWindowIcon()) { diff --git a/NothinFancy/src/include/Model.h b/NothinFancy/src/include/Model.h index ed88a42..b0e5d66 100644 --- a/NothinFancy/src/include/Model.h +++ b/NothinFancy/src/include/Model.h @@ -10,7 +10,7 @@ namespace nf { class Material : public Drawable { public: - Material(const void* vb, const size_t vbSize, const void* tc, const size_t tcSize, const void* vn, const size_t vnSize, const void* ib, const unsigned int ibCount, ATexture* diffTex, Vec3& diffColor, ATexture* specTex, float shininess, ATexture* normalTex); + Material(const void* vb, const size_t vbSize, const void* tc, const size_t tcSize, const void* vn, const size_t vnSize, const void* tan, const size_t tanSize, const void* ib, const unsigned int ibCount, ATexture* diffTex, Vec3& diffColor, ATexture* specTex, float shininess, ATexture* normalTex); void render(Shader* shader, bool onlyDepth);