Skip to content

Commit 78edfa3

Browse files
author
mcdev
committed
Adds Int16/Uint16 -> Int8/Uint8 converters for multiple vertex color channels support.
1 parent 28edd2f commit 78edfa3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

AssetLoader/src/GLTFBuilder.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ inline float ConvertElement<float, true, Uint16>(Uint16 Src)
118118
}
119119

120120

121+
// ========================== Int16/Uint16 -> Int8/Uint8 ===========================
122+
template <>
123+
inline Uint8 ConvertElement<Uint8, true, Uint16>(Uint16 Src)
124+
{
125+
return static_cast<Uint8>((static_cast<Uint32>(Src) * 255 + 128) / 65535);
126+
}
127+
128+
template <>
129+
inline Int8 ConvertElement<Int8, true, Int16>(Int16 Src)
130+
{
131+
// Scale from [-32768, 32767] ? [-128, 127] with rounding
132+
// Use 32-bit math to avoid overflow
133+
Int32 temp = static_cast<Int32>(Src) * 127; // multiply first
134+
temp += (Src >= 0 ? 16384 : -16384); // add 0.5 for rounding
135+
temp /= 32767; // scale down
136+
return static_cast<Int8>(temp);
137+
}
138+
139+
121140
template <typename SrcType, typename DstType, bool IsNormalized>
122141
inline void WriteGltfData(const void* pSrc,
123142
Uint32 NumComponents,

0 commit comments

Comments
 (0)