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_LineAspect.hxx>
32 #include <Prs3d_ShadingAspect.hxx>
33 #include <PrsMgr_PresentationManager.hxx>
34 #include <Standard_ErrorHandler.hxx>
35 #include <Prs3d_BndBox.hxx>
36 #include <StdPrs_ShadedShape.hxx>
37 #include <StdPrs_ToolTriangulatedShape.hxx>
38 #include <StdPrs_WFShape.hxx>
39 #include <TopExp_Explorer.hxx>
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 myIsCustomOrigin (Standard_True),
54 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 Message::SendFail (TCollection_AsciiString ("Error: texture with ID ") + theTextureFileName
79 + " is undefined. Texture 0 will be used instead.");
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 for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next())
202 if (aPrsIter.Value()->Mode() == 3)
204 updateAttributes (aPrsIter.Value());
209 //=======================================================================
210 //function : UnsetColor
212 //=======================================================================
214 void AIS_TexturedShape::UnsetColor()
216 AIS_Shape::UnsetColor();
219 //=======================================================================
220 //function : SetMaterial
222 //=======================================================================
224 void AIS_TexturedShape::SetMaterial (const Graphic3d_MaterialAspect& theMat)
226 AIS_Shape::SetMaterial (theMat);
227 for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next())
229 if (aPrsIter.Value()->Mode() == 3)
231 updateAttributes (aPrsIter.Value());
236 //=======================================================================
237 //function : UnsetMaterial
239 //=======================================================================
240 void AIS_TexturedShape::UnsetMaterial()
242 AIS_Shape::UnsetMaterial();
243 for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next())
245 if (aPrsIter.Value()->Mode() == 3)
247 updateAttributes (aPrsIter.Value());
252 //=======================================================================
253 //function : UpdateAttributes
255 //=======================================================================
257 void AIS_TexturedShape::UpdateAttributes()
259 updateAttributes (Presentation());
262 //=======================================================================
263 //function : updateAttributes
265 //=======================================================================
267 void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs)
269 myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect());
270 if (HasPolygonOffsets())
272 Standard_Integer aMode;
273 Standard_ShortReal aFactor, aUnits;
274 PolygonOffsets (aMode, aFactor, aUnits);
275 myAspect->SetPolygonOffsets (aMode, aFactor, aUnits);
278 Standard_Boolean hasTexture = Standard_False;
281 TCollection_AsciiString aTextureDesc;
282 if (!myTexturePixMap.IsNull())
284 myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
285 aTextureDesc = " (custom image)";
287 else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
289 myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
290 aTextureDesc = TCollection_AsciiString(" (predefined texture ") + myTexture->GetId() + ")";
294 myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
295 aTextureDesc = TCollection_AsciiString(" (") + myTextureFile + ")";
300 myTexture->EnableModulate();
304 myTexture->DisableModulate();
307 if (myTexture->IsDone())
309 hasTexture = Standard_True;
313 Message::SendFail (TCollection_AsciiString ("Error: texture can not be loaded ") + aTextureDesc);
317 myAspect->SetTextureMap (myTexture);
320 myAspect->SetTextureMapOn();
324 myAspect->SetTextureMapOff();
327 if (myToShowTriangles)
329 myAspect->SetEdgeOn();
333 myAspect->SetEdgeOff();
336 // Go through all groups to change fill aspect for all primitives
337 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (thePrs->Groups()); aGroupIt.More(); aGroupIt.Next())
339 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
340 aGroup->SetGroupPrimitivesAspect (myAspect);
344 //=======================================================================
347 //=======================================================================
348 void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager)& ,
349 const Handle(Prs3d_Presentation)& thePrs,
350 const Standard_Integer theMode)
352 if (myshape.IsNull())
357 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
359 thePrs->SetVisual (Graphic3d_TOS_ALL);
360 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
363 if (myshape.ShapeType() == TopAbs_COMPOUND)
365 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
366 if (!anExplor.More())
374 thePrs->SetInfiniteState (Standard_True);
381 StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
382 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
386 case 3: // texture mapping on triangulation
388 StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
389 if (myshape.ShapeType() > TopAbs_FACE)
391 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
397 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
403 if (theMode == AIS_Shaded)
405 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
409 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
411 myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),
413 myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0));
414 updateAttributes (thePrs);
417 catch (Standard_Failure const&)
420 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
422 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
426 case 2: // Bounding box
430 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
434 Prs3d_BndBox::Add (thePrs, BoundingBox(), myDrawer);