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