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 <Prs3d_Root.hxx>
32 #include <Prs3d_LineAspect.hxx>
33 #include <Prs3d_ShadingAspect.hxx>
34 #include <PrsMgr_PresentationManager3d.hxx>
35 #include <Standard_ErrorHandler.hxx>
36 #include <Prs3d_BndBox.hxx>
37 #include <StdPrs_ShadedShape.hxx>
38 #include <StdPrs_ToolTriangulatedShape.hxx>
39 #include <StdPrs_WFShape.hxx>
40 #include <TopExp_Explorer.hxx>
43 IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape,AIS_Shape)
45 //=======================================================================
46 //function : AIS_TexturedShape
48 //=======================================================================
49 AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
50 : AIS_Shape (theShape),
51 myPredefTexture (Graphic3d_NameOfTexture2D(0)),
52 myToMapTexture (Standard_True),
53 myModulate (Standard_True),
54 myIsCustomOrigin (Standard_True),
55 myToRepeat (Standard_True),
56 myToScale (Standard_True),
57 myToShowTriangles (Standard_False)
61 //=======================================================================
62 //function : SetTextureFileName
64 //=======================================================================
65 void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
67 myTexturePixMap.Nullify();
69 if (theTextureFileName.IsIntegerValue())
71 const Standard_Integer aValue = theTextureFileName.IntegerValue();
72 if (aValue < Graphic3d_Texture2D::NumberOfTextures()
75 myPredefTexture = Graphic3d_NameOfTexture2D (aValue);
79 Message::SendFail (TCollection_AsciiString ("Error: texture with ID ") + theTextureFileName
80 + " is undefined. Texture 0 will be used instead.");
81 myPredefTexture = Graphic3d_NameOfTexture2D (0);
87 myTextureFile = theTextureFileName;
88 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
92 //=======================================================================
93 //function : SetTexturePixMap
95 //=======================================================================
96 void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap)
99 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
100 myTexturePixMap = theTexturePixMap;
103 //=======================================================================
104 //function : SetTextureRepeat
106 //=======================================================================
108 void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat,
109 const Standard_Real theURepeat,
110 const Standard_Real theVRepeat)
112 myToRepeat = theToRepeat;
113 myUVRepeat.SetCoord (theURepeat, theVRepeat);
116 //=======================================================================
117 //function : SetTextureMapOn
119 //=======================================================================
121 void AIS_TexturedShape::SetTextureMapOn()
123 myToMapTexture = Standard_True;
126 //=======================================================================
127 //function : SetTextureMapOff
129 //=======================================================================
131 void AIS_TexturedShape::SetTextureMapOff()
133 myToMapTexture = Standard_False;
136 //=======================================================================
137 //function : SetTextureOrigin
139 //=======================================================================
141 void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin,
142 const Standard_Real theUOrigin,
143 const Standard_Real theVOrigin)
145 myIsCustomOrigin = theToSetTextureOrigin;
146 myUVOrigin.SetCoord (theUOrigin, theVOrigin);
149 //=======================================================================
150 //function : SetTextureScale
152 //=======================================================================
154 void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale,
155 const Standard_Real theScaleU,
156 const Standard_Real theScaleV)
158 myToScale = theToSetTextureScale;
159 myUVScale.SetCoord (theScaleU, theScaleV);
162 //=======================================================================
163 //function : ShowTriangles
165 //=======================================================================
167 void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles)
169 myToShowTriangles = theToShowTriangles;
172 //=======================================================================
173 //function : EnableTextureModulate
175 //=======================================================================
177 void AIS_TexturedShape::EnableTextureModulate()
179 myModulate = Standard_True;
182 //=======================================================================
183 //function : DisableTextureModulate
185 //=======================================================================
187 void AIS_TexturedShape::DisableTextureModulate()
189 myModulate = Standard_False;
192 //=======================================================================
193 //function : SetColor
195 //=======================================================================
197 void AIS_TexturedShape::SetColor (const Quantity_Color& theColor)
199 AIS_Shape::SetColor (theColor);
201 for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next())
203 if (aPrsIter.Value()->Mode() == 3)
205 updateAttributes (aPrsIter.Value());
210 //=======================================================================
211 //function : UnsetColor
213 //=======================================================================
215 void AIS_TexturedShape::UnsetColor()
217 AIS_Shape::UnsetColor();
220 //=======================================================================
221 //function : SetMaterial
223 //=======================================================================
225 void AIS_TexturedShape::SetMaterial (const Graphic3d_MaterialAspect& theMat)
227 AIS_Shape::SetMaterial (theMat);
228 for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next())
230 if (aPrsIter.Value()->Mode() == 3)
232 updateAttributes (aPrsIter.Value());
237 //=======================================================================
238 //function : UnsetMaterial
240 //=======================================================================
241 void AIS_TexturedShape::UnsetMaterial()
243 AIS_Shape::UnsetMaterial();
244 for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next())
246 if (aPrsIter.Value()->Mode() == 3)
248 updateAttributes (aPrsIter.Value());
253 //=======================================================================
254 //function : UpdateAttributes
256 //=======================================================================
258 void AIS_TexturedShape::UpdateAttributes()
260 updateAttributes (Presentation());
263 //=======================================================================
264 //function : updateAttributes
266 //=======================================================================
268 void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs)
270 myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect());
271 if (HasPolygonOffsets())
273 Standard_Integer aMode;
274 Standard_ShortReal aFactor, aUnits;
275 PolygonOffsets (aMode, aFactor, aUnits);
276 myAspect->SetPolygonOffsets (aMode, aFactor, aUnits);
279 Standard_Boolean hasTexture = Standard_False;
282 TCollection_AsciiString aTextureDesc;
283 if (!myTexturePixMap.IsNull())
285 myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
286 aTextureDesc = " (custom image)";
288 else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
290 myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
291 aTextureDesc = TCollection_AsciiString(" (predefined texture ") + myTexture->GetId() + ")";
295 myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
296 aTextureDesc = TCollection_AsciiString(" (") + myTextureFile + ")";
301 myTexture->EnableModulate();
305 myTexture->DisableModulate();
308 if (myTexture->IsDone())
310 hasTexture = Standard_True;
314 Message::SendFail (TCollection_AsciiString ("Error: texture can not be loaded ") + aTextureDesc);
318 myAspect->SetTextureMap (myTexture);
321 myAspect->SetTextureMapOn();
325 myAspect->SetTextureMapOff();
328 if (myToShowTriangles)
330 myAspect->SetEdgeOn();
334 myAspect->SetEdgeOff();
337 // Go through all groups to change fill aspect for all primitives
338 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (thePrs->Groups()); aGroupIt.More(); aGroupIt.Next())
340 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
341 aGroup->SetGroupPrimitivesAspect (myAspect);
345 //=======================================================================
348 //=======================================================================
350 void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
351 const Handle(Prs3d_Presentation)& thePrs,
352 const Standard_Integer theMode)
354 if (myshape.IsNull())
359 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
361 thePrs->SetVisual (Graphic3d_TOS_ALL);
362 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
365 if (myshape.ShapeType() == TopAbs_COMPOUND)
367 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
368 if (!anExplor.More())
376 thePrs->SetInfiniteState (Standard_True);
383 StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
384 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
388 case 3: // texture mapping on triangulation
390 StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
391 if (myshape.ShapeType() > TopAbs_FACE)
393 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
399 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
405 if (theMode == AIS_Shaded)
407 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
411 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
413 myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),
415 myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0));
416 updateAttributes (thePrs);
419 catch (Standard_Failure const&)
422 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
424 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
428 case 2: // Bounding box
432 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
436 Prs3d_BndBox::Add (thePrs, BoundingBox(), myDrawer);