0024403: BRepBuilderAPI_Sewing should have get/set Context methods
[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
e276548b 20
21#ifdef HAVE_CONFIG_H
22 #include <config.h>
23#endif
24
a577aaab 25#include <OpenGl_GlCore15.hxx>
5f8b738e 26
2166f0fa
SK
27#include <InterfaceGraphic.hxx>
28
2166f0fa
SK
29#include <OpenGl_AspectLine.hxx>
30#include <OpenGl_AspectFace.hxx>
31#include <OpenGl_AspectMarker.hxx>
32#include <OpenGl_AspectText.hxx>
bf75be98 33#include <OpenGl_Context.hxx>
a174a3c5 34#include <OpenGl_FrameBuffer.hxx>
bf75be98 35#include <OpenGl_Texture.hxx>
e276548b 36#include <OpenGl_View.hxx>
a174a3c5 37#include <OpenGl_Workspace.hxx>
4269bd1b 38#include <OpenGl_Element.hxx>
2166f0fa 39
bf75be98 40#include <Graphic3d_TextureParams.hxx>
2166f0fa 41
58655684 42#if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
a174a3c5 43 #include <OpenGl_AVIWriter.hxx>
44#endif
45
2166f0fa
SK
46IMPLEMENT_STANDARD_HANDLE(OpenGl_Workspace,OpenGl_Window)
47IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,OpenGl_Window)
48
49namespace
50{
51 static const TEL_COLOUR myDefaultHighlightColor = { { 1.F, 1.F, 1.F, 1.F } };
52
53 static const OpenGl_AspectLine myDefaultAspectLine;
54 static const OpenGl_AspectFace myDefaultAspectFace;
55 static const OpenGl_AspectMarker myDefaultAspectMarker;
56 static const OpenGl_AspectText myDefaultAspectText;
57
58 static const OpenGl_TextParam myDefaultTextParam =
59 {
60 16, Graphic3d_HTA_LEFT, Graphic3d_VTA_BOTTOM
61 };
62
63 static const OpenGl_Matrix myDefaultMatrix =
64 {
65 {{ 1.0F, 0.0F, 0.0F, 0.0F },
66 { 0.0F, 1.0F, 0.0F, 0.0F },
67 { 0.0F, 0.0F, 1.0F, 0.0F },
68 { 0.0F, 0.0F, 0.0F, 1.0F }}
69 };
bf75be98 70
2166f0fa
SK
71};
72
73// =======================================================================
74// function : OpenGl_Workspace
75// purpose :
76// =======================================================================
77OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
78 const CALL_DEF_WINDOW& theCWindow,
5e27df78 79 Aspect_RenderingContext theGContext,
58655684 80 const Handle(OpenGl_Caps)& theCaps,
5e27df78 81 const Handle(OpenGl_Context)& theShareCtx)
58655684 82: OpenGl_Window (theDisplay, theCWindow, theGContext, theCaps, theShareCtx),
a174a3c5 83 NamedStatus (0),
a174a3c5 84 HighlightColor (&myDefaultHighlightColor),
85 //
2166f0fa
SK
86 myIsTransientOpen (Standard_False),
87 myRetainMode (Standard_False),
a174a3c5 88 myTransientDrawToFront (Standard_True),
2166f0fa
SK
89 myUseTransparency (Standard_False),
90 myUseZBuffer (Standard_False),
91 myUseDepthTest (Standard_True),
92 myUseGLLight (Standard_True),
93 myBackBufferRestored (Standard_False),
94 //
2166f0fa
SK
95 AspectLine_set (&myDefaultAspectLine),
96 AspectLine_applied (NULL),
97 AspectFace_set (&myDefaultAspectFace),
98 AspectFace_applied (NULL),
99 AspectMarker_set (&myDefaultAspectMarker),
100 AspectMarker_applied (NULL),
101 AspectText_set (&myDefaultAspectText),
102 AspectText_applied (NULL),
103 TextParam_set (&myDefaultTextParam),
104 TextParam_applied (NULL),
105 ViewMatrix_applied (&myDefaultMatrix),
106 StructureMatrix_applied (&myDefaultMatrix),
0f8c0fb8 107 myModelViewMatrix (myDefaultMatrix),
2166f0fa
SK
108 PolygonOffset_applied (NULL)
109{
110 theDisplay->InitAttributes();
111
112 // General initialization of the context
113
114 // Eviter d'avoir les faces mal orientees en noir.
115 // Pourrait etre utiliser pour detecter les problemes d'orientation
bf75be98 116 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
2166f0fa
SK
117
118 // Optimisation pour le Fog et l'antialiasing
119 glHint (GL_FOG_HINT, GL_FASTEST);
120 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
121 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
122 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
123
124 // Polygon Offset
125 EnablePolygonOffset();
e276548b 126
127#ifdef HAVE_OPENCL
128
129 myComputeInitStatus = OpenGl_CLIS_NONE;
130
131 myViewModificationStatus = 0;
132 myLayersModificationStatus = 0;
133
134 myRaytraceOutputTexture[0] = 0;
135 myRaytraceOutputTexture[1] = 0;
136
137 myIsRaytraceDataValid = Standard_False;
138 myToUpdateRaytraceData = Standard_False;
139
140#endif
2166f0fa
SK
141}
142
143// =======================================================================
1981cb22 144// function : SetImmediateModeDrawToFront
145// purpose :
146// =======================================================================
147Standard_Boolean OpenGl_Workspace::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
148{
149 const Standard_Boolean aPrevMode = myTransientDrawToFront;
150 myTransientDrawToFront = theDrawToFrontBuffer;
151 return aPrevMode;
152}
153
154// =======================================================================
2166f0fa
SK
155// function : ~OpenGl_Workspace
156// purpose :
157// =======================================================================
158OpenGl_Workspace::~OpenGl_Workspace()
159{
e276548b 160#ifdef HAVE_OPENCL
161 ReleaseOpenCL();
162#endif
2166f0fa
SK
163}
164
165// =======================================================================
166// function : Activate
167// purpose :
168// =======================================================================
169Standard_Boolean OpenGl_Workspace::Activate()
170{
171 if (!OpenGl_Window::Activate())
172 return Standard_False;
173
2166f0fa
SK
174 ViewMatrix_applied = &myDefaultMatrix;
175 StructureMatrix_applied = &myDefaultMatrix;
26395493 176
177 ResetAppliedAspect();
178
179 return Standard_True;
2166f0fa
SK
180}
181
182// =======================================================================
183// function : UseTransparency
184// purpose : call_togl_transparency
185// =======================================================================
186void OpenGl_Workspace::UseTransparency (const Standard_Boolean theFlag)
187{
de75ed09 188 myUseTransparency = theFlag;
2166f0fa 189}
26395493 190
191//=======================================================================
192//function : ResetAppliedAspect
193//purpose : Sets default values of GL parameters in accordance with default aspects
194//=======================================================================
195void OpenGl_Workspace::ResetAppliedAspect()
196{
bf75be98 197 NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
26395493 198 HighlightColor = &myDefaultHighlightColor;
199 AspectLine_set = &myDefaultAspectLine;
200 AspectLine_applied = NULL;
201 AspectFace_set = &myDefaultAspectFace;
202 AspectFace_applied = NULL;
203 AspectMarker_set = &myDefaultAspectMarker;
204 AspectMarker_applied = NULL;
205 AspectText_set = &myDefaultAspectText;
206 AspectText_applied = NULL;
207 TextParam_set = &myDefaultTextParam;
208 TextParam_applied = NULL;
209 PolygonOffset_applied = NULL;
210
211 AspectLine(Standard_True);
212 AspectFace(Standard_True);
213 AspectMarker(Standard_True);
214 AspectText(Standard_True);
215}
bf75be98 216
217// =======================================================================
218// function : DisableTexture
219// purpose :
220// =======================================================================
221Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
222{
223 if (myTextureBound.IsNull())
224 {
225 return myTextureBound;
226 }
227
228 // reset texture matrix because some code may expect it is identity
229 GLint aMatrixMode = GL_TEXTURE;
230 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
231 glMatrixMode (GL_TEXTURE);
232 glLoadIdentity();
233 glMatrixMode (aMatrixMode);
234
235 myTextureBound->Unbind (myGlContext);
236 switch (myTextureBound->GetTarget())
237 {
238 case GL_TEXTURE_1D:
239 {
240 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
241 {
242 glDisable (GL_TEXTURE_GEN_S);
243 }
244 glDisable (GL_TEXTURE_1D);
245 break;
246 }
247 case GL_TEXTURE_2D:
248 {
249 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
250 {
251 glDisable (GL_TEXTURE_GEN_S);
252 glDisable (GL_TEXTURE_GEN_T);
a577aaab 253 if (myTextureBound->GetParams()->GenMode() == Graphic3d_TOTM_SPRITE)
254 {
255 glDisable (GL_POINT_SPRITE);
256 }
bf75be98 257 }
258 glDisable (GL_TEXTURE_2D);
259 break;
260 }
261 default: break;
262 }
263
264 Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
265 myTextureBound.Nullify();
266 return aPrevTexture;
267}
268
269// =======================================================================
270// function : setTextureParams
271// purpose :
272// =======================================================================
273void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture,
274 const Handle(Graphic3d_TextureParams)& theParams)
275{
276 const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
277 if (aParams.IsNull())
278 {
279 return;
280 }
281
282 GLint aMatrixMode = GL_TEXTURE;
283 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
284
285 // setup texture matrix
286 glMatrixMode (GL_TEXTURE);
287 glLoadIdentity();
288 const Graphic3d_Vec2& aScale = aParams->Scale();
289 const Graphic3d_Vec2& aTrans = aParams->Translation();
290 glScalef ( aScale.x(), aScale.y(), 1.0f);
291 glTranslatef (-aTrans.x(), -aTrans.y(), 0.0f);
292 glRotatef (-aParams->Rotation(), 0.0f, 0.0f, 1.0f);
293
294 // setup generation of texture coordinates
295 switch (aParams->GenMode())
296 {
297 case Graphic3d_TOTM_OBJECT:
298 {
299 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
300 glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData());
301 if (theTexture->GetTarget() != GL_TEXTURE_1D)
302 {
303 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
304 glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData());
305 }
306 break;
307 }
308 case Graphic3d_TOTM_SPHERE:
309 {
310 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
311 if (theTexture->GetTarget() != GL_TEXTURE_1D)
312 {
313 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
314 }
315 break;
316 }
317 case Graphic3d_TOTM_EYE:
318 {
319 glMatrixMode (GL_MODELVIEW);
320 glPushMatrix();
321 glLoadIdentity();
322
323 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
324 glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData());
325
326 if (theTexture->GetTarget() != GL_TEXTURE_1D)
327 {
328 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
329 glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData());
330 }
331 glPopMatrix();
332 break;
333 }
a577aaab 334 case Graphic3d_TOTM_SPRITE:
335 {
336 if (GetGlContext()->core20 != NULL)
337 {
338 glEnable (GL_POINT_SPRITE);
339 glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
340 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
341 GetGlContext()->core15->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT);
342 }
343 break;
344 }
bf75be98 345 case Graphic3d_TOTM_MANUAL:
346 default: break;
347 }
348
349 // setup lighting
a577aaab 350 if (aParams->GenMode() != Graphic3d_TOTM_SPRITE)
351 {
352 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aParams->IsModulate() ? GL_MODULATE : GL_DECAL);
353 }
bf75be98 354
355 // setup texture filtering and wrapping
356 //if (theTexture->GetParams() != theParams)
357 const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
358 const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : GL_CLAMP;
359 switch (theTexture->GetTarget())
360 {
361 case GL_TEXTURE_1D:
362 {
363 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
364 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
365 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode);
366 break;
367 }
368 case GL_TEXTURE_2D:
369 {
370 GLenum aFilterMin = aFilter;
371 if (theTexture->HasMipmaps())
372 {
373 aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
374 if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
375 {
376 aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
377 }
378 else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
379 {
380 aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
381 }
382
383 if (myGlContext->extAnis)
384 {
385 // setup degree of anisotropy filter
386 const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
387 switch (aParams->AnisoFilter())
388 {
389 case Graphic3d_LOTA_QUALITY:
390 {
391 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aMaxDegree);
392 break;
393 }
394 case Graphic3d_LOTA_MIDDLE:
395 {
396
397 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2));
398 break;
399 }
400 case Graphic3d_LOTA_FAST:
401 {
402 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2);
403 break;
404 }
405 case Graphic3d_LOTA_OFF:
406 default:
407 {
408 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
409 break;
410 }
411 }
412 }
413 }
414 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin);
415 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter);
416 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, aWrapMode);
417 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, aWrapMode);
418 break;
419 }
420 default: break;
421 }
422
423 switch (theTexture->GetTarget())
424 {
425 case GL_TEXTURE_1D:
426 {
427 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
428 {
429 glEnable (GL_TEXTURE_GEN_S);
430 }
431 glEnable (GL_TEXTURE_1D);
432 break;
433 }
434 case GL_TEXTURE_2D:
435 {
436 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
437 {
438 glEnable (GL_TEXTURE_GEN_S);
439 glEnable (GL_TEXTURE_GEN_T);
440 }
441 glEnable (GL_TEXTURE_2D);
442 break;
443 }
444 default: break;
445 }
446
447 glMatrixMode (aMatrixMode); // turn back active matrix
448 theTexture->SetParams (aParams);
449}
450
451// =======================================================================
452// function : EnableTexture
453// purpose :
454// =======================================================================
455Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture,
456 const Handle(Graphic3d_TextureParams)& theParams)
457{
458 if (theTexture.IsNull() || !theTexture->IsValid())
459 {
460 return DisableTexture();
461 }
462
bca1d6e2 463 if (myTextureBound == theTexture
464 && (theParams.IsNull() || theParams == theTexture->GetParams()))
bf75be98 465 {
bca1d6e2 466 // already bound
467 return myTextureBound;
bf75be98 468 }
469
470 Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
471 myTextureBound = theTexture;
472 myTextureBound->Bind (myGlContext);
473 setTextureParams (myTextureBound, theParams);
474
475 return aPrevTexture;
476}
a174a3c5 477
478// =======================================================================
479// function : Redraw
480// purpose :
481// =======================================================================
482void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView,
483 const Aspect_CLayer2d& theCUnderLayer,
484 const Aspect_CLayer2d& theCOverLayer)
485{
486 if (!Activate())
487 {
488 return;
489 }
490
491 // release pending GL resources
492 Handle(OpenGl_Context) aGlCtx = GetGlContext();
493 aGlCtx->ReleaseDelayed();
494
495 // cache render mode state
496 GLint aRendMode = GL_RENDER;
497 glGetIntegerv (GL_RENDER_MODE, &aRendMode);
498 aGlCtx->SetFeedback (aRendMode == GL_FEEDBACK);
499
500 Tint toSwap = (aRendMode == GL_RENDER); // swap buffers
501 GLint aViewPortBack[4];
502 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO;
503 if (aFrameBuffer != NULL)
504 {
505 glGetIntegerv (GL_VIEWPORT, aViewPortBack);
fd4a6963 506 aFrameBuffer->SetupViewport (aGlCtx);
507 aFrameBuffer->BindBuffer (aGlCtx);
a174a3c5 508 toSwap = 0; // no need to swap buffers
509 }
510
e276548b 511#ifdef HAVE_OPENCL
512 if (!theCView.IsRaytracing || myComputeInitStatus == OpenGl_CLIS_FAIL)
513 {
514#endif
515 Redraw1 (theCView, theCUnderLayer, theCOverLayer, toSwap);
516 if (aFrameBuffer == NULL || !myTransientDrawToFront)
517 {
518 RedrawImmediatMode();
519 }
520
521 theCView.WasRedrawnGL = Standard_True;
522#ifdef HAVE_OPENCL
523 }
524 else
a174a3c5 525 {
e276548b 526 int aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWidth;
527 int aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myHeight;
528
529 Raytrace (theCView, aSizeX, aSizeY, toSwap);
530
531 theCView.WasRedrawnGL = Standard_False;
a174a3c5 532 }
e276548b 533#endif
a174a3c5 534
535 if (aFrameBuffer != NULL)
536 {
537 aFrameBuffer->UnbindBuffer (aGlCtx);
538 // move back original viewport
539 glViewport (aViewPortBack[0], aViewPortBack[1], aViewPortBack[2], aViewPortBack[3]);
540 }
541
542#if (defined(_WIN32) || defined(__WIN32__)) && defined(HAVE_VIDEOCAPTURE)
543 if (OpenGl_AVIWriter_AllowWriting (theCView.DefWindow.XWindow))
544 {
545 GLint params[4];
546 glGetIntegerv (GL_VIEWPORT, params);
547 int nWidth = params[2] & ~0x7;
548 int nHeight = params[3] & ~0x7;
549
550 const int nBitsPerPixel = 24;
551 GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8];
552
553 glPixelStorei (GL_PACK_ALIGNMENT, 1);
554 glReadPixels (0, 0, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, aDumpData);
555 OpenGl_AVIWriter_AVIWriter (aDumpData, nWidth, nHeight, nBitsPerPixel);
556 delete[] aDumpData;
557 }
558#endif
559
560 // reset render mode state
561 aGlCtx->SetFeedback (Standard_False);
562}