0025701: Problem with the symmetry of fillet on two perpendicular cylinders
[occt.git] / src / OpenGl / OpenGl_Workspace.cxx
CommitLineData
b311480e 1// Created on: 2011-09-20
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.
b311480e 15
a577aaab 16#include <OpenGl_GlCore15.hxx>
5f8b738e 17
2166f0fa
SK
18#include <InterfaceGraphic.hxx>
19
2166f0fa
SK
20#include <OpenGl_AspectLine.hxx>
21#include <OpenGl_AspectFace.hxx>
22#include <OpenGl_AspectMarker.hxx>
23#include <OpenGl_AspectText.hxx>
bf75be98 24#include <OpenGl_Context.hxx>
679ecdee 25#include <OpenGl_Element.hxx>
a174a3c5 26#include <OpenGl_FrameBuffer.hxx>
679ecdee 27#include <OpenGl_Structure.hxx>
25ef750e 28#include <OpenGl_Sampler.hxx>
bf75be98 29#include <OpenGl_Texture.hxx>
c827ea3a 30#include <OpenGl_Utils.hxx>
e276548b 31#include <OpenGl_View.hxx>
a174a3c5 32#include <OpenGl_Workspace.hxx>
2166f0fa 33
bf75be98 34#include <Graphic3d_TextureParams.hxx>
2166f0fa 35
58655684 36#if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
a174a3c5 37 #include <OpenGl_AVIWriter.hxx>
38#endif
39
2166f0fa
SK
40IMPLEMENT_STANDARD_HANDLE(OpenGl_Workspace,OpenGl_Window)
41IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,OpenGl_Window)
42
43namespace
44{
0adbd30f 45 static const TEL_COLOUR THE_WHITE_COLOR = { { 1.0f, 1.0f, 1.0f, 1.0f } };
46 static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f);
2166f0fa
SK
47
48 static const OpenGl_AspectLine myDefaultAspectLine;
49 static const OpenGl_AspectFace myDefaultAspectFace;
50 static const OpenGl_AspectMarker myDefaultAspectMarker;
51 static const OpenGl_AspectText myDefaultAspectText;
52
53 static const OpenGl_TextParam myDefaultTextParam =
54 {
55 16, Graphic3d_HTA_LEFT, Graphic3d_VTA_BOTTOM
56 };
57
58 static const OpenGl_Matrix myDefaultMatrix =
59 {
60 {{ 1.0F, 0.0F, 0.0F, 0.0F },
61 { 0.0F, 1.0F, 0.0F, 0.0F },
62 { 0.0F, 0.0F, 1.0F, 0.0F },
63 { 0.0F, 0.0F, 0.0F, 1.0F }}
64 };
bf75be98 65
2166f0fa
SK
66};
67
0adbd30f 68// =======================================================================
69// function : Init
70// purpose :
71// =======================================================================
72void OpenGl_Material::Init (const OPENGL_SURF_PROP& theProp)
73{
74 // ambient component
75 if (theProp.color_mask & OPENGL_AMBIENT_MASK)
76 {
77 const float* aSrcAmb = theProp.isphysic ? theProp.ambcol.rgb : theProp.matcol.rgb;
78 Ambient = OpenGl_Vec4 (aSrcAmb[0] * theProp.amb,
79 aSrcAmb[1] * theProp.amb,
80 aSrcAmb[2] * theProp.amb,
81 1.0f);
82 }
83 else
84 {
85 Ambient = THE_BLACK_COLOR;
86 }
87
88 // diffusion component
89 if (theProp.color_mask & OPENGL_DIFFUSE_MASK)
90 {
91 const float* aSrcDif = theProp.isphysic ? theProp.difcol.rgb : theProp.matcol.rgb;
92 Diffuse = OpenGl_Vec4 (aSrcDif[0] * theProp.diff,
93 aSrcDif[1] * theProp.diff,
94 aSrcDif[2] * theProp.diff,
95 1.0f);
96 }
97 else
98 {
99 Diffuse = THE_BLACK_COLOR;
100 }
101
102 // specular component
103 if (theProp.color_mask & OPENGL_SPECULAR_MASK)
104 {
105 const float* aSrcSpe = theProp.isphysic ? theProp.speccol.rgb : THE_WHITE_COLOR.rgb;
106 Specular = OpenGl_Vec4 (aSrcSpe[0] * theProp.spec,
107 aSrcSpe[1] * theProp.spec,
108 aSrcSpe[2] * theProp.spec,
109 1.0f);
110 }
111 else
112 {
113 Specular = THE_BLACK_COLOR;
114 }
115
116 // emission component
117 if (theProp.color_mask & OPENGL_EMISSIVE_MASK)
118 {
119 const float* aSrcEms = theProp.isphysic ? theProp.emscol.rgb : theProp.matcol.rgb;
120 Emission = OpenGl_Vec4 (aSrcEms[0] * theProp.emsv,
121 aSrcEms[1] * theProp.emsv,
122 aSrcEms[2] * theProp.emsv,
123 1.0f);
124 }
125 else
126 {
127 Emission = THE_BLACK_COLOR;
128 }
129
130 ChangeShine() = theProp.shine;
131 ChangeTransparency() = theProp.trans;
132}
133
2166f0fa
SK
134// =======================================================================
135// function : OpenGl_Workspace
136// purpose :
137// =======================================================================
25b97fac 138OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_GraphicDriver)& theDriver,
2166f0fa 139 const CALL_DEF_WINDOW& theCWindow,
5e27df78 140 Aspect_RenderingContext theGContext,
58655684 141 const Handle(OpenGl_Caps)& theCaps,
5e27df78 142 const Handle(OpenGl_Context)& theShareCtx)
25b97fac 143: OpenGl_Window (theDriver, theCWindow, theGContext, theCaps, theShareCtx),
a174a3c5 144 NamedStatus (0),
0adbd30f 145 HighlightColor (&THE_WHITE_COLOR),
a174a3c5 146 //
fc73a202 147 myComputeInitStatus (OpenGl_RT_NONE),
148 myIsRaytraceDataValid (Standard_False),
25ef750e 149 myIsRaytraceWarnTextures (Standard_False),
fc73a202 150 myViewModificationStatus (0),
151 myLayersModificationStatus (0),
152 //
a89742cf 153 myRaytraceFilter (new OpenGl_RaytraceFilter()),
154 myToRedrawGL (Standard_True),
73192b37 155 myAntiAliasingMode (3),
a174a3c5 156 myTransientDrawToFront (Standard_True),
679ecdee 157 myBackBufferRestored (Standard_False),
158 myIsImmediateDrawn (Standard_False),
2166f0fa
SK
159 myUseZBuffer (Standard_False),
160 myUseDepthTest (Standard_True),
161 myUseGLLight (Standard_True),
b7cd4ba7 162 myIsCullingEnabled (Standard_False),
a89742cf 163 myFrameCounter (0),
2166f0fa 164 //
2166f0fa
SK
165 AspectLine_set (&myDefaultAspectLine),
166 AspectLine_applied (NULL),
167 AspectFace_set (&myDefaultAspectFace),
168 AspectFace_applied (NULL),
169 AspectMarker_set (&myDefaultAspectMarker),
170 AspectMarker_applied (NULL),
171 AspectText_set (&myDefaultAspectText),
172 AspectText_applied (NULL),
173 TextParam_set (&myDefaultTextParam),
174 TextParam_applied (NULL),
175 ViewMatrix_applied (&myDefaultMatrix),
176 StructureMatrix_applied (&myDefaultMatrix),
3b1817a9 177 myCullingMode (TelCullUndefined),
0f8c0fb8 178 myModelViewMatrix (myDefaultMatrix),
550f3b8b 179 PolygonOffset_applied (THE_DEFAULT_POFFSET)
2166f0fa 180{
73192b37 181 myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
182
183 if (!myGlContext->GetResource ("OpenGl_LineAttributes", myLineAttribs))
184 {
185 // share and register for release once the resource is no longer used
186 myLineAttribs = new OpenGl_LineAttributes();
187 myGlContext->ShareResource ("OpenGl_LineAttributes", myLineAttribs);
188 myLineAttribs->Init (myGlContext);
189 }
2166f0fa
SK
190
191 // General initialization of the context
192
ca3c13d1 193#if !defined(GL_ES_VERSION_2_0)
2166f0fa
SK
194 // Eviter d'avoir les faces mal orientees en noir.
195 // Pourrait etre utiliser pour detecter les problemes d'orientation
bf75be98 196 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
2166f0fa
SK
197
198 // Optimisation pour le Fog et l'antialiasing
199 glHint (GL_FOG_HINT, GL_FASTEST);
200 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
201 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
202 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
ca3c13d1 203#endif
2166f0fa 204
73192b37 205 // AA mode
206 const char* anAaEnv = ::getenv ("CALL_OPENGL_ANTIALIASING_MODE");
207 if (anAaEnv != NULL)
208 {
209 int v;
210 if (sscanf (anAaEnv, "%d", &v) > 0) myAntiAliasingMode = v;
211 }
2166f0fa
SK
212}
213
1981cb22 214// =======================================================================
215// function : SetImmediateModeDrawToFront
216// purpose :
217// =======================================================================
218Standard_Boolean OpenGl_Workspace::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
219{
220 const Standard_Boolean aPrevMode = myTransientDrawToFront;
221 myTransientDrawToFront = theDrawToFrontBuffer;
222 return aPrevMode;
223}
224
2166f0fa
SK
225// =======================================================================
226// function : ~OpenGl_Workspace
227// purpose :
228// =======================================================================
229OpenGl_Workspace::~OpenGl_Workspace()
230{
73192b37 231 if (!myLineAttribs.IsNull())
232 {
233 myLineAttribs.Nullify();
234 myGlContext->ReleaseResource ("OpenGl_LineAttributes", Standard_True);
235 }
236
fc73a202 237 ReleaseRaytraceResources();
2166f0fa
SK
238}
239
240// =======================================================================
241// function : Activate
242// purpose :
243// =======================================================================
244Standard_Boolean OpenGl_Workspace::Activate()
245{
246 if (!OpenGl_Window::Activate())
247 return Standard_False;
248
2166f0fa
SK
249 ViewMatrix_applied = &myDefaultMatrix;
250 StructureMatrix_applied = &myDefaultMatrix;
26395493 251
252 ResetAppliedAspect();
253
254 return Standard_True;
2166f0fa
SK
255}
256
26395493 257//=======================================================================
258//function : ResetAppliedAspect
259//purpose : Sets default values of GL parameters in accordance with default aspects
260//=======================================================================
261void OpenGl_Workspace::ResetAppliedAspect()
262{
bf75be98 263 NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
0adbd30f 264 HighlightColor = &THE_WHITE_COLOR;
26395493 265 AspectLine_set = &myDefaultAspectLine;
266 AspectLine_applied = NULL;
267 AspectFace_set = &myDefaultAspectFace;
268 AspectFace_applied = NULL;
269 AspectMarker_set = &myDefaultAspectMarker;
270 AspectMarker_applied = NULL;
271 AspectText_set = &myDefaultAspectText;
272 AspectText_applied = NULL;
273 TextParam_set = &myDefaultTextParam;
274 TextParam_applied = NULL;
550f3b8b 275 PolygonOffset_applied = THE_DEFAULT_POFFSET;
3b1817a9 276 myCullingMode = TelCullUndefined;
26395493 277
278 AspectLine(Standard_True);
279 AspectFace(Standard_True);
280 AspectMarker(Standard_True);
281 AspectText(Standard_True);
282}
bf75be98 283
284// =======================================================================
285// function : DisableTexture
286// purpose :
287// =======================================================================
288Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
289{
290 if (myTextureBound.IsNull())
291 {
292 return myTextureBound;
293 }
294
e3414ada 295 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
296 if (!aSampler.IsNull())
297 {
298 aSampler->Unbind (*myGlContext);
299 }
300
ca3c13d1 301#if !defined(GL_ES_VERSION_2_0)
bf75be98 302 // reset texture matrix because some code may expect it is identity
303 GLint aMatrixMode = GL_TEXTURE;
304 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
305 glMatrixMode (GL_TEXTURE);
306 glLoadIdentity();
307 glMatrixMode (aMatrixMode);
ca3c13d1 308#endif
bf75be98 309
310 myTextureBound->Unbind (myGlContext);
311 switch (myTextureBound->GetTarget())
312 {
ca3c13d1 313 #if !defined(GL_ES_VERSION_2_0)
bf75be98 314 case GL_TEXTURE_1D:
315 {
ca3c13d1 316
bf75be98 317 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
318 {
319 glDisable (GL_TEXTURE_GEN_S);
320 }
321 glDisable (GL_TEXTURE_1D);
322 break;
323 }
ca3c13d1 324 #endif
bf75be98 325 case GL_TEXTURE_2D:
326 {
ca3c13d1 327 #if !defined(GL_ES_VERSION_2_0)
bf75be98 328 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
329 {
330 glDisable (GL_TEXTURE_GEN_S);
331 glDisable (GL_TEXTURE_GEN_T);
a577aaab 332 if (myTextureBound->GetParams()->GenMode() == Graphic3d_TOTM_SPRITE)
333 {
334 glDisable (GL_POINT_SPRITE);
335 }
bf75be98 336 }
337 glDisable (GL_TEXTURE_2D);
05e2200b 338 #endif
bf75be98 339 break;
340 }
341 default: break;
342 }
343
344 Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
345 myTextureBound.Nullify();
346 return aPrevTexture;
347}
348
349// =======================================================================
350// function : setTextureParams
351// purpose :
352// =======================================================================
353void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture,
354 const Handle(Graphic3d_TextureParams)& theParams)
355{
356 const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
357 if (aParams.IsNull())
358 {
359 return;
360 }
361
ca3c13d1 362#if !defined(GL_ES_VERSION_2_0)
bf75be98 363 GLint aMatrixMode = GL_TEXTURE;
364 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
365
366 // setup texture matrix
367 glMatrixMode (GL_TEXTURE);
c827ea3a 368 OpenGl_Mat4 aTextureMat;
bf75be98 369 const Graphic3d_Vec2& aScale = aParams->Scale();
370 const Graphic3d_Vec2& aTrans = aParams->Translation();
c827ea3a 371 OpenGl_Utils::Scale (aTextureMat, aScale.x(), aScale.y(), 1.0f);
372 OpenGl_Utils::Translate (aTextureMat, -aTrans.x(), -aTrans.y(), 0.0f);
373 OpenGl_Utils::Rotate (aTextureMat, -aParams->Rotation(), 0.0f, 0.0f, 1.0f);
374 glLoadMatrixf (aTextureMat);
bf75be98 375
076ca35c 376 GLint anEnvMode = GL_MODULATE; // lighting mode
377 if (!aParams->IsModulate())
378 {
379 anEnvMode = GL_DECAL;
380 if (theTexture->GetFormat() == GL_ALPHA
381 || theTexture->GetFormat() == GL_LUMINANCE)
382 {
383 anEnvMode = GL_REPLACE;
384 }
385 }
386
bf75be98 387 // setup generation of texture coordinates
388 switch (aParams->GenMode())
389 {
390 case Graphic3d_TOTM_OBJECT:
391 {
392 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
393 glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData());
394 if (theTexture->GetTarget() != GL_TEXTURE_1D)
395 {
396 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
397 glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData());
398 }
399 break;
400 }
401 case Graphic3d_TOTM_SPHERE:
402 {
403 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
404 if (theTexture->GetTarget() != GL_TEXTURE_1D)
405 {
406 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
407 }
408 break;
409 }
410 case Graphic3d_TOTM_EYE:
411 {
c827ea3a 412 myGlContext->WorldViewState.Push();
413
414 myGlContext->WorldViewState.SetIdentity();
415 myGlContext->ApplyWorldViewMatrix();
bf75be98 416
417 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
418 glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData());
419
420 if (theTexture->GetTarget() != GL_TEXTURE_1D)
421 {
422 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
423 glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData());
424 }
c827ea3a 425
426 myGlContext->WorldViewState.Pop();
427
bf75be98 428 break;
429 }
a577aaab 430 case Graphic3d_TOTM_SPRITE:
431 {
432 if (GetGlContext()->core20 != NULL)
433 {
434 glEnable (GL_POINT_SPRITE);
435 glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
076ca35c 436 anEnvMode = GL_REPLACE;
a577aaab 437 GetGlContext()->core15->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT);
438 }
439 break;
440 }
bf75be98 441 case Graphic3d_TOTM_MANUAL:
442 default: break;
443 }
444
445 // setup lighting
076ca35c 446 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, anEnvMode);
ca3c13d1 447#endif
bf75be98 448
25ef750e 449 // get active sampler object to override default texture parameters
450 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
451
bf75be98 452 // setup texture filtering and wrapping
453 //if (theTexture->GetParams() != theParams)
454 const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
ca3c13d1 455 const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : myGlContext->TextureWrapClamp();
bf75be98 456 switch (theTexture->GetTarget())
457 {
ca3c13d1 458 #if !defined(GL_ES_VERSION_2_0)
bf75be98 459 case GL_TEXTURE_1D:
460 {
25ef750e 461 if (aSampler.IsNull() || !aSampler->IsValid())
462 {
463 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
464 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
465 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode);
466 }
467 else
468 {
469 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter);
470 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilter);
471 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode);
472 }
473
bf75be98 474 break;
475 }
ca3c13d1 476 #endif
bf75be98 477 case GL_TEXTURE_2D:
478 {
479 GLenum aFilterMin = aFilter;
480 if (theTexture->HasMipmaps())
481 {
482 aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
483 if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
484 {
485 aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
486 }
487 else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
488 {
489 aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
490 }
491
492 if (myGlContext->extAnis)
493 {
494 // setup degree of anisotropy filter
495 const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
25ef750e 496 GLint aDegree;
bf75be98 497 switch (aParams->AnisoFilter())
498 {
499 case Graphic3d_LOTA_QUALITY:
500 {
25ef750e 501 aDegree = aMaxDegree;
bf75be98 502 break;
503 }
504 case Graphic3d_LOTA_MIDDLE:
505 {
25ef750e 506 aDegree = (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2);
bf75be98 507 break;
508 }
509 case Graphic3d_LOTA_FAST:
510 {
25ef750e 511 aDegree = 2;
bf75be98 512 break;
513 }
514 case Graphic3d_LOTA_OFF:
515 default:
516 {
25ef750e 517 aDegree = 1;
bf75be98 518 break;
519 }
520 }
25ef750e 521
522 if (aSampler.IsNull() || !aSampler->IsValid())
523 {
524 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree);
525 }
526 else
527 {
528 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree);
529 }
bf75be98 530 }
531 }
25ef750e 532
533 if (aSampler.IsNull() || !aSampler->IsValid())
534 {
535 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin);
536 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter);
537 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, aWrapMode);
538 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, aWrapMode);
539 }
540 else
541 {
542 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilterMin);
543 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter);
544 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode);
545 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_T, aWrapMode);
546 }
547
bf75be98 548 break;
549 }
550 default: break;
551 }
552
553 switch (theTexture->GetTarget())
554 {
ca3c13d1 555 #if !defined(GL_ES_VERSION_2_0)
bf75be98 556 case GL_TEXTURE_1D:
557 {
558 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
559 {
560 glEnable (GL_TEXTURE_GEN_S);
561 }
562 glEnable (GL_TEXTURE_1D);
563 break;
564 }
ca3c13d1 565 #endif
bf75be98 566 case GL_TEXTURE_2D:
567 {
ca3c13d1 568 #if !defined(GL_ES_VERSION_2_0)
bf75be98 569 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
570 {
571 glEnable (GL_TEXTURE_GEN_S);
572 glEnable (GL_TEXTURE_GEN_T);
573 }
574 glEnable (GL_TEXTURE_2D);
05e2200b 575 #endif
bf75be98 576 break;
577 }
578 default: break;
579 }
580
ca3c13d1 581#if !defined(GL_ES_VERSION_2_0)
bf75be98 582 glMatrixMode (aMatrixMode); // turn back active matrix
ca3c13d1 583#endif
bf75be98 584 theTexture->SetParams (aParams);
585}
586
587// =======================================================================
588// function : EnableTexture
589// purpose :
590// =======================================================================
591Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture,
592 const Handle(Graphic3d_TextureParams)& theParams)
593{
594 if (theTexture.IsNull() || !theTexture->IsValid())
595 {
596 return DisableTexture();
597 }
598
bca1d6e2 599 if (myTextureBound == theTexture
600 && (theParams.IsNull() || theParams == theTexture->GetParams()))
bf75be98 601 {
bca1d6e2 602 // already bound
603 return myTextureBound;
bf75be98 604 }
605
606 Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
607 myTextureBound = theTexture;
608 myTextureBound->Bind (myGlContext);
609 setTextureParams (myTextureBound, theParams);
610
25ef750e 611 // If custom sampler object is available it will be
612 // used for overriding default texture parameters
613 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
614
615 if (!aSampler.IsNull() && aSampler->IsValid())
616 {
617 aSampler->Bind (*myGlContext);
618 }
619
bf75be98 620 return aPrevTexture;
621}
a174a3c5 622
623// =======================================================================
624// function : Redraw
625// purpose :
626// =======================================================================
627void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView,
628 const Aspect_CLayer2d& theCUnderLayer,
629 const Aspect_CLayer2d& theCOverLayer)
630{
631 if (!Activate())
632 {
633 return;
634 }
635
a89742cf 636 ++myFrameCounter;
b7cd4ba7 637 myIsCullingEnabled = theCView.IsCullingEnabled;
638
a174a3c5 639 // release pending GL resources
640 Handle(OpenGl_Context) aGlCtx = GetGlContext();
641 aGlCtx->ReleaseDelayed();
642
b5ac8292 643 // fetch OpenGl context state
644 aGlCtx->FetchState();
a174a3c5 645
25b97fac 646 Tint toSwap = (aGlCtx->IsRender() && !aGlCtx->caps->buffersNoSwap) ? 1 : 0; // swap buffers
a174a3c5 647 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO;
648 if (aFrameBuffer != NULL)
649 {
fd4a6963 650 aFrameBuffer->SetupViewport (aGlCtx);
a174a3c5 651 toSwap = 0; // no need to swap buffers
652 }
e3573bb9 653 else
654 {
655 aGlCtx->core11fwd->glViewport (0, 0, myWidth, myHeight);
656 }
a174a3c5 657
a89742cf 658 myToRedrawGL = Standard_True;
659 if (theCView.RenderParams.Method == Graphic3d_RM_RAYTRACING
660 && myComputeInitStatus != OpenGl_RT_FAIL)
e276548b 661 {
a89742cf 662 if (UpdateRaytraceGeometry (OpenGl_GUM_CHECK) && myIsRaytraceDataValid)
663 {
664 myToRedrawGL = Standard_False;
665
666 // Only non-raytracable structures should be rendered in OpenGL mode.
667 Handle(OpenGl_RenderFilter) aRenderFilter = GetRenderFilter();
668 myRaytraceFilter->SetPrevRenderFilter (aRenderFilter);
669 SetRenderFilter (myRaytraceFilter);
670
671 Standard_Integer aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWidth;
672 Standard_Integer aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myHeight;
673
674 if (myOpenGlFBO.IsNull())
675 {
676 myOpenGlFBO = new OpenGl_FrameBuffer();
677 }
678 if (myOpenGlFBO->GetVPSizeX() != aSizeX
679 || myOpenGlFBO->GetVPSizeY() != aSizeY)
680 {
681 myOpenGlFBO->Init (aGlCtx, aSizeX, aSizeY);
682 }
683
684 // OverLayer and UnderLayer shouldn't be drawn by OpenGL.
685 // They will be drawn during ray-tracing.
686 Aspect_CLayer2d anEmptyCLayer;
687 anEmptyCLayer.ptrLayer = NULL;
688
689 myOpenGlFBO->BindBuffer (aGlCtx);
690 redraw1 (theCView, anEmptyCLayer, anEmptyCLayer, 0);
691 myOpenGlFBO->UnbindBuffer (aGlCtx);
692
a1954302 693 const Standard_Boolean isImmediate = myView->HasImmediateStructures();
a89742cf 694 Raytrace (theCView, aSizeX, aSizeY, isImmediate ? 0 : toSwap,
695 theCOverLayer, theCUnderLayer, aFrameBuffer);
696
697 if (isImmediate)
698 {
699 RedrawImmediate (theCView, theCUnderLayer, theCOverLayer, Standard_True);
700 }
701
702 SetRenderFilter (aRenderFilter);
703
704 theCView.WasRedrawnGL = Standard_False;
705 }
706 }
707
708 if (myToRedrawGL)
709 {
710 // draw entire frame using normal OpenGL pipeline
711 if (aFrameBuffer != NULL)
712 {
713 aFrameBuffer->BindBuffer (aGlCtx);
714 }
715
a1954302 716 const Standard_Boolean isImmediate = myView->HasImmediateStructures();
679ecdee 717 redraw1 (theCView, theCUnderLayer, theCOverLayer, isImmediate ? 0 : toSwap);
718 if (isImmediate)
e276548b 719 {
679ecdee 720 RedrawImmediate (theCView, theCUnderLayer, theCOverLayer, Standard_True);
e276548b 721 }
722
723 theCView.WasRedrawnGL = Standard_True;
e276548b 724 }
a174a3c5 725
726 if (aFrameBuffer != NULL)
727 {
728 aFrameBuffer->UnbindBuffer (aGlCtx);
729 // move back original viewport
e3573bb9 730 aGlCtx->core11fwd->glViewport (0, 0, myWidth, myHeight);
a174a3c5 731 }
732
679ecdee 733#if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
a174a3c5 734 if (OpenGl_AVIWriter_AllowWriting (theCView.DefWindow.XWindow))
735 {
736 GLint params[4];
737 glGetIntegerv (GL_VIEWPORT, params);
738 int nWidth = params[2] & ~0x7;
739 int nHeight = params[3] & ~0x7;
740
741 const int nBitsPerPixel = 24;
742 GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8];
743
744 glPixelStorei (GL_PACK_ALIGNMENT, 1);
745 glReadPixels (0, 0, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, aDumpData);
746 OpenGl_AVIWriter_AVIWriter (aDumpData, nWidth, nHeight, nBitsPerPixel);
747 delete[] aDumpData;
748 }
749#endif
750
751 // reset render mode state
b5ac8292 752 aGlCtx->FetchState();
a174a3c5 753}
679ecdee 754
755// =======================================================================
756// function : redraw1
757// purpose :
758// =======================================================================
759void OpenGl_Workspace::redraw1 (const Graphic3d_CView& theCView,
760 const Aspect_CLayer2d& theCUnderLayer,
761 const Aspect_CLayer2d& theCOverLayer,
762 const int theToSwap)
763{
73192b37 764 if (myView.IsNull())
679ecdee 765 {
766 return;
767 }
768
769 // request reset of material
770 NamedStatus |= OPENGL_NS_RESMAT;
771
679ecdee 772 GLbitfield toClear = GL_COLOR_BUFFER_BIT;
773 if (myUseZBuffer)
774 {
775 glDepthFunc (GL_LEQUAL);
776 glDepthMask (GL_TRUE);
777 if (myUseDepthTest)
778 {
779 glEnable (GL_DEPTH_TEST);
780 }
781 else
782 {
783 glDisable (GL_DEPTH_TEST);
784 }
785
ca3c13d1 786 #if !defined(GL_ES_VERSION_2_0)
679ecdee 787 glClearDepth (1.0);
ca3c13d1 788 #else
789 glClearDepthf (1.0f);
790 #endif
679ecdee 791 toClear |= GL_DEPTH_BUFFER_BIT;
792 }
793 else
794 {
795 glDisable (GL_DEPTH_TEST);
796 }
797
a89742cf 798 if (!ToRedrawGL())
679ecdee 799 {
a89742cf 800 // set background to black
801 glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
802 toClear |= GL_DEPTH_BUFFER_BIT; //
679ecdee 803 }
804 else
805 {
a89742cf 806 if (NamedStatus & OPENGL_NS_WHITEBACK)
807 {
808 // set background to white
809 glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
810 toClear |= GL_DEPTH_BUFFER_BIT;
811 }
812 else
813 {
814 glClearColor (myBgColor.rgb[0], myBgColor.rgb[1], myBgColor.rgb[2], 0.0f);
815 }
679ecdee 816 }
817 glClear (toClear);
818
819 Handle(OpenGl_Workspace) aWS (this);
a1954302 820 myView->Render (myPrintContext, aWS, theCView, theCUnderLayer, theCOverLayer, Standard_False);
679ecdee 821
822 // swap the buffers
823 if (theToSwap)
824 {
825 GetGlContext()->SwapBuffers();
826 myBackBufferRestored = Standard_False;
827 myIsImmediateDrawn = Standard_False;
828 }
829 else
830 {
a89742cf 831 glFlush(); //
832 myBackBufferRestored = Standard_True;//
833 myIsImmediateDrawn = Standard_False;//
679ecdee 834 }
835}
836
837// =======================================================================
838// function : copyBackToFront
839// purpose :
840// =======================================================================
841void OpenGl_Workspace::copyBackToFront()
842{
ca3c13d1 843#if !defined(GL_ES_VERSION_2_0)
ca3c13d1 844
c827ea3a 845 OpenGl_Mat4 aProjectMat;
846 OpenGl_Utils::Ortho2D<Standard_ShortReal> (aProjectMat,
847 0.f, static_cast<GLfloat> (myWidth), 0.f, static_cast<GLfloat> (myHeight));
848
849 myGlContext->WorldViewState.Push();
850 myGlContext->ProjectionState.Push();
851
852 myGlContext->WorldViewState.SetIdentity();
853 myGlContext->ProjectionState.SetCurrent (aProjectMat);
854
855 myGlContext->ApplyProjectionMatrix();
856 myGlContext->ApplyWorldViewMatrix();
679ecdee 857
858 DisableFeatures();
859
860 glDrawBuffer (GL_FRONT);
861 glReadBuffer (GL_BACK);
862
863 glRasterPos2i (0, 0);
864 glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_COLOR);
a1954302 865 //glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH);
679ecdee 866
867 EnableFeatures();
868
c827ea3a 869 myGlContext->WorldViewState.Pop();
870 myGlContext->ProjectionState.Pop();
871 myGlContext->ApplyProjectionMatrix();
679ecdee 872 glDrawBuffer (GL_BACK);
873
ca3c13d1 874#endif
679ecdee 875 myIsImmediateDrawn = Standard_False;
876}
877
878// =======================================================================
879// function : DisplayCallback
880// purpose :
881// =======================================================================
882void OpenGl_Workspace::DisplayCallback (const Graphic3d_CView& theCView,
883 Standard_Integer theReason)
884{
885 if (theCView.GDisplayCB == NULL)
886 {
887 return;
888 }
889
890 Aspect_GraphicCallbackStruct aCallData;
891 aCallData.reason = theReason;
892 aCallData.glContext = GetGlContext();
893 aCallData.wsID = theCView.WsId;
894 aCallData.viewID = theCView.ViewId;
895 theCView.GDisplayCB (theCView.DefWindow.XWindow, theCView.GClientData, &aCallData);
896}
897
898// =======================================================================
899// function : RedrawImmediate
900// purpose :
901// =======================================================================
902void OpenGl_Workspace::RedrawImmediate (const Graphic3d_CView& theCView,
903 const Aspect_CLayer2d& theCUnderLayer,
904 const Aspect_CLayer2d& theCOverLayer,
905 const Standard_Boolean theToForce)
906{
907 if (!Activate())
908 {
909 return;
910 }
911
912 GLboolean isDoubleBuffer = GL_FALSE;
ca3c13d1 913#if !defined(GL_ES_VERSION_2_0)
679ecdee 914 glGetBooleanv (GL_DOUBLEBUFFER, &isDoubleBuffer);
ca3c13d1 915#endif
a1954302 916 if (!myView->HasImmediateStructures())
679ecdee 917 {
918 if (theToForce
919 || !myIsImmediateDrawn)
920 {
921 myIsImmediateDrawn = Standard_False;
922 return;
923 }
924
925 if (myBackBufferRestored
926 && isDoubleBuffer)
927 {
928 copyBackToFront();
65812bd0 929 glFlush();
679ecdee 930 }
931 else
932 {
933 Redraw (theCView, theCUnderLayer, theCOverLayer);
934 }
935 return;
936 }
937
938 if (isDoubleBuffer && myTransientDrawToFront)
939 {
940 if (!myBackBufferRestored)
941 {
942 Redraw (theCView, theCUnderLayer, theCOverLayer);
943 return;
944 }
945 copyBackToFront();
946 MakeFrontBufCurrent();
947 }
948 else
949 {
950 myBackBufferRestored = Standard_False;
951 }
952 myIsImmediateDrawn = Standard_True;
953
679ecdee 954 Handle(OpenGl_Workspace) aWS (this);
a1954302 955
956 if (myUseZBuffer)
957 {
958 glDepthFunc (GL_LEQUAL);
959 glDepthMask (GL_TRUE);
960 if (myUseDepthTest)
961 {
962 glEnable (GL_DEPTH_TEST);
963 }
964 else
965 {
966 glDisable (GL_DEPTH_TEST);
967 }
968
969 #if !defined(GL_ES_VERSION_2_0)
970 glClearDepth (1.0);
971 #else
972 glClearDepthf (1.0f);
973 #endif
974 }
975 else
976 {
977 glDisable (GL_DEPTH_TEST);
978 }
979
980 myView->Render (myPrintContext, aWS, theCView, theCUnderLayer, theCOverLayer, Standard_True);
981 if (!myView->ImmediateStructures().IsEmpty())
982 {
983 glDisable (GL_DEPTH_TEST);
984 }
679ecdee 985 for (OpenGl_SequenceOfStructure::Iterator anIter (myView->ImmediateStructures());
986 anIter.More(); anIter.Next())
987 {
988 const OpenGl_Structure* aStructure = anIter.Value();
a1954302 989 if (!aStructure->IsVisible())
990 {
991 continue;
992 }
993
679ecdee 994 aStructure->Render (aWS);
995 }
996
679ecdee 997 if (isDoubleBuffer && myTransientDrawToFront)
998 {
999 glFlush();
1000 MakeBackBufCurrent();
1001 }
1002}