/
PicoModelLoader.cpp
116 lines (88 loc) · 3.04 KB
/
PicoModelLoader.cpp
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
#include "PicoModelLoader.h"
#include "ifilesystem.h"
#include "iarchive.h"
#include "imodelcache.h"
#include "lib/picomodel.h"
#include "os/path.h"
#include "idatastream.h"
#include "string/case_conv.h"
#include "StaticModel.h"
#include "StaticModelSurface.h"
namespace model {
namespace
{
size_t picoInputStreamReam(void* inputStream, unsigned char* buffer, size_t length) {
return reinterpret_cast<InputStream*>(inputStream)->read(buffer, length);
}
} // namespace
PicoModelLoader::PicoModelLoader(const picoModule_t* module, const std::string& extension) :
ModelImporterBase(extension),
_module(module)
{}
// Load the given model from the VFS path
IModelPtr PicoModelLoader::loadModelFromPath(const std::string& path)
{
// Open an ArchiveFile to load
auto file = path_is_absolute(path.c_str()) ?
GlobalFileSystem().openFileInAbsolutePath(path) :
GlobalFileSystem().openFile(path);
if (!file)
{
rError() << "Failed to load model " << path << std::endl;
return IModelPtr();
}
// Determine the file extension (ASE or LWO) to pass down to the PicoModel
std::string fName = file->getName();
string::to_lower(fName);
std::string fExt = fName.substr(fName.size() - 3, 3);
picoModel_t* model = PicoModuleLoadModelStream(
_module,
&file->getInputStream(),
picoInputStreamReam,
file->size(),
0
);
// greebo: Check if the model load was successful
if (!model || model->numSurfaces == 0)
{
// Model is either NULL or has no surfaces, this must've failed
return IModelPtr();
}
// Convert the pico model surfaces to StaticModelSurfaces
auto surfaces = CreateSurfaces(model, fExt);
auto modelObj = std::make_shared<StaticModel>(surfaces);
// Set the filename
modelObj->setFilename(os::getFilename(file->getName()));
modelObj->setModelPath(path);
PicoFreeModel(model);
return modelObj;
}
std::vector<StaticModelSurfacePtr> PicoModelLoader::CreateSurfaces(picoModel_t* picoModel, const std::string& extension)
{
// Convert the pico model surfaces to StaticModelSurfaces
std::vector<StaticModelSurfacePtr> surfaces;
// Get the number of surfaces to create
int nSurf = PicoGetModelNumSurfaces(picoModel);
// Create a StaticModelSurface for each surface in the structure
for (int n = 0; n < nSurf; ++n)
{
// Retrieve the surface, discarding it if it is null or non-triangulated (?)
picoSurface_t* surf = PicoGetModelSurface(picoModel, n);
auto rSurf = CreateSurface(surf, extension);
if (!rSurf) continue;
surfaces.emplace_back(rSurf);
}
return surfaces;
}
StaticModelSurfacePtr PicoModelLoader::CreateSurface(picoSurface_t* picoSurface, const std::string& extension)
{
if (picoSurface == 0 || PicoGetSurfaceType(picoSurface) != PICO_TRIANGLES)
{
return StaticModelSurfacePtr();
}
// Fix the normals of the surface (?)
PicoFixSurfaceNormals(picoSurface);
// Create the StaticModelSurface object and add it to the vector
return std::make_shared<StaticModelSurface>(picoSurface, extension);
}
} // namespace model