Model.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/ArrayPtr.h"
  24. #include "../Container/Ptr.h"
  25. #include "../Graphics/GraphicsDefs.h"
  26. #include "../Atomic3D/Skeleton.h"
  27. #include "../Math/BoundingBox.h"
  28. #include "../Resource/Resource.h"
  29. #include "Animation.h"
  30. namespace Atomic
  31. {
  32. class Geometry;
  33. class IndexBuffer;
  34. class Graphics;
  35. class VertexBuffer;
  36. /// Vertex buffer morph data.
  37. struct VertexBufferMorph
  38. {
  39. /// Vertex elements.
  40. unsigned elementMask_;
  41. /// Number of vertices.
  42. unsigned vertexCount_;
  43. /// Morphed vertices data size as bytes.
  44. unsigned dataSize_;
  45. /// Morphed vertices. Stored packed as <index, data> pairs.
  46. SharedArrayPtr<unsigned char> morphData_;
  47. };
  48. /// Definition of a model's vertex morph.
  49. struct ModelMorph
  50. {
  51. /// Morph name.
  52. String name_;
  53. /// Morph name hash.
  54. StringHash nameHash_;
  55. /// Current morph weight.
  56. float weight_;
  57. /// Morph data per vertex buffer.
  58. HashMap<unsigned, VertexBufferMorph> buffers_;
  59. };
  60. /// Description of vertex buffer data for asynchronous loading.
  61. struct VertexBufferDesc
  62. {
  63. /// Vertex count.
  64. unsigned vertexCount_;
  65. /// Element mask.
  66. unsigned elementMask_;
  67. /// Vertex data size.
  68. unsigned dataSize_;
  69. /// Vertex data.
  70. SharedArrayPtr<unsigned char> data_;
  71. };
  72. /// Description of index buffer data for asynchronous loading.
  73. struct IndexBufferDesc
  74. {
  75. /// Index count.
  76. unsigned indexCount_;
  77. /// Index size.
  78. unsigned indexSize_;
  79. /// Index data size.
  80. unsigned dataSize_;
  81. /// Index data.
  82. SharedArrayPtr<unsigned char> data_;
  83. };
  84. /// Description of a geometry for asynchronous loading.
  85. struct GeometryDesc
  86. {
  87. /// Primitive type.
  88. PrimitiveType type_;
  89. /// Vertex buffer ref.
  90. unsigned vbRef_;
  91. /// Index buffer ref.
  92. unsigned ibRef_;
  93. /// Index start.
  94. unsigned indexStart_;
  95. /// Index count.
  96. unsigned indexCount_;
  97. };
  98. // ATOMIC BEGIN
  99. static const unsigned MODEL_VERSION = 1;
  100. // ATOMIC END
  101. /// 3D model resource.
  102. class ATOMIC_API Model : public Resource
  103. {
  104. OBJECT(Model);
  105. public:
  106. /// Construct.
  107. Model(Context* context);
  108. /// Destruct.
  109. virtual ~Model();
  110. /// Register object factory.
  111. static void RegisterObject(Context* context);
  112. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  113. virtual bool BeginLoad(Deserializer& source);
  114. /// Finish resource loading. Always called from the main thread. Return true if successful.
  115. virtual bool EndLoad();
  116. /// Save resource. Return true if successful.
  117. virtual bool Save(Serializer& dest) const;
  118. /// Set local-space bounding box.
  119. void SetBoundingBox(const BoundingBox& box);
  120. /// Set vertex buffers and their morph ranges.
  121. bool SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, const PODVector<unsigned>& morphRangeStarts,
  122. const PODVector<unsigned>& morphRangeCounts);
  123. /// Set index buffers.
  124. bool SetIndexBuffers(const Vector<SharedPtr<IndexBuffer> >& buffers);
  125. /// Set number of geometries.
  126. void SetNumGeometries(unsigned num);
  127. /// Set number of LOD levels in a geometry.
  128. bool SetNumGeometryLodLevels(unsigned index, unsigned num);
  129. /// Set geometry.
  130. bool SetGeometry(unsigned index, unsigned lodLevel, Geometry* geometry);
  131. /// Set geometry center.
  132. bool SetGeometryCenter(unsigned index, const Vector3& center);
  133. /// Set skeleton.
  134. void SetSkeleton(const Skeleton& skeleton);
  135. /// Set bone mappings when model has more bones than the skinning shader can handle.
  136. void SetGeometryBoneMappings(const Vector<PODVector<unsigned> >& mappings);
  137. /// Set vertex morphs.
  138. void SetMorphs(const Vector<ModelMorph>& morphs);
  139. /// Clone the model. The geometry data is deep-copied and can be modified in the clone without affecting the original.
  140. SharedPtr<Model> Clone(const String& cloneName = String::EMPTY) const;
  141. /// Return bounding box.
  142. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  143. /// Return skeleton.
  144. Skeleton& GetSkeleton() { return skeleton_; }
  145. /// Return vertex buffers.
  146. const Vector<SharedPtr<VertexBuffer> >& GetVertexBuffers() const { return vertexBuffers_; }
  147. /// Return index buffers.
  148. const Vector<SharedPtr<IndexBuffer> >& GetIndexBuffers() const { return indexBuffers_; }
  149. /// Return number of geometries.
  150. unsigned GetNumGeometries() const { return geometries_.Size(); }
  151. /// Return number of LOD levels in geometry.
  152. unsigned GetNumGeometryLodLevels(unsigned index) const;
  153. /// Return geometry pointers.
  154. const Vector<Vector<SharedPtr<Geometry> > >& GetGeometries() const { return geometries_; }
  155. /// Return geometry center points.
  156. const PODVector<Vector3>& GetGeometryCenters() const { return geometryCenters_; }
  157. /// Return geometry by index and LOD level. The LOD level is clamped if out of range.
  158. Geometry* GetGeometry(unsigned index, unsigned lodLevel) const;
  159. /// Return geometry center by index.
  160. const Vector3& GetGeometryCenter(unsigned index) const
  161. {
  162. return index < geometryCenters_.Size() ? geometryCenters_[index] : Vector3::ZERO;
  163. }
  164. /// Return geometery bone mappings.
  165. const Vector<PODVector<unsigned> >& GetGeometryBoneMappings() const { return geometryBoneMappings_; }
  166. /// Return vertex morphs.
  167. const Vector<ModelMorph>& GetMorphs() const { return morphs_; }
  168. /// Return number of vertex morphs.
  169. unsigned GetNumMorphs() const { return morphs_.Size(); }
  170. /// Return vertex morph by index.
  171. const ModelMorph* GetMorph(unsigned index) const;
  172. /// Return vertex morph by name.
  173. const ModelMorph* GetMorph(const String& name) const;
  174. /// Return vertex morph by name hash.
  175. const ModelMorph* GetMorph(StringHash nameHash) const;
  176. /// Return vertex buffer morph range start.
  177. unsigned GetMorphRangeStart(unsigned bufferIndex) const;
  178. /// Return vertex buffer morph range vertex count.
  179. unsigned GetMorphRangeCount(unsigned bufferIndex) const;
  180. // ATOMIC BEGIN
  181. void AddAnimationResource(Animation* animation);
  182. void RemoveAnimationResource(Animation* animation);
  183. void ClearAnimationResources();
  184. unsigned GetAnimationCount() const { return animationsResources_.Size(); }
  185. const Vector<SharedPtr<Animation>>& GetAnimationResources() { return animationsResources_; }
  186. // ATOMIC END
  187. private:
  188. /// Bounding box.
  189. BoundingBox boundingBox_;
  190. /// Skeleton.
  191. Skeleton skeleton_;
  192. /// Vertex buffers.
  193. Vector<SharedPtr<VertexBuffer> > vertexBuffers_;
  194. /// Index buffers.
  195. Vector<SharedPtr<IndexBuffer> > indexBuffers_;
  196. /// Geometries.
  197. Vector<Vector<SharedPtr<Geometry> > > geometries_;
  198. /// Geometry bone mappings.
  199. Vector<PODVector<unsigned> > geometryBoneMappings_;
  200. /// Geometry centers.
  201. PODVector<Vector3> geometryCenters_;
  202. /// Vertex morphs.
  203. Vector<ModelMorph> morphs_;
  204. /// Vertex buffer morph range start.
  205. PODVector<unsigned> morphRangeStarts_;
  206. /// Vertex buffer morph range vertex count.
  207. PODVector<unsigned> morphRangeCounts_;
  208. /// Vertex buffer data for asynchronous loading.
  209. Vector<VertexBufferDesc> loadVBData_;
  210. /// Index buffer data for asynchronous loading.
  211. Vector<IndexBufferDesc> loadIBData_;
  212. /// Geometry definitions for asynchronous loading.
  213. Vector<PODVector<GeometryDesc> > loadGeometries_;
  214. // ATOMIC BEGIN
  215. /// animation resources
  216. Vector<SharedPtr<Animation> > animationsResources_;
  217. // ATOMIC END
  218. };
  219. }