Model.h 5.3 KB

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