0023722: stack overflow during reading IGES in Test Harness
[occt.git] / src / OpenGl / OpenGl_Workspace.cxx
CommitLineData
b311480e 1// Created on: 2011-09-20
2// Created by: Sergey ZERCHANINOV
3// Copyright (c) 2011-2012 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
bf75be98 20#include <OpenGl_GlCore12.hxx>
5f8b738e 21
2166f0fa
SK
22#include <InterfaceGraphic.hxx>
23
24#include <OpenGl_Workspace.hxx>
25
26#include <OpenGl_AspectLine.hxx>
27#include <OpenGl_AspectFace.hxx>
28#include <OpenGl_AspectMarker.hxx>
29#include <OpenGl_AspectText.hxx>
bf75be98 30#include <OpenGl_Context.hxx>
31
32#include <OpenGl_Texture.hxx>
2166f0fa 33
bf75be98 34#include <Graphic3d_TextureParams.hxx>
2166f0fa
SK
35
36IMPLEMENT_STANDARD_HANDLE(OpenGl_Workspace,OpenGl_Window)
37IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,OpenGl_Window)
38
39namespace
40{
41 static const TEL_COLOUR myDefaultHighlightColor = { { 1.F, 1.F, 1.F, 1.F } };
42
43 static const OpenGl_AspectLine myDefaultAspectLine;
44 static const OpenGl_AspectFace myDefaultAspectFace;
45 static const OpenGl_AspectMarker myDefaultAspectMarker;
46 static const OpenGl_AspectText myDefaultAspectText;
47
48 static const OpenGl_TextParam myDefaultTextParam =
49 {
50 16, Graphic3d_HTA_LEFT, Graphic3d_VTA_BOTTOM
51 };
52
53 static const OpenGl_Matrix myDefaultMatrix =
54 {
55 {{ 1.0F, 0.0F, 0.0F, 0.0F },
56 { 0.0F, 1.0F, 0.0F, 0.0F },
57 { 0.0F, 0.0F, 1.0F, 0.0F },
58 { 0.0F, 0.0F, 0.0F, 1.0F }}
59 };
bf75be98 60
2166f0fa
SK
61};
62
63// =======================================================================
64// function : OpenGl_Workspace
65// purpose :
66// =======================================================================
67OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
68 const CALL_DEF_WINDOW& theCWindow,
5e27df78 69 Aspect_RenderingContext theGContext,
70 const Handle(OpenGl_Context)& theShareCtx)
71: OpenGl_Window (theDisplay, theCWindow, theGContext, theShareCtx),
2166f0fa
SK
72 myTransientList (0),
73 myIsTransientOpen (Standard_False),
1981cb22 74 myTransientDrawToFront (Standard_True),
2166f0fa
SK
75 myRetainMode (Standard_False),
76 myUseTransparency (Standard_False),
77 myUseZBuffer (Standard_False),
78 myUseDepthTest (Standard_True),
79 myUseGLLight (Standard_True),
80 myBackBufferRestored (Standard_False),
81 //
82 NamedStatus (0),
83 DegenerateModel (0),
84 SkipRatio (0.F),
85 HighlightColor (&myDefaultHighlightColor),
86 AspectLine_set (&myDefaultAspectLine),
87 AspectLine_applied (NULL),
88 AspectFace_set (&myDefaultAspectFace),
89 AspectFace_applied (NULL),
90 AspectMarker_set (&myDefaultAspectMarker),
91 AspectMarker_applied (NULL),
92 AspectText_set (&myDefaultAspectText),
93 AspectText_applied (NULL),
94 TextParam_set (&myDefaultTextParam),
95 TextParam_applied (NULL),
96 ViewMatrix_applied (&myDefaultMatrix),
97 StructureMatrix_applied (&myDefaultMatrix),
98 PolygonOffset_applied (NULL)
99{
100 theDisplay->InitAttributes();
101
102 // General initialization of the context
103
104 // Eviter d'avoir les faces mal orientees en noir.
105 // Pourrait etre utiliser pour detecter les problemes d'orientation
bf75be98 106 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
2166f0fa
SK
107
108 // Optimisation pour le Fog et l'antialiasing
109 glHint (GL_FOG_HINT, GL_FASTEST);
110 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
111 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
112 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
113
114 // Polygon Offset
115 EnablePolygonOffset();
116}
117
118// =======================================================================
1981cb22 119// function : SetImmediateModeDrawToFront
120// purpose :
121// =======================================================================
122Standard_Boolean OpenGl_Workspace::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
123{
124 const Standard_Boolean aPrevMode = myTransientDrawToFront;
125 myTransientDrawToFront = theDrawToFrontBuffer;
126 return aPrevMode;
127}
128
129// =======================================================================
2166f0fa
SK
130// function : ~OpenGl_Workspace
131// purpose :
132// =======================================================================
133OpenGl_Workspace::~OpenGl_Workspace()
134{
135}
136
137// =======================================================================
138// function : Activate
139// purpose :
140// =======================================================================
141Standard_Boolean OpenGl_Workspace::Activate()
142{
143 if (!OpenGl_Window::Activate())
144 return Standard_False;
145
2166f0fa
SK
146 DegenerateModel = 0;
147 SkipRatio = 0.0f;
2166f0fa
SK
148 ViewMatrix_applied = &myDefaultMatrix;
149 StructureMatrix_applied = &myDefaultMatrix;
26395493 150
151 ResetAppliedAspect();
152
153 return Standard_True;
2166f0fa
SK
154}
155
156// =======================================================================
157// function : UseTransparency
158// purpose : call_togl_transparency
159// =======================================================================
160void OpenGl_Workspace::UseTransparency (const Standard_Boolean theFlag)
161{
162 if ((myUseTransparency ? 1 : 0) != (theFlag ? 1 : 0))
163 {
164 myUseTransparency = theFlag;
165 EraseAnimation();
2166f0fa
SK
166 }
167}
26395493 168
169//=======================================================================
170//function : ResetAppliedAspect
171//purpose : Sets default values of GL parameters in accordance with default aspects
172//=======================================================================
173void OpenGl_Workspace::ResetAppliedAspect()
174{
bf75be98 175 NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
26395493 176 HighlightColor = &myDefaultHighlightColor;
177 AspectLine_set = &myDefaultAspectLine;
178 AspectLine_applied = NULL;
179 AspectFace_set = &myDefaultAspectFace;
180 AspectFace_applied = NULL;
181 AspectMarker_set = &myDefaultAspectMarker;
182 AspectMarker_applied = NULL;
183 AspectText_set = &myDefaultAspectText;
184 AspectText_applied = NULL;
185 TextParam_set = &myDefaultTextParam;
186 TextParam_applied = NULL;
187 PolygonOffset_applied = NULL;
188
189 AspectLine(Standard_True);
190 AspectFace(Standard_True);
191 AspectMarker(Standard_True);
192 AspectText(Standard_True);
193}
bf75be98 194
195// =======================================================================
196// function : DisableTexture
197// purpose :
198// =======================================================================
199Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
200{
201 if (myTextureBound.IsNull())
202 {
203 return myTextureBound;
204 }
205
206 // reset texture matrix because some code may expect it is identity
207 GLint aMatrixMode = GL_TEXTURE;
208 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
209 glMatrixMode (GL_TEXTURE);
210 glLoadIdentity();
211 glMatrixMode (aMatrixMode);
212
213 myTextureBound->Unbind (myGlContext);
214 switch (myTextureBound->GetTarget())
215 {
216 case GL_TEXTURE_1D:
217 {
218 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
219 {
220 glDisable (GL_TEXTURE_GEN_S);
221 }
222 glDisable (GL_TEXTURE_1D);
223 break;
224 }
225 case GL_TEXTURE_2D:
226 {
227 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
228 {
229 glDisable (GL_TEXTURE_GEN_S);
230 glDisable (GL_TEXTURE_GEN_T);
231 }
232 glDisable (GL_TEXTURE_2D);
233 break;
234 }
235 default: break;
236 }
237
238 Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
239 myTextureBound.Nullify();
240 return aPrevTexture;
241}
242
243// =======================================================================
244// function : setTextureParams
245// purpose :
246// =======================================================================
247void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture,
248 const Handle(Graphic3d_TextureParams)& theParams)
249{
250 const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
251 if (aParams.IsNull())
252 {
253 return;
254 }
255
256 GLint aMatrixMode = GL_TEXTURE;
257 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
258
259 // setup texture matrix
260 glMatrixMode (GL_TEXTURE);
261 glLoadIdentity();
262 const Graphic3d_Vec2& aScale = aParams->Scale();
263 const Graphic3d_Vec2& aTrans = aParams->Translation();
264 glScalef ( aScale.x(), aScale.y(), 1.0f);
265 glTranslatef (-aTrans.x(), -aTrans.y(), 0.0f);
266 glRotatef (-aParams->Rotation(), 0.0f, 0.0f, 1.0f);
267
268 // setup generation of texture coordinates
269 switch (aParams->GenMode())
270 {
271 case Graphic3d_TOTM_OBJECT:
272 {
273 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
274 glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData());
275 if (theTexture->GetTarget() != GL_TEXTURE_1D)
276 {
277 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
278 glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData());
279 }
280 break;
281 }
282 case Graphic3d_TOTM_SPHERE:
283 {
284 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
285 if (theTexture->GetTarget() != GL_TEXTURE_1D)
286 {
287 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
288 }
289 break;
290 }
291 case Graphic3d_TOTM_EYE:
292 {
293 glMatrixMode (GL_MODELVIEW);
294 glPushMatrix();
295 glLoadIdentity();
296
297 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
298 glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData());
299
300 if (theTexture->GetTarget() != GL_TEXTURE_1D)
301 {
302 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
303 glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData());
304 }
305 glPopMatrix();
306 break;
307 }
308 case Graphic3d_TOTM_MANUAL:
309 default: break;
310 }
311
312 // setup lighting
313 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aParams->IsModulate() ? GL_MODULATE : GL_DECAL);
314
315 // setup texture filtering and wrapping
316 //if (theTexture->GetParams() != theParams)
317 const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
318 const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : GL_CLAMP;
319 switch (theTexture->GetTarget())
320 {
321 case GL_TEXTURE_1D:
322 {
323 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
324 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
325 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode);
326 break;
327 }
328 case GL_TEXTURE_2D:
329 {
330 GLenum aFilterMin = aFilter;
331 if (theTexture->HasMipmaps())
332 {
333 aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
334 if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
335 {
336 aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
337 }
338 else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
339 {
340 aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
341 }
342
343 if (myGlContext->extAnis)
344 {
345 // setup degree of anisotropy filter
346 const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
347 switch (aParams->AnisoFilter())
348 {
349 case Graphic3d_LOTA_QUALITY:
350 {
351 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aMaxDegree);
352 break;
353 }
354 case Graphic3d_LOTA_MIDDLE:
355 {
356
357 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2));
358 break;
359 }
360 case Graphic3d_LOTA_FAST:
361 {
362 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2);
363 break;
364 }
365 case Graphic3d_LOTA_OFF:
366 default:
367 {
368 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
369 break;
370 }
371 }
372 }
373 }
374 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin);
375 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter);
376 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, aWrapMode);
377 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, aWrapMode);
378 break;
379 }
380 default: break;
381 }
382
383 switch (theTexture->GetTarget())
384 {
385 case GL_TEXTURE_1D:
386 {
387 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
388 {
389 glEnable (GL_TEXTURE_GEN_S);
390 }
391 glEnable (GL_TEXTURE_1D);
392 break;
393 }
394 case GL_TEXTURE_2D:
395 {
396 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
397 {
398 glEnable (GL_TEXTURE_GEN_S);
399 glEnable (GL_TEXTURE_GEN_T);
400 }
401 glEnable (GL_TEXTURE_2D);
402 break;
403 }
404 default: break;
405 }
406
407 glMatrixMode (aMatrixMode); // turn back active matrix
408 theTexture->SetParams (aParams);
409}
410
411// =======================================================================
412// function : EnableTexture
413// purpose :
414// =======================================================================
415Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture,
416 const Handle(Graphic3d_TextureParams)& theParams)
417{
418 if (theTexture.IsNull() || !theTexture->IsValid())
419 {
420 return DisableTexture();
421 }
422
bca1d6e2 423 if (myTextureBound == theTexture
424 && (theParams.IsNull() || theParams == theTexture->GetParams()))
bf75be98 425 {
bca1d6e2 426 // already bound
427 return myTextureBound;
bf75be98 428 }
429
430 Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
431 myTextureBound = theTexture;
432 myTextureBound->Bind (myGlContext);
433 setTextureParams (myTextureBound, theParams);
434
435 return aPrevTexture;
436}