Plasma Engine  2.0
Loading...
Searching...
No Matches
MeshBufferUtils_inl.h
1
2// static
3PL_ALWAYS_INLINE plGALResourceFormat::Enum plMeshNormalPrecision::ToResourceFormatNormal(Enum value)
4{
5 return value == _10Bit ? plGALResourceFormat::RGB10A2UIntNormalized
6 : (value == _16Bit ? plGALResourceFormat::RGBAUShortNormalized : plGALResourceFormat::XYZFloat);
7}
8
9// static
10PL_ALWAYS_INLINE plGALResourceFormat::Enum plMeshNormalPrecision::ToResourceFormatTangent(Enum value)
11{
12 return value == _10Bit ? plGALResourceFormat::RGB10A2UIntNormalized
13 : (value == _16Bit ? plGALResourceFormat::RGBAUShortNormalized : plGALResourceFormat::XYZWFloat);
14}
15
17
18// static
19PL_ALWAYS_INLINE plGALResourceFormat::Enum plMeshTexCoordPrecision::ToResourceFormat(Enum value)
20{
21 return value == _16Bit ? plGALResourceFormat::UVHalf : plGALResourceFormat::UVFloat;
22}
23
25
26// static
27PL_ALWAYS_INLINE plGALResourceFormat::Enum plMeshBoneWeigthPrecision::ToResourceFormat(Enum value)
28{
29 switch (value)
30 {
31 case _8Bit:
32 return plGALResourceFormat::RGBAUByteNormalized;
33 case _10Bit:
34 return plGALResourceFormat::RGB10A2UIntNormalized;
35 case _16Bit:
36 return plGALResourceFormat::RGBAUShortNormalized;
37 case _32Bit:
38 return plGALResourceFormat::RGBAFloat;
39 PL_DEFAULT_CASE_NOT_IMPLEMENTED;
40 }
41
42 return plGALResourceFormat::RGBAUByteNormalized;
43}
44
46
47// static
48PL_ALWAYS_INLINE plResult plMeshBufferUtils::EncodeNormal(const plVec3& vNormal, plArrayPtr<plUInt8> dest, plMeshNormalPrecision::Enum normalPrecision)
49{
50 return EncodeNormal(vNormal, dest, plMeshNormalPrecision::ToResourceFormatNormal(normalPrecision));
51}
52
53// static
54PL_ALWAYS_INLINE plResult plMeshBufferUtils::EncodeTangent(const plVec3& vTangent, float fTangentSign, plArrayPtr<plUInt8> dest, plMeshNormalPrecision::Enum tangentPrecision)
55{
56 return EncodeTangent(vTangent, fTangentSign, dest, plMeshNormalPrecision::ToResourceFormatTangent(tangentPrecision));
57}
58
59// static
60PL_ALWAYS_INLINE plResult plMeshBufferUtils::EncodeTexCoord(const plVec2& vTexCoord, plArrayPtr<plUInt8> dest, plMeshTexCoordPrecision::Enum texCoordPrecision)
61{
62 return EncodeTexCoord(vTexCoord, dest, plMeshTexCoordPrecision::ToResourceFormat(texCoordPrecision));
63}
64
65// static
66PL_ALWAYS_INLINE plResult plMeshBufferUtils::EncodeBoneWeights(const plVec4& vWeights, plArrayPtr<plUInt8> dest, plMeshBoneWeigthPrecision::Enum precision)
67{
68 return EncodeBoneWeights(vWeights, dest, plMeshBoneWeigthPrecision::ToResourceFormat(precision));
69}
70
71// static
72PL_ALWAYS_INLINE plResult plMeshBufferUtils::EncodeColor(const plVec4& vColor, plArrayPtr<plUInt8> dest, plMeshVertexColorConversion::Enum conversion)
73{
74 plVec4 finalColor;
75 if (conversion == plMeshVertexColorConversion::LinearToSrgb)
76 {
77 finalColor = plColor::LinearToGamma(vColor.GetAsVec3()).GetAsVec4(vColor.w);
78 }
79 else if (conversion == plMeshVertexColorConversion::SrgbToLinear)
80 {
81 finalColor = plColor::GammaToLinear(vColor.GetAsVec3()).GetAsVec4(vColor.w);
82 }
83 else
84 {
85 finalColor = vColor;
86 }
87
88 return EncodeFromVec4(finalColor, dest, plGALResourceFormat::RGBAUByteNormalized);
89}
90
91// static
92PL_ALWAYS_INLINE plResult plMeshBufferUtils::EncodeNormal(const plVec3& vNormal, plArrayPtr<plUInt8> dest, plGALResourceFormat::Enum destFormat)
93{
94 // we store normals in unsigned formats thus we need to map from -1..1 to 0..1 here
95 return EncodeFromVec3(vNormal * 0.5f + plVec3(0.5f), dest, destFormat);
96}
97
98// static
99PL_ALWAYS_INLINE plResult plMeshBufferUtils::EncodeTangent(const plVec3& vTangent, float fTangentSign, plArrayPtr<plUInt8> dest, plGALResourceFormat::Enum destFormat)
100{
101 // make sure biTangentSign is either -1 or 1
102 fTangentSign = (fTangentSign < 0.0f) ? -1.0f : 1.0f;
103
104 // we store tangents in unsigned formats thus we need to map from -1..1 to 0..1 here
105 return EncodeFromVec4(vTangent.GetAsVec4(fTangentSign) * 0.5f + plVec4(0.5f), dest, destFormat);
106}
107
108// static
109PL_ALWAYS_INLINE plResult plMeshBufferUtils::EncodeTexCoord(const plVec2& vTexCoord, plArrayPtr<plUInt8> dest, plGALResourceFormat::Enum destFormat)
110{
111 return EncodeFromVec2(vTexCoord, dest, destFormat);
112}
113
114// static
115PL_ALWAYS_INLINE plResult plMeshBufferUtils::EncodeBoneWeights(const plVec4& vWeights, plArrayPtr<plUInt8> dest, plGALResourceFormat::Enum destFormat)
116{
117 return EncodeFromVec4(vWeights, dest, destFormat);
118}
119
120// static
121PL_ALWAYS_INLINE plResult plMeshBufferUtils::DecodeNormal(plArrayPtr<const plUInt8> source, plVec3& ref_vDestNormal, plMeshNormalPrecision::Enum normalPrecision)
122{
123 return DecodeNormal(source, plMeshNormalPrecision::ToResourceFormatNormal(normalPrecision), ref_vDestNormal);
124}
125
126// static
127PL_ALWAYS_INLINE plResult plMeshBufferUtils::DecodeTangent(plArrayPtr<const plUInt8> source, plVec3& ref_vDestTangent, float& ref_fDestBiTangentSign, plMeshNormalPrecision::Enum tangentPrecision)
128{
129 return DecodeTangent(source, plMeshNormalPrecision::ToResourceFormatTangent(tangentPrecision), ref_vDestTangent, ref_fDestBiTangentSign);
130}
131
132// static
133PL_ALWAYS_INLINE plResult plMeshBufferUtils::DecodeTexCoord(plArrayPtr<const plUInt8> source, plVec2& ref_vDestTexCoord, plMeshTexCoordPrecision::Enum texCoordPrecision)
134{
135 return DecodeTexCoord(source, plMeshTexCoordPrecision::ToResourceFormat(texCoordPrecision), ref_vDestTexCoord);
136}
137
138// static
139PL_ALWAYS_INLINE plResult plMeshBufferUtils::DecodeNormal(plArrayPtr<const plUInt8> source, plGALResourceFormat::Enum sourceFormat, plVec3& ref_vDestNormal)
140{
141 plVec3 tempNormal;
142 PL_SUCCEED_OR_RETURN(DecodeToVec3(source, sourceFormat, tempNormal));
143 ref_vDestNormal = tempNormal * 2.0f - plVec3(1.0f);
144 return PL_SUCCESS;
145}
146
147// static
148PL_ALWAYS_INLINE plResult plMeshBufferUtils::DecodeTangent(plArrayPtr<const plUInt8> source, plGALResourceFormat::Enum sourceFormat, plVec3& ref_vDestTangent, float& ref_fDestBiTangentSign)
149{
150 plVec4 tempTangent;
151 PL_SUCCEED_OR_RETURN(DecodeToVec4(source, sourceFormat, tempTangent));
152 ref_vDestTangent = tempTangent.GetAsVec3() * 2.0f - plVec3(1.0f);
153 ref_fDestBiTangentSign = tempTangent.w * 2.0f - 1.0f;
154 return PL_SUCCESS;
155}
156
157// static
158PL_ALWAYS_INLINE plResult plMeshBufferUtils::DecodeTexCoord(plArrayPtr<const plUInt8> source, plGALResourceFormat::Enum sourceFormat, plVec2& ref_vDestTexCoord)
159{
160 return DecodeToVec2(source, sourceFormat, ref_vDestTexCoord);
161}
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
static float GammaToLinear(float fGamma)
Helper function to convert a float color value from gamma space to linear color space.
Definition Color.cpp:283
static float LinearToGamma(float fGamma)
Helper function to convert a float color value from linear space to gamma color space.
Definition Color.cpp:288
const plVec4Template< Type > GetAsVec4(Type w) const
Returns an plVec4Template with x,y,z from this vector and w set to the parameter.
Definition Vec4_inl.h:35
const plVec3Template< Type > GetAsVec3() const
Returns an plVec3Template with x,y and z from this vector.
Definition Vec4_inl.h:112
static plGALResourceFormat::Enum ToResourceFormat(Enum value)
Convert mesh texcoord precision to actual resource format.
Definition MeshBufferUtils_inl.h:27
static plGALResourceFormat::Enum ToResourceFormatNormal(Enum value)
Convert mesh normal precision to actual resource format used for normals.
Definition MeshBufferUtils_inl.h:3
static plGALResourceFormat::Enum ToResourceFormatTangent(Enum value)
Convert mesh normal precision to actual resource format used for tangents.
Definition MeshBufferUtils_inl.h:10
static plGALResourceFormat::Enum ToResourceFormat(Enum value)
Convert mesh texcoord precision to actual resource format.
Definition MeshBufferUtils_inl.h:19
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54