0027670: Visualization - avoid duplication of structures defining primitive array...
[occt.git] / src / OpenGl / OpenGl_LineAttributes.cxx
1 // Created on: 2011-10-25
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <OpenGl_GlCore11.hxx>
17
18 #include <OpenGl_LineAttributes.hxx>
19 #include <OpenGl_Context.hxx>
20
21 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_LineAttributes,OpenGl_Resource)
22
23 static const unsigned int myInteriors[TEL_HS_USER_DEF_START][32] =
24 {
25   // Aspect_HS_SOLID
26   {
27     0xFFFFFFFF,
28     0xFFFFFFFF,
29     0xFFFFFFFF,
30     0xFFFFFFFF,
31     0xFFFFFFFF,
32     0xFFFFFFFF,
33     0xFFFFFFFF,
34     0xFFFFFFFF,
35     0xFFFFFFFF,
36     0xFFFFFFFF,
37     0xFFFFFFFF,
38     0xFFFFFFFF,
39     0xFFFFFFFF,
40     0xFFFFFFFF,
41     0xFFFFFFFF,
42     0xFFFFFFFF,
43     0xFFFFFFFF,
44     0xFFFFFFFF,
45     0xFFFFFFFF,
46     0xFFFFFFFF,
47     0xFFFFFFFF,
48     0xFFFFFFFF,
49     0xFFFFFFFF,
50     0xFFFFFFFF,
51     0xFFFFFFFF,
52     0xFFFFFFFF,
53     0xFFFFFFFF,
54     0xFFFFFFFF,
55     0xFFFFFFFF,
56     0xFFFFFFFF,
57     0xFFFFFFFF,
58     0xFFFFFFFF
59   },
60   // Aspect_HS_GRID_DIAGONAL
61   {
62     0xFFFFFFFF,
63     0xBBBBBBBB,
64     0xEEEEEEEE,
65     0xBBBBBBBB,
66     0xEEEEEEEE,
67     0xBBBBBBBB,
68     0xEEEEEEEE,
69     0xBBBBBBBB,
70     0xEEEEEEEE,
71     0xBBBBBBBB,
72     0xEEEEEEEE,
73     0xBBBBBBBB,
74     0xEEEEEEEE,
75     0xBBBBBBBB,
76     0xEEEEEEEE,
77     0xBBBBBBBB,
78     0xEEEEEEEE,
79     0xBBBBBBBB,
80     0xEEEEEEEE,
81     0xBBBBBBBB,
82     0xEEEEEEEE,
83     0xBBBBBBBB,
84     0xEEEEEEEE,
85     0xBBBBBBBB,
86     0xEEEEEEEE,
87     0xBBBBBBBB,
88     0xEEEEEEEE,
89     0xBBBBBBBB,
90     0xEEEEEEEE,
91     0xBBBBBBBB,
92     0xEEEEEEEE,
93     0xBBBBBBBB
94   },
95   // Aspect_HS_GRID_DIAGONAL_WIDE
96   {
97     0x81818181,
98     0x24242424,
99     0x18181818,
100     0x42424242,
101     0x81818181,
102     0x24242424,
103     0x18181818,
104     0x42424242,
105     0x81818181,
106     0x24242424,
107     0x18181818,
108     0x42424242,
109     0x81818181,
110     0x24242424,
111     0x18181818,
112     0x42424242,
113     0x81818181,
114     0x24242424,
115     0x18181818,
116     0x42424242,
117     0x81818181,
118     0x24242424,
119     0x18181818,
120     0x42424242,
121     0x81818181,
122     0x24242424,
123     0x18181818,
124     0x42424242,
125     0x81818181,
126     0x24242424,
127     0x18181818,
128     0x42424242
129   },
130   // Aspect_HS_GRID
131   {
132     0xFFFFFFFF,
133     0x88888888,
134     0xFFFFFFFF,
135     0x88888888,
136     0xFFFFFFFF,
137     0x88888888,
138     0xFFFFFFFF,
139     0x88888888,
140     0xFFFFFFFF,
141     0x88888888,
142     0xFFFFFFFF,
143     0x88888888,
144     0xFFFFFFFF,
145     0x88888888,
146     0xFFFFFFFF,
147     0x88888888,
148     0xFFFFFFFF,
149     0x88888888,
150     0xFFFFFFFF,
151     0x88888888,
152     0xFFFFFFFF,
153     0x88888888,
154     0xFFFFFFFF,
155     0x88888888,
156     0xFFFFFFFF,
157     0x88888888,
158     0xFFFFFFFF,
159     0x88888888,
160     0xFFFFFFFF,
161     0x88888888,
162     0xFFFFFFFF,
163     0x88888888
164   },
165   // Aspect_HS_GRID_WIDE
166   {
167     0xFFFFFFFF,
168     0x80808080,
169     0x80808080,
170     0x80808080,
171     0xFFFFFFFF,
172     0x80808080,
173     0x80808080,
174     0x80808080,
175     0xFFFFFFFF,
176     0x80808080,
177     0x80808080,
178     0x80808080,
179     0xFFFFFFFF,
180     0x80808080,
181     0x80808080,
182     0x80808080,
183     0xFFFFFFFF,
184     0x80808080,
185     0x80808080,
186     0x80808080,
187     0xFFFFFFFF,
188     0x80808080,
189     0x80808080,
190     0x80808080,
191     0xFFFFFFFF,
192     0x80808080,
193     0x80808080,
194     0x80808080,
195     0xFFFFFFFF,
196     0x80808080,
197     0x80808080,
198     0x80808080
199   },
200   // Aspect_HS_DIAGONAL_45
201   {
202     0x88888888,
203     0x22222222,
204     0x88888888,
205     0x22222222,
206     0x88888888,
207     0x22222222,
208     0x88888888,
209     0x22222222,
210     0x88888888,
211     0x22222222,
212     0x88888888,
213     0x22222222,
214     0x88888888,
215     0x22222222,
216     0x88888888,
217     0x22222222,
218     0x88888888,
219     0x22222222,
220     0x88888888,
221     0x22222222,
222     0x88888888,
223     0x22222222,
224     0x88888888,
225     0x22222222,
226     0x88888888,
227     0x22222222,
228     0x88888888,
229     0x22222222,
230     0x88888888,
231     0x22222222,
232     0x88888888,
233     0x22222222
234   },
235   // Aspect_HS_DIAGONAL_135
236   {
237     0x11111111,
238     0x44444444,
239     0x11111111,
240     0x44444444,
241     0x11111111,
242     0x44444444,
243     0x11111111,
244     0x44444444,
245     0x11111111,
246     0x44444444,
247     0x11111111,
248     0x44444444,
249     0x11111111,
250     0x44444444,
251     0x11111111,
252     0x44444444,
253     0x11111111,
254     0x44444444,
255     0x11111111,
256     0x44444444,
257     0x11111111,
258     0x44444444,
259     0x11111111,
260     0x44444444,
261     0x11111111,
262     0x44444444,
263     0x11111111,
264     0x44444444,
265     0x11111111,
266     0x44444444,
267     0x11111111,
268     0x44444444
269   },
270   // Aspect_HS_HORIZONTAL
271   {
272     0xFFFFFFFF,
273     0x00000000,
274     0xFFFFFFFF,
275     0x00000000,
276     0xFFFFFFFF,
277     0x00000000,
278     0xFFFFFFFF,
279     0x00000000,
280     0xFFFFFFFF,
281     0x00000000,
282     0xFFFFFFFF,
283     0x00000000,
284     0xFFFFFFFF,
285     0x00000000,
286     0xFFFFFFFF,
287     0x00000000,
288     0xFFFFFFFF,
289     0x00000000,
290     0xFFFFFFFF,
291     0x00000000,
292     0xFFFFFFFF,
293     0x00000000,
294     0xFFFFFFFF,
295     0x00000000,
296     0xFFFFFFFF,
297     0x00000000,
298     0xFFFFFFFF,
299     0x00000000,
300     0xFFFFFFFF,
301     0x00000000,
302     0xFFFFFFFF,
303     0x00000000
304   },
305   // Aspect_HS_VERTICAL
306   {
307     0x11111111,
308     0x11111111,
309     0x11111111,
310     0x11111111,
311     0x11111111,
312     0x11111111,
313     0x11111111,
314     0x11111111,
315     0x11111111,
316     0x11111111,
317     0x11111111,
318     0x11111111,
319     0x11111111,
320     0x11111111,
321     0x11111111,
322     0x11111111,
323     0x11111111,
324     0x11111111,
325     0x11111111,
326     0x11111111,
327     0x11111111,
328     0x11111111,
329     0x11111111,
330     0x11111111,
331     0x11111111,
332     0x11111111,
333     0x11111111,
334     0x11111111,
335     0x11111111,
336     0x11111111,
337     0x11111111,
338     0x11111111
339   },
340   // Aspect_HS_DIAGONAL_45_WIDE
341   {
342     0x80808080,
343     0x20202020,
344     0x08080808,
345     0x02020202,
346     0x80808080,
347     0x20202020,
348     0x08080808,
349     0x02020202,
350     0x80808080,
351     0x20202020,
352     0x08080808,
353     0x02020202,
354     0x80808080,
355     0x20202020,
356     0x08080808,
357     0x02020202,
358     0x80808080,
359     0x20202020,
360     0x08080808,
361     0x02020202,
362     0x80808080,
363     0x20202020,
364     0x08080808,
365     0x02020202,
366     0x80808080,
367     0x20202020,
368     0x08080808,
369     0x02020202,
370     0x80808080,
371     0x20202020,
372     0x08080808,
373     0x02020202
374   },
375   // Aspect_HS_DIAGONAL_135_WIDE
376   {
377     0x01010101,
378     0x04040404,
379     0x10101010,
380     0x40404040,
381     0x01010101,
382     0x04040404,
383     0x10101010,
384     0x40404040,
385     0x01010101,
386     0x04040404,
387     0x10101010,
388     0x40404040,
389     0x01010101,
390     0x04040404,
391     0x10101010,
392     0x40404040,
393     0x01010101,
394     0x04040404,
395     0x10101010,
396     0x40404040,
397     0x01010101,
398     0x04040404,
399     0x10101010,
400     0x40404040,
401     0x01010101,
402     0x04040404,
403     0x10101010,
404     0x40404040,
405     0x01010101,
406     0x04040404,
407     0x10101010,
408     0x40404040
409   },
410   // Aspect_HS_HORIZONTAL_WIDE
411   {
412     0xFFFFFFFF,
413     0x00000000,
414     0x00000000,
415     0x00000000,
416     0xFFFFFFFF,
417     0x00000000,
418     0x00000000,
419     0x00000000,
420     0xFFFFFFFF,
421     0x00000000,
422     0x00000000,
423     0x00000000,
424     0xFFFFFFFF,
425     0x00000000,
426     0x00000000,
427     0x00000000,
428     0xFFFFFFFF,
429     0x00000000,
430     0x00000000,
431     0x00000000,
432     0xFFFFFFFF,
433     0x00000000,
434     0x00000000,
435     0x00000000,
436     0xFFFFFFFF,
437     0x00000000,
438     0x00000000,
439     0x00000000,
440     0xFFFFFFFF,
441     0x00000000,
442     0x00000000,
443     0x00000000
444   },
445   // Aspect_HS_VERTICAL_WIDE
446   {
447     0x10101010,
448     0x10101010,
449     0x10101010,
450     0x10101010,
451     0x10101010,
452     0x10101010,
453     0x10101010,
454     0x10101010,
455     0x10101010,
456     0x10101010,
457     0x10101010,
458     0x10101010,
459     0x10101010,
460     0x10101010,
461     0x10101010,
462     0x10101010,
463     0x10101010,
464     0x10101010,
465     0x10101010,
466     0x10101010,
467     0x10101010,
468     0x10101010,
469     0x10101010,
470     0x10101010,
471     0x10101010,
472     0x10101010,
473     0x10101010,
474     0x10101010,
475     0x10101010,
476     0x10101010,
477     0x10101010,
478     0x10101010
479   }
480 };
481
482
483 // =======================================================================
484 // function : OpenGl_LineAttributes
485 // purpose  :
486 // =======================================================================
487 OpenGl_LineAttributes::OpenGl_LineAttributes()
488 : myPatternBase(0)
489 {
490   //
491 }
492
493 // =======================================================================
494 // function : ~OpenGl_LineAttributes
495 // purpose  :
496 // =======================================================================
497 OpenGl_LineAttributes::~OpenGl_LineAttributes()
498 {
499   Release (NULL);
500 }
501
502 // =======================================================================
503 // function : Release
504 // purpose  :
505 // =======================================================================
506 void OpenGl_LineAttributes::Release (OpenGl_Context* theGlCtx)
507 {
508   // Delete surface patterns
509   if (myPatternBase != 0)
510   {
511     if (theGlCtx->IsValid())
512     {
513     #if !defined(GL_ES_VERSION_2_0)
514       glDeleteLists ((GLuint )myPatternBase, TEL_HS_USER_DEF_START);
515     #endif
516     }
517     myPatternBase = 0;
518   }
519 }
520
521 // =======================================================================
522 // function : Init
523 // purpose  :
524 // =======================================================================
525 void OpenGl_LineAttributes::Init (const Handle(OpenGl_Context)& theGlCtx)
526 {
527   // Return if already initialized
528   if (myPatternBase != 0)
529   {
530     return;
531   }
532
533 #if !defined(GL_ES_VERSION_2_0)
534   if (theGlCtx->core11 == NULL)
535   {
536     return;
537   }
538
539   // GL_POLYGON_STIPPLE need 32x32 stipple patterns
540   const int nbi = sizeof(myInteriors) / (32 * sizeof(unsigned int));
541   myPatternBase = glGenLists(TEL_HS_USER_DEF_START);
542   for (int i = 1; i < TEL_HS_USER_DEF_START; i++)
543   {
544     glNewList ((GLuint )myPatternBase + i, GL_COMPILE);
545     glPolygonStipple ((const GLubyte* )myInteriors[i < nbi ? i : 0]);
546     glEndList();
547   }
548 #else
549   (void )theGlCtx;
550 #endif
551 }
552
553 // =======================================================================
554 // function : SetTypeOfHatch
555 // purpose  :
556 // =======================================================================
557 void OpenGl_LineAttributes::SetTypeOfHatch (const int theType) const
558 {
559 #if !defined(GL_ES_VERSION_2_0)
560   if (myPatternBase == 0)
561   {
562     return;
563   }
564
565   if (theType != 0)
566   {
567     glCallList ((GLuint )myPatternBase + (GLuint )theType);
568     glEnable (GL_POLYGON_STIPPLE);
569   }
570   else
571     glDisable (GL_POLYGON_STIPPLE);
572 #else
573   (void )theType;
574 #endif
575 }