0028205: Visualization - add functionality for dumping results of detection algorithm...
[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
c357e426 16#include <OpenGl_Workspace.hxx>
5f8b738e 17
c357e426 18#include <OpenGl_ArbFBO.hxx>
2166f0fa
SK
19#include <OpenGl_AspectLine.hxx>
20#include <OpenGl_AspectFace.hxx>
21#include <OpenGl_AspectMarker.hxx>
22#include <OpenGl_AspectText.hxx>
bf75be98 23#include <OpenGl_Context.hxx>
679ecdee 24#include <OpenGl_Element.hxx>
a174a3c5 25#include <OpenGl_FrameBuffer.hxx>
c357e426 26#include <OpenGl_GlCore15.hxx>
27#include <OpenGl_SceneGeometry.hxx>
679ecdee 28#include <OpenGl_Structure.hxx>
25ef750e 29#include <OpenGl_Sampler.hxx>
bf75be98 30#include <OpenGl_Texture.hxx>
e276548b 31#include <OpenGl_View.hxx>
c357e426 32#include <OpenGl_Window.hxx>
2166f0fa 33
bf75be98 34#include <Graphic3d_TextureParams.hxx>
825aa485 35#include <Graphic3d_TransformUtils.hxx>
f9f740d6 36#include <NCollection_AlignedAllocator.hxx>
2166f0fa 37
92efcf78 38IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
39IMPLEMENT_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter)
40
c357e426 41#ifdef HAVE_GL2PS
42 #include <gl2ps.h>
43 /* OCC22216 NOTE: linker dependency can be switched off by undefining macro.
44 Pragma comment for gl2ps.lib is defined only here. */
45 #ifdef _MSC_VER
46 #pragma comment( lib, "gl2ps.lib" )
47 #endif
a174a3c5 48#endif
49
2166f0fa
SK
50namespace
51{
b6472664 52 static const OpenGl_Vec4 THE_WHITE_COLOR (1.0f, 1.0f, 1.0f, 1.0f);
53 static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f);
2166f0fa
SK
54
55 static const OpenGl_AspectLine myDefaultAspectLine;
56 static const OpenGl_AspectFace myDefaultAspectFace;
57 static const OpenGl_AspectMarker myDefaultAspectMarker;
2831708b 58 static const OpenGl_AspectText myDefaultAspectText;
2166f0fa 59
2166f0fa
SK
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
a3f6f591 68}
2166f0fa
SK
69
70// =======================================================================
0adbd30f 71// function : Init
72// purpose :
73// =======================================================================
b6472664 74void OpenGl_Material::Init (const Graphic3d_MaterialAspect& theMat,
75 const Quantity_Color& theInteriorColor)
0adbd30f 76{
b6472664 77 const bool isPhysic = theMat.MaterialType (Graphic3d_MATERIAL_PHYSIC) == Standard_True;
78
0adbd30f 79 // ambient component
b6472664 80 if (theMat.ReflectionMode (Graphic3d_TOR_AMBIENT))
0adbd30f 81 {
b6472664 82 const OpenGl_Vec3& aSrcAmb = isPhysic ? theMat.AmbientColor() : theInteriorColor;
83 Ambient = OpenGl_Vec4 (aSrcAmb * (float )theMat.Ambient(), 1.0f);
0adbd30f 84 }
85 else
86 {
87 Ambient = THE_BLACK_COLOR;
88 }
89
90 // diffusion component
b6472664 91 if (theMat.ReflectionMode (Graphic3d_TOR_DIFFUSE))
0adbd30f 92 {
b6472664 93 const OpenGl_Vec3& aSrcDif = isPhysic ? theMat.DiffuseColor() : theInteriorColor;
94 Diffuse = OpenGl_Vec4 (aSrcDif * (float )theMat.Diffuse(), 1.0f);
0adbd30f 95 }
96 else
97 {
98 Diffuse = THE_BLACK_COLOR;
99 }
100
101 // specular component
b6472664 102 if (theMat.ReflectionMode (Graphic3d_TOR_SPECULAR))
0adbd30f 103 {
b6472664 104 const OpenGl_Vec3& aSrcSpe = isPhysic ? (const OpenGl_Vec3& )theMat.SpecularColor() : THE_WHITE_COLOR.rgb();
105 Specular = OpenGl_Vec4 (aSrcSpe * (float )theMat.Specular(), 1.0f);
0adbd30f 106 }
107 else
108 {
109 Specular = THE_BLACK_COLOR;
110 }
111
112 // emission component
b6472664 113 if (theMat.ReflectionMode (Graphic3d_TOR_EMISSION))
0adbd30f 114 {
b6472664 115 const OpenGl_Vec3& aSrcEms = isPhysic ? theMat.EmissiveColor() : theInteriorColor;
116 Emission = OpenGl_Vec4 (aSrcEms * (float )theMat.Emissive(), 1.0f);
0adbd30f 117 }
118 else
119 {
120 Emission = THE_BLACK_COLOR;
121 }
122
b6472664 123 ChangeShine() = 128.0f * float(theMat.Shininess());
124 ChangeTransparency() = 1.0f - (float )theMat.Transparency();
0adbd30f 125}
126
127// =======================================================================
2166f0fa
SK
128// function : OpenGl_Workspace
129// purpose :
130// =======================================================================
c357e426 131OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow)
132: NamedStatus (0),
c357e426 133 myView (theView),
134 myWindow (theWindow),
135 myGlContext (!theWindow.IsNull() ? theWindow->GetGlContext() : NULL),
eae454e3 136 myUseZBuffer (Standard_True),
137 myUseDepthWrite (Standard_True),
2166f0fa 138 //
f9ba5c4d 139 myAspectLineSet (&myDefaultAspectLine),
140 myAspectFaceSet (&myDefaultAspectFace),
f9ba5c4d 141 myAspectMarkerSet (&myDefaultAspectMarker),
f9ba5c4d 142 myAspectTextSet (&myDefaultAspectText),
143 myAspectFaceAppliedWithHL (false),
144 //
2166f0fa
SK
145 ViewMatrix_applied (&myDefaultMatrix),
146 StructureMatrix_applied (&myDefaultMatrix),
b6472664 147 myToAllowFaceCulling (false),
b6472664 148 myModelViewMatrix (myDefaultMatrix)
2166f0fa 149{
c357e426 150 if (!myGlContext.IsNull() && myGlContext->MakeCurrent())
73192b37 151 {
c357e426 152 myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
2166f0fa 153
c357e426 154 // General initialization of the context
c357e426 155 #if !defined(GL_ES_VERSION_2_0)
156 if (myGlContext->core11 != NULL)
157 {
158 // Eviter d'avoir les faces mal orientees en noir.
159 // Pourrait etre utiliser pour detecter les problemes d'orientation
160 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
2166f0fa 161
c357e426 162 // Optimisation pour le Fog et l'antialiasing
163 glHint (GL_FOG_HINT, GL_FASTEST);
164 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
165 }
2166f0fa 166
c357e426 167 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
168 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
169 #endif
73192b37 170 }
f8ae3605 171
b6472664 172 myDefaultCappingAlgoFilter = new OpenGl_CappingAlgoFilter();
173
174 myNoneCulling .Aspect()->SetSuppressBackFaces (false);
175 myNoneCulling .Aspect()->SetDrawEdges (false);
176 myFrontCulling.Aspect()->SetSuppressBackFaces (true);
177 myFrontCulling.Aspect()->SetDrawEdges (false);
2166f0fa
SK
178}
179
180// =======================================================================
2166f0fa
SK
181// function : Activate
182// purpose :
183// =======================================================================
184Standard_Boolean OpenGl_Workspace::Activate()
185{
c357e426 186 if (myWindow.IsNull() || !myWindow->Activate())
187 {
2166f0fa 188 return Standard_False;
c357e426 189 }
2166f0fa 190
2166f0fa
SK
191 ViewMatrix_applied = &myDefaultMatrix;
192 StructureMatrix_applied = &myDefaultMatrix;
26395493 193
194 ResetAppliedAspect();
195
196 return Standard_True;
2166f0fa
SK
197}
198
26395493 199//=======================================================================
200//function : ResetAppliedAspect
201//purpose : Sets default values of GL parameters in accordance with default aspects
202//=======================================================================
203void OpenGl_Workspace::ResetAppliedAspect()
204{
4e1523ef 205 myGlContext->BindDefaultVao();
206
bf75be98 207 NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
f838dac4 208 myHighlightStyle.Nullify();
b6472664 209 myToAllowFaceCulling = false;
f9ba5c4d 210 myAspectLineSet = &myDefaultAspectLine;
211 myAspectFaceSet = &myDefaultAspectFace;
b6472664 212 myAspectFaceApplied.Nullify();
f9ba5c4d 213 myAspectMarkerSet = &myDefaultAspectMarker;
b6472664 214 myAspectMarkerApplied.Nullify();
f9ba5c4d 215 myAspectTextSet = &myDefaultAspectText;
b6472664 216 myPolygonOffsetApplied= Graphic3d_PolygonOffset();
26395493 217
f9ba5c4d 218 ApplyAspectLine();
219 ApplyAspectFace();
220 ApplyAspectMarker();
221 ApplyAspectText();
ac116c22 222
b6472664 223 myGlContext->SetTypeOfLine (myDefaultAspectLine.Aspect()->Type());
224 myGlContext->SetLineWidth (myDefaultAspectLine.Aspect()->Width());
26395493 225}
bf75be98 226
227// =======================================================================
228// function : DisableTexture
229// purpose :
230// =======================================================================
231Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
232{
233 if (myTextureBound.IsNull())
234 {
235 return myTextureBound;
236 }
237
e3414ada 238 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
239 if (!aSampler.IsNull())
240 {
241 aSampler->Unbind (*myGlContext);
242 }
243
ca3c13d1 244#if !defined(GL_ES_VERSION_2_0)
bf75be98 245 // reset texture matrix because some code may expect it is identity
4e1523ef 246 if (myGlContext->core11 != NULL)
247 {
248 GLint aMatrixMode = GL_TEXTURE;
249 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
250 glMatrixMode (GL_TEXTURE);
251 glLoadIdentity();
252 glMatrixMode (aMatrixMode);
253 }
ca3c13d1 254#endif
bf75be98 255
256 myTextureBound->Unbind (myGlContext);
257 switch (myTextureBound->GetTarget())
258 {
ca3c13d1 259 #if !defined(GL_ES_VERSION_2_0)
bf75be98 260 case GL_TEXTURE_1D:
261 {
4e1523ef 262 if (myGlContext->core11 != NULL)
bf75be98 263 {
4e1523ef 264 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
265 {
266 glDisable (GL_TEXTURE_GEN_S);
267 }
268 glDisable (GL_TEXTURE_1D);
bf75be98 269 }
bf75be98 270 break;
271 }
ca3c13d1 272 #endif
bf75be98 273 case GL_TEXTURE_2D:
274 {
ca3c13d1 275 #if !defined(GL_ES_VERSION_2_0)
4e1523ef 276 if (myGlContext->core11 != NULL)
bf75be98 277 {
4e1523ef 278 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
a577aaab 279 {
4e1523ef 280 glDisable (GL_TEXTURE_GEN_S);
281 glDisable (GL_TEXTURE_GEN_T);
282 if (myTextureBound->GetParams()->GenMode() == Graphic3d_TOTM_SPRITE)
283 {
284 glDisable (GL_POINT_SPRITE);
285 }
a577aaab 286 }
4e1523ef 287 glDisable (GL_TEXTURE_2D);
bf75be98 288 }
05e2200b 289 #endif
bf75be98 290 break;
291 }
292 default: break;
293 }
294
295 Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
296 myTextureBound.Nullify();
297 return aPrevTexture;
298}
299
300// =======================================================================
301// function : setTextureParams
302// purpose :
303// =======================================================================
304void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture,
305 const Handle(Graphic3d_TextureParams)& theParams)
306{
307 const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
308 if (aParams.IsNull())
309 {
310 return;
311 }
312
ca3c13d1 313#if !defined(GL_ES_VERSION_2_0)
4e1523ef 314 if (myGlContext->core11 != NULL)
315 {
4e1523ef 316 GLint anEnvMode = GL_MODULATE; // lighting mode
317 if (!aParams->IsModulate())
076ca35c 318 {
4e1523ef 319 anEnvMode = GL_DECAL;
320 if (theTexture->GetFormat() == GL_ALPHA
321 || theTexture->GetFormat() == GL_LUMINANCE)
322 {
323 anEnvMode = GL_REPLACE;
324 }
076ca35c 325 }
076ca35c 326
4e1523ef 327 // setup generation of texture coordinates
328 switch (aParams->GenMode())
bf75be98 329 {
4e1523ef 330 case Graphic3d_TOTM_OBJECT:
bf75be98 331 {
4e1523ef 332 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
333 glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData());
334 if (theTexture->GetTarget() != GL_TEXTURE_1D)
335 {
336 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
337 glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData());
338 }
339 break;
bf75be98 340 }
4e1523ef 341 case Graphic3d_TOTM_SPHERE:
bf75be98 342 {
4e1523ef 343 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
344 if (theTexture->GetTarget() != GL_TEXTURE_1D)
345 {
346 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
347 }
348 break;
bf75be98 349 }
4e1523ef 350 case Graphic3d_TOTM_EYE:
351 {
352 myGlContext->WorldViewState.Push();
c827ea3a 353
4e1523ef 354 myGlContext->WorldViewState.SetIdentity();
355 myGlContext->ApplyWorldViewMatrix();
bf75be98 356
4e1523ef 357 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
358 glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData());
bf75be98 359
4e1523ef 360 if (theTexture->GetTarget() != GL_TEXTURE_1D)
361 {
362 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
363 glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData());
364 }
c827ea3a 365
4e1523ef 366 myGlContext->WorldViewState.Pop();
c827ea3a 367
4e1523ef 368 break;
369 }
370 case Graphic3d_TOTM_SPRITE:
a577aaab 371 {
c357e426 372 if (myGlContext->core20fwd != NULL)
4e1523ef 373 {
374 glEnable (GL_POINT_SPRITE);
375 glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
376 anEnvMode = GL_REPLACE;
4e1523ef 377 }
378 break;
a577aaab 379 }
4e1523ef 380 case Graphic3d_TOTM_MANUAL:
381 default: break;
a577aaab 382 }
bf75be98 383
4e1523ef 384 // setup lighting
385 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, anEnvMode);
386 }
ca3c13d1 387#endif
bf75be98 388
25ef750e 389 // get active sampler object to override default texture parameters
390 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
391
bf75be98 392 // setup texture filtering and wrapping
393 //if (theTexture->GetParams() != theParams)
394 const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
ca3c13d1 395 const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : myGlContext->TextureWrapClamp();
bf75be98 396 switch (theTexture->GetTarget())
397 {
ca3c13d1 398 #if !defined(GL_ES_VERSION_2_0)
bf75be98 399 case GL_TEXTURE_1D:
400 {
25ef750e 401 if (aSampler.IsNull() || !aSampler->IsValid())
402 {
403 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
404 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
405 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode);
406 }
407 else
408 {
409 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter);
410 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilter);
411 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode);
412 }
413
bf75be98 414 break;
415 }
ca3c13d1 416 #endif
bf75be98 417 case GL_TEXTURE_2D:
418 {
419 GLenum aFilterMin = aFilter;
420 if (theTexture->HasMipmaps())
421 {
422 aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
423 if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
424 {
425 aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
426 }
427 else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
428 {
429 aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
430 }
431
432 if (myGlContext->extAnis)
433 {
434 // setup degree of anisotropy filter
435 const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
25ef750e 436 GLint aDegree;
bf75be98 437 switch (aParams->AnisoFilter())
438 {
439 case Graphic3d_LOTA_QUALITY:
440 {
25ef750e 441 aDegree = aMaxDegree;
bf75be98 442 break;
443 }
444 case Graphic3d_LOTA_MIDDLE:
445 {
25ef750e 446 aDegree = (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2);
bf75be98 447 break;
448 }
449 case Graphic3d_LOTA_FAST:
450 {
25ef750e 451 aDegree = 2;
bf75be98 452 break;
453 }
454 case Graphic3d_LOTA_OFF:
455 default:
456 {
25ef750e 457 aDegree = 1;
bf75be98 458 break;
459 }
460 }
25ef750e 461
462 if (aSampler.IsNull() || !aSampler->IsValid())
463 {
464 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree);
465 }
466 else
467 {
468 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree);
469 }
bf75be98 470 }
471 }
25ef750e 472
473 if (aSampler.IsNull() || !aSampler->IsValid())
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 }
480 else
481 {
482 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilterMin);
483 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter);
484 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode);
485 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_T, aWrapMode);
486 }
487
bf75be98 488 break;
489 }
490 default: break;
491 }
492
493 switch (theTexture->GetTarget())
494 {
ca3c13d1 495 #if !defined(GL_ES_VERSION_2_0)
bf75be98 496 case GL_TEXTURE_1D:
497 {
4e1523ef 498 if (myGlContext->core11 != NULL)
bf75be98 499 {
4e1523ef 500 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
501 {
502 glEnable (GL_TEXTURE_GEN_S);
503 }
504 glEnable (GL_TEXTURE_1D);
bf75be98 505 }
bf75be98 506 break;
507 }
ca3c13d1 508 #endif
bf75be98 509 case GL_TEXTURE_2D:
510 {
ca3c13d1 511 #if !defined(GL_ES_VERSION_2_0)
4e1523ef 512 if (myGlContext->core11 != NULL)
bf75be98 513 {
4e1523ef 514 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
515 {
516 glEnable (GL_TEXTURE_GEN_S);
517 glEnable (GL_TEXTURE_GEN_T);
518 }
519 glEnable (GL_TEXTURE_2D);
bf75be98 520 }
05e2200b 521 #endif
bf75be98 522 break;
523 }
524 default: break;
525 }
526
bf75be98 527 theTexture->SetParams (aParams);
528}
529
530// =======================================================================
531// function : EnableTexture
532// purpose :
533// =======================================================================
534Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture,
535 const Handle(Graphic3d_TextureParams)& theParams)
536{
537 if (theTexture.IsNull() || !theTexture->IsValid())
538 {
539 return DisableTexture();
540 }
541
bca1d6e2 542 if (myTextureBound == theTexture
543 && (theParams.IsNull() || theParams == theTexture->GetParams()))
bf75be98 544 {
bca1d6e2 545 // already bound
546 return myTextureBound;
bf75be98 547 }
548
549 Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
550 myTextureBound = theTexture;
551 myTextureBound->Bind (myGlContext);
552 setTextureParams (myTextureBound, theParams);
553
25ef750e 554 // If custom sampler object is available it will be
555 // used for overriding default texture parameters
556 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
557
558 if (!aSampler.IsNull() && aSampler->IsValid())
559 {
560 aSampler->Bind (*myGlContext);
561 }
562
bf75be98 563 return aPrevTexture;
564}
a174a3c5 565
566// =======================================================================
c357e426 567// function : updateMaterial
38a0206f 568// purpose :
569// =======================================================================
c357e426 570void OpenGl_Workspace::updateMaterial (const int theFlag)
38a0206f 571{
c357e426 572 // Case of hidden line
b6472664 573 if (myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HIDDENLINE)
f978241f 574 {
b6472664 575 // copy all values including line edge aspect
576 *myAspectFaceHl.Aspect().operator->() = *myAspectFaceSet->Aspect();
577 myAspectFaceHl.SetAspectEdge (myAspectFaceSet->AspectEdge());
578 myAspectFaceHl.Aspect()->SetInteriorColor (myView->BackgroundColor().GetRGB());
579 myAspectFaceHl.SetNoLighting (true);
f9ba5c4d 580 myAspectFaceSet = &myAspectFaceHl;
c357e426 581 return;
f978241f 582 }
38a0206f 583
b6472664 584 const Graphic3d_MaterialAspect* aSrcMat = &myAspectFaceSet->Aspect()->FrontMaterial();
585 const Quantity_Color* aSrcIntColor = &myAspectFaceSet->Aspect()->InteriorColor();
c357e426 586 GLenum aFace = GL_FRONT_AND_BACK;
587 if (theFlag == TEL_BACK_MATERIAL)
38a0206f 588 {
b6472664 589 aFace = GL_BACK;
590 aSrcMat = &myAspectFaceSet->Aspect()->BackMaterial();
591 aSrcIntColor = &myAspectFaceSet->Aspect()->BackInteriorColor();
38a0206f 592 }
b6472664 593 else if (myAspectFaceSet->Aspect()->Distinguish()
c357e426 594 && !(NamedStatus & OPENGL_NS_RESMAT))
38a0206f 595 {
c357e426 596 aFace = GL_FRONT;
38a0206f 597 }
38a0206f 598
b6472664 599 myMatTmp.Init (*aSrcMat, *aSrcIntColor);
f838dac4 600 if (!myHighlightStyle.IsNull())
f9ba5c4d 601 {
f838dac4 602 myMatTmp.SetColor (myHighlightStyle->ColorRGBA());
f9ba5c4d 603 }
c357e426 604
605 // handling transparency
606 if (NamedStatus & OPENGL_NS_2NDPASSDO)
38a0206f 607 {
c357e426 608 // second pass
b6472664 609 myMatTmp.Diffuse.a() = aSrcMat->EnvReflexion();
c357e426 610 }
611 else
612 {
b6472664 613 if (aSrcMat->EnvReflexion() != 0.0f)
38a0206f 614 {
c357e426 615 // if the material reflects the environment scene, the second pass is needed
616 NamedStatus |= OPENGL_NS_2NDPASSNEED;
38a0206f 617 }
38a0206f 618
b6472664 619 const float aTransp = (float )aSrcMat->Transparency();
620 if (aTransp != 0.0f)
38a0206f 621 {
c357e426 622 // render transparent
b6472664 623 myMatTmp.Diffuse.a() = 1.0f - aTransp;
c357e426 624 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
625 glEnable (GL_BLEND);
626 if (myUseDepthWrite)
627 {
628 glDepthMask (GL_FALSE);
629 }
38a0206f 630 }
631 else
632 {
c357e426 633 // render opaque
2e5139af 634 glBlendFunc (GL_ONE, GL_ZERO);
635 glDisable (GL_BLEND);
c357e426 636 if (myUseDepthWrite)
637 {
638 glDepthMask (GL_TRUE);
639 }
38a0206f 640 }
641 }
c357e426 642
643 // do not update material properties in case of zero reflection mode,
644 // because GL lighting will be disabled by OpenGl_PrimitiveArray::DrawArray() anyway.
b6472664 645 if (myAspectFaceSet->IsNoLighting())
38a0206f 646 {
c357e426 647 return;
648 }
38a0206f 649
c357e426 650 // reset material
651 if (NamedStatus & OPENGL_NS_RESMAT)
652 {
653 #if !defined(GL_ES_VERSION_2_0)
654 if (myGlContext->core11 != NULL)
655 {
656 myGlContext->core11->glMaterialfv (aFace, GL_AMBIENT, myMatTmp.Ambient.GetData());
657 myGlContext->core11->glMaterialfv (aFace, GL_DIFFUSE, myMatTmp.Diffuse.GetData());
658 myGlContext->core11->glMaterialfv (aFace, GL_SPECULAR, myMatTmp.Specular.GetData());
659 myGlContext->core11->glMaterialfv (aFace, GL_EMISSION, myMatTmp.Emission.GetData());
660 myGlContext->core11->glMaterialf (aFace, GL_SHININESS, myMatTmp.Shine());
661 }
662 #endif
38a0206f 663
c357e426 664 if (theFlag == TEL_FRONT_MATERIAL)
665 {
666 myMatFront = myMatTmp;
667 myMatBack = myMatTmp;
668 }
669 else
38a0206f 670 {
c357e426 671 myMatBack = myMatTmp;
672 }
38a0206f 673
c357e426 674 NamedStatus &= ~OPENGL_NS_RESMAT;
675 return;
676 }
38a0206f 677
c357e426 678 // reduce updates
679 OpenGl_Material& anOld = (theFlag == TEL_FRONT_MATERIAL)
680 ? myMatFront
681 : myMatBack;
682#if !defined(GL_ES_VERSION_2_0)
683 if (myGlContext->core11 != NULL)
684 {
685 if (myMatTmp.Ambient.r() != anOld.Ambient.r()
686 || myMatTmp.Ambient.g() != anOld.Ambient.g()
687 || myMatTmp.Ambient.b() != anOld.Ambient.b())
688 {
689 myGlContext->core11->glMaterialfv (aFace, GL_AMBIENT, myMatTmp.Ambient.GetData());
38a0206f 690 }
c357e426 691 if (myMatTmp.Diffuse.r() != anOld.Diffuse.r()
692 || myMatTmp.Diffuse.g() != anOld.Diffuse.g()
693 || myMatTmp.Diffuse.b() != anOld.Diffuse.b()
694 || fabs (myMatTmp.Diffuse.a() - anOld.Diffuse.a()) > 0.01f)
38a0206f 695 {
c357e426 696 myGlContext->core11->glMaterialfv (aFace, GL_DIFFUSE, myMatTmp.Diffuse.GetData());
697 }
698 if (myMatTmp.Specular.r() != anOld.Specular.r()
699 || myMatTmp.Specular.g() != anOld.Specular.g()
700 || myMatTmp.Specular.b() != anOld.Specular.b())
701 {
702 myGlContext->core11->glMaterialfv (aFace, GL_SPECULAR, myMatTmp.Specular.GetData());
703 }
704 if (myMatTmp.Emission.r() != anOld.Emission.r()
705 || myMatTmp.Emission.g() != anOld.Emission.g()
706 || myMatTmp.Emission.b() != anOld.Emission.b())
707 {
708 myGlContext->core11->glMaterialfv (aFace, GL_EMISSION, myMatTmp.Emission.GetData());
709 }
710 if (myMatTmp.Shine() != anOld.Shine())
711 {
712 myGlContext->core11->glMaterialf (aFace, GL_SHININESS, myMatTmp.Shine());
38a0206f 713 }
714 }
c357e426 715#endif
716 anOld = myMatTmp;
717 if (aFace == GL_FRONT_AND_BACK)
718 {
719 myMatBack = myMatTmp;
720 }
38a0206f 721}
722
723// =======================================================================
c357e426 724// function : SetAspectLine
38a0206f 725// purpose :
726// =======================================================================
f9ba5c4d 727const OpenGl_AspectLine* OpenGl_Workspace::SetAspectLine (const OpenGl_AspectLine* theAspect)
38a0206f 728{
f9ba5c4d 729 const OpenGl_AspectLine* aPrevAspectLine = myAspectLineSet;
730 myAspectLineSet = theAspect;
731 return aPrevAspectLine;
c357e426 732}
38a0206f 733
c357e426 734// =======================================================================
735// function : SetAspectFace
736// purpose :
737// =======================================================================
f9ba5c4d 738const OpenGl_AspectFace * OpenGl_Workspace::SetAspectFace (const OpenGl_AspectFace* theAspect)
c357e426 739{
f9ba5c4d 740 const OpenGl_AspectFace* aPrevAspectFace = myAspectFaceSet;
741 myAspectFaceSet = theAspect;
742 return aPrevAspectFace;
c357e426 743}
38a0206f 744
c357e426 745// =======================================================================
746// function : SetAspectMarker
747// purpose :
748// =======================================================================
f9ba5c4d 749const OpenGl_AspectMarker* OpenGl_Workspace::SetAspectMarker (const OpenGl_AspectMarker* theAspect)
c357e426 750{
f9ba5c4d 751 const OpenGl_AspectMarker* aPrevAspectMarker = myAspectMarkerSet;
752 myAspectMarkerSet = theAspect;
753 return aPrevAspectMarker;
c357e426 754}
f978241f 755
c357e426 756// =======================================================================
757// function : SetAspectText
758// purpose :
759// =======================================================================
f9ba5c4d 760const OpenGl_AspectText * OpenGl_Workspace::SetAspectText (const OpenGl_AspectText* theAspect)
c357e426 761{
f9ba5c4d 762 const OpenGl_AspectText* aPrevAspectText = myAspectTextSet;
763 myAspectTextSet = theAspect;
764 return aPrevAspectText;
c357e426 765}
38a0206f 766
c357e426 767// =======================================================================
f9ba5c4d 768// function : ApplyAspectFace
c357e426 769// purpose :
770// =======================================================================
f9ba5c4d 771const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
c357e426 772{
c357e426 773 if (myView->BackfacingModel() == Graphic3d_TOBM_AUTOMATIC)
38a0206f 774 {
c357e426 775 // manage back face culling mode, disable culling when clipping is enabled
b6472664 776 bool toSuppressBackFaces = myToAllowFaceCulling
777 && myAspectFaceSet->Aspect()->ToSuppressBackFaces();
778 if (toSuppressBackFaces)
f978241f 779 {
b6472664 780 if (myGlContext->Clipping().IsClippingOrCappingOn()
781 || myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HATCH)
f978241f 782 {
b6472664 783 toSuppressBackFaces = false;
c357e426 784 }
785 }
b6472664 786 if (toSuppressBackFaces)
c357e426 787 {
b6472664 788 if (!(NamedStatus & OPENGL_NS_2NDPASSDO)
789 && (float )myAspectFaceSet->Aspect()->FrontMaterial().Transparency() != 0.0f)
c357e426 790 {
b6472664 791 // disable culling in case of translucent shading aspect
792 toSuppressBackFaces = false;
f978241f 793 }
f978241f 794 }
b6472664 795 myGlContext->SetCullBackFaces (toSuppressBackFaces);
f978241f 796 }
797
b6472664 798 if (myAspectFaceSet->Aspect() == myAspectFaceApplied
f838dac4 799 && !myHighlightStyle.IsNull() == myAspectFaceAppliedWithHL)
a2e4f780 800 {
f9ba5c4d 801 return myAspectFaceSet;
a2e4f780 802 }
f838dac4 803 myAspectFaceAppliedWithHL = !myHighlightStyle.IsNull();
a2e4f780 804
c357e426 805#if !defined(GL_ES_VERSION_2_0)
b6472664 806 const Aspect_InteriorStyle anIntstyle = myAspectFaceSet->Aspect()->InteriorStyle();
807 if (myAspectFaceApplied.IsNull()
f9ba5c4d 808 || myAspectFaceApplied->InteriorStyle() != anIntstyle)
b86bb3df 809 {
c357e426 810 switch (anIntstyle)
760c21c2 811 {
c357e426 812 case Aspect_IS_EMPTY:
813 case Aspect_IS_HOLLOW:
ee51a9fe 814 {
6d0e6be5 815 myGlContext->SetPolygonMode (GL_LINE);
c357e426 816 break;
ee51a9fe 817 }
c357e426 818 case Aspect_IS_HATCH:
62e1beed 819 {
6d0e6be5 820 myGlContext->SetPolygonMode (GL_FILL);
821 myGlContext->SetPolygonHatchEnabled (true);
c357e426 822 break;
62e1beed 823 }
c357e426 824 case Aspect_IS_SOLID:
825 case Aspect_IS_HIDDENLINE:
826 {
6d0e6be5 827 myGlContext->SetPolygonMode (GL_FILL);
828 myGlContext->SetPolygonHatchEnabled (false);
c357e426 829 break;
830 }
831 case Aspect_IS_POINT:
38a0206f 832 {
6d0e6be5 833 myGlContext->SetPolygonMode (GL_POINT);
c357e426 834 break;
38a0206f 835 }
38a0206f 836 }
91c60b57 837 }
e276548b 838
c357e426 839 if (anIntstyle == Aspect_IS_HATCH)
91c60b57 840 {
6d0e6be5 841 myGlContext->SetPolygonHatchStyle (myAspectFaceSet->Aspect()->HatchStyle());
c357e426 842 }
843#endif
38a0206f 844
c357e426 845 // Aspect_POM_None means: do not change current settings
b6472664 846 if ((myAspectFaceSet->Aspect()->PolygonOffset().Mode & Aspect_POM_None) != Aspect_POM_None)
c357e426 847 {
b6472664 848 if (myPolygonOffsetApplied.Mode != myAspectFaceSet->Aspect()->PolygonOffset().Mode
849 || myPolygonOffsetApplied.Factor != myAspectFaceSet->Aspect()->PolygonOffset().Factor
850 || myPolygonOffsetApplied.Units != myAspectFaceSet->Aspect()->PolygonOffset().Units)
38a0206f 851 {
b6472664 852 SetPolygonOffset (myAspectFaceSet->Aspect()->PolygonOffset());
38a0206f 853 }
854 }
c357e426 855
856 updateMaterial (TEL_FRONT_MATERIAL);
b6472664 857 if (myAspectFaceSet->Aspect()->Distinguish())
a174a3c5 858 {
c357e426 859 updateMaterial (TEL_BACK_MATERIAL);
860 }
62e1beed 861
b6472664 862 if (myAspectFaceSet->Aspect()->ToMapTexture())
c357e426 863 {
f9ba5c4d 864 EnableTexture (myAspectFaceSet->TextureRes (myGlContext),
865 myAspectFaceSet->TextureParams());
83da37b1 866 }
867 else
868 {
869 if (!myEnvironmentTexture.IsNull())
38a0206f 870 {
83da37b1 871 EnableTexture (myEnvironmentTexture,
872 myEnvironmentTexture->GetParams());
38a0206f 873 }
c357e426 874 else
62e1beed 875 {
c357e426 876 DisableTexture();
62e1beed 877 }
a174a3c5 878 }
879
b6472664 880 myAspectFaceApplied = myAspectFaceSet->Aspect();
f9ba5c4d 881 return myAspectFaceSet;
c357e426 882}
a174a3c5 883
c357e426 884//=======================================================================
885//function : SetPolygonOffset
886//purpose :
887//=======================================================================
b6472664 888void OpenGl_Workspace::SetPolygonOffset (const Graphic3d_PolygonOffset& theParams)
c357e426 889{
b6472664 890 myPolygonOffsetApplied = theParams;
a174a3c5 891
b6472664 892 if ((theParams.Mode & Aspect_POM_Fill) == Aspect_POM_Fill)
893 {
894 glEnable (GL_POLYGON_OFFSET_FILL);
895 }
896 else
897 {
898 glDisable (GL_POLYGON_OFFSET_FILL);
899 }
900
901#if !defined(GL_ES_VERSION_2_0)
902 if ((theParams.Mode & Aspect_POM_Line) == Aspect_POM_Line)
903 {
904 glEnable (GL_POLYGON_OFFSET_LINE);
905 }
906 else
907 {
908 glDisable (GL_POLYGON_OFFSET_LINE);
909 }
910
911 if ((theParams.Mode & Aspect_POM_Point) == Aspect_POM_Point)
912 {
913 glEnable (GL_POLYGON_OFFSET_POINT);
914 }
915 else
916 {
917 glDisable (GL_POLYGON_OFFSET_POINT);
918 }
919#endif
920 glPolygonOffset (theParams.Factor, theParams.Units);
c357e426 921}
38a0206f 922
c357e426 923// =======================================================================
f9ba5c4d 924// function : ApplyAspectMarker
c357e426 925// purpose :
926// =======================================================================
f9ba5c4d 927const OpenGl_AspectMarker* OpenGl_Workspace::ApplyAspectMarker()
c357e426 928{
b6472664 929 if (myAspectMarkerSet->Aspect() != myAspectMarkerApplied)
ee51a9fe 930 {
b6472664 931 if (myAspectMarkerApplied.IsNull()
932 || (myAspectMarkerSet->Aspect()->Scale() != myAspectMarkerApplied->Scale()))
ee51a9fe 933 {
c357e426 934 #if !defined(GL_ES_VERSION_2_0)
b6472664 935 glPointSize (myAspectMarkerSet->Aspect()->Scale());
c357e426 936 #ifdef HAVE_GL2PS
b6472664 937 gl2psPointSize (myAspectMarkerSet->Aspect()->Scale());
c357e426 938 #endif
939 #endif
ee51a9fe 940 }
b6472664 941 myAspectMarkerApplied = myAspectMarkerSet->Aspect();
38a0206f 942 }
f9ba5c4d 943 return myAspectMarkerSet;
679ecdee 944}
945
946// =======================================================================
c357e426 947// function : Width
679ecdee 948// purpose :
949// =======================================================================
c357e426 950Standard_Integer OpenGl_Workspace::Width() const
679ecdee 951{
c357e426 952 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Width() : 0;
953}
679ecdee 954
c357e426 955// =======================================================================
956// function : Height
957// purpose :
958// =======================================================================
959Standard_Integer OpenGl_Workspace::Height() const
960{
961 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Height() : 0;
962}
679ecdee 963
c357e426 964// =======================================================================
c357e426 965// function : IsCullingEnabled
966// purpose :
967// =======================================================================
968Standard_Boolean OpenGl_Workspace::IsCullingEnabled() const
969{
970 return myView->IsCullingEnabled();
679ecdee 971}
972
973// =======================================================================
c357e426 974// function : FBOCreate
679ecdee 975// purpose :
976// =======================================================================
b128c892 977Handle(OpenGl_FrameBuffer) OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth,
978 const Standard_Integer theHeight)
679ecdee 979{
c357e426 980 // activate OpenGL context
981 if (!Activate())
b128c892 982 return Handle(OpenGl_FrameBuffer)();
c357e426 983
83da37b1 984 DisableTexture();
985
c357e426 986 // create the FBO
987 const Handle(OpenGl_Context)& aCtx = GetGlContext();
b128c892 988 Handle(OpenGl_FrameBuffer) aFrameBuffer = new OpenGl_FrameBuffer();
3c4b62a4 989 if (!aFrameBuffer->Init (aCtx, theWidth, theHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, 0))
679ecdee 990 {
c357e426 991 aFrameBuffer->Release (aCtx.operator->());
b128c892 992 return Handle(OpenGl_FrameBuffer)();
679ecdee 993 }
b128c892 994 return aFrameBuffer;
679ecdee 995}
996
997// =======================================================================
c357e426 998// function : FBORelease
679ecdee 999// purpose :
1000// =======================================================================
b128c892 1001void OpenGl_Workspace::FBORelease (Handle(OpenGl_FrameBuffer)& theFbo)
679ecdee 1002{
c357e426 1003 // activate OpenGL context
1004 if (!Activate()
b128c892 1005 || theFbo.IsNull())
62e1beed 1006 {
62e1beed 1007 return;
1008 }
c357e426 1009
b128c892 1010 theFbo->Release (GetGlContext().operator->());
1011 theFbo.Nullify();
c357e426 1012}
62e1beed 1013
f9f740d6 1014// =======================================================================
1015// function : getAligned
1016// purpose :
1017// =======================================================================
1018inline Standard_Size getAligned (const Standard_Size theNumber,
1019 const Standard_Size theAlignment)
1020{
1021 return theNumber + theAlignment - 1 - (theNumber - 1) % theAlignment;
1022}
1023
1024template<typename T>
1025inline void convertRowFromRgba (T* theRgbRow,
1026 const Image_ColorRGBA* theRgbaRow,
1027 const Standard_Size theWidth)
1028{
1029 for (Standard_Size aCol = 0; aCol < theWidth; ++aCol)
1030 {
1031 const Image_ColorRGBA& anRgba = theRgbaRow[aCol];
1032 T& anRgb = theRgbRow[aCol];
1033 anRgb.r() = anRgba.r();
1034 anRgb.g() = anRgba.g();
1035 anRgb.b() = anRgba.b();
1036 }
1037}
1038
1039// =======================================================================
1040// function : BufferDump
1041// purpose :
1042// =======================================================================
1043Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
1044 Image_PixMap& theImage,
1045 const Graphic3d_BufferType& theBufferType)
c357e426 1046{
f9f740d6 1047 if (theImage.IsEmpty()
1048 || !Activate())
1049 {
1050 return Standard_False;
1051 }
1052
1053 GLenum aFormat = 0;
1054 GLenum aType = 0;
1055 bool toSwapRgbaBgra = false;
1056 bool toConvRgba2Rgb = false;
1057 switch (theImage.Format())
38a0206f 1058 {
c357e426 1059 #if !defined(GL_ES_VERSION_2_0)
1060 case Image_PixMap::ImgGray:
f9f740d6 1061 aFormat = GL_DEPTH_COMPONENT;
1062 aType = GL_UNSIGNED_BYTE;
1063 break;
c357e426 1064 case Image_PixMap::ImgGrayF:
f9f740d6 1065 aFormat = GL_DEPTH_COMPONENT;
1066 aType = GL_FLOAT;
1067 break;
1068 case Image_PixMap::ImgRGB:
1069 aFormat = GL_RGB;
1070 aType = GL_UNSIGNED_BYTE;
1071 break;
c357e426 1072 case Image_PixMap::ImgBGR:
f9f740d6 1073 aFormat = GL_BGR;
1074 aType = GL_UNSIGNED_BYTE;
1075 break;
c357e426 1076 case Image_PixMap::ImgBGRA:
1077 case Image_PixMap::ImgBGR32:
f9f740d6 1078 aFormat = GL_BGRA;
1079 aType = GL_UNSIGNED_BYTE;
1080 break;
c357e426 1081 case Image_PixMap::ImgBGRF:
f9f740d6 1082 aFormat = GL_BGR;
1083 aType = GL_FLOAT;
1084 break;
c357e426 1085 case Image_PixMap::ImgBGRAF:
f9f740d6 1086 aFormat = GL_BGRA;
1087 aType = GL_FLOAT;
1088 break;
c357e426 1089 #else
1090 case Image_PixMap::ImgGray:
1091 case Image_PixMap::ImgGrayF:
c357e426 1092 case Image_PixMap::ImgBGRF:
1093 case Image_PixMap::ImgBGRAF:
f9f740d6 1094 return Standard_False;
1095 case Image_PixMap::ImgBGRA:
1096 case Image_PixMap::ImgBGR32:
1097 aFormat = GL_RGBA;
1098 aType = GL_UNSIGNED_BYTE;
1099 toSwapRgbaBgra = true;
1100 break;
1101 case Image_PixMap::ImgBGR:
c357e426 1102 case Image_PixMap::ImgRGB:
f9f740d6 1103 aFormat = GL_RGBA;
1104 aType = GL_UNSIGNED_BYTE;
1105 toConvRgba2Rgb = true;
1106 break;
1107 #endif
c357e426 1108 case Image_PixMap::ImgRGBA:
1109 case Image_PixMap::ImgRGB32:
f9f740d6 1110 aFormat = GL_RGBA;
1111 aType = GL_UNSIGNED_BYTE;
1112 break;
c357e426 1113 case Image_PixMap::ImgRGBF:
f9f740d6 1114 aFormat = GL_RGB;
1115 aType = GL_FLOAT;
1116 break;
c357e426 1117 case Image_PixMap::ImgRGBAF:
f9f740d6 1118 aFormat = GL_RGBA;
1119 aType = GL_FLOAT;
1120 break;
c357e426 1121 case Image_PixMap::ImgAlpha:
1122 case Image_PixMap::ImgAlphaF:
f9f740d6 1123 return Standard_False; // GL_ALPHA is no more supported in core context
c357e426 1124 case Image_PixMap::ImgUNKNOWN:
f9f740d6 1125 return Standard_False;
38a0206f 1126 }
c357e426 1127
f9f740d6 1128 if (aFormat == 0)
38a0206f 1129 {
c357e426 1130 return Standard_False;
38a0206f 1131 }
f9f740d6 1132
c357e426 1133#if !defined(GL_ES_VERSION_2_0)
1134 GLint aReadBufferPrev = GL_BACK;
1135 if (theBufferType == Graphic3d_BT_Depth
1136 && aFormat != GL_DEPTH_COMPONENT)
38a0206f 1137 {
c357e426 1138 return Standard_False;
1139 }
20aeeb7b 1140#else
1141 (void )theBufferType;
c357e426 1142#endif
38a0206f 1143
c357e426 1144 // bind FBO if used
b128c892 1145 if (!theFbo.IsNull() && theFbo->IsValid())
c357e426 1146 {
b128c892 1147 theFbo->BindBuffer (GetGlContext());
38a0206f 1148 }
1149 else
a2e4f780 1150 {
38a0206f 1151 #if !defined(GL_ES_VERSION_2_0)
c357e426 1152 glGetIntegerv (GL_READ_BUFFER, &aReadBufferPrev);
1153 GLint aDrawBufferPrev = GL_BACK;
1154 glGetIntegerv (GL_DRAW_BUFFER, &aDrawBufferPrev);
1155 glReadBuffer (aDrawBufferPrev);
38a0206f 1156 #endif
a2e4f780 1157 }
1158
c357e426 1159 // setup alignment
1160 const GLint anAligment = Min (GLint(theImage.MaxRowAligmentBytes()), 8); // limit to 8 bytes for OpenGL
1161 glPixelStorei (GL_PACK_ALIGNMENT, anAligment);
1162 bool isBatchCopy = !theImage.IsTopDown();
38a0206f 1163
c357e426 1164 const GLint anExtraBytes = GLint(theImage.RowExtraBytes());
1165 GLint aPixelsWidth = GLint(theImage.SizeRowBytes() / theImage.SizePixelBytes());
1166 Standard_Size aSizeRowBytesEstim = getAligned (theImage.SizePixelBytes() * aPixelsWidth, anAligment);
1167 if (anExtraBytes < anAligment)
b86bb3df 1168 {
c357e426 1169 aPixelsWidth = 0;
ee51a9fe 1170 }
c357e426 1171 else if (aSizeRowBytesEstim != theImage.SizeRowBytes())
ee51a9fe 1172 {
c357e426 1173 aPixelsWidth = 0;
1174 isBatchCopy = false;
ee51a9fe 1175 }
c357e426 1176#if !defined(GL_ES_VERSION_2_0)
1177 glPixelStorei (GL_PACK_ROW_LENGTH, aPixelsWidth);
1178#else
1179 if (aPixelsWidth != 0)
ee51a9fe 1180 {
c357e426 1181 isBatchCopy = false;
b86bb3df 1182 }
c357e426 1183#endif
f9f740d6 1184 if (toConvRgba2Rgb)
1185 {
1186 Handle(NCollection_BaseAllocator) anAlloc = new NCollection_AlignedAllocator (16);
1187 const Standard_Size aRowSize = theImage.SizeX() * 4;
1188 NCollection_Buffer aRowBuffer (anAlloc);
1189 if (!aRowBuffer.Allocate (aRowSize))
1190 {
1191 return Standard_False;
1192 }
c357e426 1193
f9f740d6 1194 for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
1195 {
1196 // Image_PixMap rows indexation always starts from the upper corner
1197 // while order in memory depends on the flag and processed by ChangeRow() method
1198 glReadPixels (0, GLint(theImage.SizeY() - aRow - 1), GLsizei (theImage.SizeX()), 1, aFormat, aType, aRowBuffer.ChangeData());
1199 const Image_ColorRGBA* aRowDataRgba = (const Image_ColorRGBA* )aRowBuffer.Data();
1200 if (theImage.Format() == Image_PixMap::ImgBGR)
1201 {
1202 convertRowFromRgba ((Image_ColorBGR* )theImage.ChangeRow (aRow), aRowDataRgba, theImage.SizeX());
1203 }
1204 else
1205 {
1206 convertRowFromRgba ((Image_ColorRGB* )theImage.ChangeRow (aRow), aRowDataRgba, theImage.SizeX());
1207 }
1208 }
1209 }
1210 else if (!isBatchCopy)
679ecdee 1211 {
c357e426 1212 // copy row by row
1213 for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
679ecdee 1214 {
c357e426 1215 // Image_PixMap rows indexation always starts from the upper corner
1216 // while order in memory depends on the flag and processed by ChangeRow() method
1217 glReadPixels (0, GLint(theImage.SizeY() - aRow - 1), GLsizei (theImage.SizeX()), 1, aFormat, aType, theImage.ChangeRow (aRow));
679ecdee 1218 }
679ecdee 1219 }
1220 else
1221 {
c357e426 1222 glReadPixels (0, 0, GLsizei (theImage.SizeX()), GLsizei (theImage.SizeY()), aFormat, aType, theImage.ChangeData());
679ecdee 1223 }
f9f740d6 1224 const bool hasErrors = myGlContext->ResetErrors (true);
a1954302 1225
c357e426 1226 glPixelStorei (GL_PACK_ALIGNMENT, 1);
eae454e3 1227#if !defined(GL_ES_VERSION_2_0)
c357e426 1228 glPixelStorei (GL_PACK_ROW_LENGTH, 0);
eae454e3 1229#endif
a1954302 1230
b128c892 1231 if (!theFbo.IsNull() && theFbo->IsValid())
c357e426 1232 {
b128c892 1233 theFbo->UnbindBuffer (GetGlContext());
c357e426 1234 }
1235 else
1236 {
1237 #if !defined(GL_ES_VERSION_2_0)
1238 glReadBuffer (aReadBufferPrev);
1239 #endif
1240 }
f9f740d6 1241
1242 if (toSwapRgbaBgra)
1243 {
1244 Image_PixMap::SwapRgbaBgra (theImage);
1245 }
1246
1247 return !hasErrors;
679ecdee 1248}
91c60b57 1249
91c60b57 1250// =======================================================================
1251// function : CanRender
1252// purpose :
1253// =======================================================================
1254Standard_Boolean OpenGl_RaytraceFilter::CanRender (const OpenGl_Element* theElement)
1255{
1256 Standard_Boolean aPrevFilterResult = Standard_True;
1257 if (!myPrevRenderFilter.IsNull())
1258 {
1259 aPrevFilterResult = myPrevRenderFilter->CanRender (theElement);
1260 }
1261 return aPrevFilterResult &&
1262 !OpenGl_Raytrace::IsRaytracedElement (theElement);
1263}