b311480e |
1 | // Created on: 2001-07-02 |
2 | // Created by: Mathias BOSSHARD |
973c2be1 |
3 | // Copyright (c) 2001-2014 OPEN CASCADE SAS |
7fd59977 |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
7fd59977 |
6 | // |
d5f74e42 |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
7fd59977 |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
b311480e |
15 | |
f376ac72 |
16 | #include <AIS_TexturedShape.hxx> |
7fd59977 |
17 | |
735dc1d2 |
18 | #include <AIS_GraphicTool.hxx> |
2bd4c032 |
19 | #include <AIS_InteractiveContext.hxx> |
7fd59977 |
20 | #include <BRepTools.hxx> |
2bd4c032 |
21 | #include <gp_Pnt2d.hxx> |
22 | #include <Graphic3d_AspectFillArea3d.hxx> |
23 | #include <Graphic3d_Group.hxx> |
7fd59977 |
24 | #include <Graphic3d_StructureManager.hxx> |
633084b8 |
25 | #include <Graphic3d_Texture2D.hxx> |
124ee9c9 |
26 | #include <Message.hxx> |
27 | #include <Message_Messenger.hxx> |
6262338c |
28 | #include <Prs3d_Drawer.hxx> |
2bd4c032 |
29 | #include <Prs3d_Presentation.hxx> |
2bd4c032 |
30 | #include <Prs3d_ShadingAspect.hxx> |
decbff0d |
31 | #include <PrsMgr_PresentationManager.hxx> |
f376ac72 |
32 | #include <Standard_ErrorHandler.hxx> |
7f24b768 |
33 | #include <Prs3d_BndBox.hxx> |
2bd4c032 |
34 | #include <StdPrs_ShadedShape.hxx> |
5ad8c033 |
35 | #include <StdPrs_ToolTriangulatedShape.hxx> |
7fd59977 |
36 | #include <StdPrs_WFShape.hxx> |
7fd59977 |
37 | |
f376ac72 |
38 | |
92efcf78 |
39 | IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape,AIS_Shape) |
40 | |
7fd59977 |
41 | //======================================================================= |
42 | //function : AIS_TexturedShape |
2bd4c032 |
43 | //purpose : |
7fd59977 |
44 | //======================================================================= |
2bd4c032 |
45 | AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape) |
f376ac72 |
46 | : AIS_Shape (theShape), |
47 | myPredefTexture (Graphic3d_NameOfTexture2D(0)), |
48 | myToMapTexture (Standard_True), |
49 | myModulate (Standard_True), |
f376ac72 |
50 | myIsCustomOrigin (Standard_True), |
f376ac72 |
51 | myToRepeat (Standard_True), |
f376ac72 |
52 | myToScale (Standard_True), |
53 | myToShowTriangles (Standard_False) |
7fd59977 |
54 | { |
55 | } |
56 | |
7fd59977 |
57 | //======================================================================= |
58 | //function : SetTextureFileName |
2bd4c032 |
59 | //purpose : |
7fd59977 |
60 | //======================================================================= |
2bd4c032 |
61 | void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName) |
7fd59977 |
62 | { |
f376ac72 |
63 | myTexturePixMap.Nullify(); |
64 | |
2bd4c032 |
65 | if (theTextureFileName.IsIntegerValue()) |
66 | { |
f376ac72 |
67 | const Standard_Integer aValue = theTextureFileName.IntegerValue(); |
68 | if (aValue < Graphic3d_Texture2D::NumberOfTextures() |
69 | && aValue >= 0) |
7fd59977 |
70 | { |
f376ac72 |
71 | myPredefTexture = Graphic3d_NameOfTexture2D (aValue); |
7fd59977 |
72 | } |
2bd4c032 |
73 | else |
7fd59977 |
74 | { |
a87b1b37 |
75 | Message::SendFail (TCollection_AsciiString ("Error: texture with ID ") + theTextureFileName |
76 | + " is undefined. Texture 0 will be used instead."); |
2bd4c032 |
77 | myPredefTexture = Graphic3d_NameOfTexture2D (0); |
7fd59977 |
78 | } |
2bd4c032 |
79 | myTextureFile = ""; |
80 | } |
81 | else |
82 | { |
83 | myTextureFile = theTextureFileName; |
f376ac72 |
84 | myPredefTexture = Graphic3d_NOT_2D_UNKNOWN; |
2bd4c032 |
85 | } |
7fd59977 |
86 | } |
87 | |
f376ac72 |
88 | //======================================================================= |
89 | //function : SetTexturePixMap |
90 | //purpose : |
91 | //======================================================================= |
92 | void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap) |
93 | { |
94 | myTextureFile = ""; |
95 | myPredefTexture = Graphic3d_NOT_2D_UNKNOWN; |
96 | myTexturePixMap = theTexturePixMap; |
97 | } |
98 | |
7fd59977 |
99 | //======================================================================= |
100 | //function : SetTextureRepeat |
2bd4c032 |
101 | //purpose : |
7fd59977 |
102 | //======================================================================= |
103 | |
f376ac72 |
104 | void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat, |
2bd4c032 |
105 | const Standard_Real theURepeat, |
106 | const Standard_Real theVRepeat) |
7fd59977 |
107 | { |
f376ac72 |
108 | myToRepeat = theToRepeat; |
109 | myUVRepeat.SetCoord (theURepeat, theVRepeat); |
7fd59977 |
110 | } |
111 | |
112 | //======================================================================= |
113 | //function : SetTextureMapOn |
2bd4c032 |
114 | //purpose : |
7fd59977 |
115 | //======================================================================= |
116 | |
117 | void AIS_TexturedShape::SetTextureMapOn() |
118 | { |
f376ac72 |
119 | myToMapTexture = Standard_True; |
7fd59977 |
120 | } |
121 | |
122 | //======================================================================= |
123 | //function : SetTextureMapOff |
2bd4c032 |
124 | //purpose : |
7fd59977 |
125 | //======================================================================= |
126 | |
127 | void AIS_TexturedShape::SetTextureMapOff() |
128 | { |
f376ac72 |
129 | myToMapTexture = Standard_False; |
7fd59977 |
130 | } |
131 | |
132 | //======================================================================= |
133 | //function : SetTextureOrigin |
2bd4c032 |
134 | //purpose : |
7fd59977 |
135 | //======================================================================= |
136 | |
f376ac72 |
137 | void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin, |
2bd4c032 |
138 | const Standard_Real theUOrigin, |
139 | const Standard_Real theVOrigin) |
7fd59977 |
140 | { |
f376ac72 |
141 | myIsCustomOrigin = theToSetTextureOrigin; |
142 | myUVOrigin.SetCoord (theUOrigin, theVOrigin); |
7fd59977 |
143 | } |
144 | |
145 | //======================================================================= |
146 | //function : SetTextureScale |
2bd4c032 |
147 | //purpose : |
7fd59977 |
148 | //======================================================================= |
149 | |
f376ac72 |
150 | void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale, |
2bd4c032 |
151 | const Standard_Real theScaleU, |
152 | const Standard_Real theScaleV) |
7fd59977 |
153 | { |
f376ac72 |
154 | myToScale = theToSetTextureScale; |
155 | myUVScale.SetCoord (theScaleU, theScaleV); |
7fd59977 |
156 | } |
157 | |
158 | //======================================================================= |
159 | //function : ShowTriangles |
2bd4c032 |
160 | //purpose : |
7fd59977 |
161 | //======================================================================= |
162 | |
f376ac72 |
163 | void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles) |
7fd59977 |
164 | { |
f376ac72 |
165 | myToShowTriangles = theToShowTriangles; |
7fd59977 |
166 | } |
167 | |
168 | //======================================================================= |
169 | //function : EnableTextureModulate |
2bd4c032 |
170 | //purpose : |
7fd59977 |
171 | //======================================================================= |
172 | |
173 | void AIS_TexturedShape::EnableTextureModulate() |
174 | { |
175 | myModulate = Standard_True; |
176 | } |
177 | |
178 | //======================================================================= |
179 | //function : DisableTextureModulate |
bf75be98 |
180 | //purpose : |
7fd59977 |
181 | //======================================================================= |
182 | |
183 | void AIS_TexturedShape::DisableTextureModulate() |
184 | { |
185 | myModulate = Standard_False; |
186 | } |
187 | |
7389f96d |
188 | //======================================================================= |
189 | //function : SetColor |
190 | //purpose : |
191 | //======================================================================= |
192 | |
193 | void AIS_TexturedShape::SetColor (const Quantity_Color& theColor) |
194 | { |
195 | AIS_Shape::SetColor (theColor); |
196 | |
7dd7c146 |
197 | for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next()) |
7389f96d |
198 | { |
7dd7c146 |
199 | if (aPrsIter.Value()->Mode() == 3) |
200 | { |
201 | updateAttributes (aPrsIter.Value()); |
202 | } |
735dc1d2 |
203 | } |
204 | } |
205 | |
206 | //======================================================================= |
207 | //function : UnsetColor |
208 | //purpose : |
209 | //======================================================================= |
210 | |
211 | void AIS_TexturedShape::UnsetColor() |
212 | { |
213 | AIS_Shape::UnsetColor(); |
735dc1d2 |
214 | } |
215 | |
216 | //======================================================================= |
217 | //function : SetMaterial |
218 | //purpose : |
219 | //======================================================================= |
220 | |
221 | void AIS_TexturedShape::SetMaterial (const Graphic3d_MaterialAspect& theMat) |
222 | { |
223 | AIS_Shape::SetMaterial (theMat); |
7dd7c146 |
224 | for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next()) |
735dc1d2 |
225 | { |
7dd7c146 |
226 | if (aPrsIter.Value()->Mode() == 3) |
227 | { |
228 | updateAttributes (aPrsIter.Value()); |
229 | } |
735dc1d2 |
230 | } |
231 | } |
232 | |
233 | //======================================================================= |
234 | //function : UnsetMaterial |
235 | //purpose : |
236 | //======================================================================= |
237 | void AIS_TexturedShape::UnsetMaterial() |
238 | { |
239 | AIS_Shape::UnsetMaterial(); |
7dd7c146 |
240 | for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next()) |
735dc1d2 |
241 | { |
7dd7c146 |
242 | if (aPrsIter.Value()->Mode() == 3) |
243 | { |
244 | updateAttributes (aPrsIter.Value()); |
245 | } |
7389f96d |
246 | } |
247 | } |
248 | |
7fd59977 |
249 | //======================================================================= |
250 | //function : UpdateAttributes |
2bd4c032 |
251 | //purpose : |
7fd59977 |
252 | //======================================================================= |
253 | |
254 | void AIS_TexturedShape::UpdateAttributes() |
8e3ebc7a |
255 | { |
256 | updateAttributes (Presentation()); |
257 | } |
258 | |
259 | //======================================================================= |
260 | //function : updateAttributes |
261 | //purpose : |
262 | //======================================================================= |
263 | |
264 | void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs) |
7fd59977 |
265 | { |
7389f96d |
266 | myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect()); |
8e3ebc7a |
267 | if (HasPolygonOffsets()) |
268 | { |
8e3ebc7a |
269 | Standard_Integer aMode; |
270 | Standard_ShortReal aFactor, aUnits; |
271 | PolygonOffsets (aMode, aFactor, aUnits); |
272 | myAspect->SetPolygonOffsets (aMode, aFactor, aUnits); |
273 | } |
274 | |
124ee9c9 |
275 | Standard_Boolean hasTexture = Standard_False; |
276 | if (myToMapTexture) |
2bd4c032 |
277 | { |
124ee9c9 |
278 | TCollection_AsciiString aTextureDesc; |
279 | if (!myTexturePixMap.IsNull()) |
280 | { |
633084b8 |
281 | myTexture = new Graphic3d_Texture2D (myTexturePixMap); |
124ee9c9 |
282 | aTextureDesc = " (custom image)"; |
283 | } |
284 | else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN) |
285 | { |
633084b8 |
286 | myTexture = new Graphic3d_Texture2D (myPredefTexture); |
124ee9c9 |
287 | aTextureDesc = TCollection_AsciiString(" (predefined texture ") + myTexture->GetId() + ")"; |
288 | } |
289 | else |
290 | { |
633084b8 |
291 | myTexture = new Graphic3d_Texture2D (myTextureFile.ToCString()); |
124ee9c9 |
292 | aTextureDesc = TCollection_AsciiString(" (") + myTextureFile + ")"; |
293 | } |
2bd4c032 |
294 | |
124ee9c9 |
295 | if (myModulate) |
296 | { |
297 | myTexture->EnableModulate(); |
298 | } |
299 | else |
300 | { |
301 | myTexture->DisableModulate(); |
302 | } |
303 | |
304 | if (myTexture->IsDone()) |
305 | { |
306 | hasTexture = Standard_True; |
307 | } |
308 | else |
309 | { |
a87b1b37 |
310 | Message::SendFail (TCollection_AsciiString ("Error: texture can not be loaded ") + aTextureDesc); |
124ee9c9 |
311 | } |
f376ac72 |
312 | } |
124ee9c9 |
313 | |
314 | myAspect->SetTextureMap (myTexture); |
315 | if (hasTexture) |
f376ac72 |
316 | { |
124ee9c9 |
317 | myAspect->SetTextureMapOn(); |
f376ac72 |
318 | } |
7fd59977 |
319 | else |
f376ac72 |
320 | { |
8e3ebc7a |
321 | myAspect->SetTextureMapOff(); |
2bd4c032 |
322 | } |
323 | |
f376ac72 |
324 | if (myToShowTriangles) |
8e3ebc7a |
325 | { |
124ee9c9 |
326 | myAspect->SetEdgeOn(); |
8e3ebc7a |
327 | } |
328 | else |
329 | { |
124ee9c9 |
330 | myAspect->SetEdgeOff(); |
8e3ebc7a |
331 | } |
332 | |
735dc1d2 |
333 | // Go through all groups to change fill aspect for all primitives |
334 | for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (thePrs->Groups()); aGroupIt.More(); aGroupIt.Next()) |
8e3ebc7a |
335 | { |
735dc1d2 |
336 | const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value(); |
124ee9c9 |
337 | aGroup->SetGroupPrimitivesAspect (myAspect); |
8e3ebc7a |
338 | } |
7fd59977 |
339 | } |
340 | |
341 | //======================================================================= |
342 | //function : Compute |
2bd4c032 |
343 | //purpose : |
7fd59977 |
344 | //======================================================================= |
decbff0d |
345 | void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager)& , |
346 | const Handle(Prs3d_Presentation)& thePrs, |
347 | const Standard_Integer theMode) |
7fd59977 |
348 | { |
e463b2f6 |
349 | if (myshape.IsNull() |
350 | || (myshape.ShapeType() == TopAbs_COMPOUND && myshape.NbChildren() == 0)) |
2bd4c032 |
351 | { |
352 | return; |
353 | } |
354 | |
e463b2f6 |
355 | if (myshape.ShapeType() >= TopAbs_WIRE |
356 | && myshape.ShapeType() <= TopAbs_VERTEX) |
2bd4c032 |
357 | { |
e463b2f6 |
358 | // TopAbs_WIRE -> 7, TopAbs_EDGE -> 8, TopAbs_VERTEX -> 9 (Graphic3d_DisplayPriority_Highlight) |
359 | const Standard_Integer aPrior = (Standard_Integer )Graphic3d_DisplayPriority_Above1 |
360 | + (Standard_Integer )myshape.ShapeType() - TopAbs_WIRE; |
2bd4c032 |
361 | thePrs->SetVisual (Graphic3d_TOS_ALL); |
e463b2f6 |
362 | thePrs->SetDisplayPriority ((Graphic3d_DisplayPriority )aPrior); |
2bd4c032 |
363 | } |
7fd59977 |
364 | |
365 | if (IsInfinite()) |
2bd4c032 |
366 | { |
367 | thePrs->SetInfiniteState (Standard_True); |
368 | } |
7fd59977 |
369 | |
2bd4c032 |
370 | switch (theMode) |
371 | { |
f376ac72 |
372 | case AIS_WireFrame: |
2bd4c032 |
373 | { |
83b0f13a |
374 | StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True); |
5ad8c033 |
375 | StdPrs_WFShape::Add (thePrs, myshape, myDrawer); |
7fd59977 |
376 | break; |
2bd4c032 |
377 | } |
f376ac72 |
378 | case AIS_Shaded: |
50b830a0 |
379 | case 3: // texture mapping on triangulation |
2bd4c032 |
380 | { |
83b0f13a |
381 | StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True); |
2bd4c032 |
382 | if (myshape.ShapeType() > TopAbs_FACE) |
7fd59977 |
383 | { |
5ad8c033 |
384 | StdPrs_WFShape::Add (thePrs, myshape, myDrawer); |
2bd4c032 |
385 | break; |
7fd59977 |
386 | } |
50b830a0 |
387 | |
2bd4c032 |
388 | if (IsInfinite()) |
389 | { |
5ad8c033 |
390 | StdPrs_WFShape::Add (thePrs, myshape, myDrawer); |
2bd4c032 |
391 | break; |
392 | } |
393 | try |
394 | { |
395 | OCC_CATCH_SIGNALS |
50b830a0 |
396 | if (theMode == AIS_Shaded) |
397 | { |
398 | StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer); |
399 | } |
400 | else |
401 | { |
402 | StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer, |
403 | Standard_True, |
404 | myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0), |
405 | myUVRepeat, |
406 | myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0)); |
407 | updateAttributes (thePrs); |
408 | } |
2bd4c032 |
409 | } |
a738b534 |
410 | catch (Standard_Failure const&) |
2bd4c032 |
411 | { |
0797d9d3 |
412 | #ifdef OCCT_DEBUG |
2bd4c032 |
413 | std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n"; |
63c629aa |
414 | #endif |
2bd4c032 |
415 | StdPrs_WFShape::Add (thePrs, myshape, myDrawer); |
416 | } |
417 | break; |
418 | } |
7fd59977 |
419 | case 2: // Bounding box |
2bd4c032 |
420 | { |
421 | if (IsInfinite()) |
7fd59977 |
422 | { |
5ad8c033 |
423 | StdPrs_WFShape::Add (thePrs, myshape, myDrawer); |
7fd59977 |
424 | } |
2bd4c032 |
425 | else |
7fd59977 |
426 | { |
7f24b768 |
427 | Prs3d_BndBox::Add (thePrs, BoundingBox(), myDrawer); |
2bd4c032 |
428 | } |
429 | break; |
430 | } |
2bd4c032 |
431 | } |
432 | } |