Skip to content
Merged
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
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ option(THREEPP_BUILD_EXAMPLES "Build examples" ON)
option(THREEPP_BUILD_TESTS "Build test suite" ON)
option(THREEPP_WITH_SVG "Build with SVGLoader" ON)
option(THREEPP_WITH_AUDIO "Build with Audio" ON)

option(THREEPP_WITH_FBX "Build with FBXLoader (OpenFBX)" OFF)
option(THREEPP_WITH_USD "Build with USDLoader (tinyusdz)" OFF)

option(THREEPP_TREAT_WARNINGS_AS_ERRORS "Treat warnings as errors" OFF)


Expand Down Expand Up @@ -87,6 +90,27 @@ if (THREEPP_WITH_USD)
)
endif ()


if (THREEPP_WITH_FBX)
include(FetchContent)
message(STATUS "Fetching OpenFBX...")
FetchContent_Declare(
openfbx
GIT_REPOSITORY https://github.com/nem0/OpenFBX.git
GIT_TAG master
GIT_SHALLOW TRUE
)
# Populate source only — do NOT call add_subdirectory (OpenFBX's CMakeLists
# has an unresolvable install-export dependency on libdeflate_static).
# CMP0169 OLD lets us call FetchContent_Populate directly without CMake 4.x
# routing through add_subdirectory.
cmake_policy(SET CMP0169 OLD)
FetchContent_GetProperties(openfbx)
if (NOT openfbx_POPULATED)
FetchContent_Populate(openfbx)
endif ()
endif ()

add_subdirectory(src)

if (THREEPP_BUILD_EXAMPLES)
Expand Down
4,314 changes: 4,314 additions & 0 deletions data/models/fbx/stanford-bunny.fbx

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions examples/loaders/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ endif ()
if (THREEPP_WITH_USD)
add_example(NAME "usd_loader")
endif ()

if (THREEPP_WITH_FBX)
add_example(NAME "fbx_loader")
endif ()
56 changes: 56 additions & 0 deletions examples/loaders/fbx_loader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

#include "threepp/loaders/FBXLoader.hpp"
#include "threepp/threepp.hpp"

#include <iostream>

using namespace threepp;

int main(int argc, char* argv[]) {
const std::string defaultPath = std::string(DATA_FOLDER) + "/models/fbx/stanford-bunny.fbx";
const std::string fbxPath = argc > 1 ? argv[1] : defaultPath;

Canvas canvas("FBX loader example");
GLRenderer renderer{canvas.size()};

auto scene = Scene::create();
scene->background = Color::aliceblue;
auto camera = PerspectiveCamera::create(60, canvas.aspect(), 0.1f, 1000000.f);
camera->position.set(0, 2, 10);

OrbitControls controls{*camera, canvas};

auto ambientLight = AmbientLight::create(0xffffff, 0.2f);
scene->add(ambientLight);

auto dirLight = DirectionalLight::create(0xffffff, 1.0f);
dirLight->position.set(1, 1, 1);
scene->add(dirLight);

FBXLoader loader;
auto model = loader.load(fbxPath);

if (!model) {
std::cerr << "Failed to load FBX: " << fbxPath << "\n";
return 1;
}
scene->add(model);

Box3 bb;
bb.setFromObject(*model);

camera->position.set(0, bb.getSize().y * 2, bb.getSize().z * 2);
camera->lookAt(bb.getCenter());

canvas.onWindowResize([&](WindowSize newSize) {
renderer.setSize(newSize);
camera->aspect = newSize.aspect();
camera->updateProjectionMatrix();
});

canvas.animate([&] {
renderer.render(*scene, *camera);
});

return 0;
}
24 changes: 24 additions & 0 deletions include/threepp/loaders/DDSLoader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <filesystem>
#include <memory>
#include <vector>

namespace threepp {

class Texture;

class DDSLoader {
public:
DDSLoader();
~DDSLoader();

std::shared_ptr<Texture> load(const std::filesystem::path& path);
std::shared_ptr<Texture> loadFromMemory(const std::vector<unsigned char>& data);

private:
struct Impl;
std::unique_ptr<Impl> pimpl_;
};

}// namespace threepp
22 changes: 22 additions & 0 deletions include/threepp/loaders/FBXLoader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <filesystem>
#include <memory>

namespace threepp {

class Group;

class FBXLoader {
public:
FBXLoader();
~FBXLoader();

std::shared_ptr<Group> load(const std::filesystem::path& path);

private:
struct Impl;
std::unique_ptr<Impl> pimpl_;
};

}// namespace threepp
5 changes: 5 additions & 0 deletions include/threepp/loaders/loaders.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifndef THREEPP_LOADERS_HPP
#define THREEPP_LOADERS_HPP

#include "DDSLoader.hpp"
#include "FontLoader.hpp"
#include "ModelLoader.hpp"
#include "OBJLoader.hpp"
Expand All @@ -12,4 +13,8 @@
#include "USDLoader.hpp"
#endif

#ifdef THREEPP_WITH_FBX
#include "FBXLoader.hpp"
#endif

#endif//THREEPP_LOADERS_HPP
2 changes: 2 additions & 0 deletions include/threepp/renderers/gl/GLState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ namespace threepp {

void texImage3D(unsigned int target, int level, int internalFormat, int width, int height, int depth, unsigned int format, unsigned int type, const void* pixels);

void texCompressedImage2D(unsigned int target, int level, unsigned int internalFormat, int width, int height, int imageSize, const void* data);

//

void scissor(const Vector4& scissor);
Expand Down
14 changes: 14 additions & 0 deletions include/threepp/textures/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifndef THREEPP_IMAGE_HPP
#define THREEPP_IMAGE_HPP

#include <optional>
#include <utility>
#include <variant>
#include <vector>
Expand All @@ -17,12 +18,25 @@ namespace threepp {
unsigned int height;
unsigned int depth;

// When set, the data_ buffer holds a compressed block payload and this
// value is the GL compressed internal format token (e.g.
// GL_COMPRESSED_RGBA_S3TC_DXT5_EXT). Used by GLTextures to call
// glCompressedTexImage2D instead of glTexImage2D.
std::optional<unsigned int> compressedFormat;

Image(ImageData data, unsigned int width, unsigned int height)
: width(width), height(height), depth(0), data_(std::move(data)){};

Image(ImageData data, unsigned int width, unsigned int height, unsigned int depth)
: width(width), height(height), depth(depth), data_(std::move(data)){};

// Constructor for compressed block data.
Image(std::vector<unsigned char> data, unsigned int width, unsigned int height,
unsigned int glCompressedFormat)
: width(width), height(height), depth(0),
compressedFormat(glCompressedFormat),
data_(std::move(data)){};

void setData(ImageData data) {

data_ = std::move(data);
Expand Down
19 changes: 19 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ set(publicHeaders
"threepp/loaders/AssimpLoader.hpp"
"threepp/loaders/CubeTextureLoader.hpp"
"threepp/loaders/ColladaLoader.hpp"
"threepp/loaders/DDSLoader.hpp"
"threepp/loaders/FontLoader.hpp"
"threepp/loaders/GLTFLoader.hpp"
"threepp/loaders/MTLLoader.hpp"
Expand Down Expand Up @@ -350,6 +351,7 @@ set(sources
"threepp/input/PeripheralsEventSource.cpp"

"threepp/loaders/ColladaLoader.cpp"
"threepp/loaders/DDSLoader.cpp"
"threepp/loaders/FontLoader.cpp"
"threepp/loaders/GLTFLoader.cpp"
"threepp/loaders/ImageLoader.cpp"
Expand Down Expand Up @@ -488,6 +490,18 @@ if (THREEPP_WITH_USD)
)
endif ()

if (THREEPP_WITH_FBX)
list(APPEND publicHeaders
"threepp/loaders/FBXLoader.hpp"
)

list(APPEND sources
"threepp/loaders/FBXLoader.cpp"
"${openfbx_SOURCE_DIR}/src/ofbx.cpp"
"${openfbx_SOURCE_DIR}/src/libdeflate.c"
)
endif ()

if (THREEPP_WITH_AUDIO)
list(APPEND publicHeaders
"threepp/audio/Audio.hpp"
Expand Down Expand Up @@ -603,6 +617,11 @@ if (THREEPP_WITH_USD)
target_compile_definitions(threepp PRIVATE THREEPP_WITH_USD)
endif ()

if (THREEPP_WITH_FBX)
target_include_directories(threepp PRIVATE "${openfbx_SOURCE_DIR}/src")
target_compile_definitions(threepp PRIVATE THREEPP_WITH_FBX)
endif ()

if (NOT DEFINED EMSCRIPTEN)

target_include_directories(threepp
Expand Down
Loading
Loading