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