Model.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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 "Map.h"
  27. #include "Skeleton.h"
  28. #include "Resource.h"
  29. #include "Ptr.h"
  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. Map<unsigned, VertexBufferMorph> buffers_;
  55. };
  56. /// 3D model resource.
  57. class 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);
  71. /// %Set bounding box.
  72. void SetBoundingBox(const BoundingBox& box);
  73. /// %Set number of geometries.
  74. void SetNumGeometries(unsigned num);
  75. /// %Set number of LOD levels in a geometry.
  76. bool SetNumGeometryLodLevels(unsigned index, unsigned num);
  77. /// %Set geometry.
  78. bool SetGeometry(unsigned index, unsigned lodLevel, Geometry* geometry);
  79. /// %Set geometry center.
  80. bool SetGeometryCenter(unsigned index, const Vector3& center);
  81. /// %Set skeleton.
  82. void SetSkeleton(const Skeleton& skeleton);
  83. /// %Set bone mappings when model has more bones than the skinning shader can handle.
  84. void SetGeometryBoneMappings(const Vector<PODVector<unsigned> >& mappings);
  85. /// %Set vertex morphs.
  86. void SetMorphs(const Vector<ModelMorph>& morphs);
  87. /// Return bounding box.
  88. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  89. /// Return skeleton.
  90. Skeleton& GetSkeleton() { return skeleton_; }
  91. /// Return vertex buffers.
  92. const Vector<SharedPtr<VertexBuffer> >& GetVertexBuffers() const { return vertexBuffers_; }
  93. /// Return index buffers.
  94. const Vector<SharedPtr<IndexBuffer> >& GetIndexBuffers() const { return indexBuffers_; }
  95. /// Return number of geometries.
  96. unsigned GetNumGeometries() const { return geometries_.Size(); }
  97. /// Return number of LOD levels in geometry.
  98. unsigned GetNumGeometryLodLevels(unsigned index) const;
  99. /// Return geometry pointers.
  100. const Vector<Vector<SharedPtr<Geometry> > >& GetGeometries() const { return geometries_; }
  101. /// Return geometry center points.
  102. const PODVector<Vector3>& GetGeometryCenters() const { return geometryCenters_; }
  103. /// Return geometry by index and LOD level.
  104. Geometry* GetGeometry(unsigned index, unsigned lodLevel) const;
  105. /// Return geometery bone mappings.
  106. const Vector<PODVector<unsigned> >& GetGeometryBoneMappings() const { return geometryBoneMappings_; }
  107. /// Return vertex morphs.
  108. const Vector<ModelMorph>& GetMorphs() const { return morphs_; }
  109. /// Return number of vertex morphs.
  110. unsigned GetNumMorphs() const { return morphs_.Size(); }
  111. /// Return vertex morph by index.
  112. const ModelMorph* GetMorph(unsigned index) const;
  113. /// Return vertex morph by name.
  114. const ModelMorph* GetMorph(const String& name) const;
  115. /// Return vertex morph by name hash.
  116. const ModelMorph* GetMorph(StringHash nameHash) const;
  117. private:
  118. /// Bounding box.
  119. BoundingBox boundingBox_;
  120. /// Skeleton.
  121. Skeleton skeleton_;
  122. /// Vertex buffers.
  123. Vector<SharedPtr<VertexBuffer> > vertexBuffers_;
  124. /// Index buffers.
  125. Vector<SharedPtr<IndexBuffer> > indexBuffers_;
  126. /// Geometries.
  127. Vector<Vector<SharedPtr<Geometry> > > geometries_;
  128. /// Geometry bone mappings.
  129. Vector<PODVector<unsigned> > geometryBoneMappings_;
  130. /// Geometry centers.
  131. PODVector<Vector3> geometryCenters_;
  132. /// Vertex morphs.
  133. Vector<ModelMorph> morphs_;
  134. };