0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / AIS / AIS_Triangulation.cxx
index b836b3a..dfc0d1e 100644 (file)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-#include <AIS_Drawer.hxx>
+#include <AIS_DisplayMode.hxx>
 #include <AIS_Triangulation.hxx>
 #include <AIS_InteractiveObject.hxx>
-#include <Standard_DefineHandle.hxx>
+#include <Standard_Type.hxx>
 #include <Poly_Array1OfTriangle.hxx>
 #include <Poly_Triangulation.hxx>
+#include <Prs3d_Drawer.hxx>
 #include <Prs3d_Root.hxx>
 #include <Prs3d_ShadingAspect.hxx>
 #include <TShort_Array1OfShortReal.hxx>
@@ -28,8 +29,7 @@
 #include <Graphic3d_ArrayOfTriangles.hxx>
 
 
-IMPLEMENT_STANDARD_HANDLE(AIS_Triangulation, AIS_InteractiveObject)
-IMPLEMENT_STANDARD_RTTIEXT(AIS_Triangulation, AIS_InteractiveObject)
+IMPLEMENT_STANDARD_RTTIEXT(AIS_Triangulation,AIS_InteractiveObject)
 
 AIS_Triangulation::AIS_Triangulation(const Handle(Poly_Triangulation)& Triangulation)
 {
@@ -39,6 +39,79 @@ AIS_Triangulation::AIS_Triangulation(const Handle(Poly_Triangulation)& Triangula
   myFlagColor     = 0;
 }
 
+//=======================================================================
+//function : SetTransparency
+//purpose  :
+//=======================================================================
+void AIS_Triangulation::SetTransparency (const Standard_Real theValue)
+{
+  if (!myDrawer->HasOwnShadingAspect())
+  {
+    myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
+    if (myDrawer->HasLink())
+    {
+      *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect();
+    }
+  }
+
+  // override transparency
+  myDrawer->ShadingAspect()->SetTransparency (theValue, myCurrentFacingModel);
+  myDrawer->SetTransparency ((Standard_ShortReal )theValue);
+
+  updatePresentation();
+}
+
+//=======================================================================
+//function : UnsetTransparency
+//purpose  :
+//=======================================================================
+void AIS_Triangulation::UnsetTransparency()
+{
+  myDrawer->SetTransparency (0.0f);
+  if (!myDrawer->HasOwnShadingAspect())
+  {
+    return;
+  }
+  else if (HasColor() || HasMaterial())
+  {
+    myDrawer->ShadingAspect()->SetTransparency (0.0, myCurrentFacingModel);
+  }
+
+  updatePresentation();
+}
+
+//=======================================================================
+//function : updatePresentation
+//purpose  :
+//=======================================================================
+void AIS_Triangulation::updatePresentation()
+{
+  if (HasVertexColors())
+  {
+    SetToUpdate (AIS_WireFrame);
+  }
+  else
+  {
+    // modify shading presentation without re-computation
+    const PrsMgr_Presentations&        aPrsList  = Presentations();
+    Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->ShadingAspect()->Aspect();
+    for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More(); aPrsIter.Next())
+    {
+      if (aPrsIter.Value()->Mode() != AIS_WireFrame)
+      {
+        continue;
+      }
+
+      const Handle(Prs3d_Presentation)& aPrs = aPrsIter.Value();
+      for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
+      {
+        const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
+        aGroup->SetGroupPrimitivesAspect (anAreaAsp);
+      }
+    }
+  }
+}
+
 //=======================================================================
 //function : Compute
 //purpose  :
@@ -47,30 +120,24 @@ void AIS_Triangulation::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aP
                                 const Handle(Prs3d_Presentation)& aPresentation,
                                 const Standard_Integer aMode)
 {
-  switch (aMode) 
+  switch (aMode)
   {
     case 0:
       const TColgp_Array1OfPnt& nodes = myTriangulation->Nodes();             //Nodes
       const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles();  //Triangle
 
       Standard_Boolean hasVNormals = myTriangulation->HasNormals();
-      Standard_Boolean hasVColors  = (myFlagColor == 1);
-
-      Handle(Graphic3d_ArrayOfTriangles) anArray =
-         new Graphic3d_ArrayOfTriangles ( myNbNodes,        //maxVertexs
-                                          myNbTriangles * 3,//maxEdges
-                                          hasVNormals,      //hasVNormals
-                                          hasVColors,       //hasVColors
-                                          Standard_False,   //hasTexels
-                                          Standard_True     //hasEdgeInfos
-                                         );
+      Standard_Boolean hasVColors  = HasVertexColors();
+
+      Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (myNbNodes, myNbTriangles * 3,
+                                                                                   hasVNormals, hasVColors, Standard_False);
       Handle(Graphic3d_Group) TheGroup = Prs3d_Root::CurrentGroup(aPresentation);
       Handle(Graphic3d_AspectFillArea3d) aspect = myDrawer->ShadingAspect()->Aspect();
 
       Standard_Integer i;
       Standard_Integer j;
 
-      Standard_Real ambient = aspect->FrontMaterial().Ambient();
+      const Standard_Real ambient = 0.2;
       if (hasVNormals)
       {
         const TShort_Array1OfShortReal& normals = myTriangulation->Normals();
@@ -80,7 +147,7 @@ void AIS_Triangulation::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aP
           for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
           {
             j = (i - nodes.Lower()) * 3;
-            anArray->AddVertex(nodes(i), AttenuateColor(colors(i), ambient));
+            anArray->AddVertex(nodes(i), attenuateColor(colors(i), ambient));
             anArray->SetVertexNormal(i, normals(j+1), normals(j+2), normals(j+3));
           }
         }
@@ -101,7 +168,7 @@ void AIS_Triangulation::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aP
           const TColStd_Array1OfInteger& colors = myColor->Array1();
           for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
           {
-            anArray->AddVertex(nodes(i), AttenuateColor(colors(i), ambient));
+            anArray->AddVertex(nodes(i), attenuateColor(colors(i), ambient));
           }
         }
         else // !hasVColors
@@ -128,7 +195,7 @@ void AIS_Triangulation::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aP
 
 //=======================================================================
 //function : ComputeSelection
-//purpose  : 
+//purpose  :
 //=======================================================================
 void AIS_Triangulation::ComputeSelection(const Handle(SelectMgr_Selection)& /*aSelection*/,
                                          const Standard_Integer /*aMode*/)
@@ -163,7 +230,7 @@ Handle(TColStd_HArray1OfInteger) AIS_Triangulation::GetColors() const
 
 //=======================================================================
 //function : SetTriangulation
-//purpose  : 
+//purpose  :
 //=======================================================================
 void AIS_Triangulation::SetTriangulation(const Handle(Poly_Triangulation)& aTriangulation)
 {
@@ -172,7 +239,7 @@ void AIS_Triangulation::SetTriangulation(const Handle(Poly_Triangulation)& aTria
 
 //=======================================================================
 //function : GetTriangulation
-//purpose  : 
+//purpose  :
 //=======================================================================
 Handle(Poly_Triangulation) AIS_Triangulation::GetTriangulation() const{
   return myTriangulation;
@@ -180,42 +247,19 @@ Handle(Poly_Triangulation) AIS_Triangulation::GetTriangulation() const{
 
 //=======================================================================
 //function : AttenuateColor
-//purpose  : Attenuates 32-bit color by a given attenuation factor (0...1):
-//           aColor = Alpha << 24 + Blue << 16 + Green << 8 + Red
-//           All color components are multiplied by aComponent, the result is then packed again as 32-bit integer.
-//           Color attenuation is applied to the vertex colors in order to have correct visual result 
-//           after glColorMaterial(GL_AMBIENT_AND_DIFFUSE). Without it, colors look unnatural and flat.
+//purpose  :
 //=======================================================================
-
-Standard_Integer AIS_Triangulation::AttenuateColor( const Standard_Integer aColor,
-                                                    const Standard_Real aComposition)
+Graphic3d_Vec4ub AIS_Triangulation::attenuateColor (const Standard_Integer theColor,
+                                                    const Standard_Real    theComposition)
 {
-  Standard_Integer  red,
-                    green,
-                    blue,
-                    alpha;
+  const Standard_Byte* anRgbx = reinterpret_cast<const Standard_Byte*> (&theColor);
 
-  alpha = aColor&0xff000000;
-  alpha >>= 24;
+  // If IsTranparent() is false alpha value will be ignored anyway.
+  Standard_Byte anAlpha = IsTransparent() ? static_cast<Standard_Byte> (255.0 - myDrawer->ShadingAspect()->Aspect()->FrontMaterial().Transparency() * 255.0)
+                                          : 255;
 
-  blue = aColor&0x00ff0000;
-  blue >>= 16;
-
-  green = aColor&0x0000ff00;
-  green >>= 8;
-
-  red = aColor&0x000000ff;
-  red >>= 0; 
-
-  red   = (Standard_Integer)(aComposition * red);
-  green = (Standard_Integer)(aComposition * green);
-  blue  = (Standard_Integer)(aComposition * blue);
-
-  Standard_Integer  color;
-  color = red;
-  color += green << 8;
-  color += blue  << 16; 
-  color += alpha << 24;
-  return color;
+  return Graphic3d_Vec4ub ((Standard_Byte)(theComposition * anRgbx[0]),
+                           (Standard_Byte)(theComposition * anRgbx[1]),
+                           (Standard_Byte)(theComposition * anRgbx[2]),
+                           anAlpha);
 }
-