1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <AIS_DisplayMode.hxx>
15 #include <AIS_Triangulation.hxx>
16 #include <AIS_InteractiveObject.hxx>
17 #include <Standard_Type.hxx>
18 #include <Poly_Array1OfTriangle.hxx>
19 #include <Poly_Triangulation.hxx>
20 #include <Prs3d_Drawer.hxx>
21 #include <Prs3d_Root.hxx>
22 #include <Prs3d_ShadingAspect.hxx>
23 #include <TShort_Array1OfShortReal.hxx>
24 #include <TColgp_Array1OfPnt.hxx>
25 #include <TColStd_HArray1OfInteger.hxx>
26 #include <TShort_HArray1OfShortReal.hxx>
27 #include <Graphic3d_Group.hxx>
28 #include <Graphic3d_AspectFillArea3d.hxx>
29 #include <Graphic3d_ArrayOfTriangles.hxx>
32 IMPLEMENT_STANDARD_RTTIEXT(AIS_Triangulation,AIS_InteractiveObject)
34 AIS_Triangulation::AIS_Triangulation(const Handle(Poly_Triangulation)& Triangulation)
36 myTriangulation = Triangulation;
37 myNbNodes = Triangulation->NbNodes();
38 myNbTriangles = Triangulation->NbTriangles();
42 //=======================================================================
43 //function : SetTransparency
45 //=======================================================================
46 void AIS_Triangulation::SetTransparency (const Standard_Real theValue)
48 if (!myDrawer->HasOwnShadingAspect())
50 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
51 if (myDrawer->HasLink())
53 *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect();
57 // override transparency
58 myDrawer->ShadingAspect()->SetTransparency (theValue, myCurrentFacingModel);
59 myDrawer->SetTransparency ((Standard_ShortReal )theValue);
64 //=======================================================================
65 //function : UnsetTransparency
67 //=======================================================================
68 void AIS_Triangulation::UnsetTransparency()
70 myDrawer->SetTransparency (0.0f);
71 if (!myDrawer->HasOwnShadingAspect())
75 else if (HasColor() || HasMaterial())
77 myDrawer->ShadingAspect()->SetTransparency (0.0, myCurrentFacingModel);
83 //=======================================================================
84 //function : updatePresentation
86 //=======================================================================
87 void AIS_Triangulation::updatePresentation()
89 if (HasVertexColors())
91 SetToUpdate (AIS_WireFrame);
95 // modify shading presentation without re-computation
96 const PrsMgr_Presentations& aPrsList = Presentations();
97 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->ShadingAspect()->Aspect();
98 for (Standard_Integer aPrsIt = 1; aPrsIt <= aPrsList.Length(); ++aPrsIt)
100 const PrsMgr_ModedPresentation& aPrsModed = aPrsList.Value (aPrsIt);
101 if (aPrsModed.Mode() != AIS_WireFrame)
106 const Handle(Prs3d_Presentation)& aPrs = aPrsModed.Presentation()->Presentation();
110 aPrs->SetDisplayPriority (10); // force highest priority for translucent objects
113 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next())
115 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
116 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
118 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
122 if (!IsTransparent())
124 aPrs->ResetDisplayPriority();
128 myRecomputeEveryPrs = Standard_False; // no mode to recalculate - only viewer update
129 myToRecomputeModes.Clear();
133 //=======================================================================
136 //=======================================================================
137 void AIS_Triangulation::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
138 const Handle(Prs3d_Presentation)& aPresentation,
139 const Standard_Integer aMode)
144 const TColgp_Array1OfPnt& nodes = myTriangulation->Nodes(); //Nodes
145 const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles(); //Triangle
147 Standard_Boolean hasVNormals = myTriangulation->HasNormals();
148 Standard_Boolean hasVColors = HasVertexColors();
150 Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (myNbNodes, myNbTriangles * 3,
151 hasVNormals, hasVColors, Standard_False);
152 Handle(Graphic3d_Group) TheGroup = Prs3d_Root::CurrentGroup(aPresentation);
153 Handle(Graphic3d_AspectFillArea3d) aspect = myDrawer->ShadingAspect()->Aspect();
158 Standard_Real ambient = aspect->FrontMaterial().Ambient();
161 const TShort_Array1OfShortReal& normals = myTriangulation->Normals();
164 const TColStd_Array1OfInteger& colors = myColor->Array1();
165 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
167 j = (i - nodes.Lower()) * 3;
168 anArray->AddVertex(nodes(i), attenuateColor(colors(i), ambient));
169 anArray->SetVertexNormal(i, normals(j+1), normals(j+2), normals(j+3));
174 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
176 j = (i - nodes.Lower()) * 3;
177 anArray->AddVertex(nodes(i));
178 anArray->SetVertexNormal(i, normals(j+1), normals(j+2), normals(j+3));
186 const TColStd_Array1OfInteger& colors = myColor->Array1();
187 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
189 anArray->AddVertex(nodes(i), attenuateColor(colors(i), ambient));
194 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
196 anArray->AddVertex(nodes(i));
201 Standard_Integer indexTriangle[3] = {0,0,0};
202 for ( i = triangles.Lower(); i<= triangles.Upper(); i++ ) {
203 triangles(i).Get(indexTriangle[0], indexTriangle[1], indexTriangle[2]);
204 anArray->AddEdge(indexTriangle[0]);
205 anArray->AddEdge(indexTriangle[1]);
206 anArray->AddEdge(indexTriangle[2]);
208 TheGroup->SetPrimitivesAspect(aspect);
209 TheGroup->AddPrimitiveArray(anArray);
214 //=======================================================================
215 //function : ComputeSelection
217 //=======================================================================
218 void AIS_Triangulation::ComputeSelection(const Handle(SelectMgr_Selection)& /*aSelection*/,
219 const Standard_Integer /*aMode*/)
224 //=======================================================================
225 //function : SetColor
226 //purpose : Set the color for each node.
227 // Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red
228 // Order of color components is essential for further usage by OpenGL
229 //=======================================================================
230 void AIS_Triangulation::SetColors(const Handle(TColStd_HArray1OfInteger)& aColor)
236 //=======================================================================
237 //function : GetColor
238 //purpose : Get the color for each node.
239 // Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red
240 // Order of color components is essential for further usage by OpenGL
241 //=======================================================================
243 Handle(TColStd_HArray1OfInteger) AIS_Triangulation::GetColors() const
249 //=======================================================================
250 //function : SetTriangulation
252 //=======================================================================
253 void AIS_Triangulation::SetTriangulation(const Handle(Poly_Triangulation)& aTriangulation)
255 myTriangulation = aTriangulation;
258 //=======================================================================
259 //function : GetTriangulation
261 //=======================================================================
262 Handle(Poly_Triangulation) AIS_Triangulation::GetTriangulation() const{
263 return myTriangulation;
266 //=======================================================================
267 //function : AttenuateColor
268 //purpose : Attenuates 32-bit color by a given attenuation factor (0...1):
269 // aColor = Alpha << 24 + Blue << 16 + Green << 8 + Red
270 // All color components are multiplied by aComponent, the result is then packed again as 32-bit integer.
271 // Color attenuation is applied to the vertex colors in order to have correct visual result
272 // after glColorMaterial(GL_AMBIENT_AND_DIFFUSE). Without it, colors look unnatural and flat.
273 //=======================================================================
275 Graphic3d_Vec4ub AIS_Triangulation::attenuateColor (const Standard_Integer theColor,
276 const Standard_Real theComposition)
279 const Graphic3d_Vec4ub& aColor = *reinterpret_cast<const Graphic3d_Vec4ub*> (&theColor);
280 // If IsTranparent() is false alpha value will be ignored anyway.
281 Standard_Byte anAlpha = IsTransparent() ? static_cast<Standard_Byte> (255.0 - myDrawer->ShadingAspect()->Aspect()->FrontMaterial().Transparency() * 255.0)
284 return Graphic3d_Vec4ub ((Standard_Byte)(theComposition * aColor.r()),
285 (Standard_Byte)(theComposition * aColor.g()),
286 (Standard_Byte)(theComposition * aColor.b()),