1 // Created on: 2001-07-02
2 // Created by: Mathias BOSSHARD
3 // Copyright (c) 2001-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <AIS_TexturedShape.hxx>
18 #include <AIS_GraphicTool.hxx>
19 #include <AIS_InteractiveContext.hxx>
20 #include <BRepTools.hxx>
21 #include <gp_Pnt2d.hxx>
22 #include <Graphic3d_AspectFillArea3d.hxx>
23 #include <Graphic3d_Group.hxx>
24 #include <Graphic3d_StructureManager.hxx>
25 #include <Graphic3d_Texture2Dmanual.hxx>
26 #include <Message.hxx>
27 #include <Message_Messenger.hxx>
28 #include <Precision.hxx>
29 #include <Prs3d_Drawer.hxx>
30 #include <Prs3d_Presentation.hxx>
31 #include <PrsMgr_ModedPresentation.hxx>
32 #include <Prs3d_Root.hxx>
33 #include <Prs3d_LineAspect.hxx>
34 #include <Prs3d_ShadingAspect.hxx>
35 #include <PrsMgr_PresentationManager3d.hxx>
36 #include <Standard_ErrorHandler.hxx>
37 #include <StdPrs_ShadedShape.hxx>
38 #include <StdPrs_ToolShadedShape.hxx>
39 #include <StdPrs_WFDeflectionShape.hxx>
40 #include <StdPrs_WFShape.hxx>
41 #include <TopExp_Explorer.hxx>
44 //=======================================================================
45 //function : AIS_TexturedShape
47 //=======================================================================
48 AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
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),
58 myToScale (Standard_True),
59 myToShowTriangles (Standard_False)
63 //=======================================================================
64 //function : SetTextureFileName
66 //=======================================================================
67 void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
69 myTexturePixMap.Nullify();
71 if (theTextureFileName.IsIntegerValue())
73 const Standard_Integer aValue = theTextureFileName.IntegerValue();
74 if (aValue < Graphic3d_Texture2D::NumberOfTextures()
77 myPredefTexture = Graphic3d_NameOfTexture2D (aValue);
81 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: texture with ID ") + theTextureFileName
82 + " is undefined! Texture 0 will be used instead.", Message_Fail);
83 myPredefTexture = Graphic3d_NameOfTexture2D (0);
89 myTextureFile = theTextureFileName;
90 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
94 //=======================================================================
95 //function : SetTexturePixMap
97 //=======================================================================
98 void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap)
101 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
102 myTexturePixMap = theTexturePixMap;
105 //=======================================================================
106 //function : SetTextureRepeat
108 //=======================================================================
110 void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat,
111 const Standard_Real theURepeat,
112 const Standard_Real theVRepeat)
114 myToRepeat = theToRepeat;
115 myUVRepeat.SetCoord (theURepeat, theVRepeat);
118 //=======================================================================
119 //function : SetTextureMapOn
121 //=======================================================================
123 void AIS_TexturedShape::SetTextureMapOn()
125 myToMapTexture = Standard_True;
128 //=======================================================================
129 //function : SetTextureMapOff
131 //=======================================================================
133 void AIS_TexturedShape::SetTextureMapOff()
135 myToMapTexture = Standard_False;
138 //=======================================================================
139 //function : SetTextureOrigin
141 //=======================================================================
143 void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin,
144 const Standard_Real theUOrigin,
145 const Standard_Real theVOrigin)
147 myIsCustomOrigin = theToSetTextureOrigin;
148 myUVOrigin.SetCoord (theUOrigin, theVOrigin);
151 //=======================================================================
152 //function : SetTextureScale
154 //=======================================================================
156 void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale,
157 const Standard_Real theScaleU,
158 const Standard_Real theScaleV)
160 myToScale = theToSetTextureScale;
161 myUVScale.SetCoord (theScaleU, theScaleV);
164 //=======================================================================
165 //function : ShowTriangles
167 //=======================================================================
169 void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles)
171 myToShowTriangles = theToShowTriangles;
174 //=======================================================================
175 //function : EnableTextureModulate
177 //=======================================================================
179 void AIS_TexturedShape::EnableTextureModulate()
181 myModulate = Standard_True;
184 //=======================================================================
185 //function : DisableTextureModulate
187 //=======================================================================
189 void AIS_TexturedShape::DisableTextureModulate()
191 myModulate = Standard_False;
194 //=======================================================================
195 //function : SetColor
197 //=======================================================================
199 void AIS_TexturedShape::SetColor (const Quantity_Color& theColor)
201 AIS_Shape::SetColor (theColor);
203 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
205 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
207 if (aPrsModed.Mode() != 3)
210 updateAttributes (aPrsModed.Presentation()->Presentation());
214 //=======================================================================
215 //function : UnsetColor
217 //=======================================================================
219 void AIS_TexturedShape::UnsetColor()
221 AIS_Shape::UnsetColor();
223 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
225 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
227 if (aPrsModed.Mode() != 3)
230 Handle(Prs3d_Presentation) aPrs = aPrsModed.Presentation()->Presentation();
231 Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (aPrs);
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))
245 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
247 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_LINE))
249 aGroup->SetGroupPrimitivesAspect (aLineAsp);
252 updateAttributes (aPrs);
256 //=======================================================================
257 //function : SetMaterial
259 //=======================================================================
261 void AIS_TexturedShape::SetMaterial (const Graphic3d_MaterialAspect& theMat)
263 AIS_Shape::SetMaterial (theMat);
265 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
267 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
269 if (aPrsModed.Mode() != 3)
272 updateAttributes (aPrsModed.Presentation()->Presentation());
276 //=======================================================================
277 //function : UnsetMaterial
279 //=======================================================================
280 void AIS_TexturedShape::UnsetMaterial()
282 AIS_Shape::UnsetMaterial();
284 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
286 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
288 if (aPrsModed.Mode() != 3)
291 updateAttributes (aPrsModed.Presentation()->Presentation());
295 //=======================================================================
296 //function : UpdateAttributes
298 //=======================================================================
300 void AIS_TexturedShape::UpdateAttributes()
302 updateAttributes (Presentation());
305 //=======================================================================
306 //function : updateAttributes
308 //=======================================================================
310 void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs)
312 myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect());
313 if (HasPolygonOffsets())
315 Standard_Integer aMode;
316 Standard_ShortReal aFactor, aUnits;
317 PolygonOffsets (aMode, aFactor, aUnits);
318 myAspect->SetPolygonOffsets (aMode, aFactor, aUnits);
321 Standard_Boolean hasTexture = Standard_False;
324 TCollection_AsciiString aTextureDesc;
325 if (!myTexturePixMap.IsNull())
327 myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
328 aTextureDesc = " (custom image)";
330 else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
332 myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
333 aTextureDesc = TCollection_AsciiString(" (predefined texture ") + myTexture->GetId() + ")";
337 myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
338 aTextureDesc = TCollection_AsciiString(" (") + myTextureFile + ")";
343 myTexture->EnableModulate();
347 myTexture->DisableModulate();
350 if (myTexture->IsDone())
352 hasTexture = Standard_True;
356 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: texture can not be loaded ") + aTextureDesc, Message_Fail);
360 myAspect->SetTextureMap (myTexture);
363 myAspect->SetTextureMapOn();
367 myAspect->SetTextureMapOff();
370 if (myToShowTriangles)
372 myAspect->SetEdgeOn();
376 myAspect->SetEdgeOff();
379 // Go through all groups to change fill aspect for all primitives
380 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (thePrs->Groups()); aGroupIt.More(); aGroupIt.Next())
382 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
383 if (!aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
388 if (aGroup->IsClosed())
390 myAspect->SuppressBackFace();
394 myAspect->AllowBackFace();
397 aGroup->SetGroupPrimitivesAspect (myAspect);
401 //=======================================================================
404 //=======================================================================
406 void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
407 const Handle(Prs3d_Presentation)& thePrs,
408 const Standard_Integer theMode)
412 if (myshape.IsNull())
417 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
419 thePrs->SetVisual (Graphic3d_TOS_ALL);
420 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
423 if (myshape.ShapeType() == TopAbs_COMPOUND)
425 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
426 if (!anExplor.More())
434 thePrs->SetInfiniteState (Standard_True);
441 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
445 case 3: // texture mapping on triangulation
447 if (myDrawer->IsAutoTriangulation())
449 Standard_Real aPrevAngle;
450 Standard_Real aNewAngle;
451 Standard_Real aPrevCoeff;
452 Standard_Real aNewCoeff;
454 Standard_Boolean isOwnDeviationAngle = OwnDeviationAngle (aNewAngle, aPrevAngle);
455 Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient (aNewCoeff,aPrevCoeff);
456 if (((Abs (aNewAngle - aPrevAngle) > Precision::Angular()) && isOwnDeviationAngle) ||
457 ((Abs (aNewCoeff - aPrevCoeff) > Precision::Confusion()) && isOwnDeviationCoefficient))
459 BRepTools::Clean (myshape);
463 if (myshape.ShapeType() > TopAbs_FACE)
465 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
469 myDrawer->SetShadingAspectGlobal (Standard_False);
472 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
478 if (theMode == AIS_Shaded)
480 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
484 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
486 myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),
488 myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0));
489 updateAttributes (thePrs);
492 catch (Standard_Failure)
495 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
497 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
501 case 2: // Bounding box
505 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
509 StdPrs_WFDeflectionRestrictedFace::AddBox (thePrs, BoundingBox(), myDrawer);