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_Drawer.hxx>
15 #include <AIS_Triangulation.hxx>
16 #include <AIS_InteractiveObject.hxx>
17 #include <Standard_DefineHandle.hxx>
18 #include <Poly_Array1OfTriangle.hxx>
19 #include <Poly_Triangulation.hxx>
20 #include <Prs3d_Root.hxx>
21 #include <Prs3d_ShadingAspect.hxx>
22 #include <TShort_Array1OfShortReal.hxx>
23 #include <TColgp_Array1OfPnt.hxx>
24 #include <TColStd_HArray1OfInteger.hxx>
25 #include <TShort_HArray1OfShortReal.hxx>
26 #include <Graphic3d_Group.hxx>
27 #include <Graphic3d_AspectFillArea3d.hxx>
28 #include <Graphic3d_ArrayOfTriangles.hxx>
31 IMPLEMENT_STANDARD_HANDLE(AIS_Triangulation, AIS_InteractiveObject)
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 //=======================================================================
45 //=======================================================================
46 void AIS_Triangulation::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
47 const Handle(Prs3d_Presentation)& aPresentation,
48 const Standard_Integer aMode)
53 const TColgp_Array1OfPnt& nodes = myTriangulation->Nodes(); //Nodes
54 const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles(); //Triangle
56 Standard_Boolean hasVNormals = myTriangulation->HasNormals();
57 Standard_Boolean hasVColors = (myFlagColor == 1);
59 Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (myNbNodes, myNbTriangles * 3,
60 hasVNormals, hasVColors, Standard_False);
61 Handle(Graphic3d_Group) TheGroup = Prs3d_Root::CurrentGroup(aPresentation);
62 Handle(Graphic3d_AspectFillArea3d) aspect = myDrawer->ShadingAspect()->Aspect();
67 Standard_Real ambient = aspect->FrontMaterial().Ambient();
70 const TShort_Array1OfShortReal& normals = myTriangulation->Normals();
73 const TColStd_Array1OfInteger& colors = myColor->Array1();
74 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
76 j = (i - nodes.Lower()) * 3;
77 anArray->AddVertex(nodes(i), AttenuateColor(colors(i), ambient));
78 anArray->SetVertexNormal(i, normals(j+1), normals(j+2), normals(j+3));
83 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
85 j = (i - nodes.Lower()) * 3;
86 anArray->AddVertex(nodes(i));
87 anArray->SetVertexNormal(i, normals(j+1), normals(j+2), normals(j+3));
95 const TColStd_Array1OfInteger& colors = myColor->Array1();
96 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
98 anArray->AddVertex(nodes(i), AttenuateColor(colors(i), ambient));
103 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
105 anArray->AddVertex(nodes(i));
110 Standard_Integer indexTriangle[3] = {0,0,0};
111 for ( i = triangles.Lower(); i<= triangles.Upper(); i++ ) {
112 triangles(i).Get(indexTriangle[0], indexTriangle[1], indexTriangle[2]);
113 anArray->AddEdge(indexTriangle[0]);
114 anArray->AddEdge(indexTriangle[1]);
115 anArray->AddEdge(indexTriangle[2]);
117 TheGroup->SetPrimitivesAspect(aspect);
118 TheGroup->AddPrimitiveArray(anArray);
123 //=======================================================================
124 //function : ComputeSelection
126 //=======================================================================
127 void AIS_Triangulation::ComputeSelection(const Handle(SelectMgr_Selection)& /*aSelection*/,
128 const Standard_Integer /*aMode*/)
133 //=======================================================================
134 //function : SetColor
135 //purpose : Set the color for each node.
136 // Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red
137 // Order of color components is essential for further usage by OpenGL
138 //=======================================================================
139 void AIS_Triangulation::SetColors(const Handle(TColStd_HArray1OfInteger)& aColor)
145 //=======================================================================
146 //function : GetColor
147 //purpose : Get the color for each node.
148 // Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red
149 // Order of color components is essential for further usage by OpenGL
150 //=======================================================================
152 Handle(TColStd_HArray1OfInteger) AIS_Triangulation::GetColors() const
158 //=======================================================================
159 //function : SetTriangulation
161 //=======================================================================
162 void AIS_Triangulation::SetTriangulation(const Handle(Poly_Triangulation)& aTriangulation)
164 myTriangulation = aTriangulation;
167 //=======================================================================
168 //function : GetTriangulation
170 //=======================================================================
171 Handle(Poly_Triangulation) AIS_Triangulation::GetTriangulation() const{
172 return myTriangulation;
175 //=======================================================================
176 //function : AttenuateColor
177 //purpose : Attenuates 32-bit color by a given attenuation factor (0...1):
178 // aColor = Alpha << 24 + Blue << 16 + Green << 8 + Red
179 // All color components are multiplied by aComponent, the result is then packed again as 32-bit integer.
180 // Color attenuation is applied to the vertex colors in order to have correct visual result
181 // after glColorMaterial(GL_AMBIENT_AND_DIFFUSE). Without it, colors look unnatural and flat.
182 //=======================================================================
184 Standard_Integer AIS_Triangulation::AttenuateColor( const Standard_Integer aColor,
185 const Standard_Real aComposition)
187 Standard_Integer red,
192 alpha = aColor&0xff000000;
195 blue = aColor&0x00ff0000;
198 green = aColor&0x0000ff00;
201 red = aColor&0x000000ff;
204 red = (Standard_Integer)(aComposition * red);
205 green = (Standard_Integer)(aComposition * green);
206 blue = (Standard_Integer)(aComposition * blue);
208 Standard_Integer color;
212 color += alpha << 24;