0028487: Visualization, TKOpenGl - add option for rendering with lower resolution
[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 // =======================================================================
24 // function : OpenGl_LineAttributes
25 // purpose  :
26 // =======================================================================
27 OpenGl_LineAttributes::OpenGl_LineAttributes()
28 : myTypeOfHatch (0),
29   myIsEnabled (true)
30 {
31   //
32 }
33
34 // =======================================================================
35 // function : ~OpenGl_LineAttributes
36 // purpose  :
37 // =======================================================================
38 OpenGl_LineAttributes::~OpenGl_LineAttributes()
39 {
40   Release (NULL);
41 }
42
43 // =======================================================================
44 // function : Release
45 // purpose  :
46 // =======================================================================
47 void OpenGl_LineAttributes::Release (OpenGl_Context* theGlCtx)
48 {
49   // Delete surface patterns
50 #if !defined(GL_ES_VERSION_2_0)
51   if (theGlCtx != NULL && theGlCtx->IsValid())
52   {
53     for (OpenGl_MapOfHatchStylesAndIds::Iterator anIter (myStyles); anIter.More(); anIter.Next())
54     {
55       theGlCtx->core11->glDeleteLists ((GLuint)anIter.Value(), 1);
56     }
57   }
58 #else
59     (void )theGlCtx;
60 #endif
61     myStyles.Clear();
62 }
63
64 // =======================================================================
65 // function : init
66 // purpose  :
67 // =======================================================================
68 unsigned int OpenGl_LineAttributes::init (const OpenGl_Context* theGlCtx,
69                                           const Handle(Graphic3d_HatchStyle)& theStyle)
70 {
71 #if !defined(GL_ES_VERSION_2_0)
72   if (theGlCtx->core11 == NULL)
73   {
74     return 0;
75   }
76
77   const unsigned int aListId = glGenLists(1);
78   theGlCtx->core11->glNewList ((GLuint)aListId, GL_COMPILE);
79   theGlCtx->core11->glPolygonStipple ((const GLubyte*)theStyle->Pattern());
80   theGlCtx->core11->glEndList();
81   return aListId;
82 #else
83   (void )theGlCtx;
84   (void )theStyle;
85   return 0;
86 #endif
87 }
88
89 // =======================================================================
90 // function : SetTypeOfHatch
91 // purpose  :
92 // =======================================================================
93 int OpenGl_LineAttributes::SetTypeOfHatch (const OpenGl_Context*               theGlCtx,
94                                            const Handle(Graphic3d_HatchStyle)& theStyle)
95 {
96   // Return if not initialized
97   if (theStyle.IsNull())
98   {
99     return 0;
100   }
101
102   const int anOldType = myTypeOfHatch;
103
104   unsigned int aGpuListId = 0;
105   if (!myStyles.Find (theStyle, aGpuListId))
106   {
107     aGpuListId = init (theGlCtx, theStyle);
108     myStyles.Bind (theStyle, aGpuListId);
109   }
110
111 #if !defined(GL_ES_VERSION_2_0)
112   if (theStyle->HatchType() != 0)
113   {
114     theGlCtx->core11->glCallList ((GLuint)aGpuListId);
115
116     if (myIsEnabled)
117     {
118       theGlCtx->core11fwd->glEnable (GL_POLYGON_STIPPLE);
119     }
120   }
121   else
122   {
123     theGlCtx->core11fwd->glDisable (GL_POLYGON_STIPPLE);
124   }
125 #else
126   (void )theGlCtx;
127 #endif
128   myTypeOfHatch = theStyle->HatchType();
129
130   return anOldType;
131 }
132
133 // =======================================================================
134 // function : SetEnabled
135 // purpose  :
136 // =======================================================================
137 bool OpenGl_LineAttributes::SetEnabled (const OpenGl_Context* theGlCtx,
138                                         const bool theToEnable)
139 {
140   // Return if not initialized
141   if (myStyles.IsEmpty())
142   {
143     return false;
144   }
145
146   const bool anOldIsEnabled = myIsEnabled;
147
148 #if !defined(GL_ES_VERSION_2_0)
149   if (theToEnable)
150   {
151     if (myTypeOfHatch != 0)
152     {
153       theGlCtx->core11fwd->glEnable (GL_POLYGON_STIPPLE);
154     }
155   }
156   else
157   {
158     theGlCtx->core11fwd->glDisable (GL_POLYGON_STIPPLE);
159   }
160 #else
161   (void )theGlCtx;
162 #endif
163   myIsEnabled = theToEnable;
164
165   return anOldIsEnabled;
166 }