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_GraphicTool.hxx>
20 #include <AIS_InteractiveContext.hxx>
21 #include <BRepTools.hxx>
22 #include <gp_Pnt2d.hxx>
23 #include <Graphic3d_AspectFillArea3d.hxx>
24 #include <Graphic3d_Group.hxx>
25 #include <Graphic3d_StructureManager.hxx>
26 #include <Graphic3d_Texture2Dmanual.hxx>
27 #include <Precision.hxx>
28 #include <Prs3d_Presentation.hxx>
29 #include <PrsMgr_ModedPresentation.hxx>
30 #include <Prs3d_Root.hxx>
31 #include <Prs3d_LineAspect.hxx>
32 #include <Prs3d_ShadingAspect.hxx>
33 #include <PrsMgr_PresentationManager3d.hxx>
34 #include <Standard_ErrorHandler.hxx>
35 #include <StdPrs_ShadedShape.hxx>
36 #include <StdPrs_ToolShadedShape.hxx>
37 #include <StdPrs_WFDeflectionShape.hxx>
38 #include <StdPrs_WFShape.hxx>
39 #include <TopExp_Explorer.hxx>
41 IMPLEMENT_STANDARD_HANDLE (AIS_TexturedShape, AIS_Shape)
42 IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape, AIS_Shape)
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);
82 std::cout << "Texture " << theTextureFileName << " doesn't exist\n";
83 std::cout << "Using Texture 0 instead ...\n";
85 myPredefTexture = Graphic3d_NameOfTexture2D (0);
91 myTextureFile = theTextureFileName;
92 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
96 //=======================================================================
97 //function : SetTexturePixMap
99 //=======================================================================
100 void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap)
103 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
104 myTexturePixMap = theTexturePixMap;
107 //=======================================================================
108 //function : SetTextureRepeat
110 //=======================================================================
112 void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat,
113 const Standard_Real theURepeat,
114 const Standard_Real theVRepeat)
116 myToRepeat = theToRepeat;
117 myUVRepeat.SetCoord (theURepeat, theVRepeat);
120 //=======================================================================
121 //function : SetTextureMapOn
123 //=======================================================================
125 void AIS_TexturedShape::SetTextureMapOn()
127 myToMapTexture = Standard_True;
130 //=======================================================================
131 //function : SetTextureMapOff
133 //=======================================================================
135 void AIS_TexturedShape::SetTextureMapOff()
137 myToMapTexture = Standard_False;
140 //=======================================================================
141 //function : SetTextureOrigin
143 //=======================================================================
145 void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin,
146 const Standard_Real theUOrigin,
147 const Standard_Real theVOrigin)
149 myIsCustomOrigin = theToSetTextureOrigin;
150 myUVOrigin.SetCoord (theUOrigin, theVOrigin);
153 //=======================================================================
154 //function : SetTextureScale
156 //=======================================================================
158 void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale,
159 const Standard_Real theScaleU,
160 const Standard_Real theScaleV)
162 myToScale = theToSetTextureScale;
163 myUVScale.SetCoord (theScaleU, theScaleV);
166 //=======================================================================
167 //function : ShowTriangles
169 //=======================================================================
171 void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles)
173 myToShowTriangles = theToShowTriangles;
176 //=======================================================================
177 //function : EnableTextureModulate
179 //=======================================================================
181 void AIS_TexturedShape::EnableTextureModulate()
183 myModulate = Standard_True;
186 //=======================================================================
187 //function : DisableTextureModulate
189 //=======================================================================
191 void AIS_TexturedShape::DisableTextureModulate()
193 myModulate = Standard_False;
196 //=======================================================================
197 //function : SetColor
199 //=======================================================================
201 void AIS_TexturedShape::SetColor (const Quantity_Color& theColor)
203 AIS_Shape::SetColor (theColor);
205 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
207 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
209 if (aPrsModed.Mode() != 3)
212 updateAttributes (aPrsModed.Presentation()->Presentation());
216 //=======================================================================
217 //function : UnsetColor
219 //=======================================================================
221 void AIS_TexturedShape::UnsetColor()
223 AIS_Shape::UnsetColor();
225 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
227 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
229 if (aPrsModed.Mode() != 3)
232 Handle(Prs3d_Presentation) aPrs = aPrsModed.Presentation()->Presentation();
233 Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (aPrs);
235 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->Link()->ShadingAspect()->Aspect();
236 Handle(Graphic3d_AspectLine3d) aLineAsp = myDrawer->Link()->LineAspect()->Aspect();
237 Quantity_Color aColor;
238 AIS_GraphicTool::GetInteriorColor (myDrawer->Link(), aColor);
239 anAreaAsp->SetInteriorColor (aColor);
240 aPrs->SetPrimitivesAspect (anAreaAsp);
241 aPrs->SetPrimitivesAspect (aLineAsp);
242 // Check if aspect of given type is set for the group,
243 // because setting aspect for group with no already set aspect
244 // can lead to loss of presentation data
245 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
247 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
249 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_LINE))
251 aGroup->SetGroupPrimitivesAspect (aLineAsp);
254 updateAttributes (aPrs);
258 //=======================================================================
259 //function : SetMaterial
261 //=======================================================================
263 void AIS_TexturedShape::SetMaterial (const Graphic3d_MaterialAspect& theMat)
265 AIS_Shape::SetMaterial (theMat);
267 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
269 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
271 if (aPrsModed.Mode() != 3)
274 updateAttributes (aPrsModed.Presentation()->Presentation());
278 //=======================================================================
279 //function : UnsetMaterial
281 //=======================================================================
282 void AIS_TexturedShape::UnsetMaterial()
284 AIS_Shape::UnsetMaterial();
286 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
288 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
290 if (aPrsModed.Mode() != 3)
293 updateAttributes (aPrsModed.Presentation()->Presentation());
297 //=======================================================================
298 //function : UpdateAttributes
300 //=======================================================================
302 void AIS_TexturedShape::UpdateAttributes()
304 updateAttributes (Presentation());
307 //=======================================================================
308 //function : updateAttributes
310 //=======================================================================
312 void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs)
314 myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect());
315 if (HasPolygonOffsets())
317 // Issue 23115: copy polygon offset settings passed through myDrawer
318 Standard_Integer aMode;
319 Standard_ShortReal aFactor, aUnits;
320 PolygonOffsets (aMode, aFactor, aUnits);
321 myAspect->SetPolygonOffsets (aMode, aFactor, aUnits);
326 myAspect->SetTextureMapOff();
330 if (!myTexturePixMap.IsNull())
332 myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
334 else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
336 myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
340 myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
343 myAspect->SetTextureMapOn();
344 myAspect->SetTextureMap (myTexture);
345 if (!myTexture->IsDone())
348 std::cout << "An error occurred while building texture\n";
350 myAspect->SetTextureMapOff();
355 myTexture->EnableModulate();
357 myTexture->DisableModulate();
359 if (myToShowTriangles)
360 myAspect->SetEdgeOn();
362 myAspect->SetEdgeOff();
364 // manage back face culling in consistent way (as in StdPrs_ShadedShape::Add())
365 if (StdPrs_ToolShadedShape::IsClosed (myshape))
367 myAspect->SuppressBackFace();
371 myAspect->AllowBackFace();
374 // Go through all groups to change fill aspect for all primitives
375 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (thePrs->Groups()); aGroupIt.More(); aGroupIt.Next())
377 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
379 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
381 aGroup->SetGroupPrimitivesAspect (myAspect);
386 //=======================================================================
389 //=======================================================================
391 void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
392 const Handle(Prs3d_Presentation)& thePrs,
393 const Standard_Integer theMode)
397 if (myshape.IsNull())
402 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
404 thePrs->SetVisual (Graphic3d_TOS_ALL);
405 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
408 if (myshape.ShapeType() == TopAbs_COMPOUND)
410 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
411 if (!anExplor.More())
419 thePrs->SetInfiniteState (Standard_True);
426 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
430 case 3: // texture mapping on triangulation
432 Standard_Real prevangle;
433 Standard_Real newangle;
434 Standard_Real prevcoeff;
435 Standard_Real newcoeff;
437 Standard_Boolean isOwnDeviationAngle = OwnDeviationAngle(newangle,prevangle);
438 Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient(newcoeff,prevcoeff);
439 if (((Abs (newangle - prevangle) > Precision::Angular()) && isOwnDeviationAngle) ||
440 ((Abs (newcoeff - prevcoeff) > Precision::Confusion()) && isOwnDeviationCoefficient)) {
441 BRepTools::Clean (myshape);
444 if (myshape.ShapeType() > TopAbs_FACE)
446 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
450 myDrawer->SetShadingAspectGlobal (Standard_False);
453 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
459 if (theMode == AIS_Shaded)
461 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
465 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
467 myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),
469 myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0));
470 updateAttributes (thePrs);
473 catch (Standard_Failure)
476 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
478 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
482 case 2: // Bounding box
486 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
490 StdPrs_WFDeflectionRestrictedFace::AddBox (thePrs, BoundingBox(), myDrawer);