sculpto
mesh.h
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file mesh.h
3 * \brief Mesh class defintion module.
4 * Mesh composes vertex array (vertex and index buffer),
5 * material (shader + uniform data, textures),
6 * and tranformation (world) matrix.
7 *
8 * \author Sabitov Kirill
9 * \date 01 July 2022
10 *********************************************************************/
11
12#pragma once
13
14#include "materials/material.h"
15#include "topology/trimesh.h"
18
19namespace scl
20{
22 template <typename Tvertex>
24 {
27 };
28
30 class mesh
31 {
32 friend class renderer;
33
35 struct submesh_data
36 {
37 shared<vertex_array> VertexArray {};
38 shared<vertex_buffer> VertexBuffer {};
39 shared<index_buffer> IndexBuffer {};
40 shared<material> Material {};
41 matr4 LocalTransform {};
42 };
43
44 public:
45 std::string FileName {};
46 std::vector<submesh_data> SubMeshes {};
49 bool IsDrawing = true;
50 bool IsCastingShadow = true;
52 /*!*
53 * Mesh constructor by topology object and material.
54 *
55 * \param TopologyObject - topology object to create vertex array from.
56 * \param Material - mesh material.
57 */
58 template <typename Ttopology>
59 mesh(const Ttopology &TopologyObject, shared<material> Material)
60 {
61 submesh_data new_sub_mesh {};
62 new_sub_mesh.VertexArray = vertex_array::Create(TopologyObject.GetType());
63 new_sub_mesh.VertexBuffer = vertex_buffer::Create(TopologyObject.GetVertices().data(),
64 (u32)TopologyObject.GetVertices().size(),
66 new_sub_mesh.IndexBuffer = index_buffer::Create((u32 *)TopologyObject.GetIndices().data(),
67 (u32)TopologyObject.GetIndices().size());
68 new_sub_mesh.VertexArray->SetIndexBuffer(new_sub_mesh.IndexBuffer);
69 new_sub_mesh.VertexArray->SetVertexBuffer(new_sub_mesh.VertexBuffer);
70
71 new_sub_mesh.Material = Material;
72 SubMeshes.push_back(new_sub_mesh);
73 FileName = typeid(Ttopology).name();
74
75 SCL_CORE_INFO("Mesh with 1 sub-mesh created.");
76 }
77
78 /*!*
79 * Mesh constructor by submeshes properties array.
80 *
81 * \param SubmeshesProperties - array of sub meshes data (topology object + material).
82 */
83 template <typename Tvertex>
84 mesh(const std::vector<submesh_props<Tvertex>> &SubmeshesProperties)
85 {
86 for (const auto &submesh_prop : SubmeshesProperties)
87 {
88 submesh_data new_sub_mesh {};
89 new_sub_mesh.VertexArray = vertex_array::Create(submesh_prop.Topology.GetType());
90 new_sub_mesh.VertexBuffer = vertex_buffer::Create(submesh_prop.Topology.GetVertices().data(),
91 (u32)submesh_prop.Topology.GetVertices().size(),
93 new_sub_mesh.IndexBuffer = index_buffer::Create((u32 *)submesh_prop.Topology.GetIndices().data(),
94 (u32)submesh_prop.Topology.GetIndices().size());
95 new_sub_mesh.VertexArray->SetIndexBuffer(new_sub_mesh.IndexBuffer);
96 new_sub_mesh.VertexArray->SetVertexBuffer(new_sub_mesh.VertexBuffer);
97
98 new_sub_mesh.Material = submesh_prop.Material;
99 SubMeshes.push_back(new_sub_mesh);
100 }
101
102 SCL_CORE_INFO("Mesh with {} sub-mesh(es) created.", SubmeshesProperties.size());
103 }
104
107 {
108 SCL_CORE_INFO("Mesh with {} sub-mesh(es) freed.", SubMeshes.size());
109 }
110
111 /*!*
112 * Mesh constructor by topology object and material.
113 *
114 * \param TopologyObject - topology object to create vertex array from.
115 * \return created mesh pointer.
116 */
117 template <typename Ttopology>
118 static shared<mesh> Create(const Ttopology &TopologyObject, shared<material> Material)
119 {
120 return CreateShared<mesh>(TopologyObject, Material);
121 }
122
123 /*!*
124 * Mesh constructor by submeshes properties array.
125 *
126 * \param SubmeshesProperties - array of sub meshes data (topology object + material).
127 */
128 template <typename Tvertex>
129 static shared<mesh> Create(const std::vector<submesh_props<Tvertex>> &SubmeshesProperties)
130 {
131 return CreateShared<mesh>(SubmeshesProperties);
132 }
133 };
134}
Buffer interfaces implementation module.
static shared< index_buffer > Create(u32 *Indices, u32 Count)
Definition: buffer.cpp:65
Definition: mesh.h:31
static shared< mesh > Create(const Ttopology &TopologyObject, shared< material > Material)
Definition: mesh.h:118
bool IsCastingShadow
Definition: mesh.h:50
std::vector< submesh_data > SubMeshes
Definition: mesh.h:46
~mesh()
Definition: mesh.h:106
mesh(const std::vector< submesh_props< Tvertex > > &SubmeshesProperties)
Definition: mesh.h:84
mesh(const Ttopology &TopologyObject, shared< material > Material)
Definition: mesh.h:59
std::string FileName
Definition: mesh.h:45
bool IsDrawing
Definition: mesh.h:49
static shared< mesh > Create(const std::vector< submesh_props< Tvertex > > &SubmeshesProperties)
Definition: mesh.h:129
static shared< vertex_array > Create(mesh_type Mesh_type)
static shared< vertex_buffer > Create(u32 Count, const vertex_layout &VertexLayout)
Definition: buffer.cpp:41
#define SCL_CORE_INFO(...)
Definition: log.h:41
Mesh material class deinition module.
Definition: base.h:33
std::shared_ptr< T > shared
Definition: smart_ptr.h:15
math::matr4< float > matr4
Definition: base.h:48
uint32_t u32
Definition: math_common.h:21
shared< material > Material
Definition: mesh.h:26
topology::trimesh Topology
Definition: mesh.h:25
static vertex_layout GetVertexLayout()
Definition: vertex.h:120
Topology object triangles mesh class declaration module.
Vertex array interfaces implementation module.