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