From a333cadfcaeb485a30833343d55faf01b28a5c5f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 9 Nov 2024 23:54:19 +0300 Subject: [PATCH] fix custom model lighting --- src/graphics/render/BlocksRenderer.cpp | 40 +++++++++++++++----------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/graphics/render/BlocksRenderer.cpp b/src/graphics/render/BlocksRenderer.cpp index f2fa5c6d4..b1d8d1358 100644 --- a/src/graphics/render/BlocksRenderer.cpp +++ b/src/graphics/render/BlocksRenderer.cpp @@ -292,23 +292,29 @@ void BlocksRenderer::blockCustomModel( overflow = true; return; } - int i = 0; - for (const auto& vertex : mesh.vertices) { - auto n = - vertex.normal.x * X + vertex.normal.y * Y + vertex.normal.z * Z; - float d = glm::dot(glm::normalize(n), SUN_VECTOR); - d = 0.8f + d * 0.2f; - const auto& vcoord = vertex.coord - 0.5f; - vertexAO( - coord + vcoord.x * X + vcoord.y * Y + vcoord.z * Z, - vertex.uv.x, - vertex.uv.y, - glm::vec4(1, 1, 1, 1), - glm::vec3(1, 0, 0), - glm::vec3(0, 1, 0), - n - ); - indexBuffer[indexSize++] = indexOffset++; + for (int triangle = 0; triangle < mesh.vertices.size() / 3; triangle++) { + auto r = mesh.vertices[triangle * 3 + (triangle % 2) * 2].coord - + mesh.vertices[triangle * 3 + 1].coord; + r = glm::normalize(r); + + for (int i = 0; i < 3; i++) { + const auto& vertex = mesh.vertices[triangle * 3 + i]; + auto n = vertex.normal.x * X + vertex.normal.y * Y + + vertex.normal.z * Z; + float d = glm::dot(n, SUN_VECTOR); + d = 0.8f + d * 0.2f; + const auto& vcoord = vertex.coord - 0.5f; + vertexAO( + coord + vcoord.x * X + vcoord.y * Y + vcoord.z * Z, + vertex.uv.x, + vertex.uv.y, + glm::vec4(d, d, d, d), + glm::cross(r, n), + r, + n + ); + indexBuffer[indexSize++] = indexOffset++; + } } } }