OcclusionBuffer.h 6.0 KB

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