1a548b53d974e263a2314c61bf39636280dc8f9f
[occt.git] / samples / mfc / occtdemo / TexturesExt / TexturesExt_Presentation.cpp
1 // TexturesExt_Presentation.cpp: implementation of the TexturesExt_Presentation class.
2 // Creation of textural presentation of shape
3 //////////////////////////////////////////////////////////////////////
4
5 #include "stdafx.h"
6 #include "TexturesExt_Presentation.h"
7
8 #include <TopExp.hxx>
9 #include <TopTools_IndexedMapOfShape.hxx>
10 #include <BRepBuilderAPI_MakeFace.hxx>
11 #include <AIS_TexturedShape.hxx>
12 #include <BRepTools.hxx>
13 #include <Graphic3d_Texture2D.hxx>
14 #include <BRep_Tool.hxx>
15 #include <TopoDS.hxx>
16 #include <BRepBuilderAPI_Transform.hxx>
17 #include <BRep_Builder.hxx>
18 #include <BRepTools.hxx>
19 #include <Geom_Surface.hxx>
20 #include <TopoDS_Face.hxx>
21 #include <V3d_DirectionalLight.hxx>
22
23 #define DISP(OBJ) getAISContext()->Display((OBJ), Standard_False)
24
25 // Initialization of global variable with an instance of this class
26 OCCDemo_Presentation* OCCDemo_Presentation::Current = new TexturesExt_Presentation;
27
28 // Initialization of array of samples
29 const TexturesExt_Presentation::PSampleFuncType TexturesExt_Presentation::SampleFuncs[] =
30 {
31   &TexturesExt_Presentation::sampleBottle,
32   &TexturesExt_Presentation::sampleTerrain,
33   &TexturesExt_Presentation::sampleKitchen
34 };
35
36 #ifdef WNT
37  #define EOL "\r\n"
38 #else
39  #define EOL "\n"
40 #endif
41
42 #define ZVIEW_SIZE 100
43
44 //////////////////////////////////////////////////////////////////////
45 // Construction/Destruction
46 //////////////////////////////////////////////////////////////////////
47
48 TexturesExt_Presentation::TexturesExt_Presentation()
49 {
50   myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
51   setName ("Textured Shapes");
52 }
53
54 //////////////////////////////////////////////////////////////////////
55 // Sample execution
56 //////////////////////////////////////////////////////////////////////
57
58 void TexturesExt_Presentation::DoSample()
59 {
60   getAISContext()->EraseAll();
61   if (myIndex >=0 && myIndex < myNbSamples)
62   {
63     // turn lights on for terrain sample
64     lightsOnOff(myIndex==1);
65     (this->*SampleFuncs[myIndex])();
66   }
67 }
68
69 void TexturesExt_Presentation::Init()
70 {
71   // initialize v3d_view so it displays TexturesExt well
72   getViewer()->InitActiveViews();
73   Handle(V3d_View) aView = getViewer()->ActiveView();
74   aView->SetSize(ZVIEW_SIZE);
75
76   setResultTitle("Textured Shape");
77   setResultText(
78     "  TopoDS_Shape aShape;" EOL
79     "" EOL
80     "  // initialize aShape" EOL
81     "  // aShape = ..." EOL
82     "" EOL
83     "  // create a textured presentation object for aShape" EOL
84     "  Handle(AIS_TexturedShape) aTShape = new AIS_TexturedShape(aShape);" EOL
85     "" EOL
86     "  TCollection_AsciiString aTFileName;" EOL
87     "" EOL
88     "  // initialize aTFileName with an existing texture file name" EOL
89     "  // (gif, bmp, xwd, rgb, and other formats are supported)" EOL
90     "  // OR with an integer value string (max = Graphic3d_Texture2D::NumberOfTexturesExt())" EOL
91     "  // which will indicate use of predefined texture of this number" EOL
92     "  // aTFileName = ..." EOL
93     "" EOL
94     "  aTShape->SetTextureFileName(aTFileName);" EOL
95     "" EOL
96     "  // do other initialization of AIS_TexturedShape" EOL
97     "  Standard_Real nRepeat;" EOL
98     "  Standard_Boolean toRepeat;" EOL
99     "  Standard_Boolean toScale;" EOL
100     "  // initialize aRepeat, toRepeat, toScale ..." EOL
101     "" EOL
102     "  aTShape->SetTextureMapOn();" EOL
103     "  aTShape->SetTextureRepeat(toRepeat, nRepeat, nRepeat);" EOL
104     "  aTShape->SetTexturesExtcale(toScale);" EOL
105     "  " EOL
106     "  // mode 3 is \"textured\" mode of AIS_TexturedShape, " EOL
107     "  // other modes will display the \"normal\", non-textured shape," EOL
108     "  // in wireframe(1) or shaded(2) modes correspondingly" EOL
109     "  aTShape->SetDisplayMode(3); " EOL);
110 }
111
112 //////////////////////////////////////////////////////////////////////
113 // Sample functions
114 //////////////////////////////////////////////////////////////////////
115 //================================================================
116 // Function : TexturesExt_Presentation::Texturize
117 // display an AIS_TexturedShape based on a given shape with texture with given filename
118 // filename can also be an integer value ("2", "5", etc.), in this case
119 // a predefined texture from Graphic3d_NameOfTexture2D with number = this value
120 // is loaded.
121 //================================================================
122 Handle(AIS_TexturedShape) TexturesExt_Presentation::Texturize(const TopoDS_Shape& aShape,
123                                                         TCollection_AsciiString aTFileName,
124                                                         Standard_Real toScaleU,
125                                                         Standard_Real toScaleV,
126                                                         Standard_Real toRepeatU,
127                                                         Standard_Real toRepeatV,
128                                                         Standard_Real originU,
129                                                         Standard_Real originV)
130 {
131   // create a textured presentation object for aShape
132   Handle(AIS_TexturedShape) aTShape = new AIS_TexturedShape(aShape);
133
134   // load texture from file if it is not an integer value
135   // integer value indicates a number of texture in predefined TexturesExt enumeration
136   if (!aTFileName.IsIntegerValue())
137   {
138     TCollection_AsciiString aTmp(aTFileName);
139     aTFileName = GetDataDir();
140     aTFileName = aTFileName + "\\" + aTmp;
141   }
142
143   aTShape->SetTextureFileName(aTFileName);
144
145   // do other initialization of AIS_TexturedShape
146   aTShape->SetTextureMapOn();
147   aTShape->SetTextureScale(Standard_True, toScaleU, toScaleV);
148   aTShape->SetTextureRepeat(Standard_True, toRepeatU, toRepeatV);
149   aTShape->SetTextureOrigin(Standard_True, originU, originV);
150   
151   aTShape->SetDisplayMode(3); // mode 3 is "textured" mode
152
153   return aTShape;
154 }
155
156
157 //================================================================
158 // Function : TexturesExt_Presentation::loadShape
159 // loads a shape from a given brep file from data dir into a given TopoDS_Shape object
160 //================================================================
161 Standard_Boolean TexturesExt_Presentation::loadShape(TopoDS_Shape& aShape, 
162                                          TCollection_AsciiString aFileName)
163 {
164   // create a TopoDS_Shape -> read from a brep file
165   TCollection_AsciiString aPath(GetDataDir());
166   aPath = aPath + "\\" + aFileName;
167
168   BRep_Builder aBld;
169   Standard_Boolean isRead = BRepTools::Read (aShape, aPath.ToCString(), aBld);
170   if (!isRead)
171   {
172     aPath += " was not found.  The sample can not be shown.";
173     setResultText(aPath.ToCString());
174     return Standard_False;
175   }
176
177   return Standard_True;
178 }
179
180 //================================================================
181 // Function : lightsOnOff
182 // Purpose  : 6 lights are used for a brighter demonstration of textured shapes
183 //            call lightsOnOff(false) before showing normal shape
184 //            call lightsOnOff(true)  before showing textured shape
185 //================================================================
186 void TexturesExt_Presentation::lightsOnOff(Standard_Boolean isOn)
187 {
188   static Handle(V3d_Light) aLight1 = new V3d_DirectionalLight(getViewer(), V3d_XnegYposZneg);
189   static Handle(V3d_Light) aLight2 = new V3d_DirectionalLight(getViewer(), V3d_XnegYnegZpos);
190   static Handle(V3d_Light) aLight3 = new V3d_DirectionalLight(getViewer(), V3d_XposYnegZpos);
191   static Handle(V3d_Light) aLight4 = new V3d_DirectionalLight(getViewer(), V3d_XnegYnegZneg);
192   static Handle(V3d_Light) aLight5 = new V3d_DirectionalLight(getViewer(), V3d_XnegYposZpos);
193   static Handle(V3d_Light) aLight6 = new V3d_DirectionalLight(getViewer(), V3d_XposYposZpos);
194
195   if (isOn)
196   {
197     getViewer()->SetLightOn(aLight1);
198     getViewer()->SetLightOn(aLight2);
199     getViewer()->SetLightOn(aLight3);
200     getViewer()->SetLightOn(aLight4);
201     getViewer()->SetLightOn(aLight5);
202     getViewer()->SetLightOn(aLight6);
203   }
204   else 
205   {
206     getViewer()->SetLightOff(aLight1);
207     getViewer()->SetLightOff(aLight2);
208     getViewer()->SetLightOff(aLight3);
209     getViewer()->SetLightOff(aLight4);
210     getViewer()->SetLightOff(aLight5);
211     getViewer()->SetLightOff(aLight6);
212   }
213 }
214
215 //================================================================
216 // Function : TexturesExt_Presentation::sampleBottle
217 // Purpose  : 
218 //================================================================
219 void TexturesExt_Presentation::sampleBottle()
220 {  
221   TopoDS_Shape aShape;
222   if (!loadShape(aShape, "bottle.brep")) 
223     return;
224
225   // resize and move the shape to the center of the viewer
226   gp_Trsf aTrsf;
227   aTrsf.SetScale(gp_Pnt(0,0,0), 0.8);
228   aShape.Move(TopLoc_Location(aTrsf));
229   aTrsf.SetTranslation(gp_Pnt(0,0,0),gp_Pnt(0,0,-20));
230   aShape.Move(TopLoc_Location(aTrsf));
231
232   TopTools_IndexedMapOfShape aFaces;
233   TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);
234
235   // display original shape in shaded display mode
236   Handle(AIS_Shape) aShapeIO = drawShape(aShape, Graphic3d_NOM_BRASS, Standard_False);
237   getAISContext()->SetDisplayMode(aShapeIO, AIS_Shaded, Standard_False);
238   DISP(aShapeIO);
239
240   Handle(AIS_TexturedShape) aTFace1 = Texturize(aFaces(16), "carrelage1.gif", 1, 1, 3, 2);
241   DISP(aTFace1);
242
243   Handle(AIS_TexturedShape) aTFace2 = Texturize(aFaces(21), "carrelage1.gif", 1, 1, 3, 2);
244   DISP(aTFace2);
245
246   getViewer()->Update();
247 }
248
249
250 //================================================================
251 // Function : TexturesExt_Presentation::sampleLand
252 // Purpose  : 
253 //================================================================
254 void TexturesExt_Presentation::sampleTerrain()
255 {
256   TopoDS_Shape aShape;
257   if (!loadShape(aShape, "terrain.brep"))
258     return;
259
260   // a part of the landscape is textured
261   TopTools_IndexedMapOfShape aFaces;
262   TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);
263
264 //  TopLoc_Location aLoc;
265 //  Handle(Geom_Surface) aSur = BRep_Tool::Surface(TopoDS::Face(aFaces(1)), aLoc);
266 //  Standard_Real u1,u2,v1,v2;
267 //  aSur->Bounds(u1,u2,v1,v2);
268 //  gp_Pnt aPnt = aSur->Value(u1, v1);
269   gp_Pnt aPnt(82100,80300,10940);// point at u1,v1
270   // resize and move the shape to the center of the viewer
271   
272   gp_Trsf aMoveTrsf;
273   gp_Ax3 New(gp_Pnt(-30,-30, 0),gp_Dir(0,0,1));
274   gp_Ax3 Current(aPnt,gp_Dir(0,0,1));
275   aMoveTrsf.SetDisplacement(Current, New);
276
277   gp_Trsf aScaleTrsf;
278   aScaleTrsf.SetScale(aPnt,0.0075);
279
280   BRepBuilderAPI_Transform aTransform(aMoveTrsf*aScaleTrsf);
281
282   aTransform.Perform(aFaces(1));
283   aShape = aTransform;
284
285   getAISContext()->Display(Texturize(aShape, "terrain.gif"));
286 }
287
288
289 //================================================================
290 // Function : moveScale
291 // Purpose  : move a shape a little left and scale it to 15%.
292 //================================================================
293 static void moveScale(TopoDS_Shape& aShape)
294 {
295   gp_Trsf aMoveTrsf;
296   gp_Ax3 New(gp_Pnt(-30,-30, -10),gp_Dir(0,0,1));
297   gp_Ax3 Current(gp_Pnt(0,0,0),gp_Dir(0,0,1));
298   aMoveTrsf.SetDisplacement(Current, New);
299
300   gp_Trsf aScaleTrsf;
301   aScaleTrsf.SetScale(gp_Pnt(0,0,0),0.15);
302
303   BRepBuilderAPI_Transform aTransform(aMoveTrsf*aScaleTrsf);
304
305   aTransform.Perform(aShape);
306   aShape = aTransform;
307 }
308
309 //================================================================
310 // Function : TexturesExt_Presentation::sampleKitchen
311 // Purpose  : kitchen with texturized items in it.
312 //================================================================
313 void TexturesExt_Presentation::sampleKitchen()
314 {
315   TopoDS_Shape aShape;
316
317   if (!loadShape(aShape, "Kitchen\\Room.brep"))
318     return;
319
320   gp_Trsf aTrsf;
321   gp_Ax3 NewCoordSystem (gp_Pnt(-1,-1, -1),gp_Dir(0,0,1));
322   gp_Ax3 CurrentCoordSystem(gp_Pnt(0,0,0),gp_Dir(0,0,1));
323   aTrsf.SetDisplacement(CurrentCoordSystem, NewCoordSystem);
324   aShape.Location(TopLoc_Location(aTrsf));
325
326   moveScale(aShape);
327
328   // draw kitchen room whithout one wall (to better see the insides)
329   TopTools_IndexedMapOfShape aFaces;
330   TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);  
331   Standard_Integer nbFaces = aFaces.Extent();
332
333   // create a wooden kitchen floor
334   // the floor's face will be textured with texture from chataignier.gif
335   DISP(Texturize(aFaces(5),"plancher.gif",1,1,2,1));
336
337   // texturize other faces of the room with texture from wallpaper.gif (walls)
338   DISP(Texturize(aFaces(1),"wallpaper.gif",1,1,8,6));
339   DISP(Texturize(aFaces(3),"wallpaper.gif",1,1,8,6));
340   DISP(Texturize(aFaces(4),"wallpaper.gif",1,1,8,6));
341
342 //  DISP(drawShape(aFaces(1), Quantity_NOC_LIGHTPINK, Standard_False));
343 //  DISP(drawShape(aFaces(3), Quantity_NOC_LIGHTPINK, Standard_False));
344 //  DISP(drawShape(aFaces(4), Quantity_NOC_LIGHTPINK, Standard_False));
345
346   // texturize furniture items with "wooden" texture
347   if (loadShape(aShape, "Kitchen\\MODERN_Table_1.brep"))
348   {
349     moveScale(aShape);
350     DISP(Texturize(aShape, "chataignier.gif"));
351   }
352   if (loadShape(aShape, "Kitchen\\MODERN_Chair_1.brep"))
353   {
354     moveScale(aShape);
355     DISP(Texturize(aShape, "chataignier.gif"));
356   }
357   if (loadShape(aShape, "Kitchen\\MODERN_Cooker_1.brep"))
358   {
359     moveScale(aShape);
360
361     aFaces.Clear();
362     TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);  
363     nbFaces = aFaces.Extent();
364
365     for (Standard_Integer i = 1; i <= nbFaces; i++)
366     {
367       if (i >= 59)
368         DISP(drawShape(aFaces(i), Graphic3d_NOM_STEEL, Standard_False));
369       else if (i >= 29)
370         DISP(drawShape(aFaces(i), Graphic3d_NOM_ALUMINIUM, Standard_False));
371       else if (i == 28)
372         DISP(Texturize(aFaces(i), "cookerplate.gif"));
373       else  
374         DISP(Texturize(aFaces(i), "chataignier.gif"));
375     }
376   }
377   if (loadShape(aShape, "Kitchen\\MODERN_Cooker_1_opened.brep"))
378   {
379     moveScale(aShape);
380     DISP(Texturize(aShape, "chataignier.gif"));
381   }
382   if (loadShape(aShape, "Kitchen\\MODERN_Exhaust_1.brep"))
383   {
384     moveScale(aShape);
385     DISP(drawShape(aShape, Graphic3d_NOM_STONE, Standard_False));
386   }
387   if (loadShape(aShape, "Kitchen\\MODERN_MVCooker_1.brep"))
388   {
389     moveScale(aShape);
390     DISP(drawShape(aShape, Graphic3d_NOM_SILVER, Standard_False));
391   }
392   if (loadShape(aShape, "Kitchen\\MODERN_MVCooker_1_opened.brep"))
393   {
394     moveScale(aShape);
395     DISP(drawShape(aShape, Graphic3d_NOM_SILVER, Standard_False));
396   }
397   if (loadShape(aShape, "Kitchen\\MODERN_Sink_1.brep"))
398   {
399     moveScale(aShape);
400
401     aFaces.Clear();
402     TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);  
403     nbFaces = aFaces.Extent();
404
405     for (Standard_Integer i = 1; i <= nbFaces; i++)
406     {
407       if (i < 145)
408         DISP(drawShape(aFaces(i), Graphic3d_NOM_ALUMINIUM, Standard_False));
409       else if (i == 145)
410         DISP(Texturize(aFaces(i), "cookerplate.gif"));
411       else  
412         DISP(Texturize(aFaces(i), "chataignier.gif"));
413     }
414   }
415   if (loadShape(aShape, "Kitchen\\MODERN_Sink_1_opened.brep"))
416   {
417     moveScale(aShape);
418     DISP(Texturize(aShape, "chataignier.gif"));
419   }
420   if (loadShape(aShape, "Kitchen\\MODERN_Refrigerator_1.brep"))
421   {
422     moveScale(aShape);
423     DISP(drawShape(aShape, Graphic3d_NOM_CHROME, Standard_False));
424   }
425   if (loadShape(aShape, "Kitchen\\MODERN_Refrigerator_1_opened.brep"))
426   {
427     moveScale(aShape);
428     DISP(drawShape(aShape, Graphic3d_NOM_CHROME, Standard_False));
429   }
430
431   getViewer()->Update();
432 }