From dcc17419215219d337c85b3bc852b585c7a1d85b Mon Sep 17 00:00:00 2001 From: isk Date: Thu, 30 Jun 2016 15:25:43 +0300 Subject: [PATCH] 0027655: Visualization - AIS_Triangulation disappears after setting non-zero transparency Add a new methods in AIS_Triangulation: SetTransparency, UnsetTransparency, HasVertexColor, updatePresentation. Add a new methods in Graphic3d_ArrayOfPrimitives: AddVertex(gp_Pnt, Graphic3d_Vec4ub) and SetVertexColor(Standard_Integer, Graphic3d_Vec4ub). Type of color of array in AIS_Triangulation is Graphic3d_Vec4ub now. --- src/AIS/AIS_Triangulation.cxx | 135 ++++++++++++++---- src/AIS/AIS_Triangulation.hxx | 14 +- src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx | 11 ++ src/Graphic3d/Graphic3d_ArrayOfPrimitives.lxx | 14 ++ src/ViewerTest/ViewerTest_ObjectCommands.cxx | 11 +- tests/bugs/vis/bug27655 | 19 +++ 6 files changed, 163 insertions(+), 41 deletions(-) create mode 100644 tests/bugs/vis/bug27655 diff --git a/src/AIS/AIS_Triangulation.cxx b/src/AIS/AIS_Triangulation.cxx index f0656b6074..189518fcc0 100644 --- a/src/AIS/AIS_Triangulation.cxx +++ b/src/AIS/AIS_Triangulation.cxx @@ -11,6 +11,7 @@ // Alternatively, this file may be used under the terms of Open CASCADE // commercial license or contractual agreement. +#include #include #include #include @@ -38,6 +39,97 @@ 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); + myTransparency = theValue; + + updatePresentation(); +} + +//======================================================================= +//function : UnsetTransparency +//purpose : +//======================================================================= +void AIS_Triangulation::UnsetTransparency() +{ + myTransparency = 0.0; + 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 (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt) + { + const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt); + if (aPrsModed.Mode() != AIS_WireFrame) + { + continue; + } + + const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation(); + + if (IsTransparent()) + { + aPrs->SetDisplayPriority (10); // force highest priority for translucent objects + } + + for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next()) + { + const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value(); + if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA)) + { + aGroup->SetGroupPrimitivesAspect (anAreaAsp); + } + } + + if (!IsTransparent()) + { + aPrs->ResetDisplayPriority(); + } + } + + myRecomputeEveryPrs = Standard_False; // no mode to recalculate - only viewer update + myToRecomputeModes.Clear(); + } +} + //======================================================================= //function : Compute //purpose : @@ -53,7 +145,7 @@ void AIS_Triangulation::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aP const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles(); //Triangle Standard_Boolean hasVNormals = myTriangulation->HasNormals(); - Standard_Boolean hasVColors = (myFlagColor == 1); + Standard_Boolean hasVColors = HasVertexColors(); Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (myNbNodes, myNbTriangles * 3, hasVNormals, hasVColors, Standard_False); @@ -73,7 +165,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)); } } @@ -94,7 +186,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 @@ -180,35 +272,18 @@ Handle(Poly_Triangulation) AIS_Triangulation::GetTriangulation() const{ // after glColorMaterial(GL_AMBIENT_AND_DIFFUSE). Without it, colors look unnatural and flat. //======================================================================= -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; - alpha = aColor&0xff000000; - alpha >>= 24; + const Graphic3d_Vec4ub& aColor = *reinterpret_cast (&theColor); + // If IsTranparent() is false alpha value will be ignored anyway. + Standard_Byte anAlpha = IsTransparent() ? static_cast (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 * aColor.r()), + (Standard_Byte)(theComposition * aColor.g()), + (Standard_Byte)(theComposition * aColor.b()), + anAlpha); } diff --git a/src/AIS/AIS_Triangulation.hxx b/src/AIS/AIS_Triangulation.hxx index 2f86b21368..d602e92480 100644 --- a/src/AIS/AIS_Triangulation.hxx +++ b/src/AIS/AIS_Triangulation.hxx @@ -55,19 +55,29 @@ public: //! Get the color for each node. //! Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red Standard_EXPORT Handle(TColStd_HArray1OfInteger) GetColors() const; + + //! Returns true if triangulation has vertex colors. + Standard_Boolean HasVertexColors() const + { + return (myFlagColor == 1); + } Standard_EXPORT void SetTriangulation (const Handle(Poly_Triangulation)& aTriangulation); //! Returns Poly_Triangulation . Standard_EXPORT Handle(Poly_Triangulation) GetTriangulation() const; + //! Sets the value aValue for transparency in the reconstructed compound shape. + Standard_EXPORT virtual void SetTransparency (const Standard_Real aValue = 0.6) Standard_OVERRIDE; - + //! Removes the setting for transparency in the reconstructed compound shape. + Standard_EXPORT virtual void UnsetTransparency() Standard_OVERRIDE; DEFINE_STANDARD_RTTIEXT(AIS_Triangulation,AIS_InteractiveObject) protected: + Standard_EXPORT void updatePresentation(); @@ -83,7 +93,7 @@ private: //! 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. - Standard_EXPORT Standard_Integer AttenuateColor (const Standard_Integer aColor, const Standard_Real aComponent); + Standard_EXPORT Graphic3d_Vec4ub attenuateColor (const Standard_Integer theColor, const Standard_Real theComponent); Handle(Poly_Triangulation) myTriangulation; Handle(TColStd_HArray1OfInteger) myColor; diff --git a/src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx b/src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx index ac40dabf45..271f4cdf86 100644 --- a/src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx +++ b/src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx @@ -97,6 +97,13 @@ Standard_Integer AddVertex (const Graphic3d_Vec3& theVertex); //! On all architecture proccers type (x86 or SPARC) you can //! use this byte order. Standard_EXPORT Standard_Integer AddVertex (const gp_Pnt& aVertice, const Standard_Integer aColor); + + //! Adds a vertice and vertex color in the vertex array. + //! returns the actual vertex number. + //! Warning: is ignored when the + //! constructor parameter is FALSE + Standard_Integer AddVertex (const gp_Pnt& theVertex, + const Graphic3d_Vec4ub& theColor); //! Adds a vertice and vertex normal in the vertex array. //! returns the actual vertex number. @@ -210,6 +217,10 @@ Standard_Integer AddVertex (const Graphic3d_Vec3& theVertex); //! Change the vertex color of rank in the array. void SetVertexColor (const Standard_Integer anIndex, const Standard_Real R, const Standard_Real G, const Standard_Real B); + + //! Change the vertex color of rank in the array. + void SetVertexColor (const Standard_Integer theIndex, + const Graphic3d_Vec4ub& theColor); //! Change the vertex color of rank in the array. //! aColor = Alpha << 24 + Blue << 16 + Green << 8 + Red diff --git a/src/Graphic3d/Graphic3d_ArrayOfPrimitives.lxx b/src/Graphic3d/Graphic3d_ArrayOfPrimitives.lxx index 79e140c2ef..54af290516 100644 --- a/src/Graphic3d/Graphic3d_ArrayOfPrimitives.lxx +++ b/src/Graphic3d/Graphic3d_ArrayOfPrimitives.lxx @@ -68,6 +68,14 @@ inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const Graphic3d_ return AddVertex (theVertex.x(), theVertex.y(), theVertex.z()); } +inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const gp_Pnt& theVertex, + const Graphic3d_Vec4ub& theColor) +{ + const Standard_Integer anIndex = AddVertex (theVertex); + SetVertexColor (anIndex, theColor); + return anIndex; +} + inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ) @@ -173,6 +181,12 @@ inline void Graphic3d_ArrayOfPrimitives::SetVertexColor (const Standard_Integer myAttribs->NbElements = Max (theIndex, myAttribs->NbElements); } +inline void Graphic3d_ArrayOfPrimitives::SetVertexColor (const Standard_Integer theIndex, + const Graphic3d_Vec4ub& theColor) +{ + SetVertexColor (theIndex, *reinterpret_cast (&theColor)); +} + inline void Graphic3d_ArrayOfPrimitives::SetVertexNormal (const Standard_Integer theIndex, const Standard_Real theNX, const Standard_Real theNY, diff --git a/src/ViewerTest/ViewerTest_ObjectCommands.cxx b/src/ViewerTest/ViewerTest_ObjectCommands.cxx index 0aeef71757..b88713308c 100644 --- a/src/ViewerTest/ViewerTest_ObjectCommands.cxx +++ b/src/ViewerTest/ViewerTest_ObjectCommands.cxx @@ -3167,20 +3167,13 @@ static int VDrawSphere (Draw_Interpretor& /*di*/, Standard_Integer argc, const c // stupid initialization of Green color in RGBA space as integer // probably wrong for big-endian CPUs - Standard_Integer aRed = 0; - Standard_Integer aGreen = 255; - Standard_Integer aBlue = 0; - Standard_Integer anAlpha = 0; // not used - Standard_Integer aColorInt = aRed; - aColorInt += aGreen << 8; - aColorInt += aBlue << 16; - aColorInt += anAlpha << 24; + const Graphic3d_Vec4ub aColor (0, 255, 0, 0); // setup colors array per vertex Handle(TColStd_HArray1OfInteger) aColorArray = new TColStd_HArray1OfInteger (1, aNumberPoints); for (Standard_Integer aNodeId = 1; aNodeId <= aNumberPoints; ++aNodeId) { - aColorArray->SetValue (aNodeId, aColorInt); + aColorArray->SetValue (aNodeId, *reinterpret_cast (&aColor)); } aShape->SetColors (aColorArray); diff --git a/tests/bugs/vis/bug27655 b/tests/bugs/vis/bug27655 new file mode 100644 index 0000000000..d6eddc0aef --- /dev/null +++ b/tests/bugs/vis/bug27655 @@ -0,0 +1,19 @@ +puts "========" +puts "CR27655" +puts "========" +puts "" +############################################################################################ +puts "Visualization - AIS_Triangulation don't disappears after setting non-zero transparency" +############################################################################################ + +vclear +vinit View1 + +box b 10 10 10 +vdisplay b -dispMode 1 +vdrawsphere s 100 +vsettransparency s 0.7 + +vfit + +vdump $imagedir/${casename}_0.png