0026122: Visualization, TKOpenGl - clipping and capping is broken when ffp is disable...
[occt.git] / src / OpenGl / OpenGl_CappingAlgo.cxx
old mode 100644 (file)
new mode 100755 (executable)
index 3340a97..50cc186
@@ -1,21 +1,17 @@
 // Created on: 2013-09-05
 // Created by: Anton POLETAEV
-// Copyright (c) 2013 OPEN CASCADE SAS
+// Copyright (c) 2013-2014 OPEN CASCADE SAS
 //
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
+// This file is part of Open CASCADE Technology software library.
 //
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
 //
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
 #include <OpenGl_CappingAlgo.hxx>
 #include <OpenGl_Workspace.hxx>
 #include <OpenGl_PrimitiveArray.hxx>
 #include <OpenGl_CappingPlaneResource.hxx>
 #include <OpenGl_Vec.hxx>
-
-IMPLEMENT_STANDARD_HANDLE(OpenGl_CappingAlgoFilter, OpenGl_RenderFilter)
-IMPLEMENT_STANDARD_RTTIEXT(OpenGl_CappingAlgoFilter, OpenGl_RenderFilter)
-
-Handle(OpenGl_RenderFilter) OpenGl_CappingAlgo::myRenderFilter;
-OpenGl_AspectFace OpenGl_CappingAlgo::myFrontCulling;
-OpenGl_AspectFace OpenGl_CappingAlgo::myNoneCulling;
-Standard_Boolean OpenGl_CappingAlgo::myIsInit = Standard_False;
+#include <OpenGl_Structure.hxx>
+#include <Graphic3d_GraphicDriver.hxx>
 
 namespace
 {
-  static const OpenGl_Vec4 THE_CAPPING_PLN_VERTS[12] =
-    { OpenGl_Vec4 ( 0.0f, 0.0f, 0.0f, 1.0f),
-      OpenGl_Vec4 ( 1.0f, 0.0f, 0.0f, 0.0f),
-      OpenGl_Vec4 ( 0.0f, 0.0f, 1.0f, 0.0f),
-      OpenGl_Vec4 ( 0.0f, 0.0f, 0.0f, 1.0f),
-      OpenGl_Vec4 ( 0.0f, 0.0f, 1.0f, 0.0f),
-      OpenGl_Vec4 (-1.0f, 0.0f, 0.0f, 0.0f),
-      OpenGl_Vec4 ( 0.0f, 0.0f, 0.0f, 1.0f),
-      OpenGl_Vec4 (-1.0f, 0.0f, 0.0f, 0.0f),
-      OpenGl_Vec4 ( 0.0f, 0.0f,-1.0f, 0.0f),
-      OpenGl_Vec4 ( 0.0f, 0.0f, 0.0f, 1.0f),
-      OpenGl_Vec4 ( 0.0f, 0.0f,-1.0f, 0.0f),
-      OpenGl_Vec4 ( 1.0f, 0.0f, 0.0f, 0.0f) };
-
-  static const OpenGl_Vec4 THE_CAPPING_PLN_TCOORD[12] = 
-    { OpenGl_Vec4 ( 0.0f, 0.0f, 0.0f, 1.0f),
-      OpenGl_Vec4 ( 1.0f, 0.0f, 0.0f, 0.0f),
-      OpenGl_Vec4 ( 0.0f, 1.0f, 0.0f, 0.0f),
-      OpenGl_Vec4 ( 0.0f, 0.0f, 0.0f, 1.0f),
-      OpenGl_Vec4 ( 0.0f, 1.0f, 0.0f, 0.0f),
-      OpenGl_Vec4 (-1.0f, 0.0f, 0.0f, 0.0f),
-      OpenGl_Vec4 ( 0.0f, 0.0f, 0.0f, 1.0f),
-      OpenGl_Vec4 (-1.0f, 0.0f, 0.0f, 0.0f),
-      OpenGl_Vec4 ( 0.0f,-1.0f, 0.0f, 0.0f),
-      OpenGl_Vec4 ( 0.0f, 0.0f, 0.0f, 1.0f),
-      OpenGl_Vec4 ( 0.0f,-1.0f, 0.0f, 0.0f),
-      OpenGl_Vec4 ( 1.0f, 0.0f, 0.0f, 0.0f) };
-};
+#if !defined(GL_ES_VERSION_2_0)
+  static const GLint THE_FILLPRIM_FROM = GL_TRIANGLES;
+  static const GLint THE_FILLPRIM_TO   = GL_POLYGON;
+#else
+  static const GLint THE_FILLPRIM_FROM = GL_TRIANGLES;
+  static const GLint THE_FILLPRIM_TO   = GL_TRIANGLE_FAN;
+#endif
+}
 
 // =======================================================================
 // function : RenderCapping
 // purpose  :
 // =======================================================================
-void OpenGl_CappingAlgo::RenderCapping (const Handle(OpenGl_Workspace)& theWorkspace,
-                                        const OpenGl_ListOfGroup& theGroups)
+void OpenGl_CappingAlgo::RenderCapping (const Handle(OpenGl_Workspace)&  theWorkspace,
+                                        const Graphic3d_SequenceOfGroup& theGroups)
 {
-  // do not draw capping surface for second transparency pass
-  if (theWorkspace->NamedStatus & OPENGL_NS_2NDPASSDO)
-    return;
-
   const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
 
-  // check whether algorithm need to be runned
+  // check whether algorithm need to be performed
   Standard_Boolean isCapping = Standard_False;
-  Graphic3d_SetOfHClipPlane aContextPlanes = aContext->Clipping().Planes();
-  Graphic3d_SetOfHClipPlane::Iterator aCappingIt (aContextPlanes);
+  const Graphic3d_SequenceOfHClipPlane& aContextPlanes = aContext->Clipping().Planes();
+  Graphic3d_SequenceOfHClipPlane::Iterator aCappingIt (aContextPlanes);
   for (; aCappingIt.More(); aCappingIt.Next())
   {
     const Handle(Graphic3d_ClipPlane)& aPlane = aCappingIt.Value();
@@ -92,22 +58,26 @@ void OpenGl_CappingAlgo::RenderCapping (const Handle(OpenGl_Workspace)& theWorks
 
   // do not perform algorithm is there is nothing to render
   if (!isCapping)
+  {
     return;
-
-  // init internal data
-  Init();
+  }
 
   // remember current aspect face defined in workspace
   const OpenGl_AspectFace* aFaceAsp = theWorkspace->AspectFace (Standard_False);
 
   // replace primitive groups rendering filter
-  static Handle(OpenGl_CappingAlgoFilter) aCappingFilter = new OpenGl_CappingAlgoFilter();
   Handle(OpenGl_RenderFilter) aRenderFilter = theWorkspace->GetRenderFilter();
-  theWorkspace->SetRenderFilter (aCappingFilter);
+  theWorkspace->SetRenderFilter (theWorkspace->DefaultCappingAlgoFilter());
 
   // prepare for rendering the clip planes
   glEnable (GL_STENCIL_TEST);
 
+  // remember current state of depth
+  // function and change its value
+  GLint aDepthFuncPrev;
+  glGetIntegerv (GL_DEPTH_FUNC, &aDepthFuncPrev);
+  glDepthFunc (GL_LESS);
+
   // generate capping for every clip plane
   for (aCappingIt.Init (aContextPlanes); aCappingIt.More(); aCappingIt.Next())
   {
@@ -119,19 +89,19 @@ void OpenGl_CappingAlgo::RenderCapping (const Handle(OpenGl_Workspace)& theWorks
     }
 
     // enable only the rendering plane to generate stencil mask
-    Graphic3d_SetOfHClipPlane::Iterator aPlaneIt (aContextPlanes);
+    Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (aContextPlanes);
     for (; aPlaneIt.More(); aPlaneIt.Next())
     {
       const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
       const Standard_Boolean isOn = (aPlane == aRenderPlane);
-      aContext->ChangeClipping().SetEnabled (aPlane, isOn);
+      aContext->ChangeClipping().SetEnabled (aContext, aPlane, isOn);
     }
 
     glClear (GL_STENCIL_BUFFER_BIT);
     glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
 
     // override aspects, disable culling
-    theWorkspace->SetAspectFace (NoneCulling());
+    theWorkspace->SetAspectFace (&theWorkspace->NoneCulling());
     theWorkspace->AspectFace (Standard_True);
 
     // evaluate number of pair faces
@@ -140,14 +110,16 @@ void OpenGl_CappingAlgo::RenderCapping (const Handle(OpenGl_Workspace)& theWorks
     glStencilFunc (GL_ALWAYS, 1, 0x01);
     glStencilOp (GL_KEEP, GL_INVERT, GL_INVERT);
 
-    OpenGl_ListOfGroup::Iterator aGroupIt (theGroups);
-    for (; aGroupIt.More(); aGroupIt.Next())
+    for (OpenGl_Structure::GroupIterator aGroupIt (theGroups); aGroupIt.More(); aGroupIt.Next())
     {
-      aGroupIt.Value()->Render (theWorkspace);
+      if (aGroupIt.Value()->IsClosed())
+      {
+        aGroupIt.Value()->Render (theWorkspace);
+      }
     }
 
-    // override material, cull backfaces
-    theWorkspace->SetAspectFace (FrontCulling());
+    // override material, cull back faces
+    theWorkspace->SetAspectFace (&theWorkspace->FrontCulling());
     theWorkspace->AspectFace (Standard_True);
 
     // enable all clip plane except the rendered one
@@ -155,13 +127,12 @@ void OpenGl_CappingAlgo::RenderCapping (const Handle(OpenGl_Workspace)& theWorks
     {
       const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
       const Standard_Boolean isOn = (aPlane != aRenderPlane);
-      aContext->ChangeClipping().SetEnabled (aPlane, isOn);
+      aContext->ChangeClipping().SetEnabled (aContext, aPlane, isOn);
     }
 
     // render capping plane using the generated stencil mask
     glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
     glDepthMask (GL_TRUE);
-    glDepthFunc (GL_LESS);
     glStencilFunc (GL_EQUAL, 1, 0x01);
     glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
     glEnable (GL_DEPTH_TEST);
@@ -171,13 +142,14 @@ void OpenGl_CappingAlgo::RenderCapping (const Handle(OpenGl_Workspace)& theWorks
 
   // restore previous application state
   glClear (GL_STENCIL_BUFFER_BIT);
+  glDepthFunc (aDepthFuncPrev);
   glStencilFunc (GL_ALWAYS, 0, 0xFF);
   glDisable (GL_STENCIL_TEST);
 
   // enable clipping
   for (aCappingIt.Init (aContextPlanes); aCappingIt.More(); aCappingIt.Next())
   {
-    aContext->ChangeClipping().SetEnabled (aCappingIt.Value(), Standard_True);
+    aContext->ChangeClipping().SetEnabled (aContext, aCappingIt.Value(), Standard_True);
   }
 
   // restore rendering aspects
@@ -214,23 +186,16 @@ void OpenGl_CappingAlgo::RenderPlane (const Handle(OpenGl_Workspace)& theWorkspa
     theWorkspace->SetAspectFace (aPlaneAspect);
   }
 
-  // apply aspect for rendering
-  theWorkspace->AspectFace (Standard_True);
-
   // set identity model matrix
-  const OpenGl_Matrix* aModelMatrix = theWorkspace->SetStructureMatrix (&OpenGl_IdentityMatrix);
-
-  glMultMatrixf ((const GLfloat*)aPlaneRes->Orientation());
-  glNormal3f (0.0f, 1.0f, 0.0f);
-  glEnableClientState (GL_VERTEX_ARRAY);
-  glVertexPointer (4, GL_FLOAT, 0, (GLfloat* )&THE_CAPPING_PLN_VERTS);
-  glEnableClientState (GL_TEXTURE_COORD_ARRAY);
-  glTexCoordPointer (4, GL_FLOAT, 0, (GLfloat*)&THE_CAPPING_PLN_TCOORD);
-  glDrawArrays (GL_TRIANGLES, 0, 12);
-  glDisableClientState (GL_VERTEX_ARRAY);
-  glDisableClientState (GL_TEXTURE_COORD_ARRAY);
-
-  theWorkspace->SetStructureMatrix (aModelMatrix);
+  aContext->ModelWorldState.Push();
+  aContext->ModelWorldState.SetCurrent (OpenGl_Mat4::Map (*aPlaneRes->Orientation()->mat));
+  aContext->ApplyModelViewMatrix();
+
+  aPlaneRes->Primitives().Render (theWorkspace);
+
+  aContext->ModelWorldState.Pop();
+  aContext->ApplyModelViewMatrix();
+
   theWorkspace->SetAspectFace (aFaceAspect);
 
   // set delayed resource release
@@ -238,47 +203,14 @@ void OpenGl_CappingAlgo::RenderPlane (const Handle(OpenGl_Workspace)& theWorkspa
   aContext->ReleaseResource (aResId, Standard_True);
 }
 
-// =======================================================================
-// function : Init
-// purpose  :
-// =======================================================================
-void OpenGl_CappingAlgo::Init()
-{
-  if (myIsInit)
-    return;
-
-  myRenderFilter = new OpenGl_CappingAlgoFilter();
-  myNoneCulling.CullingMode = TelCullNone;
-  myNoneCulling.Edge = 0;
-
-  myFrontCulling.CullingMode = TelCullBack;
-  myFrontCulling.Edge = 0;
-
-  myIsInit = Standard_True;
-}
-
 // =======================================================================
 // function : CanRender
 // purpose  :
 // =======================================================================
 Standard_Boolean OpenGl_CappingAlgoFilter::CanRender (const OpenGl_Element* theElement)
 {
-  const OpenGl_PrimitiveArray* aPArray =
-    dynamic_cast<const OpenGl_PrimitiveArray*> (theElement);
-  if (!aPArray)
-    return Standard_False;
-
-  switch (aPArray->PArray()->type)
-  {
-    case TelPolygonsArrayType :
-    case TelTrianglesArrayType :
-    case TelQuadranglesArrayType :
-    case TelTriangleStripsArrayType :
-    case TelQuadrangleStripsArrayType :
-    case TelTriangleFansArrayType :
-      return Standard_True;
-
-    default:
-      return Standard_False;
-  }
+  const OpenGl_PrimitiveArray* aPArray = dynamic_cast<const OpenGl_PrimitiveArray*> (theElement);
+  return aPArray != NULL
+      && aPArray->DrawMode() >= THE_FILLPRIM_FROM
+      && aPArray->DrawMode() <= THE_FILLPRIM_TO;
 }