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 | |
37 | #ifdef WNT |
38 | #define EOL "\r\n" |
39 | #else |
40 | #define EOL "\n" |
41 | #endif |
42 | |
43 | #define ZVIEW_SIZE 100 |
44 | |
45 | ////////////////////////////////////////////////////////////////////// |
46 | // Construction/Destruction |
47 | ////////////////////////////////////////////////////////////////////// |
48 | |
49 | TexturesExt_Presentation::TexturesExt_Presentation() |
50 | { |
51 | myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType); |
52 | setName ("Textured Shapes"); |
53 | } |
54 | |
55 | ////////////////////////////////////////////////////////////////////// |
56 | // Sample execution |
57 | ////////////////////////////////////////////////////////////////////// |
58 | |
59 | void TexturesExt_Presentation::DoSample() |
60 | { |
576f8b11 |
61 | ((CViewer3dApp*) AfxGetApp())->SetSampleName (L"Viewer3d"); |
62 | ((CViewer3dApp*) AfxGetApp())->SetSamplePath (L"..\\..\\04_Viewer3d"); |
7fd59977 |
63 | getAISContext()->EraseAll(); |
64 | if (myIndex >=0 && myIndex < myNbSamples) |
65 | { |
66 | // turn lights on for terrain sample |
67 | lightsOnOff(myIndex==1); |
68 | (this->*SampleFuncs[myIndex])(); |
69 | } |
70 | } |
71 | |
72 | void TexturesExt_Presentation::Init() |
73 | { |
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); |
79 | |
80 | // getDocument()->UpdateResultMessageDlg("Textured Shape", |
81 | TCollection_AsciiString Message ("Textured Shape", |
82 | " TopoDS_Shape aShape;" EOL |
83 | "" EOL |
84 | " // initialize aShape" EOL |
85 | " // aShape = ..." EOL |
86 | "" EOL |
87 | " // create a textured presentation object for aShape" EOL |
88 | " Handle_AIS_TexturedShape aTShape = new AIS_TexturedShape(aShape);" EOL |
89 | "" EOL |
90 | " TCollection_AsciiString aTFileName;" EOL |
91 | "" 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 |
97 | "" EOL |
98 | " aTShape->SetTextureFileName(aTFileName);" EOL |
99 | "" 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 |
105 | "" EOL |
106 | " aTShape->SetTextureMapOn();" EOL |
107 | " aTShape->SetTextureRepeat(toRepeat, nRepeat, nRepeat);" EOL |
108 | " aTShape->SetTexturesExtcale(toScale);" EOL |
109 | " " 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 |
114 | "" 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); |
122 | } |
123 | |
124 | ////////////////////////////////////////////////////////////////////// |
125 | // Sample functions |
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 |
132 | // is loaded. |
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) |
142 | { |
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 |
5c1f974e |
148 | CString initfile(((OCC_App*) AfxGetApp())->GetInitDataDir()); |
7fd59977 |
149 | initfile += "\\Data\\"; |
150 | if (!aTFileName.IsIntegerValue()) |
151 | { |
152 | initfile += aTFileName.ToCString(); |
153 | } |
154 | |
576f8b11 |
155 | TCollection_ExtendedString aFileName ((Standard_ExtString )(const wchar_t* )initfile); |
156 | aTShape->SetTextureFileName (TCollection_AsciiString (aFileName, '?')); |
7fd59977 |
157 | |
158 | // do other initialization of AIS_TexturedShape |
159 | aTShape->SetTextureMapOn(); |
160 | aTShape->SetTextureScale(Standard_True, toScaleU, toScaleV); |
161 | aTShape->SetTextureRepeat(Standard_True, toRepeatU, toRepeatV); |
162 | aTShape->SetTextureOrigin(Standard_True, originU, originV); |
163 | |
164 | aTShape->SetDisplayMode(3); // mode 3 is "textured" mode |
165 | |
166 | return aTShape; |
167 | } |
168 | |
169 | |
170 | //================================================================ |
171 | // Function : TexturesExt_Presentation::loadShape |
172 | // loads a shape from a given brep file from data dir into a given TopoDS_Shape object |
173 | //================================================================ |
174 | Standard_Boolean TexturesExt_Presentation::loadShape(TopoDS_Shape& aShape, |
175 | TCollection_AsciiString aFileName) |
176 | { |
177 | // create a TopoDS_Shape -> read from a brep file |
5c1f974e |
178 | CString initfile(((OCC_App*) AfxGetApp())->GetInitDataDir()); |
7fd59977 |
179 | initfile += "\\Data\\"; |
180 | initfile += aFileName.ToCString(); |
181 | |
576f8b11 |
182 | std::filebuf aFileBuf; |
183 | std::istream aStream (&aFileBuf); |
184 | if (!aFileBuf.open (initfile, ios::in)) |
185 | { |
186 | initfile += L" was not found. The sample can not be shown."; |
187 | getDocument()->UpdateResultMessageDlg ("Textured Shape", initfile); |
188 | return Standard_False; |
189 | } |
7fd59977 |
190 | |
191 | BRep_Builder aBld; |
576f8b11 |
192 | BRepTools::Read (aShape, aStream, aBld); |
193 | if (aShape.IsNull()) |
7fd59977 |
194 | { |
576f8b11 |
195 | initfile += " is invalid. The sample can not be shown."; |
196 | getDocument()->UpdateResultMessageDlg ("Textured Shape", initfile); |
7fd59977 |
197 | return Standard_False; |
198 | } |
199 | |
200 | return Standard_True; |
201 | } |
202 | |
203 | //================================================================ |
204 | // Function : lightsOnOff |
205 | // Purpose : 6 lights are used for a brighter demonstration of textured shapes |
206 | // call lightsOnOff(false) before showing normal shape |
207 | // call lightsOnOff(true) before showing textured shape |
208 | //================================================================ |
209 | void TexturesExt_Presentation::lightsOnOff(Standard_Boolean isOn) |
210 | { |
211 | static Handle_V3d_Light aLight1 = new V3d_DirectionalLight(getViewer(), V3d_XnegYposZneg); |
212 | static Handle_V3d_Light aLight2 = new V3d_DirectionalLight(getViewer(), V3d_XnegYnegZpos); |
213 | static Handle_V3d_Light aLight3 = new V3d_DirectionalLight(getViewer(), V3d_XposYnegZpos); |
214 | static Handle_V3d_Light aLight4 = new V3d_DirectionalLight(getViewer(), V3d_XnegYnegZneg); |
215 | static Handle_V3d_Light aLight5 = new V3d_DirectionalLight(getViewer(), V3d_XnegYposZpos); |
216 | static Handle_V3d_Light aLight6 = new V3d_DirectionalLight(getViewer(), V3d_XposYposZpos); |
217 | |
218 | if (isOn) |
219 | { |
220 | getViewer()->SetLightOn(aLight1); |
221 | getViewer()->SetLightOn(aLight2); |
222 | getViewer()->SetLightOn(aLight3); |
223 | getViewer()->SetLightOn(aLight4); |
224 | getViewer()->SetLightOn(aLight5); |
225 | getViewer()->SetLightOn(aLight6); |
226 | } |
227 | else |
228 | { |
229 | getViewer()->SetLightOff(aLight1); |
230 | getViewer()->SetLightOff(aLight2); |
231 | getViewer()->SetLightOff(aLight3); |
232 | getViewer()->SetLightOff(aLight4); |
233 | getViewer()->SetLightOff(aLight5); |
234 | getViewer()->SetLightOff(aLight6); |
235 | } |
236 | } |
237 | |
238 | //================================================================ |
239 | // Function : TexturesExt_Presentation::sampleBottle |
240 | // Purpose : |
241 | //================================================================ |
242 | void TexturesExt_Presentation::sampleBottle() |
243 | { |
244 | TopoDS_Shape aShape; |
245 | if (!loadShape(aShape, "bottle.brep")) |
246 | return; |
247 | |
248 | // resize and move the shape to the center of the viewer |
249 | gp_Trsf aTrsf1, aTrsf2; |
250 | aTrsf1.SetScale(gp_Pnt(0,0,0), 0.8); |
251 | aTrsf2.SetTranslation(gp_Pnt(0,0,0),gp_Pnt(0,0,-20)); |
252 | aTrsf1.Multiply(aTrsf2); |
253 | BRepBuilderAPI_Transform Transformer(aTrsf1); |
254 | Transformer.Perform(aShape); |
255 | aShape = Transformer.Shape(); |
256 | |
257 | TopTools_IndexedMapOfShape aFaces; |
258 | TopExp::MapShapes(aShape, TopAbs_FACE, aFaces); |
259 | |
260 | // display original shape in shaded display mode |
261 | Handle_AIS_Shape aShapeIO = drawShape(aShape, Graphic3d_NOM_BRASS, Standard_False); |
262 | getAISContext()->SetDisplayMode(aShapeIO, AIS_Shaded, Standard_False); |
cc5ac00d |
263 | // Set increased polygon offset for the main shape to avoid depth collision with textured faces |
264 | aShapeIO->SetPolygonOffsets(Aspect_POM_Fill, 1.5, 0.5); |
7fd59977 |
265 | DISP(aShapeIO); |
266 | |
267 | Handle_AIS_TexturedShape aTFace1 = Texturize(aFaces(16), "carrelage1.gif", 1, 1, 3, 2); |
7fd59977 |
268 | DISP(aTFace1); |
269 | |
270 | Handle_AIS_TexturedShape aTFace2 = Texturize(aFaces(21), "carrelage1.gif", 1, 1, 3, 2); |
7fd59977 |
271 | DISP(aTFace2); |
272 | |
273 | getViewer()->Update(); |
274 | } |
275 | |
276 | |
277 | //================================================================ |
278 | // Function : TexturesExt_Presentation::sampleLand |
279 | // Purpose : |
280 | //================================================================ |
281 | void TexturesExt_Presentation::sampleTerrain() |
282 | { |
283 | TopoDS_Shape aShape; |
284 | if (!loadShape(aShape, "terrain.brep")) |
285 | return; |
286 | |
287 | // a part of the landscape is textured |
288 | TopTools_IndexedMapOfShape aFaces; |
289 | TopExp::MapShapes(aShape, TopAbs_FACE, aFaces); |
290 | |
291 | // TopLoc_Location aLoc; |
292 | // Handle_Geom_Surface aSur = BRep_Tool::Surface(TopoDS::Face(aFaces(1)), aLoc); |
293 | // Standard_Real u1,u2,v1,v2; |
294 | // aSur->Bounds(u1,u2,v1,v2); |
295 | // gp_Pnt aPnt = aSur->Value(u1, v1); |
296 | gp_Pnt aPnt(82100,80300,10940);// point at u1,v1 |
297 | // resize and move the shape to the center of the viewer |
298 | |
299 | gp_Trsf aMoveTrsf; |
300 | gp_Ax3 New(gp_Pnt(-30,-30, 0),gp_Dir(0,0,1)); |
301 | gp_Ax3 Current(aPnt,gp_Dir(0,0,1)); |
302 | aMoveTrsf.SetDisplacement(Current, New); |
303 | |
304 | gp_Trsf aScaleTrsf; |
305 | aScaleTrsf.SetScale(aPnt,0.0075); |
306 | |
307 | BRepBuilderAPI_Transform aTransform(aMoveTrsf*aScaleTrsf); |
308 | |
309 | aTransform.Perform(aFaces(1)); |
310 | aShape = aTransform; |
311 | |
312 | getAISContext()->Display(Texturize(aShape, "terrain.gif")); |
313 | } |
314 | |
315 | |
316 | //================================================================ |
317 | // Function : moveScale |
318 | // Purpose : move a shape a little left and scale it to 15%. |
319 | //================================================================ |
320 | static void moveScale(TopoDS_Shape& aShape) |
321 | { |
322 | gp_Trsf aMoveTrsf; |
323 | gp_Ax3 New(gp_Pnt(-30,-30, -10),gp_Dir(0,0,1)); |
324 | gp_Ax3 Current(gp_Pnt(0,0,0),gp_Dir(0,0,1)); |
325 | aMoveTrsf.SetDisplacement(Current, New); |
326 | |
327 | gp_Trsf aScaleTrsf; |
328 | aScaleTrsf.SetScale(gp_Pnt(0,0,0),0.15); |
329 | |
330 | BRepBuilderAPI_Transform aTransform(aMoveTrsf*aScaleTrsf); |
331 | |
332 | aTransform.Perform(aShape); |
333 | aShape = aTransform; |
334 | } |
335 | |
336 | //================================================================ |
337 | // Function : TexturesExt_Presentation::sampleKitchen |
338 | // Purpose : kitchen with texturized items in it. |
339 | //================================================================ |
340 | void TexturesExt_Presentation::sampleKitchen() |
341 | { |
342 | TopoDS_Shape aShape; |
343 | |
344 | if (!loadShape(aShape, "Kitchen\\Room.brep")) |
345 | return; |
346 | |
347 | gp_Trsf aTrsf; |
348 | gp_Ax3 NewCoordSystem (gp_Pnt(-1,-1, -1),gp_Dir(0,0,1)); |
349 | gp_Ax3 CurrentCoordSystem(gp_Pnt(0,0,0),gp_Dir(0,0,1)); |
350 | aTrsf.SetDisplacement(CurrentCoordSystem, NewCoordSystem); |
351 | aShape.Location(TopLoc_Location(aTrsf)); |
352 | |
353 | moveScale(aShape); |
354 | |
355 | // draw kitchen room whithout one wall (to better see the insides) |
356 | TopTools_IndexedMapOfShape aFaces; |
357 | TopExp::MapShapes(aShape, TopAbs_FACE, aFaces); |
358 | Standard_Integer nbFaces = aFaces.Extent(); |
359 | |
360 | // create a wooden kitchen floor |
361 | // the floor's face will be textured with texture from chataignier.gif |
362 | DISP(Texturize(aFaces(5),"plancher.gif",1,1,2,1)); |
363 | |
364 | // texturize other faces of the room with texture from wallpaper.gif (walls) |
365 | DISP(Texturize(aFaces(1),"wallpaper.gif",1,1,8,6)); |
366 | DISP(Texturize(aFaces(3),"wallpaper.gif",1,1,8,6)); |
367 | DISP(Texturize(aFaces(4),"wallpaper.gif",1,1,8,6)); |
368 | |
369 | // DISP(drawShape(aFaces(1), Quantity_NOC_LIGHTPINK, Standard_False)); |
370 | // DISP(drawShape(aFaces(3), Quantity_NOC_LIGHTPINK, Standard_False)); |
371 | // DISP(drawShape(aFaces(4), Quantity_NOC_LIGHTPINK, Standard_False)); |
372 | |
373 | // texturize furniture items with "wooden" texture |
374 | if (loadShape(aShape, "Kitchen\\MODERN_Table_1.brep")) |
375 | { |
376 | moveScale(aShape); |
377 | DISP(Texturize(aShape, "chataignier.gif")); |
378 | } |
379 | if (loadShape(aShape, "Kitchen\\MODERN_Chair_1.brep")) |
380 | { |
381 | moveScale(aShape); |
382 | DISP(Texturize(aShape, "chataignier.gif")); |
383 | } |
384 | if (loadShape(aShape, "Kitchen\\MODERN_Cooker_1.brep")) |
385 | { |
386 | moveScale(aShape); |
387 | |
388 | aFaces.Clear(); |
389 | TopExp::MapShapes(aShape, TopAbs_FACE, aFaces); |
390 | nbFaces = aFaces.Extent(); |
391 | |
392 | for (Standard_Integer i = 1; i <= nbFaces; i++) |
393 | { |
394 | if (i >= 59) |
395 | DISP(drawShape(aFaces(i), Graphic3d_NOM_STEEL, Standard_False)); |
396 | else if (i >= 29) |
397 | DISP(drawShape(aFaces(i), Graphic3d_NOM_ALUMINIUM, Standard_False)); |
398 | else if (i == 28) |
399 | DISP(Texturize(aFaces(i), "cookerplate.gif")); |
400 | else |
401 | DISP(Texturize(aFaces(i), "chataignier.gif")); |
402 | } |
403 | } |
404 | if (loadShape(aShape, "Kitchen\\MODERN_Cooker_1_opened.brep")) |
405 | { |
406 | moveScale(aShape); |
407 | DISP(Texturize(aShape, "chataignier.gif")); |
408 | } |
409 | if (loadShape(aShape, "Kitchen\\MODERN_Exhaust_1.brep")) |
410 | { |
411 | moveScale(aShape); |
412 | DISP(drawShape(aShape, Graphic3d_NOM_STONE, Standard_False)); |
413 | } |
414 | if (loadShape(aShape, "Kitchen\\MODERN_MVCooker_1.brep")) |
415 | { |
416 | moveScale(aShape); |
417 | DISP(drawShape(aShape, Graphic3d_NOM_SILVER, Standard_False)); |
418 | } |
419 | if (loadShape(aShape, "Kitchen\\MODERN_MVCooker_1_opened.brep")) |
420 | { |
421 | moveScale(aShape); |
422 | DISP(drawShape(aShape, Graphic3d_NOM_SILVER, Standard_False)); |
423 | } |
424 | if (loadShape(aShape, "Kitchen\\MODERN_Sink_1.brep")) |
425 | { |
426 | moveScale(aShape); |
427 | |
428 | aFaces.Clear(); |
429 | TopExp::MapShapes(aShape, TopAbs_FACE, aFaces); |
430 | nbFaces = aFaces.Extent(); |
431 | |
432 | for (Standard_Integer i = 1; i <= nbFaces; i++) |
433 | { |
434 | if (i < 145) |
435 | DISP(drawShape(aFaces(i), Graphic3d_NOM_ALUMINIUM, Standard_False)); |
436 | else if (i == 145) |
437 | DISP(Texturize(aFaces(i), "cookerplate.gif")); |
438 | else |
439 | DISP(Texturize(aFaces(i), "chataignier.gif")); |
440 | } |
441 | } |
442 | if (loadShape(aShape, "Kitchen\\MODERN_Sink_1_opened.brep")) |
443 | { |
444 | moveScale(aShape); |
445 | DISP(Texturize(aShape, "chataignier.gif")); |
446 | } |
447 | if (loadShape(aShape, "Kitchen\\MODERN_Refrigerator_1.brep")) |
448 | { |
449 | moveScale(aShape); |
450 | DISP(drawShape(aShape, Graphic3d_NOM_CHROME, Standard_False)); |
451 | } |
452 | if (loadShape(aShape, "Kitchen\\MODERN_Refrigerator_1_opened.brep")) |
453 | { |
454 | moveScale(aShape); |
455 | DISP(drawShape(aShape, Graphic3d_NOM_CHROME, Standard_False)); |
456 | } |
457 | |
458 | getViewer()->Update(); |
459 | } |