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