StaticModel.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "Drawable.h"
  25. class Model;
  26. /// Static model per-geometry extra data.
  27. struct StaticModelGeometryData
  28. {
  29. /// Geometry center.
  30. Vector3 center_;
  31. /// Current LOD level.
  32. unsigned lodLevel_;
  33. };
  34. /// Static model component.
  35. class StaticModel : public Drawable
  36. {
  37. OBJECT(StaticModel);
  38. public:
  39. /// Construct.
  40. StaticModel(Context* context);
  41. /// Destruct.
  42. ~StaticModel();
  43. /// Register object factory. Drawable must be registered first.
  44. static void RegisterObject(Context* context);
  45. /// Process octree raycast. May be called from a worker thread.
  46. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  47. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  48. virtual void UpdateBatches(const FrameInfo& frame);
  49. /// Return the geometry for a specific LOD level.
  50. virtual Geometry* GetLodGeometry(unsigned batchIndex, unsigned level);
  51. /// Return number of occlusion geometry triangles.
  52. virtual unsigned GetNumOccluderTriangles();
  53. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  54. virtual bool DrawOcclusion(OcclusionBuffer* buffer);
  55. /// Set model.
  56. void SetModel(Model* model);
  57. /// Set material on all geometries.
  58. void SetMaterial(Material* material);
  59. /// Set material on one geometry. Return true if successful.
  60. bool SetMaterial(unsigned index, Material* material);
  61. /// Set occlusion LOD level. By default (M_MAX_UNSIGNED) same as visible.
  62. void SetOcclusionLodLevel(unsigned level);
  63. /// Return model.
  64. Model* GetModel() const { return model_; }
  65. /// Return model's bounding box.
  66. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  67. /// Return number of geometries.
  68. unsigned GetNumGeometries() const { return geometries_.Size(); }
  69. /// Return material by geometry index.
  70. Material* GetMaterial(unsigned index) const;
  71. /// Return occlusion LOD level.
  72. unsigned GetOcclusionLodLevel() const { return occlusionLodLevel_; }
  73. /// Set model attribute.
  74. void SetModelAttr(ResourceRef value);
  75. /// Set materials attribute.
  76. void SetMaterialsAttr(const ResourceRefList& value);
  77. /// Return model attribute.
  78. ResourceRef GetModelAttr() const;
  79. /// Return materials attribute.
  80. const ResourceRefList& GetMaterialsAttr() const;
  81. protected:
  82. /// Recalculate the world-space bounding box.
  83. virtual void OnWorldBoundingBoxUpdate();
  84. /// Set local-space bounding box.
  85. void SetBoundingBox(const BoundingBox& box);
  86. /// Set number of geometries.
  87. void SetNumGeometries(unsigned num);
  88. /// Reset LOD levels.
  89. void ResetLodLevels();
  90. /// Choose LOD levels based on distance.
  91. void CalculateLodLevels();
  92. /// Local-space bounding box.
  93. BoundingBox boundingBox_;
  94. /// Extra per-geometry data.
  95. PODVector<StaticModelGeometryData> geometryData_;
  96. /// All geometries.
  97. Vector<Vector<SharedPtr<Geometry> > > geometries_;
  98. /// Model.
  99. SharedPtr<Model> model_;
  100. /// Occlusion LOD level.
  101. unsigned occlusionLodLevel_;
  102. /// Material list attribute.
  103. mutable ResourceRefList materialsAttr_;
  104. private:
  105. /// Handle model reload finished.
  106. void HandleModelReloadFinished(StringHash eventType, VariantMap& eventData);
  107. };