0030931: Visualization, TKOpenGl - do not render into GL_FRONT within "GDI Generic...
[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);
1202160b 149 if (myGlContext->Vendor() == "microsoft corporation"
150 && !myGlContext->IsGlGreaterEqual (1, 2))
151 {
152 // this software implementation causes too slow rendering into GL_FRONT on modern Windows
153 theView->SetImmediateModeDrawToFront (false);
154 }
c357e426 155 #endif
73192b37 156 }
f8ae3605 157
b6472664 158 myNoneCulling .Aspect()->SetSuppressBackFaces (false);
159 myNoneCulling .Aspect()->SetDrawEdges (false);
c40eb6b9 160 myNoneCulling .Aspect()->SetAlphaMode (Graphic3d_AlphaMode_Opaque);
161
b6472664 162 myFrontCulling.Aspect()->SetSuppressBackFaces (true);
163 myFrontCulling.Aspect()->SetDrawEdges (false);
c40eb6b9 164 myFrontCulling.Aspect()->SetAlphaMode (Graphic3d_AlphaMode_Opaque);
2166f0fa
SK
165}
166
2166f0fa
SK
167// =======================================================================
168// function : Activate
169// purpose :
170// =======================================================================
171Standard_Boolean OpenGl_Workspace::Activate()
172{
c357e426 173 if (myWindow.IsNull() || !myWindow->Activate())
174 {
2166f0fa 175 return Standard_False;
c357e426 176 }
2166f0fa 177
2166f0fa
SK
178 ViewMatrix_applied = &myDefaultMatrix;
179 StructureMatrix_applied = &myDefaultMatrix;
26395493 180
181 ResetAppliedAspect();
182
8613985b 183 // reset state for safety
184 myGlContext->BindProgram (Handle(OpenGl_ShaderProgram)());
185 if (myGlContext->core20fwd != NULL)
186 {
187 myGlContext->core20fwd->glUseProgram (OpenGl_ShaderProgram::NO_PROGRAM);
188 }
dd1ae9df 189 if (myGlContext->caps->ffpEnable)
190 {
191 myGlContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
192 }
26395493 193 return Standard_True;
2166f0fa
SK
194}
195
26395493 196//=======================================================================
197//function : ResetAppliedAspect
198//purpose : Sets default values of GL parameters in accordance with default aspects
199//=======================================================================
200void OpenGl_Workspace::ResetAppliedAspect()
201{
4e1523ef 202 myGlContext->BindDefaultVao();
203
f838dac4 204 myHighlightStyle.Nullify();
b6472664 205 myToAllowFaceCulling = false;
bf5f0ca2 206 myAspectsSet = &myDefaultAspects;
207 myAspectsApplied.Nullify();
8d1a539c 208 myGlContext->SetPolygonOffset (Graphic3d_PolygonOffset());
26395493 209
bf5f0ca2 210 ApplyAspects();
211 myGlContext->SetTypeOfLine (myDefaultAspects.Aspect()->LineType());
212 myGlContext->SetLineWidth (myDefaultAspects.Aspect()->LineWidth());
0c33a0bf 213 if (myGlContext->core15fwd != NULL)
214 {
215 myGlContext->core15fwd->glActiveTexture (GL_TEXTURE0);
216 }
26395493 217}
bf75be98 218
a6df1715 219// =======================================================================
220// function : SetDefaultPolygonOffset
221// purpose :
222// =======================================================================
223Graphic3d_PolygonOffset OpenGl_Workspace::SetDefaultPolygonOffset (const Graphic3d_PolygonOffset& theOffset)
224{
bf5f0ca2 225 Graphic3d_PolygonOffset aPrev = myDefaultAspects.Aspect()->PolygonOffset();
226 myDefaultAspects.Aspect()->SetPolygonOffset (theOffset);
227 if (myAspectsApplied == myDefaultAspects.Aspect()
228 || myAspectsApplied.IsNull()
229 || (myAspectsApplied->PolygonOffset().Mode & Aspect_POM_None) == Aspect_POM_None)
a6df1715 230 {
231 myGlContext->SetPolygonOffset (theOffset);
232 }
233 return aPrev;
234}
235
38a0206f 236// =======================================================================
bf5f0ca2 237// function : SetAspects
c357e426 238// purpose :
239// =======================================================================
bf5f0ca2 240const OpenGl_Aspects* OpenGl_Workspace::SetAspects (const OpenGl_Aspects* theAspect)
c357e426 241{
bf5f0ca2 242 const OpenGl_Aspects* aPrevAspects = myAspectsSet;
243 myAspectsSet = theAspect;
244 return aPrevAspects;
c357e426 245}
f978241f 246
c357e426 247// =======================================================================
bf5f0ca2 248// function : ApplyAspects
c357e426 249// purpose :
250// =======================================================================
bf5f0ca2 251const OpenGl_Aspects* OpenGl_Workspace::ApplyAspects()
c357e426 252{
c357e426 253 if (myView->BackfacingModel() == Graphic3d_TOBM_AUTOMATIC)
38a0206f 254 {
b6472664 255 bool toSuppressBackFaces = myToAllowFaceCulling
bf5f0ca2 256 && myAspectsSet->Aspect()->ToSuppressBackFaces();
b6472664 257 if (toSuppressBackFaces)
f978241f 258 {
bf5f0ca2 259 if (myAspectsSet->Aspect()->InteriorStyle() == Aspect_IS_HATCH
260 || myAspectsSet->Aspect()->AlphaMode() == Graphic3d_AlphaMode_Blend
261 || myAspectsSet->Aspect()->AlphaMode() == Graphic3d_AlphaMode_Mask
262 || (myAspectsSet->Aspect()->AlphaMode() == Graphic3d_AlphaMode_BlendAuto
263 && myAspectsSet->Aspect()->FrontMaterial().Transparency() != 0.0f))
c357e426 264 {
b6472664 265 // disable culling in case of translucent shading aspect
266 toSuppressBackFaces = false;
f978241f 267 }
f978241f 268 }
b6472664 269 myGlContext->SetCullBackFaces (toSuppressBackFaces);
f978241f 270 }
271
bf5f0ca2 272 if (myAspectsSet->Aspect() == myAspectsApplied
8613985b 273 && myHighlightStyle == myAspectFaceAppliedWithHL)
a2e4f780 274 {
bf5f0ca2 275 return myAspectsSet;
a2e4f780 276 }
8613985b 277 myAspectFaceAppliedWithHL = myHighlightStyle;
a2e4f780 278
2a332745 279 // Aspect_POM_None means: do not change current settings
bf5f0ca2 280 if ((myAspectsSet->Aspect()->PolygonOffset().Mode & Aspect_POM_None) != Aspect_POM_None)
2a332745 281 {
bf5f0ca2 282 myGlContext->SetPolygonOffset (myAspectsSet->Aspect()->PolygonOffset());
2a332745 283 }
284
bf5f0ca2 285 const Aspect_InteriorStyle anIntstyle = myAspectsSet->Aspect()->InteriorStyle();
286 if (myAspectsApplied.IsNull()
287 || myAspectsApplied->InteriorStyle() != anIntstyle)
b86bb3df 288 {
2a332745 289 #if !defined(GL_ES_VERSION_2_0)
290 myGlContext->SetPolygonMode (anIntstyle == Aspect_IS_POINT ? GL_POINT : GL_FILL);
291 myGlContext->SetPolygonHatchEnabled (anIntstyle == Aspect_IS_HATCH);
292 #endif
91c60b57 293 }
e276548b 294
2a332745 295#if !defined(GL_ES_VERSION_2_0)
c357e426 296 if (anIntstyle == Aspect_IS_HATCH)
91c60b57 297 {
bf5f0ca2 298 myGlContext->SetPolygonHatchStyle (myAspectsSet->Aspect()->HatchStyle());
c357e426 299 }
300#endif
38a0206f 301
8613985b 302 // Case of hidden line
2a332745 303 if (anIntstyle == Aspect_IS_HIDDENLINE)
8613985b 304 {
305 // copy all values including line edge aspect
bf5f0ca2 306 *myAspectFaceHl.Aspect() = *myAspectsSet->Aspect();
dc89236f 307 myAspectFaceHl.Aspect()->SetShadingModel (Graphic3d_TOSM_UNLIT);
8613985b 308 myAspectFaceHl.Aspect()->SetInteriorColor (myView->BackgroundColor().GetRGB());
2a332745 309 myAspectFaceHl.Aspect()->SetDistinguish (false);
dc89236f 310 myAspectFaceHl.SetNoLighting();
bf5f0ca2 311 myAspectsSet = &myAspectFaceHl;
8613985b 312 }
313 else
a174a3c5 314 {
bf5f0ca2 315 myGlContext->SetShadingMaterial (myAspectsSet, myHighlightStyle);
c357e426 316 }
62e1beed 317
737e9a8d 318 const Handle(OpenGl_TextureSet)& aTextureSet = myAspectsSet->TextureSet (myGlContext, ToHighlight());
319 if (!aTextureSet.IsNull()
320 || myAspectsSet->Aspect()->ToMapTexture())
c357e426 321 {
737e9a8d 322 myGlContext->BindTextures (aTextureSet);
83da37b1 323 }
324 else
325 {
cc8cbabe 326 myGlContext->BindTextures (myEnvironmentTexture);
a174a3c5 327 }
328
bf5f0ca2 329 myAspectsApplied = myAspectsSet->Aspect();
330 return myAspectsSet;
679ecdee 331}
332
333// =======================================================================
c357e426 334// function : Width
679ecdee 335// purpose :
336// =======================================================================
c357e426 337Standard_Integer OpenGl_Workspace::Width() const
679ecdee 338{
c357e426 339 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Width() : 0;
340}
679ecdee 341
c357e426 342// =======================================================================
343// function : Height
344// purpose :
345// =======================================================================
346Standard_Integer OpenGl_Workspace::Height() const
347{
348 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Height() : 0;
349}
679ecdee 350
679ecdee 351// =======================================================================
c357e426 352// function : FBOCreate
679ecdee 353// purpose :
354// =======================================================================
b128c892 355Handle(OpenGl_FrameBuffer) OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth,
356 const Standard_Integer theHeight)
679ecdee 357{
c357e426 358 // activate OpenGL context
359 if (!Activate())
b128c892 360 return Handle(OpenGl_FrameBuffer)();
c357e426 361
362 // create the FBO
363 const Handle(OpenGl_Context)& aCtx = GetGlContext();
cc8cbabe 364 aCtx->BindTextures (Handle(OpenGl_TextureSet)());
b128c892 365 Handle(OpenGl_FrameBuffer) aFrameBuffer = new OpenGl_FrameBuffer();
3c4b62a4 366 if (!aFrameBuffer->Init (aCtx, theWidth, theHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, 0))
679ecdee 367 {
c357e426 368 aFrameBuffer->Release (aCtx.operator->());
b128c892 369 return Handle(OpenGl_FrameBuffer)();
679ecdee 370 }
b128c892 371 return aFrameBuffer;
679ecdee 372}
373
374// =======================================================================
c357e426 375// function : FBORelease
679ecdee 376// purpose :
377// =======================================================================
b128c892 378void OpenGl_Workspace::FBORelease (Handle(OpenGl_FrameBuffer)& theFbo)
679ecdee 379{
c357e426 380 // activate OpenGL context
381 if (!Activate()
b128c892 382 || theFbo.IsNull())
62e1beed 383 {
62e1beed 384 return;
385 }
c357e426 386
b128c892 387 theFbo->Release (GetGlContext().operator->());
388 theFbo.Nullify();
c357e426 389}
62e1beed 390
f9f740d6 391// =======================================================================
392// function : BufferDump
393// purpose :
394// =======================================================================
395Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
396 Image_PixMap& theImage,
397 const Graphic3d_BufferType& theBufferType)
c357e426 398{
6cde53c4 399 return !theImage.IsEmpty()
400 && Activate()
401 && OpenGl_FrameBuffer::BufferDump (GetGlContext(), theFbo, theImage, theBufferType);
679ecdee 402}
91c60b57 403
91c60b57 404// =======================================================================
a1073ae2 405// function : ShouldRender
91c60b57 406// purpose :
407// =======================================================================
1b661a81 408bool OpenGl_Workspace::ShouldRender (const OpenGl_Element* theElement)
91c60b57 409{
1b661a81 410 // render only non-raytracable elements when RayTracing is enabled
411 if ((myRenderFilter & OpenGl_RenderFilter_NonRaytraceableOnly) != 0)
412 {
413 if (OpenGl_Raytrace::IsRaytracedElement (theElement))
414 {
415 return false;
416 }
417 }
418 else if ((myRenderFilter & OpenGl_RenderFilter_FillModeOnly) != 0)
419 {
420 if (!theElement->IsFillDrawMode())
421 {
422 return false;
423 }
424 }
425
426 // handle opaque/transparency render passes
427 if ((myRenderFilter & OpenGl_RenderFilter_OpaqueOnly) != 0)
428 {
429 if (!theElement->IsFillDrawMode())
430 {
431 return true;
432 }
433
bf5f0ca2 434 if (OpenGl_Context::CheckIsTransparent (myAspectsSet, myHighlightStyle))
1b661a81 435 {
436 ++myNbSkippedTranspElems;
437 return false;
438 }
439 }
440 else if ((myRenderFilter & OpenGl_RenderFilter_TransparentOnly) != 0)
91c60b57 441 {
1b661a81 442 if (!theElement->IsFillDrawMode())
443 {
bf5f0ca2 444 if (dynamic_cast<const OpenGl_Aspects*> (theElement) == NULL)
1b661a81 445 {
446 return false;
447 }
448 }
bf5f0ca2 449 else if (!OpenGl_Context::CheckIsTransparent (myAspectsSet, myHighlightStyle))
1b661a81 450 {
451 return false;
452 }
91c60b57 453 }
1b661a81 454 return true;
91c60b57 455}