forked from neurolabusc/MRIcroGL10_OLD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaderui.pas
executable file
·361 lines (331 loc) · 10.4 KB
/
shaderui.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
unit shaderui;
{$D-,O+,Q-,R-,S-} //Delphi only L- Y-
{$IFDEF FPC}
{$mode delphi} {$H+}
{$ENDIF}
interface
{$include options.inc}
uses
{$IFDEF DGL} dglOpenGL, {$ELSE} gl, glext, {$ENDIF}
{$IFDEF FPC}FileUtil, GraphType, LCLProc, LCLIntf,LResources,OpenGLContext,{$ELSE}Windows,glpanel, {$ENDIF}
Graphics,Classes, SysUtils, Forms, Buttons,define_types,userdir,
Dialogs, ComCtrls, Menus, raycastglsl, Controls,
ExtCtrls, StdCtrls, shaderu;//, prefstextfx,;
procedure SetShader(lFilename: string);
function ShaderPanelHeight: integer;
function ShaderDir: string;
procedure FormCreateShaders;
procedure ZUniformChange(Sender: TObject);
procedure SetShaderAndDrop(lFilename: string);
procedure SetShaderAdjust(lProperty: string; lVal: single);
implementation
uses mainunit;
var
aCheck: array of TCheckbox;
aLabel: array of TLabel;
aTrack: array of TTrackbar;
gUpdateGLSL: boolean = false;
function Val2Percent (min,val,max: single): integer;
var
S: single;
begin
if max = min then
S := 0
else if max < min then
S := 100* ((val-max)/(min-max))
else
S := 100* ((val-min)/(max-min));
if S < 0 then
S := 0;
if S > 100 then
S := 100;
result := round(S);
end;
{$IFDEF FPC}
const
{$IFDEF WINDOWS}
//kTrackHt = 30;
kH = 30;//height
{$ELSE}
kH = 30;//height
{$ENDIF}
{$ELSE}
const
kH = 30;//height
{$ENDIF}
function ControlTop(N: integer): integer;
var
kT: integer;
begin
{$IFDEF FPC}
{$IFDEF WINDOWS}
kT := GLForm1.ElevTrack.top+2; //Check with Windows * and 150% scaling!
{$ELSE}
{$IFDEF Darwin}
kT := GLForm1.ElevTrack.top; //do this dynamically - if user adjusts text size in Windows7, the position of static controls changes!
{$ELSE} //Linux
kT := GLForm1.ElevTrack.top+4; //do this dynamically - if user adjusts text size in Windows7, the position of static controls changes!
{$ENDIF}
{$ENDIF}
{$ELSE}
kT := GLForm1.ElevTrack.top+1; //do this dynamically - if user adjusts text size in Windows7, the position of static controls changes!
{$ENDIF}
result := kT+ (kH* N);
end;
function ShaderPanelHeight: integer;
begin
{$IFDEF FPC}
{$IFDEF WINDOWS}
result := 3 + GLForm1.ElevTrack.top+round((GLForm1.ElevTrack.height+1)* (gShader.nUniform+1.5));
{$ELSE}
result := controlTop(gShader.nUniform+1)+(kH div 2);
{$ENDIF}
{$ELSE}
result := controlTop(gShader.nUniform+1)+(kH div 2)-12;
{$ENDIF}
end;
procedure CreateControl(N: integer; var aLabel: TLabel; var aCheck: TCheckbox; var aTrack: TTrackbar);
const
//kH = 30;//height
kL1 = 6;
kL2 = 118;
var
lT: integer;
//kT: integer;
begin
(*{$IFDEF FPC}
kT := GLForm1.ShaderDrop.top+4{+GLForm1.ShaderDrop.height}; //do this dynamically - if user adjusts text size in Windows7, the position of static controls changes!
{$ELSE}
kT := GLForm1.ShaderDrop.top+1{+GLForm1.ShaderDrop.height}; //do this dynamically - if user adjusts text size in Windows7, the position of static controls changes!
{$ENDIF} *)
lT := ControlTop(N);
aLabel := TLabel.Create(GLForm1);
aLabel.Parent := GLForm1.ShaderBox;
aLabel.Visible := false;
aLabel.Caption := inttostr(N);//U.Name;
aLabel.Top := lT;//kT+ (kH* N);
aLabel.Left := kL1;
aCheck := TCheckbox.Create(GLForm1);
aCheck.Parent := GLForm1.ShaderBox;
aCheck.Visible := false;
aCheck.Top := lT;//kT+ (kH* N);
aCheck.Tag := N;
aCheck.Left := kL2;
{$IFDEF FPC}
aCheck.OnClick := GLForm1.UniformChange; //2015 aCheck.OnClick := @GLForm1.UniformChange;
{$ELSE}
aCheck.OnClick := GLForm1.UniformChange;
{$ENDIF}
aTrack := TTrackbar.Create(GLForm1);
aTrack.Parent := GLForm1.ShaderBox;
aTrack.TickStyle := tsNone;
aTrack.Visible := false;
{$IFDEF LINUX}
aTrack.Top := lT-10;
{$ELSE}
aTrack.Top := lT;
{$ENDIF}
aTrack.Tag := N;
aTrack.Left := kL2;
aTrack.Min := 0;
aTrack.Max := 100;
aTrack.Width := 140;
{$IFDEF FPC}{$IFDEF WINDOWS}
aTrack.Height := 30;
{$ENDIF} {$ENDIF}
{$IFDEF FPC}
aTrack.OnChange := GLForm1.UniformChange; //aTrack.OnChange := @GLForm1.UniformChange;
{$ELSE}
aTrack.OnChange := GLForm1.UniformChange;
{$ENDIF}
{$IFNDEF FPC}
aTrack.Height := kH; //Delphi7 uses a crazy default height
{$ENDIF}
end;
procedure CreateAllControls;
var
i: integer;
begin
gUpdateGLSL := true;
setlength(aLabel,kMaxUniform+1);
setlength(aCheck,kMaxUniform+1);
setlength(aTrack,kMaxUniform+1);
for i := 1 to kMaxUniform do
CreateControl(i, aLabel[i], aCheck[i], aTrack[i]);
gUpdateGLSL := false;
end;
procedure ShowUniform(N: integer; U: TUniform);
begin
if (n > kMaxUniform) or (n < 1) then
exit;
aLabel[n].Caption := U.Name;
aLabel[n].Visible := true;
if U.Widget = kBool then begin
aCheck[n].Visible := true;
aCheck[n].Checked := U.Bool;
end else
aCheck[n].visible := false;
if (U.Widget = kInt) or (U.Widget = kFloat) then begin
aTrack[n].Visible := true;
aTrack[n].position := Val2Percent(U.Min, U.DefaultV,U.Max);
end else
aTrack[n].visible := false;
end;
procedure SetShaderAdjust(lProperty: string; lVal: single);
var
UpperName: string;
i: integer;
begin
if gShader.nUniform < 1 then
exit;
UpperName := UpperCase(lProperty);
for i := 1 to gShader.nUniform do begin
if UpperName = upperCase(aLabel[i].Caption) then begin
if aCheck[i].visible then
aCheck[i].Checked := not (lVal = 0.0)
else
aTrack[i].position := Val2Percent(gShader.Uniform[i].Min, lVal,gShader.Uniform[i].Max);
GLForm1.UniformChange(nil);
end;//if property matches shader's caption
end; //for each uniform
end;
function ShaderDir: string;
begin
result := AppDir+'shaders'
end;
procedure SetShader(lFilename: string);
var
i : integer;
begin
gShader := LoadShader(lFilename);
//if length(aLabel) < kMaxUniform then
// exit;
gUpdateGLSL := true;
if gShader.nUniform > 0 then
for i := 1 to gShader.nUniform do
ShowUniform(i, gShader.Uniform[i]);
if gShader.nUniform < kMaxUniform then begin
for i := (gShader.nUniform+1) to kMaxUniform do begin
aLabel[i].Visible := false;
aCheck[i].Visible := false;
aTrack[i].Visible := false;
end;//for all unused
end; //not max uniforms
//GLForm1.UniformChange(nil);
GLForm1.ShaderBoxResize(nil);
//deleteGradients(gTexture3D);
M_refresh := true;
GLForm1.updatetimer.enabled := true;
AreaInitialized := false;
gUpdateGLSL := false;
GLForm1.Memo1.Lines.Clear;
GLForm1.Memo1.Lines.Add(gShader.note);
end;
procedure SetShaderAndDrop(lFilename: string);
//precedence: if filename is in ShadersDrop list, set ShaderDrop, else load directly
var
lName,lNameExt,lItem: string;
i: integer;
begin
lName := UPPERCASE(ExtractFileName(lFilename));
lNameExt := lName +'.TXT';
//showmessage(inttostr(ShaderDrop.Items.Count) + lFilename) ;
if GLForm1.ShaderDrop.Items.Count > 1 then begin
for i := 0 to (GLForm1.ShaderDrop.Items.Count-1) do begin
//Showmessage(lName + ' <> '+ ShaderDrop.Items[i]);
lItem := UPPERCASE (GLForm1.ShaderDrop.Items[i]);
if (lItem = lName) or (lItem = lNameExt) then begin
GLForm1.ShaderDrop.ItemIndex := i;
GLForm1.ShaderDropChange(nil);
exit;
end;
end;//for each shader
end; //if at least one shader
//only get here if filename not in shaderdrop - load directly from disk
if fileexists(lFilename) then
SetShader(lFilename);
end;
procedure UpdateShaderDrop (var LUTdrop: TComboBox);
var
lSearchRec: TSearchRec;
lF: ansistring;
lS: TStringList;
begin
LUTdrop.Items.Clear;
lS := TStringList.Create;
if FindFirst(ShaderDir+pathdelim+'*.txt', faAnyFile, lSearchRec) = 0 then
repeat
lF :=ExtractFileName(lSearchRec.Name);
if (length(lF) > 1) and (lF[1] <> '.') then //OSX can create hidden files
lS.Add(ChangeFileExt(ExtractFileName(lSearchRec.Name),'')) ;
//lS.Add(ChangeFileExt(ExtractFileName(lSearchRec.Name,'') ;
//lS.Add((ExtractFileName(lSearchRec.Name)))
until (FindNext(lSearchRec) <> 0);
FindClose(lSearchRec);
if lS.Count < 1 then begin;
showmessage('Error: unable to find any shaders in '+ShaderDir+pathdelim+'*.txt' );
LUTdrop.Items.Add('No shaders found');
Freeandnil(lS);
exit;
end;
lS.sort;
LUTdrop.Items.AddStrings(lS);
Freeandnil(lS);
end;//UpdateColorSchemes
procedure FormCreateShaders;
begin
CreateAllControls;
gShader.nUniform := 0;
//CreateAllControls;
UpdateShaderDrop(GLForm1.ShaderDrop);
GLForm1.ShaderDrop.ItemIndex := 0;
gShader := LoadShader(ShaderDir+pathdelim+GLForm1.ShaderDrop.Items[GLForm1.ShaderDrop.ItemIndex]);
end;
function boolstr(b: boolean): string;
begin
if b then
result := 'true'
else
result := 'false';
end;
function Track2S(Pct,Min,Max: single): single;
begin
if Max > Min then
result := Min + (Pct/100)*(Max-Min)
else
result := Min;
end;
function Track2I(Pct,Min,Max: single): integer;
begin
result := round(Track2S(Pct,Min,Max));
end;
procedure ZUniformChange(Sender: TObject);
var
i: integer;
begin
if gUpdateGLSL then exit;
//GLForm1.updatetimer.enabled := true;
GLForm1.memo1.Lines.Clear;
if gShader.nUniform > 0 then
for i := 1 to gShader.nUniform do begin
case gShader.Uniform[i].Widget of
kBool: begin
if ACheck[i].visible then
gShader.Uniform[i].Bool := ACheck[i].checked;
GLForm1.memo1.lines.add('Bool '+ gShader.Uniform[i].name+' '+boolstr(gShader.Uniform[i].Bool) );
end;
kInt:begin
if aTrack[i].visible then
gShader.Uniform[i].DefaultV := Track2I(aTrack[i].Position, gShader.Uniform[i].Min,gShader.Uniform[i].Max) ;
GLForm1.memo1.lines.add('Int '+ gShader.Uniform[i].name+' '+ inttostr(round(gShader.Uniform[i].defaultV)) );
end;
kFloat:
begin
if aTrack[i].visible then
gShader.Uniform[i].DefaultV := Track2S(aTrack[i].Position, gShader.Uniform[i].Min,gShader.Uniform[i].Max) ;
GLForm1.memo1.lines.add('Float '+ gShader.Uniform[i].name+' '+ floattostrf(gShader.Uniform[i].defaultV,ffGeneral,4,4) );
end;
end;//case
end;//cor each item
end;
end.