Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bounds min,max #978

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion shards/gfx/gltf/gltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ struct Loader {
loadMeshes();
loadNodes();

// Unify everyhing into a single node per scene
// Unify everything into a single node per scene
// needs to be done before computing animation paths in skins
size_t numScenes = model.scenes.size();
sceneMap.resize(numScenes);
Expand Down Expand Up @@ -674,6 +674,54 @@ struct Loader {
}
};

// Compute the model matrix for a node
linalg::mat<float, 4, 4> getModelMatrix(const tinygltf::Node &node) {
auto translation = node.translation.empty()
? linalg::vec<float, 3>{0, 0, 0}
: linalg::vec<float, 3>{node.translation[0], node.translation[1], node.translation[2]};
auto rotation = node.rotation.empty()
? linalg::vec<float, 4>{0, 0, 0, 1}
: linalg::vec<float, 4>{node.rotation[0], node.rotation[1], node.rotation[2], node.rotation[3]};
auto scale =
node.scale.empty() ? linalg::vec<float, 3>{1, 1, 1} : linalg::vec<float, 3>{node.scale[0], node.scale[1], node.scale[2]};
auto matrix = node.matrix.empty() ? linalg::identity_t{4} : convertNodeTransform(node).getMatrix();

// Combine transformations
auto T = linalg::translation_matrix(translation);
auto R = linalg::rotation_matrix(rotation);
auto S = linalg::scaling_matrix(scale);
return linalg::mul(linalg::mul(linalg::mul(matrix, T), R), S);
}

void computeBoundingBox(const tinygltf::Node &node, const tinygltf::Model &model, linalg::vec<float, 3> &global_min,
linalg::vec<float, 3> &global_max,
const linalg::mat<float, 4, 4> &parentTransform = linalg::identity_t{4}) {
linalg::mat<float, 4, 4> modelMatrix = linalg::mul(parentTransform, getModelMatrix(node));

if (node.mesh != -1) {
const tinygltf::Mesh &mesh = model.meshes[node.mesh];
for (const auto &primitive : mesh.primitives) {
const tinygltf::Accessor &accessor = model.accessors[primitive.attributes.find("POSITION")->second];
const tinygltf::BufferView &bufferView = model.bufferViews[accessor.bufferView];
const tinygltf::Buffer &buffer = model.buffers[bufferView.buffer];

// Adjusting buffer data interpretation to float instead of double
const float *positions = reinterpret_cast<const float *>(&(buffer.data[bufferView.byteOffset + accessor.byteOffset]));
for (size_t i = 0; i < accessor.count; ++i) {
linalg::vec<float, 3> vertex(positions[i * 3], positions[i * 3 + 1], positions[i * 3 + 2]);
linalg::vec<float, 4> vertex_homogeneous(vertex, 1.0f); // Homogenize the vertex for transformation
vertex = linalg::mul(modelMatrix, vertex_homogeneous).xyz(); // Apply transformation and reduce back to 3D
global_min = linalg::min(global_min, vertex);
global_max = linalg::max(global_max, vertex);
}
}
}

for (int child : node.children) {
computeBoundingBox(model.nodes[child], model, global_min, global_max, modelMatrix);
}
}

template <typename T> glTF load(T loader) {
tinygltf::Model model;
loader(model);
Expand All @@ -693,6 +741,11 @@ template <typename T> glTF load(T loader) {
result.root = std::move(gfxLoader.sceneMap[model.defaultScene]);
result.animations = std::move(gfxLoader.animations);
result.materials = std::move(gfxLoader.materials);

// compute bounding box
if (model.scenes[model.defaultScene].nodes.size() > 0)
computeBoundingBox(model.nodes[model.scenes[model.defaultScene].nodes[0]], model, result.boundsMin, result.boundsMax);

return result;
}

Expand Down
2 changes: 2 additions & 0 deletions shards/gfx/gltf/gltf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ struct glTF {
MeshTreeDrawable::Ptr root;
std::unordered_map<std::string, Animation> animations;
std::unordered_map<std::string, MaterialPtr> materials;
linalg::vec<float, 3> boundsMin = {0, 0, 0};
linalg::vec<float, 3> boundsMax = {0, 0, 0};

glTF() = default;

Expand Down
4 changes: 4 additions & 0 deletions shards/modules/gfx/gltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ struct GLTFShard {
_model->root = std::static_pointer_cast<MeshTreeDrawable>(other->clone());
_model->animations = shOther.animations;
_model->materials = shOther.materials;
_model->boundsMin = shOther.boundsMin;
_model->boundsMax = shOther.boundsMax;
rootNodeWrapped = shOther.rootNodeWrapped;
} break;
case LoadMode::LoadFileStatic:
Expand All @@ -573,6 +575,8 @@ struct GLTFShard {
_drawable->drawable = _model->root;
_drawable->animations = _model->animations;
_drawable->materials = _model->materials;
_drawable->boundsMin = _model->boundsMin;
_drawable->boundsMax = _model->boundsMax;

if (hasAnimationController()) {
shardifyAnimationData();
Expand Down
4 changes: 4 additions & 0 deletions shards/modules/gfx/shards_types.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef E452293C_6700_4675_8B6E_5293674E0A33
#define E452293C_6700_4675_8B6E_5293674E0A33

#include "shards/shards.h"
#include <shards/common_types.hpp>
#include <shards/core/foundation.hpp>
#include <shards/core/object_type.hpp>
Expand Down Expand Up @@ -28,6 +29,9 @@ struct SHDrawable {
Variant drawable;
std::unordered_map<std::string, Animation> animations;
std::unordered_map<std::string, MaterialPtr> materials;
linalg::vec<float, 3> boundsMin = {0, 0, 0};
linalg::vec<float, 3> boundsMax = {0, 0, 0};

bool rootNodeWrapped{};

void assign(const std::shared_ptr<IDrawable> &generic) {
Expand Down
Loading