-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
121 lines (103 loc) · 2.46 KB
/
Model.cpp
File metadata and controls
121 lines (103 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "Application.h"
#include "Model.h"
#include "Mesh.h"
#include "ModuleTexture.h"
#define TINYGLTF_NO_STB_IMAGE_WRITE
#define TINYGLTF_NO_STB_IMAGE
#define TINYGLTF_NO_EXTERNAL_IMAGE
#define TINYGLTF_IMPLEMENTATION // Only in one of the includes
#include "tiny_gltf.h"
Model::Model()
{
ChangeTransform(position, rotation, scale);
}
// Destructor
Model::~Model()
{
}
void Model::Load(const char* assetFileName)
{
std::string name = assetFileName;
int last = name.find_last_of('/');
fileName = "";
for (int i = last + 1; i < name.length() - 5; i++)
{
fileName += name[i];
}
tinygltf::TinyGLTF gltfContext;
tinygltf::Model sourceModel;
std::string error, warning;
bool loadOk = gltfContext.LoadASCIIFromFile(&sourceModel, &error, &warning, assetFileName);
if (!loadOk)
{
LOG("Error loading %s: %s", assetFileName, error.c_str());
}
for (const auto& srcMesh : sourceModel.meshes)
{
for (const auto& primitive : srcMesh.primitives)
{
Mesh* mesh = new Mesh;
mesh->Load(sourceModel, srcMesh, primitive);
meshes.push_back(mesh);
vertexCount += mesh->GetVertexCount();
indexCount += mesh->GetIndexCount();
}
}
LoadMaterials(sourceModel);
}
void Model::LoadMaterials(const tinygltf::Model& srcModel)
{
for (const auto& srcMaterial : srcModel.materials)
{
unsigned int textureId = 0;
if (srcMaterial.pbrMetallicRoughness.baseColorTexture.index >= 0)
{
const tinygltf::Texture& texture = srcModel.textures[srcMaterial.pbrMetallicRoughness.baseColorTexture.index];
const tinygltf::Image& image = srcModel.images[texture.source];
App->GetTexture()->LoadFile("./Models/"+ fileName + "/" + image.uri);
textureId = App->GetTexture()->GenerateTexture();
}
textures.push_back(textureId);
}
}
void Model::Draw()
{
for (const auto& mesh : meshes)
{
mesh->Draw(textures);
}
}
bool Model::CleanUp()
{
for (const auto& mesh : meshes)
{
mesh->DeleteBuffers();
}
meshes.clear();
textures.clear();
return true;
}
void Model::ChangePosition(float3 position)
{
model = float4x4::FromTRS(position,
float4x4::RotateZ(rotation),
scale);
}
void Model::ChangeRotation(float rotation)
{
model = float4x4::FromTRS(position,
float4x4::RotateZ(rotation),
scale);
}
void Model::ChangeScale(float3 scale)
{
model = float4x4::FromTRS(position,
float4x4::RotateZ(rotation),
scale);
}
void Model::ChangeTransform(float3 position, float rotation, float3 scale)
{
model = float4x4::FromTRS(position,
float4x4::RotateZ(rotation),
scale);
}