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> |
25 | #include <Graphic3d_Texture2Dmanual.hxx> |
124ee9c9 |
26 | #include <Message.hxx> |
27 | #include <Message_Messenger.hxx> |
2bd4c032 |
28 | #include <Precision.hxx> |
6262338c |
29 | #include <Prs3d_Drawer.hxx> |
2bd4c032 |
30 | #include <Prs3d_Presentation.hxx> |
735dc1d2 |
31 | #include <PrsMgr_ModedPresentation.hxx> |
7fd59977 |
32 | #include <Prs3d_Root.hxx> |
735dc1d2 |
33 | #include <Prs3d_LineAspect.hxx> |
2bd4c032 |
34 | #include <Prs3d_ShadingAspect.hxx> |
7fd59977 |
35 | #include <PrsMgr_PresentationManager3d.hxx> |
f376ac72 |
36 | #include <Standard_ErrorHandler.hxx> |
5ad8c033 |
37 | #include <StdPrs_BndBox.hxx> |
2bd4c032 |
38 | #include <StdPrs_ShadedShape.hxx> |
5ad8c033 |
39 | #include <StdPrs_ToolTriangulatedShape.hxx> |
7fd59977 |
40 | #include <StdPrs_WFShape.hxx> |
2bd4c032 |
41 | #include <TopExp_Explorer.hxx> |
7fd59977 |
42 | |
f376ac72 |
43 | |
92efcf78 |
44 | IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape,AIS_Shape) |
45 | |
7fd59977 |
46 | //======================================================================= |
47 | //function : AIS_TexturedShape |
2bd4c032 |
48 | //purpose : |
7fd59977 |
49 | //======================================================================= |
2bd4c032 |
50 | AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape) |
f376ac72 |
51 | : AIS_Shape (theShape), |
52 | myPredefTexture (Graphic3d_NameOfTexture2D(0)), |
53 | myToMapTexture (Standard_True), |
54 | myModulate (Standard_True), |
55 | myUVOrigin (0.0, 0.0), |
56 | myIsCustomOrigin (Standard_True), |
57 | myUVRepeat (1.0, 1.0), |
58 | myToRepeat (Standard_True), |
59 | myUVScale (1.0, 1.0), |
60 | myToScale (Standard_True), |
61 | myToShowTriangles (Standard_False) |
7fd59977 |
62 | { |
63 | } |
64 | |
7fd59977 |
65 | //======================================================================= |
66 | //function : SetTextureFileName |
2bd4c032 |
67 | //purpose : |
7fd59977 |
68 | //======================================================================= |
2bd4c032 |
69 | void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName) |
7fd59977 |
70 | { |
f376ac72 |
71 | myTexturePixMap.Nullify(); |
72 | |
2bd4c032 |
73 | if (theTextureFileName.IsIntegerValue()) |
74 | { |
f376ac72 |
75 | const Standard_Integer aValue = theTextureFileName.IntegerValue(); |
76 | if (aValue < Graphic3d_Texture2D::NumberOfTextures() |
77 | && aValue >= 0) |
7fd59977 |
78 | { |
f376ac72 |
79 | myPredefTexture = Graphic3d_NameOfTexture2D (aValue); |
7fd59977 |
80 | } |
2bd4c032 |
81 | else |
7fd59977 |
82 | { |
124ee9c9 |
83 | Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: texture with ID ") + theTextureFileName |
84 | + " is undefined! Texture 0 will be used instead.", Message_Fail); |
2bd4c032 |
85 | myPredefTexture = Graphic3d_NameOfTexture2D (0); |
7fd59977 |
86 | } |
2bd4c032 |
87 | myTextureFile = ""; |
88 | } |
89 | else |
90 | { |
91 | myTextureFile = theTextureFileName; |
f376ac72 |
92 | myPredefTexture = Graphic3d_NOT_2D_UNKNOWN; |
2bd4c032 |
93 | } |
7fd59977 |
94 | } |
95 | |
f376ac72 |
96 | //======================================================================= |
97 | //function : SetTexturePixMap |
98 | //purpose : |
99 | //======================================================================= |
100 | void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap) |
101 | { |
102 | myTextureFile = ""; |
103 | myPredefTexture = Graphic3d_NOT_2D_UNKNOWN; |
104 | myTexturePixMap = theTexturePixMap; |
105 | } |
106 | |
7fd59977 |
107 | //======================================================================= |
108 | //function : SetTextureRepeat |
2bd4c032 |
109 | //purpose : |
7fd59977 |
110 | //======================================================================= |
111 | |
f376ac72 |
112 | void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat, |
2bd4c032 |
113 | const Standard_Real theURepeat, |
114 | const Standard_Real theVRepeat) |
7fd59977 |
115 | { |
f376ac72 |
116 | myToRepeat = theToRepeat; |
117 | myUVRepeat.SetCoord (theURepeat, theVRepeat); |
7fd59977 |
118 | } |
119 | |
120 | //======================================================================= |
121 | //function : SetTextureMapOn |
2bd4c032 |
122 | //purpose : |
7fd59977 |
123 | //======================================================================= |
124 | |
125 | void AIS_TexturedShape::SetTextureMapOn() |
126 | { |
f376ac72 |
127 | myToMapTexture = Standard_True; |
7fd59977 |
128 | } |
129 | |
130 | //======================================================================= |
131 | //function : SetTextureMapOff |
2bd4c032 |
132 | //purpose : |
7fd59977 |
133 | //======================================================================= |
134 | |
135 | void AIS_TexturedShape::SetTextureMapOff() |
136 | { |
f376ac72 |
137 | myToMapTexture = Standard_False; |
7fd59977 |
138 | } |
139 | |
140 | //======================================================================= |
141 | //function : SetTextureOrigin |
2bd4c032 |
142 | //purpose : |
7fd59977 |
143 | //======================================================================= |
144 | |
f376ac72 |
145 | void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin, |
2bd4c032 |
146 | const Standard_Real theUOrigin, |
147 | const Standard_Real theVOrigin) |
7fd59977 |
148 | { |
f376ac72 |
149 | myIsCustomOrigin = theToSetTextureOrigin; |
150 | myUVOrigin.SetCoord (theUOrigin, theVOrigin); |
7fd59977 |
151 | } |
152 | |
153 | //======================================================================= |
154 | //function : SetTextureScale |
2bd4c032 |
155 | //purpose : |
7fd59977 |
156 | //======================================================================= |
157 | |
f376ac72 |
158 | void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale, |
2bd4c032 |
159 | const Standard_Real theScaleU, |
160 | const Standard_Real theScaleV) |
7fd59977 |
161 | { |
f376ac72 |
162 | myToScale = theToSetTextureScale; |
163 | myUVScale.SetCoord (theScaleU, theScaleV); |
7fd59977 |
164 | } |
165 | |
166 | //======================================================================= |
167 | //function : ShowTriangles |
2bd4c032 |
168 | //purpose : |
7fd59977 |
169 | //======================================================================= |
170 | |
f376ac72 |
171 | void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles) |
7fd59977 |
172 | { |
f376ac72 |
173 | myToShowTriangles = theToShowTriangles; |
7fd59977 |
174 | } |
175 | |
176 | //======================================================================= |
177 | //function : EnableTextureModulate |
2bd4c032 |
178 | //purpose : |
7fd59977 |
179 | //======================================================================= |
180 | |
181 | void AIS_TexturedShape::EnableTextureModulate() |
182 | { |
183 | myModulate = Standard_True; |
184 | } |
185 | |
186 | //======================================================================= |
187 | //function : DisableTextureModulate |
bf75be98 |
188 | //purpose : |
7fd59977 |
189 | //======================================================================= |
190 | |
191 | void AIS_TexturedShape::DisableTextureModulate() |
192 | { |
193 | myModulate = Standard_False; |
194 | } |
195 | |
7389f96d |
196 | //======================================================================= |
197 | //function : SetColor |
198 | //purpose : |
199 | //======================================================================= |
200 | |
201 | void AIS_TexturedShape::SetColor (const Quantity_Color& theColor) |
202 | { |
203 | AIS_Shape::SetColor (theColor); |
204 | |
735dc1d2 |
205 | for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt) |
7389f96d |
206 | { |
735dc1d2 |
207 | const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt); |
208 | |
209 | if (aPrsModed.Mode() != 3) |
210 | continue; |
211 | |
212 | updateAttributes (aPrsModed.Presentation()->Presentation()); |
213 | } |
214 | } |
215 | |
216 | //======================================================================= |
217 | //function : UnsetColor |
218 | //purpose : |
219 | //======================================================================= |
220 | |
221 | void AIS_TexturedShape::UnsetColor() |
222 | { |
223 | AIS_Shape::UnsetColor(); |
224 | |
225 | for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt) |
226 | { |
227 | const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt); |
228 | |
229 | if (aPrsModed.Mode() != 3) |
230 | continue; |
231 | |
232 | Handle(Prs3d_Presentation) aPrs = aPrsModed.Presentation()->Presentation(); |
233 | Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (aPrs); |
234 | |
235 | Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->Link()->ShadingAspect()->Aspect(); |
236 | Handle(Graphic3d_AspectLine3d) aLineAsp = myDrawer->Link()->LineAspect()->Aspect(); |
237 | Quantity_Color aColor; |
238 | AIS_GraphicTool::GetInteriorColor (myDrawer->Link(), aColor); |
239 | anAreaAsp->SetInteriorColor (aColor); |
735dc1d2 |
240 | // Check if aspect of given type is set for the group, |
241 | // because setting aspect for group with no already set aspect |
242 | // can lead to loss of presentation data |
243 | if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA)) |
7389f96d |
244 | { |
735dc1d2 |
245 | aGroup->SetGroupPrimitivesAspect (anAreaAsp); |
7389f96d |
246 | } |
735dc1d2 |
247 | if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_LINE)) |
248 | { |
249 | aGroup->SetGroupPrimitivesAspect (aLineAsp); |
250 | } |
251 | |
252 | updateAttributes (aPrs); |
253 | } |
254 | } |
255 | |
256 | //======================================================================= |
257 | //function : SetMaterial |
258 | //purpose : |
259 | //======================================================================= |
260 | |
261 | void AIS_TexturedShape::SetMaterial (const Graphic3d_MaterialAspect& theMat) |
262 | { |
263 | AIS_Shape::SetMaterial (theMat); |
264 | |
265 | for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt) |
266 | { |
267 | const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt); |
268 | |
269 | if (aPrsModed.Mode() != 3) |
270 | continue; |
271 | |
272 | updateAttributes (aPrsModed.Presentation()->Presentation()); |
273 | } |
274 | } |
275 | |
276 | //======================================================================= |
277 | //function : UnsetMaterial |
278 | //purpose : |
279 | //======================================================================= |
280 | void AIS_TexturedShape::UnsetMaterial() |
281 | { |
282 | AIS_Shape::UnsetMaterial(); |
283 | |
284 | for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt) |
285 | { |
286 | const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt); |
287 | |
288 | if (aPrsModed.Mode() != 3) |
289 | continue; |
290 | |
291 | updateAttributes (aPrsModed.Presentation()->Presentation()); |
7389f96d |
292 | } |
293 | } |
294 | |
7fd59977 |
295 | //======================================================================= |
296 | //function : UpdateAttributes |
2bd4c032 |
297 | //purpose : |
7fd59977 |
298 | //======================================================================= |
299 | |
300 | void AIS_TexturedShape::UpdateAttributes() |
8e3ebc7a |
301 | { |
302 | updateAttributes (Presentation()); |
303 | } |
304 | |
305 | //======================================================================= |
306 | //function : updateAttributes |
307 | //purpose : |
308 | //======================================================================= |
309 | |
310 | void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs) |
7fd59977 |
311 | { |
7389f96d |
312 | myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect()); |
8e3ebc7a |
313 | if (HasPolygonOffsets()) |
314 | { |
8e3ebc7a |
315 | Standard_Integer aMode; |
316 | Standard_ShortReal aFactor, aUnits; |
317 | PolygonOffsets (aMode, aFactor, aUnits); |
318 | myAspect->SetPolygonOffsets (aMode, aFactor, aUnits); |
319 | } |
320 | |
124ee9c9 |
321 | Standard_Boolean hasTexture = Standard_False; |
322 | if (myToMapTexture) |
2bd4c032 |
323 | { |
124ee9c9 |
324 | TCollection_AsciiString aTextureDesc; |
325 | if (!myTexturePixMap.IsNull()) |
326 | { |
327 | myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap); |
328 | aTextureDesc = " (custom image)"; |
329 | } |
330 | else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN) |
331 | { |
332 | myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture); |
333 | aTextureDesc = TCollection_AsciiString(" (predefined texture ") + myTexture->GetId() + ")"; |
334 | } |
335 | else |
336 | { |
337 | myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString()); |
338 | aTextureDesc = TCollection_AsciiString(" (") + myTextureFile + ")"; |
339 | } |
2bd4c032 |
340 | |
124ee9c9 |
341 | if (myModulate) |
342 | { |
343 | myTexture->EnableModulate(); |
344 | } |
345 | else |
346 | { |
347 | myTexture->DisableModulate(); |
348 | } |
349 | |
350 | if (myTexture->IsDone()) |
351 | { |
352 | hasTexture = Standard_True; |
353 | } |
354 | else |
355 | { |
356 | Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: texture can not be loaded ") + aTextureDesc, Message_Fail); |
357 | } |
f376ac72 |
358 | } |
124ee9c9 |
359 | |
360 | myAspect->SetTextureMap (myTexture); |
361 | if (hasTexture) |
f376ac72 |
362 | { |
124ee9c9 |
363 | myAspect->SetTextureMapOn(); |
f376ac72 |
364 | } |
7fd59977 |
365 | else |
f376ac72 |
366 | { |
8e3ebc7a |
367 | myAspect->SetTextureMapOff(); |
2bd4c032 |
368 | } |
369 | |
f376ac72 |
370 | if (myToShowTriangles) |
8e3ebc7a |
371 | { |
124ee9c9 |
372 | myAspect->SetEdgeOn(); |
8e3ebc7a |
373 | } |
374 | else |
375 | { |
124ee9c9 |
376 | myAspect->SetEdgeOff(); |
8e3ebc7a |
377 | } |
378 | |
735dc1d2 |
379 | // Go through all groups to change fill aspect for all primitives |
380 | for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (thePrs->Groups()); aGroupIt.More(); aGroupIt.Next()) |
8e3ebc7a |
381 | { |
735dc1d2 |
382 | const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value(); |
124ee9c9 |
383 | if (!aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA)) |
384 | { |
385 | continue; |
386 | } |
124ee9c9 |
387 | aGroup->SetGroupPrimitivesAspect (myAspect); |
8e3ebc7a |
388 | } |
7fd59977 |
389 | } |
390 | |
391 | //======================================================================= |
392 | //function : Compute |
2bd4c032 |
393 | //purpose : |
7fd59977 |
394 | //======================================================================= |
395 | |
f376ac72 |
396 | void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/, |
2bd4c032 |
397 | const Handle(Prs3d_Presentation)& thePrs, |
398 | const Standard_Integer theMode) |
7fd59977 |
399 | { |
2bd4c032 |
400 | if (myshape.IsNull()) |
401 | { |
402 | return; |
403 | } |
404 | |
405 | if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE) |
406 | { |
407 | thePrs->SetVisual (Graphic3d_TOS_ALL); |
408 | thePrs->SetDisplayPriority (myshape.ShapeType() + 2); |
409 | } |
7fd59977 |
410 | |
411 | if (myshape.ShapeType() == TopAbs_COMPOUND) |
2bd4c032 |
412 | { |
413 | TopExp_Explorer anExplor (myshape, TopAbs_VERTEX); |
414 | if (!anExplor.More()) |
7fd59977 |
415 | { |
2bd4c032 |
416 | return; |
7fd59977 |
417 | } |
2bd4c032 |
418 | } |
7fd59977 |
419 | |
420 | if (IsInfinite()) |
2bd4c032 |
421 | { |
422 | thePrs->SetInfiniteState (Standard_True); |
423 | } |
7fd59977 |
424 | |
2bd4c032 |
425 | switch (theMode) |
426 | { |
f376ac72 |
427 | case AIS_WireFrame: |
2bd4c032 |
428 | { |
83b0f13a |
429 | StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True); |
5ad8c033 |
430 | StdPrs_WFShape::Add (thePrs, myshape, myDrawer); |
7fd59977 |
431 | break; |
2bd4c032 |
432 | } |
f376ac72 |
433 | case AIS_Shaded: |
50b830a0 |
434 | case 3: // texture mapping on triangulation |
2bd4c032 |
435 | { |
83b0f13a |
436 | StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True); |
2bd4c032 |
437 | if (myshape.ShapeType() > TopAbs_FACE) |
7fd59977 |
438 | { |
5ad8c033 |
439 | StdPrs_WFShape::Add (thePrs, myshape, myDrawer); |
2bd4c032 |
440 | break; |
7fd59977 |
441 | } |
50b830a0 |
442 | |
2bd4c032 |
443 | if (IsInfinite()) |
444 | { |
5ad8c033 |
445 | StdPrs_WFShape::Add (thePrs, myshape, myDrawer); |
2bd4c032 |
446 | break; |
447 | } |
448 | try |
449 | { |
450 | OCC_CATCH_SIGNALS |
50b830a0 |
451 | if (theMode == AIS_Shaded) |
452 | { |
453 | StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer); |
454 | } |
455 | else |
456 | { |
457 | StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer, |
458 | Standard_True, |
459 | myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0), |
460 | myUVRepeat, |
461 | myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0)); |
462 | updateAttributes (thePrs); |
463 | } |
2bd4c032 |
464 | } |
465 | catch (Standard_Failure) |
466 | { |
0797d9d3 |
467 | #ifdef OCCT_DEBUG |
2bd4c032 |
468 | std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n"; |
63c629aa |
469 | #endif |
2bd4c032 |
470 | StdPrs_WFShape::Add (thePrs, myshape, myDrawer); |
471 | } |
472 | break; |
473 | } |
7fd59977 |
474 | case 2: // Bounding box |
2bd4c032 |
475 | { |
476 | if (IsInfinite()) |
7fd59977 |
477 | { |
5ad8c033 |
478 | StdPrs_WFShape::Add (thePrs, myshape, myDrawer); |
7fd59977 |
479 | } |
2bd4c032 |
480 | else |
7fd59977 |
481 | { |
5ad8c033 |
482 | StdPrs_BndBox::Add (thePrs, BoundingBox(), myDrawer); |
2bd4c032 |
483 | } |
484 | break; |
485 | } |
2bd4c032 |
486 | } |
487 | } |