1 // TexturesExt_Presentation.cpp: implementation of the TexturesExt_Presentation class.
2 // Creation of textural presentation of shape
3 //////////////////////////////////////////////////////////////////////
6 #include "TexturesExt_Presentation.h"
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>
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>
23 #define DISP(OBJ) getAISContext()->Display((OBJ), Standard_False)
25 // Initialization of global variable with an instance of this class
26 OCCDemo_Presentation* OCCDemo_Presentation::Current = new TexturesExt_Presentation;
28 // Initialization of array of samples
29 const TexturesExt_Presentation::PSampleFuncType TexturesExt_Presentation::SampleFuncs[] =
31 &TexturesExt_Presentation::sampleBottle,
32 &TexturesExt_Presentation::sampleTerrain,
33 &TexturesExt_Presentation::sampleKitchen
42 #define ZVIEW_SIZE 100
44 //////////////////////////////////////////////////////////////////////
45 // Construction/Destruction
46 //////////////////////////////////////////////////////////////////////
48 TexturesExt_Presentation::TexturesExt_Presentation()
50 myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
51 setName ("Textured Shapes");
54 //////////////////////////////////////////////////////////////////////
56 //////////////////////////////////////////////////////////////////////
58 void TexturesExt_Presentation::DoSample()
60 getAISContext()->EraseAll();
61 if (myIndex >=0 && myIndex < myNbSamples)
63 // turn lights on for terrain sample
64 lightsOnOff(myIndex==1);
65 (this->*SampleFuncs[myIndex])();
69 void TexturesExt_Presentation::Init()
71 // initialize v3d_view so it displays TexturesExt well
72 getViewer()->InitActiveViews();
73 Handle(V3d_View) aView = getViewer()->ActiveView();
74 aView->SetSize(ZVIEW_SIZE);
76 setResultTitle("Textured Shape");
78 " TopoDS_Shape aShape;" EOL
80 " // initialize aShape" EOL
81 " // aShape = ..." EOL
83 " // create a textured presentation object for aShape" EOL
84 " Handle(AIS_TexturedShape) aTShape = new AIS_TexturedShape(aShape);" EOL
86 " TCollection_AsciiString aTFileName;" 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
94 " aTShape->SetTextureFileName(aTFileName);" 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
102 " aTShape->SetTextureMapOn();" EOL
103 " aTShape->SetTextureRepeat(toRepeat, nRepeat, nRepeat);" EOL
104 " aTShape->SetTexturesExtcale(toScale);" 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);
112 //////////////////////////////////////////////////////////////////////
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
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)
131 // create a textured presentation object for aShape
132 Handle(AIS_TexturedShape) aTShape = new AIS_TexturedShape(aShape);
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())
138 TCollection_AsciiString aTmp(aTFileName);
139 aTFileName = GetDataDir();
140 aTFileName = aTFileName + "\\" + aTmp;
143 aTShape->SetTextureFileName(aTFileName);
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);
151 aTShape->SetDisplayMode(3); // mode 3 is "textured" mode
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)
164 // create a TopoDS_Shape -> read from a brep file
165 TCollection_AsciiString aPath(GetDataDir());
166 aPath = aPath + "\\" + aFileName;
169 Standard_Boolean isRead = BRepTools::Read (aShape, aPath.ToCString(), aBld);
172 aPath += " was not found. The sample can not be shown.";
173 setResultText(aPath.ToCString());
174 return Standard_False;
177 return Standard_True;
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)
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);
197 getViewer()->SetLightOn(aLight1);
198 getViewer()->SetLightOn(aLight2);
199 getViewer()->SetLightOn(aLight3);
200 getViewer()->SetLightOn(aLight4);
201 getViewer()->SetLightOn(aLight5);
202 getViewer()->SetLightOn(aLight6);
206 getViewer()->SetLightOff(aLight1);
207 getViewer()->SetLightOff(aLight2);
208 getViewer()->SetLightOff(aLight3);
209 getViewer()->SetLightOff(aLight4);
210 getViewer()->SetLightOff(aLight5);
211 getViewer()->SetLightOff(aLight6);
215 //================================================================
216 // Function : TexturesExt_Presentation::sampleBottle
218 //================================================================
219 void TexturesExt_Presentation::sampleBottle()
222 if (!loadShape(aShape, "bottle.brep"))
225 // resize and move the shape to the center of the viewer
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));
232 TopTools_IndexedMapOfShape aFaces;
233 TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);
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);
240 Handle(AIS_TexturedShape) aTFace1 = Texturize(aFaces(16), "carrelage1.gif", 1, 1, 3, 2);
243 Handle(AIS_TexturedShape) aTFace2 = Texturize(aFaces(21), "carrelage1.gif", 1, 1, 3, 2);
246 getViewer()->Update();
250 //================================================================
251 // Function : TexturesExt_Presentation::sampleLand
253 //================================================================
254 void TexturesExt_Presentation::sampleTerrain()
257 if (!loadShape(aShape, "terrain.brep"))
260 // a part of the landscape is textured
261 TopTools_IndexedMapOfShape aFaces;
262 TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);
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
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);
278 aScaleTrsf.SetScale(aPnt,0.0075);
280 BRepBuilderAPI_Transform aTransform(aMoveTrsf*aScaleTrsf);
282 aTransform.Perform(aFaces(1));
285 getAISContext()->Display(Texturize(aShape, "terrain.gif"));
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)
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);
301 aScaleTrsf.SetScale(gp_Pnt(0,0,0),0.15);
303 BRepBuilderAPI_Transform aTransform(aMoveTrsf*aScaleTrsf);
305 aTransform.Perform(aShape);
309 //================================================================
310 // Function : TexturesExt_Presentation::sampleKitchen
311 // Purpose : kitchen with texturized items in it.
312 //================================================================
313 void TexturesExt_Presentation::sampleKitchen()
317 if (!loadShape(aShape, "Kitchen\\Room.brep"))
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));
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();
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));
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));
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));
346 // texturize furniture items with "wooden" texture
347 if (loadShape(aShape, "Kitchen\\MODERN_Table_1.brep"))
350 DISP(Texturize(aShape, "chataignier.gif"));
352 if (loadShape(aShape, "Kitchen\\MODERN_Chair_1.brep"))
355 DISP(Texturize(aShape, "chataignier.gif"));
357 if (loadShape(aShape, "Kitchen\\MODERN_Cooker_1.brep"))
362 TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);
363 nbFaces = aFaces.Extent();
365 for (Standard_Integer i = 1; i <= nbFaces; i++)
368 DISP(drawShape(aFaces(i), Graphic3d_NOM_STEEL, Standard_False));
370 DISP(drawShape(aFaces(i), Graphic3d_NOM_ALUMINIUM, Standard_False));
372 DISP(Texturize(aFaces(i), "cookerplate.gif"));
374 DISP(Texturize(aFaces(i), "chataignier.gif"));
377 if (loadShape(aShape, "Kitchen\\MODERN_Cooker_1_opened.brep"))
380 DISP(Texturize(aShape, "chataignier.gif"));
382 if (loadShape(aShape, "Kitchen\\MODERN_Exhaust_1.brep"))
385 DISP(drawShape(aShape, Graphic3d_NOM_STONE, Standard_False));
387 if (loadShape(aShape, "Kitchen\\MODERN_MVCooker_1.brep"))
390 DISP(drawShape(aShape, Graphic3d_NOM_SILVER, Standard_False));
392 if (loadShape(aShape, "Kitchen\\MODERN_MVCooker_1_opened.brep"))
395 DISP(drawShape(aShape, Graphic3d_NOM_SILVER, Standard_False));
397 if (loadShape(aShape, "Kitchen\\MODERN_Sink_1.brep"))
402 TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);
403 nbFaces = aFaces.Extent();
405 for (Standard_Integer i = 1; i <= nbFaces; i++)
408 DISP(drawShape(aFaces(i), Graphic3d_NOM_ALUMINIUM, Standard_False));
410 DISP(Texturize(aFaces(i), "cookerplate.gif"));
412 DISP(Texturize(aFaces(i), "chataignier.gif"));
415 if (loadShape(aShape, "Kitchen\\MODERN_Sink_1_opened.brep"))
418 DISP(Texturize(aShape, "chataignier.gif"));
420 if (loadShape(aShape, "Kitchen\\MODERN_Refrigerator_1.brep"))
423 DISP(drawShape(aShape, Graphic3d_NOM_CHROME, Standard_False));
425 if (loadShape(aShape, "Kitchen\\MODERN_Refrigerator_1_opened.brep"))
428 DISP(drawShape(aShape, Graphic3d_NOM_CHROME, Standard_False));
431 getViewer()->Update();