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