3 #include "CesiumGltf/Model.h"
82 const std::byte* _pData;
105 : _pData(nullptr), _stride(0), _offset(0), _size(0), _status(
status) {}
119 const std::byte* pData,
142 this->create(model, accessor);
165 this->create(model, *pAccessor);
177 if (i < 0 || i >= this->_size) {
178 throw std::range_error(
"index out of range");
181 return *
reinterpret_cast<const T*
>(
182 this->_pData + i * this->_stride + this->_offset);
192 int64_t
size() const noexcept {
return this->_size; }
208 int64_t
stride() const noexcept {
return this->_stride; }
216 const std::byte*
data() const noexcept {
217 return this->_pData + this->_offset;
221 void create(
const Model& model,
const Accessor& accessor) noexcept {
237 const int64_t bufferBytes = int64_t(
data.size());
243 const int64_t accessorByteStride = accessor.computeByteStride(model);
244 const int64_t accessorComponentElements =
245 accessor.computeNumberOfComponents();
246 const int64_t accessorComponentBytes =
247 accessor.computeByteSizeOfComponent();
248 const int64_t accessorBytesPerStride =
249 accessorComponentElements * accessorComponentBytes;
251 if (
sizeof(T) != accessorBytesPerStride) {
256 const int64_t accessorBytes = accessorByteStride * accessor.count;
257 const int64_t bytesRemainingInBufferView =
259 (accessor.byteOffset + accessorByteStride * (accessor.count - 1) +
260 accessorBytesPerStride);
261 if (accessorBytes > pBufferView->
byteLength ||
262 bytesRemainingInBufferView < 0) {
268 this->_stride = accessorByteStride;
269 this->_offset = accessor.byteOffset + pBufferView->
byteOffset;
270 this->_size = accessor.count;
280 #pragma pack(push, 1)
299 template <
typename T>
struct VEC2 {
311 template <
typename T>
struct VEC3 {
323 template <
typename T>
struct VEC4 {
335 template <
typename T>
struct MAT2 {
347 template <
typename T>
struct MAT3 {
359 template <
typename T>
struct MAT4 {
369 namespace CesiumImpl {
370 template <
typename TCallback,
typename TElement>
371 std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<TElement>>>
375 TCallback&& callback) {
376 if (accessor.
type == Accessor::Type::SCALAR) {
380 if (accessor.
type == Accessor::Type::VEC2) {
382 AccessorView<AccessorTypes::VEC2<TElement>>(model, accessor));
384 if (accessor.
type == Accessor::Type::VEC3) {
386 AccessorView<AccessorTypes::VEC3<TElement>>(model, accessor));
388 if (accessor.
type == Accessor::Type::VEC4) {
390 AccessorView<AccessorTypes::VEC4<TElement>>(model, accessor));
392 if (accessor.
type == Accessor::Type::MAT2) {
394 AccessorView<AccessorTypes::MAT2<TElement>>(model, accessor));
396 if (accessor.
type == Accessor::Type::MAT3) {
398 AccessorView<AccessorTypes::MAT3<TElement>>(model, accessor));
400 if (accessor.
type == Accessor::Type::MAT4) {
402 AccessorView<AccessorTypes::MAT4<TElement>>(model, accessor));
405 return callback(AccessorView<AccessorTypes::SCALAR<TElement>>(
425 template <
typename TCallback>
426 std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<float>>>
430 TCallback&& callback) {
432 case Accessor::ComponentType::BYTE:
433 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int8_t>(
436 std::forward<TCallback>(callback));
437 case Accessor::ComponentType::UNSIGNED_BYTE:
438 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint8_t>(
441 std::forward<TCallback>(callback));
442 case Accessor::ComponentType::SHORT:
443 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int16_t>(
446 std::forward<TCallback>(callback));
447 case Accessor::ComponentType::UNSIGNED_SHORT:
448 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint16_t>(
451 std::forward<TCallback>(callback));
452 case Accessor::ComponentType::INT:
453 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int32_t>(
456 std::forward<TCallback>(callback));
457 case Accessor::ComponentType::UNSIGNED_INT:
458 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint32_t>(
461 std::forward<TCallback>(callback));
462 case Accessor::ComponentType::INT64:
463 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, int64_t>(
466 std::forward<TCallback>(callback));
467 case Accessor::ComponentType::UNSIGNED_INT64:
468 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, uint64_t>(
471 std::forward<TCallback>(callback));
472 case Accessor::ComponentType::FLOAT:
473 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, float>(
476 std::forward<TCallback>(callback));
477 case Accessor::ComponentType::DOUBLE:
478 return ::CesiumGltf::CesiumImpl::createAccessorView<TCallback, double>(
481 std::forward<TCallback>(callback));
504 template <
typename TCallback>
505 std::invoke_result_t<TCallback, AccessorView<AccessorTypes::SCALAR<float>>>
508 int32_t accessorIndex,
509 TCallback&& callback) {
A view on the data of one accessor of a glTF asset.
int64_t stride() const noexcept
Returns the stride of this accessor, which is the number of bytes from the start of one element to th...
int64_t size() const noexcept
Returns the size (number of elements) of this accessor.
AccessorView(AccessorViewStatus status=AccessorViewStatus::InvalidAccessorIndex)
Construct a new instance not pointing to any data.
AccessorView(const Model &model, int32_t accessorIndex) noexcept
Creates a new instance from a given model and accessor index.
const T & operator[](int64_t i) const
Provides the specified accessor element.
AccessorView(const std::byte *pData, int64_t stride, int64_t offset, int64_t size)
Creates a new instance from low-level parameters.
const std::byte * data() const noexcept
Returns a pointer to the first byte of this accessor view's data. The elements are stored contiguousl...
AccessorView(const Model &model, const Accessor &accessor) noexcept
Creates a new instance from a given model and Accessor.
AccessorViewStatus status() const noexcept
Gets the status of this accessor view.
T value_type
The type of the elements in the accessor.
Classes for working with glTF models.
std::invoke_result_t< TCallback, AccessorView< AccessorTypes::SCALAR< float > > > createAccessorView(const Model &model, const Accessor &accessor, TCallback &&callback)
Creates an appropriate AccessorView for a given accessor.
AccessorViewStatus
Indicates the status of an accessor view.
@ BufferViewTooSmall
The accessor is too large to fit in its bufferView.
@ Valid
This accessor is valid and ready to use.
@ InvalidBufferViewIndex
The accessor's bufferView index does not refer to a valid bufferView.
@ WrongSizeT
The sizeof(T) does not match the accessor's Accessor::computeBytesPerVertex.
@ InvalidBufferIndex
The accessor's bufferView's buffer index does not refer to a valid buffer.
@ InvalidAccessorIndex
The accessor index does not refer to a valid accessor.
@ BufferTooSmall
The accessor's bufferView is too large to fit in its buffer.
@ InvalidType
The Accessor:type is invalid.
@ InvalidComponentType
The Accessor::componentType is invalid.
std::string type
Specifies if the accessor's elements are scalars, vectors, or matrices.
int32_t componentType
The datatype of the accessor's components.
A 2x2 matrix element for an AccessorView.
T value[4]
The component values of this element.
A 3x3 matrix element for an AccessorView.
T value[9]
The component values of this element.
A 4x4 matrix element for an AccessorView.
T value[16]
The component values of this element.
A scalar element for an AccessorView.
T value[1]
The component values of this element.
A 2D vector element for an AccessorView.
T value[2]
The component values of this element.
A 3D vector element for an AccessorView.
T value[3]
The component values of this element.
A 4D vector element for an AccessorView.
T value[4]
The component values of this element.
Contains types that may optionally be used with AccessorView for various Accessor::componentType valu...
A typed view into a buffer view that contains raw binary data.
std::vector< std::byte > data
The buffer's data.
A view into a buffer generally representing a subset of the buffer.
int64_t byteLength
The length of the bufferView in bytes.
int32_t buffer
The index of the buffer.
int64_t byteOffset
The offset into the buffer in bytes.
A buffer points to binary geometry, animation, or skins.
BufferCesium cesium
Holds properties that are specific to the glTF loader rather than part of the glTF spec.
std::vector< CesiumGltf::Accessor > accessors
An array of accessors.
The root object for a glTF asset.
static const T & getSafe(const std::vector< T > &items, int32_t index)
Safely gets the element with a given index, returning a default instance if the index is outside the ...