xoreos  0.0.5
model.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef GRAPHICS_AURORA_MODEL_H
26 #define GRAPHICS_AURORA_MODEL_H
27 
28 #include <vector>
29 #include <list>
30 #include <map>
31 
32 #include "glm/mat4x4.hpp"
33 
34 #include "src/common/ustring.h"
35 #include "src/common/boundingbox.h"
36 
37 #include "src/graphics/types.h"
40 
43 
45 
46 namespace Common {
47  class SeekableReadStream;
48 }
49 
50 namespace Graphics {
51 
52 namespace Aurora {
53 
54 class Animation;
55 class AnimationThread;
56 class AnimationChannel;
57 
61 };
62 
63 class Model : public GLContainer, public Renderable {
64 public:
66  ~Model();
67 
68  void show();
69  void hide();
70 
71  ModelType getType() const;
72 
74  const Common::UString &getName() const;
75 
76  float getWidth () const;
77  float getHeight() const;
78  float getDepth () const;
79 
81  void drawBound(bool enabled);
83  void drawSkeleton(bool enabled, bool showInvisible);
84 
86  void setEnvironmentMap(const Common::UString &environmentMap = "");
87 
89  void setSkinned(bool skinned);
90 
92  bool isIn(float x, float y) const;
94  bool isIn(float x, float y, float z) const;
96  bool isIn(float x1, float y1, float z1, float x2, float y2, float z2) const;
97 
98 
99  // Positioning
100 
102  void getScale (float &x, float &y, float &z) const;
104  void getOrientation(float &x, float &y, float &z, float &angle) const;
106  void getPosition(float &x, float &y, float &z) const;
107 
109  void getAbsolutePosition(float &x, float &y, float &z) const;
110 
112  void setScale (float x, float y, float z);
114  void setOrientation(float x, float y, float z, float angle);
116  void setPosition(float x, float y, float z);
117 
119  void scale (float x, float y, float z);
121  void rotate(float x, float y, float z, float angle);
123  void move (float x, float y, float z);
124 
126  void getTooltipAnchor(float &x, float &y, float &z) const;
127 
128  // States
129 
131  const std::list<Common::UString> &getStates() const;
133  void setState(const Common::UString &name = "");
135  const Common::UString &getState() const;
136 
137 
138  // Nodes
139 
141  bool hasNode(const Common::UString &node) const;
142 
144  ModelNode *getNode(const Common::UString &node);
146  const ModelNode *getNode(const Common::UString &node) const;
148  ModelNode *getNode(const Common::UString &stateName, const Common::UString &node);
150  const ModelNode *getNode(const Common::UString &stateName, const Common::UString &node) const;
152  ModelNode *getNode(uint16 nodeNumber);
154  const ModelNode *getNode(uint16 nodeNumber) const;
155 
157  const std::list<ModelNode *> &getNodes();
158 
160  void attachModel(const Common::UString &nodeName, Model *model);
161 
162  // Animation
163 
165  bool hasAnimation(const Common::UString &anim) const;
166 
168  float getAnimationScale(const Common::UString &anim);
169 
172 
173  void clearDefaultAnimations();
174  void addDefaultAnimation(const Common::UString &anim, uint8 probability);
175  void playDefaultAnimation();
176 
177  void playAnimation(const Common::UString &anim,
178  bool restart = true,
179  float length = 0.0f,
180  float speed = 1.0f);
181 
182 
183  // Renderable
184  void calculateDistance();
185  void render(RenderPass pass);
186  void renderImmediate(const glm::mat4 &parentTransform);
187  void queueRender(const glm::mat4 &parentTransform);
188  void advanceTime(float dt);
189 
191  void flushNodeBuffers();
192 
193  Model *getAttachedModel(const Common::UString &node);
194 
195 
196 protected:
197  typedef std::list<ModelNode *> NodeList;
198  typedef std::map<Common::UString, ModelNode *, Common::UString::iless> NodeMap;
199  typedef std::map<Common::UString, Animation *, Common::UString::iless> AnimationMap;
200  typedef std::map<AnimationChannelName, AnimationChannel *> AnimationChannelMap;
201 
203  struct State {
205 
208 
210  };
211 
212  typedef std::list<State *> StateList;
213  typedef std::map<Common::UString, State *> StateMap;
214 
215 
217 
219 
221 
224 
228 
229  std::list<Common::UString> _stateNames;
230 
232 
234 
236 
237  float _scale [3];
238  float _orientation[4];
239  float _position [3];
240 
241  float _center[3];
242 
243  glm::mat4 _absolutePosition;
244 
249 
250  bool _skinned;
252 
253 
254  // Rendering
255  void queueDrawBound();
256  void doDrawBound();
257  void doDrawSkeleton();
258 
259  // Animation
260 
262  Animation *getAnimation(const Common::UString &anim);
263 
264 
266  void finalize();
267 
268 
269  // GLContainer
270  void doRebuild();
271  void doDestroy();
272 
273  // Shader renderable, containing information on rendering something.
275  glm::mat4 _boundTransform;
276 
277 private:
281 
282  std::map<Common::UString, Model *> _attachedModels;
283 
285  void createStateNamesList(std::list<Common::UString> *stateNames = 0);
287  void createBound();
288 
289  void createAbsolutePosition();
290 
291  void manageAnimations(float dt);
292 
293 public:
294  // General loading helpers
295 
296  static void readValue(Common::SeekableReadStream &stream, uint32 &value);
297  static void readValue(Common::SeekableReadStream &stream, float &value);
298 
299  static void readArrayDef(Common::SeekableReadStream &stream,
300  uint32 &offset, uint32 &count);
301 
302  template<typename T>
303  static void readArray(Common::SeekableReadStream &stream,
304  uint32 offset, uint32 count, std::vector<T> &values);
305 
306  friend class ModelNode;
307  friend class Animation;
308  friend class AnimationThread;
309  friend class AnimationChannel;
310 };
311 
312 } // End of namespace Aurora
313 
314 } // End of namespace Graphics
315 
316 #endif // GRAPHICS_AURORA_MODEL_H
static void readValue(Common::SeekableReadStream &stream, uint32 &value)
Definition: model.cpp:878
Shader::ShaderRenderable _boundRenderable
Definition: model.h:274
const Common::UString & getState() const
Return the name of the current state.
Definition: model.cpp:361
NodeMap nodeMap
The nodes within the state, indexed by name.
Definition: model.h:207
void getPosition(float &x, float &y, float &z) const
Get the current position of the model.
Definition: model.cpp:207
float getHeight() const
Get the height of the model&#39;s bounding box.
Definition: model.cpp:166
Model * getAttachedModel(const Common::UString &node)
Definition: model.cpp:611
Common::BoundingBox _absoluteBoundBox
The model&#39;s box after translate/rotate.
Definition: model.h:248
Shader renderable, a class for easier managing of a collection of items (surface, material...
Definition: 2dafile.h:39
void scale(float x, float y, float z)
Scale the model, relative to its current scale.
Definition: model.cpp:265
A class holding an UTF-8 string.
Definition: ustring.h:48
void advanceTime(float dt)
Advance time (used by renderables with animations).
Definition: model.cpp:589
float getWidth() const
Get the width of the model&#39;s bounding box.
Definition: model.cpp:162
uint8_t uint8
Definition: types.h:200
std::map< Common::UString, ModelNode *, Common::UString::iless > NodeMap
Definition: model.h:198
std::map< Common::UString, Model * > _attachedModels
Definition: model.h:282
Common::UString name
The state&#39;s name.
Definition: model.h:204
A real object in the game world.
Definition: types.h:52
float getAnimationScale(const Common::UString &anim)
Determine what animation scaling applies.
Definition: model.cpp:513
NodeList rootNodes
The nodes in the state without a parent.
Definition: model.h:209
void hide()
Hide the object.
Definition: model.cpp:116
State * _currentState
The current state.
Definition: model.h:227
void move(float x, float y, float z)
Move the model, relative to its current position.
Definition: model.cpp:289
std::list< Common::UString > _stateNames
All state names.
Definition: model.h:229
Basic graphics types.
const Common::UString & getName() const
Get the model&#39;s name.
Definition: model.cpp:125
Common::UString _fileName
The model&#39;s file name.
Definition: model.h:218
A bounding box around 3D points.
Definition: boundingbox.h:33
void clearDefaultAnimations()
Definition: model.cpp:545
void addAnimationChannel(AnimationChannelName name)
Definition: model.cpp:527
A container of OpenGL elements.
Common::UString _name
The model&#39;s name.
Definition: model.h:220
void setState(const Common::UString &name="")
Set the current animation state.
Definition: model.cpp:324
void createAbsolutePosition()
Definition: model.cpp:303
std::map< Common::UString, Animation *, Common::UString::iless > AnimationMap
Definition: model.h:199
Common::BoundingBox _boundBox
The model&#39;s bounding box.
Definition: model.h:246
void setOrientation(float x, float y, float z, float angle)
Set the current orientation of the model.
Definition: model.cpp:234
void drawBound(bool enabled)
Should a bounding box be drawn around this model?
Definition: model.cpp:174
RenderPass
Definition: types.h:97
float _position[3]
Model&#39;s position.
Definition: model.h:239
AnimationMap _animationMap
Map of all animations in this model.
Definition: model.h:231
ModelType
The display type of a model.
Definition: types.h:51
ModelType _type
The model&#39;s type.
Definition: model.h:216
std::map< AnimationChannelName, AnimationChannel * > AnimationChannelMap
Definition: model.h:200
uint16_t uint16
Definition: types.h:202
bool _drawSkeletonInvisible
Definition: model.h:280
Basic Aurora graphics types.
void getScale(float &x, float &y, float &z) const
Get the current scale of the model.
Definition: model.cpp:193
static void readArray(Common::SeekableReadStream &stream, uint32 offset, uint32 count, std::vector< T > &values)
Definition: model.cpp:902
Model(ModelType type=kModelTypeObject)
Definition: model.cpp:61
A node within a 3D model.
void createStateNamesList(std::list< Common::UString > *stateNames=0)
Create the list of all state names.
Definition: model.cpp:826
void rotate(float x, float y, float z, float angle)
Rotate the model, relative to its current orientation.
Definition: model.cpp:269
bool hasNode(const Common::UString &node) const
Does the specified node exist in the current state?
Definition: model.cpp:368
bool isIn(float x, float y) const
Is that point within the model&#39;s bounding box?
Definition: model.cpp:129
const std::list< ModelNode * > & getNodes()
Get all nodes in the current state.
Definition: model.cpp:472
A bounding box.
NodeList nodeList
The nodes within the state.
Definition: model.h:206
void getAbsolutePosition(float &x, float &y, float &z) const
Get the position of the node after translate/rotate.
Definition: model.cpp:213
void setPosition(float x, float y, float z)
Set the current position of the model.
Definition: model.cpp:250
void playDefaultAnimation()
Definition: model.cpp:555
StateMap _stateMap
All states within this model, index by name.
Definition: model.h:226
AnimationChannel * getAnimationChannel(AnimationChannelName name)
Definition: model.cpp:537
std::list< ModelNode * > NodeList
Definition: model.h:197
float _scale[3]
Model&#39;s scale.
Definition: model.h:237
AnimationChannelName
Definition: model.h:58
void render(RenderPass pass)
Render the object.
Definition: model.cpp:632
Unicode string handling.
An object that can be displayed by the graphics manager.
Definition: renderable.h:42
AnimationChannelMap _animationChannels
Definition: model.h:233
float _orientation[4]
Model&#39;s orientation.
Definition: model.h:238
void createBound()
Create the model&#39;s bounding box.
Definition: model.cpp:848
float getDepth() const
Get the depth of the model&#39;s bounding box.
Definition: model.cpp:170
void getTooltipAnchor(float &x, float &y, float &z) const
Get the point where the feedback tooltip is anchored.
Definition: model.cpp:293
void show()
Show the object.
Definition: model.cpp:111
void playAnimation(const Common::UString &anim, bool restart=true, float length=0.0f, float speed=1.0f)
Definition: model.cpp:560
std::map< Common::UString, State * > StateMap
Definition: model.h:213
const std::list< Common::UString > & getStates() const
Return a list of all animation state names.
Definition: model.cpp:320
uint32_t uint32
Definition: types.h:204
static void readArrayDef(Common::SeekableReadStream &stream, uint32 &offset, uint32 &count)
Definition: model.cpp:886
void setEnvironmentMap(const Common::UString &environmentMap="")
Change the environment map on this model.
Definition: model.cpp:183
float _center[3]
Model&#39;s center.
Definition: model.h:241
void getOrientation(float &x, float &y, float &z, float &angle) const
Get the current orientation of the model.
Definition: model.cpp:199
Model * _superModel
The actual super model.
Definition: model.h:223
glm::mat4 _absolutePosition
Definition: model.h:243
void manageAnimations(float dt)
Definition: model.cpp:620
A container of OpenGL elements.
Definition: glcontainer.h:35
bool hasAnimation(const Common::UString &anim) const
Does this model have this named animation?
Definition: model.cpp:509
void finalize()
Finalize the loading procedure.
Definition: model.cpp:807
std::list< State * > StateList
Definition: model.h:212
void addDefaultAnimation(const Common::UString &anim, uint8 probability)
Definition: model.cpp:550
ModelType getType() const
Return the model&#39;s type.
Definition: model.cpp:121
#define pass
Definition: fft.cpp:257
Animation * getAnimation(const Common::UString &anim)
Get the animation from its name.
Definition: model.cpp:496
void flushNodeBuffers()
Apply buffered changes to model nodes position and geometry.
Definition: model.cpp:594
void calculateDistance()
Calculate the object&#39;s distance.
Definition: model.cpp:565
void queueRender(const glm::mat4 &parentTransform)
Queue the object for later rendering.
Definition: model.cpp:681
ModelNode * getNode(const Common::UString &node)
Get the specified node, from the current state.
Definition: model.cpp:379
StateList _stateList
All states within this model.
Definition: model.h:225
float _animationScale
The scale of the animation.
Definition: model.h:235
An object that can be displayed by the graphics manager.
Common::UString _superModelName
Name of the super model.
Definition: model.h:222
Interface for a seekable & readable data stream.
Definition: readstream.h:265
void setScale(float x, float y, float z)
Set the current scale of the model.
Definition: model.cpp:219
void drawSkeleton(bool enabled, bool showInvisible)
Should a skeleton showing the nodes and their relation be drawn inside the model? ...
Definition: model.cpp:178
void attachModel(const Common::UString &nodeName, Model *model)
Add another model as a child to the named node.
Definition: model.cpp:479
void renderImmediate(const glm::mat4 &parentTransform)
For shader based systems, don&#39;t sort anything, render this right_now.
Definition: model.cpp:666
void setSkinned(bool skinned)
Set the flag if the model has skinned animations.
Definition: model.cpp:616
glm::mat4 _boundTransform
Definition: model.h:275