TerrainPatch.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Graphics/Drawable.h"
  24. namespace Atomic
  25. {
  26. class Geometry;
  27. class Terrain;
  28. class VertexBuffer;
  29. /// Individually rendered part of a heightmap terrain.
  30. class ATOMIC_API TerrainPatch : public Drawable
  31. {
  32. ATOMIC_OBJECT(TerrainPatch, Drawable);
  33. public:
  34. /// Construct.
  35. TerrainPatch(Context* context);
  36. /// Destruct.
  37. ~TerrainPatch();
  38. /// Register object factory.
  39. static void RegisterObject(Context* context);
  40. /// Process octree raycast. May be called from a worker thread.
  41. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  42. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  43. virtual void UpdateBatches(const FrameInfo& frame);
  44. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update.)
  45. virtual void UpdateGeometry(const FrameInfo& frame);
  46. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  47. virtual UpdateGeometryType GetUpdateGeometryType();
  48. /// Return the geometry for a specific LOD level.
  49. virtual Geometry* GetLodGeometry(unsigned batchIndex, unsigned level);
  50. /// Return number of occlusion geometry triangles.
  51. virtual unsigned GetNumOccluderTriangles();
  52. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  53. virtual bool DrawOcclusion(OcclusionBuffer* buffer);
  54. /// Visualize the component as debug geometry.
  55. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  56. /// Set owner terrain.
  57. void SetOwner(Terrain* terrain);
  58. /// Set neighbor patches.
  59. void SetNeighbors(TerrainPatch* north, TerrainPatch* south, TerrainPatch* west, TerrainPatch* east);
  60. /// Set material.
  61. void SetMaterial(Material* material);
  62. /// Set local-space bounding box.
  63. void SetBoundingBox(const BoundingBox& box);
  64. /// Set patch coordinates.
  65. void SetCoordinates(const IntVector2& coordinates);
  66. /// Reset to LOD level 0.
  67. void ResetLod();
  68. /// Return visible geometry.
  69. Geometry* GetGeometry() const;
  70. /// Return max LOD geometry. Used for decals.
  71. Geometry* GetMaxLodGeometry() const;
  72. /// Return geometry used for occlusion.
  73. Geometry* GetOcclusionGeometry() const;
  74. /// Return vertex buffer.
  75. VertexBuffer* GetVertexBuffer() const;
  76. /// Return owner terrain.
  77. Terrain* GetOwner() const;
  78. /// Return north neighbor patch.
  79. TerrainPatch* GetNorthPatch() const { return north_; }
  80. /// Return south neighbor patch.
  81. TerrainPatch* GetSouthPatch() const { return south_; }
  82. /// Return west neighbor patch.
  83. TerrainPatch* GetWestPatch() const { return west_; }
  84. /// Return east neighbor patch.
  85. TerrainPatch* GetEastPatch() const { return east_; }
  86. /// Return geometrical error array.
  87. PODVector<float>& GetLodErrors() { return lodErrors_; }
  88. /// Return patch coordinates.
  89. const IntVector2& GetCoordinates() const { return coordinates_; }
  90. /// Return current LOD level.
  91. unsigned GetLodLevel() const { return lodLevel_; }
  92. protected:
  93. /// Recalculate the world-space bounding box.
  94. virtual void OnWorldBoundingBoxUpdate();
  95. private:
  96. /// Return a corrected LOD level to ensure stitching can work correctly.
  97. unsigned GetCorrectedLodLevel(unsigned lodLevel);
  98. /// Geometry.
  99. SharedPtr<Geometry> geometry_;
  100. /// Geometry that is locked to the max LOD level. Used for decals.
  101. SharedPtr<Geometry> maxLodGeometry_;
  102. /// Geometry that is used for occlusion.
  103. SharedPtr<Geometry> occlusionGeometry_;
  104. /// Vertex buffer.
  105. SharedPtr<VertexBuffer> vertexBuffer_;
  106. /// Parent terrain.
  107. WeakPtr<Terrain> owner_;
  108. /// North neighbor patch.
  109. WeakPtr<TerrainPatch> north_;
  110. /// South neighbor patch.
  111. WeakPtr<TerrainPatch> south_;
  112. /// West neighbor patch.
  113. WeakPtr<TerrainPatch> west_;
  114. /// East neighbor patch.
  115. WeakPtr<TerrainPatch> east_;
  116. /// Geometrical error per LOD level.
  117. PODVector<float> lodErrors_;
  118. /// Patch coordinates in the terrain. (0,0) is the northwest corner.
  119. IntVector2 coordinates_;
  120. /// Current LOD level.
  121. unsigned lodLevel_;
  122. };
  123. }