Debian Codegraph @

src/scene/castlerendererinternaltextureenv.pas

raw
1 {
2 Copyright 2002-2018 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Texture environment detection.
17 @exclude Internal unit for CastleRenderer and CastleRendererInternalShader. }
18 unit CastleRendererInternalTextureEnv;
19
20 {$I castleconf.inc}
21
22 interface
23
24 type
25 { Source of color for texture mixing.
26 Used to represent MultiTexture.source X3D field.
27 And used to represent the source of alpha value,
28 for MultiTexture.mode = BLEND* case. }
29 TColorSource = (
30 { Material (OpenGL calls this "primary color"). }
31 csMaterial,
32 { Current texture unit color. }
33 csCurrentTexture,
34 { Constant color. (MultiTexture.color/alpha fields.)
35 When this value is used for @link(TTextureEnv.BlendAlphaSource),
36 it indicates only MultiTexture.alpha. }
37 csConstant,
38 { Previous texture unit color, or material (in case this is the first texture unit). }
39 csPreviousTexture);
40
41 { OpenGL texture environment argument: none, GL_SOURCE0 or GL_SOURCE1. }
42 TTextureEnvArgument = (taNone, ta0, ta1);
43
44 { Function for texture mixing, corresponds to X3D MultiTexture.function field. }
45 TTextureFunction = (tfNone, tfComplement, tfAlphaReplicate);
46
47 { How to combine textures, on a single channel. }
48 TCombine = (coModulate, coReplace, coAddSigned, coAdd, coSubtract, coBlend,
49 coDot3Rgb, coDot3Rgba);
50
51 TChannel = (cRGB, cAlpha);
52 TCombinePerChannel = array [TChannel] of TCombine;
53 TArgPerChannel = array [TChannel] of TTextureEnvArgument;
54 TScalePerChannel = array [TChannel] of Single;
55 TSourcePerChannel = array [TChannel] of TColorSource;
56
57 { How to mix the current texture color into the fragment color. }
58 TTextureEnv = record
59 public
60 { How to calculate given fragment channel using this texture unit.
61 Returned values correspond to parameters of
62 GL_COMBINE_RGB_EXT, GL_COMBINE_ALPHA_EXT.
63 For some details about particular values,
64 see OpenGL EXT_texture_env_combine extension
65 (http://www.opengl.org/registry/specs/EXT/texture_env_combine.txt). }
66 Combine: TCombinePerChannel;
67
68 { Where should we load the current texture unit
69 (this is "Arg1" in X3D spec wording; Arg1 is always current texture,
70 indicated by X3D spec wording "The source field determines
71 the colour source for the second argument.") }
72 CurrentTextureArgument: TArgPerChannel;
73
74 { Where should we load the @link(Source) color. This is "Arg2" in X3D spec. }
75 SourceArgument: TArgPerChannel;
76
77 { Scaling of given channel value.
78 Fixed-function pipeline OpenGL allows only 1.0, 2.0 and 4.0 scales,
79 and only such values will be set by TTextureEnv.Init. }
80 Scale: TScalePerChannel;
81
82 Disabled: boolean;
83 NeedsConstantColor: boolean;
84
85 { If, and only if, one of SourceArgument is not taNone,
86 then SourceArgument should be loaded with this color. }
87 Source: TSourcePerChannel;
88
89 TextureFunction: TTextureFunction;
90
91 { If, and only if, one of Combine is coBlend,
92 then this specifies what is the OpenGL GL_SOURCE2.
93 It should be filled (both RGB and alpha) with alpha from this source. }
94 BlendAlphaSource: TColorSource;
95
96 { Initialize based on MultiTexture.mode, MultiTexture.source,
97 MultiTexture.function values.
98 This does not setup any OpenGL state, it only calculates fields
99 of this object. }
100 constructor Init(const Mode, SourceStr, FunctionStr: string); overload;
101
102 { Calculate values based on simple OpenGL mode value. }
103 constructor Init(const Mode: TCombine); overload;
104
105 function Hash: UInt32;
106 end;
107
108 implementation
109
110 uses SysUtils, CastleStringUtils, CastleLog;
111
112 { Simple type constructors, for ease of coding.
113 Versions with only 1 argument set both channel (rgb and alpha) to the same. }
114 function CombinePerChannel(const RGB, Alpha: TCombine): TCombinePerChannel; overload;
115 begin
116 Result[cRGB] := RGB;
117 Result[cAlpha] := Alpha;
118 end;
119
120 function CombinePerChannel(const Value: TCombine): TCombinePerChannel; overload;
121 begin
122 Result := CombinePerChannel(Value, Value);
123 end;
124
125 function ArgPerChannel(const Value: TTextureEnvArgument): TArgPerChannel;
126 begin
127 Result[cRGB] := Value;
128 Result[cAlpha] := Value;
129 end;
130
131 type
132 TStringPerChannel = array [TChannel] of string;
133
134 { If S contains two separate modes (one for RGB, one for Alpha)
135 returns @true and sets PerChannel to these separate strings.
136 Strings returned in PerChannel will not contain the separator
137 (slash, comma), and will not contain whitespace. }
138 function SplitStringPerChannel(const S: string;
139 out PerChannel: TStringPerChannel): boolean;
140 var
141 P: Integer;
142 begin
143 P := CharsPos(['/', ','], S);
144 Result := P > 0;
145 if Result then
146 begin
147 PerChannel[cRGB] := Trim(Copy(S, 1, P - 1));
148 PerChannel[cAlpha] := Trim(SEnding(S, P + 1));
149 end;
150 end;
151
152 { Calculate values knowing MultiTexture.mode value. }
153 procedure ModeFromString(const S: string;
154 out Combine: TCombinePerChannel;
155 out CurrentTextureArgument: TArgPerChannel;
156 out SourceArgument: TArgPerChannel;
157 out Scale: TScalePerChannel;
158 out Disabled: boolean;
159 out NeedsConstantColor: boolean;
160 out BlendAlphaSource: TColorSource);
161
162 { Interpret simple mode name (this is for sure only one mode,
163 without any "/" and whitespaces). This handles only the simplest
164 modes, that behave the same and are allowed separately for
165 both RGB and alpha channel.
166
167 LS passed here must already be lowercase.
168
169 Scale passed here must be initially 1.0. }
170 procedure SimpleModeFromString(
171 const LS: string;
172 out Combine: TCombine;
173 out CurrentTextureArgument, SourceArgument: TTextureEnvArgument;
174 var Scale: Single;
175 const Channels: string);
176 begin
177 if LS = 'modulate' then
178 begin
179 Combine := coModulate;
180 CurrentTextureArgument := ta0;
181 SourceArgument := ta1;
182 end else
183 if LS = 'modulate2x' then
184 begin
185 Combine := coModulate;
186 CurrentTextureArgument := ta0;
187 SourceArgument := ta1;
188 Scale := 2;
189 end else
190 if LS = 'modulate4x' then
191 begin
192 Combine := coModulate;
193 CurrentTextureArgument := ta0;
194 SourceArgument := ta1;
195 Scale := 4;
196 end else
197 if (LS = 'replace') or (LS = 'selectarg1') then
198 begin
199 { SELECTARG1 is exactly the same as REPLACE.
200
201 Note: don't get confused by X3D spec saying in table 18.3 that
202 "REPLACE" takes the Arg2, that's an error, it takes
203 from Arg1 to be consistent with other spec words.
204 I wrote some remarks about this on
205 https://castle-engine.io/x3d_implementation_status.php }
206
207 Combine := coReplace;
208 CurrentTextureArgument := ta0;
209 SourceArgument := taNone;
210 end else
211 if LS = 'selectarg2' then
212 begin
213 Combine := coReplace;
214 CurrentTextureArgument := taNone;
215 SourceArgument := ta0;
216 end else
217 if LS = 'add' then
218 begin
219 Combine := coAdd;
220 CurrentTextureArgument := ta0;
221 SourceArgument := ta1;
222 end else
223 if LS = 'addsigned' then
224 begin
225 Combine := coAddSigned;
226 CurrentTextureArgument := ta0;
227 SourceArgument := ta1;
228 end else
229 if LS = 'addsigned2x' then
230 begin
231 Combine := coAddSigned;
232 CurrentTextureArgument := ta0;
233 SourceArgument := ta1;
234 Scale := 2;
235 end else
236 if LS = 'subtract' then
237 begin
238 Combine := coSubtract;
239 CurrentTextureArgument := ta0;
240 SourceArgument := ta1;
241 end else
242 begin
243 Combine := coModulate;
244 CurrentTextureArgument := ta0;
245 SourceArgument := ta1;
246 WritelnWarning('VRML/X3D', Format('Not supported multi-texturing mode "%s" for channels "%s"', [LS, Channels]));
247 end;
248 end;
249
250 procedure RGBModeFromString(
251 const LS: string;
252 out Combine: TCombine;
253 out CurrentTextureArgument, SourceArgument: TTextureEnvArgument;
254 var Scale: Single);
255 begin
256 if LS = 'dotproduct3' then
257 begin
258 { We use DOT3_RGB_ARB here.
259 This means it will fill only RGB values.
260
261 This is our extension (X3D spec allows only DOTPRODUCT3
262 for both channels, and to fill them both, this case is handled
263 in BothModesFromString). }
264 Combine := coDot3Rgb;
265 CurrentTextureArgument := ta0;
266 SourceArgument := ta1;
267 end else
268 if LS = 'blenddiffusealpha' then
269 begin
270 Combine := coBlend;
271 CurrentTextureArgument := ta0;
272 SourceArgument := ta1;
273 { Note that BlendAlphaSource is one variable, not split into RGB or alpha
274 parts. Fortunately, the BLEND* mode is either on RGBA or RGB,
275 it cannot be used on alpha channel only for now,
276 so we can keep BlendAlphaSource in one variable. }
277 BlendAlphaSource := csMaterial;
278 end else
279 if LS = 'blendtexturealpha' then
280 begin
281 Combine := coBlend;
282 CurrentTextureArgument := ta0;
283 SourceArgument := ta1;
284 BlendAlphaSource := csCurrentTexture;
285 end else
286 if LS = 'blendfactoralpha' then
287 begin
288 Combine := coBlend;
289 CurrentTextureArgument := ta0;
290 SourceArgument := ta1;
291 BlendAlphaSource := csConstant;
292 NeedsConstantColor := true;
293 end else
294 if LS = 'blendcurrentalpha' then
295 begin
296 Combine := coBlend;
297 CurrentTextureArgument := ta0;
298 SourceArgument := ta1;
299 BlendAlphaSource := csPreviousTexture;
300 end else
301 SimpleModeFromString(LS, Combine, CurrentTextureArgument, SourceArgument, Scale, 'RGB');
302 end;
303
304 procedure AlphaModeFromString(
305 const LS: string;
306 out Combine: TCombine;
307 out CurrentTextureArgument, SourceArgument: TTextureEnvArgument;
308 var Scale: Single);
309 begin
310 SimpleModeFromString(LS, Combine, CurrentTextureArgument, SourceArgument, Scale, 'Alpha');
311 end;
312
313 procedure BothModesFromString(
314 const LS: string;
315 out Combine: TCombinePerChannel;
316 out CurrentTextureArgument, SourceArgument: TArgPerChannel;
317 var Scale: TScalePerChannel);
318 begin
319 if LS = '' then
320 begin
321 { LS = '' means that mode list was too short.
322 X3D spec says explicitly that default mode is "MODULATE"
323 in this case. (Accidentally, this also will accept
324 explict "" string as "MODULATE" --- not a worry, we don't
325 have to produce error messages for all possible invalid VRMLs...). }
326
327 Combine := CombinePerChannel(coModulate);
328 CurrentTextureArgument := ArgPerChannel(ta0);
329 SourceArgument := ArgPerChannel(ta1);
330 end else
331 if LS = 'off' then
332 begin
333 Disabled := true;
334 end else
335 if LS = 'dotproduct3' then
336 begin
337 { We use DOT3_RGBA_ARB, not DOT3_RGB_ARB.
338 See [http://www.opengl.org/registry/specs/ARB/texture_env_dot3.txt].
339
340 This means that the dot (done on only RGB channels) will
341 be replicated to all four channels (RGBA). This is exactly what
342 the X3D specification requires, so we're happy.
343 Yes, this means that COMBINE_ALPHA_ARB will be ignored. }
344
345 Combine := CombinePerChannel(coDot3Rgba,
346 coReplace { <- whatever, alpha combine will be ignored });
347 CurrentTextureArgument := ArgPerChannel(ta0);
348 SourceArgument := ArgPerChannel(ta1);
349 end else
350 if LS = 'blenddiffusealpha' then
351 begin
352 Combine := CombinePerChannel(coBlend);
353 CurrentTextureArgument := ArgPerChannel(ta0);
354 SourceArgument := ArgPerChannel(ta1);
355 BlendAlphaSource := csMaterial;
356 end else
357 if LS = 'blendtexturealpha' then
358 begin
359 Combine := CombinePerChannel(coBlend);
360 CurrentTextureArgument := ArgPerChannel(ta0);
361 SourceArgument := ArgPerChannel(ta1);
362 BlendAlphaSource := csCurrentTexture;
363 end else
364 if LS = 'blendfactoralpha' then
365 begin
366 Combine := CombinePerChannel(coBlend);
367 CurrentTextureArgument := ArgPerChannel(ta0);
368 SourceArgument := ArgPerChannel(ta1);
369 BlendAlphaSource := csConstant;
370 NeedsConstantColor := true;
371 end else
372 if LS = 'blendcurrentalpha' then
373 begin
374 Combine := CombinePerChannel(coBlend);
375 CurrentTextureArgument := ArgPerChannel(ta0);
376 SourceArgument := ArgPerChannel(ta1);
377 BlendAlphaSource := csPreviousTexture;
378 end else
379 begin
380 SimpleModeFromString(LS,
381 Combine[cRGB], CurrentTextureArgument[cRGB], SourceArgument[cRGB], Scale[cRGB], 'both RGB and Alpha');
382 Combine [cAlpha] := Combine [cRGB];
383 CurrentTextureArgument[cAlpha] := CurrentTextureArgument[cRGB];
384 SourceArgument [cAlpha] := SourceArgument [cRGB];
385 Scale [cAlpha] := Scale [cRGB];
386 end;
387 end;
388
389 var
390 LS: string;
391 StringPerChannel: TStringPerChannel;
392 begin
393 { initialize some out parameters to default values }
394 Disabled := false;
395 Scale[cRGB] := 1;
396 Scale[cAlpha] := 1;
397 NeedsConstantColor := false;
398 BlendAlphaSource := csMaterial; { unused, will define to be safe }
399
400 LS := LowerCase(S);
401 if SplitStringPerChannel(LS, StringPerChannel) then
402 begin
403 RGBModeFromString (StringPerChannel[cRGB ], Combine[cRGB ], CurrentTextureArgument[cRGB ], SourceArgument[cRGB ], Scale[cRGB ]);
404 AlphaModeFromString(StringPerChannel[cAlpha], Combine[cAlpha], CurrentTextureArgument[cAlpha], SourceArgument[cAlpha], Scale[cAlpha]);
405 end else
406 BothModesFromString(LS, Combine, CurrentTextureArgument, SourceArgument, Scale);
407 end;
408
409 { Calculate values knowing MultiTexture.source value. }
410 procedure SourceFromString(const S: string; out Source: TSourcePerChannel;
411 var NeedsConstantColor: boolean);
412
413 procedure SimpleSourceFromString(const LS: string;
414 out Source: TColorSource;
415 var NeedsConstantColor: boolean);
416 begin
417 if LS = '' then
418 Source := csPreviousTexture else
419 if (LS = 'diffuse') or (LS = 'specular') then
420 Source := csMaterial else
421 if LS = 'factor' then
422 begin
423 NeedsConstantColor := true;
424 Source := csConstant;
425 end else
426 begin
427 Source := csPreviousTexture;
428 WritelnWarning('VRML/X3D', Format('Not supported multi-texturing source "%s"', [LS]))
429 end;
430 end;
431
432 var
433 LS: string;
434 SourcePerChannel: TStringPerChannel;
435 begin
436 LS := LowerCase(S);
437 if SplitStringPerChannel(LS, SourcePerChannel) then
438 begin
439 SimpleSourceFromString(SourcePerChannel[cRGB ], Source[cRGB ], NeedsConstantColor);
440 SimpleSourceFromString(SourcePerChannel[cAlpha], Source[cAlpha], NeedsConstantColor);
441 end else
442 begin
443 SimpleSourceFromString(LS, Source[cRGB], NeedsConstantColor);
444 Source[cAlpha] := Source[cRGB];
445 end;
446 end;
447
448 { Convert MultiTexture.function string to TTextureFunction. }
449 function FunctionFromString(const S: string): TTextureFunction;
450 var
451 LS: string;
452 begin
453 LS := LowerCase(S);
454 if LS = '' then
455 Result := tfNone else
456 if LS = 'complement' then
457 Result := tfComplement else
458 if LS = 'alphareplicate' then
459 Result := tfAlphaReplicate else
460 begin
461 Result := tfNone;
462 WritelnWarning('VRML/X3D', Format('Invalid multi-texturing function "%s"', [S]));
463 end;
464 end;
465
466 constructor TTextureEnv.Init(const Mode, SourceStr, FunctionStr: string);
467 begin
468 ModeFromString(Mode, Combine, CurrentTextureArgument, SourceArgument, Scale, Disabled,
469 NeedsConstantColor, BlendAlphaSource);
470 if (SourceArgument[cRGB] <> taNone) or
471 (SourceArgument[cAlpha] <> taNone) then
472 SourceFromString(SourceStr, Source, NeedsConstantColor);
473 TextureFunction := FunctionFromString(FunctionStr);
474 end;
475
476 constructor TTextureEnv.Init(const Mode: TCombine);
477 begin
478 Combine := CombinePerChannel(Mode);
479
480 { constant default values for other fields }
481 CurrentTextureArgument := ArgPerChannel(ta0);
482 SourceArgument := ArgPerChannel(ta1);
483 Scale[cRGB ] := 1.0;
484 Scale[cAlpha] := 1.0;
485 Disabled := false;
486 NeedsConstantColor := false;
487 BlendAlphaSource := csMaterial;
488 Source[cRGB ] := csMaterial;
489 Source[cAlpha] := csMaterial;
490 TextureFunction := tfNone;
491 end;
492
493 function TTextureEnv.Hash: UInt32;
494 {$include norqcheckbegin.inc}
495 begin
496 Result :=
497 1693 * (1 + Ord(Combine[cRGB] )) +
498 1697 * (1 + Ord(Combine[cAlpha] )) +
499 1699 * (1 + Ord(CurrentTextureArgument[cRGB] )) +
500 1709 * (1 + Ord(CurrentTextureArgument[cAlpha])) +
501 1721 * (1 + Ord(SourceArgument[cRGB] )) +
502 1723 * (1 + Ord(SourceArgument[cAlpha] )) +
503 1733 * (1 + Round(Scale[cRGB] * 100 )) +
504 1741 * (1 + Round(Scale[cAlpha] * 100 )) +
505 1747 * (1 + Ord(Disabled )) +
506 1753 * (1 + Ord(NeedsConstantColor )) +
507 1759 * (1 + Ord(Source[cRGB] )) +
508 1777 * (1 + Ord(Source[cAlpha] )) +
509 1783 * (1 + Ord(BlendAlphaSource )) +
510 1787 * (1 + Ord(TextureFunction ));
511 end;
512 {$include norqcheckend.inc}
513
514 end.