Model.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "ArrayPtr.h"
  25. #include "BoundingBox.h"
  26. #include "Skeleton.h"
  27. #include "Resource.h"
  28. #include "Ptr.h"
  29. namespace Urho3D
  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. Stored packed as <index, data> pairs.
  43. SharedArrayPtr<unsigned char> morphData_;
  44. };
  45. /// Definition of a model's vertex morph.
  46. struct ModelMorph
  47. {
  48. /// Morph name.
  49. String name_;
  50. /// Morph name hash.
  51. StringHash nameHash_;
  52. /// Current morph weight.
  53. float weight_;
  54. /// Morph data per vertex buffer.
  55. HashMap<unsigned, VertexBufferMorph> buffers_;
  56. };
  57. /// 3D model resource.
  58. class Model : public Resource
  59. {
  60. OBJECT(Model);
  61. public:
  62. /// Construct.
  63. Model(Context* context);
  64. /// Destruct.
  65. virtual ~Model();
  66. /// Register object factory.
  67. static void RegisterObject(Context* context);
  68. /// Load resource. Return true if successful.
  69. virtual bool Load(Deserializer& source);
  70. /// Save resource. Return true if successful.
  71. virtual bool Save(Serializer& dest);
  72. /// Set local-space bounding box.
  73. void SetBoundingBox(const BoundingBox& box);
  74. /// Set vertex buffers.
  75. bool SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, const PODVector<unsigned>& morphRangeStarts, const PODVector<unsigned>& morphRangeCounts);
  76. /// Set index buffers.
  77. bool SetIndexBuffers(const Vector<SharedPtr<IndexBuffer> >& buffers);
  78. /// Set number of geometries.
  79. void SetNumGeometries(unsigned num);
  80. /// Set number of LOD levels in a geometry.
  81. bool SetNumGeometryLodLevels(unsigned index, unsigned num);
  82. /// Set geometry.
  83. bool SetGeometry(unsigned index, unsigned lodLevel, Geometry* geometry);
  84. /// Set geometry center.
  85. bool SetGeometryCenter(unsigned index, const Vector3& center);
  86. /// Set skeleton.
  87. void SetSkeleton(const Skeleton& skeleton);
  88. /// Set bone mappings when model has more bones than the skinning shader can handle.
  89. void SetGeometryBoneMappings(const Vector<PODVector<unsigned> >& mappings);
  90. /// Set vertex morphs.
  91. void SetMorphs(const Vector<ModelMorph>& morphs);
  92. /// Return bounding box.
  93. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  94. /// Return skeleton.
  95. Skeleton& GetSkeleton() { return skeleton_; }
  96. /// Return vertex buffers.
  97. const Vector<SharedPtr<VertexBuffer> >& GetVertexBuffers() const { return vertexBuffers_; }
  98. /// Return index buffers.
  99. const Vector<SharedPtr<IndexBuffer> >& GetIndexBuffers() const { return indexBuffers_; }
  100. /// Return number of geometries.
  101. unsigned GetNumGeometries() const { return geometries_.Size(); }
  102. /// Return number of LOD levels in geometry.
  103. unsigned GetNumGeometryLodLevels(unsigned index) const;
  104. /// Return geometry pointers.
  105. const Vector<Vector<SharedPtr<Geometry> > >& GetGeometries() const { return geometries_; }
  106. /// Return geometry center points.
  107. const PODVector<Vector3>& GetGeometryCenters() const { return geometryCenters_; }
  108. /// Return geometry by index and LOD level.
  109. Geometry* GetGeometry(unsigned index, unsigned lodLevel) const;
  110. /// Return geometery bone mappings.
  111. const Vector<PODVector<unsigned> >& GetGeometryBoneMappings() const { return geometryBoneMappings_; }
  112. /// Return vertex morphs.
  113. const Vector<ModelMorph>& GetMorphs() const { return morphs_; }
  114. /// Return number of vertex morphs.
  115. unsigned GetNumMorphs() const { return morphs_.Size(); }
  116. /// Return vertex morph by index.
  117. const ModelMorph* GetMorph(unsigned index) const;
  118. /// Return vertex morph by name.
  119. const ModelMorph* GetMorph(const String& name) const;
  120. /// Return vertex morph by name hash.
  121. const ModelMorph* GetMorph(StringHash nameHash) const;
  122. /// Return vertex buffer morph range start.
  123. unsigned GetMorphRangeStart(unsigned bufferIndex) const;
  124. /// Return vertex buffer morph range vertex count.
  125. unsigned GetMorphRangeCount(unsigned bufferIndex) const;
  126. private:
  127. /// Bounding box.
  128. BoundingBox boundingBox_;
  129. /// Skeleton.
  130. Skeleton skeleton_;
  131. /// Vertex buffers.
  132. Vector<SharedPtr<VertexBuffer> > vertexBuffers_;
  133. /// Index buffers.
  134. Vector<SharedPtr<IndexBuffer> > indexBuffers_;
  135. /// Geometries.
  136. Vector<Vector<SharedPtr<Geometry> > > geometries_;
  137. /// Geometry bone mappings.
  138. Vector<PODVector<unsigned> > geometryBoneMappings_;
  139. /// Geometry centers.
  140. PODVector<Vector3> geometryCenters_;
  141. /// Vertex morphs.
  142. Vector<ModelMorph> morphs_;
  143. /// Vertex buffer morph range start.
  144. PODVector<unsigned> morphRangeStarts_;
  145. /// Vertex buffer morph range vertex count.
  146. PODVector<unsigned> morphRangeCounts_;
  147. };
  148. }