Cafu Engine
AnimPose.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_MODEL_ANIM_POSE_HPP_INCLUDED
8 #define CAFU_MODEL_ANIM_POSE_HPP_INCLUDED
9 
10 #include "AnimExpr.hpp"
11 #include "MaterialSystem/Mesh.hpp"
12 #include "Math3D/BoundingBox.hpp"
13 #include "Math3D/Matrix.hpp"
14 
15 
16 class CafuModelT;
17 class MaterialT;
18 
19 
20 /// This class describes a specific pose of an associated model.
21 ///
22 /// A pose is defined (or "configured") by an AnimExpressionT instance
23 /// whose application to the model yields all data that is relevant for further actions
24 /// on the model in that pose, such as rendering, tracing rays, collision detection, etc.
25 ///
26 /// The data that is derived from the anim expression is cached within the pose instance.
27 /// It comprises:
28 /// - the transformation matrices for each joint in the skeleton,
29 /// - the MatSys render meshes for each mesh,
30 /// - tangent space vectors for each vertex in each mesh.
31 /// In essence, this "externalizes" all data that is specific to a pose from
32 /// the representation of a model (the \c CafuModelT instance).
33 /// As a consequence, AnimPoseT instances should be shared whenever there are multiple users
34 /// (such as "static detail model" entity instances) that all show the same model in the same pose.
35 class AnimPoseT
36 {
37  public:
38 
39  /// The instances of this struct parallel and augment the \c CafuModelT::MeshT instances in the related \c CafuModelT.
40  struct MeshInfoT
41  {
42  struct TriangleT
43  {
44  Vector3fT Normal; ///< The normal vector of this triangle, required for the shadow-silhouette determination.
45  };
46 
47  struct VertexT
48  {
49  Vector3fT Pos; ///< The spatial position of this vertex.
50  Vector3fT Normal; ///< The tangent-space normal vector of this vertex.
51  Vector3fT Tangent; ///< The tangent-space tangent vector of this vertex.
52  Vector3fT BiNormal; ///< The tangent-space binormal vector of this vertex.
53  };
54 
55  ArrayT<TriangleT> Triangles;
56  ArrayT<VertexT> Vertices;
57  };
58 
59  /// This class describes the result of tracing a ray or a bounding box against the model.
60  struct TraceResultT
61  {
62  /// The constructor.
63  TraceResultT(float Fraction_=0.0f) : Fraction(Fraction_), Material(NULL), MeshNr(-1), TriNr(-1) { }
64 
65  float Fraction; ///< The scalar along RayDir at which the hit occurred (RayOrigin + RayDir*Fraction).
66  Vector3fT Normal; ///< This is the normal vector of the hit surface.
67  const MaterialT* Material; ///< The material at the point of impact. Can be NULL, e.g. when an edge (i.e. a bevel plane) was hit or the material is not available.
68  unsigned int MeshNr; ///< The number of the hit mesh. Can be -1 (that is, \emph{larger} then the number of meshes in the model) if the hit mesh cannot be determined.
69  unsigned int TriNr; ///< The number of the hit triangle in the hit mesh. Can be -1 (that is, \emph{larger} then the number of triangles in the mesh) if the hit triangle cannot be determined.
70  };
71 
72 
73  /// The constructor.
74  AnimPoseT(const CafuModelT& Model, IntrusivePtrT<AnimExpressionT> AnimExpr);
75 
76  /// The destructor.
77  ~AnimPoseT();
78 
79  /// Returns the current anim expression of this pose.
80  IntrusivePtrT<AnimExpressionT> GetAnimExpr() const { return m_AnimExpr; }
81 
82  /// Sets a new anim expression to use for this pose.
83  ///
84  /// \param AnimExpr The new anim expression to use for this pose.
86 
87  /// This method assigns a pose of a parent or "super" model that should be used when rendering this model.
88  ///
89  /// For example, a player model can act as the super model for a weapon,
90  /// so that the skeleton of the weapon is copied from the player model
91  /// in order to align the weapon with the hands of the player.
92  ///
93  /// @param SuperPose The super model pose that should be used when rendering this model.
94  void SetSuperPose(const AnimPoseT* SuperPose);
95 
96  /// Call this if something in the related model has changed.
97  void SetNeedsRecache() { m_CachedAE=NULL; }
98 
99  /// This method renders the model in this pose.
100  /// The current MatSys model-view matrix determines the position and orientation.
101  ///
102  /// @param SkinNr The skin to render the model with, -1 for the default skin.
103  /// @param LodDist The distance to the camera for reducing the level-of-detail.
104  void Draw(int SkinNr, float LodDist) const;
105 
106  /// Returns the origin, x-axis and y-axis vectors for the given Gui fixture.
107  /// The return value indicates whether the vectors could successfully be returned in the reference parameters
108  /// (the method can fail if e.g. the model has no such Gui fixture or the Gui fixture is improperly specified).
109  bool GetGuiPlane(unsigned int GFNr, Vector3fT& Origin, Vector3fT& AxisX, Vector3fT& AxisY) const;
110 
111  /// Traces a ray against this model in this pose, and returns whether it was hit.
112  /// The ray for the trace is defined by RayOrigin + RayDir*Fraction, where Fraction is a scalar >= 0.
113  ///
114  /// @param SkinNr The skin to use for the trace, use -1 for the default skin.
115  /// @param RayOrigin The point in model space where the ray starts.
116  /// @param RayDir A unit vector in model space that describes the direction the ray extends to.
117  /// @param Result If the model was hit, this struct contains additional details of the hit.
118  ///
119  /// @returns true if the ray hit the model, false otherwise. When the model was hit, additional details are returned via the Result parameter.
120  bool TraceRay(int SkinNr, const Vector3fT& RayOrigin, const Vector3fT& RayDir, TraceResultT& Result) const;
121 
122  /// Considers the given triangle in the given mesh, and returns the vertex that the given point P is closest to in this pose.
123  /// The returned number is the index of the vertex in the mesh, \emph{not} the (0, 1 or 2) index in the triangle.
124  unsigned int FindClosestVertex(unsigned int MeshNr, unsigned int TriNr, const Vector3fT& P) const;
125 
126  /// Returns the set of transformation matrices (one per joint) at the given sequence and frame number.
127  const ArrayT<MatrixT>& GetJointMatrices() const;
128 
129  /// Returns the transformation matrix for the joint with the given name, or \c NULL if there is no such joint.
130  const MatrixT* GetJointMatrix(const std::string& JointName) const;
131 
132  /// Returns the mesh infos with additional data for each mesh in this pose.
133  const ArrayT<MeshInfoT>& GetMeshInfos() const;
134 
135  /// Returns the MatSys meshes for the model in this pose.
136  const ArrayT<MatSys::MeshT>& GetDrawMeshes() const;
137 
138  /// Returns the bounding-box for the model in this pose.
139  const BoundingBox3fT& GetBB() const;
140 
141 
142  private:
143 
144  AnimPoseT(const AnimPoseT&); ///< Use of the Copy Constructor is not allowed.
145  void operator = (const AnimPoseT&); ///< Use of the Assignment Operator is not allowed.
146 
147  void SyncDimensions() const;
148  void UpdateJointMatrices() const;
149  void UpdateVertexPositions() const;
150  void UpdateTangentSpaceHard(unsigned long MeshNr) const;
151  void UpdateTangentSpaceGlobal(unsigned long MeshNr) const;
152  void UpdateTangentSpaceSmGroups(unsigned long MeshNr) const;
153  void Recache() const;
154 
155  const CafuModelT& m_Model; ///< The related model that this is a pose for.
156  AnimExpressionPtrT m_AnimExpr; ///< The expression that describes the skeleton pose for which we have computed the cache data.
157  const AnimPoseT* m_SuperPose;
158  AnimPoseT* m_DlodPose; ///< The next pose in the chain of dlod poses matching the chain of dlod models.
159 
160  mutable AnimExpressionPtrT m_CachedAE; ///< Used to detect if the m_AnimExpr has changed, so that our matrices, meshes etc. can be recached.
161  mutable ArrayT<MatrixT> m_JointMatrices; ///< The transformation matrices that represent the pose of the skeleton at the given animation sequence and frame number.
162  mutable ArrayT<MeshInfoT> m_MeshInfos; ///< Additional data for each mesh in m_Model.
163  mutable ArrayT<MatSys::MeshT> m_Draw_Meshes; ///< The draw meshes resulting from the m_JointMatrices.
164  mutable BoundingBox3fT m_BoundingBox; ///< The bounding-box for the model in this pose.
165 };
166 
167 #endif
unsigned int TriNr
The number of the hit triangle in the hit mesh. Can be -1 (that is, larger then the number of triangl...
Definition: AnimPose.hpp:69
void Draw(int SkinNr, float LodDist) const
This method renders the model in this pose.
Definition: AnimPose.cpp:616
This class represents a native Cafu model.
Definition: Model_cmdl.hpp:45
IntrusivePtrT< AnimExpressionT > GetAnimExpr() const
Returns the current anim expression of this pose.
Definition: AnimPose.hpp:80
~AnimPoseT()
The destructor.
Definition: AnimPose.cpp:34
Vector3fT Normal
This is the normal vector of the hit surface.
Definition: AnimPose.hpp:66
TraceResultT(float Fraction_=0.0f)
The constructor.
Definition: AnimPose.hpp:63
Definition: AnimPose.hpp:42
Vector3fT Normal
The normal vector of this triangle, required for the shadow-silhouette determination.
Definition: AnimPose.hpp:44
void SetSuperPose(const AnimPoseT *SuperPose)
This method assigns a pose of a parent or "super" model that should be used when rendering this model...
Definition: AnimPose.cpp:604
const MaterialT * Material
The material at the point of impact. Can be NULL, e.g. when an edge (i.e. a bevel plane) was hit or t...
Definition: AnimPose.hpp:67
This class represents a surface material ("A datastructural representation of a scripts material def...
Definition: Material.hpp:22
Vector3fT BiNormal
The tangent-space binormal vector of this vertex.
Definition: AnimPose.hpp:52
The instances of this struct parallel and augment the CafuModelT::MeshT instances in the related Cafu...
Definition: AnimPose.hpp:40
float Fraction
The scalar along RayDir at which the hit occurred (RayOrigin + RayDir*Fraction).
Definition: AnimPose.hpp:65
unsigned int MeshNr
The number of the hit mesh. Can be -1 (that is, larger then the number of meshes in the model) if the...
Definition: AnimPose.hpp:68
AnimPoseT(const CafuModelT &Model, IntrusivePtrT< AnimExpressionT > AnimExpr)
The constructor.
Definition: AnimPose.cpp:17
Vector3fT Tangent
The tangent-space tangent vector of this vertex.
Definition: AnimPose.hpp:51
const ArrayT< MatSys::MeshT > & GetDrawMeshes() const
Returns the MatSys meshes for the model in this pose.
Definition: AnimPose.cpp:956
bool TraceRay(int SkinNr, const Vector3fT &RayOrigin, const Vector3fT &RayDir, TraceResultT &Result) const
Traces a ray against this model in this pose, and returns whether it was hit.
Definition: AnimPose.cpp:816
const ArrayT< MatrixT > & GetJointMatrices() const
Returns the set of transformation matrices (one per joint) at the given sequence and frame number...
Definition: AnimPose.cpp:928
unsigned int FindClosestVertex(unsigned int MeshNr, unsigned int TriNr, const Vector3fT &P) const
Considers the given triangle in the given mesh, and returns the vertex that the given point P is clos...
Definition: AnimPose.cpp:904
const BoundingBox3fT & GetBB() const
Returns the bounding-box for the model in this pose.
Definition: AnimPose.cpp:964
const MatrixT * GetJointMatrix(const std::string &JointName) const
Returns the transformation matrix for the joint with the given name, or NULL if there is no such join...
Definition: AnimPose.cpp:936
void SetNeedsRecache()
Call this if something in the related model has changed.
Definition: AnimPose.hpp:97
This class describes a specific pose of an associated model.
Definition: AnimPose.hpp:35
Definition: AnimPose.hpp:47
This class describes the result of tracing a ray or a bounding box against the model.
Definition: AnimPose.hpp:60
const ArrayT< MeshInfoT > & GetMeshInfos() const
Returns the mesh infos with additional data for each mesh in this pose.
Definition: AnimPose.cpp:948
Definition: Renderer.hpp:16
This class represents an axis-aligned bounding-box ("AABB") in 3-dimensional space.
Definition: BoundingBox.hpp:23
bool GetGuiPlane(unsigned int GFNr, Vector3fT &Origin, Vector3fT &AxisX, Vector3fT &AxisY) const
Returns the origin, x-axis and y-axis vectors for the given Gui fixture.
Definition: AnimPose.cpp:780
void SetAnimExpr(IntrusivePtrT< AnimExpressionT > AnimExpr)
Sets a new anim expression to use for this pose.
Definition: AnimPose.cpp:40
Vector3fT Pos
The spatial position of this vertex.
Definition: AnimPose.hpp:49
Vector3fT Normal
The tangent-space normal vector of this vertex.
Definition: AnimPose.hpp:50