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