Model.h 6.1 KB

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