Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
model.cpp
Go to the documentation of this file.
1#include "model.hpp"
2#include <stdexcept>
3#include <assimp/Importer.hpp>
4#include <assimp/scene.h>
5#include <assimp/postprocess.h>
6#include "model_material.hpp"
7
8namespace rendering_engine
9{
10Model::Model(const std::string& filename, bool flipUVs)
11{
12 Assimp::Importer importer;
13
14 uint32_t flags = aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcess_SortByPType;
15 if( flipUVs )
16 {
17 flags |= aiProcess_FlipUVs;
18 }
19
20 const aiScene* scene = importer.ReadFile(filename, flags);
21
22 if( scene == nullptr )
23 {
24 throw std::runtime_error(importer.GetErrorString());
25 }
26
27 if( scene->HasMaterials() )
28 {
29 for( uint32_t i = 0; i < scene->mNumMaterials; i++ )
30 {
31 mMaterials.push_back(std::shared_ptr<ModelMaterial>(new ModelMaterial(*this, *scene->mMaterials[i])));
32 }
33 }
34
35 if( scene->HasMeshes() )
36 {
37 for( uint32_t i = 0; i < scene->mNumMeshes; i++ )
38 {
39 mMeshes.push_back(std::shared_ptr<Mesh>(new Mesh(*this, *(scene->mMeshes[i]))));
40 }
41 }
42}
43Model::Model(std::vector<uint8_t> const& fileBytes, bool flipUVs)
44{
45 Assimp::Importer importer;
46
47 uint32_t flags = aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcess_SortByPType;
48 if (flipUVs)
49 {
50 flags |= aiProcess_FlipUVs;
51 }
52
53 const aiScene* scene = importer.ReadFileFromMemory(fileBytes.data(), fileBytes.size(), flags, "fbx");
54
55 if (scene == nullptr)
56 {
57 throw std::runtime_error(importer.GetErrorString());
58 }
59
60 if (scene->HasMaterials())
61 {
62 for (uint32_t i = 0; i < scene->mNumMaterials; i++)
63 {
64 mMaterials.push_back(std::shared_ptr<ModelMaterial>(new ModelMaterial(*this, *scene->mMaterials[i])));
65 }
66 }
67
68 if (scene->HasMeshes())
69 {
70 for (uint32_t i = 0; i < scene->mNumMeshes; i++)
71 {
72 mMeshes.push_back(std::shared_ptr<Mesh>(new Mesh(*this, *(scene->mMeshes[i]))));
73 }
74 }
75}
76bool Model::HasMeshes() const
77{
78 return !mMeshes.empty();
79}
81{
82 return !mMaterials.empty();
83}
84const std::vector<std::shared_ptr<Mesh>>& Model::Meshes() const
85{
86 return mMeshes;
87}
88const std::vector<std::shared_ptr<ModelMaterial>>& Model::Materials() const
89{
90 return mMaterials;
91}
92} //namespace rendering_engine
bool HasMeshes() const
Checks whether this model contains any meshes.
Definition model.cpp:76
bool HasMaterials() const
Checks whether this model contains any materials.
Definition model.cpp:80
Model(const std::string &filename, bool flipUVs=false)
Constructs a model from a file.
Definition model.cpp:10
friend class Mesh
Definition model.hpp:31
const std::vector< std::shared_ptr< ModelMaterial > > & Materials() const
Returns the list of materials belonging to this model.
Definition model.cpp:88
const std::vector< std::shared_ptr< Mesh > > & Meshes() const
Returns the list of meshes belonging to this model.
Definition model.cpp:84
Represents a single material imported from a 3D model file.