TerrainPatch.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Graphics/Drawable.h"
  5. namespace Urho3D
  6. {
  7. class Geometry;
  8. class Terrain;
  9. class VertexBuffer;
  10. /// Individually rendered part of a heightmap terrain.
  11. class URHO3D_API TerrainPatch : public Drawable
  12. {
  13. URHO3D_OBJECT(TerrainPatch, Drawable);
  14. public:
  15. /// Construct.
  16. explicit TerrainPatch(Context* context);
  17. /// Destruct.
  18. ~TerrainPatch() override;
  19. /// Register object factory.
  20. /// @nobind
  21. static void RegisterObject(Context* context);
  22. /// Process octree raycast. May be called from a worker thread.
  23. void ProcessRayQuery(const RayOctreeQuery& query, Vector<RayQueryResult>& results) override;
  24. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  25. void UpdateBatches(const FrameInfo& frame) override;
  26. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update).
  27. void UpdateGeometry(const FrameInfo& frame) override;
  28. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  29. UpdateGeometryType GetUpdateGeometryType() override;
  30. /// Return the geometry for a specific LOD level.
  31. Geometry* GetLodGeometry(i32 batchIndex, i32 level) override;
  32. /// Return number of occlusion geometry triangles.
  33. i32 GetNumOccluderTriangles() override;
  34. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  35. bool DrawOcclusion(OcclusionBuffer* buffer) override;
  36. /// Visualize the component as debug geometry.
  37. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) override;
  38. /// Set owner terrain.
  39. void SetOwner(Terrain* terrain);
  40. /// Set neighbor patches.
  41. void SetNeighbors(TerrainPatch* north, TerrainPatch* south, TerrainPatch* west, TerrainPatch* east);
  42. /// Set material.
  43. void SetMaterial(Material* material);
  44. /// Set local-space bounding box.
  45. void SetBoundingBox(const BoundingBox& box);
  46. /// Set patch coordinates.
  47. void SetCoordinates(const IntVector2& coordinates);
  48. /// Reset to LOD level 0.
  49. void ResetLod();
  50. /// Return visible geometry.
  51. Geometry* GetGeometry() const;
  52. /// Return max LOD geometry. Used for decals.
  53. Geometry* GetMaxLodGeometry() const;
  54. /// Return geometry used for occlusion.
  55. Geometry* GetOcclusionGeometry() const;
  56. /// Return vertex buffer.
  57. VertexBuffer* GetVertexBuffer() const;
  58. /// Return owner terrain.
  59. Terrain* GetOwner() const;
  60. /// Return north neighbor patch.
  61. TerrainPatch* GetNorthPatch() const { return north_; }
  62. /// Return south neighbor patch.
  63. TerrainPatch* GetSouthPatch() const { return south_; }
  64. /// Return west neighbor patch.
  65. TerrainPatch* GetWestPatch() const { return west_; }
  66. /// Return east neighbor patch.
  67. TerrainPatch* GetEastPatch() const { return east_; }
  68. /// Return geometrical error array.
  69. Vector<float>& GetLodErrors() { return lodErrors_; }
  70. /// Return patch coordinates.
  71. const IntVector2& GetCoordinates() const { return coordinates_; }
  72. /// Return current LOD level.
  73. i32 GetLodLevel() const { return lodLevel_; }
  74. protected:
  75. /// Recalculate the world-space bounding box.
  76. void OnWorldBoundingBoxUpdate() override;
  77. private:
  78. /// Return a corrected LOD level to ensure stitching can work correctly.
  79. i32 GetCorrectedLodLevel(i32 lodLevel);
  80. /// Geometry.
  81. SharedPtr<Geometry> geometry_;
  82. /// Geometry that is locked to the max LOD level. Used for decals.
  83. SharedPtr<Geometry> maxLodGeometry_;
  84. /// Geometry that is used for occlusion.
  85. SharedPtr<Geometry> occlusionGeometry_;
  86. /// Vertex buffer.
  87. SharedPtr<VertexBuffer> vertexBuffer_;
  88. /// Parent terrain.
  89. WeakPtr<Terrain> owner_;
  90. /// North neighbor patch.
  91. WeakPtr<TerrainPatch> north_;
  92. /// South neighbor patch.
  93. WeakPtr<TerrainPatch> south_;
  94. /// West neighbor patch.
  95. WeakPtr<TerrainPatch> west_;
  96. /// East neighbor patch.
  97. WeakPtr<TerrainPatch> east_;
  98. /// Geometrical error per LOD level.
  99. Vector<float> lodErrors_;
  100. /// Patch coordinates in the terrain. (0,0) is the northwest corner.
  101. IntVector2 coordinates_;
  102. /// Current LOD level.
  103. i32 lodLevel_;
  104. };
  105. }