2
// This unit is part of the GLScene Engine https://github.com/glscene
5
This is a collection of GLSL diffuse-specular shaders.
8
09/03/13 - Yar - Added point, parallel, spot and parallel spot light's style support to TGLSLMLDiffuseSpecularShader
9
Deleted TGLSLDiffuseSpecularShaderAM, TGLSLDiffuseSpecularShaderAM
10
17/02/13 - Yar - Added fog support to TGLSLMLDiffuseSpecularShader
11
16/03/11 - Yar - Fixes after emergence of GLMaterialEx
12
23/10/10 - Yar - Bugfixed memory leak
13
23/08/10 - Yar - Replaced OpenGL1x to OpenGLTokens
14
07/01/10 - DaStr - Bugfixed all DoInitialize() calls
15
(thanks YarUnderoaker)
16
25/07/09 - DaStr - Fixed a bug with "dot(reflect_vec, LightVector)" clamping
17
which occured on all GeForce 8x and later graphic cards
18
24/07/09 - DaStr - Added Fog support for single-light shaders and fixed
19
a bug with material Alpha (thanks Controller)
20
02/09/07 - LC - Fixed texture bug in TGLSLMLDiffuseSpecularShader.
21
(Bugtracker ID = 1786286)
22
03/04/07 - LC - Shader didn't respect the texture matrix. Changed
23
vertex shader to fix this. (Bugtracker ID = 1693389)
24
20/03/07 - DaStr - Made changes related to the new parameter passing model
25
06/03/07 - DaStr - Again replaced DecimalSeparator stuff with
26
a single Str procedure (thanks Uwe Raabe)
27
03/03/07 - DaStr - Made compatible with Delphi6
28
Added more stuff to RegisterClasses()
29
21/02/07 - DaStr - Initial version (contributed to GLScene)
32
This is a collection of GLSL Diffuse Specular shaders, comes in these variaties
33
(to know what these suffixes and prefixes mean see GLCustomShader.pas):
34
- TGLSLDiffuseSpecularShader
35
- TGLSLDiffuseSpecularShaderMT
37
- TGLSLMLDiffuseSpecularShader
38
- TGLSLMLDiffuseSpecularShaderMT
41
1) TGLSLDiffuseSpecularShader takes all Material parameters directly
42
from OpenGL (that includes TGLMaterial's)
43
2) TGLSLDiffuseSpecularShader takes all Light parameters directly
44
from OpenGL (that includes TGLLightSource's)
47
Previous version history:
48
v1.0 01 November '2006 Creation
49
v1.1 19 December '2006 TGLBaseCustomGLSLDiffuseSpecular[MT] abstracted
50
5 different versions of this shader added
51
v1.1.2 06 February '2007 IGLMaterialLibrarySupported renamed to
52
IGLMaterialLibrarySupported
53
v1.2 16 February '2007 Updated to the latest CVS version of GLScene
56
unit GLSLDiffuseSpecularShader;
65
GLTexture, GLScene, GLVectorGeometry, OpenGLTokens, GLStrings, GLCustomShader,
66
GLSLShader, GLColor, GLRenderContextInfo, GLMaterial;
69
EGLSLDiffuseSpecularShaderException = class(EGLSLShaderException);
72
TGLBaseCustomGLSLDiffuseSpecular = class(TGLCustomGLSLShader)
75
FRealisticSpecular: Boolean;
76
FFogSupport: TGLShaderFogSupport;
77
procedure SetRealisticSpecular(const Value: Boolean);
78
procedure SetFogSupport(const Value: TGLShaderFogSupport);
80
procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
81
function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
83
constructor Create(AOwner : TComponent); override;
84
property LightPower: Single read FLightPower write FLightPower;
85
property RealisticSpecular: Boolean read FRealisticSpecular write SetRealisticSpecular;
87
// User can disable fog support and save some FPS if he doesn't need it.
88
property FogSupport: TGLShaderFogSupport read FFogSupport write SetFogSupport default sfsAuto;
92
TGLBaseGLSLDiffuseSpecularShaderMT = class(TGLBaseCustomGLSLDiffuseSpecular, IGLMaterialLibrarySupported)
94
FMaterialLibrary: TGLMaterialLibrary;
95
FMainTexture: TGLTexture;
96
FMainTextureName: TGLLibMaterialName;
97
function GetMainTextureName: TGLLibMaterialName;
98
procedure SetMainTextureName(const Value: TGLLibMaterialName);
99
// Implementing IGLMaterialLibrarySupported.
100
function GetMaterialLibrary: TGLAbstractMaterialLibrary;
102
procedure SetMaterialLibrary(const Value: TGLMaterialLibrary); virtual;
103
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
104
procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
106
property MainTexture: TGLTexture read FMainTexture write FMainTexture;
107
property MainTextureName: TGLLibMaterialName read GetMainTextureName write SetMainTextureName;
108
property MaterialLibrary: TGLMaterialLibrary read FMaterialLibrary write SetMaterialLibrary;
111
{******** Single Light ************}
113
TGLCustomGLSLDiffuseSpecularShader = class(TGLBaseCustomGLSLDiffuseSpecular)
115
procedure DoInitialize(var rci: TGLRenderContextInfo; Sender: TObject); override;
116
procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
120
TGLCustomGLSLDiffuseSpecularShaderMT = class(TGLBaseGLSLDiffuseSpecularShaderMT)
122
procedure DoInitialize(var rci: TGLRenderContextInfo; Sender: TObject); override;
125
{******** Multi Light ************}
127
{ Note: probably LightCount should be replaced by LightSources, like in
130
TLightRecord = record
135
TGLCustomGLSLMLDiffuseSpecularShader = class(TGLBaseCustomGLSLDiffuseSpecular)
137
FLightTrace: array[0..7] of TLightRecord;
139
procedure DoInitialize(var rci: TGLRenderContextInfo; Sender: TObject); override;
140
procedure DoApply(var rci: TGLRenderContextInfo; Sender: TObject); override;
142
constructor Create(AOwner : TComponent); override;
145
TGLCustomGLSLMLDiffuseSpecularShaderMT = class(TGLBaseGLSLDiffuseSpecularShaderMT)
147
FLightTrace: array[0..7] of TLightRecord;
149
procedure DoInitialize(var rci: TGLRenderContextInfo; Sender: TObject); override;
150
procedure DoApply(var rci: TGLRenderContextInfo; Sender: TObject); override;
152
constructor Create(AOwner : TComponent); override;
156
{******** Published Stuff ************}
158
TGLSLDiffuseSpecularShaderMT = class(TGLCustomGLSLDiffuseSpecularShaderMT)
160
property MainTextureName;
166
TGLSLDiffuseSpecularShader = class(TGLCustomGLSLDiffuseSpecularShader)
173
TGLSLMLDiffuseSpecularShaderMT = class(TGLCustomGLSLMLDiffuseSpecularShaderMT)
175
property MainTextureName;
181
TGLSLMLDiffuseSpecularShader = class(TGLCustomGLSLMLDiffuseSpecularShader)
189
procedure GetVertexProgramCode(const Code: TStrings;
190
AFogSupport: Boolean; var rci: TGLRenderContextInfo);
195
Add('varying vec3 Normal; ');
196
Add('varying vec4 Position; ');
199
Add('varying float fogFactor; ');
203
Add('void main(void) ');
205
Add(' gl_Position = ftransform(); ');
206
Add(' gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; ');
207
Add(' Normal = normalize(gl_NormalMatrix * gl_Normal); ');
208
Add(' Position = gl_ModelViewMatrix * gl_Vertex; ');
212
Add(' const float LOG2 = 1.442695; ');
213
Add(' gl_FogFragCoord = length(Position.xyz); ');
215
case TGLSceneBuffer(rci.buffer).FogEnvironment.FogMode of
217
Add(' fogFactor = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale; ');
218
fmExp, // Yep, I don't know about this type, so I use fmExp2.
221
Add(' fogFactor = exp2( -gl_Fog.density * ');
222
Add(' gl_Fog.density * ');
223
Add(' gl_FogFragCoord * ');
224
Add(' gl_FogFragCoord * ');
228
Assert(False, glsUnknownType);
231
Add(' fogFactor = clamp(fogFactor, 0.0, 1.0); ');
238
procedure AddLightSub(const Code: TStrings);
242
Add('void pointLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3)');
244
Add(' float nDotVP; // normal . light direction');
245
Add(' float nDotHV; // normal . light half vector');
246
Add(' float pf; // power factor');
247
Add(' float attenuation; // computed attenuation factor');
248
Add(' float d; // distance from surface to light source');
249
Add(' vec3 VP; // direction from surface to light position');
250
Add(' vec3 halfVector; // direction of maximum highlights');
252
Add(' // Compute vector from surface to light position');
253
Add(' VP = vec3 (gl_LightSource[i].position) - ecPosition3;');
255
Add(' // Compute distance between surface and light position');
256
Add(' d = length(VP);');
258
Add(' // Normalize the vector from surface to light position');
259
Add(' VP = normalize(VP);');
261
Add(' // Compute attenuation');
262
Add(' attenuation = 1.0 / (gl_LightSource[i].constantAttenuation +');
263
Add(' gl_LightSource[i].linearAttenuation * d +');
264
Add(' gl_LightSource[i].quadraticAttenuation * d * d);');
266
Add(' halfVector = normalize(VP + eye);');
268
Add(' nDotVP = max(0.0, dot(normal, VP));');
269
Add(' nDotHV = max(0.0, dot(normal, halfVector));');
271
Add(' if (nDotVP == 0.0)');
277
Add(' pf = pow(nDotHV, gl_FrontMaterial.shininess);');
280
Add(' Ambient += gl_LightSource[i].ambient * attenuation;');
281
Add(' Diffuse += gl_LightSource[i].diffuse * nDotVP * attenuation;');
282
Add(' Specular += gl_LightSource[i].specular * pf * attenuation;');
285
Add('void directionalLight(in int i, in vec3 normal)');
287
Add(' float nDotVP; // normal . light direction');
288
Add(' float nDotHV; // normal . light half vector');
289
Add(' float pf; // power factor');
291
Add(' nDotVP = max(0.0, dot(normal, normalize(vec3 (gl_LightSource[i].position))));');
292
Add(' nDotHV = max(0.0, dot(normal, vec3 (gl_LightSource[i].halfVector)));');
294
Add(' if (nDotVP == 0.0)');
300
Add(' pf = pow(nDotHV, gl_FrontMaterial.shininess);');
303
Add(' Ambient += gl_LightSource[i].ambient;');
304
Add(' Diffuse += gl_LightSource[i].diffuse * nDotVP;');
305
Add(' Specular += gl_LightSource[i].specular * pf;');
307
Add('void spotLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3)');
309
Add(' float nDotVP; // normal . light direction');
310
Add(' float nDotHV; // normal . light half vector');
311
Add(' float pf; // power factor');
312
Add(' float spotDot; // cosine of angle between spotlight');
313
Add(' float spotAttenuation; // spotlight attenuation factor');
314
Add(' float attenuation; // computed attenuation factor');
315
Add(' float d; // distance from surface to light source');
316
Add(' vec3 VP; // direction from surface to light position');
317
Add(' vec3 halfVector; // direction of maximum highlights');
319
Add(' // Compute vector from surface to light position');
320
Add(' VP = vec3 (gl_LightSource[i].position) - ecPosition3;');
322
Add(' // Compute distance between surface and light position');
323
Add(' d = length(VP);');
325
Add(' // Normalize the vector from surface to light position');
326
Add(' VP = normalize(VP);');
328
Add(' // Compute attenuation');
329
Add(' attenuation = 1.0 / (gl_LightSource[i].constantAttenuation +');
330
Add(' gl_LightSource[i].linearAttenuation * d +');
331
Add(' gl_LightSource[i].quadraticAttenuation * d * d);');
333
Add(' // See if point on surface is inside cone of illumination');
334
Add(' spotDot = dot(-VP, normalize(gl_LightSource[i].spotDirection));');
336
Add(' if (spotDot < gl_LightSource[i].spotCosCutoff)');
338
Add(' spotAttenuation = 0.0; // light adds no contribution');
342
Add(' spotAttenuation = pow(spotDot, gl_LightSource[i].spotExponent);');
345
Add(' // Combine the spotlight and distance attenuation.');
346
Add(' attenuation *= spotAttenuation;');
348
Add(' halfVector = normalize(VP + eye);');
350
Add(' nDotVP = max(0.0, dot(normal, VP));');
351
Add(' nDotHV = max(0.0, dot(normal, halfVector));');
353
Add(' if (nDotVP == 0.0)');
359
Add(' pf = pow(nDotHV, gl_FrontMaterial.shininess);');
362
Add(' Ambient += gl_LightSource[i].ambient * attenuation;');
363
Add(' Diffuse += gl_LightSource[i].diffuse * nDotVP * attenuation;');
364
Add(' Specular += gl_LightSource[i].specular * pf * attenuation;');
367
Add('void infiniteSpotLight(in int i, in vec3 normal)');
369
Add(' float nDotVP; // normal . light direction');
370
Add(' float nDotHV; // normal . light half vector');
371
Add(' float pf; // power factor');
372
Add(' float spotAttenuation;');
376
Add(' nDotVP = max(0.0, dot(normal, normalize(vec3 (gl_LightSource[i].position))));');
377
Add(' nDotHV = max(0.0, dot(normal, vec3 (gl_LightSource[i].halfVector)));');
379
Add(' Ppli = -normalize(vec3(gl_LightSource[i].position));');
380
Add(' Sdli = normalize(vec3(gl_LightSource[i].spotDirection));');
382
Add(' spotAttenuation = pow(dot(Ppli, Sdli), gl_LightSource[i].spotExponent);');
383
Add(' if (nDotVP == 0.0)');
389
Add(' pf = pow(nDotHV, gl_FrontMaterial.shininess);');
392
Add(' Ambient += gl_LightSource[i].ambient * spotAttenuation;');
393
Add(' Diffuse += gl_LightSource[i].diffuse * nDotVP * spotAttenuation;');
394
Add(' Specular += gl_LightSource[i].specular * pf * spotAttenuation;');
399
procedure GetMLFragmentProgramCodeMid(const Code: TStrings;
400
const CurrentLight: Integer; AStyle: TLightStyle);
405
lsOmni: Add(Format(' pointLight(%d, N, eye, Pos); ', [CurrentLight]));
406
lsSpot: Add(Format(' spotLight(%d, N, eye, Pos); ', [CurrentLight]));
407
lsParallel: Add(Format(' directionalLight(%d, N); ', [CurrentLight]));
408
lsParallelSpot: Add(Format(' infiniteSpotLight(%d, N); ', [CurrentLight]));
413
procedure GetFragmentProgramCode(const Code: TStrings;
414
const ARealisticSpecular: Boolean; const AFogSupport: Boolean;
415
aRci: TGLRenderContextInfo);
422
Add('uniform float LightIntensity; ');
423
Add('uniform sampler2D MainTexture; ');
425
Add('varying vec3 Normal; ');
426
Add('varying vec4 Position; ');
430
Add('varying float fogFactor; ');
433
Add('vec4 Ambient;');
434
Add('vec4 Diffuse;');
435
Add('vec4 Specular;');
439
Add('void main(void) ');
441
Add(' vec4 TextureContrib = texture2D(MainTexture, gl_TexCoord[0].xy); ');
442
Add(' vec3 eye = vec3(0.0, 0.0, 1.0); ');
443
Add(' Diffuse = vec4(0.0); ');
444
Add(' Specular = vec4(0.0); ');
445
Add(' Ambient = vec4(0.0); ');
446
Add(' vec3 Pos = Position.xyz; ');
447
Add(' vec3 N = normalize(Normal); ');
448
scene := TGLScene(ARci.scene);
449
if (scene.Lights.Count > 0) then
450
GetMLFragmentProgramCodeMid(Code, 0,
451
TGLLightSource(scene.Lights[0]).LightStyle);
453
if ARealisticSpecular then
454
Add(' gl_FragColor = LightIntensity * (TextureContrib * (gl_FrontLightModelProduct.sceneColor + Ambient * gl_FrontMaterial.ambient + Diffuse * gl_FrontMaterial.diffuse) + Specular * gl_FrontMaterial.specular); ')
456
Add(' gl_FragColor = LightIntensity * TextureContrib * (gl_FrontLightModelProduct.sceneColor + Ambient * gl_FrontMaterial.ambient + Diffuse * gl_FrontMaterial.diffuse + Specular * gl_FrontMaterial.specular); ');
459
Add(' gl_FragColor = mix(gl_Fog.color, gl_FragColor, fogFactor);');
461
Add(' gl_FragColor.a = TextureContrib.a; ');
466
procedure GetMLFragmentProgramCodeBeg(const Code: TStrings;
467
const AFogSupport: Boolean);
472
Add('uniform sampler2D MainTexture;');
473
Add('uniform float LightIntensity; ');
474
Add('uniform float SpecPower; ');
475
Add('varying vec3 Normal;');
476
Add('varying vec4 Position;');
479
Add('varying float fogFactor;');
481
Add('vec4 Ambient;');
482
Add('vec4 Diffuse;');
483
Add('vec4 Specular;');
486
Add('void main(void) ');
488
Add(' vec4 TextureContrib = texture2D(MainTexture, gl_TexCoord[0].st); ');
489
Add(' vec3 eye = vec3(0.0, 0.0, 1.0); ');
490
Add(' Diffuse = vec4(0.0); ');
491
Add(' Specular = vec4(0.0); ');
492
Add(' Ambient = vec4(0.0); ');
493
Add(' vec3 Pos = Position.xyz; ');
494
Add(' vec3 N = normalize(Normal); ');
498
procedure GetMLFragmentProgramCodeEnd(const Code: TStrings;
499
const ARealisticSpecular: Boolean;
500
AFogSupport: Boolean);
504
if ARealisticSpecular then
505
Add(' gl_FragColor = LightIntensity * (TextureContrib * (gl_FrontLightModelProduct.sceneColor + Ambient * gl_FrontMaterial.ambient + Diffuse * gl_FrontMaterial.diffuse) + Specular * gl_FrontMaterial.specular); ')
507
Add(' gl_FragColor = LightIntensity * TextureContrib * (gl_FrontLightModelProduct.sceneColor + Ambient * gl_FrontMaterial.ambient + Diffuse * gl_FrontMaterial.diffuse + Specular * gl_FrontMaterial.specular); ');
510
Add(' gl_FragColor = mix(gl_Fog.color, gl_FragColor, fogFactor);');
512
Add(' gl_FragColor.a = TextureContrib.a; ');
518
{ TGLBaseCustomGLSLDiffuseSpecular }
520
constructor TGLBaseCustomGLSLDiffuseSpecular.Create(
526
FFogSupport := sfsAuto;
527
TStringList(VertexProgram.Code).OnChange := nil;
528
TStringList(FragmentProgram.Code).OnChange := nil;
529
VertexProgram.Enabled := true;
530
FragmentProgram.Enabled := true;
533
procedure TGLBaseCustomGLSLDiffuseSpecular.DoApply(
534
var rci: TGLRenderContextInfo; Sender: TObject);
536
GetGLSLProg.UseProgramObject;
537
Param['LightIntensity'].AsVector1f := FLightPower;
540
function TGLBaseCustomGLSLDiffuseSpecular.DoUnApply(
541
var rci: TGLRenderContextInfo): Boolean;
544
GetGLSLProg.EndUseProgramObject;
547
procedure TGLBaseCustomGLSLDiffuseSpecular.SetFogSupport(
548
const Value: TGLShaderFogSupport);
550
if FFogSupport <> Value then
552
FFogSupport := Value;
557
procedure TGLBaseCustomGLSLDiffuseSpecular.SetRealisticSpecular(
558
const Value: Boolean);
560
if FRealisticSpecular <> Value then
562
FRealisticSpecular := Value;
568
{ TGLBaseGLSLDiffuseSpecularShaderMT }
570
procedure TGLBaseGLSLDiffuseSpecularShaderMT.DoApply(
571
var rci: TGLRenderContextInfo; Sender: TObject);
574
Param['MainTexture'].AsTexture2D[0] := FMainTexture;
577
function TGLBaseGLSLDiffuseSpecularShaderMT.GetMainTextureName: TGLLibMaterialName;
579
Result := FMaterialLibrary.GetNameOfTexture(FMainTexture);
582
function TGLBaseGLSLDiffuseSpecularShaderMT.GetMaterialLibrary: TGLAbstractMaterialLibrary;
584
Result := FMaterialLibrary;
587
procedure TGLBaseGLSLDiffuseSpecularShaderMT.Notification(
588
AComponent: TComponent; Operation: TOperation);
593
if Operation = opRemove then
594
if AComponent = FMaterialLibrary then
595
if FMaterialLibrary <> nil then
597
//need to nil the textures that were ownned by it
598
if FMainTexture <> nil then
600
Index := FMaterialLibrary.Materials.GetTextureIndex(FMainTexture);
604
FMaterialLibrary := nil;
608
procedure TGLBaseGLSLDiffuseSpecularShaderMT.SetMainTextureName(
609
const Value: TGLLibMaterialName);
611
if FMaterialLibrary = nil then
613
FMainTextureName := Value;
614
if not (csLoading in ComponentState) then
615
raise EGLSLDiffuseSpecularShaderException.Create(glsErrorEx + glsMatLibNotDefined);
619
FMainTexture := FMaterialLibrary.TextureByName(Value);
620
FMainTextureName := '';
624
procedure TGLBaseGLSLDiffuseSpecularShaderMT.SetMaterialLibrary(
625
const Value: TGLMaterialLibrary);
627
if FMaterialLibrary <> nil then
628
FMaterialLibrary.RemoveFreeNotification(Self);
629
FMaterialLibrary := Value;
631
if FMaterialLibrary <> nil then
633
FMaterialLibrary.FreeNotification(Self);
635
if FMainTextureName <> '' then
636
SetMainTextureName(FMainTextureName);
639
FMainTextureName := '';
642
{ TGLCustomGLSLDiffuseSpecularShaderMT }
644
procedure TGLCustomGLSLDiffuseSpecularShaderMT.DoInitialize(var rci: TGLRenderContextInfo; Sender: TObject);
646
GetVertexProgramCode(VertexProgram.Code, IsFogEnabled(FFogSupport, rci), rci);
647
GetFragmentProgramCode(FragmentProgram.Code, FRealisticSpecular, IsFogEnabled(FFogSupport, rci), rci);
648
VertexProgram.Enabled := True;
649
FragmentProgram.Enabled := True;
653
{ TGLCustomGLSLDiffuseSpecularShader }
655
procedure TGLCustomGLSLDiffuseSpecularShader.DoApply(
656
var rci: TGLRenderContextInfo; Sender: TObject);
659
Param['MainTexture'].AsVector1i := 0; // Use the current texture.
662
procedure TGLCustomGLSLDiffuseSpecularShader.DoInitialize(var rci: TGLRenderContextInfo; Sender: TObject);
664
GetVertexProgramCode(VertexProgram.Code, IsFogEnabled(FFogSupport, rci), rci);
665
GetFragmentProgramCode(FragmentProgram.Code, FRealisticSpecular, IsFogEnabled(FFogSupport, rci), rci);
666
VertexProgram.Enabled := True;
667
FragmentProgram.Enabled := True;
671
{ TGLCustomGLSLMLDiffuseSpecularShader }
673
constructor TGLCustomGLSLMLDiffuseSpecularShader.Create(
679
procedure TGLCustomGLSLMLDiffuseSpecularShader.DoApply(var rci: TGLRenderContextInfo; Sender: TObject);
683
needRecompile: Boolean;
685
scene := TGLScene(rci.scene);
686
needRecompile := False;
687
for I := 0 to scene.Lights.Count - 1 do
689
if Assigned(scene.Lights[I]) then
691
if FLightTrace[I].Enabled <> TGLLightSource(scene.Lights[I]).Shining then
693
needRecompile := True;
696
if FLightTrace[I].Style <> TGLLightSource(scene.Lights[I]).LightStyle then
698
needRecompile := True;
703
if FLightTrace[I].Enabled then
705
needRecompile := True;
709
if needRecompile then
712
InitializeShader(rci, Sender);
716
Param['MainTexture'].AsVector1i := 0; // Use the current texture.
719
procedure TGLCustomGLSLMLDiffuseSpecularShader.DoInitialize(var rci: TGLRenderContextInfo; Sender: TObject);
724
GetVertexProgramCode(VertexProgram.Code, IsFogEnabled(FFogSupport, rci), rci);
725
with FragmentProgram.Code do
727
GetMLFragmentProgramCodeBeg(FragmentProgram.Code, IsFogEnabled(FFogSupport, rci));
729
// Repeat for all lights.
730
scene := TGLScene(rci.scene);
731
for I := 0 to scene.Lights.Count - 1 do
733
if Assigned(scene.Lights[I]) then
735
FLightTrace[I].Enabled := TGLLightSource(scene.Lights[I]).Shining;
736
FLightTrace[I].Style := TGLLightSource(scene.Lights[I]).LightStyle;
737
if FLightTrace[I].Enabled then
738
GetMLFragmentProgramCodeMid(FragmentProgram.Code, I, FLightTrace[I].Style);
741
FLightTrace[I].Enabled := False;
744
GetMLFragmentProgramCodeEnd(FragmentProgram.Code,
745
FRealisticSpecular, IsFogEnabled(FFogSupport, rci));
747
VertexProgram.Enabled := True;
748
FragmentProgram.Enabled := True;
749
inherited DoInitialize(rci, Sender);
752
{ TGLCustomGLSLMLDiffuseSpecularShaderMT }
754
constructor TGLCustomGLSLMLDiffuseSpecularShaderMT.Create(
760
procedure TGLCustomGLSLMLDiffuseSpecularShaderMT.DoApply(
761
var rci: TGLRenderContextInfo; Sender: TObject);
765
needRecompile: Boolean;
767
scene := TGLScene(rci.scene);
768
needRecompile := False;
769
for I := 0 to scene.Lights.Count - 1 do
771
if Assigned(scene.Lights[I]) then
773
if FLightTrace[I].Enabled <> TGLLightSource(scene.Lights[I]).Shining then
775
needRecompile := True;
778
if FLightTrace[I].Style <> TGLLightSource(scene.Lights[I]).LightStyle then
780
needRecompile := True;
785
if FLightTrace[I].Enabled then
787
needRecompile := True;
791
if needRecompile then
794
InitializeShader(rci, Sender);
800
procedure TGLCustomGLSLMLDiffuseSpecularShaderMT.DoInitialize(var rci: TGLRenderContextInfo; Sender: TObject);
805
GetVertexProgramCode(VertexProgram.Code, IsFogEnabled(FFogSupport, rci), rci);
806
with FragmentProgram.Code do
808
GetMLFragmentProgramCodeBeg(FragmentProgram.Code, IsFogEnabled(FFogSupport, rci));
810
// Repeat for all lights.
811
scene := TGLScene(rci.scene);
812
for I := 0 to scene.Lights.Count - 1 do
814
if Assigned(scene.Lights[I]) then
816
FLightTrace[I].Enabled := TGLLightSource(scene.Lights[I]).Shining;
817
FLightTrace[I].Style := TGLLightSource(scene.Lights[I]).LightStyle;
818
if FLightTrace[I].Enabled then
819
GetMLFragmentProgramCodeMid(FragmentProgram.Code, I, FLightTrace[I].Style);
822
FLightTrace[I].Enabled := False;
825
GetMLFragmentProgramCodeEnd(FragmentProgram.Code,
826
FRealisticSpecular, IsFogEnabled(FFogSupport, rci));
828
VertexProgram.Enabled := True;
829
FragmentProgram.Enabled := True;
835
TGLCustomGLSLDiffuseSpecularShader,
836
TGLCustomGLSLDiffuseSpecularShaderMT,
837
TGLCustomGLSLMLDiffuseSpecularShader,
838
TGLCustomGLSLMLDiffuseSpecularShaderMT,
840
TGLSLDiffuseSpecularShader,
841
TGLSLDiffuseSpecularShaderMT,
842
TGLSLMLDiffuseSpecularShader,
843
TGLSLMLDiffuseSpecularShaderMT