StaticModel.h 4.8 KB

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