Skip to content

Commit

Permalink
[Data/GltfLoad] Tangents are computed if not loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Razakhel committed Dec 26, 2023
1 parent a570dba commit 44b1baa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions src/RaZ/Data/GltfLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void loadVertices(const fastgltf::Primitive& primitive,
const std::vector<fastgltf::Buffer>& buffers,
const std::vector<fastgltf::BufferView>& bufferViews,
const std::vector<fastgltf::Accessor>& accessors,
std::vector<Vertex>& vertices) {
Submesh& submesh) {
Logger::debug("[GltfLoad] Loading vertices...");

const auto positionIt = primitive.findAttribute("POSITION");
Expand All @@ -54,6 +54,7 @@ void loadVertices(const fastgltf::Primitive& primitive,
if (!positionAccessor.bufferViewIndex.has_value())
return;

std::vector<Vertex>& vertices = submesh.getVertices();
vertices.resize(positionAccessor.count);

loadVertexData<float>(positionAccessor, buffers, bufferViews, vertices, [] (Vertex& vert, const float* data) {
Expand All @@ -79,6 +80,8 @@ void loadVertices(const fastgltf::Primitive& primitive,
}}
}};

bool hasLoadedTangents = false;

for (auto&& [attribName, callback] : attributes) {
const auto attribIter = primitive.findAttribute(attribName);

Expand All @@ -87,10 +90,17 @@ void loadVertices(const fastgltf::Primitive& primitive,

const fastgltf::Accessor& attribAccessor = accessors[attribIter->second];

if (attribAccessor.bufferViewIndex.has_value())
if (attribAccessor.bufferViewIndex.has_value()) {
loadVertexData(attribAccessor, buffers, bufferViews, vertices, callback);

if (attribName == "TANGENT")
hasLoadedTangents = true;
}
}

if (!hasLoadedTangents)
submesh.computeTangents();

Logger::debug("[GltfLoad] Loaded vertices");
}

Expand All @@ -117,19 +127,21 @@ void loadIndices(const fastgltf::Accessor& indicesAccessor,
const std::size_t dataStride = indicesView.byteStride.value_or(dataSize);

for (std::size_t i = 0; i < indices.size(); ++i) {
const uint8_t* const indexData = indicesData + i * dataStride;

// The indices must be of an unsigned integer type, but its size is unspecified
// See: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#_mesh_primitive_indices
switch (dataSize) {
case 1:
indices[i] = *reinterpret_cast<const uint8_t*>(indicesData + i * dataStride);
indices[i] = *indexData;
break;

case 2:
indices[i] = *reinterpret_cast<const uint16_t*>(indicesData + i * dataStride);
indices[i] = *reinterpret_cast<const uint16_t*>(indexData);
break;

case 4:
indices[i] = *reinterpret_cast<const uint32_t*>(indicesData + i * dataStride);
indices[i] = *reinterpret_cast<const uint32_t*>(indexData);
break;

default:
Expand Down Expand Up @@ -157,8 +169,9 @@ std::pair<Mesh, MeshRenderer> loadMeshes(const std::vector<fastgltf::Mesh>& mesh
Submesh& submesh = loadedMesh.addSubmesh();
SubmeshRenderer& submeshRenderer = loadedMeshRenderer.addSubmeshRenderer();

loadVertices(primitive, buffers, bufferViews, accessors, submesh.getVertices());
// Indices must be loaded first as they are needed to compute the tangents if necessary
loadIndices(accessors[*primitive.indicesAccessor], buffers, bufferViews, submesh.getTriangleIndices());
loadVertices(primitive, buffers, bufferViews, accessors, submesh);

submeshRenderer.load(submesh, (primitive.type == fastgltf::PrimitiveType::Triangles ? RenderMode::TRIANGLE : RenderMode::POINT));
submeshRenderer.setMaterialIndex(primitive.materialIndex.value_or(0));
Expand Down
2 changes: 1 addition & 1 deletion tests/src/RaZ/Data/GltfFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ TEST_CASE("GltfFormat load GLB textured") {
CHECK(mesh.getSubmeshes()[0].getVertices()[0] == Raz::Vertex{ Raz::Vec3f(-0.5f, -0.5f, 0.5f), Raz::Vec2f(0.f, 0.f), Raz::Axis::Z, Raz::Vec3f(0.f) });
CHECK(mesh.getSubmeshes()[0].getVertices()[1] == Raz::Vertex{ Raz::Vec3f(0.5f, -0.5f, 0.5f), Raz::Vec2f(0.f, 0.f), Raz::Axis::Z, Raz::Vec3f(0.f) });
CHECK(mesh.getSubmeshes()[0].getVertices()[12] == Raz::Vertex{ Raz::Vec3f(0.5f, -0.5f, 0.5f), Raz::Vec2f(0.f, 0.f), -Raz::Axis::Y, Raz::Vec3f(0.f) });
CHECK(mesh.getSubmeshes()[0].getVertices()[23] == Raz::Vertex{ Raz::Vec3f(0.5f, 0.5f, -0.5f), Raz::Vec2f(1.f, 1.f), -Raz::Axis::Z, Raz::Vec3f(0.f) });
CHECK(mesh.getSubmeshes()[0].getVertices()[23] == Raz::Vertex{ Raz::Vec3f(0.5f, 0.5f, -0.5f), Raz::Vec2f(1.f, 1.f), -Raz::Axis::Z, Raz::Axis::X });

REQUIRE(meshRenderer.getSubmeshRenderers().size() == 1);
CHECK(meshRenderer.getSubmeshRenderers()[0].getRenderMode() == Raz::RenderMode::TRIANGLE);
Expand Down

0 comments on commit 44b1baa

Please sign in to comment.