Debian Codegraph @

src/scene/x3d/x3dnodes_standard_cubemaptexturing.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2008-2022 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 {$ifdef read_interface}
18 { Base type for cubic environment map textures. }
19 TAbstractEnvironmentTextureNode = class(TAbstractSingleTextureNode)
20 {$I auto_generated_node_helpers/x3dnodes_x3denvironmenttexturenode.inc}
21 end;
22
23 { Cube environment map texture defined as a six individual 2D texture nodes. }
24 TComposedCubeMapTextureNode = class(TAbstractEnvironmentTextureNode)
25 strict private
26 FAlphaChannelData: TAlphaChannel;
27 strict protected
28 function AlphaChannelData: TAlphaChannel; override;
29 public
30 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
31
32 { Make sure all 6 sides are loaded.
33 Make sure that all 6 fields for each cube map side are assigned,
34 are TAbstractTexture2DNode instance (X3D spec requires it),
35 load them, and check are they TCastleImage (we cannot support
36 TGPUCompressedImage when loading cubemaps this way,
37 as we have to rotate images from ComposedCubeMapTextureNode
38 when passing them to GPU, which is only possible for TCastleImage).
39
40 Also calculate AlphaChannel for the whole cube map.
41 Our AlphaChannel method will reflect the state from
42 last call of this. }
43 function LoadSides: boolean;
44
45 {$I auto_generated_node_helpers/x3dnodes_composedcubemaptexture.inc}
46 end;
47
48 { Cube environment map texture generated by rendering the 3D world,
49 useful for real-time mirrors. }
50 TGeneratedCubeMapTextureNode = class(TAbstractEnvironmentTextureNode)
51 strict private
52 type
53 TGenCubeMapGenTex = class(TGeneratedTextureFunctionality)
54 strict private
55 FParent: TGeneratedCubeMapTextureNode;
56 protected
57 function GetUpdate: TTextureUpdate; override;
58 procedure SetUpdate(const Value: TTextureUpdate); override;
59 public
60 constructor Create(const AParent: TGeneratedCubeMapTextureNode);
61 end;
62 var
63 FGenTexFunctionality: TGenCubeMapGenTex;
64 public
65 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
66 {$I auto_generated_node_helpers/x3dnodes_generatedcubemaptexture.inc}
67 end;
68
69 { Cube environment map texture loaded from a single file, like DDS. }
70 TImageCubeMapTextureNode = class(TAbstractEnvironmentTextureNode)
71 strict private
72 FAlphaChannelData: TAlphaChannel;
73 strict protected
74 function AlphaChannelData: TAlphaChannel; override;
75 public
76 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
77
78 { Load cube environment map from a composite (DDS, KTX...) image.
79
80 In case of problems, will make WritelnWarning.
81 This includes situations when url cannot be loaded for whatever reason.
82 Also problems when url contains valid Composite image,
83 but not describing cube map with all 6 sides.
84
85 If all URLs failed, will return @nil.
86
87 Although the loaded image is not saved here, we do save the AlphaChannel
88 type. Our AlphaChannel method will reflect last loaded image. }
89 function LoadImage: TCompositeImage;
90
91 {$I auto_generated_node_helpers/x3dnodes_imagecubemaptexture.inc}
92 end;
93
94 {$endif read_interface}
95
96 {$ifdef read_implementation}
97
98 { TComposedCubeMapTextureNode ------------------------------------------------ }
99
100 function TComposedCubeMapTextureNode.LoadSides: boolean;
101
102 { Checks is given side has non-nil valid node class,
103 and then if image there can be loaded. }
104 function SideLoaded(SideField: TSFNode): boolean;
105 var
106 SideTex: TAbstractTexture2DNode;
107 begin
108 Result :=
109 (SideField.Value <> nil) and
110 (SideField.Value is TAbstractTexture2DNode);
111 if Result then
112 begin
113 SideTex := TAbstractTexture2DNode(SideField.Value);
114 Result := SideTex.IsTextureImage;
115
116 if Result and not (SideTex.TextureImage is TCastleImage) then
117 begin
118 WritelnWarning('VRML/X3D', 'ComposedCubeMapTexture cannot contain images compressed GPU-compression algorithms, as we have to rotate images within, and we cannot do this (fast) with compressed textures');
119 Result := false;
120 end;
121
122 { If any slice has full-range alpha, then assume whole texture does. }
123 AlphaMaxVar(FAlphaChannelData, SideTex.AlphaChannelFinal);
124 end;
125 end;
126
127 begin
128 FAlphaChannelData := acNone;
129 Result :=
130 SideLoaded(FdBack) and
131 SideLoaded(FdBottom) and
132 SideLoaded(FdFront) and
133 SideLoaded(FdLeft) and
134 SideLoaded(FdRight) and
135 SideLoaded(FdTop);
136 end;
137
138 function TComposedCubeMapTextureNode.AlphaChannelData: TAlphaChannel;
139 begin
140 Result := FAlphaChannelData;
141 end;
142
143 { TGeneratedCubeMapTextureNode.TGenCubeMapGenTex -------------------------------- }
144
145 constructor TGeneratedCubeMapTextureNode.TGenCubeMapGenTex.Create(const AParent: TGeneratedCubeMapTextureNode);
146 begin
147 inherited Create(AParent);
148 FParent := AParent;
149 end;
150
151 function TGeneratedCubeMapTextureNode.TGenCubeMapGenTex.GetUpdate: TTextureUpdate;
152 begin
153 Result := FParent.Update;
154 end;
155
156 procedure TGeneratedCubeMapTextureNode.TGenCubeMapGenTex.SetUpdate(const Value: TTextureUpdate);
157 begin
158 FParent.Update := Value;
159 end;
160
161 { TCompositeCubeMapTextureNode ---------------------------------------------- }
162
163 constructor TComposedCubeMapTextureNode.Create(const AX3DName, ABaseUrl: String);
164 begin
165 inherited;
166 FdBack.AddAlternativeName('backTexture', 4);
167 FdBottom.AddAlternativeName('bottomTexture', 4);
168 FdFront.AddAlternativeName('frontTexture', 4);
169 FdLeft.AddAlternativeName('leftTexture', 4);
170 FdRight.AddAlternativeName('rightTexture', 4);
171 FdTop.AddAlternativeName('topTexture', 4);
172 end;
173
174 { TGeneratedCubeMapTextureNode ----------------------------------------------- }
175
176 constructor TGeneratedCubeMapTextureNode.Create(const AX3DName, ABaseUrl: String);
177 begin
178 inherited;
179 FGenTexFunctionality := TGenCubeMapGenTex.Create(Self);
180 AddFunctionality(FGenTexFunctionality);
181 end;
182
183 { TImageCubeMapTextureNode --------------------------------------------------- }
184
185 constructor TImageCubeMapTextureNode.Create(const AX3DName, ABaseUrl: String);
186 begin
187 inherited;
188 AddFunctionality(TUrlFunctionality.Create(Self));
189 end;
190
191 function TImageCubeMapTextureNode.LoadImage: TCompositeImage;
192 var
193 I: Integer;
194 FullUrl: String;
195 begin
196 Result := TCompositeImage.Create;
197 try
198 for I := 0 to FdUrl.Items.Count - 1 do
199 begin
200 FullUrl := PathFromBaseUrl(FdUrl.Items[I]);
201
202 if not TCompositeImage.MatchesURL(FullUrl) then
203 begin
204 WritelnWarning('VRML/X3D', Format('Only composite (DDS, KTX...) format is supported for ImageCubeMapTexture node, but URL is "%s"', [FullUrl]));
205 Continue;
206 end;
207
208 try
209 Result.LoadFromFile(FullUrl);
210 except
211 on E: Exception do
212 begin
213 Result.Close;
214 WritelnWarning('VRML/X3D', Format('Error when loading composite file "%s": %s', [FullUrl, E.Message]));
215 Continue;
216 end;
217 end;
218
219 FAlphaChannelData := Result.Images[0].AlphaChannel;
220
221 if Result.CompositeType <> ctCubeMap then
222 begin
223 Result.Close;
224 WritelnWarning('VRML/X3D', Format('Composite image "%s" given for ImageCubeMapTexture doesn''t describe a cube map texture', [FullUrl]));
225 Continue;
226 end;
227
228 if Result.CubeMapSides <> AllCubeMapSides then
229 begin
230 Result.Close;
231 WritelnWarning('VRML/X3D', Format('Composite image "%s" given for ImageCubeMapTexture doesn''t contain all cube map sides', [FullUrl]));
232 Continue;
233 end;
234
235 Exit;
236 end;
237
238 { If we got here, then no URL was good. So set Result to @nil. }
239 FreeAndNil(Result);
240 except FreeAndNil(Result); raise end;
241 end;
242
243 function TImageCubeMapTextureNode.AlphaChannelData: TAlphaChannel;
244 begin
245 Result := FAlphaChannelData;
246 end;
247
248 procedure RegisterCubeMapTexturingNodes;
249 begin
250 NodesManager.RegisterNodeClasses([
251 TComposedCubeMapTextureNode,
252 TGeneratedCubeMapTextureNode,
253 TImageCubeMapTextureNode
254 ]);
255 end;
256
257 {$endif read_implementation}