View.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "Batch.h"
  25. #include "Object.h"
  26. class Camera;
  27. class DebugRenderer;
  28. class Light;
  29. class Drawable;
  30. class OcclusionBuffer;
  31. class RenderSurface;
  32. class Zone;
  33. struct Viewport;
  34. static const int MAX_LIGHT_SPLITS = 6;
  35. /// Geometry view space depth minimum and maximum values
  36. struct GeometryDepthBounds
  37. {
  38. float min_;
  39. float max_;
  40. };
  41. /// Helper structure for checking whether a transparent object is already lit by a certain light
  42. struct LitTransparencyCheck
  43. {
  44. LitTransparencyCheck(Light* light, Drawable* drawable, unsigned batchIndex) :
  45. light_(light),
  46. drawable_(drawable),
  47. batchIndex_(batchIndex)
  48. {
  49. }
  50. /// Test if less than another lit transparency check
  51. bool operator < (const LitTransparencyCheck& rhs) const
  52. {
  53. if (light_ == rhs.light_)
  54. {
  55. if (drawable_ == rhs.drawable_)
  56. return batchIndex_ < rhs.batchIndex_;
  57. else
  58. return drawable_ < rhs.drawable_;
  59. }
  60. else
  61. return light_ < rhs.light_;
  62. }
  63. /// Test if greater than another lit transparency check
  64. bool operator > (const LitTransparencyCheck& rhs) const
  65. {
  66. if (light_ == rhs.light_)
  67. {
  68. if (drawable_ == rhs.drawable_)
  69. return batchIndex_ > rhs.batchIndex_;
  70. else
  71. return drawable_ > rhs.drawable_;
  72. }
  73. else
  74. return light_ > rhs.light_;
  75. }
  76. Light* light_;
  77. Drawable* drawable_;
  78. unsigned batchIndex_;
  79. };
  80. /// A rendering view. Includes the main view(s) and any auxiliary views, but not shadow cameras
  81. class View : public Object
  82. {
  83. OBJECT(View);
  84. public:
  85. /// Construct
  86. View(Context* context);
  87. /// Destruct
  88. virtual ~View();
  89. /// Define with rendertarget and viewport. Return true if successful
  90. bool Define(RenderSurface* renderTarget, const Viewport& viewport);
  91. /// Update culling and construct rendering batches
  92. void Update(const FrameInfo& frame);
  93. /// Render batches
  94. void Render();
  95. /// Return octree
  96. Octree* GetOctree() const { return octree_; }
  97. /// Return camera
  98. Camera* GetCamera() const { return camera_; }
  99. /// Return zone
  100. Zone* GetZone() const { return zone_; }
  101. /// Return the render target. 0 if using the backbuffer
  102. RenderSurface* GetRenderTarget() const { return renderTarget_; }
  103. /// Return the depth stencil. 0 if using the backbuffer's depth stencil
  104. RenderSurface* GetDepthStencil() const { return depthStencil_; }
  105. /// Return geometry objects
  106. const std::vector<Drawable*>& GetGeometries() const { return geometries_; }
  107. /// Return occluder objects
  108. const std::vector<Drawable*>& GetOccluders() const { return occluders_; }
  109. /// Return directional light shadow rendering occluders
  110. const std::vector<Drawable*>& GetShadowOccluders() const { return shadowOccluders_; }
  111. /// Return lights
  112. const std::vector<Light*>& GetLights() const { return lights_; }
  113. /// Return light batch queues
  114. const std::vector<LightBatchQueue>& GetLightQueues() const { return lightQueues_; }
  115. private:
  116. /// Query the octree for scene nodes
  117. void GetDrawables();
  118. /// Construct batches from the scene nodes
  119. void GetBatches();
  120. /// Get lit batches for a certain light and drawable
  121. void GetLitBatches(Drawable* drawable, Light* light, Light* SplitLight, LightBatchQueue* lightQueue, std::set<LitTransparencyCheck>& litTransparencies, PassType gBufferPass);
  122. /// Render batches, forward mode
  123. void RenderBatcheforward();
  124. /// Render batches, deferred mode
  125. void RenderBatchesDeferred();
  126. /// Query for occluders as seen from a camera
  127. void UpdateOccluders(std::vector<Drawable*>& occluders, Camera* camera);
  128. /// Draw occluders to occlusion buffer
  129. void DrawOccluders(OcclusionBuffer* buffer, const std::vector<Drawable*>& occluders);
  130. /// Query for lit geometries and shadow casters for a light
  131. unsigned ProcessLight(Light* light);
  132. /// Generate combined bounding boxes for lit geometries and shadow casters and check shadow caster visibility
  133. void ProcessLightQuery(unsigned splitIndex, const std::vector<Drawable*>& result, BoundingBox& geometryBox, BoundingBox& shadowSpaceBox, bool getLitGeometries, bool GetShadowCasters);
  134. /// Check visibility of one shadow caster
  135. bool IsShadowCasterVisible(Drawable* drawable, BoundingBox lightViewBox, Camera* shadowCamera, const Matrix4x3& lightView, const Frustum& lightViewFrustum, const BoundingBox& lightViewFrustumBox);
  136. /// Set up initial shadow camera view
  137. void SetupShadowCamera(Light* light, bool shadowOcclusion = false);
  138. /// Focus shadow camera to use shadow map texture space more optimally
  139. void FocusShadowCamera(Light* light, const BoundingBox& geometryBox, const BoundingBox& shadowCasterBox);
  140. /// Quantize the directional light shadow camera view to eliminate artefacts
  141. void QuantizeDirShadowCamera(Light* light, const BoundingBox& viewBox);
  142. /// Optimize light rendering by setting up a scissor rectangle
  143. void OptimizeLightByScissor(Light* light);
  144. /// Return scissor rectangle for a light
  145. const Rect& GetLightScissor(Light* light);
  146. /// Split directional or point light for shadow rendering
  147. unsigned SplitLight(Light* light);
  148. /// Return material technique, considering the drawable's LOD distance
  149. Technique* GetTechnique(Drawable* drawable, Material*& material);
  150. /// Check if material should render an auxiliary view (if it has a camera attached)
  151. void CheckMaterialForAuxView(Material* material);
  152. /// Sort all batches
  153. void SortBatches();
  154. /// Prepare instancing buffer by filling it with all instance transforms
  155. void PrepareInstancingBuffer();
  156. /// Set per-view shader parameters
  157. void SetShaderParameters();
  158. /// Draw a split light to stencil buffer
  159. void DrawSplitLightToStencil(Camera& camera, Light* light, bool clear = false);
  160. /// Draw everything in a batch queue, priority batches first
  161. void RenderBatchQueue(const BatchQueue& queue, bool useScissor = false, bool useLightBuffer = false);
  162. /// Draw a forward (shadowed) light batch queue
  163. void RenderForwardLightBatchQueue(const BatchQueue& queue, Light* forwardQueueLight);
  164. /// Render a shadow map
  165. void RenderShadowMap(const LightBatchQueue& queue);
  166. /// Graphics
  167. WeakPtr<Graphics> graphics_;
  168. /// Renderer
  169. WeakPtr<Renderer> renderer_;
  170. /// Octree to use
  171. Octree* octree_;
  172. /// Camera to use
  173. Camera* camera_;
  174. /// Zone to get global rendering settings from
  175. Zone* zone_;
  176. /// Color buffer to use
  177. RenderSurface* renderTarget_;
  178. /// Depth buffer to use
  179. RenderSurface* depthStencil_;
  180. /// Screen rectangle
  181. IntRect screenRect_;
  182. /// Render target width
  183. int width_;
  184. /// Render target height
  185. int height_;
  186. /// Rendering mode
  187. RenderMode mode_;
  188. /// Draw shadows flag
  189. bool drawShadows_;
  190. /// Shader Model 3 support flag
  191. bool hasSM3_;
  192. /// Material quality level
  193. int materialQuality_;
  194. /// Maximum number of occluder triangles
  195. int maxOccluderTriangles_;
  196. /// Jitter counter for temporal antialiasing
  197. int jitterCounter_;
  198. /// Previous view matrix for temporal antialiasing
  199. Matrix4x3 lastCameraView_;
  200. /// Information of the frame being rendered
  201. FrameInfo frame_;
  202. /// Combined bounding box of visible geometries
  203. BoundingBox sceneBox_;
  204. /// Combined bounding box of visible geometries in view space
  205. BoundingBox sceneViewBox_;
  206. /// Cache for light scissor queries
  207. std::map<Light*, Rect> lightScissorCache_;
  208. /// Current split lights being processed
  209. Light* splitLights_[MAX_LIGHT_SPLITS];
  210. /// Current lit geometries being processed
  211. std::vector<Drawable*> litGeometries_[MAX_LIGHT_SPLITS];
  212. /// Current shadow casters being processed
  213. std::vector<Drawable*> shadowCasters_[MAX_LIGHT_SPLITS];
  214. /// Temporary drawable query result
  215. std::vector<Drawable*> tempDrawables_;
  216. /// Geometry objects
  217. std::vector<Drawable*> geometries_;
  218. /// Occluder objects
  219. std::vector<Drawable*> occluders_;
  220. /// Directional light shadow rendering occluders
  221. std::vector<Drawable*> shadowOccluders_;
  222. /// Depth minimum and maximum values for visible geometries
  223. std::vector<GeometryDepthBounds> geometryDepthBounds_;
  224. /// Lights
  225. std::vector<Light*> lights_;
  226. /// G-buffer size error displayed
  227. std::set<RenderSurface*> gBufferErrorDisplayed_;
  228. /// G-buffer batches
  229. BatchQueue gBufferQueue_;
  230. /// Base pass batches
  231. BatchQueue baseQueue_;
  232. /// Extra pass batches
  233. BatchQueue extraQueue_;
  234. /// Transparent geometry batches
  235. BatchQueue transparentQueue_;
  236. /// Unshadowed light volume batches
  237. BatchQueue noShadowLightQueue_;
  238. /// Shadowed light queues
  239. std::vector<LightBatchQueue> lightQueues_;
  240. };