Model.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "BoundingBox.h"
  25. #include "Skeleton.h"
  26. #include "Resource.h"
  27. #include "SharedPtr.h"
  28. #include "SharedArrayPtr.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. /// Model 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. /// 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 skeleton
  79. void SetSkeleton(const Skeleton& skeleton);
  80. /// Set bone mappings when model has more bones than the skinning shader can handle
  81. void SetGeometryBoneMappings(const Vector<PODVector<unsigned> >& mappings);
  82. /// Set vertex morphs
  83. void SetMorphs(const Vector<ModelMorph>& morphs);
  84. /// Return bounding box
  85. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  86. /// Return skeleton
  87. Skeleton& GetSkeleton() { return skeleton_; }
  88. /// Return vertex buffers
  89. const Vector<SharedPtr<VertexBuffer> >& GetVertexBuffers() const { return vertexBuffers_; }
  90. /// Return index buffers
  91. const Vector<SharedPtr<IndexBuffer> >& GetIndexBuffers() const { return indexBuffers_; }
  92. /// Return number of geometries
  93. unsigned GetNumGeometries() const { return geometries_.Size(); }
  94. /// Return number of LOD levels in geometry
  95. unsigned GetNumGeometryLodLevels(unsigned index) const;
  96. /// Return geometry pointers
  97. const Vector<Vector<SharedPtr<Geometry> > >& GetGeometries() const { return geometries_; }
  98. /// Return geometry by index and LOD level
  99. Geometry* GetGeometry(unsigned index, unsigned lodLevel) const;
  100. /// Return geometery bone mappings
  101. const Vector<PODVector<unsigned> >& GetGeometryBoneMappings() const { return geometryBoneMappings_; }
  102. /// Return vertex morphs
  103. const Vector<ModelMorph>& GetMorphs() const { return morphs_; }
  104. /// Return number of vertex morphs
  105. unsigned GetNumMorphs() const { return morphs_.Size(); }
  106. /// Return vertex morph by index
  107. const ModelMorph* GetMorph(unsigned index) const;
  108. /// Return vertex morph by name
  109. const ModelMorph* GetMorph(const String& name) const;
  110. /// Return vertex morph by name hash
  111. const ModelMorph* GetMorph(StringHash nameHash) const;
  112. private:
  113. /// Bounding box
  114. BoundingBox boundingBox_;
  115. /// Skeleton
  116. Skeleton skeleton_;
  117. /// Vertex buffers
  118. Vector<SharedPtr<VertexBuffer> > vertexBuffers_;
  119. /// Index buffers
  120. Vector<SharedPtr<IndexBuffer> > indexBuffers_;
  121. /// Geometries
  122. Vector<Vector<SharedPtr<Geometry> > > geometries_;
  123. /// Geometry bone mappings
  124. Vector<PODVector<unsigned> > geometryBoneMappings_;
  125. /// Vertex morphs
  126. Vector<ModelMorph> morphs_;
  127. };