/
modeldrawable.h
480 lines (403 loc) · 14.9 KB
/
modeldrawable.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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/** @file modeldrawable.h Drawable specialized for 3D models.
*
* @authors Copyright (c) 2014-2015 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef LIBGUI_MODELDRAWABLE_H
#define LIBGUI_MODELDRAWABLE_H
#include <de/Asset>
#include <de/File>
#include <de/GLProgram>
#include <de/GLState>
#include <de/AtlasTexture>
#include <de/Vector>
#include <QBitArray>
#include <QVariant>
#include <functional>
namespace de {
class GLBuffer;
/**
* Drawable that is constructed out of a 3D model.
*
* 3D model data is loaded using the Open Asset Import Library from multiple
* different source formats.
*
* Lifetime.
*
* Texture maps.
*
* Animation.
*
* @todo Refactor: Split the non-Assimp specific parts into a MeshDrawable base
* class, so it can be used with meshes generated procedurally (e.g., the map),
* taking advantage of the rendering pass and instancing features.
*
* @ingroup gl
*/
class LIBGUI_PUBLIC ModelDrawable : public AssetGroup
{
public:
/// An error occurred during the loading of the model data. @ingroup errors
DENG2_ERROR(LoadError);
/// There was a shader program related problem. @ingroup errors
DENG2_ERROR(ProgramError);
enum TextureMap // note: enum values used as indices internally
{
Diffuse = 0, ///< Surface color and opacity.
Normals = 1, /**< Normal map where RGB values are directly interpreted
as vectors. Blue 255 is Z+1 meaning straight up.
Color value 128 means zero. The default normal vector
pointing straight away from the surface is therefore
(128, 128, 255) => (0, 0, 1). */
Specular = 2, ///< Specular color (RGB) and reflection sharpness (A).
Emissive = 3, /**< Additional light emitted by the surface that is not
affected by external factors. */
Height = 4, /**< Height values are converted to a normal map. Lighter
regions are higher than dark regions. */
Unknown
};
static TextureMap textToTextureMap(String const &text);
static String textureMapToText(TextureMap map);
/**
* Animation state for a model. There can be any number of ongoing animations,
* targeting individual nodes of a model.
*
* @ingroup gl
*/
class LIBGUI_PUBLIC Animator
{
public:
/**
* Specialized animators may derive from OngoingSequence to extend the amount of
* data associated with each running animation sequence.
*/
class LIBGUI_PUBLIC OngoingSequence
{
public:
enum Flag
{
ClampToDuration = 0x1,
Defaults = 0
};
Q_DECLARE_FLAGS(Flags, Flag)
public:
int animId; ///< Which animation to use in a ModelDrawable.
ddouble time; ///< Animation time.
ddouble duration; ///< Animation duration.
String node; ///< Target node.
Flags flags;
public:
virtual ~OngoingSequence() {}
/**
* Called after the basic parameters of the animation have been set for
* a newly constructed sequence.
*/
virtual void initialize();
DENG2_AS_IS_METHODS()
/**
* Determines if the sequence is at its duration or past it.
*/
bool atEnd() const;
/**
* Constructs an OngoingSequence instance. This is used by default if no other
* constructor is provided.
*
* @return OngoingSequence instance, to be owned by ModelDrawable::Animator.
*/
static OngoingSequence *make();
};
typedef std::function<OngoingSequence * ()> Constructor;
/// Referenced node or animation was not found in the model. @ingroup errors
DENG2_ERROR(InvalidError);
public:
Animator(Constructor sequenceConstructor = OngoingSequence::make);
Animator(ModelDrawable const &model,
Constructor sequenceConstructor = OngoingSequence::make);
virtual ~Animator() {}
void setModel(ModelDrawable const &model);
/**
* Returns the model with which this animation is being used.
*/
ModelDrawable const &model() const;
/**
* Returns the number of ongoing animations.
*/
int count() const;
inline bool isEmpty() const { return !count(); }
OngoingSequence const &at(int index) const;
OngoingSequence &at(int index);
bool isRunning(String const &animName, String const &rootNode = "") const;
bool isRunning(int animId, String const &rootNode = "") const;
OngoingSequence *find(String const &rootNode = "") const;
OngoingSequence *find(int animId, String const &rootNode = "") const;
/**
* Starts an animation sequence. A previous sequence running on this node will
* be automatically stopped.
*
* @param animName Animation sequence name.
* @param rootNode Animation root.
*
* @return OngoingSequence.
*/
OngoingSequence &start(String const &animName, String const &rootNode = "");
/**
* Starts an animation sequence. A previous sequence running on this node will
* be automatically stopped.
*
* @param animId Animation sequence number.
* @param rootNode Animation root.
*
* @return OngoingSequence.
*/
OngoingSequence &start(int animId, String const &rootNode = "");
void stop(int index);
void clear();
/**
* Advances the animation state. Progresses ongoing animations and possibly
* triggers new ones.
*
* @param elapsed Duration of elapsed time.
*/
virtual void advanceTime(TimeDelta const &elapsed);
/**
* Returns the time to be used when drawing the model.
*
* @param index Animation index.
*
* @return Time in the model's animation sequence.
*/
virtual ddouble currentTime(int index) const;
private:
DENG2_PRIVATE(d)
};
/**
* Interface for image loaders that provide the content for texture images
* when given a path. The default loader just checks if there is an image
* file in the file system at the given path.
*/
class LIBGUI_PUBLIC IImageLoader
{
public:
virtual ~IImageLoader() {}
/**
* Loads an image. If the image can't be loaded, the loader must throw an
* exception explaining the reason for the failure.
*
* @param path Path of the image. This is an absolute de::FS path inferred from
* the location of the source model file and the material metadata
* it contains.
*
* @return Loaded image.
*/
virtual Image loadImage(String const &path) = 0;
};
/**
* Rendering pass. When no rendering passes are specified, all the meshes
* of the model are rendered in one pass with regular alpha blending.
*/
struct LIBGUI_PUBLIC Pass
{
enum Flag
{
DefaultFlags = 0
};
Q_DECLARE_FLAGS(Flags, Flag)
String name;
Flags flags = DefaultFlags;
QBitArray meshes; ///< One bit per model mesh.
GLProgram *program = nullptr; ///< Shading program.
gl::BlendFunc blendFunc { gl::SrcAlpha, gl::OneMinusSrcAlpha };
gl::BlendOp blendOp = gl::Add;
};
typedef QList<Pass> Passes;
// Audiences:
DENG2_DEFINE_AUDIENCE2(AboutToGLInit, void modelAboutToGLInit(ModelDrawable &))
enum ProgramBinding { AboutToBind, Unbound };
typedef std::function<void (GLProgram &, ProgramBinding)> ProgramBindingFunc;
enum PassState { PassBegun, PassEnded };
typedef std::function<void (Pass const &, PassState)> RenderingPassFunc;
public:
ModelDrawable();
/**
* Sets the object responsible for loading texture images.
*
* By default, ModelDrawable uses a simple loader that tries to load image
* files directly from the file system.
*
* @param loader Image loader.
*/
void setImageLoader(IImageLoader &loader);
void useDefaultImageLoader();
/**
* Releases all the data: the loaded model and any GL resources.
*/
void clear();
/**
* Loads a model from a file. This is a synchronous operation and may take
* a while, but can be called in a background thread.
*
* After loading, you must call glInit() before drawing it. glInit() will be
* called automatically if needed.
*
* @param file Model file to load.
*/
void load(File const &file);
/**
* Finds the id of an animation that has the name @a name. Note that
* animation names are optional.
*
* @param name Animation name.
*
* @return Animation id, or -1 if not found.
*/
int animationIdForName(String const &name) const;
int animationCount() const;
int meshCount() const;
/**
* Returns the number of the mesh with the given name.
*
* @param name Mesh name.
*
* @return Mesh id, or -1 if no mesh with that name exists.
*/
int meshId(String const &name) const;
/**
* Locates a material specified in the model by its name.
*
* @param name Name of the material
*
* @return Material id.
*/
int materialId(String const &name) const;
bool nodeExists(String const &name) const;
/**
* Atlas to use for any textures needed by the model. This is needed for
* glInit().
*
* @param atlas Atlas for model textures.
*/
void setAtlas(AtlasTexture &atlas);
/**
* Removes the model's atlas. All allocations this model has made from the atlas
* are freed.
*/
void unsetAtlas();
typedef QList<TextureMap> Mapping;
/**
* Sets which textures are to be passed to the model shader via the GL buffer.
*
* By default, the model only has a diffuse map. The user of ModelDrawable
* must specify the indices for the other texture maps depending on how the
* shader expects to receive them.
*
* @param mapsToUse Up to four map types. The map at index zero will be specified
* as the first texture bounds (@c aBounds in the shader), index
* one will become the second texture bounds (@c aBounds2), etc.
*/
void setTextureMapping(Mapping mapsToUse);
static Mapping diffuseNormalsSpecularEmission();
/**
* Sets the texture map that is used if no other map is provided.
*
* @param textureType Type of the texture.
* @param atlasId Identifier in the atlas.
*/
void setDefaultTexture(TextureMap textureType, Id const &atlasId);
/**
* Prepares a loaded model for drawing by constructing all the required GL
* objects.
*
* This method will be called automatically when needed, however you can
* also call it manually at a suitable time. Only call this from the main
* (UI) thread.
*/
void glInit();
/**
* Releases all the GL resources of the model.
*/
void glDeinit();
/**
* Sets or changes one of the texture maps used by the model. This can be
* used to override the maps set up automatically by glInit() (which gets
* information from the model file).
*
* @param materialId Which material to modify.
* @param textureMap Texture to set.
* @param path Path of the texture image.
*/
void setTexturePath(int materialId, TextureMap textureMap, String const &path);
/**
* Sets or changes one of the texture maps in an alternative material.
* By default, alternative maps are not in use.
*
* @param altMaterialName
* @param textureMap
* @param path
*/
/*void setTexturePath(String const &altMaterialName, TextureMap textureMap,
String const &path);*/
/**
* Sets the GL program used for shading the model. This program is used if the
* rendering passes don't specify other shaders.
*
* @param program GL program.
*/
void setProgram(GLProgram *program);
GLProgram *program() const;
/**
* Draws the model.
*
* @param animation Animation state.
* @param drawPasses Rendering passes. If omitted, all meshes are drawn
* with normal alpha blending.
* @param passMask Sets a mask that specifies which rendering passes are
* enabled. Each bit in the array corresponds to an
* element in @a drawPasses. An empty mask (size zero)
* means that all passes are enabled.
* @param programCallback
* @param passCallback
*/
void draw(Animator const *animation = nullptr,
Passes const *drawPasses = nullptr,
QBitArray const &passMask = QBitArray(),
ProgramBindingFunc programCallback = ProgramBindingFunc(),
RenderingPassFunc passCallback = RenderingPassFunc()) const;
void drawInstanced(GLBuffer const &instanceAttribs,
Animator const *animation = nullptr) const;
/**
* When a draw operation is ongoing, returns the current rendering pass.
* Otherwise returns nullptr.
*/
Pass const *currentPass() const;
/**
* When a draw operation is ongoing, returns the current GL program.
* Otherwise returns nullptr.
*/
GLProgram *currentProgram() const;
/**
* Dimensions of the default pose, in model space.
*/
Vector3f dimensions() const;
/**
* Center of the default pose, in model space.
*/
Vector3f midPoint() const;
private:
DENG2_PRIVATE(d)
};
Q_DECLARE_OPERATORS_FOR_FLAGS(ModelDrawable::Pass::Flags)
Q_DECLARE_OPERATORS_FOR_FLAGS(ModelDrawable::Animator::OngoingSequence::Flags)
} // namespace de
#endif // LIBGUI_MODELDRAWABLE_H