0030748: Visualization - Marker displayed in immediate layer ruins QT Quick view...
[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>
bf5f0ca2 19#include <OpenGl_Aspects.hxx>
bf75be98 20#include <OpenGl_Context.hxx>
679ecdee 21#include <OpenGl_Element.hxx>
a174a3c5 22#include <OpenGl_FrameBuffer.hxx>
c357e426 23#include <OpenGl_GlCore15.hxx>
24#include <OpenGl_SceneGeometry.hxx>
679ecdee 25#include <OpenGl_Structure.hxx>
25ef750e 26#include <OpenGl_Sampler.hxx>
8613985b 27#include <OpenGl_ShaderManager.hxx>
bf75be98 28#include <OpenGl_Texture.hxx>
e276548b 29#include <OpenGl_View.hxx>
c357e426 30#include <OpenGl_Window.hxx>
2166f0fa 31
bf75be98 32#include <Graphic3d_TextureParams.hxx>
825aa485 33#include <Graphic3d_TransformUtils.hxx>
f9f740d6 34#include <NCollection_AlignedAllocator.hxx>
2166f0fa 35
92efcf78 36IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
92efcf78 37
2166f0fa
SK
38namespace
39{
b6472664 40 static const OpenGl_Vec4 THE_WHITE_COLOR (1.0f, 1.0f, 1.0f, 1.0f);
41 static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f);
2166f0fa 42
2166f0fa
SK
43 static const OpenGl_Matrix myDefaultMatrix =
44 {
45 {{ 1.0F, 0.0F, 0.0F, 0.0F },
46 { 0.0F, 1.0F, 0.0F, 0.0F },
47 { 0.0F, 0.0F, 1.0F, 0.0F },
48 { 0.0F, 0.0F, 0.0F, 1.0F }}
49 };
bf75be98 50
a3f6f591 51}
2166f0fa 52
0adbd30f 53// =======================================================================
54// function : Init
55// purpose :
56// =======================================================================
b6472664 57void OpenGl_Material::Init (const Graphic3d_MaterialAspect& theMat,
58 const Quantity_Color& theInteriorColor)
0adbd30f 59{
4e1bc39a 60 const bool isPhysic = theMat.MaterialType (Graphic3d_MATERIAL_PHYSIC);
a71a71de 61 ChangeShine() = 128.0f * theMat.Shininess();
62 ChangeTransparency() = theMat.Alpha();
b6472664 63
0adbd30f 64 // ambient component
b6472664 65 if (theMat.ReflectionMode (Graphic3d_TOR_AMBIENT))
0adbd30f 66 {
b6472664 67 const OpenGl_Vec3& aSrcAmb = isPhysic ? theMat.AmbientColor() : theInteriorColor;
4e1bc39a 68 Ambient = OpenGl_Vec4 (aSrcAmb * theMat.Ambient(), 1.0f);
0adbd30f 69 }
70 else
71 {
72 Ambient = THE_BLACK_COLOR;
73 }
74
75 // diffusion component
b6472664 76 if (theMat.ReflectionMode (Graphic3d_TOR_DIFFUSE))
0adbd30f 77 {
b6472664 78 const OpenGl_Vec3& aSrcDif = isPhysic ? theMat.DiffuseColor() : theInteriorColor;
4e1bc39a 79 Diffuse = OpenGl_Vec4 (aSrcDif * theMat.Diffuse(), 1.0f);
0adbd30f 80 }
81 else
82 {
83 Diffuse = THE_BLACK_COLOR;
84 }
85
86 // specular component
b6472664 87 if (theMat.ReflectionMode (Graphic3d_TOR_SPECULAR))
0adbd30f 88 {
b6472664 89 const OpenGl_Vec3& aSrcSpe = isPhysic ? (const OpenGl_Vec3& )theMat.SpecularColor() : THE_WHITE_COLOR.rgb();
4e1bc39a 90 Specular = OpenGl_Vec4 (aSrcSpe * theMat.Specular(), 1.0f);
0adbd30f 91 }
92 else
93 {
94 Specular = THE_BLACK_COLOR;
95 }
96
97 // emission component
b6472664 98 if (theMat.ReflectionMode (Graphic3d_TOR_EMISSION))
0adbd30f 99 {
b6472664 100 const OpenGl_Vec3& aSrcEms = isPhysic ? theMat.EmissiveColor() : theInteriorColor;
4e1bc39a 101 Emission = OpenGl_Vec4 (aSrcEms * theMat.Emissive(), 1.0f);
0adbd30f 102 }
103 else
104 {
105 Emission = THE_BLACK_COLOR;
106 }
0adbd30f 107}
108
2166f0fa
SK
109// =======================================================================
110// function : OpenGl_Workspace
111// purpose :
112// =======================================================================
c357e426 113OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow)
1a0dfc1b 114: myView (theView),
c357e426 115 myWindow (theWindow),
116 myGlContext (!theWindow.IsNull() ? theWindow->GetGlContext() : NULL),
eae454e3 117 myUseZBuffer (Standard_True),
118 myUseDepthWrite (Standard_True),
2166f0fa 119 //
1b661a81 120 myNbSkippedTranspElems (0),
121 myRenderFilter (OpenGl_RenderFilter_Empty),
122 //
bf5f0ca2 123 myAspectsSet (&myDefaultAspects),
f9ba5c4d 124 //
2166f0fa
SK
125 ViewMatrix_applied (&myDefaultMatrix),
126 StructureMatrix_applied (&myDefaultMatrix),
b6472664 127 myToAllowFaceCulling (false),
b6472664 128 myModelViewMatrix (myDefaultMatrix)
2166f0fa 129{
c357e426 130 if (!myGlContext.IsNull() && myGlContext->MakeCurrent())
73192b37 131 {
c357e426 132 myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
2166f0fa 133
c357e426 134 // General initialization of the context
c357e426 135 #if !defined(GL_ES_VERSION_2_0)
136 if (myGlContext->core11 != NULL)
137 {
dd1ae9df 138 // enable two-side lighting by default
c357e426 139 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
dd1ae9df 140 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
141 if (myGlContext->caps->ffpEnable)
142 {
143 glHint (GL_FOG_HINT, GL_FASTEST);
144 }
c357e426 145 }
2166f0fa 146
c357e426 147 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
148 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
149 #endif
73192b37 150 }
f8ae3605 151
b6472664 152 myNoneCulling .Aspect()->SetSuppressBackFaces (false);
153 myNoneCulling .Aspect()->SetDrawEdges (false);
c40eb6b9 154 myNoneCulling .Aspect()->SetAlphaMode (Graphic3d_AlphaMode_Opaque);
155
b6472664 156 myFrontCulling.Aspect()->SetSuppressBackFaces (true);
157 myFrontCulling.Aspect()->SetDrawEdges (false);
c40eb6b9 158 myFrontCulling.Aspect()->SetAlphaMode (Graphic3d_AlphaMode_Opaque);
2166f0fa
SK
159}
160
2166f0fa
SK
161// =======================================================================
162// function : Activate
163// purpose :
164// =======================================================================
165Standard_Boolean OpenGl_Workspace::Activate()
166{
c357e426 167 if (myWindow.IsNull() || !myWindow->Activate())
168 {
2166f0fa 169 return Standard_False;
c357e426 170 }
2166f0fa 171
2166f0fa
SK
172 ViewMatrix_applied = &myDefaultMatrix;
173 StructureMatrix_applied = &myDefaultMatrix;
26395493 174
175 ResetAppliedAspect();
176
8613985b 177 // reset state for safety
178 myGlContext->BindProgram (Handle(OpenGl_ShaderProgram)());
179 if (myGlContext->core20fwd != NULL)
180 {
181 myGlContext->core20fwd->glUseProgram (OpenGl_ShaderProgram::NO_PROGRAM);
182 }
dd1ae9df 183 if (myGlContext->caps->ffpEnable)
184 {
185 myGlContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
186 }
26395493 187 return Standard_True;
2166f0fa
SK
188}
189
26395493 190//=======================================================================
191//function : ResetAppliedAspect
192//purpose : Sets default values of GL parameters in accordance with default aspects
193//=======================================================================
194void OpenGl_Workspace::ResetAppliedAspect()
195{
4e1523ef 196 myGlContext->BindDefaultVao();
197
f838dac4 198 myHighlightStyle.Nullify();
b6472664 199 myToAllowFaceCulling = false;
bf5f0ca2 200 myAspectsSet = &myDefaultAspects;
201 myAspectsApplied.Nullify();
8d1a539c 202 myGlContext->SetPolygonOffset (Graphic3d_PolygonOffset());
26395493 203
bf5f0ca2 204 ApplyAspects();
205 myGlContext->SetTypeOfLine (myDefaultAspects.Aspect()->LineType());
206 myGlContext->SetLineWidth (myDefaultAspects.Aspect()->LineWidth());
0c33a0bf 207 if (myGlContext->core15fwd != NULL)
208 {
209 myGlContext->core15fwd->glActiveTexture (GL_TEXTURE0);
210 }
26395493 211}
bf75be98 212
a6df1715 213// =======================================================================
214// function : SetDefaultPolygonOffset
215// purpose :
216// =======================================================================
217Graphic3d_PolygonOffset OpenGl_Workspace::SetDefaultPolygonOffset (const Graphic3d_PolygonOffset& theOffset)
218{
bf5f0ca2 219 Graphic3d_PolygonOffset aPrev = myDefaultAspects.Aspect()->PolygonOffset();
220 myDefaultAspects.Aspect()->SetPolygonOffset (theOffset);
221 if (myAspectsApplied == myDefaultAspects.Aspect()
222 || myAspectsApplied.IsNull()
223 || (myAspectsApplied->PolygonOffset().Mode & Aspect_POM_None) == Aspect_POM_None)
a6df1715 224 {
225 myGlContext->SetPolygonOffset (theOffset);
226 }
227 return aPrev;
228}
229
38a0206f 230// =======================================================================
bf5f0ca2 231// function : SetAspects
c357e426 232// purpose :
233// =======================================================================
bf5f0ca2 234const OpenGl_Aspects* OpenGl_Workspace::SetAspects (const OpenGl_Aspects* theAspect)
c357e426 235{
bf5f0ca2 236 const OpenGl_Aspects* aPrevAspects = myAspectsSet;
237 myAspectsSet = theAspect;
238 return aPrevAspects;
c357e426 239}
f978241f 240
c357e426 241// =======================================================================
bf5f0ca2 242// function : ApplyAspects
c357e426 243// purpose :
244// =======================================================================
bf5f0ca2 245const OpenGl_Aspects* OpenGl_Workspace::ApplyAspects()
c357e426 246{
c357e426 247 if (myView->BackfacingModel() == Graphic3d_TOBM_AUTOMATIC)
38a0206f 248 {
b6472664 249 bool toSuppressBackFaces = myToAllowFaceCulling
bf5f0ca2 250 && myAspectsSet->Aspect()->ToSuppressBackFaces();
b6472664 251 if (toSuppressBackFaces)
f978241f 252 {
bf5f0ca2 253 if (myAspectsSet->Aspect()->InteriorStyle() == Aspect_IS_HATCH
254 || myAspectsSet->Aspect()->AlphaMode() == Graphic3d_AlphaMode_Blend
255 || myAspectsSet->Aspect()->AlphaMode() == Graphic3d_AlphaMode_Mask
256 || (myAspectsSet->Aspect()->AlphaMode() == Graphic3d_AlphaMode_BlendAuto
257 && myAspectsSet->Aspect()->FrontMaterial().Transparency() != 0.0f))
c357e426 258 {
b6472664 259 // disable culling in case of translucent shading aspect
260 toSuppressBackFaces = false;
f978241f 261 }
f978241f 262 }
b6472664 263 myGlContext->SetCullBackFaces (toSuppressBackFaces);
f978241f 264 }
265
bf5f0ca2 266 if (myAspectsSet->Aspect() == myAspectsApplied
8613985b 267 && myHighlightStyle == myAspectFaceAppliedWithHL)
a2e4f780 268 {
bf5f0ca2 269 return myAspectsSet;
a2e4f780 270 }
8613985b 271 myAspectFaceAppliedWithHL = myHighlightStyle;
a2e4f780 272
2a332745 273 // Aspect_POM_None means: do not change current settings
bf5f0ca2 274 if ((myAspectsSet->Aspect()->PolygonOffset().Mode & Aspect_POM_None) != Aspect_POM_None)
2a332745 275 {
bf5f0ca2 276 myGlContext->SetPolygonOffset (myAspectsSet->Aspect()->PolygonOffset());
2a332745 277 }
278
bf5f0ca2 279 const Aspect_InteriorStyle anIntstyle = myAspectsSet->Aspect()->InteriorStyle();
280 if (myAspectsApplied.IsNull()
281 || myAspectsApplied->InteriorStyle() != anIntstyle)
b86bb3df 282 {
2a332745 283 #if !defined(GL_ES_VERSION_2_0)
284 myGlContext->SetPolygonMode (anIntstyle == Aspect_IS_POINT ? GL_POINT : GL_FILL);
285 myGlContext->SetPolygonHatchEnabled (anIntstyle == Aspect_IS_HATCH);
286 #endif
91c60b57 287 }
e276548b 288
2a332745 289#if !defined(GL_ES_VERSION_2_0)
c357e426 290 if (anIntstyle == Aspect_IS_HATCH)
91c60b57 291 {
bf5f0ca2 292 myGlContext->SetPolygonHatchStyle (myAspectsSet->Aspect()->HatchStyle());
c357e426 293 }
294#endif
38a0206f 295
8613985b 296 // Case of hidden line
2a332745 297 if (anIntstyle == Aspect_IS_HIDDENLINE)
8613985b 298 {
299 // copy all values including line edge aspect
bf5f0ca2 300 *myAspectFaceHl.Aspect() = *myAspectsSet->Aspect();
dc89236f 301 myAspectFaceHl.Aspect()->SetShadingModel (Graphic3d_TOSM_UNLIT);
8613985b 302 myAspectFaceHl.Aspect()->SetInteriorColor (myView->BackgroundColor().GetRGB());
2a332745 303 myAspectFaceHl.Aspect()->SetDistinguish (false);
dc89236f 304 myAspectFaceHl.SetNoLighting();
bf5f0ca2 305 myAspectsSet = &myAspectFaceHl;
8613985b 306 }
307 else
a174a3c5 308 {
bf5f0ca2 309 myGlContext->SetShadingMaterial (myAspectsSet, myHighlightStyle);
c357e426 310 }
62e1beed 311
737e9a8d 312 const Handle(OpenGl_TextureSet)& aTextureSet = myAspectsSet->TextureSet (myGlContext, ToHighlight());
313 if (!aTextureSet.IsNull()
314 || myAspectsSet->Aspect()->ToMapTexture())
c357e426 315 {
737e9a8d 316 myGlContext->BindTextures (aTextureSet);
83da37b1 317 }
318 else
319 {
cc8cbabe 320 myGlContext->BindTextures (myEnvironmentTexture);
a174a3c5 321 }
322
bf5f0ca2 323 myAspectsApplied = myAspectsSet->Aspect();
324 return myAspectsSet;
679ecdee 325}
326
327// =======================================================================
c357e426 328// function : Width
679ecdee 329// purpose :
330// =======================================================================
c357e426 331Standard_Integer OpenGl_Workspace::Width() const
679ecdee 332{
c357e426 333 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Width() : 0;
334}
679ecdee 335
c357e426 336// =======================================================================
337// function : Height
338// purpose :
339// =======================================================================
340Standard_Integer OpenGl_Workspace::Height() const
341{
342 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Height() : 0;
343}
679ecdee 344
679ecdee 345// =======================================================================
c357e426 346// function : FBOCreate
679ecdee 347// purpose :
348// =======================================================================
b128c892 349Handle(OpenGl_FrameBuffer) OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth,
350 const Standard_Integer theHeight)
679ecdee 351{
c357e426 352 // activate OpenGL context
353 if (!Activate())
b128c892 354 return Handle(OpenGl_FrameBuffer)();
c357e426 355
356 // create the FBO
357 const Handle(OpenGl_Context)& aCtx = GetGlContext();
cc8cbabe 358 aCtx->BindTextures (Handle(OpenGl_TextureSet)());
b128c892 359 Handle(OpenGl_FrameBuffer) aFrameBuffer = new OpenGl_FrameBuffer();
3c4b62a4 360 if (!aFrameBuffer->Init (aCtx, theWidth, theHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, 0))
679ecdee 361 {
c357e426 362 aFrameBuffer->Release (aCtx.operator->());
b128c892 363 return Handle(OpenGl_FrameBuffer)();
679ecdee 364 }
b128c892 365 return aFrameBuffer;
679ecdee 366}
367
368// =======================================================================
c357e426 369// function : FBORelease
679ecdee 370// purpose :
371// =======================================================================
b128c892 372void OpenGl_Workspace::FBORelease (Handle(OpenGl_FrameBuffer)& theFbo)
679ecdee 373{
c357e426 374 // activate OpenGL context
375 if (!Activate()
b128c892 376 || theFbo.IsNull())
62e1beed 377 {
62e1beed 378 return;
379 }
c357e426 380
b128c892 381 theFbo->Release (GetGlContext().operator->());
382 theFbo.Nullify();
c357e426 383}
62e1beed 384
f9f740d6 385// =======================================================================
386// function : BufferDump
387// purpose :
388// =======================================================================
389Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
390 Image_PixMap& theImage,
391 const Graphic3d_BufferType& theBufferType)
c357e426 392{
6cde53c4 393 return !theImage.IsEmpty()
394 && Activate()
395 && OpenGl_FrameBuffer::BufferDump (GetGlContext(), theFbo, theImage, theBufferType);
679ecdee 396}
91c60b57 397
91c60b57 398// =======================================================================
a1073ae2 399// function : ShouldRender
91c60b57 400// purpose :
401// =======================================================================
1b661a81 402bool OpenGl_Workspace::ShouldRender (const OpenGl_Element* theElement)
91c60b57 403{
1b661a81 404 // render only non-raytracable elements when RayTracing is enabled
405 if ((myRenderFilter & OpenGl_RenderFilter_NonRaytraceableOnly) != 0)
406 {
407 if (OpenGl_Raytrace::IsRaytracedElement (theElement))
408 {
409 return false;
410 }
411 }
412 else if ((myRenderFilter & OpenGl_RenderFilter_FillModeOnly) != 0)
413 {
414 if (!theElement->IsFillDrawMode())
415 {
416 return false;
417 }
418 }
419
420 // handle opaque/transparency render passes
421 if ((myRenderFilter & OpenGl_RenderFilter_OpaqueOnly) != 0)
422 {
423 if (!theElement->IsFillDrawMode())
424 {
425 return true;
426 }
427
bf5f0ca2 428 if (OpenGl_Context::CheckIsTransparent (myAspectsSet, myHighlightStyle))
1b661a81 429 {
430 ++myNbSkippedTranspElems;
431 return false;
432 }
433 }
434 else if ((myRenderFilter & OpenGl_RenderFilter_TransparentOnly) != 0)
91c60b57 435 {
1b661a81 436 if (!theElement->IsFillDrawMode())
437 {
bf5f0ca2 438 if (dynamic_cast<const OpenGl_Aspects*> (theElement) == NULL)
1b661a81 439 {
440 return false;
441 }
442 }
bf5f0ca2 443 else if (!OpenGl_Context::CheckIsTransparent (myAspectsSet, myHighlightStyle))
1b661a81 444 {
445 return false;
446 }
91c60b57 447 }
1b661a81 448 return true;
91c60b57 449}