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_Drawer.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 <Precision.hxx>
27 #include <Prs3d_Presentation.hxx>
28 #include <Prs3d_Root.hxx>
29 #include <Prs3d_ShadingAspect.hxx>
30 #include <PrsMgr_PresentationManager3d.hxx>
31 #include <Standard_ErrorHandler.hxx>
32 #include <StdPrs_ShadedShape.hxx>
33 #include <StdPrs_WFDeflectionShape.hxx>
34 #include <StdPrs_WFShape.hxx>
35 #include <TopExp_Explorer.hxx>
37 IMPLEMENT_STANDARD_HANDLE (AIS_TexturedShape, AIS_Shape)
38 IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape, AIS_Shape)
40 //=======================================================================
41 //function : AIS_TexturedShape
43 //=======================================================================
44 AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
45 : AIS_Shape (theShape),
46 myPredefTexture (Graphic3d_NameOfTexture2D(0)),
47 myToMapTexture (Standard_True),
48 myModulate (Standard_True),
49 myUVOrigin (0.0, 0.0),
50 myIsCustomOrigin (Standard_True),
51 myUVRepeat (1.0, 1.0),
52 myToRepeat (Standard_True),
54 myToScale (Standard_True),
55 myToShowTriangles (Standard_False)
59 //=======================================================================
60 //function : SetTextureFileName
62 //=======================================================================
63 void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
65 myTexturePixMap.Nullify();
67 if (theTextureFileName.IsIntegerValue())
69 const Standard_Integer aValue = theTextureFileName.IntegerValue();
70 if (aValue < Graphic3d_Texture2D::NumberOfTextures()
73 myPredefTexture = Graphic3d_NameOfTexture2D (aValue);
77 std::cout << "Texture " << theTextureFileName << " doesn't exist\n";
78 std::cout << "Using Texture 0 instead ...\n";
79 myPredefTexture = Graphic3d_NameOfTexture2D (0);
85 myTextureFile = theTextureFileName;
86 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
90 //=======================================================================
91 //function : SetTexturePixMap
93 //=======================================================================
94 void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap)
97 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
98 myTexturePixMap = theTexturePixMap;
101 //=======================================================================
102 //function : SetTextureRepeat
104 //=======================================================================
106 void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat,
107 const Standard_Real theURepeat,
108 const Standard_Real theVRepeat)
110 myToRepeat = theToRepeat;
111 myUVRepeat.SetCoord (theURepeat, theVRepeat);
114 //=======================================================================
115 //function : SetTextureMapOn
117 //=======================================================================
119 void AIS_TexturedShape::SetTextureMapOn()
121 myToMapTexture = Standard_True;
124 //=======================================================================
125 //function : SetTextureMapOff
127 //=======================================================================
129 void AIS_TexturedShape::SetTextureMapOff()
131 myToMapTexture = Standard_False;
134 //=======================================================================
135 //function : SetTextureOrigin
137 //=======================================================================
139 void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin,
140 const Standard_Real theUOrigin,
141 const Standard_Real theVOrigin)
143 myIsCustomOrigin = theToSetTextureOrigin;
144 myUVOrigin.SetCoord (theUOrigin, theVOrigin);
147 //=======================================================================
148 //function : SetTextureScale
150 //=======================================================================
152 void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale,
153 const Standard_Real theScaleU,
154 const Standard_Real theScaleV)
156 myToScale = theToSetTextureScale;
157 myUVScale.SetCoord (theScaleU, theScaleV);
160 //=======================================================================
161 //function : ShowTriangles
163 //=======================================================================
165 void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles)
167 myToShowTriangles = theToShowTriangles;
170 //=======================================================================
171 //function : EnableTextureModulate
173 //=======================================================================
175 void AIS_TexturedShape::EnableTextureModulate()
177 myModulate = Standard_True;
180 //=======================================================================
181 //function : DisableTextureModulate
183 //=======================================================================
185 void AIS_TexturedShape::DisableTextureModulate()
187 myModulate = Standard_False;
190 //=======================================================================
191 //function : UpdateAttributes
193 //=======================================================================
195 void AIS_TexturedShape::UpdateAttributes()
197 Prs3d_ShadingAspect aDummy;
198 myAspect = aDummy.Aspect();
199 Handle(Prs3d_Presentation) aPrs = Presentation();
202 myAspect->SetTextureMapOff();
206 if (!myTexturePixMap.IsNull())
208 myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
210 else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
212 myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
216 myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
219 myAspect->SetTextureMapOn();
221 myAspect->SetTextureMap (myTexture);
222 if (!myTexture->IsDone())
224 std::cout << "An error occurred while building texture \n";
228 if (myToShowTriangles)
229 myAspect->SetEdgeOn();
231 myAspect->SetEdgeOff();
233 Prs3d_Root::CurrentGroup (aPrs)->SetGroupPrimitivesAspect (myAspect);
236 //=======================================================================
239 //=======================================================================
241 void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
242 const Handle(Prs3d_Presentation)& thePrs,
243 const Standard_Integer theMode)
247 if (myshape.IsNull())
252 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
254 thePrs->SetVisual (Graphic3d_TOS_ALL);
255 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
258 if (myshape.ShapeType() == TopAbs_COMPOUND)
260 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
261 if (!anExplor.More())
269 thePrs->SetInfiniteState (Standard_True);
276 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
281 Standard_Real prevangle;
282 Standard_Real newangle;
283 Standard_Real prevcoeff;
284 Standard_Real newcoeff;
286 Standard_Boolean isOwnDeviationAngle = OwnDeviationAngle(newangle,prevangle);
287 Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient(newcoeff,prevcoeff);
288 if (((Abs (newangle - prevangle) > Precision::Angular()) && isOwnDeviationAngle) ||
289 ((Abs (newcoeff - prevcoeff) > Precision::Confusion()) && isOwnDeviationCoefficient)) {
290 BRepTools::Clean (myshape);
292 if (myshape.ShapeType() > TopAbs_FACE)
294 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
297 myDrawer->SetShadingAspectGlobal (Standard_False);
300 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
306 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
308 catch (Standard_Failure)
310 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
311 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
315 case 2: // Bounding box
319 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
323 AIS_Shape::DisplayBox (thePrs, BoundingBox(), myDrawer);
327 case 3: // texture mapping on triangulation
329 BRepTools::Clean (myshape);
330 BRepTools::Update (myshape);
333 Handle(Prs3d_ShadingAspect) aPrs3d_ShadingAspect = new Prs3d_ShadingAspect();
334 myAspect = aPrs3d_ShadingAspect->Aspect();
336 // Issue 23115: copy polygon offset settings passed through myDrawer
337 if (HasPolygonOffsets())
339 Standard_Integer aMode;
340 Standard_ShortReal aFactor, aUnits;
341 PolygonOffsets(aMode, aFactor, aUnits);
342 myAspect->SetPolygonOffsets(aMode, aFactor, aUnits);
347 myAspect->SetTextureMapOff();
350 myAspect->SetTextureMapOn();
352 if (!myTexturePixMap.IsNull())
354 myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
356 else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
358 myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
362 myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
365 if (!myTexture->IsDone())
367 std::cout << "An error occurred while building texture \n";
372 myTexture->EnableModulate();
374 myTexture->DisableModulate();
376 myAspect->SetTextureMap (myTexture);
377 if (myToShowTriangles)
378 myAspect->SetEdgeOn();
380 myAspect->SetEdgeOff();
383 myTexture->EnableRepeat();
385 myTexture->DisableRepeat();
390 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
392 myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),
394 myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0));
395 // within primitive arrays - object should be in one group of primitives
396 Prs3d_Root::CurrentGroup (thePrs)->SetGroupPrimitivesAspect (myAspect);
398 catch (Standard_Failure)
400 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
401 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);