0024930: Visualization - SetMaterial/UnsetMaterial/UnsetColor are not implemented...
[occt.git] / src / AIS / AIS_TexturedShape.cxx
CommitLineData
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
2bd4c032 18#include <AIS_Drawer.hxx>
735dc1d2 19#include <AIS_GraphicTool.hxx>
2bd4c032 20#include <AIS_InteractiveContext.hxx>
7fd59977 21#include <BRepTools.hxx>
2bd4c032 22#include <gp_Pnt2d.hxx>
23#include <Graphic3d_AspectFillArea3d.hxx>
24#include <Graphic3d_Group.hxx>
7fd59977 25#include <Graphic3d_StructureManager.hxx>
26#include <Graphic3d_Texture2Dmanual.hxx>
2bd4c032 27#include <Precision.hxx>
28#include <Prs3d_Presentation.hxx>
735dc1d2 29#include <PrsMgr_ModedPresentation.hxx>
7fd59977 30#include <Prs3d_Root.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>
2bd4c032 35#include <StdPrs_ShadedShape.hxx>
8e3ebc7a 36#include <StdPrs_ToolShadedShape.hxx>
7fd59977 37#include <StdPrs_WFDeflectionShape.hxx>
7fd59977 38#include <StdPrs_WFShape.hxx>
2bd4c032 39#include <TopExp_Explorer.hxx>
7fd59977 40
f376ac72 41IMPLEMENT_STANDARD_HANDLE (AIS_TexturedShape, AIS_Shape)
42IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape, AIS_Shape)
43
7fd59977 44//=======================================================================
45//function : AIS_TexturedShape
2bd4c032 46//purpose :
7fd59977 47//=======================================================================
2bd4c032 48AIS_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),
53 myUVOrigin (0.0, 0.0),
54 myIsCustomOrigin (Standard_True),
55 myUVRepeat (1.0, 1.0),
56 myToRepeat (Standard_True),
57 myUVScale (1.0, 1.0),
58 myToScale (Standard_True),
59 myToShowTriangles (Standard_False)
7fd59977 60{
61}
62
7fd59977 63//=======================================================================
64//function : SetTextureFileName
2bd4c032 65//purpose :
7fd59977 66//=======================================================================
2bd4c032 67void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
7fd59977 68{
f376ac72 69 myTexturePixMap.Nullify();
70
2bd4c032 71 if (theTextureFileName.IsIntegerValue())
72 {
f376ac72 73 const Standard_Integer aValue = theTextureFileName.IntegerValue();
74 if (aValue < Graphic3d_Texture2D::NumberOfTextures()
75 && aValue >= 0)
7fd59977 76 {
f376ac72 77 myPredefTexture = Graphic3d_NameOfTexture2D (aValue);
7fd59977 78 }
2bd4c032 79 else
7fd59977 80 {
f376ac72 81 std::cout << "Texture " << theTextureFileName << " doesn't exist\n";
2bd4c032 82 std::cout << "Using Texture 0 instead ...\n";
83 myPredefTexture = Graphic3d_NameOfTexture2D (0);
7fd59977 84 }
2bd4c032 85 myTextureFile = "";
86 }
87 else
88 {
89 myTextureFile = theTextureFileName;
f376ac72 90 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
2bd4c032 91 }
7fd59977 92}
93
f376ac72 94//=======================================================================
95//function : SetTexturePixMap
96//purpose :
97//=======================================================================
98void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap)
99{
100 myTextureFile = "";
101 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
102 myTexturePixMap = theTexturePixMap;
103}
104
7fd59977 105//=======================================================================
106//function : SetTextureRepeat
2bd4c032 107//purpose :
7fd59977 108//=======================================================================
109
f376ac72 110void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat,
2bd4c032 111 const Standard_Real theURepeat,
112 const Standard_Real theVRepeat)
7fd59977 113{
f376ac72 114 myToRepeat = theToRepeat;
115 myUVRepeat.SetCoord (theURepeat, theVRepeat);
7fd59977 116}
117
118//=======================================================================
119//function : SetTextureMapOn
2bd4c032 120//purpose :
7fd59977 121//=======================================================================
122
123void AIS_TexturedShape::SetTextureMapOn()
124{
f376ac72 125 myToMapTexture = Standard_True;
7fd59977 126}
127
128//=======================================================================
129//function : SetTextureMapOff
2bd4c032 130//purpose :
7fd59977 131//=======================================================================
132
133void AIS_TexturedShape::SetTextureMapOff()
134{
f376ac72 135 myToMapTexture = Standard_False;
7fd59977 136}
137
138//=======================================================================
139//function : SetTextureOrigin
2bd4c032 140//purpose :
7fd59977 141//=======================================================================
142
f376ac72 143void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin,
2bd4c032 144 const Standard_Real theUOrigin,
145 const Standard_Real theVOrigin)
7fd59977 146{
f376ac72 147 myIsCustomOrigin = theToSetTextureOrigin;
148 myUVOrigin.SetCoord (theUOrigin, theVOrigin);
7fd59977 149}
150
151//=======================================================================
152//function : SetTextureScale
2bd4c032 153//purpose :
7fd59977 154//=======================================================================
155
f376ac72 156void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale,
2bd4c032 157 const Standard_Real theScaleU,
158 const Standard_Real theScaleV)
7fd59977 159{
f376ac72 160 myToScale = theToSetTextureScale;
161 myUVScale.SetCoord (theScaleU, theScaleV);
7fd59977 162}
163
164//=======================================================================
165//function : ShowTriangles
2bd4c032 166//purpose :
7fd59977 167//=======================================================================
168
f376ac72 169void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles)
7fd59977 170{
f376ac72 171 myToShowTriangles = theToShowTriangles;
7fd59977 172}
173
174//=======================================================================
175//function : EnableTextureModulate
2bd4c032 176//purpose :
7fd59977 177//=======================================================================
178
179void AIS_TexturedShape::EnableTextureModulate()
180{
181 myModulate = Standard_True;
182}
183
184//=======================================================================
185//function : DisableTextureModulate
bf75be98 186//purpose :
7fd59977 187//=======================================================================
188
189void AIS_TexturedShape::DisableTextureModulate()
190{
191 myModulate = Standard_False;
192}
193
7389f96d 194//=======================================================================
195//function : SetColor
196//purpose :
197//=======================================================================
198
199void AIS_TexturedShape::SetColor (const Quantity_Color& theColor)
200{
201 AIS_Shape::SetColor (theColor);
202
735dc1d2 203 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
7389f96d 204 {
735dc1d2 205 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
206
207 if (aPrsModed.Mode() != 3)
208 continue;
209
210 updateAttributes (aPrsModed.Presentation()->Presentation());
211 }
212}
213
214//=======================================================================
215//function : UnsetColor
216//purpose :
217//=======================================================================
218
219void AIS_TexturedShape::UnsetColor()
220{
221 AIS_Shape::UnsetColor();
222
223 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
224 {
225 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
226
227 if (aPrsModed.Mode() != 3)
228 continue;
229
230 Handle(Prs3d_Presentation) aPrs = aPrsModed.Presentation()->Presentation();
231 Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (aPrs);
232
233 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->Link()->ShadingAspect()->Aspect();
234 Handle(Graphic3d_AspectLine3d) aLineAsp = myDrawer->Link()->LineAspect()->Aspect();
235 Quantity_Color aColor;
236 AIS_GraphicTool::GetInteriorColor (myDrawer->Link(), aColor);
237 anAreaAsp->SetInteriorColor (aColor);
238 aPrs->SetPrimitivesAspect (anAreaAsp);
239 aPrs->SetPrimitivesAspect (aLineAsp);
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
261void 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//=======================================================================
280void 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
300void AIS_TexturedShape::UpdateAttributes()
8e3ebc7a 301{
302 updateAttributes (Presentation());
303}
304
305//=======================================================================
306//function : updateAttributes
307//purpose :
308//=======================================================================
309
310void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs)
7fd59977 311{
7389f96d 312 myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect());
8e3ebc7a 313 if (HasPolygonOffsets())
314 {
315 // Issue 23115: copy polygon offset settings passed through myDrawer
316 Standard_Integer aMode;
317 Standard_ShortReal aFactor, aUnits;
318 PolygonOffsets (aMode, aFactor, aUnits);
319 myAspect->SetPolygonOffsets (aMode, aFactor, aUnits);
320 }
321
f376ac72 322 if (!myToMapTexture)
2bd4c032 323 {
324 myAspect->SetTextureMapOff();
325 return;
326 }
327
f376ac72 328 if (!myTexturePixMap.IsNull())
329 {
330 myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
331 }
332 else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
333 {
334 myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
335 }
7fd59977 336 else
f376ac72 337 {
338 myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
339 }
2bd4c032 340
7fd59977 341 myAspect->SetTextureMapOn();
f376ac72 342 myAspect->SetTextureMap (myTexture);
343 if (!myTexture->IsDone())
2bd4c032 344 {
8e3ebc7a 345 std::cout << "An error occurred while building texture\n";
346 myAspect->SetTextureMapOff();
2bd4c032 347 return;
348 }
349
d399d3c3 350 if (myModulate)
351 myTexture->EnableModulate();
352 else
353 myTexture->DisableModulate();
354
f376ac72 355 if (myToShowTriangles)
7fd59977 356 myAspect->SetEdgeOn();
357 else
358 myAspect->SetEdgeOff();
2bd4c032 359
8e3ebc7a 360 // manage back face culling in consistent way (as in StdPrs_ShadedShape::Add())
361 if (StdPrs_ToolShadedShape::IsClosed (myshape))
362 {
363 myAspect->SuppressBackFace();
364 }
365 else
366 {
367 myAspect->AllowBackFace();
368 }
369
735dc1d2 370 // Go through all groups to change fill aspect for all primitives
371 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (thePrs->Groups()); aGroupIt.More(); aGroupIt.Next())
8e3ebc7a 372 {
735dc1d2 373 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
374
375 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
376 {
377 aGroup->SetGroupPrimitivesAspect (myAspect);
378 }
8e3ebc7a 379 }
7fd59977 380}
381
382//=======================================================================
383//function : Compute
2bd4c032 384//purpose :
7fd59977 385//=======================================================================
386
f376ac72 387void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
2bd4c032 388 const Handle(Prs3d_Presentation)& thePrs,
389 const Standard_Integer theMode)
7fd59977 390{
2bd4c032 391 thePrs->Clear();
392
393 if (myshape.IsNull())
394 {
395 return;
396 }
397
398 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
399 {
400 thePrs->SetVisual (Graphic3d_TOS_ALL);
401 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
402 }
7fd59977 403
404 if (myshape.ShapeType() == TopAbs_COMPOUND)
2bd4c032 405 {
406 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
407 if (!anExplor.More())
7fd59977 408 {
2bd4c032 409 return;
7fd59977 410 }
2bd4c032 411 }
7fd59977 412
413 if (IsInfinite())
2bd4c032 414 {
415 thePrs->SetInfiniteState (Standard_True);
416 }
7fd59977 417
2bd4c032 418 switch (theMode)
419 {
f376ac72 420 case AIS_WireFrame:
2bd4c032 421 {
422 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
7fd59977 423 break;
2bd4c032 424 }
f376ac72 425 case AIS_Shaded:
2bd4c032 426 {
427 Standard_Real prevangle;
428 Standard_Real newangle;
429 Standard_Real prevcoeff;
430 Standard_Real newcoeff;
7fd59977 431
bbf847ad
P
432 Standard_Boolean isOwnDeviationAngle = OwnDeviationAngle(newangle,prevangle);
433 Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient(newcoeff,prevcoeff);
434 if (((Abs (newangle - prevangle) > Precision::Angular()) && isOwnDeviationAngle) ||
435 ((Abs (newcoeff - prevcoeff) > Precision::Confusion()) && isOwnDeviationCoefficient)) {
436 BRepTools::Clean (myshape);
2bd4c032 437 }
438 if (myshape.ShapeType() > TopAbs_FACE)
7fd59977 439 {
2bd4c032 440 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
441 break;
7fd59977 442 }
2bd4c032 443 myDrawer->SetShadingAspectGlobal (Standard_False);
444 if (IsInfinite())
445 {
446 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
447 break;
448 }
449 try
450 {
451 OCC_CATCH_SIGNALS
452 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
453 }
454 catch (Standard_Failure)
455 {
456 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
457 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
458 }
459 break;
460 }
7fd59977 461 case 2: // Bounding box
2bd4c032 462 {
463 if (IsInfinite())
7fd59977 464 {
2bd4c032 465 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
7fd59977 466 }
2bd4c032 467 else
7fd59977 468 {
d33222c1 469 StdPrs_WFDeflectionRestrictedFace::AddBox (thePrs, BoundingBox(), myDrawer);
2bd4c032 470 }
471 break;
472 }
2bd4c032 473 case 3: // texture mapping on triangulation
474 {
f376ac72 475 BRepTools::Clean (myshape);
2bd4c032 476 BRepTools::Update (myshape);
2bd4c032 477 try
478 {
479 OCC_CATCH_SIGNALS
480 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
f376ac72 481 Standard_True,
482 myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),
483 myUVRepeat,
484 myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0));
8e3ebc7a 485
486 updateAttributes (thePrs);
2bd4c032 487 }
488 catch (Standard_Failure)
489 {
8e3ebc7a 490 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed\n";
2bd4c032 491 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
492 }
493 break;
494 }
495 }
496}