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