0029874: Visualization - capping plane transparency does not work
[occt.git] / src / OpenGl / OpenGl_ShaderManager.hxx
CommitLineData
30f0ad28 1// Created on: 2013-09-26
2// Created by: Denis BOGOLEPOV
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
30f0ad28 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
30f0ad28 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.
30f0ad28 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
30f0ad28 15
16#ifndef _OpenGl_ShaderManager_HeaderFile
17#define _OpenGl_ShaderManager_HeaderFile
18
494782f6 19#include <Graphic3d_ShaderProgram.hxx>
f978241f 20#include <Graphic3d_StereoMode.hxx>
30f0ad28 21
22#include <NCollection_DataMap.hxx>
23#include <NCollection_Sequence.hxx>
24
256f9ac0 25#include <OpenGl_SetOfShaderPrograms.hxx>
30f0ad28 26#include <OpenGl_ShaderStates.hxx>
8625ef7e 27#include <OpenGl_AspectFace.hxx>
28#include <OpenGl_AspectLine.hxx>
29#include <OpenGl_AspectText.hxx>
30#include <OpenGl_AspectMarker.hxx>
8613985b 31#include <OpenGl_MaterialState.hxx>
8625ef7e 32#include <OpenGl_Texture.hxx>
c357e426 33
e6804ff7 34class OpenGl_View;
35
30f0ad28 36//! List of shader programs.
37typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
38
30f0ad28 39//! This class is responsible for managing shader programs.
40class OpenGl_ShaderManager : public Standard_Transient
41{
8613985b 42 DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderManager, Standard_Transient)
30f0ad28 43 friend class OpenGl_ShaderProgram;
30f0ad28 44public:
45
46 //! Creates new empty shader manager.
47 Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
48
49 //! Releases resources of shader manager.
50 Standard_EXPORT virtual ~OpenGl_ShaderManager();
51
05e2200b 52 //! Release all resources.
53 Standard_EXPORT void clear();
54
7c3ef2f7 55 //! Return local camera transformation.
56 const gp_XYZ& LocalOrigin() const { return myLocalOrigin; }
57
58 //! Setup local camera transformation for compensating float precision issues.
59 void SetLocalOrigin (const gp_XYZ& theOrigin)
60 {
61 myLocalOrigin = theOrigin;
62 myHasLocalOrigin = !theOrigin.IsEqual (gp_XYZ(0.0, 0.0, 0.0), gp::Resolution());
63 }
64
392ac980 65 //! Creates new shader program or re-use shared instance.
66 //! @param theProxy [IN] program definition
67 //! @param theShareKey [OUT] sharing key
68 //! @param theProgram [OUT] OpenGL program
8625ef7e 69 //! @return true on success
70 Standard_EXPORT Standard_Boolean Create (const Handle(Graphic3d_ShaderProgram)& theProxy,
71 TCollection_AsciiString& theShareKey,
72 Handle(OpenGl_ShaderProgram)& theProgram);
30f0ad28 73
74 //! Unregisters specified shader program.
392ac980 75 Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey,
76 Handle(OpenGl_ShaderProgram)& theProgram);
30f0ad28 77
78 //! Returns list of registered shader programs.
79 Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
80
81 //! Returns true if no program objects are registered in the manager.
82 Standard_EXPORT Standard_Boolean IsEmpty() const;
83
8625ef7e 84 //! Bind program for filled primitives rendering
cc8cbabe 85 Standard_Boolean BindFaceProgram (const Handle(OpenGl_TextureSet)& theTextures,
dc89236f 86 const Graphic3d_TypeOfShadingModel theShadingModel,
c40eb6b9 87 const Graphic3d_AlphaMode theAlphaMode,
299e0ab9 88 const Standard_Boolean theHasVertColor,
89 const Standard_Boolean theEnableEnvMap,
90 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
8625ef7e 91 {
92 if (!theCustomProgram.IsNull()
93 || myContext->caps->ffpEnable)
94 {
299e0ab9 95 return bindProgramWithState (theCustomProgram);
8625ef7e 96 }
97
dc89236f 98 const Graphic3d_TypeOfShadingModel aShadeModelOnFace = theShadingModel != Graphic3d_TOSM_UNLIT
99 && (theTextures.IsNull() || theTextures->IsModulate())
100 ? theShadingModel
101 : Graphic3d_TOSM_UNLIT;
c40eb6b9 102 const Standard_Integer aBits = getProgramBits (theTextures, theAlphaMode, theHasVertColor, theEnableEnvMap);
dc89236f 103 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (aShadeModelOnFace, aBits);
299e0ab9 104 return bindProgramWithState (aProgram);
8625ef7e 105 }
30f0ad28 106
8625ef7e 107 //! Bind program for line rendering
cc8cbabe 108 Standard_Boolean BindLineProgram (const Handle(OpenGl_TextureSet)& theTextures,
dc89236f 109 const Aspect_TypeOfLine theLineType,
110 const Graphic3d_TypeOfShadingModel theShadingModel,
c40eb6b9 111 const Graphic3d_AlphaMode theAlphaMode,
299e0ab9 112 const Standard_Boolean theHasVertColor,
113 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
8625ef7e 114 {
115 if (!theCustomProgram.IsNull()
116 || myContext->caps->ffpEnable)
117 {
299e0ab9 118 return bindProgramWithState (theCustomProgram);
8625ef7e 119 }
120
c40eb6b9 121 Standard_Integer aBits = getProgramBits (theTextures, theAlphaMode, theHasVertColor, false);
dc89236f 122 if (theLineType != Aspect_TOL_SOLID)
ac116c22 123 {
124 aBits |= OpenGl_PO_StippleLine;
125 }
126
dc89236f 127 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theShadingModel, aBits);
299e0ab9 128 return bindProgramWithState (aProgram);
8625ef7e 129 }
30f0ad28 130
8625ef7e 131 //! Bind program for point rendering
cc8cbabe 132 Standard_Boolean BindMarkerProgram (const Handle(OpenGl_TextureSet)& theTextures,
dc89236f 133 const Graphic3d_TypeOfShadingModel theShadingModel,
c40eb6b9 134 const Graphic3d_AlphaMode theAlphaMode,
299e0ab9 135 const Standard_Boolean theHasVertColor,
136 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
8625ef7e 137 {
138 if (!theCustomProgram.IsNull()
139 || myContext->caps->ffpEnable)
140 {
299e0ab9 141 return bindProgramWithState (theCustomProgram);
8625ef7e 142 }
143
c40eb6b9 144 const Standard_Integer aBits = getProgramBits (theTextures, theAlphaMode, theHasVertColor, false) | OpenGl_PO_Point;
dc89236f 145 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theShadingModel, aBits);
299e0ab9 146 return bindProgramWithState (aProgram);
8625ef7e 147 }
148
149 //! Bind program for rendering alpha-textured font.
299e0ab9 150 Standard_Boolean BindFontProgram (const Handle(OpenGl_ShaderProgram)& theCustomProgram)
8625ef7e 151 {
152 if (!theCustomProgram.IsNull()
153 || myContext->caps->ffpEnable)
154 {
299e0ab9 155 return bindProgramWithState (theCustomProgram);
8625ef7e 156 }
157
158 if (myFontProgram.IsNull())
159 {
160 prepareStdProgramFont();
161 }
299e0ab9 162
163 return bindProgramWithState (myFontProgram);
8625ef7e 164 }
30f0ad28 165
b86bb3df 166 //! Bind program for FBO blit operation.
167 Standard_Boolean BindFboBlitProgram()
168 {
169 if (myBlitProgram.IsNull())
170 {
171 prepareStdProgramFboBlit();
172 }
173 return !myBlitProgram.IsNull()
174 && myContext->BindProgram (myBlitProgram);
175 }
176
a1073ae2 177 //! Bind program for blended order-independent transparency buffers compositing.
178 Standard_Boolean BindOitCompositingProgram (const Standard_Boolean theIsMSAAEnabled)
179 {
180 const Standard_Integer aProgramIdx = theIsMSAAEnabled ? 1 : 0;
181 if (myOitCompositingProgram[aProgramIdx].IsNull())
182 {
183 prepareStdProgramOitCompositing (theIsMSAAEnabled);
184 }
185
186 const Handle(OpenGl_ShaderProgram)& aProgram = myOitCompositingProgram [aProgramIdx];
187 return !aProgram.IsNull() && myContext->BindProgram (aProgram);
188 }
189
f978241f 190 //! Bind program for rendering stereoscopic image.
191 Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode)
38a0206f 192 {
f978241f 193 if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB)
38a0206f 194 {
f978241f 195 return Standard_False;
38a0206f 196 }
f978241f 197
198 if (myStereoPrograms[theStereoMode].IsNull())
199 {
200 prepareStdProgramStereo (myStereoPrograms[theStereoMode], theStereoMode);
201 }
202 const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[theStereoMode];
203 return !aProgram.IsNull()
204 && myContext->BindProgram (aProgram);
38a0206f 205 }
206
30f0ad28 207public:
208
209 //! Returns current state of OCCT light sources.
8613985b 210 const OpenGl_LightSourceState& LightSourceState() const { return myLightSourceState; }
30f0ad28 211
212 //! Updates state of OCCT light sources.
992ed6b3 213 Standard_EXPORT void UpdateLightSourceStateTo (const Handle(Graphic3d_LightSet)& theLights);
30f0ad28 214
7c3ef2f7 215 //! Invalidate state of OCCT light sources.
216 Standard_EXPORT void UpdateLightSourceState();
217
30f0ad28 218 //! Pushes current state of OCCT light sources to specified program.
219 Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
220
221public:
222
223 //! Returns current state of OCCT projection transform.
8613985b 224 const OpenGl_ProjectionState& ProjectionState() const { return myProjectionState; }
30f0ad28 225
226 //! Updates state of OCCT projection transform.
c827ea3a 227 Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
30f0ad28 228
30f0ad28 229 //! Pushes current state of OCCT projection transform to specified program.
230 Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
231
232public:
233
234 //! Returns current state of OCCT model-world transform.
8613985b 235 const OpenGl_ModelWorldState& ModelWorldState() const { return myModelWorldState; }
30f0ad28 236
237 //! Updates state of OCCT model-world transform.
c827ea3a 238 Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
30f0ad28 239
30f0ad28 240 //! Pushes current state of OCCT model-world transform to specified program.
241 Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
242
243public:
244
245 //! Returns current state of OCCT world-view transform.
8613985b 246 const OpenGl_WorldViewState& WorldViewState() const { return myWorldViewState; }
30f0ad28 247
248 //! Updates state of OCCT world-view transform.
c827ea3a 249 Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
30f0ad28 250
30f0ad28 251 //! Pushes current state of OCCT world-view transform to specified program.
252 Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
253
254public:
255
256 //! Updates state of OCCT clipping planes.
257 Standard_EXPORT void UpdateClippingState();
258
259 //! Reverts state of OCCT clipping planes.
260 Standard_EXPORT void RevertClippingState();
261
262 //! Pushes current state of OCCT clipping planes to specified program.
263 Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
264
265public:
266
8613985b 267 //! Returns current state of material.
268 const OpenGl_MaterialState& MaterialState() const { return myMaterialState; }
269
270 //! Updates state of material.
271 void UpdateMaterialStateTo (const OpenGl_Material& theFrontMat,
272 const OpenGl_Material& theBackMat,
c40eb6b9 273 const float theAlphaCutoff,
8613985b 274 const bool theToDistinguish,
275 const bool theToMapTexture)
276 {
c40eb6b9 277 myMaterialState.Set (theFrontMat, theBackMat, theAlphaCutoff, theToDistinguish, theToMapTexture);
8613985b 278 myMaterialState.Update();
279 }
280
281 //! Updates state of material.
282 void UpdateMaterialState()
283 {
284 myMaterialState.Update();
285 }
286
287 //! Pushes current state of material to specified program.
288 void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
289
290public:
291
726b5d9e 292 //! Returns state of OIT uniforms.
293 const OpenGl_OitState& OitState() const { return myOitState; }
294
a1073ae2 295 //! Set the state of OIT rendering pass.
296 //! @param theToEnableOitWrite [in] flag indicating whether the special output should be written for OIT algorithm.
297 //! @param theDepthFactor [in] the scalar factor of depth influence to the fragment's coverage.
298 void SetOitState (const bool theToEnableOitWrite, const float theDepthFactor)
299 {
300 myOitState.Set (theToEnableOitWrite, theDepthFactor);
301 myOitState.Update();
302 }
303
304 //! Pushes state of OIT uniforms to the specified program.
305 Standard_EXPORT void PushOitState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
306
307public:
308
30f0ad28 309 //! Pushes current state of OCCT graphics parameters to specified program.
310 Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
311
392ac980 312public:
313
314 //! Overwrites context
315 void SetContext (OpenGl_Context* theCtx)
316 {
317 myContext = theCtx;
318 }
319
c357e426 320 //! Returns true when provided context is the same as used one by shader manager.
321 bool IsSameContext (OpenGl_Context* theCtx) const
322 {
323 return myContext == theCtx;
324 }
325
db5d29de 326 //! Choose Shading Model for filled primitives.
327 //! Fallbacks to FACET model if there are no normal attributes.
328 Graphic3d_TypeOfShadingModel ChooseFaceShadingModel (Graphic3d_TypeOfShadingModel theCustomModel,
329 bool theHasNodalNormals) const
dc89236f 330 {
331 if (!myContext->ColorMask())
332 {
333 return Graphic3d_TOSM_UNLIT;
334 }
335 Graphic3d_TypeOfShadingModel aModel = theCustomModel != Graphic3d_TOSM_DEFAULT ? theCustomModel : myShadingModel;
336 switch (aModel)
337 {
338 case Graphic3d_TOSM_DEFAULT:
339 case Graphic3d_TOSM_UNLIT:
340 case Graphic3d_TOSM_FACET:
db5d29de 341 return aModel;
dc89236f 342 case Graphic3d_TOSM_VERTEX:
343 case Graphic3d_TOSM_FRAGMENT:
db5d29de 344 return theHasNodalNormals ? aModel : Graphic3d_TOSM_FACET;
dc89236f 345 }
db5d29de 346 return Graphic3d_TOSM_UNLIT;
347 }
348
349 //! Choose Shading Model for line primitives.
350 //! Fallbacks to UNLIT model if there are no normal attributes.
351 Graphic3d_TypeOfShadingModel ChooseLineShadingModel (Graphic3d_TypeOfShadingModel theCustomModel,
352 bool theHasNodalNormals) const
353 {
354 if (!myContext->ColorMask())
355 {
356 return Graphic3d_TOSM_UNLIT;
357 }
358 Graphic3d_TypeOfShadingModel aModel = theCustomModel != Graphic3d_TOSM_DEFAULT ? theCustomModel : myShadingModel;
359 switch (aModel)
360 {
361 case Graphic3d_TOSM_DEFAULT:
362 case Graphic3d_TOSM_UNLIT:
363 case Graphic3d_TOSM_FACET:
364 return Graphic3d_TOSM_UNLIT;
365 case Graphic3d_TOSM_VERTEX:
366 case Graphic3d_TOSM_FRAGMENT:
367 return theHasNodalNormals ? aModel : Graphic3d_TOSM_UNLIT;
368 }
369 return Graphic3d_TOSM_UNLIT;
370 }
371
372 //! Choose Shading Model for Marker primitives.
373 Graphic3d_TypeOfShadingModel ChooseMarkerShadingModel (Graphic3d_TypeOfShadingModel theCustomModel,
374 bool theHasNodalNormals) const
375 {
376 return ChooseLineShadingModel (theCustomModel, theHasNodalNormals);
dc89236f 377 }
378
379 //! Returns default Shading Model.
380 Graphic3d_TypeOfShadingModel ShadingModel() const { return myShadingModel; }
381
8625ef7e 382 //! Sets shading model.
c357e426 383 Standard_EXPORT void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel);
8625ef7e 384
e6804ff7 385 //! Sets last view manger used with.
386 //! Helps to handle matrix states in multi-view configurations.
387 void SetLastView (const OpenGl_View* theLastView)
388 {
389 myLastView = theLastView;
390 }
391
392 //! Returns true when provided view is the same as cached one.
393 bool IsSameView (const OpenGl_View* theView) const
394 {
395 return myLastView == theView;
396 }
397
30f0ad28 398protected:
399
8625ef7e 400 //! Define program bits.
cc8cbabe 401 Standard_Integer getProgramBits (const Handle(OpenGl_TextureSet)& theTextures,
c40eb6b9 402 Graphic3d_AlphaMode theAlphaMode,
403 Standard_Boolean theHasVertColor,
404 Standard_Boolean theEnableEnvMap)
e135a155 405
8625ef7e 406 {
407 Standard_Integer aBits = 0;
c40eb6b9 408 if (theAlphaMode == Graphic3d_AlphaMode_Mask)
409 {
410 aBits |= OpenGl_PO_AlphaTest;
411 }
1a7ece8f 412
413 const Standard_Integer aNbPlanes = myContext->Clipping().NbClippingOrCappingOn();
414 if (aNbPlanes > 0)
8625ef7e 415 {
1a7ece8f 416 aBits |= OpenGl_PO_ClipPlanesN;
25c35042 417 if (myContext->Clipping().HasClippingChains())
418 {
419 aBits |= OpenGl_PO_ClipChains;
420 }
421
1a7ece8f 422 if (aNbPlanes == 1)
423 {
424 aBits |= OpenGl_PO_ClipPlanes1;
425 }
426 else if (aNbPlanes == 2)
427 {
428 aBits |= OpenGl_PO_ClipPlanes2;
429 }
8625ef7e 430 }
1a7ece8f 431
83da37b1 432 if (theEnableEnvMap)
e135a155 433 {
434 // Environment map overwrites material texture
435 aBits |= OpenGl_PO_TextureEnv;
436 }
cc8cbabe 437 else if (!theTextures.IsNull()
438 && !theTextures->IsEmpty()
439 && !theTextures->First().IsNull())
8625ef7e 440 {
cc8cbabe 441 aBits |= theTextures->First()->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB;
8625ef7e 442 }
443 if (theHasVertColor)
444 {
445 aBits |= OpenGl_PO_VertColor;
446 }
a1073ae2 447
448 if (myOitState.ToEnableWrite())
449 {
450 aBits |= OpenGl_PO_WriteOit;
451 }
8625ef7e 452 return aBits;
453 }
454
455 //! Prepare standard GLSL program.
dc89236f 456 Handle(OpenGl_ShaderProgram)& getStdProgram (Graphic3d_TypeOfShadingModel theShadingModel,
457 Standard_Integer theBits)
8625ef7e 458 {
dc89236f 459 if (theShadingModel == Graphic3d_TOSM_UNLIT
460 || (theBits & OpenGl_PO_TextureEnv) != 0)
8625ef7e 461 {
dc89236f 462 // If environment map is enabled lighting calculations are
463 // not needed (in accordance with default OCCT behavior)
464 Handle(OpenGl_ShaderProgram)& aProgram = myUnlitPrograms->ChangeValue (Graphic3d_TOSM_UNLIT, theBits);
8625ef7e 465 if (aProgram.IsNull())
466 {
dc89236f 467 prepareStdProgramUnlit (aProgram, theBits);
8625ef7e 468 }
469 return aProgram;
470 }
471
dc89236f 472 Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theShadingModel, theBits);
8625ef7e 473 if (aProgram.IsNull())
474 {
dc89236f 475 prepareStdProgramLight (aProgram, theShadingModel, theBits);
8625ef7e 476 }
477 return aProgram;
478 }
479
299e0ab9 480 //! Prepare standard GLSL program for accessing point sprite alpha.
fd59283a 481 Standard_EXPORT TCollection_AsciiString pointSpriteAlphaSrc (const Standard_Integer theBits);
299e0ab9 482
483 //! Prepare standard GLSL program for computing point sprite shading.
484 Standard_EXPORT TCollection_AsciiString pointSpriteShadingSrc (const TCollection_AsciiString theBaseColorSrc, const Standard_Integer theBits);
485
8625ef7e 486 //! Prepare standard GLSL program for textured font.
487 Standard_EXPORT Standard_Boolean prepareStdProgramFont();
488
b86bb3df 489 //! Prepare standard GLSL program for FBO blit operation.
490 Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit();
491
a1073ae2 492 //! Prepare standard GLSL programs for OIT compositing operation.
493 Standard_EXPORT Standard_Boolean prepareStdProgramOitCompositing (const Standard_Boolean theMsaa);
494
8625ef7e 495 //! Prepare standard GLSL program without lighting.
dc89236f 496 Standard_EXPORT Standard_Boolean prepareStdProgramUnlit (Handle(OpenGl_ShaderProgram)& theProgram,
497 const Standard_Integer theBits);
8625ef7e 498
499 //! Prepare standard GLSL program with lighting.
500 Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram,
dc89236f 501 Graphic3d_TypeOfShadingModel theShadingModel,
502 Standard_Integer theBits)
8625ef7e 503 {
dc89236f 504 switch (theShadingModel)
8c3237d4 505 {
dc89236f 506 case Graphic3d_TOSM_UNLIT: return prepareStdProgramUnlit (theProgram, theBits);
8c3237d4 507 case Graphic3d_TOSM_FACET: return prepareStdProgramPhong (theProgram, theBits, true);
508 case Graphic3d_TOSM_VERTEX: return prepareStdProgramGouraud(theProgram, theBits);
dc89236f 509 case Graphic3d_TOSM_DEFAULT:
8c3237d4 510 case Graphic3d_TOSM_FRAGMENT: return prepareStdProgramPhong (theProgram, theBits, false);
511 }
512 return false;
8625ef7e 513 }
514
515 //! Prepare standard GLSL program with per-vertex lighting.
516 Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram,
517 const Standard_Integer theBits);
518
519 //! Prepare standard GLSL program with per-pixel lighting.
8c3237d4 520 //! @param theIsFlatNormal when TRUE, the Vertex normals will be ignored and Face normal will be computed instead
8625ef7e 521 Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram,
8c3237d4 522 const Standard_Integer theBits,
523 const Standard_Boolean theIsFlatNormal = false);
8625ef7e 524
256f9ac0 525 //! Define computeLighting GLSL function depending on current lights configuration
daf73ab7 526 //! @param theNbLights [out] number of defined light sources
527 //! @param theHasVertColor [in] flag to use getVertColor() instead of Ambient and Diffuse components of active material
528 Standard_EXPORT TCollection_AsciiString stdComputeLighting (Standard_Integer& theNbLights,
529 Standard_Boolean theHasVertColor);
256f9ac0 530
8625ef7e 531 //! Bind specified program to current context and apply state.
299e0ab9 532 Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram);
8625ef7e 533
256f9ac0 534 //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms
535 Standard_EXPORT void switchLightPrograms();
536
f978241f 537 //! Prepare standard GLSL program for stereoscopic image.
538 Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram,
539 const Graphic3d_StereoMode theStereoMode);
38a0206f 540
8625ef7e 541protected:
542
7c3ef2f7 543 //! Packed properties of light source
544 struct OpenGl_ShaderLightParameters
545 {
546 OpenGl_Vec4 Color;
547 OpenGl_Vec4 Position;
548 OpenGl_Vec4 Direction;
549 OpenGl_Vec4 Parameters;
550
551 //! Returns packed (serialized) representation of light source properties
552 const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
553 static Standard_Integer NbOfVec4() { return 4; }
554 };
555
556 //! Packed light source type information
557 struct OpenGl_ShaderLightType
558 {
559 Standard_Integer Type;
560 Standard_Integer IsHeadlight;
561
562 //! Returns packed (serialized) representation of light source type
563 const OpenGl_Vec2i* Packed() const { return reinterpret_cast<const OpenGl_Vec2i*> (this); }
564 static Standard_Integer NbOfVec2i() { return 1; }
565 };
566
8613985b 567 //! Fake OpenGL program for tracking FFP state in the way consistent to programmable pipeline.
568 class OpenGl_ShaderProgramFFP : public OpenGl_ShaderProgram
569 {
570 DEFINE_STANDARD_RTTI_INLINE(OpenGl_ShaderProgramFFP, OpenGl_ShaderProgram)
571 friend class OpenGl_ShaderManager;
572 protected:
573 OpenGl_ShaderProgramFFP() {}
574 };
575
7c3ef2f7 576protected:
577
25c35042 578 //! Append clipping plane definition to temporary buffers.
579 void addClippingPlane (Standard_Integer& thePlaneId,
580 const Graphic3d_ClipPlane& thePlane,
581 const Graphic3d_Vec4d& theEq,
582 const Standard_Integer theChainFwd) const
583 {
584 myClipChainArray.SetValue (thePlaneId, theChainFwd);
585 OpenGl_Vec4& aPlaneEq = myClipPlaneArray.ChangeValue (thePlaneId);
586 aPlaneEq.x() = float(theEq.x());
587 aPlaneEq.y() = float(theEq.y());
588 aPlaneEq.z() = float(theEq.z());
589 aPlaneEq.w() = float(theEq.w());
590 if (myHasLocalOrigin)
591 {
592 const gp_XYZ aPos = thePlane.ToPlane().Position().Location().XYZ() - myLocalOrigin;
593 const Standard_Real aD = -(theEq.x() * aPos.X() + theEq.y() * aPos.Y() + theEq.z() * aPos.Z());
594 aPlaneEq.w() = float(aD);
595 }
596 ++thePlaneId;
597 }
598
599protected:
600
8613985b 601 Handle(OpenGl_ShaderProgramFFP) myFfpProgram;
602
c357e426 603 Graphic3d_TypeOfShadingModel myShadingModel; //!< lighting shading model
256f9ac0 604 OpenGl_ShaderProgramList myProgramList; //!< The list of shader programs
605 Handle(OpenGl_SetOfShaderPrograms) myLightPrograms; //!< pointer to active lighting programs matrix
dc89236f 606 Handle(OpenGl_SetOfShaderPrograms) myUnlitPrograms; //!< programs matrix without lighting
256f9ac0 607 Handle(OpenGl_ShaderProgram) myFontProgram; //!< standard program for textured text
b86bb3df 608 Handle(OpenGl_ShaderProgram) myBlitProgram; //!< standard program for FBO blit emulation
a1073ae2 609 Handle(OpenGl_ShaderProgram) myOitCompositingProgram[2]; //!< standard program for OIT compositing (default and MSAA).
dc89236f 610 OpenGl_MapOfShaderPrograms myMapOfLightPrograms; //!< map of lighting programs depending on lights configuration
8625ef7e 611
f978241f 612 Handle(OpenGl_ShaderProgram) myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs
38a0206f 613
256f9ac0 614 OpenGl_Context* myContext; //!< OpenGL context
8625ef7e 615
616protected:
617
256f9ac0 618 OpenGl_ProjectionState myProjectionState; //!< State of OCCT projection transformation
619 OpenGl_ModelWorldState myModelWorldState; //!< State of OCCT model-world transformation
620 OpenGl_WorldViewState myWorldViewState; //!< State of OCCT world-view transformation
8cf06aa2 621 OpenGl_ClippingState myClippingState; //!< State of OCCT clipping planes
256f9ac0 622 OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources
8613985b 623 OpenGl_MaterialState myMaterialState; //!< State of Front and Back materials
a1073ae2 624 OpenGl_OitState myOitState; //!< State of OIT uniforms
8613985b 625
7c3ef2f7 626 gp_XYZ myLocalOrigin; //!< local camera transformation
627 Standard_Boolean myHasLocalOrigin; //!< flag indicating that local camera transformation has been set
628
daf73ab7 629 mutable NCollection_Array1<OpenGl_ShaderLightType> myLightTypeArray;
630 mutable NCollection_Array1<OpenGl_ShaderLightParameters> myLightParamsArray;
631 mutable NCollection_Array1<OpenGl_Vec4> myClipPlaneArray;
632 mutable NCollection_Array1<OpenGl_Vec4d> myClipPlaneArrayFfp;
25c35042 633 mutable NCollection_Array1<Standard_Integer> myClipChainArray;
30f0ad28 634
635private:
636
256f9ac0 637 const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with
8625ef7e 638
30f0ad28 639};
640
8613985b 641DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient)
642
30f0ad28 643#endif // _OpenGl_ShaderManager_HeaderFile