Model.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // Copyright (c) 2008-2014 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 "ArrayPtr.h"
  24. #include "BoundingBox.h"
  25. #include "Skeleton.h"
  26. #include "Resource.h"
  27. #include "Ptr.h"
  28. namespace Urho3D
  29. {
  30. class Geometry;
  31. class IndexBuffer;
  32. class Graphics;
  33. class VertexBuffer;
  34. /// Vertex buffer morph data.
  35. struct VertexBufferMorph
  36. {
  37. /// Vertex elements.
  38. unsigned elementMask_;
  39. /// Number of vertices.
  40. unsigned vertexCount_;
  41. /// Morphed vertices data size as bytes.
  42. unsigned dataSize_;
  43. /// Morphed vertices. Stored packed as <index, data> pairs.
  44. SharedArrayPtr<unsigned char> morphData_;
  45. };
  46. /// Definition of a model's vertex morph.
  47. struct ModelMorph
  48. {
  49. /// Morph name.
  50. String name_;
  51. /// Morph name hash.
  52. StringHash nameHash_;
  53. /// Current morph weight.
  54. float weight_;
  55. /// Morph data per vertex buffer.
  56. HashMap<unsigned, VertexBufferMorph> buffers_;
  57. };
  58. /// 3D model resource.
  59. class URHO3D_API Model : public Resource
  60. {
  61. OBJECT(Model);
  62. public:
  63. /// Construct.
  64. Model(Context* context);
  65. /// Destruct.
  66. virtual ~Model();
  67. /// Register object factory.
  68. static void RegisterObject(Context* context);
  69. /// Load resource. Return true if successful.
  70. virtual bool Load(Deserializer& source);
  71. /// Save resource. Return true if successful.
  72. virtual bool Save(Serializer& dest) const;
  73. /// Set local-space bounding box.
  74. void SetBoundingBox(const BoundingBox& box);
  75. /// Set vertex buffers and their morph ranges.
  76. bool SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, const PODVector<unsigned>& morphRangeStarts, const PODVector<unsigned>& morphRangeCounts);
  77. /// Set index buffers.
  78. bool SetIndexBuffers(const Vector<SharedPtr<IndexBuffer> >& buffers);
  79. /// Set number of geometries.
  80. void SetNumGeometries(unsigned num);
  81. /// Set number of LOD levels in a geometry.
  82. bool SetNumGeometryLodLevels(unsigned index, unsigned num);
  83. /// Set geometry.
  84. bool SetGeometry(unsigned index, unsigned lodLevel, Geometry* geometry);
  85. /// Set geometry center.
  86. bool SetGeometryCenter(unsigned index, const Vector3& center);
  87. /// Set skeleton.
  88. void SetSkeleton(const Skeleton& skeleton);
  89. /// Set bone mappings when model has more bones than the skinning shader can handle.
  90. void SetGeometryBoneMappings(const Vector<PODVector<unsigned> >& mappings);
  91. /// Set vertex morphs.
  92. void SetMorphs(const Vector<ModelMorph>& morphs);
  93. /// Clone the model. The geometry data is deep-copied and can be modified in the clone without affecting the original.
  94. SharedPtr<Model> Clone(const String& cloneName = String::EMPTY) const;
  95. /// Return bounding box.
  96. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  97. /// Return skeleton.
  98. Skeleton& GetSkeleton() { return skeleton_; }
  99. /// Return vertex buffers.
  100. const Vector<SharedPtr<VertexBuffer> >& GetVertexBuffers() const { return vertexBuffers_; }
  101. /// Return index buffers.
  102. const Vector<SharedPtr<IndexBuffer> >& GetIndexBuffers() const { return indexBuffers_; }
  103. /// Return number of geometries.
  104. unsigned GetNumGeometries() const { return geometries_.Size(); }
  105. /// Return number of LOD levels in geometry.
  106. unsigned GetNumGeometryLodLevels(unsigned index) const;
  107. /// Return geometry pointers.
  108. const Vector<Vector<SharedPtr<Geometry> > >& GetGeometries() const { return geometries_; }
  109. /// Return geometry center points.
  110. const PODVector<Vector3>& GetGeometryCenters() const { return geometryCenters_; }
  111. /// Return geometry by index and LOD level. The LOD level is clamped if out of range.
  112. Geometry* GetGeometry(unsigned index, unsigned lodLevel) const;
  113. /// Return geometery bone mappings.
  114. const Vector<PODVector<unsigned> >& GetGeometryBoneMappings() const { return geometryBoneMappings_; }
  115. /// Return vertex morphs.
  116. const Vector<ModelMorph>& GetMorphs() const { return morphs_; }
  117. /// Return number of vertex morphs.
  118. unsigned GetNumMorphs() const { return morphs_.Size(); }
  119. /// Return vertex morph by index.
  120. const ModelMorph* GetMorph(unsigned index) const;
  121. /// Return vertex morph by name.
  122. const ModelMorph* GetMorph(const String& name) const;
  123. /// Return vertex morph by name hash.
  124. const ModelMorph* GetMorph(StringHash nameHash) const;
  125. /// Return vertex buffer morph range start.
  126. unsigned GetMorphRangeStart(unsigned bufferIndex) const;
  127. /// Return vertex buffer morph range vertex count.
  128. unsigned GetMorphRangeCount(unsigned bufferIndex) const;
  129. private:
  130. /// Bounding box.
  131. BoundingBox boundingBox_;
  132. /// Skeleton.
  133. Skeleton skeleton_;
  134. /// Vertex buffers.
  135. Vector<SharedPtr<VertexBuffer> > vertexBuffers_;
  136. /// Index buffers.
  137. Vector<SharedPtr<IndexBuffer> > indexBuffers_;
  138. /// Geometries.
  139. Vector<Vector<SharedPtr<Geometry> > > geometries_;
  140. /// Geometry bone mappings.
  141. Vector<PODVector<unsigned> > geometryBoneMappings_;
  142. /// Geometry centers.
  143. PODVector<Vector3> geometryCenters_;
  144. /// Vertex morphs.
  145. Vector<ModelMorph> morphs_;
  146. /// Vertex buffer morph range start.
  147. PODVector<unsigned> morphRangeStarts_;
  148. /// Vertex buffer morph range vertex count.
  149. PODVector<unsigned> morphRangeCounts_;
  150. };
  151. }