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