0024624: Lost word in license statement in source files
[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
16// Modified:
7fd59977 17////////////////////////////////////////////////////////////////////////
18
19
20#include <AIS_TexturedShape.ixx>
21#include <Standard_ErrorHandler.hxx>
22
2bd4c032 23#include <AIS_Drawer.hxx>
24#include <AIS_InteractiveContext.hxx>
7fd59977 25#include <BRepTools.hxx>
2bd4c032 26#include <gp_Pnt2d.hxx>
27#include <Graphic3d_AspectFillArea3d.hxx>
28#include <Graphic3d_Group.hxx>
7fd59977 29#include <Graphic3d_StructureManager.hxx>
30#include <Graphic3d_Texture2Dmanual.hxx>
2bd4c032 31#include <Precision.hxx>
32#include <Prs3d_Presentation.hxx>
7fd59977 33#include <Prs3d_Root.hxx>
2bd4c032 34#include <Prs3d_ShadingAspect.hxx>
7fd59977 35#include <PrsMgr_PresentationManager3d.hxx>
2bd4c032 36#include <StdPrs_ShadedShape.hxx>
7fd59977 37#include <StdPrs_WFDeflectionShape.hxx>
7fd59977 38#include <StdPrs_WFShape.hxx>
2bd4c032 39#include <TopExp_Explorer.hxx>
7fd59977 40
41//=======================================================================
42//function : AIS_TexturedShape
2bd4c032 43//purpose :
7fd59977 44//=======================================================================
2bd4c032 45AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
46: AIS_Shape (theShape),
47 myPredefTexture (Graphic3d_NameOfTexture2D(0)),
48 myTextureFile (""),
49 DoRepeat (Standard_True),
50 myURepeat (1.0),
51 myVRepeat (1.0),
52 DoMapTexture (Standard_True),
53 DoSetTextureOrigin (Standard_True),
54 myUOrigin (0.0),
55 myVOrigin (0.0),
56 DoSetTextureScale (Standard_True),
57 myScaleU (1.0),
58 myScaleV (1.0),
59 DoShowTriangles (Standard_False),
60 myModulate (Standard_True)
7fd59977 61{
62}
63
7fd59977 64//=======================================================================
65//function : SetTextureFileName
2bd4c032 66//purpose :
7fd59977 67//=======================================================================
2bd4c032 68void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
7fd59977 69{
2bd4c032 70 if (theTextureFileName.IsIntegerValue())
71 {
72 if (theTextureFileName.IntegerValue() < Graphic3d_Texture2D::NumberOfTextures()
73 && theTextureFileName.IntegerValue() >= 0)
7fd59977 74 {
2bd4c032 75 myPredefTexture = Graphic3d_NameOfTexture2D (theTextureFileName.IntegerValue());
7fd59977 76 }
2bd4c032 77 else
7fd59977 78 {
2bd4c032 79 std::cout << "Texture " << theTextureFileName << " doesn't exist \n";
80 std::cout << "Using Texture 0 instead ...\n";
81 myPredefTexture = Graphic3d_NameOfTexture2D (0);
7fd59977 82 }
2bd4c032 83 myTextureFile = "";
84 }
85 else
86 {
87 myTextureFile = theTextureFileName;
88 myPredefTexture = Graphic3d_NameOfTexture2D (-1);
89 }
7fd59977 90}
91
92//=======================================================================
93//function : SetTextureRepeat
2bd4c032 94//purpose :
7fd59977 95//=======================================================================
96
2bd4c032 97void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theRepeatYN,
98 const Standard_Real theURepeat,
99 const Standard_Real theVRepeat)
7fd59977 100{
2bd4c032 101 DoRepeat = theRepeatYN;
102 myURepeat = theURepeat;
103 myVRepeat = theVRepeat;
7fd59977 104}
105
106//=======================================================================
107//function : SetTextureMapOn
2bd4c032 108//purpose :
7fd59977 109//=======================================================================
110
111void AIS_TexturedShape::SetTextureMapOn()
112{
113 DoMapTexture = Standard_True;
114}
115
116//=======================================================================
117//function : SetTextureMapOff
2bd4c032 118//purpose :
7fd59977 119//=======================================================================
120
121void AIS_TexturedShape::SetTextureMapOff()
122{
123 DoMapTexture = Standard_False;
124}
125
126//=======================================================================
127//function : SetTextureOrigin
2bd4c032 128//purpose :
7fd59977 129//=======================================================================
130
2bd4c032 131void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean toSetTextureOriginYN,
132 const Standard_Real theUOrigin,
133 const Standard_Real theVOrigin)
7fd59977 134{
2bd4c032 135 DoSetTextureOrigin = toSetTextureOriginYN;
136 myUOrigin = theUOrigin;
137 myVOrigin = theVOrigin;
7fd59977 138}
139
140//=======================================================================
141//function : SetTextureScale
2bd4c032 142//purpose :
7fd59977 143//=======================================================================
144
2bd4c032 145void AIS_TexturedShape::SetTextureScale (const Standard_Boolean toSetTextureScaleYN,
146 const Standard_Real theScaleU,
147 const Standard_Real theScaleV)
7fd59977 148{
2bd4c032 149 DoSetTextureScale = toSetTextureScaleYN;
150 myScaleU = theScaleU;
151 myScaleV = theScaleV;
7fd59977 152}
153
154//=======================================================================
155//function : ShowTriangles
2bd4c032 156//purpose :
7fd59977 157//=======================================================================
158
2bd4c032 159void AIS_TexturedShape::ShowTriangles (const Standard_Boolean toShowTrianglesYN)
7fd59977 160{
2bd4c032 161 DoShowTriangles = toShowTrianglesYN;
7fd59977 162}
163
164//=======================================================================
165//function : EnableTextureModulate
2bd4c032 166//purpose :
7fd59977 167//=======================================================================
168
169void AIS_TexturedShape::EnableTextureModulate()
170{
171 myModulate = Standard_True;
172}
173
174//=======================================================================
175//function : DisableTextureModulate
bf75be98 176//purpose :
7fd59977 177//=======================================================================
178
179void AIS_TexturedShape::DisableTextureModulate()
180{
181 myModulate = Standard_False;
182}
183
184//=======================================================================
185//function : UpdateAttributes
2bd4c032 186//purpose :
7fd59977 187//=======================================================================
188
189void AIS_TexturedShape::UpdateAttributes()
190{
2bd4c032 191 Prs3d_ShadingAspect aDummy;
192 myAspect = aDummy.Aspect();
7fd59977 193 Handle(Prs3d_Presentation) aPrs = Presentation();
194 if (!DoMapTexture)
2bd4c032 195 {
196 myAspect->SetTextureMapOff();
197 return;
198 }
199
200 if (myPredefTexture != -1)
bf75be98 201 mytexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
7fd59977 202 else
bf75be98 203 mytexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
2bd4c032 204
7fd59977 205 myAspect->SetTextureMapOn();
2bd4c032 206
207 myAspect->SetTextureMap (mytexture);
7fd59977 208 if (!mytexture->IsDone())
2bd4c032 209 {
210 std::cout << "An error occured while building texture \n";
211 return;
212 }
213
7fd59977 214 if (DoShowTriangles)
215 myAspect->SetEdgeOn();
216 else
217 myAspect->SetEdgeOff();
2bd4c032 218
219 Prs3d_Root::CurrentGroup (aPrs)->SetGroupPrimitivesAspect (myAspect);
7fd59977 220}
221
222//=======================================================================
223//function : Compute
2bd4c032 224//purpose :
7fd59977 225//=======================================================================
226
2bd4c032 227void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePresManager*/,
228 const Handle(Prs3d_Presentation)& thePrs,
229 const Standard_Integer theMode)
7fd59977 230{
2bd4c032 231 thePrs->Clear();
232
233 if (myshape.IsNull())
234 {
235 return;
236 }
237
238 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
239 {
240 thePrs->SetVisual (Graphic3d_TOS_ALL);
241 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
242 }
7fd59977 243
244 if (myshape.ShapeType() == TopAbs_COMPOUND)
2bd4c032 245 {
246 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
247 if (!anExplor.More())
7fd59977 248 {
2bd4c032 249 return;
7fd59977 250 }
2bd4c032 251 }
7fd59977 252
253 if (IsInfinite())
2bd4c032 254 {
255 thePrs->SetInfiniteState (Standard_True);
256 }
7fd59977 257
2bd4c032 258 switch (theMode)
259 {
7fd59977 260 case 0: // Wireframe
2bd4c032 261 {
262 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
7fd59977 263 break;
2bd4c032 264 }
265 case 1: // Shading
266 {
267 Standard_Real prevangle;
268 Standard_Real newangle;
269 Standard_Real prevcoeff;
270 Standard_Real newcoeff;
7fd59977 271
bbf847ad
P
272 Standard_Boolean isOwnDeviationAngle = OwnDeviationAngle(newangle,prevangle);
273 Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient(newcoeff,prevcoeff);
274 if (((Abs (newangle - prevangle) > Precision::Angular()) && isOwnDeviationAngle) ||
275 ((Abs (newcoeff - prevcoeff) > Precision::Confusion()) && isOwnDeviationCoefficient)) {
276 BRepTools::Clean (myshape);
2bd4c032 277 }
278 if (myshape.ShapeType() > TopAbs_FACE)
7fd59977 279 {
2bd4c032 280 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
281 break;
7fd59977 282 }
2bd4c032 283 myDrawer->SetShadingAspectGlobal (Standard_False);
284 if (IsInfinite())
285 {
286 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
287 break;
288 }
289 try
290 {
291 OCC_CATCH_SIGNALS
292 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
293 }
294 catch (Standard_Failure)
295 {
296 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
297 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
298 }
299 break;
300 }
7fd59977 301
302 case 2: // Bounding box
2bd4c032 303 {
304 if (IsInfinite())
7fd59977 305 {
2bd4c032 306 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
7fd59977 307 }
2bd4c032 308 else
7fd59977 309 {
2bd4c032 310 AIS_Shape::DisplayBox (thePrs, BoundingBox(), myDrawer);
311 }
312 break;
313 }
314
315 case 3: // texture mapping on triangulation
316 {
317 BRepTools::Clean (myshape);
318 BRepTools::Update (myshape);
7fd59977 319
2bd4c032 320 {
321 Handle(Prs3d_ShadingAspect) aPrs3d_ShadingAspect = new Prs3d_ShadingAspect();
322 myAspect = aPrs3d_ShadingAspect->Aspect();
60761105 323
324 // Issue 23115: copy polygon offset settings passed through myDrawer
325 if (HasPolygonOffsets())
326 {
327 Standard_Integer aMode;
60be1f9b 328 Standard_ShortReal aFactor, aUnits;
60761105 329 PolygonOffsets(aMode, aFactor, aUnits);
330 myAspect->SetPolygonOffsets(aMode, aFactor, aUnits);
331 }
2bd4c032 332 }
333 if (!DoMapTexture)
334 {
335 myAspect->SetTextureMapOff();
336 return;
337 }
338 myAspect->SetTextureMapOn();
7fd59977 339
2bd4c032 340 if (myPredefTexture != -1)
bf75be98 341 mytexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
2bd4c032 342 else
bf75be98 343 mytexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
7fd59977 344
2bd4c032 345 if (!mytexture->IsDone())
346 {
347 std::cout << "An error occured while building texture \n";
348 return;
349 }
7fd59977 350
2bd4c032 351 if (myModulate)
352 mytexture->EnableModulate();
353 else
354 mytexture->DisableModulate();
7fd59977 355
2bd4c032 356 myAspect->SetTextureMap (mytexture);
357 if (DoShowTriangles)
358 myAspect->SetEdgeOn();
359 else
360 myAspect->SetEdgeOff();
7fd59977 361
2bd4c032 362 if (DoRepeat)
363 mytexture->EnableRepeat();
364 else
365 mytexture->DisableRepeat();
7fd59977 366
2bd4c032 367 const gp_Pnt2d aUVOrigin (myUOrigin, myVOrigin);
368 const gp_Pnt2d aUVRepeat (myURepeat, myVRepeat);
369 const gp_Pnt2d aUVScale (myScaleU, myScaleV);
370 try
371 {
372 OCC_CATCH_SIGNALS
373 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
374 Standard_True, aUVOrigin, aUVRepeat, aUVScale);
375 // within primitive arrays - object should be in one group of primitives
376 Prs3d_Root::CurrentGroup (thePrs)->SetGroupPrimitivesAspect (myAspect);
377 }
378 catch (Standard_Failure)
379 {
380 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
381 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
382 }
383 break;
384 }
385 }
386}
7fd59977 387
388Standard_Boolean AIS_TexturedShape::TextureMapState() const
389{
390 return DoMapTexture;
391}
392
393Standard_Real AIS_TexturedShape::URepeat() const
394{
395 return myURepeat;
396}
397
398Standard_Boolean AIS_TexturedShape::TextureRepeat() const
399{
400 return DoRepeat;
401}
402
403Standard_Real AIS_TexturedShape::Deflection() const
404{
405 return myDeflection;
406}
407
408Standard_CString AIS_TexturedShape::TextureFile() const
409{
410 return myTextureFile.ToCString();
411}
412
413Standard_Real AIS_TexturedShape::VRepeat() const
414{
415 return myVRepeat;
416}
2bd4c032 417
7fd59977 418Standard_Boolean AIS_TexturedShape::ShowTriangles() const
419{
420 return DoShowTriangles;
421}
2bd4c032 422
7fd59977 423Standard_Real AIS_TexturedShape::TextureUOrigin() const
424{
425 return myUOrigin;
426}
2bd4c032 427
7fd59977 428Standard_Real AIS_TexturedShape::TextureVOrigin() const
429{
430 return myVOrigin;
431}
2bd4c032 432
7fd59977 433Standard_Real AIS_TexturedShape::TextureScaleU() const
434{
435 return myScaleU;
436}
2bd4c032 437
7fd59977 438Standard_Real AIS_TexturedShape::TextureScaleV() const
439{
440 return myScaleV;
441}
2bd4c032 442
443Standard_Boolean AIS_TexturedShape::TextureScale() const
7fd59977 444{
445 return DoSetTextureScale;
446}
2bd4c032 447
448Standard_Boolean AIS_TexturedShape::TextureOrigin() const
7fd59977 449{
450 return DoSetTextureOrigin;
451}
2bd4c032 452
453Standard_Boolean AIS_TexturedShape::TextureModulate() const
7fd59977 454{
455 return myModulate;
456}