Model.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // Copyright (c) 2008-2017 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 "../Graphics/Skeleton.h"
  27. #include "../Math/BoundingBox.h"
  28. #include "../Resource/Resource.h"
  29. namespace Atomic
  30. {
  31. class Geometry;
  32. class IndexBuffer;
  33. class Graphics;
  34. class VertexBuffer;
  35. /// Vertex buffer morph data.
  36. struct VertexBufferMorph
  37. {
  38. /// Vertex elements.
  39. unsigned elementMask_;
  40. /// Number of vertices.
  41. unsigned vertexCount_;
  42. /// Morphed vertices data size as bytes.
  43. unsigned dataSize_;
  44. /// Morphed vertices. Stored packed as <index, data> pairs.
  45. SharedArrayPtr<unsigned char> morphData_;
  46. };
  47. /// Definition of a model's vertex morph.
  48. struct ModelMorph
  49. {
  50. /// Morph name.
  51. String name_;
  52. /// Morph name hash.
  53. StringHash nameHash_;
  54. /// Current morph weight.
  55. float weight_;
  56. /// Morph data per vertex buffer.
  57. HashMap<unsigned, VertexBufferMorph> buffers_;
  58. };
  59. /// Description of vertex buffer data for asynchronous loading.
  60. struct VertexBufferDesc
  61. {
  62. /// Vertex count.
  63. unsigned vertexCount_;
  64. /// Vertex declaration.
  65. PODVector<VertexElement> vertexElements_;
  66. /// Vertex data size.
  67. unsigned dataSize_;
  68. /// Vertex data.
  69. SharedArrayPtr<unsigned char> data_;
  70. };
  71. /// Description of index buffer data for asynchronous loading.
  72. struct IndexBufferDesc
  73. {
  74. /// Index count.
  75. unsigned indexCount_;
  76. /// Index size.
  77. unsigned indexSize_;
  78. /// Index data size.
  79. unsigned dataSize_;
  80. /// Index data.
  81. SharedArrayPtr<unsigned char> data_;
  82. };
  83. /// Description of a geometry for asynchronous loading.
  84. struct GeometryDesc
  85. {
  86. /// Primitive type.
  87. PrimitiveType type_;
  88. /// Vertex buffer ref.
  89. unsigned vbRef_;
  90. /// Index buffer ref.
  91. unsigned ibRef_;
  92. /// Index start.
  93. unsigned indexStart_;
  94. /// Index count.
  95. unsigned indexCount_;
  96. };
  97. // ATOMIC BEGIN
  98. static const unsigned MODEL_VERSION = 1;
  99. // ATOMIC END
  100. /// 3D model resource.
  101. class ATOMIC_API Model : public ResourceWithMetadata
  102. {
  103. ATOMIC_OBJECT(Model, ResourceWithMetadata);
  104. public:
  105. /// Construct.
  106. Model(Context* context);
  107. /// Destruct.
  108. virtual ~Model();
  109. /// Register object factory.
  110. static void RegisterObject(Context* context);
  111. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  112. virtual bool BeginLoad(Deserializer& source);
  113. /// Finish resource loading. Always called from the main thread. Return true if successful.
  114. virtual bool EndLoad();
  115. /// Save resource. Return true if successful.
  116. virtual bool Save(Serializer& dest) const;
  117. /// Set local-space bounding box.
  118. void SetBoundingBox(const BoundingBox& box);
  119. /// Set vertex buffers and their morph ranges.
  120. bool SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, const PODVector<unsigned>& morphRangeStarts,
  121. const PODVector<unsigned>& morphRangeCounts);
  122. /// Set index buffers.
  123. bool SetIndexBuffers(const Vector<SharedPtr<IndexBuffer> >& buffers);
  124. /// Set number of geometries.
  125. void SetNumGeometries(unsigned num);
  126. /// Set number of LOD levels in a geometry.
  127. bool SetNumGeometryLodLevels(unsigned index, unsigned num);
  128. /// Set geometry.
  129. bool SetGeometry(unsigned index, unsigned lodLevel, Geometry* geometry);
  130. /// Set geometry center.
  131. bool SetGeometryCenter(unsigned index, const Vector3& center);
  132. /// Set skeleton.
  133. void SetSkeleton(const Skeleton& skeleton);
  134. /// Set bone mappings when model has more bones than the skinning shader can handle.
  135. void SetGeometryBoneMappings(const Vector<PODVector<unsigned> >& mappings);
  136. /// Set vertex morphs.
  137. void SetMorphs(const Vector<ModelMorph>& morphs);
  138. /// Clone the model. The geometry data is deep-copied and can be modified in the clone without affecting the original.
  139. SharedPtr<Model> Clone(const String& cloneName = String::EMPTY) const;
  140. /// Return bounding box.
  141. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  142. /// Return skeleton.
  143. Skeleton& GetSkeleton() { return skeleton_; }
  144. /// Return vertex buffers.
  145. const Vector<SharedPtr<VertexBuffer> >& GetVertexBuffers() const { return vertexBuffers_; }
  146. /// Return index buffers.
  147. const Vector<SharedPtr<IndexBuffer> >& GetIndexBuffers() const { return indexBuffers_; }
  148. /// Return number of geometries.
  149. unsigned GetNumGeometries() const { return geometries_.Size(); }
  150. /// Return number of LOD levels in geometry.
  151. unsigned GetNumGeometryLodLevels(unsigned index) const;
  152. /// Return geometry pointers.
  153. const Vector<Vector<SharedPtr<Geometry> > >& GetGeometries() const { return geometries_; }
  154. /// Return geometry center points.
  155. const PODVector<Vector3>& GetGeometryCenters() const { return geometryCenters_; }
  156. /// Return geometry by index and LOD level. The LOD level is clamped if out of range.
  157. Geometry* GetGeometry(unsigned index, unsigned lodLevel) const;
  158. /// Return geometry center by index.
  159. const Vector3& GetGeometryCenter(unsigned index) const
  160. {
  161. return index < geometryCenters_.Size() ? geometryCenters_[index] : Vector3::ZERO;
  162. }
  163. /// Return geometery bone mappings.
  164. const Vector<PODVector<unsigned> >& GetGeometryBoneMappings() const { return geometryBoneMappings_; }
  165. /// Return vertex morphs.
  166. const Vector<ModelMorph>& GetMorphs() const { return morphs_; }
  167. /// Return number of vertex morphs.
  168. unsigned GetNumMorphs() const { return morphs_.Size(); }
  169. /// Return vertex morph by index.
  170. const ModelMorph* GetMorph(unsigned index) const;
  171. /// Return vertex morph by name.
  172. const ModelMorph* GetMorph(const String& name) const;
  173. /// Return vertex morph by name hash.
  174. const ModelMorph* GetMorph(StringHash nameHash) const;
  175. /// Return vertex buffer morph range start.
  176. unsigned GetMorphRangeStart(unsigned bufferIndex) const;
  177. /// Return vertex buffer morph range vertex count.
  178. unsigned GetMorphRangeCount(unsigned bufferIndex) const;
  179. // ATOMIC BEGIN
  180. bool SetGeometryName(unsigned index, const String& name);
  181. const String& GetGeometryName(unsigned index) const;
  182. const Vector<String>& GetGeometryNames() const { return geometryNames_; }
  183. // ATOMIC END
  184. private:
  185. /// Bounding box.
  186. BoundingBox boundingBox_;
  187. /// Skeleton.
  188. Skeleton skeleton_;
  189. /// Vertex buffers.
  190. Vector<SharedPtr<VertexBuffer> > vertexBuffers_;
  191. /// Index buffers.
  192. Vector<SharedPtr<IndexBuffer> > indexBuffers_;
  193. /// Geometries.
  194. Vector<Vector<SharedPtr<Geometry> > > geometries_;
  195. /// Geometry bone mappings.
  196. Vector<PODVector<unsigned> > geometryBoneMappings_;
  197. /// Geometry centers.
  198. PODVector<Vector3> geometryCenters_;
  199. /// Vertex morphs.
  200. Vector<ModelMorph> morphs_;
  201. /// Vertex buffer morph range start.
  202. PODVector<unsigned> morphRangeStarts_;
  203. /// Vertex buffer morph range vertex count.
  204. PODVector<unsigned> morphRangeCounts_;
  205. /// Vertex buffer data for asynchronous loading.
  206. Vector<VertexBufferDesc> loadVBData_;
  207. /// Index buffer data for asynchronous loading.
  208. Vector<IndexBufferDesc> loadIBData_;
  209. /// Geometry definitions for asynchronous loading.
  210. Vector<PODVector<GeometryDesc> > loadGeometries_;
  211. // ATOMIC BEGIN
  212. Vector<String> geometryNames_;
  213. // ATOMIC END
  214. };
  215. }