Normal maps now work as expected

This commit is contained in:
Grayson Riffe (Laptop) 2021-09-28 13:51:48 -05:00
parent bf2c02e240
commit 037ad47468
7 changed files with 69 additions and 23 deletions

View File

@ -52,6 +52,13 @@ void MainState::update(double deltaTime) {
camera->moveLeft(speed * 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)); light.setPosition(nf::Vec3(std::sin(circle) * 10.0, 5.0, std::cos(circle) * 10.0));
circle += 2.0 * deltaTime; circle += 2.0 * deltaTime;
@ -70,8 +77,8 @@ void MainState::render(nf::Renderer& renderer) {
renderer.render(test); renderer.render(test);
renderer.render(plane); renderer.render(plane);
renderer.render(light); renderer.render(light);
renderer.render(light2); /*renderer.render(light2);
renderer.render(light3); renderer.render(light3);*/
renderer.render(text); renderer.render(text);
renderer.render(uiTex); renderer.render(uiTex);
renderer.render(button); renderer.render(button);

View File

@ -3,6 +3,7 @@
in vec3 fragPos; in vec3 fragPos;
in vec2 texCoord; in vec2 texCoord;
in vec3 normal; in vec3 normal;
in mat3 tbn;
struct Material { struct Material {
bool hasDiffuseTex; bool hasDiffuseTex;
@ -27,10 +28,12 @@ void main() {
if (material.hasNormTex) { if (material.hasNormTex) {
normals = texture(material.normalTexture, texCoord).xyz; normals = texture(material.normalTexture, texCoord).xyz;
normals = normalize(normals * 2.0 - 1.0); normals = normals * 2.0 - 1.0;
normals = normalize(tbn * normals);
} }
else else
normals = normalize(normal); normals = normal;
if (material.hasDiffuseTex) if (material.hasDiffuseTex)
diffuse = texture(material.diffuseTexture, texCoord).rgb; diffuse = texture(material.diffuseTexture, texCoord).rgb;

View File

@ -3,6 +3,7 @@
layout(location = 0) in vec3 pos; layout(location = 0) in vec3 pos;
layout(location = 1) in vec2 texCoords; layout(location = 1) in vec2 texCoords;
layout(location = 2) in vec3 normals; layout(location = 2) in vec3 normals;
layout(location = 3) in vec3 tangent;
uniform mat4 model; uniform mat4 model;
uniform mat4 view; uniform mat4 view;
@ -11,13 +12,19 @@ uniform mat4 proj;
out vec3 fragPos; out vec3 fragPos;
out vec2 texCoord; out vec2 texCoord;
out vec3 normal; out vec3 normal;
out vec3 tang;
out mat3 tbn;
void main() { void main() {
vec4 world = model * vec4(pos, 1.0); vec4 world = model * vec4(pos, 1.0);
fragPos = world.xyz; fragPos = world.xyz;
texCoord = texCoords; texCoord = texCoords;
mat3 normalMat = transpose(inverse(mat3(model))); 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; gl_Position = proj * view * world;
} }

View File

@ -18,10 +18,7 @@ namespace nf {
m_width = Application::getApp()->getConfig().width; m_width = Application::getApp()->getConfig().width;
m_height = Application::getApp()->getConfig().height; m_height = Application::getApp()->getConfig().height;
glGenTextures(1, &m_textures[0]); glGenTextures(m_textures.size(), &m_textures[0]);
glGenTextures(1, &m_textures[1]);
glGenTextures(1, &m_textures[2]);
glGenTextures(1, &m_textures[3]);
for (unsigned int i = 0; i < m_textures.size(); i++) { for (unsigned int i = 0; i < m_textures.size(); i++) {
glBindTexture(GL_TEXTURE_2D, m_textures[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); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_width, m_height, 0, GL_RGB, GL_FLOAT, nullptr);

View File

@ -10,7 +10,7 @@
#include "Utility.h" #include "Utility.h"
namespace nf { 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 = new VertexArray();
m_vao->addBuffer(vb, vbSize); m_vao->addBuffer(vb, vbSize);
m_vao->push<float>(3); m_vao->push<float>(3);
@ -21,6 +21,9 @@ namespace nf {
m_vao->addBuffer(vn, vnSize); m_vao->addBuffer(vn, vnSize);
m_vao->push<float>(3); m_vao->push<float>(3);
m_vao->finishBufferLayout(); m_vao->finishBufferLayout();
m_vao->addBuffer(tan, tanSize);
m_vao->push<float>(3);
m_vao->finishBufferLayout();
m_ib = new IndexBuffer(ib, ibCount); m_ib = new IndexBuffer(ib, ibCount);
if (diffTex) { if (diffTex) {
m_hasDiffuse = true; m_hasDiffuse = true;
@ -103,6 +106,8 @@ namespace nf {
std::vector<float> outVN; std::vector<float> outVN;
std::vector<float> unindexedVN; std::vector<float> unindexedVN;
std::vector<unsigned int> vnIndices; std::vector<unsigned int> vnIndices;
std::vector<float> unindexedTan;
std::vector<float> outTan;
std::vector<unsigned int> outIB; std::vector<unsigned int> outIB;
unsigned int ibCount = 0; unsigned int ibCount = 0;
@ -243,6 +248,33 @@ namespace nf {
mats[curr]->unindexedVN.push_back(vnY); mats[curr]->unindexedVN.push_back(vnY);
mats[curr]->unindexedVN.push_back(vnZ); 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 { struct Vertex {
@ -282,27 +314,27 @@ namespace nf {
mats[curr]->outVN.push_back(currVertex.vnX); mats[curr]->outVN.push_back(currVertex.vnX);
mats[curr]->outVN.push_back(currVertex.vnY); mats[curr]->outVN.push_back(currVertex.vnY);
mats[curr]->outVN.push_back(currVertex.vnZ); 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; unsigned int index = (mats[curr]->outVB.size() / 3) - 1;
mats[curr]->outIB.push_back(index); mats[curr]->outIB.push_back(index);
vertexMap[currVertex] = index; vertexMap[currVertex] = index;
mats[curr]->ibCount++; mats[curr]->ibCount++;
} }
} }
}
for (auto& m : mats) { TempMaterial& curr2 = *m.second;
TempMaterial& curr = *m.second;
ATexture* diff = nullptr; ATexture* diff = nullptr;
if (curr.diffuseTextureName.size()) if (curr2.diffuseTextureName.size())
diff = model->neededTextures[curr.diffuseTextureName]; diff = model->neededTextures[curr2.diffuseTextureName];
ATexture* spec = nullptr; ATexture* spec = nullptr;
if (curr.specularTextureName.size()) if (curr2.specularTextureName.size())
spec = model->neededTextures[curr.specularTextureName]; spec = model->neededTextures[curr2.specularTextureName];
ATexture* norm = nullptr; ATexture* norm = nullptr;
if (curr.normalTextureName.size()) if (curr2.normalTextureName.size())
norm = model->neededTextures[curr.normalTextureName]; norm = model->neededTextures[curr2.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)); 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; delete m.second;
} }
} }

View File

@ -80,7 +80,7 @@ namespace nf {
m_gBuffer = new GBuffer; m_gBuffer = new GBuffer;
m_directionalDepthTexSize = 4096; m_directionalDepthTexSize = 4096;
m_pointDepthTexSize = 1024; m_pointDepthTexSize = 2048;
createShadowMaps(); createShadowMaps();
if (!m_app->isCustomWindowIcon()) { if (!m_app->isCustomWindowIcon()) {

View File

@ -10,7 +10,7 @@ namespace nf {
class Material : public Drawable { class Material : public Drawable {
public: 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); void render(Shader* shader, bool onlyDepth);