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