OcclusionBuffer.h 5.7 KB

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