1 // TexturesExt_Presentation.cpp: implementation of the TexturesExt_Presentation class.
2 // Creation of textural presentation of shape
3 //////////////////////////////////////////////////////////////////////
6 #include "TexturesExt_Presentation.h"
7 #include "Viewer3dApp.h"
10 #include <TopTools_IndexedMapOfShape.hxx>
11 #include <BRepBuilderAPI_MakeFace.hxx>
12 #include <AIS_TexturedShape.hxx>
13 #include <BRepTools.hxx>
14 #include <Graphic3d_Texture2D.hxx>
15 #include <BRep_Tool.hxx>
17 #include <BRepBuilderAPI_Transform.hxx>
18 #include <BRep_Builder.hxx>
19 #include <BRepTools.hxx>
20 #include <Geom_Surface.hxx>
21 #include <TopoDS_Face.hxx>
22 #include <V3d_DirectionalLight.hxx>
24 #define DISP(OBJ) getAISContext()->Display((OBJ), Standard_False)
26 // Initialization of global variable with an instance of this class
27 OCCDemo_Presentation* OCCDemo_Presentation::Current = new TexturesExt_Presentation;
29 // Initialization of array of samples
30 const TexturesExt_Presentation::PSampleFuncType TexturesExt_Presentation::SampleFuncs[] =
32 &TexturesExt_Presentation::sampleBottle,
33 &TexturesExt_Presentation::sampleTerrain,
34 &TexturesExt_Presentation::sampleKitchen
43 #define ZVIEW_SIZE 100
45 //////////////////////////////////////////////////////////////////////
46 // Construction/Destruction
47 //////////////////////////////////////////////////////////////////////
49 TexturesExt_Presentation::TexturesExt_Presentation()
51 myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
52 setName ("Textured Shapes");
55 //////////////////////////////////////////////////////////////////////
57 //////////////////////////////////////////////////////////////////////
59 void TexturesExt_Presentation::DoSample()
61 ((CViewer3dApp*) AfxGetApp())->SetSampleName("Viewer3d");
62 ((CViewer3dApp*) AfxGetApp())->SetSamplePath("..\\..\\04_Viewer3d");
63 getAISContext()->EraseAll();
64 if (myIndex >=0 && myIndex < myNbSamples)
66 // turn lights on for terrain sample
67 lightsOnOff(myIndex==1);
68 (this->*SampleFuncs[myIndex])();
72 void TexturesExt_Presentation::Init()
74 // initialize v3d_view so it displays TexturesExt well
75 getViewer()->InitActiveViews();
76 Handle_V3d_View aView = getViewer()->ActiveView();
77 aView->SetSurfaceDetail(V3d_TEX_ALL);
78 aView->SetSize(ZVIEW_SIZE);
80 // getDocument()->UpdateResultMessageDlg("Textured Shape",
81 TCollection_AsciiString Message ("Textured Shape",
82 " TopoDS_Shape aShape;" EOL
84 " // initialize aShape" EOL
85 " // aShape = ..." EOL
87 " // create a textured presentation object for aShape" EOL
88 " Handle_AIS_TexturedShape aTShape = new AIS_TexturedShape(aShape);" EOL
90 " TCollection_AsciiString aTFileName;" EOL
92 " // initialize aTFileName with an existing texture file name" EOL
93 " // (gif, bmp, xwd, rgb, and other formats are supported)" EOL
94 " // OR with an integer value string (max = Graphic3d_Texture2D::NumberOfTexturesExt())" EOL
95 " // which will indicate use of predefined texture of this number" EOL
96 " // aTFileName = ..." EOL
98 " aTShape->SetTextureFileName(aTFileName);" EOL
100 " // do other initialization of AIS_TexturedShape" EOL
101 " Standard_Real nRepeat;" EOL
102 " Standard_Boolean toRepeat;" EOL
103 " Standard_Boolean toScale;" EOL
104 " // initialize aRepeat, toRepeat, toScale ..." EOL
106 " aTShape->SetTextureMapOn();" EOL
107 " aTShape->SetTextureRepeat(toRepeat, nRepeat, nRepeat);" EOL
108 " aTShape->SetTexturesExtcale(toScale);" EOL
110 " // mode 3 is \"textured\" mode of AIS_TexturedShape, " EOL
111 " // other modes will display the \"normal\", non-textured shape," EOL
112 " // in wireframe(1) or shaded(2) modes correspondingly" EOL
113 " aTShape->SetDisplayMode(3); " EOL
115 " // V3d_TEX_ALL constant must be set as surface detail" EOL
116 " // for current view to see AIS_TexturedShape" EOL
117 " myCurrentView->SetSurfaceDetail(V3d_TEX_ALL);" EOL);
118 // CString text(Message.ToCString());
119 getDocument()->ClearDialog();
120 getDocument()->SetDialogTitle("Change face color");
121 getDocument()->AddTextInDialog(Message);
124 //////////////////////////////////////////////////////////////////////
126 //////////////////////////////////////////////////////////////////////
127 //================================================================
128 // Function : TexturesExt_Presentation::Texturize
129 // display an AIS_TexturedShape based on a given shape with texture with given filename
130 // filename can also be an integer value ("2", "5", etc.), in this case
131 // a predefined texture from Graphic3d_NameOfTexture2D with number = this value
133 //================================================================
134 Handle_AIS_TexturedShape TexturesExt_Presentation::Texturize(const TopoDS_Shape& aShape,
135 TCollection_AsciiString aTFileName,
136 Standard_Real toScaleU,
137 Standard_Real toScaleV,
138 Standard_Real toRepeatU,
139 Standard_Real toRepeatV,
140 Standard_Real originU,
141 Standard_Real originV)
143 // create a textured presentation object for aShape
144 Handle_AIS_TexturedShape aTShape = new AIS_TexturedShape(aShape);
145 TCollection_AsciiString TFileName;
146 // load texture from file if it is not an integer value
147 // integer value indicates a number of texture in predefined TexturesExt enumeration
148 CString initfile(((OCC_BaseApp*) AfxGetApp())->GetInitDataDir());
149 initfile += "\\Data\\";
150 if (!aTFileName.IsIntegerValue())
152 initfile += aTFileName.ToCString();
155 aTShape->SetTextureFileName((Standard_CString)(LPCTSTR)initfile);
157 // do other initialization of AIS_TexturedShape
158 aTShape->SetTextureMapOn();
159 aTShape->SetTextureScale(Standard_True, toScaleU, toScaleV);
160 aTShape->SetTextureRepeat(Standard_True, toRepeatU, toRepeatV);
161 aTShape->SetTextureOrigin(Standard_True, originU, originV);
163 aTShape->SetDisplayMode(3); // mode 3 is "textured" mode
169 //================================================================
170 // Function : TexturesExt_Presentation::loadShape
171 // loads a shape from a given brep file from data dir into a given TopoDS_Shape object
172 //================================================================
173 Standard_Boolean TexturesExt_Presentation::loadShape(TopoDS_Shape& aShape,
174 TCollection_AsciiString aFileName)
176 // create a TopoDS_Shape -> read from a brep file
177 CString initfile(((OCC_BaseApp*) AfxGetApp())->GetInitDataDir());
178 initfile += "\\Data\\";
179 initfile += aFileName.ToCString();
181 TCollection_AsciiString Path((Standard_CString)(LPCTSTR)initfile);
185 //Standard_Boolean isRead = BRepTools::Read (aShape, aPath.ToCString(), aBld);
187 // isRead = BRepTools::Read (aShape, bPath.ToCString(), aBld);
188 Standard_Boolean isRead = BRepTools::Read (aShape, Path.ToCString(), aBld);
191 Path += " was not found. The sample can not be shown.";
192 getDocument()->UpdateResultMessageDlg("Textured Shape", Path.ToCString());
193 return Standard_False;
196 return Standard_True;
199 //================================================================
200 // Function : lightsOnOff
201 // Purpose : 6 lights are used for a brighter demonstration of textured shapes
202 // call lightsOnOff(false) before showing normal shape
203 // call lightsOnOff(true) before showing textured shape
204 //================================================================
205 void TexturesExt_Presentation::lightsOnOff(Standard_Boolean isOn)
207 static Handle_V3d_Light aLight1 = new V3d_DirectionalLight(getViewer(), V3d_XnegYposZneg);
208 static Handle_V3d_Light aLight2 = new V3d_DirectionalLight(getViewer(), V3d_XnegYnegZpos);
209 static Handle_V3d_Light aLight3 = new V3d_DirectionalLight(getViewer(), V3d_XposYnegZpos);
210 static Handle_V3d_Light aLight4 = new V3d_DirectionalLight(getViewer(), V3d_XnegYnegZneg);
211 static Handle_V3d_Light aLight5 = new V3d_DirectionalLight(getViewer(), V3d_XnegYposZpos);
212 static Handle_V3d_Light aLight6 = new V3d_DirectionalLight(getViewer(), V3d_XposYposZpos);
216 getViewer()->SetLightOn(aLight1);
217 getViewer()->SetLightOn(aLight2);
218 getViewer()->SetLightOn(aLight3);
219 getViewer()->SetLightOn(aLight4);
220 getViewer()->SetLightOn(aLight5);
221 getViewer()->SetLightOn(aLight6);
225 getViewer()->SetLightOff(aLight1);
226 getViewer()->SetLightOff(aLight2);
227 getViewer()->SetLightOff(aLight3);
228 getViewer()->SetLightOff(aLight4);
229 getViewer()->SetLightOff(aLight5);
230 getViewer()->SetLightOff(aLight6);
234 //================================================================
235 // Function : TexturesExt_Presentation::sampleBottle
237 //================================================================
238 void TexturesExt_Presentation::sampleBottle()
241 if (!loadShape(aShape, "bottle.brep"))
244 // resize and move the shape to the center of the viewer
245 gp_Trsf aTrsf1, aTrsf2;
246 aTrsf1.SetScale(gp_Pnt(0,0,0), 0.8);
247 aTrsf2.SetTranslation(gp_Pnt(0,0,0),gp_Pnt(0,0,-20));
248 aTrsf1.Multiply(aTrsf2);
249 BRepBuilderAPI_Transform Transformer(aTrsf1);
250 Transformer.Perform(aShape);
251 aShape = Transformer.Shape();
253 TopTools_IndexedMapOfShape aFaces;
254 TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);
256 // display original shape in shaded display mode
257 Handle_AIS_Shape aShapeIO = drawShape(aShape, Graphic3d_NOM_BRASS, Standard_False);
258 getAISContext()->SetDisplayMode(aShapeIO, AIS_Shaded, Standard_False);
259 // Set increased polygon offset for the main shape to avoid depth collision with textured faces
260 aShapeIO->SetPolygonOffsets(Aspect_POM_Fill, 1.5, 0.5);
263 Handle_AIS_TexturedShape aTFace1 = Texturize(aFaces(16), "carrelage1.gif", 1, 1, 3, 2);
266 Handle_AIS_TexturedShape aTFace2 = Texturize(aFaces(21), "carrelage1.gif", 1, 1, 3, 2);
269 getViewer()->Update();
273 //================================================================
274 // Function : TexturesExt_Presentation::sampleLand
276 //================================================================
277 void TexturesExt_Presentation::sampleTerrain()
280 if (!loadShape(aShape, "terrain.brep"))
283 // a part of the landscape is textured
284 TopTools_IndexedMapOfShape aFaces;
285 TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);
287 // TopLoc_Location aLoc;
288 // Handle_Geom_Surface aSur = BRep_Tool::Surface(TopoDS::Face(aFaces(1)), aLoc);
289 // Standard_Real u1,u2,v1,v2;
290 // aSur->Bounds(u1,u2,v1,v2);
291 // gp_Pnt aPnt = aSur->Value(u1, v1);
292 gp_Pnt aPnt(82100,80300,10940);// point at u1,v1
293 // resize and move the shape to the center of the viewer
296 gp_Ax3 New(gp_Pnt(-30,-30, 0),gp_Dir(0,0,1));
297 gp_Ax3 Current(aPnt,gp_Dir(0,0,1));
298 aMoveTrsf.SetDisplacement(Current, New);
301 aScaleTrsf.SetScale(aPnt,0.0075);
303 BRepBuilderAPI_Transform aTransform(aMoveTrsf*aScaleTrsf);
305 aTransform.Perform(aFaces(1));
308 getAISContext()->Display(Texturize(aShape, "terrain.gif"));
312 //================================================================
313 // Function : moveScale
314 // Purpose : move a shape a little left and scale it to 15%.
315 //================================================================
316 static void moveScale(TopoDS_Shape& aShape)
319 gp_Ax3 New(gp_Pnt(-30,-30, -10),gp_Dir(0,0,1));
320 gp_Ax3 Current(gp_Pnt(0,0,0),gp_Dir(0,0,1));
321 aMoveTrsf.SetDisplacement(Current, New);
324 aScaleTrsf.SetScale(gp_Pnt(0,0,0),0.15);
326 BRepBuilderAPI_Transform aTransform(aMoveTrsf*aScaleTrsf);
328 aTransform.Perform(aShape);
332 //================================================================
333 // Function : TexturesExt_Presentation::sampleKitchen
334 // Purpose : kitchen with texturized items in it.
335 //================================================================
336 void TexturesExt_Presentation::sampleKitchen()
340 if (!loadShape(aShape, "Kitchen\\Room.brep"))
344 gp_Ax3 NewCoordSystem (gp_Pnt(-1,-1, -1),gp_Dir(0,0,1));
345 gp_Ax3 CurrentCoordSystem(gp_Pnt(0,0,0),gp_Dir(0,0,1));
346 aTrsf.SetDisplacement(CurrentCoordSystem, NewCoordSystem);
347 aShape.Location(TopLoc_Location(aTrsf));
351 // draw kitchen room whithout one wall (to better see the insides)
352 TopTools_IndexedMapOfShape aFaces;
353 TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);
354 Standard_Integer nbFaces = aFaces.Extent();
356 // create a wooden kitchen floor
357 // the floor's face will be textured with texture from chataignier.gif
358 DISP(Texturize(aFaces(5),"plancher.gif",1,1,2,1));
360 // texturize other faces of the room with texture from wallpaper.gif (walls)
361 DISP(Texturize(aFaces(1),"wallpaper.gif",1,1,8,6));
362 DISP(Texturize(aFaces(3),"wallpaper.gif",1,1,8,6));
363 DISP(Texturize(aFaces(4),"wallpaper.gif",1,1,8,6));
365 // DISP(drawShape(aFaces(1), Quantity_NOC_LIGHTPINK, Standard_False));
366 // DISP(drawShape(aFaces(3), Quantity_NOC_LIGHTPINK, Standard_False));
367 // DISP(drawShape(aFaces(4), Quantity_NOC_LIGHTPINK, Standard_False));
369 // texturize furniture items with "wooden" texture
370 if (loadShape(aShape, "Kitchen\\MODERN_Table_1.brep"))
373 DISP(Texturize(aShape, "chataignier.gif"));
375 if (loadShape(aShape, "Kitchen\\MODERN_Chair_1.brep"))
378 DISP(Texturize(aShape, "chataignier.gif"));
380 if (loadShape(aShape, "Kitchen\\MODERN_Cooker_1.brep"))
385 TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);
386 nbFaces = aFaces.Extent();
388 for (Standard_Integer i = 1; i <= nbFaces; i++)
391 DISP(drawShape(aFaces(i), Graphic3d_NOM_STEEL, Standard_False));
393 DISP(drawShape(aFaces(i), Graphic3d_NOM_ALUMINIUM, Standard_False));
395 DISP(Texturize(aFaces(i), "cookerplate.gif"));
397 DISP(Texturize(aFaces(i), "chataignier.gif"));
400 if (loadShape(aShape, "Kitchen\\MODERN_Cooker_1_opened.brep"))
403 DISP(Texturize(aShape, "chataignier.gif"));
405 if (loadShape(aShape, "Kitchen\\MODERN_Exhaust_1.brep"))
408 DISP(drawShape(aShape, Graphic3d_NOM_STONE, Standard_False));
410 if (loadShape(aShape, "Kitchen\\MODERN_MVCooker_1.brep"))
413 DISP(drawShape(aShape, Graphic3d_NOM_SILVER, Standard_False));
415 if (loadShape(aShape, "Kitchen\\MODERN_MVCooker_1_opened.brep"))
418 DISP(drawShape(aShape, Graphic3d_NOM_SILVER, Standard_False));
420 if (loadShape(aShape, "Kitchen\\MODERN_Sink_1.brep"))
425 TopExp::MapShapes(aShape, TopAbs_FACE, aFaces);
426 nbFaces = aFaces.Extent();
428 for (Standard_Integer i = 1; i <= nbFaces; i++)
431 DISP(drawShape(aFaces(i), Graphic3d_NOM_ALUMINIUM, Standard_False));
433 DISP(Texturize(aFaces(i), "cookerplate.gif"));
435 DISP(Texturize(aFaces(i), "chataignier.gif"));
438 if (loadShape(aShape, "Kitchen\\MODERN_Sink_1_opened.brep"))
441 DISP(Texturize(aShape, "chataignier.gif"));
443 if (loadShape(aShape, "Kitchen\\MODERN_Refrigerator_1.brep"))
446 DISP(drawShape(aShape, Graphic3d_NOM_CHROME, Standard_False));
448 if (loadShape(aShape, "Kitchen\\MODERN_Refrigerator_1_opened.brep"))
451 DISP(drawShape(aShape, Graphic3d_NOM_CHROME, Standard_False));
454 getViewer()->Update();