0027700: Visualization - glPolygonMode() used for frame drawing affects label text...
[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 // function : OpenGl_LineAttributes
484 // purpose  :
485 // =======================================================================
486 OpenGl_LineAttributes::OpenGl_LineAttributes()
487 : myPatternBase (0),
488   myTypeOfHatch (0),
489   myIsEnabled (true)
490 {
491   //
492 }
493
494 // =======================================================================
495 // function : ~OpenGl_LineAttributes
496 // purpose  :
497 // =======================================================================
498 OpenGl_LineAttributes::~OpenGl_LineAttributes()
499 {
500   Release (NULL);
501 }
502
503 // =======================================================================
504 // function : Release
505 // purpose  :
506 // =======================================================================
507 void OpenGl_LineAttributes::Release (OpenGl_Context* theGlCtx)
508 {
509   // Delete surface patterns
510   if (myPatternBase != 0)
511   {
512 #if !defined(GL_ES_VERSION_2_0)
513     if (theGlCtx->IsValid())
514     {
515       theGlCtx->core11->glDeleteLists ((GLuint )myPatternBase, TEL_HS_USER_DEF_START);
516     }
517 #else
518     (void )theGlCtx;
519 #endif
520     myPatternBase = 0;
521   }
522 }
523
524 // =======================================================================
525 // function : Init
526 // purpose  :
527 // =======================================================================
528 void OpenGl_LineAttributes::Init (const OpenGl_Context* theGlCtx)
529 {
530   // Return if already initialized
531   if (myPatternBase != 0)
532   {
533     return;
534   }
535
536 #if !defined(GL_ES_VERSION_2_0)
537   if (theGlCtx->core11 == NULL)
538   {
539     return;
540   }
541
542   // GL_POLYGON_STIPPLE need 32x32 stipple patterns
543   const int nbi = sizeof(myInteriors) / (32 * sizeof(unsigned int));
544   myPatternBase = glGenLists(TEL_HS_USER_DEF_START);
545   for (int i = 1; i < TEL_HS_USER_DEF_START; i++)
546   {
547     theGlCtx->core11->glNewList ((GLuint )myPatternBase + i, GL_COMPILE);
548     theGlCtx->core11->glPolygonStipple ((const GLubyte* )myInteriors[i < nbi ? i : 0]);
549     theGlCtx->core11->glEndList();
550   }
551 #else
552   (void )theGlCtx;
553 #endif
554 }
555
556 // =======================================================================
557 // function : SetTypeOfHatch
558 // purpose  :
559 // =======================================================================
560 int OpenGl_LineAttributes::SetTypeOfHatch (const OpenGl_Context* theGlCtx, const int theType)
561 {
562   // Return if not initialized
563   if (myPatternBase == 0)
564   {
565     return 0;
566   }
567
568   const int anOldType = myTypeOfHatch;
569
570 #if !defined(GL_ES_VERSION_2_0)
571   if (theType != 0)
572   {
573     theGlCtx->core11->glCallList ((GLuint )myPatternBase + (GLuint )theType);
574
575     if (myIsEnabled)
576     {
577       theGlCtx->core11fwd->glEnable (GL_POLYGON_STIPPLE);
578     }
579   }
580   else
581   {
582     theGlCtx->core11fwd->glDisable (GL_POLYGON_STIPPLE);
583   }
584 #else
585   (void )theGlCtx;
586 #endif
587   myTypeOfHatch = theType;
588
589   return anOldType;
590 }
591
592 // =======================================================================
593 // function : SetEnabled
594 // purpose  :
595 // =======================================================================
596 bool OpenGl_LineAttributes::SetEnabled (const OpenGl_Context* theGlCtx,
597                                         const bool theToEnable)
598 {
599   // Return if not initialized
600   if (myPatternBase == 0)
601   {
602     return false;
603   }
604
605   const bool anOldIsEnabled = myIsEnabled;
606
607 #if !defined(GL_ES_VERSION_2_0)
608   if (theToEnable)
609   {
610     if (myTypeOfHatch != 0)
611     {
612       theGlCtx->core11fwd->glEnable (GL_POLYGON_STIPPLE);
613     }
614   }
615   else
616   {
617     theGlCtx->core11fwd->glDisable (GL_POLYGON_STIPPLE);
618   }
619 #else
620   (void )theGlCtx;
621 #endif
622   myIsEnabled = theToEnable;
623
624   return anOldIsEnabled;
625 }