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 <PrsMgr_ModedPresentation.hxx>
32 #include <Prs3d_Root.hxx>
33 #include <Prs3d_LineAspect.hxx>
34 #include <Prs3d_ShadingAspect.hxx>
35 #include <PrsMgr_PresentationManager3d.hxx>
36 #include <Standard_ErrorHandler.hxx>
37 #include <StdPrs_BndBox.hxx>
38 #include <StdPrs_ShadedShape.hxx>
39 #include <StdPrs_ToolTriangulatedShape.hxx>
40 #include <StdPrs_WFShape.hxx>
41 #include <TopExp_Explorer.hxx>
44 IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape,AIS_Shape)
46 //=======================================================================
47 //function : AIS_TexturedShape
49 //=======================================================================
50 AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
51 : AIS_Shape (theShape),
52 myPredefTexture (Graphic3d_NameOfTexture2D(0)),
53 myToMapTexture (Standard_True),
54 myModulate (Standard_True),
55 myIsCustomOrigin (Standard_True),
56 myToRepeat (Standard_True),
57 myToScale (Standard_True),
58 myToShowTriangles (Standard_False)
62 //=======================================================================
63 //function : SetTextureFileName
65 //=======================================================================
66 void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
68 myTexturePixMap.Nullify();
70 if (theTextureFileName.IsIntegerValue())
72 const Standard_Integer aValue = theTextureFileName.IntegerValue();
73 if (aValue < Graphic3d_Texture2D::NumberOfTextures()
76 myPredefTexture = Graphic3d_NameOfTexture2D (aValue);
80 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: texture with ID ") + theTextureFileName
81 + " is undefined! Texture 0 will be used instead.", Message_Fail);
82 myPredefTexture = Graphic3d_NameOfTexture2D (0);
88 myTextureFile = theTextureFileName;
89 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
93 //=======================================================================
94 //function : SetTexturePixMap
96 //=======================================================================
97 void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap)
100 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
101 myTexturePixMap = theTexturePixMap;
104 //=======================================================================
105 //function : SetTextureRepeat
107 //=======================================================================
109 void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat,
110 const Standard_Real theURepeat,
111 const Standard_Real theVRepeat)
113 myToRepeat = theToRepeat;
114 myUVRepeat.SetCoord (theURepeat, theVRepeat);
117 //=======================================================================
118 //function : SetTextureMapOn
120 //=======================================================================
122 void AIS_TexturedShape::SetTextureMapOn()
124 myToMapTexture = Standard_True;
127 //=======================================================================
128 //function : SetTextureMapOff
130 //=======================================================================
132 void AIS_TexturedShape::SetTextureMapOff()
134 myToMapTexture = Standard_False;
137 //=======================================================================
138 //function : SetTextureOrigin
140 //=======================================================================
142 void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin,
143 const Standard_Real theUOrigin,
144 const Standard_Real theVOrigin)
146 myIsCustomOrigin = theToSetTextureOrigin;
147 myUVOrigin.SetCoord (theUOrigin, theVOrigin);
150 //=======================================================================
151 //function : SetTextureScale
153 //=======================================================================
155 void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale,
156 const Standard_Real theScaleU,
157 const Standard_Real theScaleV)
159 myToScale = theToSetTextureScale;
160 myUVScale.SetCoord (theScaleU, theScaleV);
163 //=======================================================================
164 //function : ShowTriangles
166 //=======================================================================
168 void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles)
170 myToShowTriangles = theToShowTriangles;
173 //=======================================================================
174 //function : EnableTextureModulate
176 //=======================================================================
178 void AIS_TexturedShape::EnableTextureModulate()
180 myModulate = Standard_True;
183 //=======================================================================
184 //function : DisableTextureModulate
186 //=======================================================================
188 void AIS_TexturedShape::DisableTextureModulate()
190 myModulate = Standard_False;
193 //=======================================================================
194 //function : SetColor
196 //=======================================================================
198 void AIS_TexturedShape::SetColor (const Quantity_Color& theColor)
200 AIS_Shape::SetColor (theColor);
202 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
204 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
206 if (aPrsModed.Mode() != 3)
209 updateAttributes (aPrsModed.Presentation()->Presentation());
213 //=======================================================================
214 //function : UnsetColor
216 //=======================================================================
218 void AIS_TexturedShape::UnsetColor()
220 AIS_Shape::UnsetColor();
222 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
224 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
226 if (aPrsModed.Mode() != 3)
229 Handle(Prs3d_Presentation) aPrs = aPrsModed.Presentation()->Presentation();
230 Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (aPrs);
232 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->Link()->ShadingAspect()->Aspect();
233 Handle(Graphic3d_AspectLine3d) aLineAsp = myDrawer->Link()->LineAspect()->Aspect();
234 Quantity_Color aColor;
235 AIS_GraphicTool::GetInteriorColor (myDrawer->Link(), aColor);
236 anAreaAsp->SetInteriorColor (aColor);
237 // Check if aspect of given type is set for the group,
238 // because setting aspect for group with no already set aspect
239 // can lead to loss of presentation data
240 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
242 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
244 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_LINE))
246 aGroup->SetGroupPrimitivesAspect (aLineAsp);
249 updateAttributes (aPrs);
253 //=======================================================================
254 //function : SetMaterial
256 //=======================================================================
258 void AIS_TexturedShape::SetMaterial (const Graphic3d_MaterialAspect& theMat)
260 AIS_Shape::SetMaterial (theMat);
262 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
264 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
266 if (aPrsModed.Mode() != 3)
269 updateAttributes (aPrsModed.Presentation()->Presentation());
273 //=======================================================================
274 //function : UnsetMaterial
276 //=======================================================================
277 void AIS_TexturedShape::UnsetMaterial()
279 AIS_Shape::UnsetMaterial();
281 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
283 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
285 if (aPrsModed.Mode() != 3)
288 updateAttributes (aPrsModed.Presentation()->Presentation());
292 //=======================================================================
293 //function : UpdateAttributes
295 //=======================================================================
297 void AIS_TexturedShape::UpdateAttributes()
299 updateAttributes (Presentation());
302 //=======================================================================
303 //function : updateAttributes
305 //=======================================================================
307 void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs)
309 myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect());
310 if (HasPolygonOffsets())
312 Standard_Integer aMode;
313 Standard_ShortReal aFactor, aUnits;
314 PolygonOffsets (aMode, aFactor, aUnits);
315 myAspect->SetPolygonOffsets (aMode, aFactor, aUnits);
318 Standard_Boolean hasTexture = Standard_False;
321 TCollection_AsciiString aTextureDesc;
322 if (!myTexturePixMap.IsNull())
324 myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
325 aTextureDesc = " (custom image)";
327 else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
329 myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
330 aTextureDesc = TCollection_AsciiString(" (predefined texture ") + myTexture->GetId() + ")";
334 myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
335 aTextureDesc = TCollection_AsciiString(" (") + myTextureFile + ")";
340 myTexture->EnableModulate();
344 myTexture->DisableModulate();
347 if (myTexture->IsDone())
349 hasTexture = Standard_True;
353 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: texture can not be loaded ") + aTextureDesc, Message_Fail);
357 myAspect->SetTextureMap (myTexture);
360 myAspect->SetTextureMapOn();
364 myAspect->SetTextureMapOff();
367 if (myToShowTriangles)
369 myAspect->SetEdgeOn();
373 myAspect->SetEdgeOff();
376 // Go through all groups to change fill aspect for all primitives
377 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (thePrs->Groups()); aGroupIt.More(); aGroupIt.Next())
379 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
380 if (!aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
384 aGroup->SetGroupPrimitivesAspect (myAspect);
388 //=======================================================================
391 //=======================================================================
393 void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
394 const Handle(Prs3d_Presentation)& thePrs,
395 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_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
427 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
431 case 3: // texture mapping on triangulation
433 StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
434 if (myshape.ShapeType() > TopAbs_FACE)
436 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
442 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
448 if (theMode == AIS_Shaded)
450 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
454 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
456 myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),
458 myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0));
459 updateAttributes (thePrs);
462 catch (Standard_Failure)
465 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
467 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
471 case 2: // Bounding box
475 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
479 StdPrs_BndBox::Add (thePrs, BoundingBox(), myDrawer);