OcclusionBuffer.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // Copyright (c) 2008-2015 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 "../Core/Object.h"
  24. #include "../Core/Timer.h"
  25. #include "../Container/ArrayPtr.h"
  26. #include "../Graphics/GraphicsDefs.h"
  27. #include "../Math/Frustum.h"
  28. namespace Atomic
  29. {
  30. class BoundingBox;
  31. class Camera;
  32. class IndexBuffer;
  33. class IntRect;
  34. class VertexBuffer;
  35. struct Edge;
  36. struct Gradients;
  37. /// Occlusion hierarchy depth range.
  38. struct DepthValue
  39. {
  40. /// Minimum value.
  41. int min_;
  42. /// Maximum value.
  43. int max_;
  44. };
  45. static const int OCCLUSION_MIN_SIZE = 8;
  46. static const int OCCLUSION_DEFAULT_MAX_TRIANGLES = 5000;
  47. static const float OCCLUSION_RELATIVE_BIAS = 0.00001f;
  48. static const int OCCLUSION_FIXED_BIAS = 16;
  49. static const float OCCLUSION_X_SCALE = 65536.0f;
  50. static const float OCCLUSION_Z_SCALE = 16777216.0f;
  51. /// Software renderer for occlusion.
  52. class ATOMIC_API OcclusionBuffer : public Object
  53. {
  54. OBJECT(OcclusionBuffer);
  55. public:
  56. /// Construct.
  57. OcclusionBuffer(Context* context);
  58. /// Destruct.
  59. virtual ~OcclusionBuffer();
  60. /// Set occlusion buffer size.
  61. bool SetSize(int width, int height);
  62. /// Set camera view to render from.
  63. void SetView(Camera* camera);
  64. /// Set maximum triangles to render.
  65. void SetMaxTriangles(unsigned triangles);
  66. /// Set culling mode.
  67. void SetCullMode(CullMode mode);
  68. /// Reset number of triangles.
  69. void Reset();
  70. /// Clear the buffer.
  71. void Clear();
  72. /// Draw a triangle mesh to the buffer using non-indexed geometry.
  73. bool Draw(const Matrix3x4& model, const void* vertexData, unsigned vertexSize, unsigned vertexStart, unsigned vertexCount);
  74. /// Draw a triangle mesh to the buffer using indexed geometry.
  75. bool Draw(const Matrix3x4& model, const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize,
  76. unsigned indexStart, unsigned indexCount);
  77. /// Build reduced size mip levels.
  78. void BuildDepthHierarchy();
  79. /// Reset last used timer.
  80. void ResetUseTimer();
  81. /// Return highest level depth values.
  82. int* GetBuffer() const { return buffer_; }
  83. /// Return view transform matrix.
  84. const Matrix3x4& GetView() const { return view_; }
  85. /// Return projection matrix.
  86. const Matrix4& GetProjection() const { return projection_; }
  87. /// Return buffer width.
  88. int GetWidth() const { return width_; }
  89. /// Return buffer height.
  90. int GetHeight() const { return height_; }
  91. /// Return number of rendered triangles.
  92. unsigned GetNumTriangles() const { return numTriangles_; }
  93. /// Return maximum number of triangles.
  94. unsigned GetMaxTriangles() const { return maxTriangles_; }
  95. /// Return culling mode.
  96. CullMode GetCullMode() const { return cullMode_; }
  97. /// Test a bounding box for visibility. For best performance, build depth hierarchy first.
  98. bool IsVisible(const BoundingBox& worldSpaceBox) const;
  99. /// Return time since last use in milliseconds.
  100. unsigned GetUseTimer();
  101. private:
  102. /// Apply modelview transform to vertex.
  103. inline Vector4 ModelTransform(const Matrix4& transform, const Vector3& vertex) const;
  104. /// Apply projection and viewport transform to vertex.
  105. inline Vector3 ViewportTransform(const Vector4& vertex) const;
  106. /// Clip an edge.
  107. inline Vector4 ClipEdge(const Vector4& v0, const Vector4& v1, float d0, float d1) const;
  108. /// Return signed area of a triangle. If negative, is clockwise.
  109. inline float SignedArea(const Vector3& v0, const Vector3& v1, const Vector3& v2) const;
  110. /// Calculate viewport transform.
  111. void CalculateViewport();
  112. /// Draw a triangle.
  113. void DrawTriangle(Vector4* vertices);
  114. /// Clip vertices against a plane.
  115. void ClipVertices(const Vector4& plane, Vector4* vertices, bool* triangles, unsigned& numTriangles);
  116. /// Draw a clipped triangle.
  117. void DrawTriangle2D(const Vector3* vertices, bool clockwise);
  118. /// Highest level depth buffer.
  119. int* buffer_;
  120. /// Buffer width.
  121. int width_;
  122. /// Buffer height.
  123. int height_;
  124. /// Number of rendered triangles.
  125. unsigned numTriangles_;
  126. /// Maximum number of triangles.
  127. unsigned maxTriangles_;
  128. /// Culling mode.
  129. CullMode cullMode_;
  130. /// Depth hierarchy needs update flag.
  131. bool depthHierarchyDirty_;
  132. /// Culling reverse flag.
  133. bool reverseCulling_;
  134. /// View transform matrix.
  135. Matrix3x4 view_;
  136. /// Projection matrix.
  137. Matrix4 projection_;
  138. /// Combined view and projection matrix.
  139. Matrix4 viewProj_;
  140. /// Last used timer.
  141. Timer useTimer_;
  142. /// Near clip distance.
  143. float nearClip_;
  144. /// Far clip distance.
  145. float farClip_;
  146. /// X scaling for viewport transform.
  147. float scaleX_;
  148. /// Y scaling for viewport transform.
  149. float scaleY_;
  150. /// X offset for viewport transform.
  151. float offsetX_;
  152. /// Y offset for viewport transform.
  153. float offsetY_;
  154. /// Combined X projection and viewport transform.
  155. float projOffsetScaleX_;
  156. /// Combined Y projection and viewport transform.
  157. float projOffsetScaleY_;
  158. /// Highest level buffer with safety padding.
  159. SharedArrayPtr<int> fullBuffer_;
  160. /// Reduced size depth buffers.
  161. Vector<SharedArrayPtr<DepthValue> > mipBuffers_;
  162. };
  163. }