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.
17 ////////////////////////////////////////////////////////////////////////
20 #include <AIS_TexturedShape.ixx>
21 #include <Standard_ErrorHandler.hxx>
23 #include <AIS_Drawer.hxx>
24 #include <AIS_InteractiveContext.hxx>
25 #include <BRepTools.hxx>
26 #include <gp_Pnt2d.hxx>
27 #include <Graphic3d_AspectFillArea3d.hxx>
28 #include <Graphic3d_Group.hxx>
29 #include <Graphic3d_StructureManager.hxx>
30 #include <Graphic3d_Texture2Dmanual.hxx>
31 #include <Precision.hxx>
32 #include <Prs3d_Presentation.hxx>
33 #include <Prs3d_Root.hxx>
34 #include <Prs3d_ShadingAspect.hxx>
35 #include <PrsMgr_PresentationManager3d.hxx>
36 #include <StdPrs_ShadedShape.hxx>
37 #include <StdPrs_WFDeflectionShape.hxx>
38 #include <StdPrs_WFShape.hxx>
39 #include <TopExp_Explorer.hxx>
41 //=======================================================================
42 //function : AIS_TexturedShape
44 //=======================================================================
45 AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
46 : AIS_Shape (theShape),
47 myPredefTexture (Graphic3d_NameOfTexture2D(0)),
49 DoRepeat (Standard_True),
52 DoMapTexture (Standard_True),
53 DoSetTextureOrigin (Standard_True),
56 DoSetTextureScale (Standard_True),
59 DoShowTriangles (Standard_False),
60 myModulate (Standard_True)
64 //=======================================================================
65 //function : SetTextureFileName
67 //=======================================================================
68 void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
70 if (theTextureFileName.IsIntegerValue())
72 if (theTextureFileName.IntegerValue() < Graphic3d_Texture2D::NumberOfTextures()
73 && theTextureFileName.IntegerValue() >= 0)
75 myPredefTexture = Graphic3d_NameOfTexture2D (theTextureFileName.IntegerValue());
79 std::cout << "Texture " << theTextureFileName << " doesn't exist \n";
80 std::cout << "Using Texture 0 instead ...\n";
81 myPredefTexture = Graphic3d_NameOfTexture2D (0);
87 myTextureFile = theTextureFileName;
88 myPredefTexture = Graphic3d_NameOfTexture2D (-1);
92 //=======================================================================
93 //function : SetTextureRepeat
95 //=======================================================================
97 void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theRepeatYN,
98 const Standard_Real theURepeat,
99 const Standard_Real theVRepeat)
101 DoRepeat = theRepeatYN;
102 myURepeat = theURepeat;
103 myVRepeat = theVRepeat;
106 //=======================================================================
107 //function : SetTextureMapOn
109 //=======================================================================
111 void AIS_TexturedShape::SetTextureMapOn()
113 DoMapTexture = Standard_True;
116 //=======================================================================
117 //function : SetTextureMapOff
119 //=======================================================================
121 void AIS_TexturedShape::SetTextureMapOff()
123 DoMapTexture = Standard_False;
126 //=======================================================================
127 //function : SetTextureOrigin
129 //=======================================================================
131 void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean toSetTextureOriginYN,
132 const Standard_Real theUOrigin,
133 const Standard_Real theVOrigin)
135 DoSetTextureOrigin = toSetTextureOriginYN;
136 myUOrigin = theUOrigin;
137 myVOrigin = theVOrigin;
140 //=======================================================================
141 //function : SetTextureScale
143 //=======================================================================
145 void AIS_TexturedShape::SetTextureScale (const Standard_Boolean toSetTextureScaleYN,
146 const Standard_Real theScaleU,
147 const Standard_Real theScaleV)
149 DoSetTextureScale = toSetTextureScaleYN;
150 myScaleU = theScaleU;
151 myScaleV = theScaleV;
154 //=======================================================================
155 //function : ShowTriangles
157 //=======================================================================
159 void AIS_TexturedShape::ShowTriangles (const Standard_Boolean toShowTrianglesYN)
161 DoShowTriangles = toShowTrianglesYN;
164 //=======================================================================
165 //function : EnableTextureModulate
167 //=======================================================================
169 void AIS_TexturedShape::EnableTextureModulate()
171 myModulate = Standard_True;
174 //=======================================================================
175 //function : DisableTextureModulate
177 //=======================================================================
179 void AIS_TexturedShape::DisableTextureModulate()
181 myModulate = Standard_False;
184 //=======================================================================
185 //function : UpdateAttributes
187 //=======================================================================
189 void AIS_TexturedShape::UpdateAttributes()
191 Prs3d_ShadingAspect aDummy;
192 myAspect = aDummy.Aspect();
193 Handle(Prs3d_Presentation) aPrs = Presentation();
196 myAspect->SetTextureMapOff();
200 if (myPredefTexture != -1)
201 mytexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
203 mytexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
205 myAspect->SetTextureMapOn();
207 myAspect->SetTextureMap (mytexture);
208 if (!mytexture->IsDone())
210 std::cout << "An error occured while building texture \n";
215 myAspect->SetEdgeOn();
217 myAspect->SetEdgeOff();
219 Prs3d_Root::CurrentGroup (aPrs)->SetGroupPrimitivesAspect (myAspect);
222 //=======================================================================
225 //=======================================================================
227 void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePresManager*/,
228 const Handle(Prs3d_Presentation)& thePrs,
229 const Standard_Integer theMode)
233 if (myshape.IsNull())
238 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
240 thePrs->SetVisual (Graphic3d_TOS_ALL);
241 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
244 if (myshape.ShapeType() == TopAbs_COMPOUND)
246 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
247 if (!anExplor.More())
255 thePrs->SetInfiniteState (Standard_True);
262 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
267 Standard_Real prevangle;
268 Standard_Real newangle;
269 Standard_Real prevcoeff;
270 Standard_Real newcoeff;
272 Standard_Boolean isOwnDeviationAngle = OwnDeviationAngle(newangle,prevangle);
273 Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient(newcoeff,prevcoeff);
274 if (((Abs (newangle - prevangle) > Precision::Angular()) && isOwnDeviationAngle) ||
275 ((Abs (newcoeff - prevcoeff) > Precision::Confusion()) && isOwnDeviationCoefficient)) {
276 BRepTools::Clean (myshape);
278 if (myshape.ShapeType() > TopAbs_FACE)
280 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
283 myDrawer->SetShadingAspectGlobal (Standard_False);
286 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
292 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
294 catch (Standard_Failure)
296 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
297 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
302 case 2: // Bounding box
306 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
310 AIS_Shape::DisplayBox (thePrs, BoundingBox(), myDrawer);
315 case 3: // texture mapping on triangulation
317 BRepTools::Clean (myshape);
318 BRepTools::Update (myshape);
321 Handle(Prs3d_ShadingAspect) aPrs3d_ShadingAspect = new Prs3d_ShadingAspect();
322 myAspect = aPrs3d_ShadingAspect->Aspect();
324 // Issue 23115: copy polygon offset settings passed through myDrawer
325 if (HasPolygonOffsets())
327 Standard_Integer aMode;
328 Standard_ShortReal aFactor, aUnits;
329 PolygonOffsets(aMode, aFactor, aUnits);
330 myAspect->SetPolygonOffsets(aMode, aFactor, aUnits);
335 myAspect->SetTextureMapOff();
338 myAspect->SetTextureMapOn();
340 if (myPredefTexture != -1)
341 mytexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
343 mytexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
345 if (!mytexture->IsDone())
347 std::cout << "An error occured while building texture \n";
352 mytexture->EnableModulate();
354 mytexture->DisableModulate();
356 myAspect->SetTextureMap (mytexture);
358 myAspect->SetEdgeOn();
360 myAspect->SetEdgeOff();
363 mytexture->EnableRepeat();
365 mytexture->DisableRepeat();
367 const gp_Pnt2d aUVOrigin (myUOrigin, myVOrigin);
368 const gp_Pnt2d aUVRepeat (myURepeat, myVRepeat);
369 const gp_Pnt2d aUVScale (myScaleU, myScaleV);
373 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
374 Standard_True, aUVOrigin, aUVRepeat, aUVScale);
375 // within primitive arrays - object should be in one group of primitives
376 Prs3d_Root::CurrentGroup (thePrs)->SetGroupPrimitivesAspect (myAspect);
378 catch (Standard_Failure)
380 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
381 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
388 Standard_Boolean AIS_TexturedShape::TextureMapState() const
393 Standard_Real AIS_TexturedShape::URepeat() const
398 Standard_Boolean AIS_TexturedShape::TextureRepeat() const
403 Standard_Real AIS_TexturedShape::Deflection() const
408 Standard_CString AIS_TexturedShape::TextureFile() const
410 return myTextureFile.ToCString();
413 Standard_Real AIS_TexturedShape::VRepeat() const
418 Standard_Boolean AIS_TexturedShape::ShowTriangles() const
420 return DoShowTriangles;
423 Standard_Real AIS_TexturedShape::TextureUOrigin() const
428 Standard_Real AIS_TexturedShape::TextureVOrigin() const
433 Standard_Real AIS_TexturedShape::TextureScaleU() const
438 Standard_Real AIS_TexturedShape::TextureScaleV() const
443 Standard_Boolean AIS_TexturedShape::TextureScale() const
445 return DoSetTextureScale;
448 Standard_Boolean AIS_TexturedShape::TextureOrigin() const
450 return DoSetTextureOrigin;
453 Standard_Boolean AIS_TexturedShape::TextureModulate() const