23#ifndef VCL_RENDER_IO_CAMERA_GLTF_LOAD_H
24#define VCL_RENDER_IO_CAMERA_GLTF_LOAD_H
26#include <vclib/io/file_info.h>
28#include <vclib/render/concepts/camera.h>
29#include <vclib/render/viewer/camera.h>
41inline tinygltf::Model loadTinyGltfCameraModel(
const std::string& filename)
43 tinygltf::TinyGLTF loader;
44 tinygltf::Model model;
45 std::string err, warn;
50 ret = loader.LoadASCIIFromFile(&model, &err, &warn, filename);
51 else if (ext ==
".glb")
52 ret = loader.LoadBinaryFromFile(&model, &err, &warn, filename);
54 throw UnknownFileFormatException(ext);
57 throw std::runtime_error(
58 "Failed to load glTF cameras: " + err +
59 (warn.empty() ?
"" :
" " + warn));
65template<CameraConcept CameraType = Camera<
float>>
66inline std::vector<CameraType> loadCamerasGltf(
const std::string& filename)
68 using Scalar =
typename CameraType::ScalarType;
69 tinygltf::Model model = detail::loadTinyGltfCameraModel(filename);
71 std::vector<CameraType> cams;
72 cams.reserve(model.cameras.size());
74 for (
size_t cameraIdx = 0; cameraIdx < model.cameras.size(); ++cameraIdx) {
75 const tinygltf::Camera& gltfCamera = model.cameras[cameraIdx];
78 if (gltfCamera.type ==
"perspective") {
79 camera.projectionMode() = CameraType::ProjectionMode::PERSPECTIVE;
80 camera.fieldOfView() = gltfCamera.perspective.yfov * 180.0 / M_PI;
82 camera.aspectRatio() = gltfCamera.perspective.aspectRatio;
83 camera.nearPlane() = gltfCamera.perspective.znear;
84 camera.farPlane() = gltfCamera.perspective.zfar;
86 else if (gltfCamera.type ==
"orthographic") {
87 camera.projectionMode() = CameraType::ProjectionMode::ORTHO;
88 camera.verticalHeight() = gltfCamera.orthographic.ymag * 2.0;
90 camera.aspectRatio() =
91 gltfCamera.orthographic.xmag / gltfCamera.orthographic.ymag;
92 camera.nearPlane() = gltfCamera.orthographic.znear;
93 camera.farPlane() = gltfCamera.orthographic.zfar;
96 bool foundNode =
false;
97 for (
const auto& node : model.nodes) {
98 if (node.camera ==
static_cast<int>(cameraIdx)) {
101 if (node.matrix.size() == 16) {
110 if (node.translation.size() == 3) {
114 node.translation[2]);
116 if (node.rotation.size() == 4) {
123 if (node.scale.size() == 3) {
125 node.scale[0], node.scale[1], node.scale[2]);
131 affine.rotate(rotation);
133 mat = affine.matrix();
136 typename CameraType::PointType eye(
137 mat(0, 3), mat(1, 3), mat(2, 3));
140 typename CameraType::PointType forward(
141 -mat(0, 2), -mat(1, 2), -mat(2, 2));
142 forward = forward.normalized();
144 typename CameraType::PointType up(
145 mat(0, 1), mat(1, 1), mat(2, 1));
146 camera.up() = up.normalized();
157 throw std::runtime_error(
158 "Invalid glTF camera: no node references camera index " +
159 std::to_string(cameraIdx));
162 cams.push_back(camera);
165 assert(cams.size() == model.cameras.size());
170template<CameraConcept CameraType = Camera<
float>>
171inline CameraType loadCameraGltf(
172 const std::string& filename,
175 auto cams = loadCamerasGltf<CameraType>(filename);
176 if (cameraIdx >= cams.size())
177 throw std::out_of_range(
178 "Camera index " + std::to_string(cameraIdx) +
179 " is out of range. Total cameras: " + std::to_string(cams.size()));
180 return cams[cameraIdx];
A class representing a box in N-dimensional space.
Definition box.h:46
void translate(const PointT &p)
Translates the box by summing the values of p.
Definition box.h:456
Box()
The Empty constructor of a box, initializes a null box.
Definition box.h:65
PointT center() const
Calculates the center point of the box.
Definition box.h:259
static std::string extension(const std::string &filename)
Get the extension of a file.
Definition file_info.h:260
The Point class represents an N-dimensional point containing N scalar values.
Definition point.h:55
auto distance(const PointType &point0, const PointType &point1)
Compute the distance between two Points of any dimension.
Definition distance.h:45
Quaternion< double > Quaterniond
A convenience alias for Quaternion with double-precision floating-point components.
Definition quaternion.h:201
Point3< double > Point3d
A convenience alias for a 3-dimensional Point with double-precision floating-point components.
Definition point.h:780