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 | { |
61 | ((CViewer3dApp*) AfxGetApp())->SetSampleName("Viewer3d"); |
62 | ((CViewer3dApp*) AfxGetApp())->SetSamplePath("..\\..\\04_Viewer3d"); |
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 | |
155 | aTShape->SetTextureFileName((Standard_CString)(LPCTSTR)initfile); |
156 | |
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); |
162 | |
163 | aTShape->SetDisplayMode(3); // mode 3 is "textured" mode |
164 | |
165 | return aTShape; |
166 | } |
167 | |
168 | |
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) |
175 | { |
176 | // create a TopoDS_Shape -> read from a brep file |
5c1f974e |
177 | CString initfile(((OCC_App*) AfxGetApp())->GetInitDataDir()); |
7fd59977 |
178 | initfile += "\\Data\\"; |
179 | initfile += aFileName.ToCString(); |
180 | |
181 | TCollection_AsciiString Path((Standard_CString)(LPCTSTR)initfile); |
182 | |
183 | |
184 | BRep_Builder aBld; |
185 | //Standard_Boolean isRead = BRepTools::Read (aShape, aPath.ToCString(), aBld); |
186 | //if (!isRead) |
187 | // isRead = BRepTools::Read (aShape, bPath.ToCString(), aBld); |
188 | Standard_Boolean isRead = BRepTools::Read (aShape, Path.ToCString(), aBld); |
189 | if (!isRead) |
190 | { |
191 | Path += " was not found. The sample can not be shown."; |
192 | getDocument()->UpdateResultMessageDlg("Textured Shape", Path.ToCString()); |
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 | { |
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); |
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 |
257 | Handle_AIS_Shape aShapeIO = drawShape(aShape, Graphic3d_NOM_BRASS, Standard_False); |
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 | |
263 | Handle_AIS_TexturedShape aTFace1 = Texturize(aFaces(16), "carrelage1.gif", 1, 1, 3, 2); |
7fd59977 |
264 | DISP(aTFace1); |
265 | |
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; |
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 |
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 | } |