View Issue Details

IDProjectCategoryView StatusLast Update
0027748Open CASCADEOCCT:Visualizationpublic2021-08-24 14:13
Reporterkgv Assigned Tokgv  
Status newResolutionopen 
Target Version7.7.0 
Summary0027748: Visualization - clipping and capping improvements
DescriptionThis is a meta task describing possible improvements of Clipping / Capping algorithms in TKV3d/TKOpenGl.

Current *Clipping* algorithm:
- glClipPlane() within FFP (deprecated).
- discard within Fragment Shader.
In both cases Back Face Culling is explicitly DISABLED even for closed primitives to properly render object interior.

for (int aPlaneIter = 0; aPlaneIter < occClipPlaneCount; ++aPlaneIter)
  vec4 aClipEquation = occClipPlaneEquations[aPlaneIter];
  int  aClipSpace    = occClipPlaneSpaces[aPlaneIter];
  if (aClipSpace == OccEquationCoords_World)
    if (dot (, + aClipEquation.w < 0.0)
  else if (aClipSpace == OccEquationCoords_View)
    if (dot (, + aClipEquation.w < 0.0)

1) Hardware supporting gl_ClipDistance (GLSL1.3 on desktop; APPLE_clip_distance or GL_EXT_clip_cull_distance on OpenGL ES)
   might show better performance since it allows skipping Fragment Shader if entire primitive (e.g. triangle) is behind the clipping plane(s).
(*) 2) Fragment Shader iterates through Clipping Planes within for loop limited by value of uniform variable occClipPlaneCount.
   On mobile hardware, this is quite expensive approach, and it is better to unroll the loop for fixed number of clipping planes.
   It might be reasonable to cache GLSL programs for 1-2 Clipping Planes and use general approach for greater amount.
(*) 3) Clipping performed on GPU on per-fragment level even if entire object is clipped.
   In case when global Clipping Plane is enabled, clipping objects on CPU side might dramatically improve performance on big models of many small parts,
   especially considering expensive multi-pass Capping Plane rendering.
   Moreover, if planes do not clip bounding box of presentation,
   expensive capping might be also skipped and discard from Fragmetn shader.

Current *Capping* algorithm:

- Draw object as is, with Capping planes used for Clipping.
- Iterate through Capping planes.
  - Disable all Clipping/Capping planes, enable active Capping plane for Clipping.
  - Draw object (only closed groups) with disabled Color/Depth writes to fill in Stencil.
  - Enable all Clipping/Capping planes for Clipping except active Capping plane.
  - Draw Capping plane as triangulation with special coordinates (using special .w values for infinite rendering)
    with dedicated FillArea aspect (possible hatching, lighting, texturing).
    Stencil test is used for rendering, Depth is written.

4) Back Face Culling is DISABLED even if only Capping (no Clipping) planes are defined.
   This is done for simplifying rendering pipeline, however for efficiency it is better
   enabling Back Face Culling if there are no Clipping planes (only Capping)
   and there Capping does not define hatching style or transparency (note that transparency can not be enabled by current API).

In addition, Capping planes which are ordered opposite to camera can be entirely skipped if object is fully opaque.

(*) 5) Capping FillArea aspect is managed inefficiently.
   allocates Graphic3d_AspectFillArea3d on each pass even if it is not changed.
   It is better storing Graphic3d_AspectFillArea3d directly in Graphic3d_ClipPlane.
(*) 6) Clipping planes lists are managed by value.
   OpenGl_Structure::Render() performs continuous coping of User planes (defined at structure level)
   into temporary sequences and then into sequence in OpenGl_Clipping.
   It makes sense to optimize this logic and avoid extra copying,
   so that OpenGl_Clipping will take handle to the extra sequences.
   This also implies storing clipping planes in Graphic3d_CStructure
   (but we can keep some assigning methods as deprecated for compatibility with old high-level API).
   Similar changes can be done for a global list of Clipping planes.

(*) 7) Box Clipping or Section 3/4.
   This is very useful feature that is unsupported by current algorithm.
   It can be implemented with minor efforts as GLSL Shader extension:
   - Introduce new flag "Chain" for each Clipping indicating the number of planes in the Chain.
     Default value is 1 meaning sole Clipping Plane.
     Validate definition consistency at C++ level.
   - Extend GLSL program, so that Fragment is discarded only when test is passed for all Planes in a Chain.
pln[0].Chain = 1;
pln[1].Chain = 3; //!< chain start
pln[2].Chain = 2;
pln[3].Chain = 1; //!< chain end
pln[4].Chain = 1;
for (int aPlnIter = 0; aPlnIter < NbPlanes;)
  aPlane = pln[aPlnIter];
  if (!ToClip (aPlane))
    aPlnIter += aPlane.Chain;

  if (aPlane.Chain == 1) { discard; }

(*) 8) *Capping* preserving *original material*.

   Current algorithm implies dedicated FillArea aspect for each Capping Plane.
   This might be useful (e.g. it allows drawing Capping with special texture),
   but complicates usage when Capping should preserve material of the object.

   Since object might be defined with multiple materials (e.g. AIS_ColoredShape),
   such scenario is not supported by current API, which allows
   defining Clipping Planes only on Structure (Presentation) level.
   So that different primitive Groups within single Presentation can not be drawn
   with different Capping material without splitting Interactive Object into several ones.

   And in latter case (although it is already complicated implementing such approach in application)
   Capping Plane should be drawn more times reducing overall performance (mostly due to higher CPU load).

   It can be useful providing an API simplifying such scenario at application level.

9) Reduce amount of rendering passes for rendering Capping Plane.

   Current algorithm implies significant amount of rendering (and technical) passes for rendering the Capping Plane:
    > render object (normal), clear Stencil, render object (fill Stencil, no color), render plane <
   Thus, it adds 2 extra rendering passes (+1 for clearing Stencil) per Capping plane.
   This dramatically affects performance on big models, increasing CPU (multiple passes) and GPU load.

   In some cases it might be reasonable implementing 2-passes (+1 technical for clearing Stencil)
   rendering technique involving more complex GLSL program.
   This, hover, would require also extra rendering pass for not-closed primitive groups in Structure.

   - First rendering pass.
     Render Back Faces only with simple Clipping - color write off, stencil always sets 1.
   - Second rendering pass.
     Fragment Shader performs special handling of Back Faces if fragment is not clipped by planes.
     Shader constructs a ray from world position to the camera eye / projection on screen plane,
     and searches the farthest intersection with Capping Planes.
     The lighting is computed using Capping Plane normal and gl_FragDepth is computed explicitly.

  Although such algorithm is relatively expensive for slow GPUs (but it should not be a problem for modern desktop GPUs),
  it will reduce overall number of rendering passes (especially for more then 1 Capping Plane)
  and implicitly allows keeping Material of original object (thus no extra Structures / Capping rendering passes for each Material).
Steps To ReproduceN/A
TagsNo tags attached.
Test case number


parent of 0027751 closedbugmaster Open CASCADE Visualization, Graphic3d_ClipPlane - add option to inherit material from object 
parent of 0027787 closedbugmaster Open CASCADE Visualization, TKOpenGl - Optimize rendering by additional check whether the object is fully clipped or not 
parent of 0027816 closedbugmaster Open CASCADE Visualization - provide an API for overriding clipping planes list 
parent of 0027189 newkgv Community Visualization - support combination of hatching and capping 
parent of 0028177 newkgv Community Visualization - MSAA has no effect on clipping planes when using built-in GLSL programs 
parent of 0029729 closedbugmaster Open CASCADE Visualization, Graphic3d_ClipPlane - add support of clipping plane chains 
related to 0027750 closedbugmaster Open CASCADE Visualization, V3d_View - remove unused functionality ZClipping and ZCueing 
related to 0027223 assignedmzernova Open CASCADE Visualization, Ray Tracing - add support of clipping planes 
Not all the children of this issue are yet resolved or closed.



2016-08-27 13:02

developer   ~0057226

5 and 8 is done in scope of 0027751


2016-08-27 13:04

developer   ~0057227

2 and 3 is done in scope of 0027787


2016-09-16 09:46

developer   ~0057843

6 is done in scope of 0027816

Issue History

Date Modified Username Field Change
2016-08-03 12:47 kgv New Issue
2016-08-03 12:47 kgv Assigned To => kgv
2016-08-03 12:47 kgv Assigned To kgv => dbp
2016-08-03 12:47 kgv Assigned To dbp => san
2016-08-03 12:47 kgv Assigned To san => kgv
2016-08-03 17:21 kgv Relationship added related to 0027750
2016-08-04 18:53 kgv Relationship added parent of 0027751
2016-08-16 23:54 kgv Description Updated
2016-08-17 17:40 kgv Relationship added parent of 0027787
2016-08-27 13:02 kgv Note Added: 0057226
2016-08-27 13:04 kgv Note Added: 0057227
2016-08-27 13:04 kgv Description Updated
2016-08-27 13:15 kgv Relationship added parent of 0027816
2016-09-09 18:09 kgv Relationship added related to 0027223
2016-09-16 09:46 kgv Note Added: 0057843
2016-09-16 09:46 kgv Description Updated
2016-09-29 12:03 kgv Relationship added parent of 0027189
2016-10-25 09:29 kgv Target Version 7.1.0 => 7.2.0
2016-11-30 15:20 kgv Relationship added parent of 0028177
2017-07-20 10:59 kgv Target Version 7.2.0 => 7.4.0
2018-04-26 16:06 kgv Relationship added parent of 0029729
2018-05-04 00:28 kgv Description Updated
2018-05-04 00:30 kgv Description Updated
2019-09-04 15:36 abv Target Version 7.4.0 => 7.5.0
2020-08-28 14:32 kgv Target Version 7.5.0 => 7.6.0
2021-08-24 14:13 kgv Target Version 7.6.0 => 7.7.0