StaticModel.h 4.6 KB

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