-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaterializedObjModel.h
103 lines (91 loc) · 3.75 KB
/
MaterializedObjModel.h
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
//
// Created by rthier on 2016.06.20..
//
#ifndef MATERIALIZEDOBJMODEL_H
#define MATERIALIZEDOBJMODEL_H
#include <vector>
#include <memory>
#include "MaterializedObjMeshObject.h"
#include "GpuTexturePreparationLibrary.h"
#include "Obj.h"
namespace ObjMaster {
/**
* Renderable 3D obj model with possible materials.
*
* A GpuTexturePreparationLibrary implementation should be provided as a template
* parameter so that we can unload/load textures properly - even in the destructor.
*/
template <class GpuTexturePreparationLibraryImpl>
class MaterializedObjModel {
public:
bool inited = false;
std::vector<MaterializedObjMeshObject> meshes;
std::string path;
// Copies are defeaulted
MaterializedObjModel(const MaterializedObjModel &other) = default;
MaterializedObjModel& operator=(const MaterializedObjModel &other) = default;
// Moves are defaulted
MaterializedObjModel(MaterializedObjModel &&other) = default;
MaterializedObjModel& operator=(MaterializedObjModel &&other) = default;
/** Create a materialized obj model using the given obj representation */
MaterializedObjModel(const Obj &obj) {
// The path of the model is the same as the path for obj
path = obj.objPath;
// We create one mesh per each object material group
for(auto gPair : obj.objectMaterialGroups) {
#ifdef DEBUG
OMLOGI("!!!!! Object material group have found as %s", gPair.first.c_str());
#endif
meshes.push_back(std::move(MaterializedObjMeshObject(obj,
&(obj.fs[gPair.second.faceIndex]),
gPair.second.meshFaceCount,
gPair.second.textureDataHoldingMaterial,
gPair.first)));
}
// Indicate that the model is loaded
inited = true;
}
/** Create a materialized obj model that is not inited (empty) */
MaterializedObjModel() {}
/** Destructor of the model - tries to unload all material groups textures */
~MaterializedObjModel() { unloadAllTextures(); }
/**
* Load the textures of meshes onto the GPU for rendering.
*
* This iterates through all meshes materials and do the following to it:
* - Load mesh material textures into the main memory (if applicable)
* - Load mesh material textures from main memory onto the GPU (if applicable)
* - Unload mesh material textures from the main memory
*
* The result of this operation is that all the materials texture binding
* fields will be filled according to this and the model can be rendered!
*/
void loadAllTextures(const TexturePreparationLibrary &texLibrary) {
for(auto &mesh : meshes) {
mesh.material.loadTexturesIntoMemory(path.c_str(), texLibrary);
mesh.material.loadTexturesIntoGPU(gpuTexLibrary);
mesh.material.unloadTexturesFromMemory();
}
}
/** First unload all model textures from the GPU then also unload any textures from main memory */
void unloadAllTextures() {
for(auto &mesh : meshes) {
mesh.material.unloadTexturesFromGPU(gpuTexLibrary);
mesh.material.unloadTexturesFromMemory();
}
}
private:
/**
* Have to keep a library because the GPU-unload need to be managed by this! The unload
* from memory does not need the library, but unloading from the GPU is different and
* still we want to do that when the destructor is called so we are not leaking memory
* of the GPU. This way, when the model go out of scope or things like that, the cleanup
* works automatically. The normal texture prep library however is not cached as it can
* be convenient to load textures from varoius places - however it is not convenient to
* change the graphics API on the fly while using the same models so it is okay that the
* user code should supply this when constructing the model as template param.
*/
GpuTexturePreparationLibraryImpl gpuTexLibrary = GpuTexturePreparationLibraryImpl();
};
}
#endif // MATERIALIZEDOBJMODEL_H