0031279: Visualization, TKOpenGl - environment background is misplaced within Ray...
[occt.git] / src / OpenGl / OpenGl_LineAttributes.cxx
CommitLineData
b311480e 1// Created on: 2011-10-25
2// Created by: Sergey ZERCHANINOV
973c2be1 3// Copyright (c) 2011-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
2166f0fa 15
5f8b738e 16#include <OpenGl_GlCore11.hxx>
17
73192b37 18#include <OpenGl_LineAttributes.hxx>
19#include <OpenGl_Context.hxx>
2166f0fa 20
92efcf78 21IMPLEMENT_STANDARD_RTTIEXT(OpenGl_LineAttributes,OpenGl_Resource)
22
73192b37 23// =======================================================================
24// function : OpenGl_LineAttributes
25// purpose :
26// =======================================================================
27OpenGl_LineAttributes::OpenGl_LineAttributes()
640d5fe2 28: myTypeOfHatch (0),
6d0e6be5 29 myIsEnabled (true)
2166f0fa 30{
73192b37 31 //
32}
33
34// =======================================================================
35// function : ~OpenGl_LineAttributes
36// purpose :
37// =======================================================================
38OpenGl_LineAttributes::~OpenGl_LineAttributes()
39{
40 Release (NULL);
41}
42
43// =======================================================================
44// function : Release
45// purpose :
46// =======================================================================
10b9c7df 47void OpenGl_LineAttributes::Release (OpenGl_Context* theGlCtx)
73192b37 48{
73192b37 49 // Delete surface patterns
6d0e6be5 50#if !defined(GL_ES_VERSION_2_0)
640d5fe2 51 if (theGlCtx != NULL && theGlCtx->IsValid())
52 {
53 for (OpenGl_MapOfHatchStylesAndIds::Iterator anIter (myStyles); anIter.More(); anIter.Next())
73192b37 54 {
640d5fe2 55 theGlCtx->core11->glDeleteLists ((GLuint)anIter.Value(), 1);
73192b37 56 }
640d5fe2 57 }
6d0e6be5 58#else
59 (void )theGlCtx;
60#endif
640d5fe2 61 myStyles.Clear();
73192b37 62}
2166f0fa 63
73192b37 64// =======================================================================
640d5fe2 65// function : init
73192b37 66// purpose :
67// =======================================================================
640d5fe2 68unsigned int OpenGl_LineAttributes::init (const OpenGl_Context* theGlCtx,
69 const Handle(Graphic3d_HatchStyle)& theStyle)
73192b37 70{
ca3c13d1 71#if !defined(GL_ES_VERSION_2_0)
4e1523ef 72 if (theGlCtx->core11 == NULL)
73 {
640d5fe2 74 return 0;
4e1523ef 75 }
76
640d5fe2 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;
20aeeb7b 82#else
83 (void )theGlCtx;
640d5fe2 84 (void )theStyle;
85 return 0;
ca3c13d1 86#endif
2166f0fa
SK
87}
88
73192b37 89// =======================================================================
90// function : SetTypeOfHatch
91// purpose :
92// =======================================================================
640d5fe2 93int OpenGl_LineAttributes::SetTypeOfHatch (const OpenGl_Context* theGlCtx,
94 const Handle(Graphic3d_HatchStyle)& theStyle)
2166f0fa 95{
6d0e6be5 96 // Return if not initialized
640d5fe2 97 if (theStyle.IsNull())
79f4f036 98 {
6d0e6be5 99 return 0;
79f4f036 100 }
101
6d0e6be5 102 const int anOldType = myTypeOfHatch;
103
640d5fe2 104 unsigned int aGpuListId = 0;
105 if (!myStyles.Find (theStyle, aGpuListId))
106 {
107 aGpuListId = init (theGlCtx, theStyle);
108 myStyles.Bind (theStyle, aGpuListId);
109 }
110
6d0e6be5 111#if !defined(GL_ES_VERSION_2_0)
2a332745 112 if (theGlCtx->core11 == NULL)
113 {
114 return 0;
115 }
116 else if (theStyle->HatchType() != 0)
2166f0fa 117 {
640d5fe2 118 theGlCtx->core11->glCallList ((GLuint)aGpuListId);
6d0e6be5 119
120 if (myIsEnabled)
121 {
122 theGlCtx->core11fwd->glEnable (GL_POLYGON_STIPPLE);
123 }
124 }
125 else
126 {
127 theGlCtx->core11fwd->glDisable (GL_POLYGON_STIPPLE);
128 }
129#else
130 (void )theGlCtx;
131#endif
640d5fe2 132 myTypeOfHatch = theStyle->HatchType();
6d0e6be5 133
134 return anOldType;
135}
136
137// =======================================================================
138// function : SetEnabled
139// purpose :
140// =======================================================================
141bool OpenGl_LineAttributes::SetEnabled (const OpenGl_Context* theGlCtx,
142 const bool theToEnable)
143{
144 // Return if not initialized
640d5fe2 145 if (myStyles.IsEmpty())
6d0e6be5 146 {
147 return false;
148 }
149
150 const bool anOldIsEnabled = myIsEnabled;
151
152#if !defined(GL_ES_VERSION_2_0)
2a332745 153 if (theGlCtx->core11 == NULL)
154 {
155 return 0;
156 }
157 else if (theToEnable)
6d0e6be5 158 {
159 if (myTypeOfHatch != 0)
160 {
161 theGlCtx->core11fwd->glEnable (GL_POLYGON_STIPPLE);
162 }
2166f0fa
SK
163 }
164 else
6d0e6be5 165 {
166 theGlCtx->core11fwd->glDisable (GL_POLYGON_STIPPLE);
167 }
20aeeb7b 168#else
6d0e6be5 169 (void )theGlCtx;
ca3c13d1 170#endif
6d0e6be5 171 myIsEnabled = theToEnable;
172
173 return anOldIsEnabled;
2166f0fa 174}