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> |
d6c48921 |
15 | |
16 | #include <AIS_DisplayMode.hxx> |
7fd59977 |
17 | #include <AIS_InteractiveObject.hxx> |
ec357c5c |
18 | #include <Standard_Type.hxx> |
7fd59977 |
19 | #include <Poly_Triangulation.hxx> |
6262338c |
20 | #include <Prs3d_Drawer.hxx> |
7fd59977 |
21 | #include <Prs3d_ShadingAspect.hxx> |
7fd59977 |
22 | #include <TColStd_HArray1OfInteger.hxx> |
7fd59977 |
23 | #include <Graphic3d_Group.hxx> |
24 | #include <Graphic3d_AspectFillArea3d.hxx> |
25 | #include <Graphic3d_ArrayOfTriangles.hxx> |
7fd59977 |
26 | |
27 | |
92efcf78 |
28 | IMPLEMENT_STANDARD_RTTIEXT(AIS_Triangulation,AIS_InteractiveObject) |
29 | |
7fd59977 |
30 | AIS_Triangulation::AIS_Triangulation(const Handle(Poly_Triangulation)& Triangulation) |
31 | { |
32 | myTriangulation = Triangulation; |
33 | myNbNodes = Triangulation->NbNodes(); |
34 | myNbTriangles = Triangulation->NbTriangles(); |
35 | myFlagColor = 0; |
36 | } |
37 | |
dcc17419 |
38 | //======================================================================= |
39 | //function : SetTransparency |
40 | //purpose : |
41 | //======================================================================= |
42 | void AIS_Triangulation::SetTransparency (const Standard_Real theValue) |
43 | { |
44 | if (!myDrawer->HasOwnShadingAspect()) |
45 | { |
46 | myDrawer->SetShadingAspect (new Prs3d_ShadingAspect()); |
47 | if (myDrawer->HasLink()) |
48 | { |
49 | *myDrawer->ShadingAspect()->Aspect() = *myDrawer->Link()->ShadingAspect()->Aspect(); |
50 | } |
51 | } |
52 | |
53 | // override transparency |
54 | myDrawer->ShadingAspect()->SetTransparency (theValue, myCurrentFacingModel); |
f838dac4 |
55 | myDrawer->SetTransparency ((Standard_ShortReal )theValue); |
dcc17419 |
56 | |
57 | updatePresentation(); |
58 | } |
59 | |
60 | //======================================================================= |
61 | //function : UnsetTransparency |
62 | //purpose : |
63 | //======================================================================= |
64 | void AIS_Triangulation::UnsetTransparency() |
65 | { |
f838dac4 |
66 | myDrawer->SetTransparency (0.0f); |
dcc17419 |
67 | if (!myDrawer->HasOwnShadingAspect()) |
68 | { |
69 | return; |
70 | } |
71 | else if (HasColor() || HasMaterial()) |
72 | { |
73 | myDrawer->ShadingAspect()->SetTransparency (0.0, myCurrentFacingModel); |
74 | } |
75 | |
76 | updatePresentation(); |
77 | } |
78 | |
79 | //======================================================================= |
80 | //function : updatePresentation |
81 | //purpose : |
82 | //======================================================================= |
83 | void AIS_Triangulation::updatePresentation() |
84 | { |
85 | if (HasVertexColors()) |
86 | { |
87 | SetToUpdate (AIS_WireFrame); |
88 | } |
89 | else |
90 | { |
91 | // modify shading presentation without re-computation |
92 | const PrsMgr_Presentations& aPrsList = Presentations(); |
93 | Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->ShadingAspect()->Aspect(); |
7dd7c146 |
94 | for (PrsMgr_Presentations::Iterator aPrsIter (aPrsList); aPrsIter.More(); aPrsIter.Next()) |
dcc17419 |
95 | { |
7dd7c146 |
96 | if (aPrsIter.Value()->Mode() != AIS_WireFrame) |
dcc17419 |
97 | { |
98 | continue; |
99 | } |
100 | |
7dd7c146 |
101 | const Handle(Prs3d_Presentation)& aPrs = aPrsIter.Value(); |
dcc17419 |
102 | for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (aPrs->Groups()); aGroupIt.More(); aGroupIt.Next()) |
103 | { |
104 | const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value(); |
bf5f0ca2 |
105 | aGroup->SetGroupPrimitivesAspect (anAreaAsp); |
dcc17419 |
106 | } |
dcc17419 |
107 | } |
dcc17419 |
108 | } |
109 | } |
110 | |
7fd59977 |
111 | //======================================================================= |
112 | //function : Compute |
113 | //purpose : |
114 | //======================================================================= |
decbff0d |
115 | void AIS_Triangulation::Compute (const Handle(PrsMgr_PresentationManager)& , |
116 | const Handle(Prs3d_Presentation)& thePrs, |
117 | const Standard_Integer theMode) |
7fd59977 |
118 | { |
decbff0d |
119 | if (theMode != 0) |
7fd59977 |
120 | { |
decbff0d |
121 | return; |
122 | } |
7fd59977 |
123 | |
decbff0d |
124 | Standard_Boolean hasVNormals = myTriangulation->HasNormals(); |
125 | Standard_Boolean hasVColors = HasVertexColors(); |
7fd59977 |
126 | |
decbff0d |
127 | Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (myNbNodes, myNbTriangles * 3, |
128 | hasVNormals, hasVColors, Standard_False); |
129 | Handle(Graphic3d_Group) aGroup = thePrs->CurrentGroup(); |
130 | Handle(Graphic3d_AspectFillArea3d) anAspect = myDrawer->ShadingAspect()->Aspect(); |
7fd59977 |
131 | |
61a05a36 |
132 | const Standard_Real anAmbient = 1.0; |
decbff0d |
133 | if (hasVNormals) |
134 | { |
135 | gp_Vec3f aNormal; |
136 | if (hasVColors) |
137 | { |
138 | const TColStd_Array1OfInteger& colors = myColor->Array1(); |
139 | for (Standard_Integer aNodeIter = 1; aNodeIter <= myTriangulation->NbNodes(); ++aNodeIter) |
16e65a03 |
140 | { |
decbff0d |
141 | anArray->AddVertex (myTriangulation->Node (aNodeIter), attenuateColor (colors[aNodeIter], anAmbient)); |
142 | myTriangulation->Normal (aNodeIter, aNormal); |
143 | anArray->SetVertexNormal (aNodeIter, aNormal.x(), aNormal.y(), aNormal.z()); |
16e65a03 |
144 | } |
decbff0d |
145 | } |
146 | else // !hasVColors |
147 | { |
148 | for (Standard_Integer aNodeIter = 1; aNodeIter <= myTriangulation->NbNodes(); ++aNodeIter) |
16e65a03 |
149 | { |
decbff0d |
150 | anArray->AddVertex (myTriangulation->Node (aNodeIter)); |
151 | myTriangulation->Normal (aNodeIter, aNormal); |
152 | anArray->SetVertexNormal(aNodeIter, aNormal.x(), aNormal.y(), aNormal.z()); |
7fd59977 |
153 | } |
decbff0d |
154 | } |
155 | } |
156 | else // !hasVNormals |
157 | { |
158 | if (hasVColors) |
159 | { |
160 | const TColStd_Array1OfInteger& colors = myColor->Array1(); |
161 | for (Standard_Integer aNodeIter = 1; aNodeIter <= myTriangulation->NbNodes(); ++aNodeIter) |
162 | { |
163 | anArray->AddVertex (myTriangulation->Node (aNodeIter), attenuateColor (colors[aNodeIter], anAmbient)); |
164 | } |
165 | } |
166 | else // !hasVColors |
167 | { |
168 | for (Standard_Integer aNodeIter = 1; aNodeIter <= myTriangulation->NbNodes(); ++aNodeIter) |
a8b605eb |
169 | { |
decbff0d |
170 | anArray->AddVertex (myTriangulation->Node (aNodeIter)); |
7fd59977 |
171 | } |
decbff0d |
172 | } |
173 | } |
174 | |
175 | Standard_Integer aTriIndices[3] = {0,0,0}; |
176 | for (Standard_Integer aTriIter = 1; aTriIter <= myTriangulation->NbTriangles(); ++aTriIter) |
177 | { |
178 | myTriangulation->Triangle (aTriIter).Get (aTriIndices[0], aTriIndices[1], aTriIndices[2]); |
179 | anArray->AddEdge (aTriIndices[0]); |
180 | anArray->AddEdge (aTriIndices[1]); |
181 | anArray->AddEdge (aTriIndices[2]); |
7fd59977 |
182 | } |
decbff0d |
183 | aGroup->SetPrimitivesAspect (anAspect); |
184 | aGroup->AddPrimitiveArray (anArray); |
7fd59977 |
185 | } |
186 | |
187 | //======================================================================= |
188 | //function : ComputeSelection |
7379f442 |
189 | //purpose : |
7fd59977 |
190 | //======================================================================= |
35e08fe8 |
191 | void AIS_Triangulation::ComputeSelection(const Handle(SelectMgr_Selection)& /*aSelection*/, |
7fd59977 |
192 | const Standard_Integer /*aMode*/) |
193 | { |
194 | |
195 | } |
196 | |
197 | //======================================================================= |
198 | //function : SetColor |
199 | //purpose : Set the color for each node. |
200 | // Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red |
201 | // Order of color components is essential for further usage by OpenGL |
202 | //======================================================================= |
203 | void AIS_Triangulation::SetColors(const Handle(TColStd_HArray1OfInteger)& aColor) |
204 | { |
205 | myFlagColor = 1; |
206 | myColor = aColor; |
207 | } |
208 | |
209 | //======================================================================= |
210 | //function : GetColor |
211 | //purpose : Get the color for each node. |
212 | // Each 32-bit color is Alpha << 24 + Blue << 16 + Green << 8 + Red |
213 | // Order of color components is essential for further usage by OpenGL |
214 | //======================================================================= |
215 | |
216 | Handle(TColStd_HArray1OfInteger) AIS_Triangulation::GetColors() const |
217 | { |
218 | return myColor; |
219 | } |
220 | |
221 | |
222 | //======================================================================= |
223 | //function : SetTriangulation |
7379f442 |
224 | //purpose : |
7fd59977 |
225 | //======================================================================= |
226 | void AIS_Triangulation::SetTriangulation(const Handle(Poly_Triangulation)& aTriangulation) |
227 | { |
228 | myTriangulation = aTriangulation; |
229 | } |
230 | |
231 | //======================================================================= |
232 | //function : GetTriangulation |
7379f442 |
233 | //purpose : |
7fd59977 |
234 | //======================================================================= |
235 | Handle(Poly_Triangulation) AIS_Triangulation::GetTriangulation() const{ |
236 | return myTriangulation; |
237 | } |
238 | |
239 | //======================================================================= |
240 | //function : AttenuateColor |
7379f442 |
241 | //purpose : |
7fd59977 |
242 | //======================================================================= |
dcc17419 |
243 | Graphic3d_Vec4ub AIS_Triangulation::attenuateColor (const Standard_Integer theColor, |
244 | const Standard_Real theComposition) |
7fd59977 |
245 | { |
7379f442 |
246 | const Standard_Byte* anRgbx = reinterpret_cast<const Standard_Byte*> (&theColor); |
7fd59977 |
247 | |
dcc17419 |
248 | // If IsTranparent() is false alpha value will be ignored anyway. |
249 | Standard_Byte anAlpha = IsTransparent() ? static_cast<Standard_Byte> (255.0 - myDrawer->ShadingAspect()->Aspect()->FrontMaterial().Transparency() * 255.0) |
250 | : 255; |
7fd59977 |
251 | |
7379f442 |
252 | return Graphic3d_Vec4ub ((Standard_Byte)(theComposition * anRgbx[0]), |
253 | (Standard_Byte)(theComposition * anRgbx[1]), |
254 | (Standard_Byte)(theComposition * anRgbx[2]), |
dcc17419 |
255 | anAlpha); |
7fd59977 |
256 | } |