Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
mesh.cpp
Go to the documentation of this file.
1#include "mesh.hpp"
2#include "assimp/scene.h"
3namespace rendering_engine
4{
5Mesh::Mesh(Model& model, aiMesh& mesh)
6 :
7 mModel(&model),
8 mMaterial(nullptr),
9 mName(mesh.mName.C_Str()),
10 mVertices(),
11 mNormals(),
12 mTangents(),
13 mBiNormals(),
14 mTextureCoordinates(),
15 mVertexColors(),
16 mFaceCount(0),
17 mIndices()
18{
19 mMaterial; // = mModel.Materials().at(mesh.mMaterialIndex);
20
21 // Vertices
22 mVertices.reserve(mesh.mNumVertices);
23 for( uint32_t i = 0; i < mesh.mNumVertices; i++ )
24 {
25 const auto& vertex = mesh.mVertices[i];
26 mVertices.emplace_back(vertex.x, vertex.y, vertex.z);
27 }
28
29 // Normals
30 if( mesh.HasNormals() )
31 {
32 mNormals.reserve(mesh.mNumVertices);
33 for( uint32_t i = 0; i < mesh.mNumVertices; i++ )
34 {
35 const auto& normal = mesh.mNormals[i];
36 mNormals.emplace_back(normal.x, normal.y, normal.z);
37 }
38 }
39
40 // Tangents and Binormals
41 if( mesh.HasTangentsAndBitangents() )
42 {
43 mTangents.reserve(mesh.mNumVertices);
44 mBiNormals.reserve(mesh.mNumVertices);
45 for( uint32_t i = 0; i < mesh.mNumVertices; i++ )
46 {
47 const auto& tangent = mesh.mTangents[i];
48 mTangents.emplace_back(tangent.x, tangent.y, tangent.z);
49
50 const auto& binormal = mesh.mBitangents[i];
51 mBiNormals.emplace_back(binormal.x, binormal.y, binormal.z);
52 }
53 }
54
55 // Texture Coordinates
56 uint32_t uvChannelCount = mesh.GetNumUVChannels();
57 for( uint32_t i = 0; i < uvChannelCount; i++ )
58 {
59 std::vector<glm::vec3> textureCoordinates;
60 textureCoordinates.reserve(mesh.mNumVertices);
61 aiVector3D* aiTextureCoordinates = mesh.mTextureCoords[i];
62 for( uint32_t j = 0; j < mesh.mNumVertices; j++ )
63 {
64 const auto& uv = aiTextureCoordinates[j];
65 textureCoordinates.emplace_back(uv.x, uv.y, uv.z);
66 }
67
68 mTextureCoordinates.push_back(std::move(textureCoordinates));
69 }
70
71 // Vertex Colors
72 uint32_t colorChannelCount = mesh.GetNumColorChannels();
73 for( uint32_t i = 0; i < colorChannelCount; i++ )
74 {
75 std::vector<glm::vec4> vertexColors;
76 vertexColors.reserve(mesh.mNumVertices);
77 aiColor4D* aiVertexColors = mesh.mColors[i];
78 for( uint32_t j = 0; j < mesh.mNumVertices; j++ )
79 {
80 const auto& c = aiVertexColors[j];
81 vertexColors.emplace_back(c.r, c.g, c.b, c.a);
82 }
83 mVertexColors.push_back(move(vertexColors));
84 }
85
86 // Faces (note: could pre-reserve if we limit primitive types)
87 if( mesh.HasFaces() )
88 {
89 mFaceCount = mesh.mNumFaces;
90 for( uint32_t i = 0; i < mFaceCount; i++ )
91 {
92 aiFace* face = &mesh.mFaces[i];
93
94 for( uint32_t j = 0; j < face->mNumIndices; j++ )
95 {
96 mIndices.push_back(face->mIndices[j]);
97 }
98 }
99 }
100}
101
102
104{
105 return *mModel;
106}
107
108std::shared_ptr<ModelMaterial> Mesh::GetMaterial() const
109{
110 return mMaterial;
111}
112
113const std::string& Mesh::Name() const
114{
115 return mName;
116}
117
118const std::vector<glm::vec3>& Mesh::Vertices() const
119{
120 return mVertices;
121}
122
123const std::vector<glm::vec3>& Mesh::Normals() const
124{
125 return mNormals;
126}
127
128const std::vector<glm::vec3>& Mesh::Tangents() const
129{
130 return mTangents;
131}
132
133const std::vector<glm::vec3>& Mesh::BiNormals() const
134{
135 return mBiNormals;
136}
137
138const std::vector<std::vector<glm::vec3>>& Mesh::TextureCoordinates() const
139{
140 return mTextureCoordinates;
141}
142
143const std::vector<std::vector<glm::vec4>>& Mesh::VertexColors() const
144{
145 return mVertexColors;
146}
147
148std::uint32_t Mesh::FaceCount() const
149{
150 return mFaceCount;
151}
152
153const std::vector<std::uint32_t>& Mesh::Indices() const
154{
155 return mIndices;
156}
157
158}// namespace rendering_engine
friend class Model
Definition mesh.hpp:38
const std::vector< std::vector< glm::vec3 > > & TextureCoordinates() const
Returns texture coordinate sets.
Definition mesh.cpp:138
std::shared_ptr< ModelMaterial > GetMaterial() const
Returns the material associated with this mesh.
Definition mesh.cpp:108
const std::vector< std::uint32_t > & Indices() const
Returns the vertex index buffer.
Definition mesh.cpp:153
const std::vector< glm::vec3 > & Vertices() const
Returns the vertex position array.
Definition mesh.cpp:118
const std::vector< glm::vec3 > & BiNormals() const
Returns the vertex binormal array.
Definition mesh.cpp:133
Model & GetModel()
Returns the parent model that owns this mesh.
Definition mesh.cpp:103
const std::vector< glm::vec3 > & Tangents() const
Returns the vertex tangent array.
Definition mesh.cpp:128
const std::vector< std::vector< glm::vec4 > > & VertexColors() const
Returns per-vertex color sets.
Definition mesh.cpp:143
Mesh(const Mesh &)=default
std::uint32_t FaceCount() const
Returns the number of faces in the mesh.
Definition mesh.cpp:148
const std::vector< glm::vec3 > & Normals() const
Returns the vertex normal array.
Definition mesh.cpp:123
const std::string & Name() const
Returns the mesh name as imported from the source file.
Definition mesh.cpp:113
Represents a 3D model composed of multiple meshes and materials.
Definition model.hpp:30