7fd59977 |
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 | #include "Viewer3dApp.h" |
8 | |
9 | #include <TopExp.hxx> |
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> |
16 | #include <TopoDS.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> |
23 | |
24 | #define DISP(OBJ) getAISContext()->Display((OBJ), Standard_False) |
25 | |
26 | // Initialization of global variable with an instance of this class |
27 | OCCDemo_Presentation* OCCDemo_Presentation::Current = new TexturesExt_Presentation; |
28 | |
29 | // Initialization of array of samples |
30 | const TexturesExt_Presentation::PSampleFuncType TexturesExt_Presentation::SampleFuncs[] = |
31 | { |
32 | &TexturesExt_Presentation::sampleBottle, |
33 | &TexturesExt_Presentation::sampleTerrain, |
34 | &TexturesExt_Presentation::sampleKitchen |
35 | }; |
36 | |
50fe2dab |
37 | #define EOL "\r\n" |
7fd59977 |
38 | |
39 | #define ZVIEW_SIZE 100 |
40 | |
41 | ////////////////////////////////////////////////////////////////////// |
42 | // Construction/Destruction |
43 | ////////////////////////////////////////////////////////////////////// |
44 | |
45 | TexturesExt_Presentation::TexturesExt_Presentation() |
46 | { |
47 | myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType); |
48 | setName ("Textured Shapes"); |
49 | } |
50 | |
51 | ////////////////////////////////////////////////////////////////////// |
52 | // Sample execution |
53 | ////////////////////////////////////////////////////////////////////// |
54 | |
55 | void TexturesExt_Presentation::DoSample() |
56 | { |
576f8b11 |
57 | ((CViewer3dApp*) AfxGetApp())->SetSampleName (L"Viewer3d"); |
58 | ((CViewer3dApp*) AfxGetApp())->SetSamplePath (L"..\\..\\04_Viewer3d"); |
7fd59977 |
59 | getAISContext()->EraseAll(); |
60 | if (myIndex >=0 && myIndex < myNbSamples) |
61 | { |
62 | // turn lights on for terrain sample |
63 | lightsOnOff(myIndex==1); |
64 | (this->*SampleFuncs[myIndex])(); |
65 | } |
66 | } |
67 | |
68 | void TexturesExt_Presentation::Init() |
69 | { |
70 | // initialize v3d_view so it displays TexturesExt well |
71 | getViewer()->InitActiveViews(); |
92efcf78 |
72 | Handle(V3d_View) aView = getViewer()->ActiveView(); |
7fd59977 |
73 | aView->SetSurfaceDetail(V3d_TEX_ALL); |
74 | aView->SetSize(ZVIEW_SIZE); |
75 | |
76 | // getDocument()->UpdateResultMessageDlg("Textured Shape", |
77 | TCollection_AsciiString Message ("Textured Shape", |
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 |
92efcf78 |
84 | " Handle(AIS_TexturedShape) aTShape = new AIS_TexturedShape(aShape);" EOL |
7fd59977 |
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 | "" EOL |
111 | " // V3d_TEX_ALL constant must be set as surface detail" EOL |
112 | " // for current view to see AIS_TexturedShape" EOL |
113 | " myCurrentView->SetSurfaceDetail(V3d_TEX_ALL);" EOL); |
114 | // CString text(Message.ToCString()); |
115 | getDocument()->ClearDialog(); |
116 | getDocument()->SetDialogTitle("Change face color"); |
117 | getDocument()->AddTextInDialog(Message); |
118 | } |
119 | |
120 | ////////////////////////////////////////////////////////////////////// |
121 | // Sample functions |
122 | ////////////////////////////////////////////////////////////////////// |
123 | //================================================================ |
124 | // Function : TexturesExt_Presentation::Texturize |
125 | // display an AIS_TexturedShape based on a given shape with texture with given filename |
126 | // filename can also be an integer value ("2", "5", etc.), in this case |
127 | // a predefined texture from Graphic3d_NameOfTexture2D with number = this value |
128 | // is loaded. |
129 | //================================================================ |
92efcf78 |
130 | Handle(AIS_TexturedShape) TexturesExt_Presentation::Texturize(const TopoDS_Shape& aShape, |
7fd59977 |
131 | TCollection_AsciiString aTFileName, |
132 | Standard_Real toScaleU, |
133 | Standard_Real toScaleV, |
134 | Standard_Real toRepeatU, |
135 | Standard_Real toRepeatV, |
136 | Standard_Real originU, |
137 | Standard_Real originV) |
138 | { |
139 | // create a textured presentation object for aShape |
92efcf78 |
140 | Handle(AIS_TexturedShape) aTShape = new AIS_TexturedShape(aShape); |
7fd59977 |
141 | TCollection_AsciiString TFileName; |
142 | // load texture from file if it is not an integer value |
143 | // integer value indicates a number of texture in predefined TexturesExt enumeration |
5c1f974e |
144 | CString initfile(((OCC_App*) AfxGetApp())->GetInitDataDir()); |
3554ea68 |
145 | initfile += "..\\..\\..\\04_Viewer3d\\Data\\"; |
7fd59977 |
146 | if (!aTFileName.IsIntegerValue()) |
147 | { |
148 | initfile += aTFileName.ToCString(); |
149 | } |
150 | |
576f8b11 |
151 | TCollection_ExtendedString aFileName ((Standard_ExtString )(const wchar_t* )initfile); |
152 | aTShape->SetTextureFileName (TCollection_AsciiString (aFileName, '?')); |
7fd59977 |
153 | |
154 | // do other initialization of AIS_TexturedShape |
155 | aTShape->SetTextureMapOn(); |
156 | aTShape->SetTextureScale(Standard_True, toScaleU, toScaleV); |
157 | aTShape->SetTextureRepeat(Standard_True, toRepeatU, toRepeatV); |
158 | aTShape->SetTextureOrigin(Standard_True, originU, originV); |
159 | |
160 | aTShape->SetDisplayMode(3); // mode 3 is "textured" mode |
161 | |
162 | return aTShape; |
163 | } |
164 | |
165 | |
166 | //================================================================ |
167 | // Function : TexturesExt_Presentation::loadShape |
168 | // loads a shape from a given brep file from data dir into a given TopoDS_Shape object |
169 | //================================================================ |
170 | Standard_Boolean TexturesExt_Presentation::loadShape(TopoDS_Shape& aShape, |
171 | TCollection_AsciiString aFileName) |
172 | { |
173 | // create a TopoDS_Shape -> read from a brep file |
5c1f974e |
174 | CString initfile(((OCC_App*) AfxGetApp())->GetInitDataDir()); |
3554ea68 |
175 | initfile += "..\\..\\..\\04_Viewer3d\\Data\\"; |
7fd59977 |
176 | initfile += aFileName.ToCString(); |
177 | |
576f8b11 |
178 | std::filebuf aFileBuf; |
179 | std::istream aStream (&aFileBuf); |
180 | if (!aFileBuf.open (initfile, ios::in)) |
181 | { |
182 | initfile += L" was not found. The sample can not be shown."; |
183 | getDocument()->UpdateResultMessageDlg ("Textured Shape", initfile); |
184 | return Standard_False; |
185 | } |
7fd59977 |
186 | |
187 | BRep_Builder aBld; |
576f8b11 |
188 | BRepTools::Read (aShape, aStream, aBld); |
189 | if (aShape.IsNull()) |
7fd59977 |
190 | { |
576f8b11 |
191 | initfile += " is invalid. The sample can not be shown."; |
192 | getDocument()->UpdateResultMessageDlg ("Textured Shape", initfile); |
7fd59977 |
193 | return Standard_False; |
194 | } |
195 | |
196 | return Standard_True; |
197 | } |
198 | |
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) |
206 | { |
92efcf78 |
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); |
7fd59977 |
213 | |
214 | if (isOn) |
215 | { |
216 | getViewer()->SetLightOn(aLight1); |
217 | getViewer()->SetLightOn(aLight2); |
218 | getViewer()->SetLightOn(aLight3); |
219 | getViewer()->SetLightOn(aLight4); |
220 | getViewer()->SetLightOn(aLight5); |
221 | getViewer()->SetLightOn(aLight6); |
222 | } |
223 | else |
224 | { |
225 | getViewer()->SetLightOff(aLight1); |
226 | getViewer()->SetLightOff(aLight2); |
227 | getViewer()->SetLightOff(aLight3); |
228 | getViewer()->SetLightOff(aLight4); |
229 | getViewer()->SetLightOff(aLight5); |
230 | getViewer()->SetLightOff(aLight6); |
231 | } |
232 | } |
233 | |
234 | //================================================================ |
235 | // Function : TexturesExt_Presentation::sampleBottle |
236 | // Purpose : |
237 | //================================================================ |
238 | void TexturesExt_Presentation::sampleBottle() |
239 | { |
240 | TopoDS_Shape aShape; |
241 | if (!loadShape(aShape, "bottle.brep")) |
242 | return; |
243 | |
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(); |
252 | |
253 | TopTools_IndexedMapOfShape aFaces; |
254 | TopExp::MapShapes(aShape, TopAbs_FACE, aFaces); |
255 | |
256 | // display original shape in shaded display mode |
92efcf78 |
257 | Handle(AIS_Shape) aShapeIO = drawShape(aShape, Graphic3d_NOM_BRASS, Standard_False); |
7fd59977 |
258 | getAISContext()->SetDisplayMode(aShapeIO, AIS_Shaded, Standard_False); |
cc5ac00d |
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); |
7fd59977 |
261 | DISP(aShapeIO); |
262 | |
92efcf78 |
263 | Handle(AIS_TexturedShape) aTFace1 = Texturize(aFaces(16), "carrelage1.gif", 1, 1, 3, 2); |
7fd59977 |
264 | DISP(aTFace1); |
265 | |
92efcf78 |
266 | Handle(AIS_TexturedShape) aTFace2 = Texturize(aFaces(21), "carrelage1.gif", 1, 1, 3, 2); |
7fd59977 |
267 | DISP(aTFace2); |
268 | |
269 | getViewer()->Update(); |
270 | } |
271 | |
272 | |
273 | //================================================================ |
274 | // Function : TexturesExt_Presentation::sampleLand |
275 | // Purpose : |
276 | //================================================================ |
277 | void TexturesExt_Presentation::sampleTerrain() |
278 | { |
279 | TopoDS_Shape aShape; |
280 | if (!loadShape(aShape, "terrain.brep")) |
281 | return; |
282 | |
283 | // a part of the landscape is textured |
284 | TopTools_IndexedMapOfShape aFaces; |
285 | TopExp::MapShapes(aShape, TopAbs_FACE, aFaces); |
286 | |
287 | // TopLoc_Location aLoc; |
92efcf78 |
288 | // Handle(Geom_Surface) aSur = BRep_Tool::Surface(TopoDS::Face(aFaces(1)), aLoc); |
7fd59977 |
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 |
294 | |
295 | gp_Trsf aMoveTrsf; |
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); |
299 | |
300 | gp_Trsf aScaleTrsf; |
301 | aScaleTrsf.SetScale(aPnt,0.0075); |
302 | |
303 | BRepBuilderAPI_Transform aTransform(aMoveTrsf*aScaleTrsf); |
304 | |
305 | aTransform.Perform(aFaces(1)); |
306 | aShape = aTransform; |
307 | |
308 | getAISContext()->Display(Texturize(aShape, "terrain.gif")); |
309 | } |
310 | |
311 | |
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) |
317 | { |
318 | gp_Trsf aMoveTrsf; |
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); |
322 | |
323 | gp_Trsf aScaleTrsf; |
324 | aScaleTrsf.SetScale(gp_Pnt(0,0,0),0.15); |
325 | |
326 | BRepBuilderAPI_Transform aTransform(aMoveTrsf*aScaleTrsf); |
327 | |
328 | aTransform.Perform(aShape); |
329 | aShape = aTransform; |
330 | } |
331 | |
332 | //================================================================ |
333 | // Function : TexturesExt_Presentation::sampleKitchen |
334 | // Purpose : kitchen with texturized items in it. |
335 | //================================================================ |
336 | void TexturesExt_Presentation::sampleKitchen() |
337 | { |
338 | TopoDS_Shape aShape; |
339 | |
340 | if (!loadShape(aShape, "Kitchen\\Room.brep")) |
341 | return; |
342 | |
343 | gp_Trsf aTrsf; |
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)); |
348 | |
349 | moveScale(aShape); |
350 | |
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(); |
355 | |
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)); |
359 | |
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)); |
364 | |
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)); |
368 | |
369 | // texturize furniture items with "wooden" texture |
370 | if (loadShape(aShape, "Kitchen\\MODERN_Table_1.brep")) |
371 | { |
372 | moveScale(aShape); |
373 | DISP(Texturize(aShape, "chataignier.gif")); |
374 | } |
375 | if (loadShape(aShape, "Kitchen\\MODERN_Chair_1.brep")) |
376 | { |
377 | moveScale(aShape); |
378 | DISP(Texturize(aShape, "chataignier.gif")); |
379 | } |
380 | if (loadShape(aShape, "Kitchen\\MODERN_Cooker_1.brep")) |
381 | { |
382 | moveScale(aShape); |
383 | |
384 | aFaces.Clear(); |
385 | TopExp::MapShapes(aShape, TopAbs_FACE, aFaces); |
386 | nbFaces = aFaces.Extent(); |
387 | |
388 | for (Standard_Integer i = 1; i <= nbFaces; i++) |
389 | { |
390 | if (i >= 59) |
391 | DISP(drawShape(aFaces(i), Graphic3d_NOM_STEEL, Standard_False)); |
392 | else if (i >= 29) |
393 | DISP(drawShape(aFaces(i), Graphic3d_NOM_ALUMINIUM, Standard_False)); |
394 | else if (i == 28) |
395 | DISP(Texturize(aFaces(i), "cookerplate.gif")); |
396 | else |
397 | DISP(Texturize(aFaces(i), "chataignier.gif")); |
398 | } |
399 | } |
400 | if (loadShape(aShape, "Kitchen\\MODERN_Cooker_1_opened.brep")) |
401 | { |
402 | moveScale(aShape); |
403 | DISP(Texturize(aShape, "chataignier.gif")); |
404 | } |
405 | if (loadShape(aShape, "Kitchen\\MODERN_Exhaust_1.brep")) |
406 | { |
407 | moveScale(aShape); |
408 | DISP(drawShape(aShape, Graphic3d_NOM_STONE, Standard_False)); |
409 | } |
410 | if (loadShape(aShape, "Kitchen\\MODERN_MVCooker_1.brep")) |
411 | { |
412 | moveScale(aShape); |
413 | DISP(drawShape(aShape, Graphic3d_NOM_SILVER, Standard_False)); |
414 | } |
415 | if (loadShape(aShape, "Kitchen\\MODERN_MVCooker_1_opened.brep")) |
416 | { |
417 | moveScale(aShape); |
418 | DISP(drawShape(aShape, Graphic3d_NOM_SILVER, Standard_False)); |
419 | } |
420 | if (loadShape(aShape, "Kitchen\\MODERN_Sink_1.brep")) |
421 | { |
422 | moveScale(aShape); |
423 | |
424 | aFaces.Clear(); |
425 | TopExp::MapShapes(aShape, TopAbs_FACE, aFaces); |
426 | nbFaces = aFaces.Extent(); |
427 | |
428 | for (Standard_Integer i = 1; i <= nbFaces; i++) |
429 | { |
430 | if (i < 145) |
431 | DISP(drawShape(aFaces(i), Graphic3d_NOM_ALUMINIUM, Standard_False)); |
432 | else if (i == 145) |
433 | DISP(Texturize(aFaces(i), "cookerplate.gif")); |
434 | else |
435 | DISP(Texturize(aFaces(i), "chataignier.gif")); |
436 | } |
437 | } |
438 | if (loadShape(aShape, "Kitchen\\MODERN_Sink_1_opened.brep")) |
439 | { |
440 | moveScale(aShape); |
441 | DISP(Texturize(aShape, "chataignier.gif")); |
442 | } |
443 | if (loadShape(aShape, "Kitchen\\MODERN_Refrigerator_1.brep")) |
444 | { |
445 | moveScale(aShape); |
446 | DISP(drawShape(aShape, Graphic3d_NOM_CHROME, Standard_False)); |
447 | } |
448 | if (loadShape(aShape, "Kitchen\\MODERN_Refrigerator_1_opened.brep")) |
449 | { |
450 | moveScale(aShape); |
451 | DISP(drawShape(aShape, Graphic3d_NOM_CHROME, Standard_False)); |
452 | } |
453 | |
454 | getViewer()->Update(); |
455 | } |