View.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "HashSet.h"
  26. #include "Object.h"
  27. #include "Polyhedron.h"
  28. class Camera;
  29. class DebugRenderer;
  30. class Light;
  31. class Drawable;
  32. class OcclusionBuffer;
  33. class Octree;
  34. class RenderSurface;
  35. class Technique;
  36. class Texture2D;
  37. class Zone;
  38. struct Viewport;
  39. /// %Geometry view space depth minimum and maximum values.
  40. struct GeometryDepthBounds
  41. {
  42. /// Minimum value.
  43. float min_;
  44. /// Maximum value.
  45. float max_;
  46. };
  47. /// 3D rendering view. Includes the main view(s) and any auxiliary views, but not shadow cameras.
  48. class View : public Object
  49. {
  50. OBJECT(View);
  51. public:
  52. /// Construct.
  53. View(Context* context);
  54. /// Destruct.
  55. virtual ~View();
  56. /// Define with rendertarget and viewport. Return true if successful.
  57. bool Define(RenderSurface* renderTarget, const Viewport& viewport);
  58. /// Update culling and construct rendering batches.
  59. void Update(const FrameInfo& frame);
  60. /// Render batches.
  61. void Render();
  62. /// Return octree.
  63. Octree* GetOctree() const { return octree_; }
  64. /// Return camera.
  65. Camera* GetCamera() const { return camera_; }
  66. /// Return the render target. 0 if using the backbuffer.
  67. RenderSurface* GetRenderTarget() const { return renderTarget_; }
  68. /// Return the depth stencil. 0 if using the backbuffer's depth stencil.
  69. RenderSurface* GetDepthStencil() const { return depthStencil_; }
  70. /// Return geometry objects.
  71. const PODVector<Drawable*>& GetGeometries() const { return geometries_; }
  72. /// Return occluder objects.
  73. const PODVector<Drawable*>& GetOccluders() const { return occluders_; }
  74. /// Return directional light shadow rendering occluders.
  75. const PODVector<Drawable*>& GetShadowOccluders() const { return shadowOccluders_; }
  76. /// Return lights.
  77. const PODVector<Light*>& GetLights() const { return lights_; }
  78. /// Return light batch queues.
  79. const Vector<LightBatchQueue>& GetLightQueues() const { return lightQueues_; }
  80. private:
  81. /// Query the octree for drawable objects.
  82. void GetDrawables();
  83. /// Construct batches from the drawable objects.
  84. void GetBatches();
  85. /// Get lit batches for a certain light and drawable.
  86. void GetLitBatches(Drawable* drawable, LightBatchQueue& lightQueue);
  87. /// Render batches.
  88. void RenderBatches();
  89. /// Query for occluders as seen from a camera.
  90. void UpdateOccluders(PODVector<Drawable*>& occluders, Camera* camera);
  91. /// Draw occluders to occlusion buffer.
  92. void DrawOccluders(OcclusionBuffer* buffer, const PODVector<Drawable*>& occluders);
  93. /// Query for lit geometries and shadow casters for a light.
  94. unsigned ProcessLight(Light* light);
  95. /// Process shadow casters' visibilities and build their combined view- or projection-space bounding box.
  96. void ProcessShadowCasters(Light* light, unsigned splitIndex, const PODVector<Drawable*>& drawables, BoundingBox& shadowCasterBox);
  97. /// %Set up initial shadow camera view(s). Returns the number of splits used.
  98. unsigned SetupShadowCameras(Light* light);
  99. /// %Set up a directional light shadow camera
  100. void SetupDirLightShadowCamera(Camera* shadowCamera, Light* light, float nearSplit, float farSplit, bool shadowOcclusion);
  101. /// Finalize shadow camera view after shadow casters and the shadow map are known.
  102. void FinalizeShadowCamera(Camera* shadowCamera, Light* light, const IntRect& shadowViewport, const BoundingBox& shadowCasterBox);
  103. /// Quantize a directional light shadow camera view to eliminate swimming.
  104. void QuantizeDirLightShadowCamera(Camera* shadowCamera, Light* light, const IntRect& shadowViewport, const BoundingBox& viewBox);
  105. /// Check visibility of one shadow caster.
  106. bool IsShadowCasterVisible(Drawable* drawable, BoundingBox lightViewBox, Camera* shadowCamera, const Matrix3x4& lightView, const Frustum& lightViewFrustum, const BoundingBox& lightViewFrustumBox);
  107. /// Return the viewport for a shadow map split.
  108. IntRect GetShadowMapViewport(Light* light, unsigned splitIndex, Texture2D* shadowMap);
  109. /// Optimize light rendering by setting up a scissor rectangle.
  110. void OptimizeLightByScissor(Light* light);
  111. /// Optimize spot or point light rendering by drawing its volume to the stencil buffer.
  112. void OptimizeLightByStencil(Light* light);
  113. /// Return scissor rectangle for a light.
  114. const Rect& GetLightScissor(Light* light);
  115. /// Split directional or point light for shadow rendering.
  116. unsigned SplitLight(Light* light);
  117. /// Return the drawable's zone, or camera/default zone if not set.
  118. Zone* GetZone(Drawable* drawable);
  119. /// Return the drawable's light mask, considering also its zone.
  120. unsigned GetLightMask(Drawable* drawable);
  121. /// Return material technique, considering the drawable's LOD distance.
  122. Technique* GetTechnique(Drawable* drawable, Material*& material);
  123. /// Check if material should render an auxiliary view (if it has a camera attached.)
  124. void CheckMaterialForAuxView(Material* material);
  125. /// Sort all batches.
  126. void SortBatches();
  127. /// Prepare instancing buffer by filling it with all instance transforms.
  128. void PrepareInstancingBuffer();
  129. /// Render everything in a batch queue, priority batches first.
  130. void RenderBatchQueue(const BatchQueue& queue, bool useScissor = false);
  131. /// Render batches lit by a specific light.
  132. void RenderLightBatchQueue(const BatchQueue& queue, Light* forwardQueueLight);
  133. /// Render a shadow map.
  134. void RenderShadowMap(const LightBatchQueue& queue);
  135. /// Graphics subsystem.
  136. WeakPtr<Graphics> graphics_;
  137. /// Renderer subsystem.
  138. WeakPtr<Renderer> renderer_;
  139. /// Octree to use.
  140. Octree* octree_;
  141. /// Camera to use.
  142. Camera* camera_;
  143. /// Zone the camera is inside, or default zone if not assigned.
  144. Zone* cameraZone_;
  145. /// Color buffer to use.
  146. RenderSurface* renderTarget_;
  147. /// Depth buffer to use.
  148. RenderSurface* depthStencil_;
  149. /// Screen rectangle.
  150. IntRect screenRect_;
  151. /// Render target width.
  152. int width_;
  153. /// Render target height.
  154. int height_;
  155. /// Draw shadows flag.
  156. bool drawShadows_;
  157. /// Material quality level.
  158. int materialQuality_;
  159. /// Maximum number of occluder triangles.
  160. int maxOccluderTriangles_;
  161. /// Information of the frame being rendered.
  162. FrameInfo frame_;
  163. /// Information of the frame being rendered, with shadow camera.
  164. FrameInfo shadowFrame_;
  165. /// Camera frustum.
  166. Frustum frustum_;
  167. /// Combined bounding box of visible geometries.
  168. BoundingBox sceneBox_;
  169. /// Combined bounding box of visible geometries in view space.
  170. BoundingBox sceneViewBox_;
  171. /// Volume for frustum clipping.
  172. Polyhedron frustumVolume_;
  173. /// Current shadow cameras being processed.
  174. Camera* shadowCameras_[MAX_LIGHT_SPLITS];
  175. /// Current shadow casters being processed.
  176. PODVector<Drawable*> shadowCasters_[MAX_LIGHT_SPLITS];
  177. /// Combined bounding box of shadow casters in light view or projection space.
  178. BoundingBox shadowCasterBox_[MAX_LIGHT_SPLITS];
  179. /// Shadow camera near splits (directional lights only.)
  180. float shadowNearSplits_[MAX_LIGHT_SPLITS];
  181. /// Shadow camera far splits (directional lights only.)
  182. float shadowFarSplits_[MAX_LIGHT_SPLITS];
  183. /// Current lit geometries being processed.
  184. PODVector<Drawable*> litGeometries_;
  185. /// Temporary drawable query result.
  186. PODVector<Drawable*> tempDrawables_;
  187. /// Temporary zone query result.
  188. PODVector<Drawable*> tempZones_;
  189. /// Geometry objects.
  190. PODVector<Drawable*> geometries_;
  191. /// Occluder objects.
  192. PODVector<Drawable*> occluders_;
  193. /// Directional light shadow rendering occluders.
  194. PODVector<Drawable*> shadowOccluders_;
  195. /// Depth minimum and maximum values for visible geometries.
  196. PODVector<GeometryDepthBounds> geometryDepthBounds_;
  197. /// Lights.
  198. PODVector<Light*> lights_;
  199. /// Drawables that limit their maximum light count.
  200. HashSet<Drawable*> maxLightsDrawables_;
  201. /// Light queue indices of processed lights.
  202. Map<Light*, unsigned> lightQueueIndex_;
  203. /// Cache for light scissor queries.
  204. HashMap<Light*, Rect> lightScissorCache_;
  205. /// Base pass batches.
  206. BatchQueue baseQueue_;
  207. /// Pre-transparent pass batches.
  208. BatchQueue preAlphaQueue_;
  209. /// Transparent geometry batches.
  210. BatchQueue alphaQueue_;
  211. /// Post-transparent pass batches.
  212. BatchQueue postAlphaQueue_;
  213. /// Light queues.
  214. Vector<LightBatchQueue> lightQueues_;
  215. /// Current stencil value for light optimization.
  216. unsigned char lightStencilValue_;
  217. /// Camera zone's override flag.
  218. bool cameraZoneOverride_;
  219. };