0025305: Visualization, TKOpenGl - support stipple line aspects within built-in GLSL...
[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 static const unsigned int myInteriors[TEL_HS_USER_DEF_START][32] =
22 {
23   //TEL_HS_SOLID
24   {
25     0xFFFFFFFF,
26     0xFFFFFFFF,
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   },
58   //TEL_HS_CROSS
59   {
60     0xFFFFFFFF,
61     0xBBBBBBBB,
62     0xEEEEEEEE,
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   },
93   //TEL_HS_CROSS_SPARSE
94   {
95     0x81818181,
96     0x24242424,
97     0x18181818,
98     0x42424242,
99     0x81818181,
100     0x24242424,
101     0x18181818,
102     0x42424242,
103     0x81818181,
104     0x24242424,
105     0x18181818,
106     0x42424242,
107     0x81818181,
108     0x24242424,
109     0x18181818,
110     0x42424242,
111     0x81818181,
112     0x24242424,
113     0x18181818,
114     0x42424242,
115     0x81818181,
116     0x24242424,
117     0x18181818,
118     0x42424242,
119     0x81818181,
120     0x24242424,
121     0x18181818,
122     0x42424242,
123     0x81818181,
124     0x24242424,
125     0x18181818,
126     0x42424242
127   },
128   //TEL_HS_GRID
129   {
130     0xFFFFFFFF,
131     0x88888888,
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   },
163   //TEL_HS_GRID_SPARSE
164   {
165     0xFFFFFFFF,
166     0x80808080,
167     0x80808080,
168     0x80808080,
169     0xFFFFFFFF,
170     0x80808080,
171     0x80808080,
172     0x80808080,
173     0xFFFFFFFF,
174     0x80808080,
175     0x80808080,
176     0x80808080,
177     0xFFFFFFFF,
178     0x80808080,
179     0x80808080,
180     0x80808080,
181     0xFFFFFFFF,
182     0x80808080,
183     0x80808080,
184     0x80808080,
185     0xFFFFFFFF,
186     0x80808080,
187     0x80808080,
188     0x80808080,
189     0xFFFFFFFF,
190     0x80808080,
191     0x80808080,
192     0x80808080,
193     0xFFFFFFFF,
194     0x80808080,
195     0x80808080,
196     0x80808080
197   },
198   //TEL_HS_DIAG_45
199   {
200     0x88888888,
201     0x22222222,
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   },
233   //TEL_HS_DIAG_135
234   {
235     0x11111111,
236     0x44444444,
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   },
268   //TEL_HS_HORIZONTAL
269   {
270     0xFFFFFFFF,
271     0x00000000,
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   },
303   //TEL_HS_VERTICAL
304   {
305     0x11111111,
306     0x11111111,
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   },
338   //TEL_HS_DIAG_45_SPARSE
339   {
340     0x80808080,
341     0x20202020,
342     0x08080808,
343     0x02020202,
344     0x80808080,
345     0x20202020,
346     0x08080808,
347     0x02020202,
348     0x80808080,
349     0x20202020,
350     0x08080808,
351     0x02020202,
352     0x80808080,
353     0x20202020,
354     0x08080808,
355     0x02020202,
356     0x80808080,
357     0x20202020,
358     0x08080808,
359     0x02020202,
360     0x80808080,
361     0x20202020,
362     0x08080808,
363     0x02020202,
364     0x80808080,
365     0x20202020,
366     0x08080808,
367     0x02020202,
368     0x80808080,
369     0x20202020,
370     0x08080808,
371     0x02020202
372   },
373   //TEL_HS_DIAG_135_SPARSE
374   {
375     0x01010101,
376     0x04040404,
377     0x10101010,
378     0x40404040,
379     0x01010101,
380     0x04040404,
381     0x10101010,
382     0x40404040,
383     0x01010101,
384     0x04040404,
385     0x10101010,
386     0x40404040,
387     0x01010101,
388     0x04040404,
389     0x10101010,
390     0x40404040,
391     0x01010101,
392     0x04040404,
393     0x10101010,
394     0x40404040,
395     0x01010101,
396     0x04040404,
397     0x10101010,
398     0x40404040,
399     0x01010101,
400     0x04040404,
401     0x10101010,
402     0x40404040,
403     0x01010101,
404     0x04040404,
405     0x10101010,
406     0x40404040
407   },
408   //TEL_HS_HORIZONTAL_SPARSE
409   {
410     0xFFFFFFFF,
411     0x00000000,
412     0x00000000,
413     0x00000000,
414     0xFFFFFFFF,
415     0x00000000,
416     0x00000000,
417     0x00000000,
418     0xFFFFFFFF,
419     0x00000000,
420     0x00000000,
421     0x00000000,
422     0xFFFFFFFF,
423     0x00000000,
424     0x00000000,
425     0x00000000,
426     0xFFFFFFFF,
427     0x00000000,
428     0x00000000,
429     0x00000000,
430     0xFFFFFFFF,
431     0x00000000,
432     0x00000000,
433     0x00000000,
434     0xFFFFFFFF,
435     0x00000000,
436     0x00000000,
437     0x00000000,
438     0xFFFFFFFF,
439     0x00000000,
440     0x00000000,
441     0x00000000
442   },
443   //TEL_HS_VERTICAL_SPARSE
444   {
445     0x10101010,
446     0x10101010,
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   }
478 };
479
480 IMPLEMENT_STANDARD_HANDLE (OpenGl_LineAttributes, OpenGl_Resource)
481 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_LineAttributes, OpenGl_Resource)
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 #endif
549 }
550
551 // =======================================================================
552 // function : SetTypeOfHatch
553 // purpose  :
554 // =======================================================================
555 void OpenGl_LineAttributes::SetTypeOfHatch (const int theType) const
556 {
557 #if !defined(GL_ES_VERSION_2_0)
558   if (theType != 0)
559   {
560     glCallList ((GLuint )myPatternBase + (GLuint )theType);
561     glEnable (GL_POLYGON_STIPPLE);
562   }
563   else
564     glDisable (GL_POLYGON_STIPPLE);
565 #endif
566 }