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(); |
86 | Handle_V3d_View aView = getViewer()->ActiveView(); |
87 | aView->SetSurfaceDetail(V3d_TEX_ALL); |
88 | aView->SetSize(ZVIEW_SIZE); |
89 | |
90 | setResultTitle("Textured Shape"); |
91 | setResultText( |
92 | " TopoDS_Shape aShape;" EOL |
93 | "" EOL |
94 | " // initialize aShape" EOL |
95 | " // aShape = ..." EOL |
96 | "" EOL |
97 | " // create a textured presentation object for aShape" EOL |
98 | " Handle_AIS_TexturedShape aTShape = new AIS_TexturedShape(aShape);" EOL |
99 | "" EOL |
100 | " TCollection_AsciiString aTFileName;" EOL |
101 | "" EOL |
102 | " // initialize aTFileName with an existing texture file name" EOL |
103 | " // (gif, bmp, xwd, rgb, and other formats are supported)" EOL |
104 | " // OR with an integer value string (max = Graphic3d_Texture2D::NumberOfTextures())" EOL |
105 | " // which will indicate use of predefined texture of this number" EOL |
106 | " // aTFileName = ..." EOL |
107 | "" EOL |
108 | " aTShape->SetTextureFileName(aTFileName);" EOL |
109 | "" EOL |
110 | " // do other initialization of AIS_TexturedShape" EOL |
111 | " Standard_Real nRepeat;" EOL |
112 | " Standard_Boolean toRepeat;" EOL |
113 | " Standard_Boolean toScale;" EOL |
114 | " // initialize aRepeat, toRepeat, toScale ..." EOL |
115 | "" EOL |
116 | " aTShape->SetTextureMapOn();" EOL |
117 | " aTShape->SetTextureRepeat(toRepeat, nRepeat, nRepeat);" EOL |
118 | " aTShape->SetTextureScale(toScale);" EOL |
119 | " " EOL |
120 | " // mode 3 is \"textured\" mode of AIS_TexturedShape, " EOL |
121 | " // other modes will display the \"normal\", non-textured shape," EOL |
122 | " // in wireframe(1) or shaded(2) modes correspondingly" EOL |
123 | " aTShape->SetDisplayMode(3); " EOL |
124 | "" EOL |
125 | " // V3d_TEX_ALL constant must be set as surface detail" EOL |
126 | " // for current view to see AIS_TexturedShape" EOL |
127 | " myCurrentView->SetSurfaceDetail(V3d_TEX_ALL);" EOL); |
128 | } |
129 | |
130 | ////////////////////////////////////////////////////////////////////// |
131 | // Sample functions |
132 | ////////////////////////////////////////////////////////////////////// |
133 | |
134 | |
135 | |
136 | |
137 | //================================================================ |
138 | // Function : lightsOnOff |
139 | // Purpose : 6 lights are used for a brighter demonstration of textured shapes |
140 | // call lightsOnOff(false) before showing normal shape |
141 | // call lightsOnOff(true) before showing textured shape |
142 | //================================================================ |
143 | void Textures_Presentation::lightsOnOff(Standard_Boolean isOn) |
144 | { |
145 | static Handle_V3d_Light aLight1 = new V3d_DirectionalLight(getViewer(), V3d_XnegYposZneg); |
146 | static Handle_V3d_Light aLight2 = new V3d_DirectionalLight(getViewer(), V3d_XnegYnegZpos); |
147 | static Handle_V3d_Light aLight3 = new V3d_DirectionalLight(getViewer(), V3d_XposYnegZpos); |
148 | static Handle_V3d_Light aLight4 = new V3d_DirectionalLight(getViewer(), V3d_XnegYnegZneg); |
149 | static Handle_V3d_Light aLight5 = new V3d_DirectionalLight(getViewer(), V3d_XnegYposZpos); |
150 | static Handle_V3d_Light aLight6 = new V3d_DirectionalLight(getViewer(), V3d_XposYposZpos); |
151 | |
152 | if (isOn) |
153 | { |
154 | getViewer()->SetLightOn(aLight1); |
155 | getViewer()->SetLightOn(aLight2); |
156 | getViewer()->SetLightOn(aLight3); |
157 | getViewer()->SetLightOn(aLight4); |
158 | getViewer()->SetLightOn(aLight5); |
159 | getViewer()->SetLightOn(aLight6); |
160 | } |
161 | else |
162 | { |
163 | getViewer()->SetLightOff(aLight1); |
164 | getViewer()->SetLightOff(aLight2); |
165 | getViewer()->SetLightOff(aLight3); |
166 | getViewer()->SetLightOff(aLight4); |
167 | getViewer()->SetLightOff(aLight5); |
168 | getViewer()->SetLightOff(aLight6); |
169 | } |
170 | } |
171 | |
172 | //================================================================ |
173 | // Function : Textures_Presentation::displayTextured |
174 | // display an AIS_TexturedShape based on a given shape with texture with given filename |
175 | // filename can also be an integer value ("2", "5", etc.), in this case |
176 | // a predefined texture from Graphic3d_NameOfTexture2D with number = this value |
177 | // is loaded. |
178 | // the function returns !WAIT_A_SECOND |
179 | //================================================================ |
180 | Standard_Boolean Textures_Presentation::displayTextured(const TopoDS_Shape& aShape, |
181 | TCollection_AsciiString aTFileName, |
182 | Standard_Boolean toDisplayOriginal, |
183 | Standard_Boolean toScale, |
184 | Standard_Integer nRepeat) |
185 | { |
186 | // create a textured presentation object for aShape |
187 | Handle_AIS_TexturedShape aTShape = new AIS_TexturedShape(aShape); |
188 | |
189 | // load texture from file if it is not an integer value |
190 | // integer value indicates a number of texture in predefined textures enumeration |
191 | if (!aTFileName.IsIntegerValue()) |
192 | { |
193 | TCollection_AsciiString aTmp(aTFileName); |
194 | aTFileName = GetDataDir(); |
195 | aTFileName = aTFileName + "\\" + aTmp; |
196 | } |
197 | |
198 | aTShape->SetTextureFileName(aTFileName); |
199 | |
200 | // do other initialization of AIS_TexturedShape |
201 | aTShape->SetTextureMapOn(); |
202 | aTShape->SetTextureRepeat(Standard_True, nRepeat, nRepeat); |
203 | aTShape->SetTextureScale(toScale); |
204 | |
205 | aTShape->SetDisplayMode(3); // mode 3 is "textured" mode |
206 | |
207 | // output to the screen, first original shape, then textured |
208 | if (toDisplayOriginal) |
209 | { |
210 | lightsOnOff(Standard_False); |
211 | |
212 | Handle_AIS_Shape aAisShape = drawShape(aShape/*, Quantity_NOC_WHITE*/); |
213 | if (WAIT_A_SECOND) return Standard_False; |
214 | getAISContext()->Erase(aAisShape, Standard_False); |
215 | } |
216 | |
217 | // show textured shape more brightly - turn on more lights |
218 | lightsOnOff(Standard_True); |
219 | |
220 | getAISContext()->Display(aTShape, Standard_True); |
221 | |
222 | return Standard_True; |
223 | } |
224 | |
225 | |
226 | //================================================================ |
227 | // Function : Textures_Presentation::loadShape |
228 | // loads a shape from a given brep file from data dir into a given TopoDS_Shape object |
229 | //================================================================ |
230 | Standard_Boolean Textures_Presentation::loadShape(TopoDS_Shape& aShape, |
231 | TCollection_AsciiString aFileName) |
232 | { |
233 | // create a TopoDS_Shape -> read from a brep file |
234 | TCollection_AsciiString aPath(GetDataDir()); |
235 | aPath = aPath + "\\" + aFileName; |
236 | |
237 | BRep_Builder aBld; |
238 | Standard_Boolean isRead = BRepTools::Read (aShape, aPath.ToCString(), aBld); |
239 | if (!isRead) |
240 | { |
241 | aPath += " was not found. The sample can not be shown."; |
242 | setResultText(aPath.ToCString()); |
243 | return Standard_False; |
244 | } |
245 | |
246 | return Standard_True; |
247 | } |
248 | |
249 | |
250 | //================================================================ |
251 | // Function : ScaleShape |
252 | // Purpose : scales a given TopoDS_Shape |
253 | //================================================================ |
254 | static void ScaleShape(TopoDS_Shape& aShape, Standard_Real ScaleFactor) |
255 | { |
256 | gp_Trsf aTrsf; |
257 | aTrsf.SetScaleFactor(ScaleFactor); |
258 | aShape.Location(TopLoc_Location(aTrsf)); |
259 | } |
260 | |
261 | //================================================================ |
262 | // Function : Textures_Presentation::samplePoints |
263 | // Purpose : |
264 | //================================================================ |
265 | void Textures_Presentation::sampleCustomTexture1() |
266 | { |
267 | Standard_Real a = 30.0; |
268 | TopoDS_Shape aShape = BRepPrimAPI_MakeBox(gp_Pnt(-a/2,-a/2,-a/2),a,a,a); |
269 | TopTools_IndexedMapOfShape aFaces; |
270 | TopExp::MapShapes(aShape, TopAbs_FACE, aFaces); |
271 | |
272 | // faces on faces |
273 | Standard_CString OurFaces[] = |
274 | { |
275 | "aev.gif","mla.gif","msv.gif","asv.gif","agn.gif","emo.gif",0 |
276 | }; |
277 | |
278 | lightsOnOff(Standard_False); |
279 | for (Standard_Integer i = 1; i <= aFaces.Extent(); i++) |
280 | { |
281 | drawShape(aFaces(i)); |
282 | if (WAIT_A_LITTLE) return; |
283 | } |
284 | |
285 | getViewer()->Erase(); |
286 | |
287 | for (i = 1; i <= aFaces.Extent(); i++) |
288 | { |
289 | displayTextured(aFaces(i), OurFaces[i-1], Standard_False, Standard_True, 1); |
290 | if (WAIT_A_LITTLE) return; |
291 | } |
292 | } |
293 | |
294 | |
295 | //================================================================ |
296 | // Function : Textures_Presentation::sampleCustomTexture2 |
297 | // Purpose : |
298 | //================================================================ |
299 | void Textures_Presentation::sampleCustomTexture2() |
300 | { |
301 | TopoDS_Shape aShape; |
302 | if (loadShape(aShape, "Pump_Nut.brep")) |
303 | { |
304 | // move the shape to [0,0,0] |
305 | gp_Trsf aTrsf; |
306 | gp_Ax3 oldAx3(gp_Pnt(81,280,0), gp::DZ()); |
307 | gp_Ax3 newAx3(gp_Pnt(0,0,0), gp::DZ()); |
308 | aTrsf.SetDisplacement(oldAx3, newAx3); |
309 | aShape.Move(TopLoc_Location(aTrsf)); |
310 | |
311 | displayTextured(aShape, "texture1.gif"); |
312 | } |
313 | } |
314 | |
315 | |
316 | //================================================================ |
317 | // Function : Textures_Presentation::sampleCustomTexture3 |
318 | // Purpose : |
319 | //================================================================ |
320 | void Textures_Presentation::sampleCustomTexture3() |
321 | { |
322 | TopoDS_Shape aShape; |
323 | if (loadShape(aShape, "Pump_TopCover.brep")) |
324 | { |
325 | ScaleShape(aShape, 0.2); |
326 | displayTextured(aShape, "texture2.gif"); |
327 | } |
328 | } |
329 | |
330 | /* |
331 | //================================================================ |
332 | // Function : Textures_Presentation::samplePredefTexture1 |
333 | // Purpose : |
334 | //================================================================ |
335 | void Textures_Presentation::samplePredefTexture1() |
336 | { |
337 | // the number of points of wires: |
338 | const Standard_Integer aNbOfPnt = 4; |
339 | |
340 | // creates arrays of coordinates of wires: |
341 | Standard_Real aCoords [][aNbOfPnt][3] = |
342 | {{{-4,0,-2},{0,4,-2},{4,0,-2},{0,-4,-2}},{{-2,-2,2},{-2,2,2},{2,2,2},{2,-2,2}}}; |
343 | |
344 | // the number of wires: |
345 | Standard_Integer aNbOfWire = (sizeof(aCoords)/(sizeof(Standard_Real)*3))/aNbOfPnt; |
346 | |
347 | BRepOffsetAPI_ThruSections aTSec(Standard_True,Standard_True,0.001); |
348 | for( Standard_Integer i = 0 ; i < aNbOfWire ; i++) |
349 | { |
350 | BRepBuilderAPI_MakePolygon aPol; |
351 | for( Standard_Integer j = 0 ; j < aNbOfPnt ; j++) |
352 | aPol.Add(gp_Pnt(aCoords[i][j][0],aCoords[i][j][1],aCoords[i][j][2])); |
353 | |
354 | aPol.Close(); |
355 | aTSec.AddWire(aPol.Wire()); |
356 | } |
357 | |
358 | aTSec.AddWire(BRepBuilderAPI_MakeWire( |
359 | BRepBuilderAPI_MakeEdge(gce_MakeCirc(gp_Pnt(0,0,4),gp::DZ(),5)))); |
360 | |
361 | aTSec.Build(); |
362 | |
363 | if (!aTSec.IsDone()) return; |
364 | |
365 | TopoDS_Shape aShape = aTSec.Shape(); |
366 | |
367 | ScaleShape(aShape, 5); |
368 | |
369 | // texture 1 - "alien skin" |
370 | displayTextured(aShape, "1"); |
371 | } |
372 | |
373 | |
374 | //================================================================ |
375 | // Function : Textures_Presentation::samplePredefTexture2 |
376 | // Purpose : |
377 | //================================================================ |
378 | void Textures_Presentation::samplePredefTexture2() |
379 | { |
380 | Standard_Integer aColLength = 4; |
381 | Standard_Integer aRowLength = 4; |
382 | TColStd_Array2OfReal aZPoints(1,aColLength,1,aRowLength); |
383 | |
384 | Standard_Real theZCoords [] = {{-2},{1},{1},{-1},{1},{-2}, |
385 | {-2},{1},{-2},{-3},{-2},{-2},{-4},{-2},{-2},{-2}}; |
386 | |
387 | for(Standard_Integer i = 0; i < aRowLength ; i++) |
388 | for(Standard_Integer j = 0; j < aColLength ; j++) |
389 | aZPoints(i+1,j+1) = (theZCoords[i*aRowLength+j] * 50); |
390 | |
391 | Standard_Real aXStep = 250, aYStep = 250; |
392 | Standard_Real aXBound = -450, aYBound = -300; |
393 | |
394 | Handle_Geom_BSplineSurface aSurface = |
395 | GeomAPI_PointsToBSplineSurface(aZPoints,aXBound,aXStep,aYBound,aYStep,3,8); |
396 | |
397 | TopoDS_Face aShape = BRepBuilderAPI_MakeFace(aSurface); |
398 | |
399 | ScaleShape(aShape, 0.09); |
400 | |
401 | // texture 9 - "clouds" |
402 | displayTextured(aShape, "9", Standard_True, Standard_True, 1); |
403 | } |
404 | */ |