1 // Created on: 2001-07-02
2 // Created by: Mathias BOSSHARD
3 // Copyright (c) 2001-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 ////////////////////////////////////////////////////////////////////////
24 #include <AIS_TexturedShape.ixx>
25 #include <Standard_ErrorHandler.hxx>
27 #include <AIS_Drawer.hxx>
28 #include <AIS_InteractiveContext.hxx>
29 #include <BRepTools.hxx>
30 #include <gp_Pnt2d.hxx>
31 #include <Graphic3d_AspectFillArea3d.hxx>
32 #include <Graphic3d_Group.hxx>
33 #include <Graphic3d_StructureManager.hxx>
34 #include <Graphic3d_Texture2Dmanual.hxx>
35 #include <Precision.hxx>
36 #include <Prs3d_Presentation.hxx>
37 #include <Prs3d_Root.hxx>
38 #include <Prs3d_ShadingAspect.hxx>
39 #include <PrsMgr_PresentationManager3d.hxx>
40 #include <StdPrs_ShadedShape.hxx>
41 #include <StdPrs_WFDeflectionShape.hxx>
42 #include <StdPrs_WFShape.hxx>
43 #include <TopExp_Explorer.hxx>
45 //=======================================================================
46 //function : AIS_TexturedShape
48 //=======================================================================
49 AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
50 : AIS_Shape (theShape),
51 myPredefTexture (Graphic3d_NameOfTexture2D(0)),
53 DoRepeat (Standard_True),
56 DoMapTexture (Standard_True),
57 DoSetTextureOrigin (Standard_True),
60 DoSetTextureScale (Standard_True),
63 DoShowTriangles (Standard_False),
64 myModulate (Standard_True)
68 //=======================================================================
69 //function : SetTextureFileName
71 //=======================================================================
72 void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
74 if (theTextureFileName.IsIntegerValue())
76 if (theTextureFileName.IntegerValue() < Graphic3d_Texture2D::NumberOfTextures()
77 && theTextureFileName.IntegerValue() >= 0)
79 myPredefTexture = Graphic3d_NameOfTexture2D (theTextureFileName.IntegerValue());
83 std::cout << "Texture " << theTextureFileName << " doesn't exist \n";
84 std::cout << "Using Texture 0 instead ...\n";
85 myPredefTexture = Graphic3d_NameOfTexture2D (0);
91 myTextureFile = theTextureFileName;
92 myPredefTexture = Graphic3d_NameOfTexture2D (-1);
96 //=======================================================================
97 //function : SetTextureRepeat
99 //=======================================================================
101 void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theRepeatYN,
102 const Standard_Real theURepeat,
103 const Standard_Real theVRepeat)
105 DoRepeat = theRepeatYN;
106 myURepeat = theURepeat;
107 myVRepeat = theVRepeat;
110 //=======================================================================
111 //function : SetTextureMapOn
113 //=======================================================================
115 void AIS_TexturedShape::SetTextureMapOn()
117 DoMapTexture = Standard_True;
120 //=======================================================================
121 //function : SetTextureMapOff
123 //=======================================================================
125 void AIS_TexturedShape::SetTextureMapOff()
127 DoMapTexture = Standard_False;
130 //=======================================================================
131 //function : SetTextureOrigin
133 //=======================================================================
135 void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean toSetTextureOriginYN,
136 const Standard_Real theUOrigin,
137 const Standard_Real theVOrigin)
139 DoSetTextureOrigin = toSetTextureOriginYN;
140 myUOrigin = theUOrigin;
141 myVOrigin = theVOrigin;
144 //=======================================================================
145 //function : SetTextureScale
147 //=======================================================================
149 void AIS_TexturedShape::SetTextureScale (const Standard_Boolean toSetTextureScaleYN,
150 const Standard_Real theScaleU,
151 const Standard_Real theScaleV)
153 DoSetTextureScale = toSetTextureScaleYN;
154 myScaleU = theScaleU;
155 myScaleV = theScaleV;
158 //=======================================================================
159 //function : ShowTriangles
161 //=======================================================================
163 void AIS_TexturedShape::ShowTriangles (const Standard_Boolean toShowTrianglesYN)
165 DoShowTriangles = toShowTrianglesYN;
168 //=======================================================================
169 //function : EnableTextureModulate
171 //=======================================================================
173 void AIS_TexturedShape::EnableTextureModulate()
175 myModulate = Standard_True;
178 //=======================================================================
179 //function : DisableTextureModulate
181 //=======================================================================
183 void AIS_TexturedShape::DisableTextureModulate()
185 myModulate = Standard_False;
188 //=======================================================================
189 //function : UpdateAttributes
191 //=======================================================================
193 void AIS_TexturedShape::UpdateAttributes()
195 Prs3d_ShadingAspect aDummy;
196 myAspect = aDummy.Aspect();
197 Handle(Prs3d_Presentation) aPrs = Presentation();
200 myAspect->SetTextureMapOff();
204 if (myPredefTexture != -1)
205 mytexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
207 mytexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
209 myAspect->SetTextureMapOn();
211 myAspect->SetTextureMap (mytexture);
212 if (!mytexture->IsDone())
214 std::cout << "An error occured while building texture \n";
219 myAspect->SetEdgeOn();
221 myAspect->SetEdgeOff();
223 Prs3d_Root::CurrentGroup (aPrs)->SetGroupPrimitivesAspect (myAspect);
226 //=======================================================================
229 //=======================================================================
231 void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePresManager*/,
232 const Handle(Prs3d_Presentation)& thePrs,
233 const Standard_Integer theMode)
237 if (myshape.IsNull())
242 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
244 thePrs->SetVisual (Graphic3d_TOS_ALL);
245 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
248 if (myshape.ShapeType() == TopAbs_COMPOUND)
250 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
251 if (!anExplor.More())
259 thePrs->SetInfiniteState (Standard_True);
266 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
271 Standard_Real prevangle;
272 Standard_Real newangle;
273 Standard_Real prevcoeff;
274 Standard_Real newcoeff;
276 Standard_Boolean isOwnDeviationAngle = OwnDeviationAngle(newangle,prevangle);
277 Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient(newcoeff,prevcoeff);
278 if (((Abs (newangle - prevangle) > Precision::Angular()) && isOwnDeviationAngle) ||
279 ((Abs (newcoeff - prevcoeff) > Precision::Confusion()) && isOwnDeviationCoefficient)) {
280 BRepTools::Clean (myshape);
282 if (myshape.ShapeType() > TopAbs_FACE)
284 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
287 myDrawer->SetShadingAspectGlobal (Standard_False);
290 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
296 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
298 catch (Standard_Failure)
300 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
301 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
306 case 2: // Bounding box
310 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
314 AIS_Shape::DisplayBox (thePrs, BoundingBox(), myDrawer);
319 case 3: // texture mapping on triangulation
321 BRepTools::Clean (myshape);
322 BRepTools::Update (myshape);
325 Handle(Prs3d_ShadingAspect) aPrs3d_ShadingAspect = new Prs3d_ShadingAspect();
326 myAspect = aPrs3d_ShadingAspect->Aspect();
328 // Issue 23115: copy polygon offset settings passed through myDrawer
329 if (HasPolygonOffsets())
331 Standard_Integer aMode;
332 Standard_ShortReal aFactor, aUnits;
333 PolygonOffsets(aMode, aFactor, aUnits);
334 myAspect->SetPolygonOffsets(aMode, aFactor, aUnits);
339 myAspect->SetTextureMapOff();
342 myAspect->SetTextureMapOn();
344 if (myPredefTexture != -1)
345 mytexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
347 mytexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
349 if (!mytexture->IsDone())
351 std::cout << "An error occured while building texture \n";
356 mytexture->EnableModulate();
358 mytexture->DisableModulate();
360 myAspect->SetTextureMap (mytexture);
362 myAspect->SetEdgeOn();
364 myAspect->SetEdgeOff();
367 mytexture->EnableRepeat();
369 mytexture->DisableRepeat();
371 const gp_Pnt2d aUVOrigin (myUOrigin, myVOrigin);
372 const gp_Pnt2d aUVRepeat (myURepeat, myVRepeat);
373 const gp_Pnt2d aUVScale (myScaleU, myScaleV);
377 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
378 Standard_True, aUVOrigin, aUVRepeat, aUVScale);
379 // within primitive arrays - object should be in one group of primitives
380 Prs3d_Root::CurrentGroup (thePrs)->SetGroupPrimitivesAspect (myAspect);
382 catch (Standard_Failure)
384 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
385 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
392 Standard_Boolean AIS_TexturedShape::TextureMapState() const
397 Standard_Real AIS_TexturedShape::URepeat() const
402 Standard_Boolean AIS_TexturedShape::TextureRepeat() const
407 Standard_Real AIS_TexturedShape::Deflection() const
412 Standard_CString AIS_TexturedShape::TextureFile() const
414 return myTextureFile.ToCString();
417 Standard_Real AIS_TexturedShape::VRepeat() const
422 Standard_Boolean AIS_TexturedShape::ShowTriangles() const
424 return DoShowTriangles;
427 Standard_Real AIS_TexturedShape::TextureUOrigin() const
432 Standard_Real AIS_TexturedShape::TextureVOrigin() const
437 Standard_Real AIS_TexturedShape::TextureScaleU() const
442 Standard_Real AIS_TexturedShape::TextureScaleV() const
447 Standard_Boolean AIS_TexturedShape::TextureScale() const
449 return DoSetTextureScale;
452 Standard_Boolean AIS_TexturedShape::TextureOrigin() const
454 return DoSetTextureOrigin;
457 Standard_Boolean AIS_TexturedShape::TextureModulate() const