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_ToolShadedShape.hxx>
34 #include <StdPrs_WFDeflectionShape.hxx>
35 #include <StdPrs_WFShape.hxx>
36 #include <TopExp_Explorer.hxx>
38 IMPLEMENT_STANDARD_HANDLE (AIS_TexturedShape, AIS_Shape)
39 IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape, AIS_Shape)
41 //=======================================================================
42 //function : AIS_TexturedShape
44 //=======================================================================
45 AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
46 : AIS_Shape (theShape),
47 myPredefTexture (Graphic3d_NameOfTexture2D(0)),
48 myToMapTexture (Standard_True),
49 myModulate (Standard_True),
50 myUVOrigin (0.0, 0.0),
51 myIsCustomOrigin (Standard_True),
52 myUVRepeat (1.0, 1.0),
53 myToRepeat (Standard_True),
55 myToScale (Standard_True),
56 myToShowTriangles (Standard_False)
60 //=======================================================================
61 //function : SetTextureFileName
63 //=======================================================================
64 void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
66 myTexturePixMap.Nullify();
68 if (theTextureFileName.IsIntegerValue())
70 const Standard_Integer aValue = theTextureFileName.IntegerValue();
71 if (aValue < Graphic3d_Texture2D::NumberOfTextures()
74 myPredefTexture = Graphic3d_NameOfTexture2D (aValue);
78 std::cout << "Texture " << theTextureFileName << " doesn't exist\n";
79 std::cout << "Using Texture 0 instead ...\n";
80 myPredefTexture = Graphic3d_NameOfTexture2D (0);
86 myTextureFile = theTextureFileName;
87 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
91 //=======================================================================
92 //function : SetTexturePixMap
94 //=======================================================================
95 void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap)
98 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
99 myTexturePixMap = theTexturePixMap;
102 //=======================================================================
103 //function : SetTextureRepeat
105 //=======================================================================
107 void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat,
108 const Standard_Real theURepeat,
109 const Standard_Real theVRepeat)
111 myToRepeat = theToRepeat;
112 myUVRepeat.SetCoord (theURepeat, theVRepeat);
115 //=======================================================================
116 //function : SetTextureMapOn
118 //=======================================================================
120 void AIS_TexturedShape::SetTextureMapOn()
122 myToMapTexture = Standard_True;
125 //=======================================================================
126 //function : SetTextureMapOff
128 //=======================================================================
130 void AIS_TexturedShape::SetTextureMapOff()
132 myToMapTexture = Standard_False;
135 //=======================================================================
136 //function : SetTextureOrigin
138 //=======================================================================
140 void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin,
141 const Standard_Real theUOrigin,
142 const Standard_Real theVOrigin)
144 myIsCustomOrigin = theToSetTextureOrigin;
145 myUVOrigin.SetCoord (theUOrigin, theVOrigin);
148 //=======================================================================
149 //function : SetTextureScale
151 //=======================================================================
153 void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale,
154 const Standard_Real theScaleU,
155 const Standard_Real theScaleV)
157 myToScale = theToSetTextureScale;
158 myUVScale.SetCoord (theScaleU, theScaleV);
161 //=======================================================================
162 //function : ShowTriangles
164 //=======================================================================
166 void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles)
168 myToShowTriangles = theToShowTriangles;
171 //=======================================================================
172 //function : EnableTextureModulate
174 //=======================================================================
176 void AIS_TexturedShape::EnableTextureModulate()
178 myModulate = Standard_True;
181 //=======================================================================
182 //function : DisableTextureModulate
184 //=======================================================================
186 void AIS_TexturedShape::DisableTextureModulate()
188 myModulate = Standard_False;
191 //=======================================================================
192 //function : SetColor
194 //=======================================================================
196 void AIS_TexturedShape::SetColor (const Quantity_Color& theColor)
198 AIS_Shape::SetColor (theColor);
200 if (!GetContext().IsNull())
202 if (GetContext()->MainPrsMgr()->HasPresentation (this, 3))
204 updateAttributes (GetContext()->MainPrsMgr()->Presentation (this, 3)->Presentation());
209 //=======================================================================
210 //function : UpdateAttributes
212 //=======================================================================
214 void AIS_TexturedShape::UpdateAttributes()
216 updateAttributes (Presentation());
219 //=======================================================================
220 //function : updateAttributes
222 //=======================================================================
224 void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs)
226 myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect());
227 if (HasPolygonOffsets())
229 // Issue 23115: copy polygon offset settings passed through myDrawer
230 Standard_Integer aMode;
231 Standard_ShortReal aFactor, aUnits;
232 PolygonOffsets (aMode, aFactor, aUnits);
233 myAspect->SetPolygonOffsets (aMode, aFactor, aUnits);
238 myAspect->SetTextureMapOff();
242 if (!myTexturePixMap.IsNull())
244 myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
246 else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
248 myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
252 myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
255 myAspect->SetTextureMapOn();
256 myAspect->SetTextureMap (myTexture);
257 if (!myTexture->IsDone())
259 std::cout << "An error occurred while building texture\n";
260 myAspect->SetTextureMapOff();
265 myTexture->EnableModulate();
267 myTexture->DisableModulate();
269 if (myToShowTriangles)
270 myAspect->SetEdgeOn();
272 myAspect->SetEdgeOff();
274 // manage back face culling in consistent way (as in StdPrs_ShadedShape::Add())
275 if (StdPrs_ToolShadedShape::IsClosed (myshape))
277 myAspect->SuppressBackFace();
281 myAspect->AllowBackFace();
284 if (!thePrs.IsNull())
286 Prs3d_Root::CurrentGroup (thePrs)->SetGroupPrimitivesAspect (myAspect);
290 //=======================================================================
293 //=======================================================================
295 void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
296 const Handle(Prs3d_Presentation)& thePrs,
297 const Standard_Integer theMode)
301 if (myshape.IsNull())
306 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
308 thePrs->SetVisual (Graphic3d_TOS_ALL);
309 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
312 if (myshape.ShapeType() == TopAbs_COMPOUND)
314 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
315 if (!anExplor.More())
323 thePrs->SetInfiniteState (Standard_True);
330 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
335 Standard_Real prevangle;
336 Standard_Real newangle;
337 Standard_Real prevcoeff;
338 Standard_Real newcoeff;
340 Standard_Boolean isOwnDeviationAngle = OwnDeviationAngle(newangle,prevangle);
341 Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient(newcoeff,prevcoeff);
342 if (((Abs (newangle - prevangle) > Precision::Angular()) && isOwnDeviationAngle) ||
343 ((Abs (newcoeff - prevcoeff) > Precision::Confusion()) && isOwnDeviationCoefficient)) {
344 BRepTools::Clean (myshape);
346 if (myshape.ShapeType() > TopAbs_FACE)
348 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
351 myDrawer->SetShadingAspectGlobal (Standard_False);
354 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
360 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
362 catch (Standard_Failure)
364 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
365 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
369 case 2: // Bounding box
373 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
377 AIS_Shape::DisplayBox (thePrs, BoundingBox(), myDrawer);
381 case 3: // texture mapping on triangulation
383 BRepTools::Clean (myshape);
384 BRepTools::Update (myshape);
388 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
390 myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),
392 myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0));
394 updateAttributes (thePrs);
396 catch (Standard_Failure)
398 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed\n";
399 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);