2
// This unit is part of the GLScene Engine https://github.com/glscene
5
Lattice shader that simulate Lattice.
6
At this time only one light source is supported
9
08/12/15 - J.Delauney - Made compatible with the latest SVN version of GLScene
10
02/11/06 - Da Stranger - Creation
14
unit GLSLLatticeShader;
23
GLScene, GLCrossPlatform, GLBaseClasses, GLState, OpenGLTokens, OpenGL1x, GLContext, GLRenderContextInfo,
24
GLVectorGeometry, GLCoordinates,
25
GLTextureFormat,GLColor, GLTexture, GLMaterial,
26
GLSLShader, GLCustomShader;
28
//TGLCustomGLSLSimpleLatticeShader
30
{ Custom class for GLSLSimpleLatticeShader.
31
A shader that simulate Lattice }
33
TGLCustomGLSLSimpleLatticeShader = class(TGLCustomGLSLShader)
35
FLatticeScale: TGLCoordinates2;
36
FLatticeThreshold: TGLCoordinates2;
37
procedure SetLatticeScale(const Value: TGLCoordinates2);
38
procedure SetLatticeThreshold(const Value: TGLCoordinates2);
40
procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
41
function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
43
constructor Create(AOwner : TComponent); override;
44
destructor Destroy; override;
45
property LatticeScale: TGLCoordinates2 read FLatticeScale write SetLatticeScale;
46
property LatticeThreshold: TGLCoordinates2 read FLatticeThreshold write SetLatticeThreshold;
50
//TGLCustomGLSLLatticeShader
52
{ Custom class for GLSLLatticeShader.
53
A shader that simulate Lattice with Diffuse/Specular and support Texture }
54
TGLCustomGLSLLatticeShader = class(TGLCustomGLSLSimpleLatticeShader)
56
FAmbientColor: TGLColor;
57
FDiffuseColor: TGLColor;
58
FSpecularColor: TGLColor;
60
FMaterialLibrary: TGLAbstractMaterialLibrary;
61
FMainTexture: TGLTexture;
62
FMainTexName : TGLLibMaterialName;
64
FSpecularPower: Single;
67
function GetMaterialLibrary: TGLAbstractMaterialLibrary;
69
procedure SetMainTexTexture(const Value: TGLTexture);
70
function GetMainTexName: TGLLibMaterialName;
71
procedure SetMainTexName(const Value: TGLLibMaterialName);
73
procedure SetDiffuseColor(AValue: TGLColor);
74
procedure SetAmbientColor(AValue: TGLColor);
75
procedure SetSpecularColor(AValue: TGLColor);
78
procedure DoInitialize(var rci : TGLRenderContextInfo; Sender : TObject); override;
79
procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
81
procedure SetMaterialLibrary(const Value: TGLAbstractMaterialLibrary); virtual;
82
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
84
constructor Create(AOwner : TComponent); override;
85
destructor Destroy; override;
87
property DiffuseColor : TGLColor read FDiffuseColor Write setDiffuseColor;
88
property SpecularColor : TGLColor Read FSpecularColor Write setSpecularColor;
89
property AmbientColor : TGLColor Read FAmbientColor Write setAmbientColor;
91
property MaterialLibrary: TGLAbstractMaterialLibrary read getMaterialLibrary write SetMaterialLibrary;
92
property MainTexture: TGLTexture read FMainTexture write SetMainTexTexture;
93
property MainTextureName: TGLLibMaterialName read GetMainTexName write SetMainTexName;
95
property SpecularPower: Single read FSpecularPower write FSpecularPower;
96
property LightPower: Single read FLightPower write FLightPower;
100
TGLSLSimpleLatticeShader = class(TGLCustomGLSLSimpleLatticeShader)
102
property LatticeScale;
103
property LatticeThreshold;
106
TGLSLLatticeShader = class(TGLCustomGLSLLatticeShader)
108
property LatticeScale;
109
property LatticeThreshold;
111
property AmbientColor;
112
property DiffuseColor;
113
property SpecularColor;
115
property MainTexture;
117
property SpecularPower;
123
{ TGLCustomGLSLSimpleLatticeShader }
125
constructor TGLCustomGLSLSimpleLatticeShader.Create(AOwner: TComponent);
128
with FragmentProgram.Code do
131
Add(' uniform vec2 Scale; ');
132
Add(' uniform vec2 Threshold; ');
134
Add(' void main (void) ');
136
Add(' float ss = fract(gl_TexCoord[0].s * Scale.s); ');
137
Add(' float tt = fract(gl_TexCoord[0].t * Scale.t); ');
139
Add(' if ((ss > Threshold.s) && (tt > Threshold.t)) discard; ');
140
Add(' gl_FragColor = gl_Color;');
145
FLatticeScale := TGLCoordinates2.Create(Self);
146
FLatticeThreshold := TGLCoordinates2.Create(Self);
148
FLatticeScale.SetPoint2D(10, 40);
149
FLatticeThreshold.SetPoint2D(0.15, 0.3);
152
destructor TGLCustomGLSLSimpleLatticeShader.Destroy;
154
FLatticeScale.Destroy;
155
FLatticeThreshold.Destroy;
159
procedure TGLCustomGLSLSimpleLatticeShader.DoApply(var rci: TGLRenderContextInfo;Sender: TObject);
161
GetGLSLProg.UseProgramObject;
162
Param['Scale'].AsVector2f := FLatticeScale.AsPoint2D;
163
Param['Threshold'].AsVector2f := FLatticeThreshold.AsPoint2D;
166
function TGLCustomGLSLSimpleLatticeShader.DoUnApply(var rci: TGLRenderContextInfo): Boolean;
169
//gl.ActiveTexture(GL_TEXTURE0_ARB);
170
GetGLSLProg.EndUseProgramObject;
173
procedure TGLCustomGLSLSimpleLatticeShader.SetLatticeScale(
174
const Value: TGLCoordinates2);
176
FLatticeScale.Assign(Value);
179
procedure TGLCustomGLSLSimpleLatticeShader.SetLatticeThreshold(
180
const Value: TGLCoordinates2);
182
FLatticeThreshold.Assign(Value);
185
{ TGLCustomGLSLLatticeShader }
187
constructor TGLCustomGLSLLatticeShader.Create(
191
FAmbientColor := TGLColor.Create(Self);
192
FDiffuseColor := TGLColor.Create(Self);
193
FSpecularColor := TGLColor.Create(Self);
195
//setup initial parameters
196
FAmbientColor.SetColor(0.15, 0.15, 0.15, 1);
197
FDiffuseColor.SetColor(1, 1, 1, 1);
198
FSpecularColor.SetColor(1, 1, 1, 1);
200
FSpecularPower := 8; //6
204
destructor TGLCustomGLSLLatticeShader.Destroy;
206
FAmbientColor.Destroy;
207
FDiffuseColor.Destroy;
208
FSpecularColor.Destroy;
213
procedure TGLCustomGLSLLatticeShader.DoApply(var rci: TGLRenderContextInfo; Sender: TObject);
218
Param['AmbientColor'].AsVector4f := FAmbientColor.Color;
219
Param['DiffuseColor'].AsVector4f := FDiffuseColor.Color;
220
Param['SpecularColor'].AsVector4f := FSpecularColor.Color;
222
Param['SpecPower'].AsVector1f := FSpecularPower;
223
Param['LightIntensity'].AsVector1f := FLightPower;
225
Param['MainTexture'].AsTexture2D[0] := FMainTexture;
229
procedure TGLCustomGLSLLatticeShader.DoInitialize(var rci : TGLRenderContextInfo; Sender : TObject);
231
with VertexProgram.Code do
234
Add('varying vec3 Normal; ');
235
Add('varying vec3 LightVector; ');
236
Add('varying vec3 CameraVector; ');
237
Add('varying vec2 Texcoord; ');
240
Add('void main(void) ');
242
Add(' gl_Position = ftransform(); ');
243
Add(' Texcoord = gl_MultiTexCoord0.xy; ');
244
Add(' Normal = normalize(gl_NormalMatrix * gl_Normal); ');
245
Add(' vec3 p = (gl_ModelViewMatrix * gl_Vertex).xyz; ');
246
Add(' LightVector = normalize(gl_LightSource[0].position.xyz - p); ');
247
Add(' CameraVector = normalize(p); ');
251
with FragmentProgram.Code do
254
Add(' uniform vec2 Scale; ');
255
Add(' uniform vec2 Threshold; ');
257
Add('uniform vec4 AmbientColor; ');
258
Add('uniform vec4 DiffuseColor; ');
259
Add('uniform vec4 SpecularColor; ');
261
Add('uniform float LightIntensity; ');
262
Add('uniform float SpecPower; ');
263
Add('uniform sampler2D MainTexture; ');
265
Add('varying vec3 Normal; ');
266
Add('varying vec3 LightVector; ');
267
Add('varying vec3 CameraVector; ');
268
Add('varying vec2 Texcoord; ');
270
Add('void main(void) ');
272
Add(' float ss = fract(Texcoord[0] * Scale.s); ');
273
Add(' float tt = fract(Texcoord[1] * Scale.t); ');
275
Add(' if ((ss > Threshold.s) && (tt > Threshold.t)) discard; ');
277
Add(' vec4 TextureContrib = texture2D(MainTexture, Texcoord); ');
278
Add(' vec4 DiffuseContrib = clamp(DiffuseColor * dot(LightVector, Normal), 0.0, 1.0); ');
280
Add(' vec3 reflect_vec = reflect(CameraVector, -Normal); ');
281
Add(' float Temp = dot(reflect_vec, LightVector); ');
282
Add(' vec4 SpecContrib = SpecularColor * clamp(pow(Temp, SpecPower), 0.0, 0.95); ');
284
Add(' gl_FragColor = TextureContrib * LightIntensity * (AmbientColor + DiffuseContrib) + LightIntensity * SpecContrib; ');
291
function TGLCustomGLSLLatticeShader.GetMaterialLibrary: TGLAbstractMaterialLibrary;
293
Result := FMaterialLibrary;
296
procedure TGLCustomGLSLLatticeShader.SetMaterialLibrary(const Value: TGLAbstractMaterialLibrary);
298
if FMaterialLibrary <> nil then FMaterialLibrary.RemoveFreeNotification(Self);
299
FMaterialLibrary := Value;
300
if (FMaterialLibrary <> nil)
301
and (FMaterialLibrary is TGLAbstractMaterialLibrary) then
302
FMaterialLibrary.FreeNotification(Self);
305
procedure TGLCustomGLSLLatticeShader.SetMainTexTexture(const Value: TGLTexture);
307
if FMainTexture = Value then Exit;
308
FMainTexture := Value;
312
function TGLCustomGLSLLatticeShader.GetMainTexName: TGLLibMaterialName;
314
Result := TGLMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FMainTexture);
315
if Result = '' then Result := FMainTexName;
318
procedure TGLCustomGLSLLatticeShader.SetMainTexName(const Value: TGLLibMaterialName);
320
// Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
321
if FMainTexName = Value then Exit;
322
FMainTexName := Value;
324
FMainTexture := TGLMaterialLibrary(FMaterialLibrary).TextureByName(FMainTexName);
329
procedure TGLCustomGLSLLatticeShader.SetDiffuseColor(AValue: TGLColor);
331
FDiffuseColor.DirectColor := AValue.Color;
334
procedure TGLCustomGLSLLatticeShader.SetAmbientColor(AValue: TGLColor);
336
FAmbientColor.DirectColor := AValue.Color;
339
procedure TGLCustomGLSLLatticeShader.SetSpecularColor(AValue: TGLColor);
341
FSpecularColor.DirectColor := AValue.Color;
344
procedure TGLCustomGLSLLatticeShader.Notification(AComponent: TComponent; Operation: TOperation);
349
if Operation = opRemove then
350
if AComponent = FMaterialLibrary then
351
if FMaterialLibrary <> nil then
354
if FMainTexture <> nil then
356
Index := TGLMaterialLibrary(FMaterialLibrary).Materials.GetTextureIndex(FMainTexture);
358
SetMainTexTexture(nil);
361
FMaterialLibrary := nil;