git.s-ol.nu ~forks/DiligentTools / dca9874
GLTF Loader: updated primitives handling assiduous 1 year, 1 month ago
2 changed file(s) with 38 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
144144
145145 struct Primitive
146146 {
147 Uint32 FirstIndex = 0;
148 Uint32 IndexCount = 0;
149 Uint32 VertexCount = 0;
150 Material& material;
151 bool hasIndices;
152
153 BoundBox BB;
154 bool IsValidBB = false;
155
156 Primitive(Uint32 _FirstIndex,
157 Uint32 _IndexCount,
158 Uint32 _VertexCount,
159 Material& _material) :
147 const Uint32 FirstIndex;
148 const Uint32 IndexCount;
149 const Uint32 VertexCount;
150 const Uint32 MaterialId;
151
152 const BoundBox BB;
153
154 Primitive(Uint32 _FirstIndex,
155 Uint32 _IndexCount,
156 Uint32 _VertexCount,
157 Uint32 _MaterialId,
158 const float3& BBMin,
159 const float3& BBMax) :
160160 FirstIndex{_FirstIndex},
161161 IndexCount{_IndexCount},
162162 VertexCount{_VertexCount},
163 material{_material},
164 hasIndices{_IndexCount > 0}
165 {
166 }
167
168 void SetBoundingBox(const float3& min, const float3& max)
169 {
170 BB.Min = min;
171 BB.Max = max;
172 IsValidBB = true;
163 MaterialId{_MaterialId},
164 BB{BBMin, BBMax}
165 {
166 }
167
168 Primitive(Primitive&&) = default;
169
170 bool HasIndices() const
171 {
172 return IndexCount > 0;
173173 }
174174 };
175175
177177
178178 struct Mesh
179179 {
180 std::vector<std::unique_ptr<Primitive>> Primitives;
180 std::vector<Primitive> Primitives;
181181
182182 BoundBox BB;
183183
219219 Skin* _Skin = nullptr;
220220 Int32 SkinIndex = -1;
221221 float3 Translation;
222 float3 Scale = float3(1.0f, 1.0f, 1.0f);
222 float3 Scale = float3{1, 1, 1};
223223 Quaternion Rotation;
224224 BoundBox BVH;
225225 BoundBox AABB;
530530 return;
531531 }
532532 }
533 std::unique_ptr<Primitive> newPrimitive(
534 new Primitive //
535 {
536 indexStart,
537 indexCount,
538 vertexCount,
539 primitive.material > -1 ? Materials[primitive.material] : Materials.back() //
540 } //
533 NewMesh->Primitives.emplace_back( //
534 indexStart,
535 indexCount,
536 vertexCount,
537 primitive.material >= 0 ? static_cast<Uint32>(primitive.material) : static_cast<Uint32>(Materials.size() - 1),
538 PosMin,
539 PosMax
540 //
541541 );
542
543 newPrimitive->SetBoundingBox(PosMin, PosMax);
544 NewMesh->Primitives.push_back(std::move(newPrimitive));
545542 }
546543
547544 // Mesh BB from BBs of primitives
548545 for (const auto& prim : NewMesh->Primitives)
549546 {
550 if (prim->IsValidBB && !NewMesh->IsValidBB)
551 {
552 NewMesh->BB = prim->BB;
547 if (!NewMesh->IsValidBB)
548 {
549 NewMesh->BB = prim.BB;
553550 NewMesh->IsValidBB = true;
554551 }
555 float3 bb_min = std::min(NewMesh->BB.Min, prim->BB.Min);
556 float3 bb_max = std::max(NewMesh->BB.Max, prim->BB.Max);
552 float3 bb_min = std::min(NewMesh->BB.Min, prim.BB.Min);
553 float3 bb_max = std::max(NewMesh->BB.Max, prim.BB.Max);
557554 NewMesh->SetBoundingBox(bb_min, bb_max);
558555 }
559556 NewNode->_Mesh = std::move(NewMesh);