0029570: Visualization, Graphic3d_Aspect - merge Graphic3d_Group aspects
[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
735dc1d2 18#include <AIS_GraphicTool.hxx>
2bd4c032 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>
124ee9c9 26#include <Message.hxx>
27#include <Message_Messenger.hxx>
2bd4c032 28#include <Precision.hxx>
6262338c 29#include <Prs3d_Drawer.hxx>
2bd4c032 30#include <Prs3d_Presentation.hxx>
735dc1d2 31#include <PrsMgr_ModedPresentation.hxx>
7fd59977 32#include <Prs3d_Root.hxx>
735dc1d2 33#include <Prs3d_LineAspect.hxx>
2bd4c032 34#include <Prs3d_ShadingAspect.hxx>
7fd59977 35#include <PrsMgr_PresentationManager3d.hxx>
f376ac72 36#include <Standard_ErrorHandler.hxx>
5ad8c033 37#include <StdPrs_BndBox.hxx>
2bd4c032 38#include <StdPrs_ShadedShape.hxx>
5ad8c033 39#include <StdPrs_ToolTriangulatedShape.hxx>
7fd59977 40#include <StdPrs_WFShape.hxx>
2bd4c032 41#include <TopExp_Explorer.hxx>
7fd59977 42
f376ac72 43
92efcf78 44IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape,AIS_Shape)
45
7fd59977 46//=======================================================================
47//function : AIS_TexturedShape
2bd4c032 48//purpose :
7fd59977 49//=======================================================================
2bd4c032 50AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
f376ac72 51: AIS_Shape (theShape),
52 myPredefTexture (Graphic3d_NameOfTexture2D(0)),
53 myToMapTexture (Standard_True),
54 myModulate (Standard_True),
f376ac72 55 myIsCustomOrigin (Standard_True),
f376ac72 56 myToRepeat (Standard_True),
f376ac72 57 myToScale (Standard_True),
58 myToShowTriangles (Standard_False)
7fd59977 59{
60}
61
7fd59977 62//=======================================================================
63//function : SetTextureFileName
2bd4c032 64//purpose :
7fd59977 65//=======================================================================
2bd4c032 66void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
7fd59977 67{
f376ac72 68 myTexturePixMap.Nullify();
69
2bd4c032 70 if (theTextureFileName.IsIntegerValue())
71 {
f376ac72 72 const Standard_Integer aValue = theTextureFileName.IntegerValue();
73 if (aValue < Graphic3d_Texture2D::NumberOfTextures()
74 && aValue >= 0)
7fd59977 75 {
f376ac72 76 myPredefTexture = Graphic3d_NameOfTexture2D (aValue);
7fd59977 77 }
2bd4c032 78 else
7fd59977 79 {
124ee9c9 80 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: texture with ID ") + theTextureFileName
81 + " is undefined! Texture 0 will be used instead.", Message_Fail);
2bd4c032 82 myPredefTexture = Graphic3d_NameOfTexture2D (0);
7fd59977 83 }
2bd4c032 84 myTextureFile = "";
85 }
86 else
87 {
88 myTextureFile = theTextureFileName;
f376ac72 89 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
2bd4c032 90 }
7fd59977 91}
92
f376ac72 93//=======================================================================
94//function : SetTexturePixMap
95//purpose :
96//=======================================================================
97void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap)
98{
99 myTextureFile = "";
100 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
101 myTexturePixMap = theTexturePixMap;
102}
103
7fd59977 104//=======================================================================
105//function : SetTextureRepeat
2bd4c032 106//purpose :
7fd59977 107//=======================================================================
108
f376ac72 109void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat,
2bd4c032 110 const Standard_Real theURepeat,
111 const Standard_Real theVRepeat)
7fd59977 112{
f376ac72 113 myToRepeat = theToRepeat;
114 myUVRepeat.SetCoord (theURepeat, theVRepeat);
7fd59977 115}
116
117//=======================================================================
118//function : SetTextureMapOn
2bd4c032 119//purpose :
7fd59977 120//=======================================================================
121
122void AIS_TexturedShape::SetTextureMapOn()
123{
f376ac72 124 myToMapTexture = Standard_True;
7fd59977 125}
126
127//=======================================================================
128//function : SetTextureMapOff
2bd4c032 129//purpose :
7fd59977 130//=======================================================================
131
132void AIS_TexturedShape::SetTextureMapOff()
133{
f376ac72 134 myToMapTexture = Standard_False;
7fd59977 135}
136
137//=======================================================================
138//function : SetTextureOrigin
2bd4c032 139//purpose :
7fd59977 140//=======================================================================
141
f376ac72 142void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin,
2bd4c032 143 const Standard_Real theUOrigin,
144 const Standard_Real theVOrigin)
7fd59977 145{
f376ac72 146 myIsCustomOrigin = theToSetTextureOrigin;
147 myUVOrigin.SetCoord (theUOrigin, theVOrigin);
7fd59977 148}
149
150//=======================================================================
151//function : SetTextureScale
2bd4c032 152//purpose :
7fd59977 153//=======================================================================
154
f376ac72 155void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale,
2bd4c032 156 const Standard_Real theScaleU,
157 const Standard_Real theScaleV)
7fd59977 158{
f376ac72 159 myToScale = theToSetTextureScale;
160 myUVScale.SetCoord (theScaleU, theScaleV);
7fd59977 161}
162
163//=======================================================================
164//function : ShowTriangles
2bd4c032 165//purpose :
7fd59977 166//=======================================================================
167
f376ac72 168void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles)
7fd59977 169{
f376ac72 170 myToShowTriangles = theToShowTriangles;
7fd59977 171}
172
173//=======================================================================
174//function : EnableTextureModulate
2bd4c032 175//purpose :
7fd59977 176//=======================================================================
177
178void AIS_TexturedShape::EnableTextureModulate()
179{
180 myModulate = Standard_True;
181}
182
183//=======================================================================
184//function : DisableTextureModulate
bf75be98 185//purpose :
7fd59977 186//=======================================================================
187
188void AIS_TexturedShape::DisableTextureModulate()
189{
190 myModulate = Standard_False;
191}
192
7389f96d 193//=======================================================================
194//function : SetColor
195//purpose :
196//=======================================================================
197
198void AIS_TexturedShape::SetColor (const Quantity_Color& theColor)
199{
200 AIS_Shape::SetColor (theColor);
201
735dc1d2 202 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
7389f96d 203 {
735dc1d2 204 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
205
206 if (aPrsModed.Mode() != 3)
207 continue;
208
209 updateAttributes (aPrsModed.Presentation()->Presentation());
210 }
211}
212
213//=======================================================================
214//function : UnsetColor
215//purpose :
216//=======================================================================
217
218void AIS_TexturedShape::UnsetColor()
219{
220 AIS_Shape::UnsetColor();
735dc1d2 221}
222
223//=======================================================================
224//function : SetMaterial
225//purpose :
226//=======================================================================
227
228void AIS_TexturedShape::SetMaterial (const Graphic3d_MaterialAspect& theMat)
229{
230 AIS_Shape::SetMaterial (theMat);
231
232 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
233 {
234 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
235
236 if (aPrsModed.Mode() != 3)
237 continue;
238
239 updateAttributes (aPrsModed.Presentation()->Presentation());
240 }
241}
242
243//=======================================================================
244//function : UnsetMaterial
245//purpose :
246//=======================================================================
247void AIS_TexturedShape::UnsetMaterial()
248{
249 AIS_Shape::UnsetMaterial();
250
251 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
252 {
253 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
254
255 if (aPrsModed.Mode() != 3)
256 continue;
257
258 updateAttributes (aPrsModed.Presentation()->Presentation());
7389f96d 259 }
260}
261
7fd59977 262//=======================================================================
263//function : UpdateAttributes
2bd4c032 264//purpose :
7fd59977 265//=======================================================================
266
267void AIS_TexturedShape::UpdateAttributes()
8e3ebc7a 268{
269 updateAttributes (Presentation());
270}
271
272//=======================================================================
273//function : updateAttributes
274//purpose :
275//=======================================================================
276
277void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs)
7fd59977 278{
7389f96d 279 myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect());
8e3ebc7a 280 if (HasPolygonOffsets())
281 {
8e3ebc7a 282 Standard_Integer aMode;
283 Standard_ShortReal aFactor, aUnits;
284 PolygonOffsets (aMode, aFactor, aUnits);
285 myAspect->SetPolygonOffsets (aMode, aFactor, aUnits);
286 }
287
124ee9c9 288 Standard_Boolean hasTexture = Standard_False;
289 if (myToMapTexture)
2bd4c032 290 {
124ee9c9 291 TCollection_AsciiString aTextureDesc;
292 if (!myTexturePixMap.IsNull())
293 {
294 myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
295 aTextureDesc = " (custom image)";
296 }
297 else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
298 {
299 myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
300 aTextureDesc = TCollection_AsciiString(" (predefined texture ") + myTexture->GetId() + ")";
301 }
302 else
303 {
304 myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
305 aTextureDesc = TCollection_AsciiString(" (") + myTextureFile + ")";
306 }
2bd4c032 307
124ee9c9 308 if (myModulate)
309 {
310 myTexture->EnableModulate();
311 }
312 else
313 {
314 myTexture->DisableModulate();
315 }
316
317 if (myTexture->IsDone())
318 {
319 hasTexture = Standard_True;
320 }
321 else
322 {
323 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: texture can not be loaded ") + aTextureDesc, Message_Fail);
324 }
f376ac72 325 }
124ee9c9 326
327 myAspect->SetTextureMap (myTexture);
328 if (hasTexture)
f376ac72 329 {
124ee9c9 330 myAspect->SetTextureMapOn();
f376ac72 331 }
7fd59977 332 else
f376ac72 333 {
8e3ebc7a 334 myAspect->SetTextureMapOff();
2bd4c032 335 }
336
f376ac72 337 if (myToShowTriangles)
8e3ebc7a 338 {
124ee9c9 339 myAspect->SetEdgeOn();
8e3ebc7a 340 }
341 else
342 {
124ee9c9 343 myAspect->SetEdgeOff();
8e3ebc7a 344 }
345
735dc1d2 346 // Go through all groups to change fill aspect for all primitives
347 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (thePrs->Groups()); aGroupIt.More(); aGroupIt.Next())
8e3ebc7a 348 {
735dc1d2 349 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
124ee9c9 350 aGroup->SetGroupPrimitivesAspect (myAspect);
8e3ebc7a 351 }
7fd59977 352}
353
354//=======================================================================
355//function : Compute
2bd4c032 356//purpose :
7fd59977 357//=======================================================================
358
f376ac72 359void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
2bd4c032 360 const Handle(Prs3d_Presentation)& thePrs,
361 const Standard_Integer theMode)
7fd59977 362{
2bd4c032 363 if (myshape.IsNull())
364 {
365 return;
366 }
367
368 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
369 {
370 thePrs->SetVisual (Graphic3d_TOS_ALL);
371 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
372 }
7fd59977 373
374 if (myshape.ShapeType() == TopAbs_COMPOUND)
2bd4c032 375 {
376 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
377 if (!anExplor.More())
7fd59977 378 {
2bd4c032 379 return;
7fd59977 380 }
2bd4c032 381 }
7fd59977 382
383 if (IsInfinite())
2bd4c032 384 {
385 thePrs->SetInfiniteState (Standard_True);
386 }
7fd59977 387
2bd4c032 388 switch (theMode)
389 {
f376ac72 390 case AIS_WireFrame:
2bd4c032 391 {
83b0f13a 392 StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
5ad8c033 393 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
7fd59977 394 break;
2bd4c032 395 }
f376ac72 396 case AIS_Shaded:
50b830a0 397 case 3: // texture mapping on triangulation
2bd4c032 398 {
83b0f13a 399 StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
2bd4c032 400 if (myshape.ShapeType() > TopAbs_FACE)
7fd59977 401 {
5ad8c033 402 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
2bd4c032 403 break;
7fd59977 404 }
50b830a0 405
2bd4c032 406 if (IsInfinite())
407 {
5ad8c033 408 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
2bd4c032 409 break;
410 }
411 try
412 {
413 OCC_CATCH_SIGNALS
50b830a0 414 if (theMode == AIS_Shaded)
415 {
416 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
417 }
418 else
419 {
420 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
421 Standard_True,
422 myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),
423 myUVRepeat,
424 myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0));
425 updateAttributes (thePrs);
426 }
2bd4c032 427 }
428 catch (Standard_Failure)
429 {
0797d9d3 430#ifdef OCCT_DEBUG
2bd4c032 431 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
63c629aa 432#endif
2bd4c032 433 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
434 }
435 break;
436 }
7fd59977 437 case 2: // Bounding box
2bd4c032 438 {
439 if (IsInfinite())
7fd59977 440 {
5ad8c033 441 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
7fd59977 442 }
2bd4c032 443 else
7fd59977 444 {
5ad8c033 445 StdPrs_BndBox::Add (thePrs, BoundingBox(), myDrawer);
2bd4c032 446 }
447 break;
448 }
2bd4c032 449 }
450}