7fd59977 |
1 | // Textures_Presentation.cpp: implementation of the Textures_Presentation class. |
2 | // Creation of textural presentation of shape |
3 | ////////////////////////////////////////////////////////////////////// |
4 | |
5 | #include "stdafx.h" |
6 | #include "Textures_Presentation.h" |
7 | |
8 | |
9 | #include <TopoDS_Solid.hxx> |
10 | #include <BRepPrimAPI_MakeBox.hxx> |
11 | #include <BRepFeat_Gluer.hxx> |
12 | #include <TopExp.hxx> |
13 | #include <TopTools_IndexedMapOfShape.hxx> |
14 | #include <BRepBuilderAPI_MakeEdge.hxx> |
15 | #include <Geom_Circle.hxx> |
16 | #include <Geom_TrimmedCurve.hxx> |
17 | #include <BRepBuilderAPI_MakeWire.hxx> |
18 | #include <BRepPrimAPI_MakePrism.hxx> |
19 | #include <BRepBuilderAPI_MakeFace.hxx> |
20 | #include <BRepPrimAPI_MakeCylinder.hxx> |
21 | #include <BRepAlgoAPI_Cut.hxx> |
22 | #include <AIS_TexturedShape.hxx> |
23 | #include <BRepTools.hxx> |
24 | #include <Geom_BSplineSurface.hxx> |
25 | #include <GeomAPI_PointsToBSplineSurface.hxx> |
26 | #include <TopTools_SequenceOfShape.hxx> |
27 | #include <BRepBuilderAPI_MakePolygon.hxx> |
28 | #include <gce_MakeCirc.hxx> |
29 | #include <BRepOffsetAPI_ThruSections.hxx> |
30 | #include <V3d_AmbientLight.hxx> |
31 | #include <V3d_PositionalLight.hxx> |
32 | #include <V3d_DirectionalLight.hxx> |
33 | #include <V3d_SpotLight.hxx> |
6262338c |
34 | #include <Prs3d_Drawer.hxx> |
7fd59977 |
35 | #include <Prs3d_ShadingAspect.hxx> |
36 | #include <Graphic3d_AspectFillArea3d.hxx> |
37 | #include <Graphic3d_Texture2D.hxx> |
38 | |
39 | |
40 | // Initialization of global variable with an instance of this class |
41 | OCCDemo_Presentation* OCCDemo_Presentation::Current = new Textures_Presentation; |
42 | |
43 | // Initialization of array of samples |
44 | const Textures_Presentation::PSampleFuncType Textures_Presentation::SampleFuncs[] = |
45 | { |
46 | &Textures_Presentation::sampleCustomTexture2, |
47 | &Textures_Presentation::sampleCustomTexture3, |
48 | // &Textures_Presentation::samplePredefTexture1, |
49 | // &Textures_Presentation::samplePredefTexture2, |
50 | &Textures_Presentation::sampleCustomTexture1 |
51 | }; |
52 | |
53 | #ifdef WNT |
54 | #define EOL "\r\n" |
55 | #else |
56 | #define EOL "\n" |
57 | #endif |
58 | |
59 | #define ZVIEW_SIZE 100 |
60 | |
61 | ////////////////////////////////////////////////////////////////////// |
62 | // Construction/Destruction |
63 | ////////////////////////////////////////////////////////////////////// |
64 | |
65 | Textures_Presentation::Textures_Presentation() |
66 | { |
67 | myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType); |
68 | setName ("Textured Shapes"); |
69 | } |
70 | |
71 | ////////////////////////////////////////////////////////////////////// |
72 | // Sample execution |
73 | ////////////////////////////////////////////////////////////////////// |
74 | |
75 | void Textures_Presentation::DoSample() |
76 | { |
77 | getAISContext()->EraseAll(); |
78 | if (myIndex >=0 && myIndex < myNbSamples) |
79 | (this->*SampleFuncs[myIndex])(); |
80 | } |
81 | |
82 | void Textures_Presentation::Init() |
83 | { |
84 | // initialize v3d_view so it displays textures well |
85 | getViewer()->InitActiveViews(); |
92efcf78 |
86 | Handle(V3d_View) aView = getViewer()->ActiveView(); |
7fd59977 |
87 | aView->SetSize(ZVIEW_SIZE); |
88 | |
89 | setResultTitle("Textured Shape"); |
90 | setResultText( |
91 | " TopoDS_Shape aShape;" EOL |
92 | "" EOL |
93 | " // initialize aShape" EOL |
94 | " // aShape = ..." EOL |
95 | "" EOL |
96 | " // create a textured presentation object for aShape" EOL |
92efcf78 |
97 | " Handle(AIS_TexturedShape) aTShape = new AIS_TexturedShape(aShape);" EOL |
7fd59977 |
98 | "" EOL |
99 | " TCollection_AsciiString aTFileName;" EOL |
100 | "" EOL |
101 | " // initialize aTFileName with an existing texture file name" EOL |
102 | " // (gif, bmp, xwd, rgb, and other formats are supported)" EOL |
103 | " // OR with an integer value string (max = Graphic3d_Texture2D::NumberOfTextures())" EOL |
104 | " // which will indicate use of predefined texture of this number" EOL |
105 | " // aTFileName = ..." EOL |
106 | "" EOL |
107 | " aTShape->SetTextureFileName(aTFileName);" EOL |
108 | "" EOL |
109 | " // do other initialization of AIS_TexturedShape" EOL |
110 | " Standard_Real nRepeat;" EOL |
111 | " Standard_Boolean toRepeat;" EOL |
112 | " Standard_Boolean toScale;" EOL |
113 | " // initialize aRepeat, toRepeat, toScale ..." EOL |
114 | "" EOL |
115 | " aTShape->SetTextureMapOn();" EOL |
116 | " aTShape->SetTextureRepeat(toRepeat, nRepeat, nRepeat);" EOL |
117 | " aTShape->SetTextureScale(toScale);" EOL |
118 | " " EOL |
119 | " // mode 3 is \"textured\" mode of AIS_TexturedShape, " EOL |
120 | " // other modes will display the \"normal\", non-textured shape," EOL |
121 | " // in wireframe(1) or shaded(2) modes correspondingly" EOL |
83da37b1 |
122 | " aTShape->SetDisplayMode(3); " EOL); |
7fd59977 |
123 | } |
124 | |
125 | ////////////////////////////////////////////////////////////////////// |
126 | // Sample functions |
127 | ////////////////////////////////////////////////////////////////////// |
128 | |
129 | |
130 | |
131 | |
132 | //================================================================ |
133 | // Function : lightsOnOff |
134 | // Purpose : 6 lights are used for a brighter demonstration of textured shapes |
135 | // call lightsOnOff(false) before showing normal shape |
136 | // call lightsOnOff(true) before showing textured shape |
137 | //================================================================ |
138 | void Textures_Presentation::lightsOnOff(Standard_Boolean isOn) |
139 | { |
992ed6b3 |
140 | static Handle(V3d_Light) aLight1 = new V3d_DirectionalLight(V3d_XnegYposZneg); |
141 | static Handle(V3d_Light) aLight2 = new V3d_DirectionalLight(V3d_XnegYnegZpos); |
142 | static Handle(V3d_Light) aLight3 = new V3d_DirectionalLight(V3d_XposYnegZpos); |
143 | static Handle(V3d_Light) aLight4 = new V3d_DirectionalLight(V3d_XnegYnegZneg); |
144 | static Handle(V3d_Light) aLight5 = new V3d_DirectionalLight(V3d_XnegYposZpos); |
145 | static Handle(V3d_Light) aLight6 = new V3d_DirectionalLight(V3d_XposYposZpos); |
7fd59977 |
146 | |
147 | if (isOn) |
148 | { |
149 | getViewer()->SetLightOn(aLight1); |
150 | getViewer()->SetLightOn(aLight2); |
151 | getViewer()->SetLightOn(aLight3); |
152 | getViewer()->SetLightOn(aLight4); |
153 | getViewer()->SetLightOn(aLight5); |
154 | getViewer()->SetLightOn(aLight6); |
155 | } |
156 | else |
157 | { |
158 | getViewer()->SetLightOff(aLight1); |
159 | getViewer()->SetLightOff(aLight2); |
160 | getViewer()->SetLightOff(aLight3); |
161 | getViewer()->SetLightOff(aLight4); |
162 | getViewer()->SetLightOff(aLight5); |
163 | getViewer()->SetLightOff(aLight6); |
164 | } |
165 | } |
166 | |
167 | //================================================================ |
168 | // Function : Textures_Presentation::displayTextured |
169 | // display an AIS_TexturedShape based on a given shape with texture with given filename |
170 | // filename can also be an integer value ("2", "5", etc.), in this case |
171 | // a predefined texture from Graphic3d_NameOfTexture2D with number = this value |
172 | // is loaded. |
173 | // the function returns !WAIT_A_SECOND |
174 | //================================================================ |
175 | Standard_Boolean Textures_Presentation::displayTextured(const TopoDS_Shape& aShape, |
176 | TCollection_AsciiString aTFileName, |
177 | Standard_Boolean toDisplayOriginal, |
178 | Standard_Boolean toScale, |
179 | Standard_Integer nRepeat) |
180 | { |
181 | // create a textured presentation object for aShape |
92efcf78 |
182 | Handle(AIS_TexturedShape) aTShape = new AIS_TexturedShape(aShape); |
7fd59977 |
183 | |
184 | // load texture from file if it is not an integer value |
185 | // integer value indicates a number of texture in predefined textures enumeration |
186 | if (!aTFileName.IsIntegerValue()) |
187 | { |
188 | TCollection_AsciiString aTmp(aTFileName); |
189 | aTFileName = GetDataDir(); |
190 | aTFileName = aTFileName + "\\" + aTmp; |
191 | } |
192 | |
193 | aTShape->SetTextureFileName(aTFileName); |
194 | |
195 | // do other initialization of AIS_TexturedShape |
196 | aTShape->SetTextureMapOn(); |
197 | aTShape->SetTextureRepeat(Standard_True, nRepeat, nRepeat); |
198 | aTShape->SetTextureScale(toScale); |
199 | |
200 | aTShape->SetDisplayMode(3); // mode 3 is "textured" mode |
201 | |
202 | // output to the screen, first original shape, then textured |
203 | if (toDisplayOriginal) |
204 | { |
205 | lightsOnOff(Standard_False); |
206 | |
92efcf78 |
207 | Handle(AIS_Shape) aAisShape = drawShape(aShape/*, Quantity_NOC_WHITE*/); |
7fd59977 |
208 | if (WAIT_A_SECOND) return Standard_False; |
209 | getAISContext()->Erase(aAisShape, Standard_False); |
210 | } |
211 | |
212 | // show textured shape more brightly - turn on more lights |
213 | lightsOnOff(Standard_True); |
214 | |
215 | getAISContext()->Display(aTShape, Standard_True); |
216 | |
217 | return Standard_True; |
218 | } |
219 | |
220 | |
221 | //================================================================ |
222 | // Function : Textures_Presentation::loadShape |
223 | // loads a shape from a given brep file from data dir into a given TopoDS_Shape object |
224 | //================================================================ |
225 | Standard_Boolean Textures_Presentation::loadShape(TopoDS_Shape& aShape, |
226 | TCollection_AsciiString aFileName) |
227 | { |
228 | // create a TopoDS_Shape -> read from a brep file |
229 | TCollection_AsciiString aPath(GetDataDir()); |
230 | aPath = aPath + "\\" + aFileName; |
231 | |
232 | BRep_Builder aBld; |
233 | Standard_Boolean isRead = BRepTools::Read (aShape, aPath.ToCString(), aBld); |
234 | if (!isRead) |
235 | { |
236 | aPath += " was not found. The sample can not be shown."; |
237 | setResultText(aPath.ToCString()); |
238 | return Standard_False; |
239 | } |
240 | |
241 | return Standard_True; |
242 | } |
243 | |
244 | |
245 | //================================================================ |
246 | // Function : ScaleShape |
247 | // Purpose : scales a given TopoDS_Shape |
248 | //================================================================ |
249 | static void ScaleShape(TopoDS_Shape& aShape, Standard_Real ScaleFactor) |
250 | { |
251 | gp_Trsf aTrsf; |
252 | aTrsf.SetScaleFactor(ScaleFactor); |
253 | aShape.Location(TopLoc_Location(aTrsf)); |
254 | } |
255 | |
256 | //================================================================ |
257 | // Function : Textures_Presentation::samplePoints |
258 | // Purpose : |
259 | //================================================================ |
260 | void Textures_Presentation::sampleCustomTexture1() |
261 | { |
262 | Standard_Real a = 30.0; |
263 | TopoDS_Shape aShape = BRepPrimAPI_MakeBox(gp_Pnt(-a/2,-a/2,-a/2),a,a,a); |
264 | TopTools_IndexedMapOfShape aFaces; |
265 | TopExp::MapShapes(aShape, TopAbs_FACE, aFaces); |
266 | |
267 | // faces on faces |
268 | Standard_CString OurFaces[] = |
269 | { |
270 | "aev.gif","mla.gif","msv.gif","asv.gif","agn.gif","emo.gif",0 |
271 | }; |
272 | |
273 | lightsOnOff(Standard_False); |
274 | for (Standard_Integer i = 1; i <= aFaces.Extent(); i++) |
275 | { |
276 | drawShape(aFaces(i)); |
277 | if (WAIT_A_LITTLE) return; |
278 | } |
279 | |
280 | getViewer()->Erase(); |
281 | |
282 | for (i = 1; i <= aFaces.Extent(); i++) |
283 | { |
284 | displayTextured(aFaces(i), OurFaces[i-1], Standard_False, Standard_True, 1); |
285 | if (WAIT_A_LITTLE) return; |
286 | } |
287 | } |
288 | |
289 | |
290 | //================================================================ |
291 | // Function : Textures_Presentation::sampleCustomTexture2 |
292 | // Purpose : |
293 | //================================================================ |
294 | void Textures_Presentation::sampleCustomTexture2() |
295 | { |
296 | TopoDS_Shape aShape; |
297 | if (loadShape(aShape, "Pump_Nut.brep")) |
298 | { |
299 | // move the shape to [0,0,0] |
300 | gp_Trsf aTrsf; |
301 | gp_Ax3 oldAx3(gp_Pnt(81,280,0), gp::DZ()); |
302 | gp_Ax3 newAx3(gp_Pnt(0,0,0), gp::DZ()); |
303 | aTrsf.SetDisplacement(oldAx3, newAx3); |
304 | aShape.Move(TopLoc_Location(aTrsf)); |
305 | |
306 | displayTextured(aShape, "texture1.gif"); |
307 | } |
308 | } |
309 | |
310 | |
311 | //================================================================ |
312 | // Function : Textures_Presentation::sampleCustomTexture3 |
313 | // Purpose : |
314 | //================================================================ |
315 | void Textures_Presentation::sampleCustomTexture3() |
316 | { |
317 | TopoDS_Shape aShape; |
318 | if (loadShape(aShape, "Pump_TopCover.brep")) |
319 | { |
320 | ScaleShape(aShape, 0.2); |
321 | displayTextured(aShape, "texture2.gif"); |
322 | } |
323 | } |
324 | |
325 | /* |
326 | //================================================================ |
327 | // Function : Textures_Presentation::samplePredefTexture1 |
328 | // Purpose : |
329 | //================================================================ |
330 | void Textures_Presentation::samplePredefTexture1() |
331 | { |
332 | // the number of points of wires: |
333 | const Standard_Integer aNbOfPnt = 4; |
334 | |
335 | // creates arrays of coordinates of wires: |
336 | Standard_Real aCoords [][aNbOfPnt][3] = |
337 | {{{-4,0,-2},{0,4,-2},{4,0,-2},{0,-4,-2}},{{-2,-2,2},{-2,2,2},{2,2,2},{2,-2,2}}}; |
338 | |
339 | // the number of wires: |
340 | Standard_Integer aNbOfWire = (sizeof(aCoords)/(sizeof(Standard_Real)*3))/aNbOfPnt; |
341 | |
342 | BRepOffsetAPI_ThruSections aTSec(Standard_True,Standard_True,0.001); |
343 | for( Standard_Integer i = 0 ; i < aNbOfWire ; i++) |
344 | { |
345 | BRepBuilderAPI_MakePolygon aPol; |
346 | for( Standard_Integer j = 0 ; j < aNbOfPnt ; j++) |
347 | aPol.Add(gp_Pnt(aCoords[i][j][0],aCoords[i][j][1],aCoords[i][j][2])); |
348 | |
349 | aPol.Close(); |
350 | aTSec.AddWire(aPol.Wire()); |
351 | } |
352 | |
353 | aTSec.AddWire(BRepBuilderAPI_MakeWire( |
354 | BRepBuilderAPI_MakeEdge(gce_MakeCirc(gp_Pnt(0,0,4),gp::DZ(),5)))); |
355 | |
356 | aTSec.Build(); |
357 | |
358 | if (!aTSec.IsDone()) return; |
359 | |
360 | TopoDS_Shape aShape = aTSec.Shape(); |
361 | |
362 | ScaleShape(aShape, 5); |
363 | |
364 | // texture 1 - "alien skin" |
365 | displayTextured(aShape, "1"); |
366 | } |
367 | |
368 | |
369 | //================================================================ |
370 | // Function : Textures_Presentation::samplePredefTexture2 |
371 | // Purpose : |
372 | //================================================================ |
373 | void Textures_Presentation::samplePredefTexture2() |
374 | { |
375 | Standard_Integer aColLength = 4; |
376 | Standard_Integer aRowLength = 4; |
377 | TColStd_Array2OfReal aZPoints(1,aColLength,1,aRowLength); |
378 | |
379 | Standard_Real theZCoords [] = {{-2},{1},{1},{-1},{1},{-2}, |
380 | {-2},{1},{-2},{-3},{-2},{-2},{-4},{-2},{-2},{-2}}; |
381 | |
382 | for(Standard_Integer i = 0; i < aRowLength ; i++) |
383 | for(Standard_Integer j = 0; j < aColLength ; j++) |
384 | aZPoints(i+1,j+1) = (theZCoords[i*aRowLength+j] * 50); |
385 | |
386 | Standard_Real aXStep = 250, aYStep = 250; |
387 | Standard_Real aXBound = -450, aYBound = -300; |
388 | |
92efcf78 |
389 | Handle(Geom_BSplineSurface) aSurface = |
7fd59977 |
390 | GeomAPI_PointsToBSplineSurface(aZPoints,aXBound,aXStep,aYBound,aYStep,3,8); |
391 | |
392 | TopoDS_Face aShape = BRepBuilderAPI_MakeFace(aSurface); |
393 | |
394 | ScaleShape(aShape, 0.09); |
395 | |
396 | // texture 9 - "clouds" |
397 | displayTextured(aShape, "9", Standard_True, Standard_True, 1); |
398 | } |
399 | */ |