Debian Codegraph @

src/scene/x3d/x3dnodes_utils_box.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2002-2023 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 { Make a box.
18
19 @param(MaterialIndex VRML 1.0 material index, generated like for
20 material binding per part. Will be used with
21 material binding BIND_PER_FACE_INDEXED. Pass @nil if you don't
22 need it.) }
23 procedure Box_Proxy(CoordIndex: TInt32List;
24 Coord: TVector3List; Normal: TVector3List;
25 TexCoord: TVector2List; MaterialIndex: TInt32List;
26 Sizes: TVector3);
27
28 var
29 Detail: TVector3Cardinal;
30
31 { Make a rectangle that changes along the RestOf3DCoords(ConstCoord, ...).
32 E.g. for ConstCoord = 0, it changes along YZ (so X below corresponds
33 actually to global Y, and Y below corresponds actually to global Z).
34
35 X1, Y1 specify the corner of the 1st vertex, we go CCW on the face
36 to the (-X1, -Y1) corner. So the sign of X1, Y1 determine which side
37 of the face is CCW.
38
39 @param(TextureSIsY By default, texture S goes along X and texture T
40 goes along Y. Pass TextureSIsY = @true to change it.) }
41 procedure MakeRect(const ConstCoord: T3DAxis; const ConstCoordValue: Single;
42 const X1, Y1: Single; const TexX1, TexY1: Single;
43 const FaceMaterialIndex: Integer;
44 const TextureSIsY: boolean = false);
45
46 function MakeTexCoord(const S, T: Single): TVector2;
47 begin
48 if TextureSIsY then
49 begin
50 Result.X := T;
51 Result.Y := S;
52 end else
53 begin
54 Result.X := S;
55 Result.Y := T;
56 end;
57 end;
58
59 var
60 FaceIndex, Index: Integer;
61 C1, C2: T3DAxis;
62 X2, Y2, TexX2, TexY2: Single;
63 I, J, NewCoords, ThisIndex: Integer;
64 N: TVector3;
65 begin
66 RestOf3DCoords(ConstCoord, C1, C2);
67
68 Index := Coord.Count;
69 NewCoords := (Detail[C1] + 2) * (Detail[C2] + 2);
70 Coord.Count := Coord.Count + NewCoords;
71 Normal.Count := Normal.Count + NewCoords;
72 if TexCoord <> nil then TexCoord.Count := TexCoord.Count + NewCoords;
73
74 X2 := -X1;
75 Y2 := -Y1;
76 TexX2 := 1 - TexX1;
77 TexY2 := 1 - TexY1;
78
79 { Normal vector is constant for the whole rectangle }
80 if ConstCoordValue > 0 then
81 N.Data[ConstCoord] := 1
82 else
83 N.Data[ConstCoord] := -1;
84 N.Data[C1] := 0;
85 N.Data[C2] := 0;
86
87 for I := 0 to Detail[C1] + 1 do
88 for J := 0 to Detail[C2] + 1 do
89 begin
90 ThisIndex := Index + I * (Detail[C2] + 2) + J;
91
92 Coord.L[ThisIndex].Data[C1] := Lerp(I / (Detail[C1] + 1), X1, X2);
93 Coord.L[ThisIndex].Data[C2] := Lerp(J / (Detail[C2] + 1), Y1, Y2);
94 Coord.L[ThisIndex].Data[ConstCoord] := ConstCoordValue;
95
96 Normal.L[ThisIndex] := N;
97
98 if TexCoord <> nil then
99 TexCoord.L[ThisIndex] := MakeTexCoord(
100 Lerp(I / (Detail[C1] + 1), TexX1, TexX2),
101 Lerp(J / (Detail[C2] + 1), TexY1, TexY2));
102 end;
103
104 for I := 1 to Detail[C1] + 1 do
105 for J := 1 to Detail[C2] + 1 do
106 begin
107 FaceIndex := CoordIndex.Count;
108 CoordIndex.Count := CoordIndex.Count + 5;
109 CoordIndex.L[FaceIndex ] := Index + (I - 1) * (Detail[C2] + 2) + J - 1;
110 CoordIndex.L[FaceIndex + 1] := Index + I * (Detail[C2] + 2) + J - 1;
111 CoordIndex.L[FaceIndex + 2] := Index + I * (Detail[C2] + 2) + J;
112 CoordIndex.L[FaceIndex + 3] := Index + (I - 1) * (Detail[C2] + 2) + J;
113 CoordIndex.L[FaceIndex + 4] := -1;
114 end;
115
116 if MaterialIndex <> nil then
117 MaterialIndex.AddDuplicate(FaceMaterialIndex, (Detail[C1] + 1) * (Detail[C2] + 1));
118 end;
119
120 begin
121 { For VRML 1.0, some of these MF fields by default have non-empty content.
122 It's safest to just clean them. }
123 CoordIndex.Count := 0;
124 Coord.Count := 0;
125 Normal.Count := 0;
126 if TexCoord <> nil then TexCoord.Count := 0;
127 if MaterialIndex <> nil then MaterialIndex.Count := 0;
128
129 { Currently, all three Detail* used are always equal.
130 Although the code is written to support different Detail* values,
131 just in case in the future it will be useful.
132
133 TODO: simplify the surrounding code, because right now
134 Detail is always 0. }
135 Detail := TVector3Cardinal.Zero;
136
137 Sizes := Sizes / 2;
138
139 MakeRect(0, -Sizes.X, +Sizes.Y, -Sizes.Z, 1, 0, 2, true);
140 MakeRect(0, Sizes.X, -Sizes.Y, -Sizes.Z, 0, 1, 3, true);
141 MakeRect(1, -Sizes.Y, -Sizes.X, -Sizes.Z, 0, 0, 5);
142 MakeRect(1, Sizes.Y, +Sizes.X, -Sizes.Z, 1, 1, 4);
143 MakeRect(2, -Sizes.Z, +Sizes.X, -Sizes.Y, 0, 0, 1);
144 MakeRect(2, Sizes.Z, -Sizes.X, -Sizes.Y, 0, 0, 0);
145 end;
146
147 { Make a wireframe box. }
148 procedure BoxWireframe_Proxy(CoordIndex: TInt32List;
149 Coord: TVector3List; Sizes: TVector3);
150 begin
151 Sizes := Sizes / 2;
152
153 Coord.AddRange([
154 Vector3(-Sizes.X, -Sizes.Y, -Sizes.Z),
155 Vector3( Sizes.X, -Sizes.Y, -Sizes.Z),
156 Vector3( Sizes.X, Sizes.Y, -Sizes.Z),
157 Vector3(-Sizes.X, Sizes.Y, -Sizes.Z),
158
159 Vector3(-Sizes.X, -Sizes.Y, Sizes.Z),
160 Vector3( Sizes.X, -Sizes.Y, Sizes.Z),
161 Vector3( Sizes.X, Sizes.Y, Sizes.Z),
162 Vector3(-Sizes.X, Sizes.Y, Sizes.Z)
163 ]);
164
165 // Each pair of indexes forms a line, matching TIndexedLineSetNode with Mode = lmPair
166 CoordIndex.AddRange([
167 // line loop on Z = min
168 0, 1,
169 1, 2,
170 2, 3,
171 3, 0,
172
173 // line loop on Z = max
174 4, 5,
175 5, 6,
176 6, 7,
177 7, 4,
178
179 // connect Z = min with Z = max
180 0, 4,
181 1, 5,
182 2, 6,
183 3, 7
184 ]);
185 end;
186
187 function TBoxNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode;
188
189 procedure BoxWireframe;
190 var
191 CoordNode: TCoordinateNode;
192 ILS: TIndexedLineSetNode absolute Result;
193 begin
194 ILS := TIndexedLineSetNode.Create(X3DName, BaseUrl);
195 try
196 CoordNode := TCoordinateNode.Create('', BaseUrl);
197 ILS.FdCoord.Value := CoordNode;
198 ILS.Mode := lmPair;
199
200 BoxWireframe_Proxy(ILS.FdCoordIndex.Items, CoordNode.FdPoint.Items,
201 FdSize.Value);
202 except FreeAndNil(Result); raise end;
203 end;
204
205 procedure BoxFilled;
206 var
207 CoordNode: TCoordinateNode;
208 NormalNode: TNormalNode;
209 TexCoordNode: TTextureCoordinateNode;
210 TexCoords: TVector2List;
211 IFS: TIndexedFaceSetNode absolute Result;
212 begin
213 IFS := TIndexedFaceSetNode.Create(X3DName, BaseUrl);
214 try
215 CoordNode := TCoordinateNode.Create('', BaseUrl);
216 IFS.FdCoord.Value := CoordNode;
217
218 NormalNode := TNormalNode.Create('', BaseUrl);
219 IFS.FdNormal.Value := NormalNode;
220 IFS.FdNormalPerVertex.Value := true;
221
222 if (FdTexCoord.Value <> nil) and FdTexCoord.CurrentChildAllowed then
223 begin
224 { No need for Box_Proxy to create tex coords. }
225 IFS.FdTexCoord.Value := FdTexCoord.Value;
226 TexCoords := nil;
227 end else
228 begin
229 TexCoordNode := TTextureCoordinateNode.Create('', BaseUrl);
230 IFS.FdTexCoord.Value := TexCoordNode;
231 TexCoords := TexCoordNode.FdPoint.Items;
232 end;
233
234 Box_Proxy(IFS.FdCoordIndex.Items,
235 CoordNode.FdPoint.Items, NormalNode.FdVector.Items, TexCoords, nil,
236 FdSize.Value);
237
238 IFS.FdSolid.Value := FdSolid.Value;
239 IFS.FdCcw.Value := FdCcw.Value;
240
241 { Box_Proxy generated normals from CCW,
242 we need to flip them if CW is front face }
243 if not FdCcw.Value then
244 NormalNode.FdVector.Items.Negate;
245
246 { Smooth nothing. Not really needed, we use explicit normal node now. }
247 IFS.FdCreaseAngle.Value := 0;
248 except FreeAndNil(Result); raise end;
249 end;
250
251 begin
252 if (State.ShapeNode <> nil) and
253 (State.ShapeNode.Shading = shWireframe) then
254 BoxWireframe
255 else
256 BoxFilled;
257 end;
258
259 {$warnings off} // using deprecated to keep supporting VRML 1
260 function TCubeNode_1.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode;
261 var
262 CoordNode: TCoordinate3Node_1;
263 NormalNode: TNormalNode;
264 NormalBinding: TNormalBindingNode_1;
265 TexCoordNode: TTextureCoordinate2Node_1;
266 ShapeHints: TShapeHintsNode_1;
267 MaterialBinding: TMaterialBindingNode_1;
268 MaterialIndex: TInt32List;
269 IFS: TIndexedFaceSetNode_1 absolute Result;
270 begin
271 IFS := TIndexedFaceSetNode_1.Create(X3DName, BaseUrl);
272 try
273 { we have to modify State, so make a copy of it }
274 State := TX3DGraphTraverseState.CreateCopy(State);
275
276 CoordNode := TCoordinate3Node_1.Create('', BaseUrl);
277 State.VRML1State.SetOwnNode(vsCoordinate3, CoordNode);
278
279 NormalNode := TNormalNode.Create('', BaseUrl);
280 State.VRML1State.SetOwnNode(vsNormal, NormalNode);
281
282 NormalBinding := TNormalBindingNode_1.Create('', BaseUrl);
283 { NormalBinding.value = PER_VERTEX means we use niPerVertexCoordIndexed,
284 so coordIndex chooses the normal. }
285 NormalBinding.FdValue.Value := BIND_PER_VERTEX;
286 State.VRML1State.SetOwnNode(vsNormalBinding, NormalBinding);
287
288 TexCoordNode := TTextureCoordinate2Node_1.Create('', BaseUrl);
289 State.VRML1State.SetOwnNode(vsTextureCoordinate2, TexCoordNode);
290
291 ShapeHints := TShapeHintsNode_1.Create('', BaseUrl);
292 { For VRML 1.0, Box is never solid. }
293 ShapeHints.FdshapeType.Value := SHTYPE_UNKNOWN;
294 ShapeHints.FdvertexOrdering.Value := VERTORDER_COUNTERCLOCKWISE;
295 { Smooth nothing. Not really needed, we use explicit normal node now. }
296 ShapeHints.FdCreaseAngle.Value := 0;
297 State.VRML1State.SetOwnNode(vsShapeHints, ShapeHints);
298
299 { calculate MaterialBinding node and MaterialIndex }
300 MaterialBinding := TMaterialBindingNode_1.Create('', BaseUrl);
301 if State.VRML1State.MaterialBinding.FdValue.Value in
302 [BIND_PER_PART, BIND_PER_PART_INDEXED,
303 BIND_PER_FACE, BIND_PER_FACE_INDEXED] then
304 begin
305 MaterialIndex := IFS.FdMaterialIndex.Items;
306 MaterialBinding.FdValue.Value := BIND_PER_FACE_INDEXED;
307 end else
308 begin
309 MaterialIndex := nil;
310 MaterialBinding.FdValue.Value := BIND_OVERALL;
311 end;
312 State.VRML1State.SetOwnNode(vsMaterialBinding, MaterialBinding);
313
314 Box_Proxy(IFS.FdCoordIndex.Items,
315 CoordNode.FdPoint.Items, NormalNode.FdVector.Items,
316 TexCoordNode.FdPoint.Items, MaterialIndex,
317 Vector3(FdWidth.Value, FdHeight.Value, FdDepth.Value));
318
319 { For VRML 1.0, unfortunately textureCoordIndex must be set
320 (even though it's exactly equivalent to coordIndex).
321 This is a problem of VRML 1.0 "state" idea --- there is no
322 other way to "turn off" texture than to just use empty textureCoordIndex. }
323 IFS.FdTextureCoordIndex.Items.Assign(IFS.FdCoordIndex.Items);
324 except FreeAndNil(Result); raise end;
325 end;
326 {$warnings on}