Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
igagis committed Nov 11, 2024
1 parent 8632ab7 commit 2f8c60d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/ruis/render/scene/gltf_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,10 @@ utki::shared_ref<ruis::render::vertex_array> gltf_loader::create_vao_with_tangen
std::vector<ruis::vec3> bitangents; // texture y-axis

tangents.resize(num_vertices);
std::fill(tangents.begin(), tangents.end(), ruis::vec3(0, 0, 0));

bitangents.resize(num_vertices);
std::fill(bitangents.begin(), bitangents.end(), ruis::vec3(0, 0, 0));

// Calculate the vertex tangents and bitangents.
for (uint32_t i = 0; i < num_triangles; ++i) {
Expand All @@ -651,7 +654,7 @@ utki::shared_ref<ruis::render::vertex_array> gltf_loader::create_vao_with_tangen

// Calculate the triangle face tangent and bitangent.

float det = tex_edge_1.cross(tex_edge_2);
auto det = tex_edge_1.cross(tex_edge_2);

constexpr auto epsilon = ruis::real(1e-6f);

Expand All @@ -662,6 +665,7 @@ utki::shared_ref<ruis::render::vertex_array> gltf_loader::create_vao_with_tangen
if (abs(det) >= epsilon) {
auto det_reciprocal = ruis::real(1) / det;

// TODO: figure out what is happening here and refactor with vector ops
tangent[0] = (tex_edge_2[1] * edge1[0] - tex_edge_1[1] * edge2[0]) * det_reciprocal;
tangent[1] = (tex_edge_2[1] * edge1[1] - tex_edge_1[1] * edge2[1]) * det_reciprocal;
tangent[2] = (tex_edge_2[1] * edge1[2] - tex_edge_1[1] * edge2[2]) * det_reciprocal;
Expand All @@ -671,8 +675,6 @@ utki::shared_ref<ruis::render::vertex_array> gltf_loader::create_vao_with_tangen
bitangent[2] = (-tex_edge_2[0] * edge1[2] + tex_edge_1[0] * edge2[2]) * det_reciprocal;
}

// TODO: figure out what is happening here and refactor with vector ops

// Accumulate the tangents and bitangents.
tangents[index0] += tangent;
bitangents[index0] += bitangent;
Expand All @@ -688,18 +690,17 @@ utki::shared_ref<ruis::render::vertex_array> gltf_loader::create_vao_with_tangen
for (uint32_t i = 0; i < num_vertices; ++i) {
// Gram-Schmidt orthogonalize tangent with normal.

float n_dot_t = normals[i] * tangents[i]; // dot product
auto n_dot_t = normals[i] * tangents[i]; // dot product

tangents[i] -= normals[i] * n_dot_t;

// Normalize the tangent.
tangents[i].normalize();

ruis::vec3 bitangent_other = normals[i].cross(tangents[i]);

float b_dot_b = bitangent_other * bitangents[i];
auto b_dot_b = bitangent_other * bitangents[i];

float sign = (b_dot_b < 0.0f) ? -1.0f : 1.0f;
auto sign = b_dot_b < ruis::real(0) ? ruis::real(-1) : ruis::real(1);

bitangents[i] = bitangent_other * sign;
}
Expand Down

0 comments on commit 2f8c60d

Please sign in to comment.