OcclusionBuffer.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Copyright (c) 2008-2014 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 "ArrayPtr.h"
  24. #include "Frustum.h"
  25. #include "Object.h"
  26. #include "GraphicsDefs.h"
  27. #include "Timer.h"
  28. namespace Urho3D
  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 URHO3D_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, unsigned indexStart, unsigned indexCount);
  76. /// Build reduced size mip levels.
  77. void BuildDepthHierarchy();
  78. /// Reset last used timer.
  79. void ResetUseTimer();
  80. /// Return highest level depth values.
  81. int* GetBuffer() const { return buffer_; }
  82. /// Return view transform matrix.
  83. const Matrix3x4& GetView() const { return view_; }
  84. /// Return projection matrix.
  85. const Matrix4& GetProjection() const { return projection_; }
  86. /// Return buffer width.
  87. int GetWidth() const { return width_; }
  88. /// Return buffer height.
  89. int GetHeight() const { return height_; }
  90. /// Return number of rendered triangles.
  91. unsigned GetNumTriangles() const { return numTriangles_; }
  92. /// Return maximum number of triangles.
  93. unsigned GetMaxTriangles() const { return maxTriangles_; }
  94. /// Return culling mode.
  95. CullMode GetCullMode() const { return cullMode_; }
  96. /// Test a bounding box for visibility. For best performance, build depth hierarchy first.
  97. bool IsVisible(const BoundingBox& worldSpaceBox) const;
  98. /// Return time since last use in milliseconds.
  99. unsigned GetUseTimer();
  100. private:
  101. /// Apply modelview transform to vertex.
  102. inline Vector4 ModelTransform(const Matrix4& transform, const Vector3& vertex) const;
  103. /// Apply projection and viewport transform to vertex.
  104. inline Vector3 ViewportTransform(const Vector4& vertex) const;
  105. /// Clip an edge.
  106. inline Vector4 ClipEdge(const Vector4& v0, const Vector4& v1, float d0, float d1) const;
  107. /// Return signed area of a triangle. If negative, is clockwise.
  108. inline float SignedArea(const Vector3& v0, const Vector3& v1, const Vector3& v2) const;
  109. /// Calculate viewport transform.
  110. void CalculateViewport();
  111. /// Draw a triangle.
  112. void DrawTriangle(Vector4* vertices);
  113. /// Clip vertices against a plane.
  114. void ClipVertices(const Vector4& plane, Vector4* vertices, bool* triangles, unsigned& numTriangles);
  115. /// Draw a clipped triangle.
  116. void DrawTriangle2D(const Vector3* vertices, bool clockwise);
  117. /// Highest level depth buffer.
  118. int* buffer_;
  119. /// Buffer width.
  120. int width_;
  121. /// Buffer height.
  122. int height_;
  123. /// Number of rendered triangles.
  124. unsigned numTriangles_;
  125. /// Maximum number of triangles.
  126. unsigned maxTriangles_;
  127. /// Culling mode.
  128. CullMode cullMode_;
  129. /// Depth hierarchy needs update flag.
  130. bool depthHierarchyDirty_;
  131. /// Culling reverse flag.
  132. bool reverseCulling_;
  133. /// View transform matrix.
  134. Matrix3x4 view_;
  135. /// Projection matrix.
  136. Matrix4 projection_;
  137. /// Combined view and projection matrix.
  138. Matrix4 viewProj_;
  139. /// Last used timer.
  140. Timer useTimer_;
  141. /// Near clip distance.
  142. float nearClip_;
  143. /// Far clip distance.
  144. float farClip_;
  145. /// X scaling for viewport transform.
  146. float scaleX_;
  147. /// Y scaling for viewport transform.
  148. float scaleY_;
  149. /// X offset for viewport transform.
  150. float offsetX_;
  151. /// Y offset for viewport transform.
  152. float offsetY_;
  153. /// Combined X projection and viewport transform.
  154. float projOffsetScaleX_;
  155. /// Combined Y projection and viewport transform.
  156. float projOffsetScaleY_;
  157. /// Highest level buffer with safety padding.
  158. SharedArrayPtr<int> fullBuffer_;
  159. /// Reduced size depth buffers.
  160. Vector<SharedArrayPtr<DepthValue> > mipBuffers_;
  161. };
  162. }