NSUNI/NSLAR Library a250670
Loading...
Searching...
No Matches
vertexde.hpp
Go to the documentation of this file.
1
16
17#pragma once
18
21#include "NNL/common/io.hpp"
23namespace nnl {
34namespace vertexde {
52namespace fmt_code {
53constexpr u32 k0 = 0;
54
55constexpr u32 k8 = 1;
56constexpr u32 k16 = 2;
57constexpr u32 k32 = 3;
58
59constexpr u32 k565 = 4;
60constexpr u32 k5551 = 5;
61constexpr u32 k4444 = 6;
62constexpr u32 k8888 = 7;
63} // namespace fmt_code
73namespace fmt_shift {
74constexpr u32 kUV = 0;
75
76constexpr u32 kColor = 2;
77
78constexpr u32 kNormal = 5;
79
80constexpr u32 kPosition = 7;
81
82constexpr u32 kWeight = 9;
83
84constexpr u32 kIndex = 11;
85
86constexpr u32 kWeightNum = 14;
87
88constexpr u32 kMorphNum = 18;
89
90constexpr u32 kThrough = 23;
91} // namespace fmt_shift
100namespace fmt_mask {
101constexpr u32 kUV = 0b0'00'000'0'000'0'00'00'00'00'000'11;
102
103constexpr u32 kColor = 0b0'00'000'0'000'0'00'00'00'00'111'00;
104
105constexpr u32 kNormal = 0b0'00'000'0'000'0'00'00'00'11'000'00;
106
107constexpr u32 kPosition = 0b0'00'000'0'000'0'00'00'11'00'000'00;
108
109constexpr u32 kWeight = 0b0'00'000'0'000'0'00'11'00'00'000'00;
110
111constexpr u32 kIndex = 0b0'00'000'0'000'0'11'00'00'00'000'00;
112
113constexpr u32 kWeightNum = 0b0'00'000'0'111'0'00'00'00'00'000'00;
114
115constexpr u32 kMorphNum = 0b0'00'111'0'000'0'00'00'00'00'000'00;
116
117constexpr u32 kThrough = 0b1'00'000'0'000'0'00'00'00'00'000'00;
118} // namespace fmt_mask
120
126
145namespace fmt {
149constexpr u32 kUV8 = fmt_code::k8 << fmt_shift::kUV;
153constexpr u32 kUV16 = fmt_code::k16 << fmt_shift::kUV;
157constexpr u32 kUV32 = fmt_code::k32 << fmt_shift::kUV;
161constexpr u32 kColor565 = fmt_code::k565 << fmt_shift::kColor;
165constexpr u32 kColor5551 = fmt_code::k5551 << fmt_shift::kColor;
169constexpr u32 kColor4444 = fmt_code::k4444 << fmt_shift::kColor;
173constexpr u32 kColor8888 = fmt_code::k8888 << fmt_shift::kColor;
177constexpr u32 kNormal8 = fmt_code::k8 << fmt_shift::kNormal;
181constexpr u32 kNormal16 = fmt_code::k16 << fmt_shift::kNormal;
185constexpr u32 kNormal32 = fmt_code::k32 << fmt_shift::kNormal;
189constexpr u32 kPosition8 = fmt_code::k8 << fmt_shift::kPosition;
193constexpr u32 kPosition16 = fmt_code::k16 << fmt_shift::kPosition;
197constexpr u32 kPosition32 = fmt_code::k32 << fmt_shift::kPosition;
202constexpr u32 kWeight8 = fmt_code::k8 << fmt_shift::kWeight;
207constexpr u32 kWeight16 = fmt_code::k16 << fmt_shift::kWeight;
212constexpr u32 kWeight32 = fmt_code::k32 << fmt_shift::kWeight;
221constexpr u32 kIndex8 = fmt_code::k8 << fmt_shift::kIndex;
227constexpr u32 kIndex16 = fmt_code::k16 << fmt_shift::kIndex;
228
229// kIndex32: technically exists but unused
230
242constexpr u32 kThrough = 1 << fmt_shift::kThrough;
243
259constexpr inline u32 kWeightNum(u32 weight_num) {
260 NNL_EXPECTS_DBG(weight_num > 0 && weight_num <= 8);
261 return ((weight_num - 1) << fmt_shift::kWeightNum);
262}
263
283constexpr inline u32 kMorphNum(u32 morph_num) {
284 NNL_EXPECTS_DBG(morph_num > 0 && morph_num <= 8);
285 return ((morph_num - 1) << fmt_shift::kMorphNum);
286}
287} // namespace fmt
288
309std::vector<SVertex> Decode(BufferView vertex_buffer, u32 vertex_format, std::array<u16, 8> bone_indices = {0});
326Buffer Encode(const std::vector<SVertex>& vertices, u32 vertex_format, std::array<u16, 8> bone_indices = {0}); // Main
328
334
340constexpr std::array<u32, 8> format_sizes{0, 1, 2, 4, 2, 2, 2, 4};
341
342// through
351inline u32 GetThrough(u32 vertex_format) { return (vertex_format & fmt_mask::kThrough) >> fmt_shift::kThrough; }
352
353// WEIGHTS
361inline u32 GetWeightFormat(u32 vertex_format) { return (vertex_format & fmt_mask::kWeight) >> fmt_shift::kWeight; }
370inline u32 GetWeightSize(u32 vertex_format) { return format_sizes.at(GetWeightFormat(vertex_format)); }
378inline u32 GetWeightNum(u32 vertex_format) {
379 return GetWeightFormat(vertex_format) != 0 ? ((vertex_format & fmt_mask::kWeightNum) >> fmt_shift::kWeightNum) + 1
380 : 0;
381}
382
383// UV
390inline u32 GetUVFormat(u32 vertex_format) { return (vertex_format & fmt_mask::kUV) >> fmt_shift::kUV; }
398inline u32 GetUVSize(u32 vertex_format) { return format_sizes.at(GetUVFormat(vertex_format)); }
404inline u32 GetUVNum(u32 vertex_format) { return GetUVFormat(vertex_format) != 0 ? 2 : 0; }
405
406// COLOR
414inline u32 GetColorFormat(u32 vertex_format) { return (vertex_format & fmt_mask::kColor) >> fmt_shift::kColor; }
421inline u32 GetColorSize(u32 vertex_format) { return format_sizes.at(GetColorFormat(vertex_format)); }
427inline u32 GetColorNum(u32 vertex_format) { return GetColorFormat(vertex_format) != 0 ? 1 : 0; }
428
429// NORMAL
437inline u32 GetNormalFormat(u32 vertex_format) { return (vertex_format & fmt_mask::kNormal) >> fmt_shift::kNormal; }
445inline u32 GetNormalSize(u32 vertex_format) { return format_sizes.at(GetNormalFormat(vertex_format)); }
453inline u32 GetNormalNum(u32 vertex_format) { return GetNormalFormat(vertex_format) != 0 ? 3 : 0; }
454
455// POSITION
463inline u32 GetPositionFormat(u32 vertex_format) {
464 return (vertex_format & fmt_mask::kPosition) >> fmt_shift::kPosition;
465}
466
475inline u32 GetPositionSize(u32 vertex_format) { return format_sizes.at(GetPositionFormat(vertex_format)); }
485inline u32 GetPositionNum(u32 vertex_format) { return GetPositionFormat(vertex_format) != 0 ? 3 : 0; }
486
487// MORPHING
495inline u32 GetMorphNum(u32 vertex_format) {
496 return ((vertex_format & fmt_mask::kMorphNum) >> fmt_shift::kMorphNum) + 1;
497}
498
499// INDEX
508inline u32 GetIndexFormat(u32 vertex_format) { return (vertex_format & fmt_mask::kIndex) >> fmt_shift::kIndex; }
516inline u32 GetIndexSize(u32 vertex_format) { return format_sizes.at(GetIndexFormat(vertex_format)); }
517
518// HAS
525inline bool HasWeights(u32 vertex_format) { return static_cast<bool>(GetWeightFormat(vertex_format)); }
532inline bool HasUV(u32 vertex_format) { return static_cast<bool>(GetUVFormat(vertex_format)); }
540inline bool HasColor(u32 vertex_format) { return static_cast<bool>(GetColorFormat(vertex_format)); }
548inline bool HasNormal(u32 vertex_format) { return static_cast<bool>(GetNormalFormat(vertex_format)); }
557inline bool HasPosition(u32 vertex_format) { return static_cast<bool>(GetPositionFormat(vertex_format)); }
558
559// IS
569inline bool IsIndexed(u32 vertex_format) { return static_cast<bool>(GetIndexFormat(vertex_format)); }
570
580inline bool IsThrough(u32 vertex_format) { return static_cast<bool>(GetThrough(vertex_format)); }
581
582// INDICES
590inline bool Is8Indices(u32 vertex_format) {
591 return (vertexde::GetIndexFormat(vertex_format) == vertexde::fmt_code::k8);
592}
593
599inline bool Is16Indices(u32 vertex_format) {
600 return (vertexde::GetIndexFormat(vertex_format) == vertexde::fmt_code::k16);
601}
602
603// WEIGHTS
613inline bool Is8Weights(u32 vertex_format) {
614 return (vertexde::GetWeightFormat(vertex_format) == vertexde::fmt_code::k8);
615}
616
625inline bool Is16Weights(u32 vertex_format) {
626 return (vertexde::GetWeightFormat(vertex_format) == vertexde::fmt_code::k16);
627}
628
638inline bool Is32Weights(u32 vertex_format) {
639 return (vertexde::GetWeightFormat(vertex_format) == vertexde::fmt_code::k32);
640}
641
650inline bool IsFixedWeights(u32 vertex_format) { return (Is8Weights(vertex_format) || Is16Weights(vertex_format)); }
651
652// UV
662inline bool Is8UV(u32 vertex_format) { return (vertexde::GetUVFormat(vertex_format) == vertexde::fmt_code::k8); }
672inline bool Is16UV(u32 vertex_format) { return (vertexde::GetUVFormat(vertex_format) == vertexde::fmt_code::k16); }
682inline bool Is32UV(u32 vertex_format) { return (vertexde::GetUVFormat(vertex_format) == vertexde::fmt_code::k32); }
692inline bool IsFixedUV(u32 vertex_format) { return (Is8UV(vertex_format) || Is16UV(vertex_format)); }
693
694// COLOR
701inline bool Is565Color(u32 vertex_format) {
702 return (vertexde::GetColorFormat(vertex_format) == vertexde::fmt_code::k565);
703}
704
710inline bool Is5551Color(u32 vertex_format) {
711 return (vertexde::GetColorFormat(vertex_format) == vertexde::fmt_code::k5551);
712}
713
719inline bool Is4444Color(u32 vertex_format) {
720 return (vertexde::GetColorFormat(vertex_format) == vertexde::fmt_code::k4444);
721}
722
728inline bool Is8888Color(u32 vertex_format) {
729 return (vertexde::GetColorFormat(vertex_format) == vertexde::fmt_code::k8888);
730}
731
732// NORMAL
733
743inline bool Is8Normal(u32 vertex_format) {
744 return (vertexde::GetNormalFormat(vertex_format) == vertexde::fmt_code::k8);
745}
746
755inline bool Is16Normal(u32 vertex_format) {
756 return (vertexde::GetNormalFormat(vertex_format) == vertexde::fmt_code::k16);
757}
758
767inline bool Is32Normal(u32 vertex_format) {
768 return (vertexde::GetNormalFormat(vertex_format) == vertexde::fmt_code::k32);
769}
770
777inline bool IsFixedNormal(u32 vertex_format) { return (Is8Normal(vertex_format) || Is16Normal(vertex_format)); }
778
779// POSITION
789inline bool Is8Position(u32 vertex_format) {
790 return (vertexde::GetPositionFormat(vertex_format) == vertexde::fmt_code::k8);
791}
792
801inline bool Is16Position(u32 vertex_format) {
802 return (vertexde::GetPositionFormat(vertex_format) == vertexde::fmt_code::k16);
803}
804
813inline bool Is32Position(u32 vertex_format) {
814 return (vertexde::GetPositionFormat(vertex_format) == vertexde::fmt_code::k32);
815}
816
825inline bool IsFixedPosition(u32 vertex_format) { return (Is8Position(vertex_format) || Is16Position(vertex_format)); }
835std::string GetDescription(u32 vertex_format);
847 u32 offset_weights = 0;
848 u32 offset_uv = 0;
849 u32 offset_color = 0;
850 u32 offset_normal = 0;
851 u32 offset_position = 0;
852 u32 vertex_size = 0;
853};
854
870
871} // namespace vertexde
872
873} // namespace nnl
Defines macros for Design-by-Contract verification.
Contains macros and definitions for fixed-width types.
#define NNL_EXPECTS_DBG(precondition)
Debug-only precondition check.
Definition contract.hpp:59
std::uint32_t u32
32-bit unsigned integer
Definition fixed_type.hpp:60
Reader implementation for read-only memory buffers.
Definition io.hpp:598
std::vector< u8 > Buffer
A type alias for std::vector<u8> that denotes a raw, contiguous memory region that may be interpreted...
Definition io.hpp:40
Structure representing the layout of a vertex.
Definition vertexde.hpp:846
bool Is16Normal(u32 vertex_format)
Checks if the vertex format uses 16-bit normal components.
Definition vertexde.hpp:755
u32 GetColorSize(u32 vertex_format)
Retrieves the byte size of a vertex color from the vertex format.
Definition vertexde.hpp:421
u32 GetUVNum(u32 vertex_format)
Retrieves the number of UV components.
Definition vertexde.hpp:404
bool Is8Position(u32 vertex_format)
Checks if the vertex format uses 8-bit positions.
Definition vertexde.hpp:789
u32 GetColorNum(u32 vertex_format)
Retrieves the number of colors.
Definition vertexde.hpp:427
constexpr std::array< u32, 8 > format_sizes
Maps format codes to attribute sizes.
Definition vertexde.hpp:340
u32 GetColorFormat(u32 vertex_format)
Retrieves the color format code from the vertex format.
Definition vertexde.hpp:414
bool HasUV(u32 vertex_format)
Checks if the vertex format includes UV information.
Definition vertexde.hpp:532
bool IsIndexed(u32 vertex_format)
Checks if the vertex format uses indexed drawing.
Definition vertexde.hpp:569
u32 GetNormalFormat(u32 vertex_format)
Retrieves the normal format code from the vertex format.
Definition vertexde.hpp:437
bool Is8Indices(u32 vertex_format)
Checks if the vertex format uses 8-bit indices.
Definition vertexde.hpp:590
u32 GetWeightSize(u32 vertex_format)
Retrieves the byte size of a weight component from the vertex format.
Definition vertexde.hpp:370
bool IsThrough(u32 vertex_format)
Checks if the vertex format uses Through Mode (2D drawing).
Definition vertexde.hpp:580
u32 GetIndexSize(u32 vertex_format)
Retrieves the byte size of a single index from the vertex format.
Definition vertexde.hpp:516
bool IsFixedWeights(u32 vertex_format)
Checks if the vertex format uses fixed-point weights.
Definition vertexde.hpp:650
u32 GetUVFormat(u32 vertex_format)
Retrieves the UV format code from the vertex format.
Definition vertexde.hpp:390
u32 GetIndexFormat(u32 vertex_format)
Retrieves the index format code from the vertex format.
Definition vertexde.hpp:508
u32 GetUVSize(u32 vertex_format)
Retrieves the byte size of a UV component from the vertex format.
Definition vertexde.hpp:398
bool IsFixedNormal(u32 vertex_format)
Checks if the vertex format uses fixed-point normals.
Definition vertexde.hpp:777
bool HasPosition(u32 vertex_format)
Checks if the vertex format includes position information.
Definition vertexde.hpp:557
u32 GetPositionNum(u32 vertex_format)
Retrieves the number of position components.
Definition vertexde.hpp:485
bool Is4444Color(u32 vertex_format)
Checks if the vertex format uses 4444 vertex colors.
Definition vertexde.hpp:719
u32 GetPositionSize(u32 vertex_format)
Retrieves the byte size of a position component from the vertex format.
Definition vertexde.hpp:475
bool Is32Position(u32 vertex_format)
Checks if the vertex format uses float positions.
Definition vertexde.hpp:813
u32 GetMorphNum(u32 vertex_format)
Retrieves the number of morph targets from the vertex format.
Definition vertexde.hpp:495
u32 GetNormalSize(u32 vertex_format)
Retrieves the byte size of a normal component from the vertex format.
Definition vertexde.hpp:445
bool IsFixedUV(u32 vertex_format)
Checks if the vertex format uses fixed-point UV components.
Definition vertexde.hpp:692
u32 GetWeightNum(u32 vertex_format)
Retrieves the number of weight components from the vertex format.
Definition vertexde.hpp:378
bool Is8Weights(u32 vertex_format)
Checks if the vertex format uses 8-bit weights.
Definition vertexde.hpp:613
u32 GetThrough(u32 vertex_format)
Retrieves the Through Mode status from the vertex format.
Definition vertexde.hpp:351
u32 GetNormalNum(u32 vertex_format)
Retrieves the number of normal components from the vertex format.
Definition vertexde.hpp:453
bool Is8888Color(u32 vertex_format)
Checks if the vertex format uses 8888 vertex colors.
Definition vertexde.hpp:728
bool Is16Weights(u32 vertex_format)
Checks if the vertex format uses 16-bit weights.
Definition vertexde.hpp:625
bool HasWeights(u32 vertex_format)
Checks if the vertex format includes weight information.
Definition vertexde.hpp:525
bool IsFixedPosition(u32 vertex_format)
Checks if the vertex format uses fixed-point positions.
Definition vertexde.hpp:825
bool Is32Weights(u32 vertex_format)
Checks if the vertex format uses float weights.
Definition vertexde.hpp:638
bool Is32Normal(u32 vertex_format)
Checks if the vertex format uses float normals.
Definition vertexde.hpp:767
bool Is32UV(u32 vertex_format)
Checks if the vertex format uses float UVs.
Definition vertexde.hpp:682
bool Is565Color(u32 vertex_format)
Checks if the vertex format uses 565 vertex colors.
Definition vertexde.hpp:701
bool Is16UV(u32 vertex_format)
Checks if the vertex format uses 16-bit UVs.
Definition vertexde.hpp:672
bool Is16Indices(u32 vertex_format)
Checks if the vertex format uses 16-bit indices.
Definition vertexde.hpp:599
bool HasNormal(u32 vertex_format)
Checks if the vertex format includes normal information.
Definition vertexde.hpp:548
u32 GetPositionFormat(u32 vertex_format)
Retrieves the position format code from the vertex format.
Definition vertexde.hpp:463
bool Is8UV(u32 vertex_format)
Checks if the vertex format uses 8-bit UVs.
Definition vertexde.hpp:662
bool Is5551Color(u32 vertex_format)
Checks if the vertex format uses 5551 vertex colors.
Definition vertexde.hpp:710
VertexLayout GetLayout(u32 vertex_format)
Retrieves the layout of a vertex based on its format.
u32 GetWeightFormat(u32 vertex_format)
Retrieves the weight format code from the vertex format.
Definition vertexde.hpp:361
bool HasColor(u32 vertex_format)
Checks if the vertex format includes color information.
Definition vertexde.hpp:540
bool Is8Normal(u32 vertex_format)
Checks if the vertex format uses 8-bit normals.
Definition vertexde.hpp:743
bool Is16Position(u32 vertex_format)
Checks if the vertex format uses 16-bit positions.
Definition vertexde.hpp:801
std::string GetDescription(u32 vertex_format)
Generates a textual description of the vertex format.
std::vector< SVertex > Decode(BufferView vertex_buffer, u32 vertex_format, std::array< u16, 8 > bone_indices={0})
Extracts vertex data from a buffer and converts it to a simple representation.
Buffer Encode(const std::vector< SVertex > &vertices, u32 vertex_format, std::array< u16, 8 > bone_indices={0})
Converts vertex data from a simple representation to a binary vertex buffer.
Provides classes for reading and writing binary data to and from various sources.
This namespace defines constants that represent fundamental format codes.
constexpr u32 k8888
4 byte color
Definition vertexde.hpp:62
constexpr u32 k565
2 byte color
Definition vertexde.hpp:59
constexpr u32 k8
1 byte component
Definition vertexde.hpp:55
constexpr u32 k32
4 byte component
Definition vertexde.hpp:57
constexpr u32 k0
no component
Definition vertexde.hpp:53
constexpr u32 k4444
2 byte color
Definition vertexde.hpp:61
constexpr u32 k5551
2 byte color
Definition vertexde.hpp:60
constexpr u32 k16
2 byte component
Definition vertexde.hpp:56
This namespace contains constants representing bit masks for parts of the vertex format.
This namespace contains constants representing shift values that are applied to format codes to creat...
This namespace contains constants and functions for defining the vertex format.
constexpr u32 kIndex16
unsigned 16-bit indices (in index buffers)
Definition vertexde.hpp:227
constexpr u32 kMorphNum(u32 morph_num)
Generates a constant that encodes the number of morph targets.
Definition vertexde.hpp:283
constexpr u32 kPosition16
16-bit fixed-point vertex coordinates (signed).
Definition vertexde.hpp:193
constexpr u32 kPosition32
floating-point vertex coordinates.
Definition vertexde.hpp:197
constexpr u32 kWeight16
16-bit fixed-point bone weights (unsigned).
Definition vertexde.hpp:207
constexpr u32 kNormal32
floating-point vertex normal.
Definition vertexde.hpp:185
constexpr u32 kWeight32
floating-point bone weights.
Definition vertexde.hpp:212
constexpr u32 kUV16
16-bit fixed-point texture coordinates (unsigned)
Definition vertexde.hpp:153
constexpr u32 kIndex8
unsigned 8-bit indices (in index buffers)
Definition vertexde.hpp:221
constexpr u32 kWeightNum(u32 weight_num)
Generates a constant that encodes the number of weights.
Definition vertexde.hpp:259
constexpr u32 kNormal16
16-bit fixed-point vertex normal (signed).
Definition vertexde.hpp:181
constexpr u32 kColor5551
2-byte color format. 1 bit for the alpha channel.
Definition vertexde.hpp:165
constexpr u32 kUV8
8-bit fixed-point texture coordinates (unsigned)
Definition vertexde.hpp:149
constexpr u32 kColor4444
2-byte color format. 4 bits per channel.
Definition vertexde.hpp:169
constexpr u32 kWeight8
8-bit fixed-point bone weights (unsigned).
Definition vertexde.hpp:202
constexpr u32 kUV32
floating-point texture coordinates
Definition vertexde.hpp:157
constexpr u32 kThrough
Indicates whether the 2D drawing mode is used.
Definition vertexde.hpp:242
constexpr u32 kColor8888
4-byte color format. 8 bits per channel.
Definition vertexde.hpp:173
constexpr u32 kColor565
2-byte color format. No alpha channel.
Definition vertexde.hpp:161
constexpr u32 kNormal8
8-bit fixed-point vertex normal (signed).
Definition vertexde.hpp:177
constexpr u32 kPosition8
8-bit fixed-point vertex coordinates (signed).
Definition vertexde.hpp:189
Contains functions that facilitate conversion of vertex data.
Definition vertexde.hpp:34
Definition exception.hpp:56
Provides data structures and functions for managing essential components of a 3D model.