0024725: AIS_TexturedShape - flag switching texture modulation mode does not work
[occt.git] / src / AIS / AIS_TexturedShape.cxx
CommitLineData
b311480e 1// Created on: 2001-07-02
2// Created by: Mathias BOSSHARD
973c2be1 3// Copyright (c) 2001-2014 OPEN CASCADE SAS
7fd59977 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
7fd59977 6//
d5f74e42 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
973c2be1 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.
7fd59977 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
f376ac72 16#include <AIS_TexturedShape.hxx>
7fd59977 17
2bd4c032 18#include <AIS_Drawer.hxx>
19#include <AIS_InteractiveContext.hxx>
7fd59977 20#include <BRepTools.hxx>
2bd4c032 21#include <gp_Pnt2d.hxx>
22#include <Graphic3d_AspectFillArea3d.hxx>
23#include <Graphic3d_Group.hxx>
7fd59977 24#include <Graphic3d_StructureManager.hxx>
25#include <Graphic3d_Texture2Dmanual.hxx>
2bd4c032 26#include <Precision.hxx>
27#include <Prs3d_Presentation.hxx>
7fd59977 28#include <Prs3d_Root.hxx>
2bd4c032 29#include <Prs3d_ShadingAspect.hxx>
7fd59977 30#include <PrsMgr_PresentationManager3d.hxx>
f376ac72 31#include <Standard_ErrorHandler.hxx>
2bd4c032 32#include <StdPrs_ShadedShape.hxx>
8e3ebc7a 33#include <StdPrs_ToolShadedShape.hxx>
7fd59977 34#include <StdPrs_WFDeflectionShape.hxx>
7fd59977 35#include <StdPrs_WFShape.hxx>
2bd4c032 36#include <TopExp_Explorer.hxx>
7fd59977 37
f376ac72 38IMPLEMENT_STANDARD_HANDLE (AIS_TexturedShape, AIS_Shape)
39IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape, AIS_Shape)
40
7fd59977 41//=======================================================================
42//function : AIS_TexturedShape
2bd4c032 43//purpose :
7fd59977 44//=======================================================================
2bd4c032 45AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
f376ac72 46: AIS_Shape (theShape),
47 myPredefTexture (Graphic3d_NameOfTexture2D(0)),
48 myToMapTexture (Standard_True),
49 myModulate (Standard_True),
50 myUVOrigin (0.0, 0.0),
51 myIsCustomOrigin (Standard_True),
52 myUVRepeat (1.0, 1.0),
53 myToRepeat (Standard_True),
54 myUVScale (1.0, 1.0),
55 myToScale (Standard_True),
56 myToShowTriangles (Standard_False)
7fd59977 57{
58}
59
7fd59977 60//=======================================================================
61//function : SetTextureFileName
2bd4c032 62//purpose :
7fd59977 63//=======================================================================
2bd4c032 64void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
7fd59977 65{
f376ac72 66 myTexturePixMap.Nullify();
67
2bd4c032 68 if (theTextureFileName.IsIntegerValue())
69 {
f376ac72 70 const Standard_Integer aValue = theTextureFileName.IntegerValue();
71 if (aValue < Graphic3d_Texture2D::NumberOfTextures()
72 && aValue >= 0)
7fd59977 73 {
f376ac72 74 myPredefTexture = Graphic3d_NameOfTexture2D (aValue);
7fd59977 75 }
2bd4c032 76 else
7fd59977 77 {
f376ac72 78 std::cout << "Texture " << theTextureFileName << " doesn't exist\n";
2bd4c032 79 std::cout << "Using Texture 0 instead ...\n";
80 myPredefTexture = Graphic3d_NameOfTexture2D (0);
7fd59977 81 }
2bd4c032 82 myTextureFile = "";
83 }
84 else
85 {
86 myTextureFile = theTextureFileName;
f376ac72 87 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
2bd4c032 88 }
7fd59977 89}
90
f376ac72 91//=======================================================================
92//function : SetTexturePixMap
93//purpose :
94//=======================================================================
95void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap)
96{
97 myTextureFile = "";
98 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
99 myTexturePixMap = theTexturePixMap;
100}
101
7fd59977 102//=======================================================================
103//function : SetTextureRepeat
2bd4c032 104//purpose :
7fd59977 105//=======================================================================
106
f376ac72 107void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat,
2bd4c032 108 const Standard_Real theURepeat,
109 const Standard_Real theVRepeat)
7fd59977 110{
f376ac72 111 myToRepeat = theToRepeat;
112 myUVRepeat.SetCoord (theURepeat, theVRepeat);
7fd59977 113}
114
115//=======================================================================
116//function : SetTextureMapOn
2bd4c032 117//purpose :
7fd59977 118//=======================================================================
119
120void AIS_TexturedShape::SetTextureMapOn()
121{
f376ac72 122 myToMapTexture = Standard_True;
7fd59977 123}
124
125//=======================================================================
126//function : SetTextureMapOff
2bd4c032 127//purpose :
7fd59977 128//=======================================================================
129
130void AIS_TexturedShape::SetTextureMapOff()
131{
f376ac72 132 myToMapTexture = Standard_False;
7fd59977 133}
134
135//=======================================================================
136//function : SetTextureOrigin
2bd4c032 137//purpose :
7fd59977 138//=======================================================================
139
f376ac72 140void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin,
2bd4c032 141 const Standard_Real theUOrigin,
142 const Standard_Real theVOrigin)
7fd59977 143{
f376ac72 144 myIsCustomOrigin = theToSetTextureOrigin;
145 myUVOrigin.SetCoord (theUOrigin, theVOrigin);
7fd59977 146}
147
148//=======================================================================
149//function : SetTextureScale
2bd4c032 150//purpose :
7fd59977 151//=======================================================================
152
f376ac72 153void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale,
2bd4c032 154 const Standard_Real theScaleU,
155 const Standard_Real theScaleV)
7fd59977 156{
f376ac72 157 myToScale = theToSetTextureScale;
158 myUVScale.SetCoord (theScaleU, theScaleV);
7fd59977 159}
160
161//=======================================================================
162//function : ShowTriangles
2bd4c032 163//purpose :
7fd59977 164//=======================================================================
165
f376ac72 166void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles)
7fd59977 167{
f376ac72 168 myToShowTriangles = theToShowTriangles;
7fd59977 169}
170
171//=======================================================================
172//function : EnableTextureModulate
2bd4c032 173//purpose :
7fd59977 174//=======================================================================
175
176void AIS_TexturedShape::EnableTextureModulate()
177{
178 myModulate = Standard_True;
179}
180
181//=======================================================================
182//function : DisableTextureModulate
bf75be98 183//purpose :
7fd59977 184//=======================================================================
185
186void AIS_TexturedShape::DisableTextureModulate()
187{
188 myModulate = Standard_False;
189}
190
191//=======================================================================
192//function : UpdateAttributes
2bd4c032 193//purpose :
7fd59977 194//=======================================================================
195
196void AIS_TexturedShape::UpdateAttributes()
8e3ebc7a 197{
198 updateAttributes (Presentation());
199}
200
201//=======================================================================
202//function : updateAttributes
203//purpose :
204//=======================================================================
205
206void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs)
7fd59977 207{
2bd4c032 208 Prs3d_ShadingAspect aDummy;
209 myAspect = aDummy.Aspect();
8e3ebc7a 210 if (HasPolygonOffsets())
211 {
212 // Issue 23115: copy polygon offset settings passed through myDrawer
213 Standard_Integer aMode;
214 Standard_ShortReal aFactor, aUnits;
215 PolygonOffsets (aMode, aFactor, aUnits);
216 myAspect->SetPolygonOffsets (aMode, aFactor, aUnits);
217 }
218
f376ac72 219 if (!myToMapTexture)
2bd4c032 220 {
221 myAspect->SetTextureMapOff();
222 return;
223 }
224
f376ac72 225 if (!myTexturePixMap.IsNull())
226 {
227 myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
228 }
229 else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
230 {
231 myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
232 }
7fd59977 233 else
f376ac72 234 {
235 myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
236 }
2bd4c032 237
7fd59977 238 myAspect->SetTextureMapOn();
f376ac72 239 myAspect->SetTextureMap (myTexture);
240 if (!myTexture->IsDone())
2bd4c032 241 {
8e3ebc7a 242 std::cout << "An error occurred while building texture\n";
243 myAspect->SetTextureMapOff();
2bd4c032 244 return;
245 }
246
d399d3c3 247 if (myModulate)
248 myTexture->EnableModulate();
249 else
250 myTexture->DisableModulate();
251
f376ac72 252 if (myToShowTriangles)
7fd59977 253 myAspect->SetEdgeOn();
254 else
255 myAspect->SetEdgeOff();
2bd4c032 256
8e3ebc7a 257 // manage back face culling in consistent way (as in StdPrs_ShadedShape::Add())
258 if (StdPrs_ToolShadedShape::IsClosed (myshape))
259 {
260 myAspect->SuppressBackFace();
261 }
262 else
263 {
264 myAspect->AllowBackFace();
265 }
266
267 if (!thePrs.IsNull())
268 {
269 Prs3d_Root::CurrentGroup (thePrs)->SetGroupPrimitivesAspect (myAspect);
270 }
7fd59977 271}
272
273//=======================================================================
274//function : Compute
2bd4c032 275//purpose :
7fd59977 276//=======================================================================
277
f376ac72 278void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
2bd4c032 279 const Handle(Prs3d_Presentation)& thePrs,
280 const Standard_Integer theMode)
7fd59977 281{
2bd4c032 282 thePrs->Clear();
283
284 if (myshape.IsNull())
285 {
286 return;
287 }
288
289 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
290 {
291 thePrs->SetVisual (Graphic3d_TOS_ALL);
292 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
293 }
7fd59977 294
295 if (myshape.ShapeType() == TopAbs_COMPOUND)
2bd4c032 296 {
297 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
298 if (!anExplor.More())
7fd59977 299 {
2bd4c032 300 return;
7fd59977 301 }
2bd4c032 302 }
7fd59977 303
304 if (IsInfinite())
2bd4c032 305 {
306 thePrs->SetInfiniteState (Standard_True);
307 }
7fd59977 308
2bd4c032 309 switch (theMode)
310 {
f376ac72 311 case AIS_WireFrame:
2bd4c032 312 {
313 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
7fd59977 314 break;
2bd4c032 315 }
f376ac72 316 case AIS_Shaded:
2bd4c032 317 {
318 Standard_Real prevangle;
319 Standard_Real newangle;
320 Standard_Real prevcoeff;
321 Standard_Real newcoeff;
7fd59977 322
bbf847ad
P
323 Standard_Boolean isOwnDeviationAngle = OwnDeviationAngle(newangle,prevangle);
324 Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient(newcoeff,prevcoeff);
325 if (((Abs (newangle - prevangle) > Precision::Angular()) && isOwnDeviationAngle) ||
326 ((Abs (newcoeff - prevcoeff) > Precision::Confusion()) && isOwnDeviationCoefficient)) {
327 BRepTools::Clean (myshape);
2bd4c032 328 }
329 if (myshape.ShapeType() > TopAbs_FACE)
7fd59977 330 {
2bd4c032 331 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
332 break;
7fd59977 333 }
2bd4c032 334 myDrawer->SetShadingAspectGlobal (Standard_False);
335 if (IsInfinite())
336 {
337 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
338 break;
339 }
340 try
341 {
342 OCC_CATCH_SIGNALS
343 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
344 }
345 catch (Standard_Failure)
346 {
347 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
348 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
349 }
350 break;
351 }
7fd59977 352 case 2: // Bounding box
2bd4c032 353 {
354 if (IsInfinite())
7fd59977 355 {
2bd4c032 356 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
7fd59977 357 }
2bd4c032 358 else
7fd59977 359 {
2bd4c032 360 AIS_Shape::DisplayBox (thePrs, BoundingBox(), myDrawer);
361 }
362 break;
363 }
2bd4c032 364 case 3: // texture mapping on triangulation
365 {
f376ac72 366 BRepTools::Clean (myshape);
2bd4c032 367 BRepTools::Update (myshape);
2bd4c032 368 try
369 {
370 OCC_CATCH_SIGNALS
371 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
f376ac72 372 Standard_True,
373 myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),
374 myUVRepeat,
375 myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0));
8e3ebc7a 376
377 updateAttributes (thePrs);
2bd4c032 378 }
379 catch (Standard_Failure)
380 {
8e3ebc7a 381 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed\n";
2bd4c032 382 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
383 }
384 break;
385 }
386 }
387}