b311480e |
1 | // Created on: 2001-07-02 |
2 | // Created by: Mathias BOSSHARD |
3 | // Copyright (c) 2001-2012 OPEN CASCADE SAS |
7fd59977 |
4 | // |
b311480e |
5 | // The content of this file is subject to the Open CASCADE Technology Public |
6 | // License Version 6.5 (the "License"). You may not use the content of this file |
7 | // except in compliance with the License. Please obtain a copy of the License |
8 | // at http://www.opencascade.org and read it completely before using this file. |
7fd59977 |
9 | // |
b311480e |
10 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
11 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
7fd59977 |
12 | // |
b311480e |
13 | // The Original Code and all software distributed under the License is |
14 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
15 | // Initial Developer hereby disclaims all such warranties, including without |
16 | // limitation, any warranties of merchantability, fitness for a particular |
17 | // purpose or non-infringement. Please see the License for the specific terms |
18 | // and conditions governing the rights and limitations under the License. |
19 | |
20 | // Modified: |
7fd59977 |
21 | //////////////////////////////////////////////////////////////////////// |
22 | |
23 | |
24 | #include <AIS_TexturedShape.ixx> |
25 | #include <Standard_ErrorHandler.hxx> |
26 | |
2bd4c032 |
27 | #include <AIS_Drawer.hxx> |
28 | #include <AIS_InteractiveContext.hxx> |
7fd59977 |
29 | #include <BRepTools.hxx> |
2bd4c032 |
30 | #include <gp_Pnt2d.hxx> |
31 | #include <Graphic3d_AspectFillArea3d.hxx> |
32 | #include <Graphic3d_Group.hxx> |
7fd59977 |
33 | #include <Graphic3d_StructureManager.hxx> |
34 | #include <Graphic3d_Texture2Dmanual.hxx> |
2bd4c032 |
35 | #include <Precision.hxx> |
36 | #include <Prs3d_Presentation.hxx> |
7fd59977 |
37 | #include <Prs3d_Root.hxx> |
2bd4c032 |
38 | #include <Prs3d_ShadingAspect.hxx> |
7fd59977 |
39 | #include <PrsMgr_PresentationManager3d.hxx> |
2bd4c032 |
40 | #include <StdPrs_ShadedShape.hxx> |
7fd59977 |
41 | #include <StdPrs_WFDeflectionShape.hxx> |
7fd59977 |
42 | #include <StdPrs_WFShape.hxx> |
2bd4c032 |
43 | #include <TopExp_Explorer.hxx> |
7fd59977 |
44 | |
45 | //======================================================================= |
46 | //function : AIS_TexturedShape |
2bd4c032 |
47 | //purpose : |
7fd59977 |
48 | //======================================================================= |
2bd4c032 |
49 | AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape) |
50 | : AIS_Shape (theShape), |
51 | myPredefTexture (Graphic3d_NameOfTexture2D(0)), |
52 | myTextureFile (""), |
53 | DoRepeat (Standard_True), |
54 | myURepeat (1.0), |
55 | myVRepeat (1.0), |
56 | DoMapTexture (Standard_True), |
57 | DoSetTextureOrigin (Standard_True), |
58 | myUOrigin (0.0), |
59 | myVOrigin (0.0), |
60 | DoSetTextureScale (Standard_True), |
61 | myScaleU (1.0), |
62 | myScaleV (1.0), |
63 | DoShowTriangles (Standard_False), |
64 | myModulate (Standard_True) |
7fd59977 |
65 | { |
66 | } |
67 | |
7fd59977 |
68 | //======================================================================= |
69 | //function : SetTextureFileName |
2bd4c032 |
70 | //purpose : |
7fd59977 |
71 | //======================================================================= |
2bd4c032 |
72 | void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName) |
7fd59977 |
73 | { |
2bd4c032 |
74 | if (theTextureFileName.IsIntegerValue()) |
75 | { |
76 | if (theTextureFileName.IntegerValue() < Graphic3d_Texture2D::NumberOfTextures() |
77 | && theTextureFileName.IntegerValue() >= 0) |
7fd59977 |
78 | { |
2bd4c032 |
79 | myPredefTexture = Graphic3d_NameOfTexture2D (theTextureFileName.IntegerValue()); |
7fd59977 |
80 | } |
2bd4c032 |
81 | else |
7fd59977 |
82 | { |
2bd4c032 |
83 | std::cout << "Texture " << theTextureFileName << " doesn't exist \n"; |
84 | std::cout << "Using Texture 0 instead ...\n"; |
85 | myPredefTexture = Graphic3d_NameOfTexture2D (0); |
7fd59977 |
86 | } |
2bd4c032 |
87 | myTextureFile = ""; |
88 | } |
89 | else |
90 | { |
91 | myTextureFile = theTextureFileName; |
92 | myPredefTexture = Graphic3d_NameOfTexture2D (-1); |
93 | } |
7fd59977 |
94 | } |
95 | |
96 | //======================================================================= |
97 | //function : SetTextureRepeat |
2bd4c032 |
98 | //purpose : |
7fd59977 |
99 | //======================================================================= |
100 | |
2bd4c032 |
101 | void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theRepeatYN, |
102 | const Standard_Real theURepeat, |
103 | const Standard_Real theVRepeat) |
7fd59977 |
104 | { |
2bd4c032 |
105 | DoRepeat = theRepeatYN; |
106 | myURepeat = theURepeat; |
107 | myVRepeat = theVRepeat; |
7fd59977 |
108 | } |
109 | |
110 | //======================================================================= |
111 | //function : SetTextureMapOn |
2bd4c032 |
112 | //purpose : |
7fd59977 |
113 | //======================================================================= |
114 | |
115 | void AIS_TexturedShape::SetTextureMapOn() |
116 | { |
117 | DoMapTexture = Standard_True; |
118 | } |
119 | |
120 | //======================================================================= |
121 | //function : SetTextureMapOff |
2bd4c032 |
122 | //purpose : |
7fd59977 |
123 | //======================================================================= |
124 | |
125 | void AIS_TexturedShape::SetTextureMapOff() |
126 | { |
127 | DoMapTexture = Standard_False; |
128 | } |
129 | |
130 | //======================================================================= |
131 | //function : SetTextureOrigin |
2bd4c032 |
132 | //purpose : |
7fd59977 |
133 | //======================================================================= |
134 | |
2bd4c032 |
135 | void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean toSetTextureOriginYN, |
136 | const Standard_Real theUOrigin, |
137 | const Standard_Real theVOrigin) |
7fd59977 |
138 | { |
2bd4c032 |
139 | DoSetTextureOrigin = toSetTextureOriginYN; |
140 | myUOrigin = theUOrigin; |
141 | myVOrigin = theVOrigin; |
7fd59977 |
142 | } |
143 | |
144 | //======================================================================= |
145 | //function : SetTextureScale |
2bd4c032 |
146 | //purpose : |
7fd59977 |
147 | //======================================================================= |
148 | |
2bd4c032 |
149 | void AIS_TexturedShape::SetTextureScale (const Standard_Boolean toSetTextureScaleYN, |
150 | const Standard_Real theScaleU, |
151 | const Standard_Real theScaleV) |
7fd59977 |
152 | { |
2bd4c032 |
153 | DoSetTextureScale = toSetTextureScaleYN; |
154 | myScaleU = theScaleU; |
155 | myScaleV = theScaleV; |
7fd59977 |
156 | } |
157 | |
158 | //======================================================================= |
159 | //function : ShowTriangles |
2bd4c032 |
160 | //purpose : |
7fd59977 |
161 | //======================================================================= |
162 | |
2bd4c032 |
163 | void AIS_TexturedShape::ShowTriangles (const Standard_Boolean toShowTrianglesYN) |
7fd59977 |
164 | { |
2bd4c032 |
165 | DoShowTriangles = toShowTrianglesYN; |
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 |
180 | //purpose : |
181 | //======================================================================= |
182 | |
183 | void AIS_TexturedShape::DisableTextureModulate() |
184 | { |
185 | myModulate = Standard_False; |
186 | } |
187 | |
188 | //======================================================================= |
189 | //function : UpdateAttributes |
2bd4c032 |
190 | //purpose : |
7fd59977 |
191 | //======================================================================= |
192 | |
193 | void AIS_TexturedShape::UpdateAttributes() |
194 | { |
195 | Handle(Graphic3d_StructureManager) aStrucMana = GetContext()->MainPrsMgr()->StructureManager(); |
2bd4c032 |
196 | Prs3d_ShadingAspect aDummy; |
197 | myAspect = aDummy.Aspect(); |
7fd59977 |
198 | Handle(Prs3d_Presentation) aPrs = Presentation(); |
199 | if (!DoMapTexture) |
2bd4c032 |
200 | { |
201 | myAspect->SetTextureMapOff(); |
202 | return; |
203 | } |
204 | |
205 | if (myPredefTexture != -1) |
206 | mytexture = new Graphic3d_Texture2Dmanual (aStrucMana, myPredefTexture); |
7fd59977 |
207 | else |
2bd4c032 |
208 | mytexture = new Graphic3d_Texture2Dmanual (aStrucMana, myTextureFile.ToCString()); |
209 | |
7fd59977 |
210 | myAspect->SetTextureMapOn(); |
2bd4c032 |
211 | |
212 | myAspect->SetTextureMap (mytexture); |
7fd59977 |
213 | if (!mytexture->IsDone()) |
2bd4c032 |
214 | { |
215 | std::cout << "An error occured while building texture \n"; |
216 | return; |
217 | } |
218 | |
7fd59977 |
219 | if (DoShowTriangles) |
220 | myAspect->SetEdgeOn(); |
221 | else |
222 | myAspect->SetEdgeOff(); |
2bd4c032 |
223 | |
224 | Prs3d_Root::CurrentGroup (aPrs)->SetGroupPrimitivesAspect (myAspect); |
7fd59977 |
225 | } |
226 | |
227 | //======================================================================= |
228 | //function : Compute |
2bd4c032 |
229 | //purpose : |
7fd59977 |
230 | //======================================================================= |
231 | |
2bd4c032 |
232 | void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePresManager*/, |
233 | const Handle(Prs3d_Presentation)& thePrs, |
234 | const Standard_Integer theMode) |
7fd59977 |
235 | { |
2bd4c032 |
236 | thePrs->Clear(); |
237 | |
238 | if (myshape.IsNull()) |
239 | { |
240 | return; |
241 | } |
242 | |
243 | if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE) |
244 | { |
245 | thePrs->SetVisual (Graphic3d_TOS_ALL); |
246 | thePrs->SetDisplayPriority (myshape.ShapeType() + 2); |
247 | } |
7fd59977 |
248 | |
249 | if (myshape.ShapeType() == TopAbs_COMPOUND) |
2bd4c032 |
250 | { |
251 | TopExp_Explorer anExplor (myshape, TopAbs_VERTEX); |
252 | if (!anExplor.More()) |
7fd59977 |
253 | { |
2bd4c032 |
254 | return; |
7fd59977 |
255 | } |
2bd4c032 |
256 | } |
7fd59977 |
257 | |
258 | if (IsInfinite()) |
2bd4c032 |
259 | { |
260 | thePrs->SetInfiniteState (Standard_True); |
261 | } |
7fd59977 |
262 | |
2bd4c032 |
263 | switch (theMode) |
264 | { |
7fd59977 |
265 | case 0: // Wireframe |
2bd4c032 |
266 | { |
267 | StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer); |
7fd59977 |
268 | break; |
2bd4c032 |
269 | } |
270 | case 1: // Shading |
271 | { |
272 | Standard_Real prevangle; |
273 | Standard_Real newangle; |
274 | Standard_Real prevcoeff; |
275 | Standard_Real newcoeff; |
7fd59977 |
276 | |
6318e884 |
277 | if (OwnDeviationAngle (newangle, prevangle) || OwnDeviationCoefficient (newcoeff, prevcoeff)) |
2bd4c032 |
278 | { |
6318e884 |
279 | if (Abs (newangle - prevangle) > Precision::Angular() || Abs (newcoeff - prevcoeff) > Precision::Confusion()) |
280 | { |
281 | BRepTools::Clean (myshape); |
282 | } |
2bd4c032 |
283 | } |
284 | if (myshape.ShapeType() > TopAbs_FACE) |
7fd59977 |
285 | { |
2bd4c032 |
286 | StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer); |
287 | break; |
7fd59977 |
288 | } |
2bd4c032 |
289 | myDrawer->SetShadingAspectGlobal (Standard_False); |
290 | if (IsInfinite()) |
291 | { |
292 | StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer); |
293 | break; |
294 | } |
295 | try |
296 | { |
297 | OCC_CATCH_SIGNALS |
298 | StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer); |
299 | } |
300 | catch (Standard_Failure) |
301 | { |
302 | std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n"; |
303 | StdPrs_WFShape::Add (thePrs, myshape, myDrawer); |
304 | } |
305 | break; |
306 | } |
7fd59977 |
307 | |
308 | case 2: // Bounding box |
2bd4c032 |
309 | { |
310 | if (IsInfinite()) |
7fd59977 |
311 | { |
2bd4c032 |
312 | StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer); |
7fd59977 |
313 | } |
2bd4c032 |
314 | else |
7fd59977 |
315 | { |
2bd4c032 |
316 | AIS_Shape::DisplayBox (thePrs, BoundingBox(), myDrawer); |
317 | } |
318 | break; |
319 | } |
320 | |
321 | case 3: // texture mapping on triangulation |
322 | { |
323 | BRepTools::Clean (myshape); |
324 | BRepTools::Update (myshape); |
7fd59977 |
325 | |
2bd4c032 |
326 | Handle(Graphic3d_StructureManager) aStrucMana = GetContext()->MainPrsMgr()->StructureManager(); |
327 | { |
328 | Handle(Prs3d_ShadingAspect) aPrs3d_ShadingAspect = new Prs3d_ShadingAspect(); |
329 | myAspect = aPrs3d_ShadingAspect->Aspect(); |
60761105 |
330 | |
331 | // Issue 23115: copy polygon offset settings passed through myDrawer |
332 | if (HasPolygonOffsets()) |
333 | { |
334 | Standard_Integer aMode; |
60be1f9b |
335 | Standard_ShortReal aFactor, aUnits; |
60761105 |
336 | PolygonOffsets(aMode, aFactor, aUnits); |
337 | myAspect->SetPolygonOffsets(aMode, aFactor, aUnits); |
338 | } |
2bd4c032 |
339 | } |
340 | if (!DoMapTexture) |
341 | { |
342 | myAspect->SetTextureMapOff(); |
343 | return; |
344 | } |
345 | myAspect->SetTextureMapOn(); |
7fd59977 |
346 | |
2bd4c032 |
347 | if (myPredefTexture != -1) |
348 | mytexture = new Graphic3d_Texture2Dmanual (aStrucMana, myPredefTexture); |
349 | else |
350 | mytexture = new Graphic3d_Texture2Dmanual (aStrucMana, myTextureFile.ToCString()); |
7fd59977 |
351 | |
2bd4c032 |
352 | if (!mytexture->IsDone()) |
353 | { |
354 | std::cout << "An error occured while building texture \n"; |
355 | return; |
356 | } |
7fd59977 |
357 | |
2bd4c032 |
358 | if (myModulate) |
359 | mytexture->EnableModulate(); |
360 | else |
361 | mytexture->DisableModulate(); |
7fd59977 |
362 | |
2bd4c032 |
363 | myAspect->SetTextureMap (mytexture); |
364 | if (DoShowTriangles) |
365 | myAspect->SetEdgeOn(); |
366 | else |
367 | myAspect->SetEdgeOff(); |
7fd59977 |
368 | |
2bd4c032 |
369 | if (DoRepeat) |
370 | mytexture->EnableRepeat(); |
371 | else |
372 | mytexture->DisableRepeat(); |
7fd59977 |
373 | |
2bd4c032 |
374 | const gp_Pnt2d aUVOrigin (myUOrigin, myVOrigin); |
375 | const gp_Pnt2d aUVRepeat (myURepeat, myVRepeat); |
376 | const gp_Pnt2d aUVScale (myScaleU, myScaleV); |
377 | try |
378 | { |
379 | OCC_CATCH_SIGNALS |
380 | StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer, |
381 | Standard_True, aUVOrigin, aUVRepeat, aUVScale); |
382 | // within primitive arrays - object should be in one group of primitives |
383 | Prs3d_Root::CurrentGroup (thePrs)->SetGroupPrimitivesAspect (myAspect); |
384 | } |
385 | catch (Standard_Failure) |
386 | { |
387 | std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n"; |
388 | StdPrs_WFShape::Add (thePrs, myshape, myDrawer); |
389 | } |
390 | break; |
391 | } |
392 | } |
393 | } |
7fd59977 |
394 | |
395 | Standard_Boolean AIS_TexturedShape::TextureMapState() const |
396 | { |
397 | return DoMapTexture; |
398 | } |
399 | |
400 | Standard_Real AIS_TexturedShape::URepeat() const |
401 | { |
402 | return myURepeat; |
403 | } |
404 | |
405 | Standard_Boolean AIS_TexturedShape::TextureRepeat() const |
406 | { |
407 | return DoRepeat; |
408 | } |
409 | |
410 | Standard_Real AIS_TexturedShape::Deflection() const |
411 | { |
412 | return myDeflection; |
413 | } |
414 | |
415 | Standard_CString AIS_TexturedShape::TextureFile() const |
416 | { |
417 | return myTextureFile.ToCString(); |
418 | } |
419 | |
420 | Standard_Real AIS_TexturedShape::VRepeat() const |
421 | { |
422 | return myVRepeat; |
423 | } |
2bd4c032 |
424 | |
7fd59977 |
425 | Standard_Boolean AIS_TexturedShape::ShowTriangles() const |
426 | { |
427 | return DoShowTriangles; |
428 | } |
2bd4c032 |
429 | |
7fd59977 |
430 | Standard_Real AIS_TexturedShape::TextureUOrigin() const |
431 | { |
432 | return myUOrigin; |
433 | } |
2bd4c032 |
434 | |
7fd59977 |
435 | Standard_Real AIS_TexturedShape::TextureVOrigin() const |
436 | { |
437 | return myVOrigin; |
438 | } |
2bd4c032 |
439 | |
7fd59977 |
440 | Standard_Real AIS_TexturedShape::TextureScaleU() const |
441 | { |
442 | return myScaleU; |
443 | } |
2bd4c032 |
444 | |
7fd59977 |
445 | Standard_Real AIS_TexturedShape::TextureScaleV() const |
446 | { |
447 | return myScaleV; |
448 | } |
2bd4c032 |
449 | |
450 | Standard_Boolean AIS_TexturedShape::TextureScale() const |
7fd59977 |
451 | { |
452 | return DoSetTextureScale; |
453 | } |
2bd4c032 |
454 | |
455 | Standard_Boolean AIS_TexturedShape::TextureOrigin() const |
7fd59977 |
456 | { |
457 | return DoSetTextureOrigin; |
458 | } |
2bd4c032 |
459 | |
460 | Standard_Boolean AIS_TexturedShape::TextureModulate() const |
7fd59977 |
461 | { |
462 | return myModulate; |
463 | } |