-
Notifications
You must be signed in to change notification settings - Fork 47
/
ModelCache.cpp
215 lines (166 loc) · 4.89 KB
/
ModelCache.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "ModelCache.h"
#include "ifilesystem.h"
#include "imodel.h"
#include "imd5model.h"
#include "imd5anim.h"
#include "ieventmanager.h"
#include "iparticles.h"
#include "iparticlenode.h"
#include <iostream>
#include "os/path.h"
#include "os/file.h"
#include "module/StaticModule.h"
#include <functional>
#include "map/algorithm/Models.h"
namespace model
{
ModelCache::ModelCache() :
_enabled(true)
{}
scene::INodePtr ModelCache::getModelNode(const std::string& modelPath)
{
// Check if we have a reference to a modeldef
IModelDefPtr modelDef = GlobalEntityClassManager().findModel(modelPath);
// The actual model path (is usually the same as the incoming modelPath)
std::string actualModelPath(modelPath);
if (modelDef)
{
// We have a valid modelDef, override the model path
actualModelPath = modelDef->mesh;
}
// Get the extension of this model
std::string type = actualModelPath.substr(actualModelPath.rfind(".") + 1);
if (type == "prt")
{
// This is a particle, pass the call to the Particles Manager
return GlobalParticlesManager().createParticleNode(actualModelPath);
}
// Get a suitable model loader
IModelImporterPtr modelLoader = GlobalModelFormatManager().getImporter(type);
// Try to construct a model node using the suitable loader
scene::INodePtr node = modelLoader->loadModel(actualModelPath);
if (node)
{
// For MD5 models, apply the idle animation by default
if (modelDef)
{
model::ModelNodePtr modelNode = Node_getModel(node);
if (!modelNode)
{
return node;
}
// Set the animation to play
try
{
md5::IMD5Model& md5model = dynamic_cast<md5::IMD5Model&>(modelNode->getIModel());
// Look up the "idle" anim if there is one
IModelDef::Anims::const_iterator found = modelDef->anims.find("idle");
if (found != modelDef->anims.end())
{
// Load the anim
md5::IMD5AnimPtr anim = GlobalAnimationCache().getAnim(found->second);
if (anim)
{
md5model.setAnim(anim);
md5model.updateAnim(0);
}
}
}
catch (std::bad_cast&)
{
// not an MD5 model, do nothing
}
}
// Model load was successful
return node;
}
// The model load failed, let's return a NullModel
IModelImporterPtr nullModelLoader = GlobalModelFormatManager().getImporter("");
return nullModelLoader->loadModel(actualModelPath);
}
IModelPtr ModelCache::getModel(const std::string& modelPath)
{
// Try to lookup the existing model
ModelMap::iterator found = _modelMap.find(modelPath);
if (_enabled && found != _modelMap.end())
{
return found->second;
}
// The model is not cached or the cache is disabled, load afresh
// Get the extension of this model
std::string type = modelPath.substr(modelPath.rfind(".") + 1);
// Find a suitable model loader
IModelImporterPtr modelLoader = GlobalModelFormatManager().getImporter(type);
IModelPtr model = modelLoader->loadModelFromPath(modelPath);
if (model)
{
// Model successfully loaded, insert a reference into the map
_modelMap.insert(ModelMap::value_type(modelPath, model));
}
return model;
}
void ModelCache::removeModel(const std::string& modelPath)
{
// greebo: Disable the modelcache. During map::clear(), the nodes
// get cleared, which might trigger a loopback to insert().
_enabled = false;
ModelMap::iterator found = _modelMap.find(modelPath);
if (found != _modelMap.end())
{
_modelMap.erase(found);
}
// Allow usage of the modelnodemap again.
_enabled = true;
}
void ModelCache::clear()
{
// greebo: Disable the modelcache. During map::clear(), the nodes
// get cleared, which might trigger a loopback to insert().
_enabled = false;
_modelMap.clear();
// Allow usage of the modelnodemap again.
_enabled = true;
}
sigc::signal<void> ModelCache::signal_modelsReloaded()
{
return _sigModelsReloaded;
}
// RegisterableModule implementation
const std::string& ModelCache::getName() const
{
static std::string _name("ModelCache");
return _name;
}
const StringSet& ModelCache::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_MODELFORMATMANAGER);
_dependencies.insert(MODULE_COMMANDSYSTEM);
}
return _dependencies;
}
void ModelCache::initialiseModule(const ApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;
GlobalCommandSystem().addCommand("RefreshModels",
std::bind(&ModelCache::refreshModels, this, std::placeholders::_1));
GlobalCommandSystem().addCommand("RefreshSelectedModels",
std::bind(&ModelCache::refreshSelectedModels, this, std::placeholders::_1));
}
void ModelCache::shutdownModule()
{
clear();
}
void ModelCache::refreshModels(const cmd::ArgumentList& args)
{
map::algorithm::refreshModels(true);
}
void ModelCache::refreshSelectedModels(const cmd::ArgumentList& args)
{
map::algorithm::refreshSelectedModels(true);
}
// The static module
module::StaticModule<ModelCache> modelCacheModule;
} // namespace model