Rendering Engine 0.2.17
Modular Graphics Rendering Engine | v0.2.17
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 // Vertices
20 mVertices.reserve(mesh.mNumVertices);
21 for( uint32_t i = 0; i < mesh.mNumVertices; i++ )
22 {
23 const auto& vertex = mesh.mVertices[i];
24 mVertices.emplace_back(vertex.x, vertex.y, vertex.z);
25 }
26
27 // Normals
28 if( mesh.HasNormals() )
29 {
30 mNormals.reserve(mesh.mNumVertices);
31 for( uint32_t i = 0; i < mesh.mNumVertices; i++ )
32 {
33 const auto& normal = mesh.mNormals[i];
34 mNormals.emplace_back(normal.x, normal.y, normal.z);
35 }
36 }
37
38 // Tangents and Binormals
39 if( mesh.HasTangentsAndBitangents() )
40 {
41 mTangents.reserve(mesh.mNumVertices);
42 mBiNormals.reserve(mesh.mNumVertices);
43 for( uint32_t i = 0; i < mesh.mNumVertices; i++ )
44 {
45 const auto& tangent = mesh.mTangents[i];
46 mTangents.emplace_back(tangent.x, tangent.y, tangent.z);
47
48 const auto& binormal = mesh.mBitangents[i];
49 mBiNormals.emplace_back(binormal.x, binormal.y, binormal.z);
50 }
51 }
52
53 // Texture Coordinates
54 uint32_t uvChannelCount = mesh.GetNumUVChannels();
55 for( uint32_t i = 0; i < uvChannelCount; i++ )
56 {
57 std::vector<glm::vec3> textureCoordinates;
58 textureCoordinates.reserve(mesh.mNumVertices);
59 aiVector3D* aiTextureCoordinates = mesh.mTextureCoords[i];
60 for( uint32_t j = 0; j < mesh.mNumVertices; j++ )
61 {
62 const auto& uv = aiTextureCoordinates[j];
63 textureCoordinates.emplace_back(uv.x, uv.y, uv.z);
64 }
65
66 mTextureCoordinates.push_back(std::move(textureCoordinates));
67 }
68
69 // Vertex Colors
70 uint32_t colorChannelCount = mesh.GetNumColorChannels();
71 for( uint32_t i = 0; i < colorChannelCount; i++ )
72 {
73 std::vector<glm::vec4> vertexColors;
74 vertexColors.reserve(mesh.mNumVertices);
75 aiColor4D* aiVertexColors = mesh.mColors[i];
76 for( uint32_t j = 0; j < mesh.mNumVertices; j++ )
77 {
78 const auto& c = aiVertexColors[j];
79 vertexColors.emplace_back(c.r, c.g, c.b, c.a);
80 }
81 mVertexColors.push_back(std::move(vertexColors));
82 }
83
84 // Faces (note: could pre-reserve if we limit primitive types)
85 if( mesh.HasFaces() )
86 {
87 mFaceCount = mesh.mNumFaces;
88 for( uint32_t i = 0; i < mFaceCount; i++ )
89 {
90 aiFace* face = &mesh.mFaces[i];
91
92 for( uint32_t j = 0; j < face->mNumIndices; j++ )
93 {
94 mIndices.push_back(face->mIndices[j]);
95 }
96 }
97 }
98}
99
100
101Model& Mesh::GetModel()
102{
103 return *mModel;
104}
105
106std::shared_ptr<ModelMaterial> Mesh::GetMaterial() const
107{
108 return mMaterial;
109}
110
111const std::string& Mesh::Name() const
112{
113 return mName;
114}
115
116const std::vector<glm::vec3>& Mesh::Vertices() const
117{
118 return mVertices;
119}
120
121const std::vector<glm::vec3>& Mesh::Normals() const
122{
123 return mNormals;
124}
125
126const std::vector<glm::vec3>& Mesh::Tangents() const
127{
128 return mTangents;
129}
130
131const std::vector<glm::vec3>& Mesh::BiNormals() const
132{
133 return mBiNormals;
134}
135
136const std::vector<std::vector<glm::vec3>>& Mesh::TextureCoordinates() const
137{
138 return mTextureCoordinates;
139}
140
141const std::vector<std::vector<glm::vec4>>& Mesh::VertexColors() const
142{
143 return mVertexColors;
144}
145
146std::uint32_t Mesh::FaceCount() const
147{
148 return mFaceCount;
149}
150
151const std::vector<std::uint32_t>& Mesh::Indices() const
152{
153 return mIndices;
154}
155
156}// namespace rendering_engine
Mesh(const Mesh &)=default
Represents a 3D model composed of multiple meshes and materials.
Definition model.hpp:30