/
RenderablePicoModel.cpp
437 lines (366 loc) · 12.2 KB
/
RenderablePicoModel.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include "RenderablePicoModel.h"
#include "RenderablePicoSurface.h"
#include "ivolumetest.h"
#include "iselectiontest.h"
#include "texturelib.h"
#include "ishaders.h"
#include "modelskin.h"
#include "ifilter.h"
#include "imodelsurface.h"
#include "VolumeIntersectionValue.h"
#include "math/Ray.h"
#include "BasicUndoMemento.h"
namespace model
{
// Constructor
RenderablePicoModel::RenderablePicoModel(picoModel_t* mod, const std::string& fExt) :
_scaleTransformed(1,1,1),
_scale(1,1,1),
_undoStateSaver(nullptr),
_mapFileChangeTracker(nullptr)
{
// Get the number of surfaces to create
int nSurf = PicoGetModelNumSurfaces(mod);
// Create a RenderablePicoSurface 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(mod, n);
if (surf == 0 || PicoGetSurfaceType(surf) != PICO_TRIANGLES)
continue;
// Fix the normals of the surface (?)
PicoFixSurfaceNormals(surf);
// Create the RenderablePicoSurface object and add it to the vector
RenderablePicoSurfacePtr rSurf(new RenderablePicoSurface(surf, fExt));
_surfVec.push_back(Surface(rSurf));
// Extend the model AABB to include the surface's AABB
_localAABB.includeAABB(rSurf->getAABB());
}
}
RenderablePicoModel::RenderablePicoModel(const RenderablePicoModel& other) :
_surfVec(other._surfVec.size()),
_scaleTransformed(other._scaleTransformed),
_scale(other._scale), // use scale of other model
_localAABB(other._localAABB),
_filename(other._filename),
_modelPath(other._modelPath),
_undoStateSaver(nullptr),
_mapFileChangeTracker(nullptr)
{
// Copy the other model's surfaces, but not its shaders, revert to default
for (std::size_t i = 0; i < other._surfVec.size(); ++i)
{
// Copy-construct the other surface, inheriting any applied scale
_surfVec[i].surface = std::make_shared<RenderablePicoSurface>(*(other._surfVec[i].surface));
_surfVec[i].originalSurface = other._surfVec[i].originalSurface;
_surfVec[i].surface->setActiveMaterial(_surfVec[i].surface->getDefaultMaterial());
}
}
void RenderablePicoModel::connectUndoSystem(IMapFileChangeTracker& changeTracker)
{
assert(_undoStateSaver == nullptr);
// Keep a reference around, we need it when faces are changing
_mapFileChangeTracker = &changeTracker;
_undoStateSaver = GlobalUndoSystem().getStateSaver(*this, changeTracker);
}
void RenderablePicoModel::disconnectUndoSystem(IMapFileChangeTracker& changeTracker)
{
assert(_undoStateSaver != nullptr);
_mapFileChangeTracker = nullptr;
_undoStateSaver = nullptr;
GlobalUndoSystem().releaseStateSaver(*this);
}
void RenderablePicoModel::foreachVisibleSurface(const std::function<void(const Surface& s)>& func) const
{
for (const Surface& surface : _surfVec)
{
assert(surface.shader);
// Check if the surface's shader is filtered, if not then submit it for rendering
const MaterialPtr& surfaceShader = surface.shader->getMaterial();
if (surfaceShader && surfaceShader->isVisible())
{
func(surface);
}
}
}
void RenderablePicoModel::renderSolid(RenderableCollector& rend,
const Matrix4& localToWorld,
const IRenderEntity& entity,
const LightSources& lights) const
{
// Submit renderables from each surface
foreachVisibleSurface([&](const Surface& s)
{
// Submit the ordinary shader for material-based rendering
rend.addRenderable(*s.shader, *s.surface, localToWorld,
&lights, &entity);
});
}
void RenderablePicoModel::renderWireframe(RenderableCollector& rend, const Matrix4& localToWorld,
const IRenderEntity& entity) const
{
// Submit renderables from each surface
foreachVisibleSurface([&](const Surface& s)
{
// Submit the wireframe shader for non-shaded renderers
rend.addRenderable(*entity.getWireShader(), *s.surface, localToWorld,
nullptr, &entity);
});
}
void RenderablePicoModel::setRenderSystem(const RenderSystemPtr& renderSystem)
{
_renderSystem = renderSystem;
captureShaders();
}
// OpenGL (back-end) render function
void RenderablePicoModel::render(const RenderInfo& info) const
{
// greebo: No GL state changes in render methods!
#if 0
// Render options
if (info.checkFlag(RENDER_TEXTURE_2D))
{
glEnable(GL_TEXTURE_2D);
}
if (info.checkFlag(RENDER_SMOOTH))
{
glShadeModel(GL_SMOOTH);
}
#endif
// Iterate over the surfaces, calling the render function on each one
for (SurfaceList::const_iterator i = _surfVec.begin();
i != _surfVec.end();
++i)
{
// greebo: Shader visibility checks have already been performed in the front end pass
#if 0
// Get the Material to test the shader name against the filter system
const MaterialPtr& surfaceShader = i->shader->getMaterial();
if (surfaceShader->isVisible())
{
// Bind the OpenGL texture and render the surface geometry
TexturePtr tex = surfaceShader->getEditorImage();
glBindTexture(GL_TEXTURE_2D, tex->getGLTexNum());
i->surface->render(info.getFlags());
}
#else
i->surface->render(info.getFlags());
#endif
}
}
std::string RenderablePicoModel::getFilename() const
{
return _filename;
}
void RenderablePicoModel::setFilename(const std::string& name)
{
_filename = name;
}
// Return vertex count of this model
int RenderablePicoModel::getVertexCount() const
{
int sum = 0;
for (const Surface& s : _surfVec)
{
sum += s.surface->getNumVertices();
}
return sum;
}
// Return poly count of this model
int RenderablePicoModel::getPolyCount() const
{
int sum = 0;
for (const Surface& s : _surfVec)
{
sum += s.surface->getNumTriangles();
}
return sum;
}
const IModelSurface& RenderablePicoModel::getSurface(unsigned surfaceNum) const
{
assert(surfaceNum >= 0 && surfaceNum < _surfVec.size());
return *(_surfVec[surfaceNum].surface);
}
// Apply the given skin to this model
void RenderablePicoModel::applySkin(const ModelSkin& skin)
{
// Apply the skin to each surface, then try to capture shaders
for (SurfaceList::iterator i = _surfVec.begin();
i != _surfVec.end();
++i)
{
const std::string& defaultMaterial = i->surface->getDefaultMaterial();
const std::string& activeMaterial = i->surface->getActiveMaterial();
// Look up the remap for this surface's material name. If there is a remap
// change the Shader* to point to the new shader.
std::string remap = skin.getRemap(defaultMaterial);
if (!remap.empty() && remap != activeMaterial)
{
// Save the remapped shader name
i->surface->setActiveMaterial(remap);
}
else if (remap.empty() && activeMaterial != defaultMaterial)
{
// No remap, so reset our shader to the original unskinned shader
i->surface->setActiveMaterial(defaultMaterial);
}
}
captureShaders();
// greebo: Update the active material list after applying this skin
updateMaterialList();
}
void RenderablePicoModel::captureShaders()
{
RenderSystemPtr renderSystem = _renderSystem.lock();
// Capture or release our shaders
for (SurfaceList::iterator i = _surfVec.begin(); i != _surfVec.end(); ++i)
{
if (renderSystem)
{
i->shader = renderSystem->capture(i->surface->getActiveMaterial());
}
else
{
i->shader.reset();
}
}
}
// Update the list of active materials
void RenderablePicoModel::updateMaterialList() const
{
_materialList.clear();
for (const auto& s : _surfVec)
{
_materialList.push_back(s.surface->getActiveMaterial());
}
}
// Return the list of active skins for this model
const StringList& RenderablePicoModel::getActiveMaterials() const
{
// If the material list is empty, populate it
if (_materialList.empty())
{
updateMaterialList();
}
// Return the list
return _materialList;
}
// Perform selection test
void RenderablePicoModel::testSelect(Selector& selector,
SelectionTest& test,
const Matrix4& localToWorld)
{
// Perform a volume intersection (AABB) check on each surface. For those
// that intersect, call the surface's own testSelection method to perform
// a proper selection test.
for (SurfaceList::iterator i = _surfVec.begin();
i != _surfVec.end();
++i)
{
// Check volume intersection
if (test.getVolume().TestAABB(i->surface->getAABB(), localToWorld) != VOLUME_OUTSIDE)
{
// Volume intersection passed, delegate the selection test
i->surface->testSelect(selector, test, localToWorld);
}
}
}
bool RenderablePicoModel::getIntersection(const Ray& ray, Vector3& intersection, const Matrix4& localToWorld)
{
Vector3 bestIntersection = ray.origin;
// Test each surface and take the nearest point to the ray origin
for (SurfaceList::iterator i = _surfVec.begin(); i != _surfVec.end(); ++i)
{
Vector3 surfaceIntersection;
if (i->surface->getIntersection(ray, surfaceIntersection, localToWorld))
{
// Test if this surface intersection is better than what we currently have
float oldDistSquared = (bestIntersection - ray.origin).getLengthSquared();
float newDistSquared = (surfaceIntersection - ray.origin).getLengthSquared();
if ((oldDistSquared == 0 && newDistSquared > 0) || newDistSquared < oldDistSquared)
{
bestIntersection = surfaceIntersection;
}
}
}
if ((bestIntersection - ray.origin).getLengthSquared() > 0)
{
intersection = bestIntersection;
return true;
}
else
{
return false;
}
}
const RenderablePicoModel::SurfaceList& RenderablePicoModel::getSurfaces() const
{
return _surfVec;
}
std::string RenderablePicoModel::getModelPath() const
{
return _modelPath;
}
void RenderablePicoModel::setModelPath(const std::string& modelPath)
{
_modelPath = modelPath;
}
void RenderablePicoModel::revertScale()
{
_scaleTransformed = _scale;
}
void RenderablePicoModel::evaluateScale(const Vector3& scale)
{
_scaleTransformed *= scale;
applyScaleToSurfaces();
}
void RenderablePicoModel::applyScaleToSurfaces()
{
_localAABB = AABB();
// Apply the scale to each surface
for (Surface& surf : _surfVec)
{
// Are we still using the original surface? If yes,
// it's now time to create a working copy
if (surf.surface == surf.originalSurface)
{
// Copy-construct the surface
surf.surface = std::make_shared<RenderablePicoSurface>(*surf.originalSurface);
}
// Apply the scale, on top of the original surface, this should save us from
// reverting the transformation each time the scale changes
surf.surface->applyScale(_scaleTransformed, *(surf.originalSurface));
// Extend the model AABB to include the surface's AABB
_localAABB.includeAABB(surf.surface->getAABB());
}
}
// Freeze transform, move the applied scale to the original model
void RenderablePicoModel::freezeScale()
{
undoSave();
// Apply the scale to each surface
_scale = _scaleTransformed;
}
void RenderablePicoModel::undoSave()
{
if (_undoStateSaver != nullptr)
{
_undoStateSaver->save(*this);
}
}
IUndoMementoPtr RenderablePicoModel::exportState() const
{
return IUndoMementoPtr(new undo::BasicUndoMemento<Vector3>(_scale));
}
void RenderablePicoModel::importState(const IUndoMementoPtr& state)
{
undoSave();
_scale = std::static_pointer_cast< undo::BasicUndoMemento<Vector3> >(state)->data();
_scaleTransformed = _scale;
applyScaleToSurfaces();
}
const Vector3& RenderablePicoModel::getScale() const
{
return _scale;
}
} // namespace