0024947: Redesign OCCT legacy type system -- automatic
[occt.git] / src / AIS / AIS_Triangulation.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 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
973c2be1 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.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
7fd59977 14#include <AIS_Triangulation.hxx>
15#include <AIS_InteractiveObject.hxx>
ec357c5c 16#include <Standard_Type.hxx>
7fd59977 17#include <Poly_Array1OfTriangle.hxx>
18#include <Poly_Triangulation.hxx>
6262338c 19#include <Prs3d_Drawer.hxx>
7fd59977 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>
7fd59977 29
30
7fd59977 31AIS_Triangulation::AIS_Triangulation(const Handle(Poly_Triangulation)& Triangulation)
32{
33 myTriangulation = Triangulation;
34 myNbNodes = Triangulation->NbNodes();
35 myNbTriangles = Triangulation->NbTriangles();
36 myFlagColor = 0;
37}
38
39//=======================================================================
40//function : Compute
41//purpose :
42//=======================================================================
35e08fe8 43void AIS_Triangulation::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
7fd59977 44 const Handle(Prs3d_Presentation)& aPresentation,
45 const Standard_Integer aMode)
46{
47 switch (aMode)
48 {
49 case 0:
50 const TColgp_Array1OfPnt& nodes = myTriangulation->Nodes(); //Nodes
51 const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles(); //Triangle
7fd59977 52
16e65a03 53 Standard_Boolean hasVNormals = myTriangulation->HasNormals();
54 Standard_Boolean hasVColors = (myFlagColor == 1);
7fd59977 55
871fa103 56 Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (myNbNodes, myNbTriangles * 3,
57 hasVNormals, hasVColors, Standard_False);
7fd59977 58 Handle(Graphic3d_Group) TheGroup = Prs3d_Root::CurrentGroup(aPresentation);
59 Handle(Graphic3d_AspectFillArea3d) aspect = myDrawer->ShadingAspect()->Aspect();
60
61 Standard_Integer i;
62 Standard_Integer j;
63
64 Standard_Real ambient = aspect->FrontMaterial().Ambient();
16e65a03 65 if (hasVNormals)
66 {
67 const TShort_Array1OfShortReal& normals = myTriangulation->Normals();
68 if (hasVColors)
69 {
70 const TColStd_Array1OfInteger& colors = myColor->Array1();
71 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
72 {
73 j = (i - nodes.Lower()) * 3;
74 anArray->AddVertex(nodes(i), AttenuateColor(colors(i), ambient));
75 anArray->SetVertexNormal(i, normals(j+1), normals(j+2), normals(j+3));
76 }
77 }
78 else // !hasVColors
79 {
80 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
81 {
82 j = (i - nodes.Lower()) * 3;
83 anArray->AddVertex(nodes(i));
84 anArray->SetVertexNormal(i, normals(j+1), normals(j+2), normals(j+3));
85 }
86 }
87 }
88 else // !hasVNormals
89 {
90 if (hasVColors)
91 {
92 const TColStd_Array1OfInteger& colors = myColor->Array1();
93 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
94 {
95 anArray->AddVertex(nodes(i), AttenuateColor(colors(i), ambient));
96 }
97 }
98 else // !hasVColors
99 {
100 for ( i = nodes.Lower(); i <= nodes.Upper(); i++ )
101 {
102 anArray->AddVertex(nodes(i));
103 }
104 }
7fd59977 105 }
106
107 Standard_Integer indexTriangle[3] = {0,0,0};
108 for ( i = triangles.Lower(); i<= triangles.Upper(); i++ ) {
109 triangles(i).Get(indexTriangle[0], indexTriangle[1], indexTriangle[2]);
b8ddfc2f 110 anArray->AddEdge(indexTriangle[0]);
111 anArray->AddEdge(indexTriangle[1]);
112 anArray->AddEdge(indexTriangle[2]);
7fd59977 113 }
114 TheGroup->SetPrimitivesAspect(aspect);
b8ddfc2f 115 TheGroup->AddPrimitiveArray(anArray);
7fd59977 116 break;
117 }
118}
119
120//=======================================================================
121//function : ComputeSelection
122//purpose :
123//=======================================================================
35e08fe8 124void AIS_Triangulation::ComputeSelection(const Handle(SelectMgr_Selection)& /*aSelection*/,
7fd59977 125 const Standard_Integer /*aMode*/)
126{
127
128}
129
130//=======================================================================
131//function : SetColor
132//purpose : Set the color for each node.
133// Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red
134// Order of color components is essential for further usage by OpenGL
135//=======================================================================
136void AIS_Triangulation::SetColors(const Handle(TColStd_HArray1OfInteger)& aColor)
137{
138 myFlagColor = 1;
139 myColor = aColor;
140}
141
142//=======================================================================
143//function : GetColor
144//purpose : Get the color for each node.
145// Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red
146// Order of color components is essential for further usage by OpenGL
147//=======================================================================
148
149Handle(TColStd_HArray1OfInteger) AIS_Triangulation::GetColors() const
150{
151 return myColor;
152}
153
154
155//=======================================================================
156//function : SetTriangulation
157//purpose :
158//=======================================================================
159void AIS_Triangulation::SetTriangulation(const Handle(Poly_Triangulation)& aTriangulation)
160{
161 myTriangulation = aTriangulation;
162}
163
164//=======================================================================
165//function : GetTriangulation
166//purpose :
167//=======================================================================
168Handle(Poly_Triangulation) AIS_Triangulation::GetTriangulation() const{
169 return myTriangulation;
170}
171
172//=======================================================================
173//function : AttenuateColor
174//purpose : Attenuates 32-bit color by a given attenuation factor (0...1):
175// aColor = Alpha << 24 + Blue << 16 + Green << 8 + Red
176// All color components are multiplied by aComponent, the result is then packed again as 32-bit integer.
177// Color attenuation is applied to the vertex colors in order to have correct visual result
178// after glColorMaterial(GL_AMBIENT_AND_DIFFUSE). Without it, colors look unnatural and flat.
179//=======================================================================
180
181Standard_Integer AIS_Triangulation::AttenuateColor( const Standard_Integer aColor,
182 const Standard_Real aComposition)
183{
184 Standard_Integer red,
185 green,
186 blue,
187 alpha;
188
189 alpha = aColor&0xff000000;
190 alpha >>= 24;
191
192 blue = aColor&0x00ff0000;
193 blue >>= 16;
194
195 green = aColor&0x0000ff00;
196 green >>= 8;
197
198 red = aColor&0x000000ff;
199 red >>= 0;
200
201 red = (Standard_Integer)(aComposition * red);
202 green = (Standard_Integer)(aComposition * green);
203 blue = (Standard_Integer)(aComposition * blue);
204
205 Standard_Integer color;
206 color = red;
207 color += green << 8;
208 color += blue << 16;
209 color += alpha << 24;
210 return color;
211}
212