Berserk
Mesh.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of Berserk Engine project */
3 /* https://github.com/EgorOrachyov/Berserk */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2018 - 2021 Egor Orachyov */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef BERSERK_MESH_HPP
29 #define BERSERK_MESH_HPP
30 
31 #include <core/Config.hpp>
32 #include <core/Typedefs.hpp>
33 #include <core/math/TAabb.hpp>
35 
38 
39 #include <rhi/RHIBuffer.hpp>
41 
42 #include <unordered_map>
43 #include <vector>
44 
46 
56 class SubMesh final : public RefCnt {
57 public:
58  BRK_API SubMesh() = default;
59  BRK_API ~SubMesh() override = default;
60 
62  BRK_API const StringName &GetName() const { return mName; }
63 
65  BRK_API const Ref<RHIIndexBuffer> &GetIndexBuffer() const { return mIndexBuffer; }
67  BRK_API RHIPrimitivesType GetPrimitivesType() const { return mPrimitivesType; }
69  BRK_API RHIIndexType GetIndexType() const { return mIndexType; }
71  BRK_API const Aabbf &GetAabb() const { return mAabb; }
72 
74  BRK_API uint32 GetVerticesCount() const { return mVerticesCount; }
76  BRK_API uint32 GetBaseVertex() const { return mBaseVertex; }
78  BRK_API uint32 GetIndicesCount() const { return mIndicesCount; }
80  BRK_API uint32 GetMaterialIndex() const { return mMaterialIndex; }
81 
83  BRK_API bool IsIndexed() const { return mIndexType != RHIIndexType::Unknown; }
84 
85 private:
86  friend class Mesh;
87 
88  StringName mName;
90  Ref<RHIIndexBuffer> mIndexBuffer;
92  RHIIndexType mIndexType = RHIIndexType::Unknown;
93  Aabbf mAabb;
95  uint32 mVerticesCount{};
96  uint32 mBaseVertex{};
97  uint32 mIndicesCount{};
98  uint32 mMaterialIndex = 0xffffffff;
99 };
100 
105 struct MeshArrays {
106  const float *positions = nullptr;
107  const float *normals = nullptr;
108  const float *tangents = nullptr;
109  const float *colors = nullptr;
110  const float *uvs = nullptr;
111  const float *uvs2 = nullptr;
112  const float *weights = nullptr;
113  const int32 *bones = nullptr;
114 };
115 
130 class Mesh final : public RefCnt {
131 public:
141  BRK_API Mesh(MeshFormat format, uint32 verticesCount, const Ref<Data> &vertexData, const Ref<Data> &attributeData, const Ref<Data> &skinningData);
142 
152  BRK_API Mesh(MeshFormat format, uint32 verticesCount, const MeshArrays &meshArrays);
153 
154  BRK_API ~Mesh() override = default;
155 
157  BRK_API void SetName(StringName name);
159  BRK_API void SetAabb(const Aabbf &aabb);
160 
162  BRK_API void AddMaterial(Ref<Material> material);
163 
175  BRK_API void AddSubMesh(const StringName &name, RHIPrimitivesType primitivesType, const Aabbf &aabb, uint32 baseVertex, RHIIndexType indexType, uint32 indicesCount, const Ref<Data> &indexData);
176 
183  BRK_API void SetSubMeshMaterial(const StringName &name, uint32 index);
184 
187 
189  BRK_API const StringName &GetName() const { return mName; }
191  BRK_API const MeshFormat &GetFormat() const { return mFormat; }
193  BRK_API const Aabbf &GetAabb() const { return mAabb; }
194 
196  BRK_API const std::vector<Ref<Material>> &GetMaterials() const { return mMaterials; }
198  BRK_API const std::vector<Ref<SubMesh>> &GetSubMeshes() const { return mSubMeshes; }
200  BRK_API const std::unordered_map<StringName, uint32> &GetSubMeshLookUp() const { return mSubMeshLookUp; }
201 
203  BRK_API uint32 GetVerticesCount() const { return mVerticesCount; }
204 
206  BRK_API const Ref<RHIVertexBuffer> &GetVertexData() const { return mVertexData; }
208  BRK_API const Ref<RHIVertexBuffer> &GetAttributeData() const { return mAttributeData; }
210  BRK_API const Ref<RHIVertexBuffer> &GetSkinningData() const { return mSkinningData; }
211 
213  BRK_API const Ref<RHIVertexDeclaration> &GetDeclaration() const { return mDeclaration; }
214 
215  BRK_API bool HasVertexData() const { return mVertexData.IsNotNull(); }
216  BRK_API bool HasAttributeData() const { return mAttributeData.IsNotNull(); }
217  BRK_API bool HasSkinningData() const { return mSkinningData.IsNotNull(); }
218 
219 private:
220  void InitFromData(MeshFormat format, uint32 verticesCount, const Ref<Data> &vertexData, const Ref<Data> &attributeData, const Ref<Data> &skinningData);
221 
222 private:
223  StringName mName;
224  MeshFormat mFormat;
225  Aabbf mAabb;
227  std::vector<Ref<Material>> mMaterials;
228  std::vector<Ref<SubMesh>> mSubMeshes;
229  std::unordered_map<StringName, uint32> mSubMeshLookUp;
231  uint32 mVerticesCount = 0;
233  Ref<RHIVertexBuffer> mVertexData;
234  Ref<RHIVertexBuffer> mAttributeData;
235  Ref<RHIVertexBuffer> mSkinningData;
237  Ref<RHIVertexDeclaration> mDeclaration;
238 };
239 
245 
246 #endif//BERSERK_MESH_HPP
#define BRK_NS_END
Definition: Config.hpp:48
#define BRK_API
Definition: Config.hpp:32
std::int32_t int32
Definition: Typedefs.hpp:43
std::uint32_t uint32
Definition: Typedefs.hpp:44
Mask defining mesh format (composed from attributes)
Contains vertex array-based geometry.
Definition: Mesh.hpp:130
BRK_API const Ref< RHIVertexBuffer > & GetSkinningData() const
Definition: Mesh.hpp:210
BRK_API void AddSubMesh(const StringName &name, RHIPrimitivesType primitivesType, const Aabbf &aabb, uint32 baseVertex, RHIIndexType indexType, uint32 indicesCount, const Ref< Data > &indexData)
Add new indexed sub-mesh to the mesh.
Definition: Mesh.cpp:110
BRK_API Ref< const SubMesh > FindSubMesh(const StringName &name) const
Definition: Mesh.cpp:162
BRK_API void SetSubMeshMaterial(const StringName &name, uint32 index)
Set sub-mesh material.
Definition: Mesh.cpp:146
BRK_API uint32 GetVerticesCount() const
Definition: Mesh.hpp:203
BRK_API const Ref< RHIVertexBuffer > & GetVertexData() const
Definition: Mesh.hpp:206
BRK_API void SetAabb(const Aabbf &aabb)
Definition: Mesh.cpp:101
BRK_API const std::unordered_map< StringName, uint32 > & GetSubMeshLookUp() const
Definition: Mesh.hpp:200
BRK_API const Ref< RHIVertexBuffer > & GetAttributeData() const
Definition: Mesh.hpp:208
BRK_API void AddMaterial(Ref< Material > material)
Definition: Mesh.cpp:105
BRK_API const StringName & GetName() const
Definition: Mesh.hpp:189
BRK_API ~Mesh() override=default
BRK_API void SetName(StringName name)
Definition: Mesh.cpp:97
BRK_API const Aabbf & GetAabb() const
Definition: Mesh.hpp:193
BRK_API bool HasVertexData() const
Definition: Mesh.hpp:215
BRK_API const Ref< RHIVertexDeclaration > & GetDeclaration() const
Definition: Mesh.hpp:213
BRK_API bool HasAttributeData() const
Definition: Mesh.hpp:216
BRK_API const std::vector< Ref< SubMesh > > & GetSubMeshes() const
Definition: Mesh.hpp:198
BRK_API const std::vector< Ref< Material > > & GetMaterials() const
Definition: Mesh.hpp:196
BRK_API bool HasSkinningData() const
Definition: Mesh.hpp:217
BRK_API Mesh(MeshFormat format, uint32 verticesCount, const Ref< Data > &vertexData, const Ref< Data > &attributeData, const Ref< Data > &skinningData)
Creates mesh from raw vertex data.
Definition: Mesh.cpp:34
BRK_API const MeshFormat & GetFormat() const
Definition: Mesh.hpp:191
Reference counted base object.
Definition: RefCnt.hpp:52
bool IsNotNull() const
Definition: Ref.hpp:94
Cached shared utf-8 string id.
Definition: StringName.hpp:61
Separate sufficient entity inside mesh object.
Definition: Mesh.hpp:56
BRK_API const StringName & GetName() const
Definition: Mesh.hpp:62
BRK_API SubMesh()=default
BRK_API RHIPrimitivesType GetPrimitivesType() const
Definition: Mesh.hpp:67
BRK_API uint32 GetBaseVertex() const
Definition: Mesh.hpp:76
BRK_API RHIIndexType GetIndexType() const
Definition: Mesh.hpp:69
BRK_API uint32 GetVerticesCount() const
Definition: Mesh.hpp:74
BRK_API const Aabbf & GetAabb() const
Definition: Mesh.hpp:71
BRK_API bool IsIndexed() const
Definition: Mesh.hpp:83
BRK_API ~SubMesh() override=default
BRK_API uint32 GetIndicesCount() const
Definition: Mesh.hpp:78
BRK_API uint32 GetMaterialIndex() const
Definition: Mesh.hpp:80
BRK_API const Ref< RHIIndexBuffer > & GetIndexBuffer() const
Definition: Mesh.hpp:65
RHIIndexType
Definition: RHIDefs.hpp:57
RHIPrimitivesType
Definition: RHIDefs.hpp:220
Definition: GLDevice.cpp:46
Struct describing mesh data per attribute.
Definition: Mesh.hpp:105
const float * normals
Definition: Mesh.hpp:107
const float * positions
Definition: Mesh.hpp:106
const float * weights
Definition: Mesh.hpp:112
const float * uvs2
Definition: Mesh.hpp:111
const float * tangents
Definition: Mesh.hpp:108
const float * uvs
Definition: Mesh.hpp:110
const int32 * bones
Definition: Mesh.hpp:113
const float * colors
Definition: Mesh.hpp:109