Skip to content

Commit

Permalink
Merge pull request #211 from edunad/bugfixes/gltf-bbox
Browse files Browse the repository at this point in the history
Add BBOX support to GLTF
  • Loading branch information
edunad authored Oct 16, 2024
2 parents 6b6c6b8 + aaade73 commit f9d091b
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 8 deletions.
3 changes: 2 additions & 1 deletion rawrbox.gltf/include/rawrbox/gltf/importer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace rawrbox {
const uint32_t IMPORT_ANIMATIONS = 1 << 3;
const uint32_t IMPORT_BLEND_SHAPES = 1 << 4;

const uint32_t CALCULATE_BBOX = 1 << 5;

namespace Debug {
const uint32_t PRINT_BONE_STRUCTURE = 1 << 10;
const uint32_t PRINT_MATERIALS = 1 << 11;
Expand Down Expand Up @@ -151,7 +153,6 @@ namespace rawrbox {
struct GLTFMesh : public rawrbox::GLTFNode {
public:
rawrbox::BBOX bbox = {};

std::vector<rawrbox::GLTFPrimitive> primitives = {};

ozz::animation::Skeleton* skeleton = nullptr;
Expand Down
14 changes: 14 additions & 0 deletions rawrbox.gltf/src/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,20 @@ namespace rawrbox {
}
}
// ----------------

// BBOX CALCULATION --
if ((this->loadFlags & rawrbox::ModelLoadFlags::CALCULATE_BBOX) > 0) {
gltfMesh->bbox.min = rawrbox::Vector3f(std::numeric_limits<float>::max());
gltfMesh->bbox.max = rawrbox::Vector3f(std::numeric_limits<float>::min());

for (const auto& vertex : rawrPrimitive.vertices) {
gltfMesh->bbox.min = gltfMesh->bbox.min.min(vertex.position);
gltfMesh->bbox.max = gltfMesh->bbox.max.max(vertex.position);
}

gltfMesh->bbox.size = gltfMesh->bbox.max - gltfMesh->bbox.min;
}
// -------------
}
// -------------------

Expand Down
9 changes: 6 additions & 3 deletions rawrbox.math/include/rawrbox/math/bbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ namespace rawrbox {
return pos.x >= this->min.x && pos.x <= this->max.x && pos.y >= this->min.y && pos.y <= this->max.y && pos.z >= this->min.z && pos.z <= this->max.z;
}

void combine(const BBOX_t<NumberType>& b) {
void combine(const BBOXType& b) {
this->min = {std::min(this->min.x, b.min.x), std::min(this->min.y, b.min.y), std::min(this->min.z, b.min.z)};
this->max = {std::max(this->max.x, b.max.x), std::max(this->max.y, b.max.y), std::max(this->max.z, b.max.z)};

this->size = this->min.abs() + this->max.abs();
}

bool operator==(const BBOX_t<NumberType>& other) const { return this->size == other.size; }
bool operator!=(const BBOX_t<NumberType>& other) const { return !operator==(other); }
bool operator==(const BBOXType& other) const { return this->size == other.size; }
bool operator!=(const BBOXType& other) const { return !operator==(other); }

BBOXType operator*(NumberType other) const { return BBOXType(this->min * other, this->max * other, this->size * other); }
BBOXType operator*(const rawrbox::Vector3_t<NumberType>& other) const { return BBOXType(this->min * other, this->max * other, this->size * other); }
};

using BBOXd = BBOX_t<double>;
Expand Down
11 changes: 10 additions & 1 deletion rawrbox.math/tests/bbox.spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,21 @@ TEST_CASE("BBOX should behave as expected", "[rawrbox::BBOX]") {
REQUIRE(bbox1.max == rawrbox::Vector3f(2, 2, 2));
}

SECTION("rawrbox::BBOX::operators") {
SECTION("rawrbox::BBOX::operator==") {
rawrbox::BBOX bbox1(rawrbox::Vector3f(0, 0, 0), rawrbox::Vector3f(1, 1, 1), rawrbox::Vector3f(1, 1, 1));
rawrbox::BBOX bbox2(rawrbox::Vector3f(0, 0, 0), rawrbox::Vector3f(1, 1, 1), rawrbox::Vector3f(1, 1, 1));
rawrbox::BBOX bbox3(rawrbox::Vector3f(0, 0, 0), rawrbox::Vector3f(2, 2, 2), rawrbox::Vector3f(2, 2, 2));

REQUIRE(bbox1 == bbox2);
REQUIRE(bbox1 != bbox3);
}

SECTION("rawrbox::BBOX::operator*") {
rawrbox::BBOX bbox(rawrbox::Vector3f(0, 0, 0), rawrbox::Vector3f(1, 1, 1), rawrbox::Vector3f(1, 1, 1));
rawrbox::BBOX scaledBbox = bbox * 2.0F;

REQUIRE(scaledBbox.min == rawrbox::Vector3f(0, 0, 0));
REQUIRE(scaledBbox.max == rawrbox::Vector3f(2, 2, 2));
REQUIRE(scaledBbox.size == rawrbox::Vector3f(2, 2, 2));
}
}
2 changes: 1 addition & 1 deletion rawrbox.render/include/rawrbox/render/models/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ namespace rawrbox {
[[nodiscard]] virtual const std::vector<T>& getVertices() const { return this->vertices; }
[[nodiscard]] virtual const std::vector<uint32_t>& getIndices() const { return this->indices; }

[[nodiscard]] virtual const rawrbox::BBOX& getBBOX() const { return this->bbox; }
[[nodiscard]] virtual rawrbox::BBOX getBBOX() const { return this->bbox * this->_scale; }

[[nodiscard]] virtual bool empty() const {
return this->indices.empty() || this->vertices.empty();
Expand Down
2 changes: 1 addition & 1 deletion rawrbox.render/include/rawrbox/render/models/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ namespace rawrbox {
this->updateLights();
}

[[nodiscard]] virtual const rawrbox::BBOX& getBBOX() const { return this->_bbox; }
[[nodiscard]] virtual rawrbox::BBOX getBBOX() { return this->_bbox * this->getScale(); }
[[nodiscard]] virtual size_t totalMeshes() const {
return this->_meshes.size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace rawrbox {
requires(std::is_integral_v<T> || std::is_floating_point_v<T>)
static void registerTemplate(lua_State* L, const std::string& name) {
using BBOXT = rawrbox::BBOX_t<T>;
using VECT = rawrbox::Vector3_t<T>;

luabridge::getGlobalNamespace(L)
.beginClass<BBOXT>(name.c_str())
Expand All @@ -21,6 +22,10 @@ namespace rawrbox {
.addFunction("isEmpty", &BBOXT::isEmpty)
.addFunction("combine", &BBOXT::combine)

.addFunction("__mul",
luabridge::overload<T>(&BBOXT::operator*),
luabridge::overload<const VECT&>(&BBOXT::operator*))

.addFunction("__eq", &BBOXT::operator==)
.addFunction("__ne", &BBOXT::operator!=)

Expand Down
2 changes: 2 additions & 0 deletions samples/009-gltf/include/gltf/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace gltf {
std::unique_ptr<rawrbox::GLTFModel<rawrbox::MaterialSkinnedLit>> _wolfLit;

std::unique_ptr<rawrbox::Model<>> _modelGrid;
std::unique_ptr<rawrbox::Model<>> _bbox;

std::unique_ptr<rawrbox::Text3D<>> _text;

bool _ready = false;
Expand Down
11 changes: 10 additions & 1 deletion samples/009-gltf/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace gltf {
void Game::loadContent() {
std::vector<std::pair<std::string, uint32_t>> initialContentFiles = {
{"./assets/models/ps1_phasmophobia/scene.glb", rawrbox::ModelLoadFlags::IMPORT_TEXTURES | rawrbox::ModelLoadFlags::IMPORT_LIGHT | rawrbox::ModelLoadFlags::Optimizer::MESH},
{"./assets/models/shape_keys/shape_keys.glb", rawrbox::ModelLoadFlags::IMPORT_TEXTURES | rawrbox::ModelLoadFlags::IMPORT_BLEND_SHAPES | rawrbox::ModelLoadFlags::Debug::PRINT_BLENDSHAPES},
{"./assets/models/shape_keys/shape_keys.glb", rawrbox::ModelLoadFlags::CALCULATE_BBOX | rawrbox::ModelLoadFlags::IMPORT_TEXTURES | rawrbox::ModelLoadFlags::IMPORT_BLEND_SHAPES | rawrbox::ModelLoadFlags::Debug::PRINT_BLENDSHAPES},
{"./assets/models/wolf/wolf.glb", rawrbox::ModelLoadFlags::IMPORT_TEXTURES | rawrbox::ModelLoadFlags::IMPORT_ANIMATIONS | rawrbox::ModelLoadFlags::Optimizer::SKELETON_ANIMATIONS | rawrbox::ModelLoadFlags::Debug::PRINT_ANIMATIONS},
{"./assets/models/anim_test.glb", rawrbox::ModelLoadFlags::IMPORT_ANIMATIONS},
{"./assets/models/grandma_tv/scene.gltf", rawrbox::ModelLoadFlags::IMPORT_TEXTURES | rawrbox::ModelLoadFlags::IMPORT_ANIMATIONS | rawrbox::ModelLoadFlags::Debug::PRINT_MATERIALS}};
Expand All @@ -77,6 +77,8 @@ namespace gltf {
void Game::contentLoaded() {
if (this->_ready) return;

this->_bbox = std::make_unique<rawrbox::Model<>>();

// PHASMO
{
auto* mdl = rawrbox::RESOURCES::getFile<rawrbox::ResourceGLTF>("./assets/models/ps1_phasmophobia/scene.glb")->get();
Expand Down Expand Up @@ -118,6 +120,8 @@ namespace gltf {

this->_blendTest->setPos({-5, 0, 0});
this->_blendTest->upload(rawrbox::UploadType::FIXED_DYNAMIC);

this->_bbox->addMesh(rawrbox::MeshUtils::generateBBOX({-5, 0, 0}, this->_blendTest->getBBOX()));
}
// ----------

Expand Down Expand Up @@ -184,6 +188,8 @@ namespace gltf {
}
// ----

this->_bbox->upload();

// LIGHT ----
rawrbox::LIGHTS::add<rawrbox::PointLight>(rawrbox::Vector3f{-1, 1.6F, -1.4F}, rawrbox::Colors::White() * 100, 1.6F);
rawrbox::LIGHTS::add<rawrbox::PointLight>(rawrbox::Vector3f{-1, 0.6F, -1.4F}, rawrbox::Colors::White() * 100, 1.6F);
Expand Down Expand Up @@ -224,6 +230,7 @@ namespace gltf {
this->_text.reset();
this->_animTest.reset();
this->_phasmo.reset();
this->_bbox.reset();

rawrbox::RESOURCES::shutdown();
}
Expand Down Expand Up @@ -275,6 +282,8 @@ namespace gltf {
// TEXT ----
if (this->_text != nullptr) this->_text->draw();
// ----

if (this->_bbox != nullptr) this->_bbox->draw();
}

void Game::draw() {
Expand Down

0 comments on commit f9d091b

Please sign in to comment.