~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/components/ogre/model/ModelDefinition.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// C++ Interface: ModelDefinition
 
3
//
 
4
// Description: 
 
5
//
 
6
//
 
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2004
 
8
// Copyright (c) 2005 The Cataclysmos Team
 
9
//
 
10
// This program is free software; you can redistribute it and/or modify
 
11
// it under the terms of the GNU General Public License as published by
 
12
// the Free Software Foundation; either version 2 of the License, or
 
13
// (at your option) any later version.
 
14
// 
 
15
// This program is distributed in the hope that it will be useful,
 
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
// GNU General Public License for more details.
 
19
// 
 
20
// You should have received a copy of the GNU General Public License
 
21
// along with this program; if not, write to the Free Software
 
22
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.//
 
23
//
 
24
#ifndef EMBEROGREMODELDEFINITION_H
 
25
#define EMBEROGREMODELDEFINITION_H
 
26
 
 
27
#include "components/ogre/EmberOgrePrerequisites.h"
 
28
//#include <ext/hash_map>
 
29
 
 
30
namespace EmberOgre {
 
31
namespace Model {
 
32
 
 
33
 
 
34
class Model;
 
35
class PartDefinition;
 
36
class SubModelDefinition;
 
37
class ModelDefinition;
 
38
class SubEntityDefinition;
 
39
 
 
40
class ActionDefinition;
 
41
struct SoundDefinition;
 
42
class AnimationDefinition;
 
43
struct AnimationPartDefinition;
 
44
struct AttachPointDefinition;
 
45
struct ViewDefinition;
 
46
 
 
47
typedef std::map<std::string, Model*> ModelInstanceStore;
 
48
 
 
49
typedef std::vector<SubModelDefinition*> SubModelDefinitionsStore;
 
50
typedef std::vector<PartDefinition*> PartDefinitionsStore;
 
51
typedef std::vector<SubEntityDefinition*> SubEntityDefinitionsStore;
 
52
typedef std::vector<AnimationDefinition*> AnimationDefinitionsStore;
 
53
typedef std::vector<AnimationPartDefinition*> AnimationPartDefinitionsStore;
 
54
typedef std::vector<SoundDefinition*> SoundDefinitionsStore;
 
55
typedef std::vector<ActionDefinition*> ActionDefinitionsStore;
 
56
typedef std::vector<AttachPointDefinition> AttachPointDefinitionStore;
 
57
typedef std::map<std::string, ViewDefinition*> ViewDefinitionStore;
 
58
typedef std::map<std::string, std::string> StringParamStore;
 
59
 
 
60
/**
 
61
A rendering definition. This allows you to specify a different render method than the default one (regular Model).
 
62
All of this requires that you create functionality for implementing the different schemes that might be specified.
 
63
*/
 
64
class RenderingDefinition
 
65
{
 
66
friend class XMLModelDefinitionSerializer;
 
67
public:
 
68
 
 
69
/**
 
70
 * Gets the scheme which will be used.
 
71
 * @return 
 
72
 */
 
73
const std::string& getScheme() const;
 
74
/**
 
75
 * Sets the scheme.
 
76
 * @param scheme 
 
77
 */
 
78
void setScheme(const std::string& scheme);
 
79
/**
 
80
 * Gets a collection of parameters for the rendering scheme.
 
81
 * @return 
 
82
 */
 
83
const StringParamStore& getParameters() const;
 
84
 
 
85
private:
 
86
StringParamStore mParams;
 
87
std::string mScheme;
 
88
};
 
89
 
 
90
class SubEntityDefinition
 
91
{
 
92
friend class PartDefinition;
 
93
public:
 
94
        const std::string& getSubEntityName() const;
 
95
        unsigned int getSubEntityIndex() const;
 
96
        //void setSubEntityName(const std::string&);
 
97
 
 
98
        const std::string& getMaterialName() const;
 
99
        void setMaterialName(const std::string& materialName);
 
100
        
 
101
        PartDefinition& getPartDefinition();
 
102
 
 
103
private:
 
104
        SubEntityDefinition(const std::string& subEntityName, PartDefinition& partdef);
 
105
        SubEntityDefinition(unsigned int subEntityIndex, PartDefinition& partdef);
 
106
        std::string mSubEntityName;
 
107
        std::string mMaterialName;
 
108
        unsigned int mSubEntityIndex;
 
109
        PartDefinition& mPartDef;
 
110
};      
 
111
 
 
112
 
 
113
class PartDefinition
 
114
{
 
115
friend class SubModelDefinition;
 
116
public:
 
117
        ~PartDefinition();
 
118
 
 
119
        void setName(const std::string& name);
 
120
        const std::string& getName() const;
 
121
        
 
122
        void setGroup(const std::string& group);
 
123
        const std::string& getGroup() const;
 
124
        
 
125
        void setShow(bool show);
 
126
        bool getShow() const;
 
127
        
 
128
        SubEntityDefinition* createSubEntityDefinition(const std::string& subEntityName);
 
129
        SubEntityDefinition* createSubEntityDefinition(unsigned int subEntityIndex);
 
130
        const SubEntityDefinitionsStore& getSubEntityDefinitions();
 
131
        void removeSubEntityDefinition(SubEntityDefinition* def);
 
132
        
 
133
        SubModelDefinition& getSubModelDefinition();
 
134
        
 
135
private:
 
136
        PartDefinition(const std::string& name, SubModelDefinition& subModelDef);
 
137
        std::string mName;
 
138
        bool mShow;
 
139
        std::string mGroup;
 
140
        SubEntityDefinitionsStore mSubEntities;
 
141
        SubModelDefinition& mSubModelDef;
 
142
};
 
143
 
 
144
 
 
145
class SubModelDefinition
 
146
{
 
147
friend class ModelDefinition;
 
148
public: 
 
149
        ~SubModelDefinition();
 
150
        
 
151
        const std::string& getMeshName() const;
 
152
        
 
153
        PartDefinition* createPartDefinition(const std::string& partname);
 
154
        const PartDefinitionsStore& getPartDefinitions();
 
155
        void removePartDefinition(PartDefinition* def);
 
156
        
 
157
        ModelDefinition& getModelDefinition();
 
158
        
 
159
private:
 
160
        SubModelDefinition(const std::string& meshname, ModelDefinition& modelDef);
 
161
        std::string mMeshName;
 
162
        PartDefinitionsStore mParts;
 
163
        ModelDefinition& mModelDef;
 
164
};
 
165
 
 
166
/**
 
167
A simple struct for defining a certain view of the Model. These settings needs to be applied to the camera rendering the Model.
 
168
*/
 
169
struct ViewDefinition
 
170
{
 
171
        /**
 
172
        The name of the view.
 
173
        */
 
174
        std::string Name;
 
175
        
 
176
        /**
 
177
        The rotation of the camera related to the Model.
 
178
        */
 
179
        Ogre::Quaternion Rotation;
 
180
        
 
181
        /**
 
182
        The distance of the camera from the Model.
 
183
        */
 
184
        float Distance;
 
185
};
 
186
 
 
187
struct AttachPointDefinition
 
188
{
 
189
        std::string Name;
 
190
        std::string BoneName;
 
191
        Ogre::Quaternion Rotation;
 
192
};
 
193
 
 
194
struct AnimationPartDefinition
 
195
{
 
196
        std::string Name;
 
197
        Ogre::Real Weight;
 
198
};
 
199
 
 
200
class AnimationDefinition
 
201
{
 
202
friend class ActionDefinition;
 
203
public:
 
204
        ~AnimationDefinition();
 
205
        AnimationPartDefinition* createAnimationPartDefinition(const std::string& ogreAnimationName, Ogre::Real weight = 1);
 
206
        const AnimationPartDefinitionsStore& getAnimationPartDefinitions();
 
207
        void removeAnimationPartDefinition(AnimationPartDefinition* def);
 
208
        
 
209
        const std::string& getName() const;
 
210
        int getIterations() const;
 
211
        
 
212
private:
 
213
        AnimationDefinition(int iterations);
 
214
        
 
215
        AnimationPartDefinitionsStore mAnimationParts;
 
216
        std::string mName;
 
217
        int mIterations;
 
218
};
 
219
 
 
220
struct SoundDefinition
 
221
{
 
222
        std::string groupName;
 
223
        unsigned int playOrder;
 
224
};
 
225
 
 
226
class ActionDefinition
 
227
{
 
228
friend class ModelDefinition;
 
229
public:
 
230
        ~ActionDefinition();
 
231
        
 
232
        AnimationDefinition* createAnimationDefinition(int iterations = 1);
 
233
        const AnimationDefinitionsStore& getAnimationDefinitions();
 
234
        void removeAnimationDefinition(AnimationDefinition* def);
 
235
 
 
236
        SoundDefinition* createSoundDefinition(const std::string& groupName, unsigned int play);
 
237
 
 
238
        const SoundDefinitionsStore& getSoundDefinitions();
 
239
        void removeSoundDefinition(SoundDefinition* def);
 
240
        
 
241
        const std::string& getName() const;
 
242
        Ogre::Real getAnimationSpeed() const { return mAnimationSpeed; }
 
243
        void setAnimationSpeed(Ogre::Real speed) { mAnimationSpeed = speed; }
 
244
 
 
245
private:
 
246
        ActionDefinition(const std::string& name);
 
247
        
 
248
        
 
249
        std::string mName;
 
250
        AnimationDefinitionsStore mAnimations;
 
251
        SoundDefinitionsStore mSounds;
 
252
        Ogre::Real mAnimationSpeed;
 
253
};
 
254
 
 
255
 
 
256
/**
 
257
@author Erik Hjortsberg
 
258
*/
 
259
class ModelDefinition : public Ogre::Resource {
 
260
        
 
261
        friend class XMLModelDefinitionSerializer;
 
262
        friend class Model;
 
263
 
 
264
public:
 
265
 
 
266
        /**
 
267
        whether to use a certain axis for scaling
 
268
        for example, if you use a model of a human you probably want to scale according to the height
 
269
        this might mean that width and depths aren't correct though
 
270
        */
 
271
        enum UseScaleOf {
 
272
                MODEL_ALL = 0,
 
273
                MODEL_NONE = 1,
 
274
                MODEL_WIDTH = 2,
 
275
                MODEL_DEPTH = 3,
 
276
                MODEL_HEIGHT = 4
 
277
        };
 
278
 
 
279
        
 
280
    //th ModelDefinition(const Ogre::String& name, const Ogre::String& path);
 
281
 
 
282
        ModelDefinition(Ogre::ResourceManager* creator, const Ogre::String& name, Ogre::ResourceHandle handle,
 
283
                const Ogre::String& group, bool isManual = false, Ogre::ManualResourceLoader* loader = 0);
 
284
 
 
285
    virtual ~ModelDefinition();
 
286
 
 
287
    bool isValid(void);
 
288
        void setValid(bool valid);
 
289
 
 
290
        //Ogre resource virtual functions
 
291
        void loadImpl(void);
 
292
 
 
293
        void unloadImpl(void);
 
294
 
 
295
        size_t calculateSize(void) const;
 
296
 
 
297
        //Model* createModel(Ogre::String name, Ogre::SceneManager* sceneManager);
 
298
        
 
299
        /**
 
300
         *    Gets the amount of scale that needs to be applied to derived Models.
 
301
         * @return 
 
302
         */
 
303
        Ogre::Real getScale() const;
 
304
        void setScale(Ogre::Real scale);
 
305
        
 
306
        /**
 
307
         *    Gets how derived Models needs to be scaled.
 
308
         Defaults to "ALL"
 
309
         * @return 
 
310
         */
 
311
        const UseScaleOf getUseScaleOf() const;
 
312
        void setUseScaleOf(const UseScaleOf useScale);
 
313
        
 
314
        /**
 
315
         *    Gets an optional translation vector which should be applied to derived Models.
 
316
         * @return 
 
317
         */
 
318
        const Ogre::Vector3& getTranslate() const;
 
319
        void setTranslate(const Ogre::Vector3 translate);
 
320
        
 
321
        /**
 
322
         *      Whether contained entities should be shown or not.
 
323
         Defaults to true.
 
324
         * @return true if contained entities should be shown, else false
 
325
         */
 
326
        bool getShowContained() const;
 
327
        void getShowContained(bool show);
 
328
        
 
329
        /**
 
330
         *    If set to something else than 0, all models beyond this distance won't be shown.
 
331
         * @return 
 
332
         */
 
333
        float getRenderingDistance() const;
 
334
        void setRenderingDistance(float distance);
 
335
        
 
336
        /**
 
337
         *    Returns a vector defining how much, if ever, contained entities should be offset.
 
338
         *    If they shouldn't, Ogre::Vector3::ZERO will be returned.
 
339
         * @return A offset vector.
 
340
         */
 
341
        const Ogre::Vector3& getContentOffset() const;
 
342
        void setContentOffset(const Ogre::Vector3&);
 
343
        
 
344
        /**
 
345
         *    Gets the rotation of the model.
 
346
         * @return 
 
347
         */
 
348
        const Ogre::Quaternion& getRotation() const;
 
349
        
 
350
        /**
 
351
         *    Sets the rotation of the model.
 
352
         * @param rotation 
 
353
         */
 
354
        void setRotation(const Ogre::Quaternion rotation);
 
355
        
 
356
        
 
357
        /**
 
358
         *    Gets a path to an icon resource, if defined.
 
359
         * @return a path to an image which can be used as an icon for the model
 
360
         */
 
361
        const std::string& getIconPath() const;
 
362
        
 
363
        
 
364
        /**
 
365
         *    Creates and returns a new sub model definition for the supplied mesh name.
 
366
         * @param meshname The name of the mesh to base the new sub model on. Must be a valid mesh.
 
367
         * @return 
 
368
         */
 
369
        SubModelDefinition* createSubModelDefinition(const std::string& meshname);
 
370
        
 
371
        /**
 
372
         *    Returns all SubModelDefinitions defined.
 
373
         * @return The SubModelDefinitions store.
 
374
         */
 
375
        const SubModelDefinitionsStore& getSubModelDefinitions();
 
376
        
 
377
        /**
 
378
         *    Removes a certain SubModelDefinition.
 
379
         * @param def The definition to remove.
 
380
         */
 
381
        void removeSubModelDefinition(SubModelDefinition* def);
 
382
        
 
383
        /**
 
384
         * Creates and returns a new ActionDefintion with the given name.
 
385
         * @param actionname The name of the new ActionDefinition.
 
386
         * @return A pointer to the new ActionDefinition.
 
387
         */
 
388
        ActionDefinition* createActionDefinition(const std::string& actionname);
 
389
        
 
390
        /**
 
391
         *    Returns all ActionDefinitions defined.
 
392
         * @return 
 
393
         */
 
394
        const ActionDefinitionsStore& getActionDefinitions();
 
395
        
 
396
        /**
 
397
         *    Removes a certain ActionDefinition.
 
398
         * @param def The definition to remove.
 
399
         */
 
400
        void removeActionDefinition(ActionDefinition* def);
 
401
        
 
402
        const AttachPointDefinitionStore& getAttachPointsDefinitions();
 
403
 
 
404
        /**
 
405
        Creates and returns a new ViewDefinition with the supplied name.
 
406
        @param viewname The name of the view
 
407
        @return A pointer to the new view.
 
408
        */
 
409
        ViewDefinition* createViewDefinition(const std::string& viewname);
 
410
        
 
411
        /**
 
412
         * Returns all views defined.
 
413
         * @return 
 
414
         */
 
415
        const ViewDefinitionStore& getViewDefinitions();
 
416
        
 
417
        /**
 
418
         * Removed a named view. If no view can be found, no exception will be thrown.
 
419
         * @param name The name of the view to to remove.
 
420
         */
 
421
        void removeViewDefinition(const std::string name);
 
422
        
 
423
        
 
424
        /**
 
425
         * Utility method for removing a defintion from a non-associative stores (vector, list etc.)
 
426
         * @param def The defintion to remove.
 
427
         * @param store The store to remove from.
 
428
         */
 
429
        template <typename T, typename T1>
 
430
        static void removeDefinition(T* def, T1& store);
 
431
        
 
432
        
 
433
        /**
 
434
         *    Reloads all the Model instances that uses this definition.
 
435
         */
 
436
        void reloadAllInstances();
 
437
        
 
438
        /**
 
439
         *    Gets a pointer to the rendering scheme definition, or null if none specified.
 
440
         * @return 
 
441
         */
 
442
        const RenderingDefinition* getRenderingDefinition() const;
 
443
 
 
444
private:
 
445
 
 
446
        
 
447
        struct BindingDefinition
 
448
        {
 
449
                std::string EmitterVar;
 
450
                std::string AtlasAttribute;
 
451
        };
 
452
 
 
453
        typedef std::vector<BindingDefinition> BindingSet;
 
454
 
 
455
        struct ParticleSystemDefinition
 
456
        {
 
457
                std::string Script;
 
458
                BindingSet Bindings;
 
459
        };
 
460
 
 
461
        typedef std::vector<ParticleSystemDefinition> ParticleSystemSet;
 
462
 
 
463
        struct LightDefinition
 
464
        {
 
465
                Ogre::Light::LightTypes type;
 
466
                Ogre::ColourValue diffuseColour, specularColour;
 
467
                Ogre::Real range, constant, linear, quadratic;
 
468
                Ogre::Vector3 position;
 
469
        };
 
470
 
 
471
        typedef std::vector<LightDefinition> LightSet;
 
472
 
 
473
        
 
474
        
 
475
        /**
 
476
         *    Adds a model instance to the internal store of instances. This method should be called from the class Model when a new Model is created.
 
477
         * @param  
 
478
         */
 
479
        void addModelInstance(Model*);
 
480
        /**
 
481
         *    Removed a model instance from the internal store of instances. This method should be called from the class Model when a new Model is removed.
 
482
         * @param  
 
483
         */
 
484
        void removeModelInstance(Model*);
 
485
        
 
486
        /**
 
487
        A store of all model instances of this definition.
 
488
        This can be used to update all instances at once.
 
489
        */
 
490
        ModelInstanceStore mModelInstances;
 
491
 
 
492
        
 
493
        /**
 
494
        The minimum distance at which the model will be shown.
 
495
        */
 
496
        float mRenderingDistance;
 
497
        
 
498
        SubModelDefinitionsStore mSubModels;
 
499
        ActionDefinitionsStore mActions;
 
500
        ParticleSystemSet mParticleSystems;
 
501
        LightSet mLights;
 
502
        
 
503
        AttachPointDefinitionStore mAttachPoints;
 
504
        
 
505
        UseScaleOf mUseScaleOf;
 
506
        Ogre::Real mScale;
 
507
        Ogre::Quaternion mRotation;
 
508
//      const Ogre::String mPath;
 
509
        
 
510
        /**
 
511
        Defines how much contained entities should be offset. ZERO if not.
 
512
        */
 
513
        Ogre::Vector3 mContentOffset;
 
514
        
 
515
        /**
 
516
        Whether contained entities should be shown or not.
 
517
        Defaults to true.
 
518
        */
 
519
        bool mShowContained;
 
520
        
 
521
        /**
 
522
        How, if any, to transform the model from the base position.
 
523
        Defaults to a zeroed vector.
 
524
        */
 
525
        Ogre::Vector3 mTranslate;
 
526
        
 
527
        bool mIsValid;
 
528
        
 
529
        ViewDefinitionStore mViews;
 
530
        
 
531
        /**
 
532
        A path to an image resource which can be shown as an icon for the model.
 
533
        */
 
534
        std::string mIconPath;
 
535
        
 
536
        RenderingDefinition* mRenderingDef;
 
537
        
 
538
};
 
539
 
 
540
/** Specialisation of SharedPtr to allow SharedPtr to be assigned to ModelDefnPtr 
 
541
@note Has to be a subclass since we need operator=.
 
542
We could templatise this instead of repeating per Resource subclass, 
 
543
except to do so requires a form VC6 does not support i.e.
 
544
ResourceSubclassPtr<T> : public SharedPtr<T>
 
545
*/
 
546
class ModelDefnPtr : public Ogre::SharedPtr<ModelDefinition> 
 
547
{
 
548
public:
 
549
        ModelDefnPtr() : Ogre::SharedPtr<ModelDefinition>() {}
 
550
        explicit ModelDefnPtr(ModelDefinition* rep) : Ogre::SharedPtr<ModelDefinition>(rep) {}
 
551
        ModelDefnPtr(const ModelDefnPtr& r) : Ogre::SharedPtr<ModelDefinition>(r) {} 
 
552
        ModelDefnPtr(const Ogre::ResourcePtr& r);
 
553
        /// Operator used to convert a ResourcePtr to a ModelDefnPtr
 
554
        ModelDefnPtr& operator=(const Ogre::ResourcePtr& r);
 
555
    protected:
 
556
        /// Override destroy since we need to delete Mesh after fully defined
 
557
//         void destroy(void);
 
558
};
 
559
 
 
560
typedef ModelDefnPtr ModelDefinitionPtr;
 
561
 
 
562
 
 
563
///implementations
 
564
 
 
565
inline const Ogre::Vector3& ModelDefinition::getContentOffset() const 
 
566
{
 
567
        return mContentOffset; 
 
568
}
 
569
inline void ModelDefinition::setContentOffset(const Ogre::Vector3& offset) 
 
570
 
571
        mContentOffset = offset; 
 
572
}
 
573
 
 
574
inline void ModelDefinition::setValid(bool valid) 
 
575
{
 
576
        mIsValid = valid; 
 
577
}
 
578
 
 
579
inline size_t ModelDefinition::calculateSize(void) const 
 
580
{
 
581
        //TODO:implement this
 
582
        return 0; 
 
583
}
 
584
 
 
585
inline Ogre::Real ModelDefinition::getScale() const 
 
586
{
 
587
        return mScale; 
 
588
}
 
589
inline void ModelDefinition::setScale(Ogre::Real scale) 
 
590
{
 
591
        mScale = scale; 
 
592
}
 
593
 
 
594
inline const ModelDefinition::UseScaleOf ModelDefinition::getUseScaleOf() const 
 
595
{
 
596
        return mUseScaleOf; 
 
597
}
 
598
inline void ModelDefinition::setUseScaleOf(const UseScaleOf useScale)  
 
599
{
 
600
        mUseScaleOf = useScale; 
 
601
}
 
602
 
 
603
inline float ModelDefinition::getRenderingDistance() const
 
604
{
 
605
        return mRenderingDistance;
 
606
}
 
607
 
 
608
inline void ModelDefinition::setRenderingDistance(float distance)
 
609
{
 
610
        mRenderingDistance = distance;
 
611
}
 
612
        
 
613
inline const std::string& ModelDefinition::getIconPath() const
 
614
{
 
615
        return mIconPath;
 
616
}
 
617
 
 
618
inline int AnimationDefinition::getIterations() const
 
619
{
 
620
        return mIterations;
 
621
}
 
622
 
 
623
}
 
624
}
 
625
#endif