Debian Codegraph @

src/scene/x3d/x3dnodes_standard_geometry3d.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2002-2024 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 { Rectangular box. }
19 TBoxNode = class(TAbstractGeometryNode)
20 public
21 function TexCoordField: TSFNode; override;
22 function SolidField: TSFBool; override;
23
24 function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override;
25 function BoundingBox(State: TX3DGraphTraverseState;
26 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override;
27 function LocalBoundingBox(State: TX3DGraphTraverseState;
28 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override;
29 function TrianglesCount(State: TX3DGraphTraverseState;
30 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override;
31 function AutoGenerate3DTexCoords: boolean; override;
32
33 {$I auto_generated_node_helpers/x3dnodes_box.inc}
34 end;
35
36 { Cone. }
37 TConeNode = class(TAbstractGeometryNode)
38 protected
39 function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override;
40 public
41 function TexCoordField: TSFNode; override;
42 function SolidField: TSFBool; override;
43 function CalculateSlices: Cardinal;
44 function CalculateStacks: Cardinal;
45
46 class function ForVRMLVersion(const Version: TX3DVersion): boolean;
47 override;
48 function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override;
49 function BoundingBox(State: TX3DGraphTraverseState;
50 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override;
51 function LocalBoundingBox(State: TX3DGraphTraverseState;
52 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override;
53 function TrianglesCount(State: TX3DGraphTraverseState;
54 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override;
55 function AutoGenerate3DTexCoords: boolean; override;
56
57 {$I auto_generated_node_helpers/x3dnodes_cone.inc}
58 end;
59 TConeNode_2 = TConeNode;
60
61 { Cylinder. }
62 TCylinderNode = class(TAbstractGeometryNode)
63 protected
64 function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override;
65 public
66 function TexCoordField: TSFNode; override;
67 function SolidField: TSFBool; override;
68 function CalculateSlices: Cardinal;
69 function CalculateStacks: Cardinal;
70
71 class function ForVRMLVersion(const Version: TX3DVersion): boolean;
72 override;
73 function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override;
74 function BoundingBox(State: TX3DGraphTraverseState;
75 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override;
76 function LocalBoundingBox(State: TX3DGraphTraverseState;
77 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override;
78 function TrianglesCount(State: TX3DGraphTraverseState;
79 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override;
80 function AutoGenerate3DTexCoords: boolean; override;
81
82 {$I auto_generated_node_helpers/x3dnodes_cylinder.inc}
83 end;
84 TCylinderNode_2 = TCylinderNode;
85
86 { Uniform rectangular grid of varying height above the Y=0 plane,
87 aka "height map". }
88 TElevationGridNode = class(TAbstractGeometryNode)
89 strict private
90 procedure EventSet_HeightReceive(
91 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
92 class function ClassIsNotEmpty(
93 const AXDimension, AZDimension: Integer;
94 const AXSpacing, AZSpacing: Single;
95 const AHeight: TSingleList): Boolean;
96 public
97 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
98
99 { Do we have (correct) data to render.
100
101 Checks whether
102 - xDimension and zDimension are >= 2,
103 - xSpacing and zSpacing are > 0
104 - height list has at least the required number of values.
105 }
106 function IsNotEmpty: boolean;
107
108 function ColorField: TSFNode; override;
109 function InternalFogCoord: TMFFloat; override;
110 function AttribField: TMFNode; override;
111 function SolidField: TSFBool; override;
112 function FieldSetByEvent(const Event: TX3DEvent): TX3DField; override;
113
114 function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override;
115
116 function LocalBoundingBox(State: TX3DGraphTraverseState;
117 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override;
118 function VerticesCount(State: TX3DGraphTraverseState;
119 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override;
120 function TrianglesCount(State: TX3DGraphTraverseState;
121 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override;
122
123 { Create triangle set to render the elevation grid.
124 Useful for
125 - Proxy in this class
126 - TCastleTerrain which wants direct TIndexedTriangleSetNode
127 (faster to later update than to maintain TElevationGridNode). }
128 class function InternalCreateTriangles(
129 const AXDimension, AZDimension: Integer;
130 const AXSpacing, AZSpacing: Single;
131 const AHeight: TSingleList;
132 const ATexCoord: TAbstractTextureCoordinateNode;
133 const TriangulationIgnoreHeights: Boolean): TIndexedTriangleSetNode;
134
135 {$I auto_generated_node_helpers/x3dnodes_elevationgrid.inc}
136 end;
137
138 { 2D cross-section shape extruded along a 3D spine. }
139 TExtrusionNode = class(TAbstractGeometryNode)
140 protected
141 function DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; override;
142 strict private
143 procedure Eventset_crossSectionReceive(
144 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
145 procedure Eventset_orientationReceive(
146 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
147 procedure Eventset_scaleReceive(
148 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
149 procedure Eventset_spineReceive(
150 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
151 public
152 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
153
154 function TexCoordField: TSFNode; override;
155 function SolidField: TSFBool; override;
156 function ConvexField: TSFBool; override;
157
158 { For given Index, return appropriate FdCrossIndex item as 3D vertex.
159 That is, uses FdCrossIndex values as X, Z of 3D vertex and sets Y = 0
160 (that's how Extrusion is defined in VRML / X3D). }
161 function CrossSection3D(Index: integer): TVector3;
162
163 { Check is spine closed. This happens when "spine" field is non-empty
164 and it's first and last points are equal. }
165 function SpineClosed: boolean;
166
167 { Check is crossSection closed. This happens when "crossSection"
168 field is non-empty and it's first and last points are equal. }
169 function CrossSectionClosed: boolean;
170
171 function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override;
172
173 function LocalBoundingBox(State: TX3DGraphTraverseState;
174 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override;
175 function BoundingBox(State: TX3DGraphTraverseState;
176 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override;
177 function TrianglesCount(State: TX3DGraphTraverseState;
178 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override;
179 function AutoGenerate3DTexCoords: boolean; override;
180
181 {$I auto_generated_node_helpers/x3dnodes_extrusion.inc}
182 end;
183
184 { Mesh with faces (polygons) constructed from vertices.
185 This is probably the most universal, and most often used,
186 geometry node in X3D. }
187 TIndexedFaceSetNode = class(TAbstractComposedGeometryNode)
188 strict private
189 procedure Eventset_colorIndexReceive(
190 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
191 procedure Eventset_coordIndexReceive(
192 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
193 procedure Eventset_normalIndexReceive(
194 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
195 procedure Eventset_texCoordIndexReceive(
196 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
197 public
198 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
199
200 function ConvexField: TSFBool; override;
201
202 class function ForVRMLVersion(const Version: TX3DVersion): boolean;
203 override;
204
205 function CoordIndexField: TMFLong; override;
206
207 procedure InternalCoordPolygons(
208 State: TX3DGraphTraverseState;
209 PolygonHandler: TIndexedPolygonHandler); override;
210
211 function TrianglesCount(State: TX3DGraphTraverseState;
212 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override;
213
214 {$I auto_generated_node_helpers/x3dnodes_indexedfaceset.inc}
215 end;
216 TIndexedFaceSetNode_2 = TIndexedFaceSetNode;
217
218 { Sphere. }
219 TSphereNode = class(TAbstractGeometryNode)
220 public
221 function TexCoordField: TSFNode; override;
222 function SolidField: TSFBool; override;
223 function CalculateSlices: Cardinal;
224 function CalculateStacks: Cardinal;
225
226 class function ForVRMLVersion(const Version: TX3DVersion): boolean;
227 override;
228 function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override;
229
230 function BoundingBox(State: TX3DGraphTraverseState;
231 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override;
232 function LocalBoundingBox(State: TX3DGraphTraverseState;
233 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override;
234 function TrianglesCount(State: TX3DGraphTraverseState;
235 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal; override;
236 function AutoGenerate3DTexCoords: boolean; override;
237
238 {$I auto_generated_node_helpers/x3dnodes_sphere.inc}
239 end;
240 TSphereNode_2 = TSphereNode;
241
242 {$endif read_interface}
243
244 {$ifdef read_implementation}
245
246 { TBoxNode ------------------------------------------------------------------- }
247
248 function TBoxNode.TexCoordField: TSFNode;
249 begin
250 Result := FdTexCoord;
251 end;
252
253 function TBoxNode.AutoGenerate3DTexCoords: boolean;
254 begin
255 Result := (FdTexCoord.Value = nil) or not FdTexCoord.CurrentChildAllowed;
256 end;
257
258 function TBoxNode.SolidField: TSFBool;
259 begin
260 Result := FdSolid;
261 end;
262
263 { TConeNode ------------------------------------------------------------------ }
264
265 class function TConeNode.ForVRMLVersion(const Version: TX3DVersion): boolean;
266 begin
267 Result := Version.Major >= 2;
268 end;
269
270 function TConeNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer;
271 begin
272 Result := inherited;
273 if Result <> nil then Exit;
274
275 Result := FdtexCoord.Enumerate(Func);
276 if Result <> nil then Exit;
277 end;
278
279 function TConeNode.TexCoordField: TSFNode;
280 begin
281 Result := FdTexCoord;
282 end;
283
284 function TConeNode.AutoGenerate3DTexCoords: boolean;
285 begin
286 Result := (FdTexCoord.Value = nil) or not FdTexCoord.CurrentChildAllowed;
287 end;
288
289 function TConeNode.SolidField: TSFBool;
290 begin
291 Result := FdSolid;
292 end;
293
294 function TConeNode.CalculateSlices: Cardinal;
295 begin
296 // use default in case of 0 or invalid value
297 if FdSlices.Value < MinTriangulationSlices then
298 Result := DefaultTriangulationSlices
299 else
300 Result := FdSlices.Value;
301 end;
302
303 function TConeNode.CalculateStacks: Cardinal;
304 begin
305 // use default in case of 0 or invalid value
306 if FdStacks.Value < MinTriangulationStacks then
307 Result := DefaultTriangulationStacks
308 else
309 Result := FdStacks.Value;
310 end;
311
312 { TCylinderNode -------------------------------------------------------------- }
313
314 class function TCylinderNode.ForVRMLVersion(const Version: TX3DVersion): boolean;
315 begin
316 Result := Version.Major >= 2;
317 end;
318
319 function TCylinderNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer;
320 begin
321 Result := inherited;
322 if Result <> nil then Exit;
323
324 Result := FdtexCoord.Enumerate(Func);
325 if Result <> nil then Exit;
326 end;
327
328 function TCylinderNode.TexCoordField: TSFNode;
329 begin
330 Result := FdTexCoord;
331 end;
332
333 function TCylinderNode.AutoGenerate3DTexCoords: boolean;
334 begin
335 Result := (FdTexCoord.Value = nil) or not FdTexCoord.CurrentChildAllowed;
336 end;
337
338 function TCylinderNode.SolidField: TSFBool;
339 begin
340 Result := FdSolid;
341 end;
342
343 function TCylinderNode.CalculateSlices: Cardinal;
344 begin
345 // use default in case of 0 or invalid value
346 if FdSlices.Value < MinTriangulationSlices then
347 Result := DefaultTriangulationSlices
348 else
349 Result := FdSlices.Value;
350 end;
351
352 function TCylinderNode.CalculateStacks: Cardinal;
353 begin
354 Result := 1;
355
356 { As we use Phong shading now by default,
357 there's no point in using stacks for Cylinders -- they do not
358 improve geometry, they do not improve shading.
359
360 (Note that we still use stacks for Cone, where they improve the
361 shading on sides.)
362 }
363
364 (*
365
366 // use default in case of 0 or invalid value
367 if FdStacks.Value < MinTriangulationStacks then
368 Result := DefaultTriangulationStacks
369 else
370 Result := FdStacks.Value;
371 *)
372 end;
373
374 { TElevationGridNode --------------------------------------------------------- }
375
376 constructor TElevationGridNode.Create(const AX3DName, ABaseUrl: String);
377 begin
378 inherited;
379 Eventset_height.AddNotification({$ifdef FPC}@{$endif} EventSet_HeightReceive);
380 end;
381
382 class function TElevationGridNode.ClassIsNotEmpty(
383 const AXDimension, AZDimension: Integer;
384 const AXSpacing, AZSpacing: Single;
385 const AHeight: TSingleList): Boolean;
386 begin
387 Result :=
388 (AXDimension >= 2) and
389 (AZDimension >= 2) and
390 { VRML spec says that xSpacing and ySpacing shall be > 0.
391 So when they are = 0 (or < 0) we don't render anything. }
392 (AXSpacing > 0) and
393 (AZSpacing > 0) and
394 (AHeight.Count >= AXDimension * AZDimension);
395 end;
396
397 function TElevationGridNode.IsNotEmpty: Boolean;
398 begin
399 Result := ClassIsNotEmpty(
400 FdXDimension.Value, FdZDimension.Value,
401 FdXSpacing.Value, FdZSpacing.Value,
402 FdHeight.Items);
403 end;
404
405 function TElevationGridNode.ColorField: TSFNode;
406 begin
407 Result := FdColor;
408 end;
409
410 function TElevationGridNode.InternalFogCoord: TMFFloat;
411 begin
412 if (FdFogCoord.Value <> nil) and
413 (FdFogCoord.Value is TFogCoordinateNode) then
414 Result := TFogCoordinateNode(FdFogCoord.Value).FdDepth else
415 Result := nil;
416 end;
417
418 function TElevationGridNode.AttribField: TMFNode;
419 begin
420 Result := FdAttrib;
421 end;
422
423 procedure TElevationGridNode.EventSet_HeightReceive(
424 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
425 begin
426 FdHeight.Send(Value);
427 end;
428
429 function TElevationGridNode.SolidField: TSFBool;
430 begin
431 Result := FdSolid;
432 end;
433
434 function TElevationGridNode.FieldSetByEvent(const Event: TX3DEvent): TX3DField;
435 begin
436 { Thanks to this method, animations that modify ElevationGrid
437 by sending set_height event are correctly accounted
438 in TCastleSceneCore "affected fields", thus e.g. ResetAnimationState
439 correctly resets them. }
440 if Event = EventSet_height then
441 Exit(FdHeight);
442 Result := inherited;
443 end;
444
445 { TExtrusionNode ------------------------------------------------------------- }
446
447 constructor TExtrusionNode.Create(const AX3DName, ABaseUrl: String);
448 begin
449 inherited;
450 Eventset_crossSection.AddNotification({$ifdef FPC}@{$endif} EventSet_crossSectionReceive);
451 Eventset_orientation.AddNotification({$ifdef FPC}@{$endif} EventSet_orientationReceive);
452 Eventset_scale.AddNotification({$ifdef FPC}@{$endif} EventSet_scaleReceive);
453 Eventset_spine.AddNotification({$ifdef FPC}@{$endif} EventSet_spineReceive);
454 end;
455
456 function TExtrusionNode.CrossSection3D(Index: integer): TVector3;
457 begin
458 Result.X := FdCrossSection.Items.L[Index].X;
459 Result.Y := 0;
460 Result.Z := FdCrossSection.Items.L[Index].Y;
461 end;
462
463 function TExtrusionNode.SpineClosed: boolean;
464 var
465 SpinePoints: TVector3List;
466 begin
467 SpinePoints := FdSpine.Items;
468 Result := (SpinePoints.Count <> 0) and
469 TVector3.PerfectlyEquals(SpinePoints.Last,
470 SpinePoints.First);
471 end;
472
473 function TExtrusionNode.CrossSectionClosed: boolean;
474 var
475 CrossSectionPoints: TVector2List;
476 begin
477 CrossSectionPoints := FdCrossSection.Items;
478 Result := (CrossSectionPoints.Count <> 0) and
479 // Makes Internal Error with FPC 3.0.2 on Linux x86_64.
480 // TVector2.PerfectlyEquals(CrossSectionPoints.Last,
481 // CrossSectionPoints.First);
482 TVector2.PerfectlyEquals(CrossSectionPoints[CrossSectionPoints.Count - 1],
483 CrossSectionPoints[0]);
484 end;
485
486 function TExtrusionNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode;
487
488 { Some of the things that are non-optimal when you will render
489 Extrusion using this Proxy instead of directly rendering node:
490
491 - In specialized Extrusion rendering, you now that sides are
492 composed of quads, and so you can use QUAD_STRIP to render them.
493 This allows vertices sharing. IndexedFaceSet uses vertex arrays,
494 that also provides sharing but may be slightly worse.
495
496 - In specialized Extrusion rendering, you know that sides' quads
497 are convex. There's never any need to triangulate them carefully.
498 For IndexedFaceSet, when caps may be non-convex, you have to set
499 convex field to false, so sides will be unnecessarily triangulated
500 with the slower algorithm.
501
502 You gain:
503
504 - creaseAngle support, which is really the crucial point why
505 I'm implementing this. I already have a specialized Extrusion
506 rendering, but adding there creaseAngle support is a major pain.
507
508 You would have to calculate all coordinates first, and then calculate
509 their normal vectors using a specialized routine like CreateNormals
510 that knows about Extrusion connectivity. This is painful to implement,
511 and naive implementation would even suffer from a slowdown not present
512 in Proxy approach: calculating all coordinates two times (before
513 calculating normals, and later for actual rendering). Sure, smarter
514 approach would remember these coordinates. Proxy approach provides
515 this automatically, saving coordinates inside Coordinate node,
516 for both normals calculation and rendering.
517
518 - A little ease of implementation: 230 lines of Proxy approach
519 versus 390 lines of TExtrusionRenderer. And remember that
520 actually handling creaseAngle would complicate TExtrusionRenderer
521 even more (that's why I didn't dare to do it...), and that
522 Proxy approach could also be used to implement things like
523 bounding box and triangulate eventually (although for now we use
524 our optimized methods).
525 }
526
527 var
528 IFS: TIndexedFaceSetNode absolute Result;
529 Index: Integer;
530 E: TVRMLExtrusion;
531 TexCoords: TVector2List;
532
533 procedure NextIndex(SpineIndex, CrossSectionIndex: Integer;
534 SpecialTex: boolean; SpecialTexIndex: Integer);
535 begin
536 if TexCoords <> nil then
537 begin
538 if SpecialTex then
539 IFS.FdTexCoordIndex.Items.L[Index] := SpecialTexIndex else
540 IFS.FdTexCoordIndex.Items.L[Index] :=
541 SpineIndex * FdCrossSection.Count + CrossSectionIndex;
542 end;
543
544 { For closed spine and/or crossSection, specify the same indexes.
545 For coordIndex (for texCoordIndex, this is not correct, texture
546 may have a seam there).
547 This will allow the correct smoothing between spine/crossSection
548 edges when calculating normal vectors. }
549
550 if (SpineIndex = FdSpine.Items.Count - 1) and
551 E.BeginEndCapsMatching then
552 SpineIndex := 0;
553
554 if (CrossSectionIndex = FdCrossSection.Items.Count - 1) and
555 E.CrossSectionClosed then
556 CrossSectionIndex := 0;
557
558 IFS.FdCoordIndex.Items.L[Index] :=
559 SpineIndex * FdCrossSection.Count + CrossSectionIndex;
560
561 Inc(Index);
562 end;
563
564 procedure NextIndexFaceDelimiter;
565 begin
566 IFS.FdCoordIndex.Items.L[Index] := -1;
567 if TexCoords <> nil then
568 IFS.FdTexCoordIndex.Items.L[Index] := -1;
569 Inc(Index);
570 end;
571
572 function SideTexCoord(SpineIndex, CrossSectionIndex: Integer): TVector2;
573 begin
574 { Check ranges, to avoid dividing by zero (eventually the
575 corresponding coord is set to a predictable zero :) ). }
576 if FdCrossSection.Count > 1 then
577 Result.X := CrossSectionIndex / (FdCrossSection.Count - 1) else
578 Result.X := 0;
579 if E.High > 0 then
580 Result.Y := SpineIndex / E.High else
581 Result.Y := 0;
582 end;
583
584 var
585 CoordNode: TCoordinateNode;
586 TexCoordNode: TTextureCoordinateNode;
587 Transform: TMatrix4;
588 I, J, Len, SidesCount: Integer;
589 LastY, LastZ: TVector3;
590 TexCapsMin, TexCapsMax: TVector2;
591 TexCapsTranslation, TexCapsScale: Single;
592 begin
593 IFS := TIndexedFaceSetNode.Create(X3DName, BaseUrl);
594 try
595 E := TVRMLExtrusion.Create;
596 try
597 E.Node := Self;
598
599 { calculate SidesCount = number of verts on sides, often used number }
600 SidesCount := (E.High + 1) * FdCrossSection.Count;
601
602 { Fill CoordNode }
603
604 CoordNode := TCoordinateNode.Create('', BaseUrl);
605 IFS.FdCoord.Value := CoordNode;
606
607 CoordNode.FdPoint.Items.Count := SidesCount;
608
609 for I := 0 to E.High do
610 begin
611 E.SpineTransformVar(I, LastY, LastZ, Transform);
612 for J := 0 to FdCrossSection.Count - 1 do
613 begin
614 CoordNode.FdPoint.Items.L[I * FdCrossSection.Count + J] :=
615 Transform.MultPoint(CrossSection3D(J));
616 end;
617 end;
618
619 { Fill TexCoordNode }
620
621 if (FdTexCoord.Value <> nil) and FdTexCoord.CurrentChildAllowed then
622 begin
623 { No need for to create tex coords. }
624 IFS.FdTexCoord.Value := FdTexCoord.Value;
625 TexCoordNode := nil;
626 TexCoords := nil;
627 end else
628 begin
629 TexCoordNode := TTextureCoordinateNode.Create('', BaseUrl);
630 IFS.FdTexCoord.Value := TexCoordNode;
631 TexCoords := TexCoordNode.FdPoint.Items;
632 end;
633
634 if TexCoords <> nil then
635 begin
636 Len := SidesCount;
637 if FdBeginCap.Value or FdEndCap.Value then
638 Len := Len + FdCrossSection.Count;
639 TexCoords.Count := Len;
640
641 for I := 0 to E.High do
642 for J := 0 to FdCrossSection.Count - 1 do
643 TexCoords.L[I * FdCrossSection.Count + J] := SideTexCoord(I, J);
644
645 { calculate TexCapsTranslation, TexCapsScale }
646 if FdBeginCap.Value or FdEndCap.Value then
647 begin
648 TexCapsTranslation := 0;
649 TexCapsScale := 1;
650 if FdCrossSection.Items.MinMax(TexCapsMin, TexCapsMax) then
651 begin
652 if TexCapsMax[0] - TexCapsMin[0] >=
653 TexCapsMax[1] - TexCapsMin[1] then
654 begin
655 if not IsZero(TexCapsMax[0] - TexCapsMin[0]) then
656 begin
657 { scale such that U is along the X of cap.
658 Coord calculation will go like
659 u := (x - TexCapsMin[0]) / (TexCapsMax[0] - TexCapsMin[0]) }
660 TexCapsTranslation := -TexCapsMin[0];
661 TexCapsScale := 1 / (TexCapsMax[0] - TexCapsMin[0]);
662 end;
663 end else
664 begin
665 if not IsZero(TexCapsMax[1] - TexCapsMin[1]) then
666 begin
667 TexCapsTranslation := -TexCapsMin[1];
668 TexCapsScale := 1 / (TexCapsMax[1] - TexCapsMin[1]);
669 end;
670 end;
671 end;
672
673 for I := 0 to FdCrossSection.Count - 1 do
674 begin
675 TexCoordNode.FdPoint.Items.L[SidesCount + I] :=
676 (FdCrossSection.Items.L[I] +
677 Vector2(TexCapsTranslation, TexCapsTranslation))
678 * TexCapsScale;
679 end;
680 end;
681 end;
682
683 { Fill CoordIndex and TexCoordIndex }
684
685 if E.High > 0 then
686 Len := 5 * E.High * (FdCrossSection.Count - 1) else
687 Len := 0; { E.High may be -1, then Len should remain 0 }
688 if FdBeginCap.Value then
689 Len := Len + (FdCrossSection.Count + 1 - E.CrossSectionOmit);
690 if FdEndCap.Value then
691 Len := Len + (FdCrossSection.Count + 1 - E.CrossSectionOmit);
692 IFS.FdCoordIndex.Items.Count := Len;
693 if TexCoords <> nil then
694 IFS.FdTexCoordIndex.Items.Count := Len;
695
696 Index := 0;
697
698 for I := 1 to E.High do
699 begin
700 for J := 1 to FdCrossSection.Count - 1 do
701 begin
702 { The VRML / X3D specifications say that ordering of sides
703 should be
704 spine[0](crossSection[0], crossSection[1])
705 spine[1](crossSection[1], crossSection[0])
706 This is important, because we want faces to be ordered just
707 like the specification requires. This makes using "ccw"
708 and "solid" fields predictable for VRML author, since he
709 knows which side will be CCW and which CW.
710
711 So below we produce the same order. }
712
713 NextIndex(I - 1, J - 1, false, 0);
714 NextIndex(I - 1, J , false, 0);
715 NextIndex(I , J , false, 0);
716 NextIndex(I , J - 1, false, 0);
717 NextIndexFaceDelimiter;
718 end;
719 end;
720
721 if FdBeginCap.Value then
722 begin
723 { "downto" order, to match the ordering of sides, this makes
724 things such a "solid TRUE" work Ok. }
725 for J := FdCrossSection.Count - 1 downto E.CrossSectionOmit do
726 NextIndex(0, J, true, SidesCount + J);
727 NextIndexFaceDelimiter;
728 end;
729
730 if FdEndCap.Value then
731 begin
732 for J := E.CrossSectionOmit to FdCrossSection.Count - 1 do
733 NextIndex(E.High, J, true, SidesCount + J);
734 NextIndexFaceDelimiter;
735 end;
736
737 { Check that we actually filled all indexes we planned }
738 Assert(Index = IFS.FdCoordIndex.Items.Count);
739
740 finally FreeAndNil(E) end;
741
742 IFS.FdSolid.Value := FdSolid.Value;
743 IFS.FdCcw.Value := FdCcw.Value;
744 IFS.FdCreaseAngle.Value := FdCreaseAngle.Value;
745 IFS.FdConvex.Value := FdConvex.Value;
746 except FreeAndNil(Result); raise end;
747 end;
748
749 function TExtrusionNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer;
750 begin
751 Result := inherited;
752 if Result <> nil then Exit;
753
754 Result := FdtexCoord.Enumerate(Func);
755 if Result <> nil then Exit;
756 end;
757
758 function TExtrusionNode.TexCoordField: TSFNode;
759 begin
760 Result := FdTexCoord;
761 end;
762
763 function TExtrusionNode.AutoGenerate3DTexCoords: boolean;
764 begin
765 Result := (FdTexCoord.Value = nil) or not FdTexCoord.CurrentChildAllowed;
766 end;
767
768 procedure TExtrusionNode.EventSet_crossSectionReceive(
769 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
770 begin
771 FdcrossSection.Assign(Value);
772 FdcrossSection.Changed;
773 end;
774
775 procedure TExtrusionNode.EventSet_orientationReceive(
776 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
777 begin
778 Fdorientation.Assign(Value);
779 Fdorientation.Changed;
780 end;
781
782 procedure TExtrusionNode.EventSet_scaleReceive(
783 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
784 begin
785 Fdscale.Assign(Value);
786 Fdscale.Changed;
787 end;
788
789 procedure TExtrusionNode.EventSet_spineReceive(
790 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
791 begin
792 Fdspine.Assign(Value);
793 Fdspine.Changed;
794 end;
795
796 function TExtrusionNode.ConvexField: TSFBool;
797 begin
798 Result := FdConvex;
799 end;
800
801 function TExtrusionNode.SolidField: TSFBool;
802 begin
803 Result := FdSolid;
804 end;
805
806 { TIndexedFaceSetNode -------------------------------------------------------- }
807
808 constructor TIndexedFaceSetNode.Create(const AX3DName, ABaseUrl: String);
809 begin
810 inherited;
811 {$ifndef CASTLE_SLIM_NODES}
812 Eventset_colorIndex.AddNotification({$ifdef FPC}@{$endif} EventSet_colorIndexReceive);
813 Eventset_coordIndex.AddNotification({$ifdef FPC}@{$endif} EventSet_coordIndexReceive);
814 Eventset_normalIndex.AddNotification({$ifdef FPC}@{$endif} EventSet_normalIndexReceive);
815 Eventset_texCoordIndex.AddNotification({$ifdef FPC}@{$endif} EventSet_texCoordIndexReceive);
816 {$endif}
817 FdColorIndex.SaveToStreamLineUptoNegative := true;
818 FdCoordIndex.SaveToStreamLineUptoNegative := true;
819 FdNormalIndex.SaveToStreamLineUptoNegative := true;
820 FdTexCoordIndex.SaveToStreamLineUptoNegative := true;
821 end;
822
823 class function TIndexedFaceSetNode.ForVRMLVersion(const Version: TX3DVersion): boolean;
824 begin
825 Result := Version.Major >= 2;
826 end;
827
828 function TIndexedFaceSetNode.CoordIndexField: TMFLong;
829 begin
830 Result := FdCoordIndex;
831 end;
832
833 procedure TIndexedFaceSetNode.EventSet_colorIndexReceive(
834 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
835 begin
836 FdcolorIndex.Assign(Value);
837 FdcolorIndex.Changed;
838 end;
839
840 procedure TIndexedFaceSetNode.EventSet_coordIndexReceive(
841 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
842 begin
843 FdcoordIndex.Assign(Value);
844 FdcoordIndex.Changed;
845 end;
846
847 procedure TIndexedFaceSetNode.EventSet_normalIndexReceive(
848 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
849 begin
850 FdnormalIndex.Assign(Value);
851 FdnormalIndex.Changed;
852 end;
853
854 procedure TIndexedFaceSetNode.EventSet_texCoordIndexReceive(
855 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
856 begin
857 FdtexCoordIndex.Assign(Value);
858 FdtexCoordIndex.Changed;
859 end;
860
861 function TIndexedFaceSetNode.ConvexField: TSFBool;
862 begin
863 Result := FdConvex;
864 end;
865
866 class function TSphereNode.ForVRMLVersion(const Version: TX3DVersion): boolean;
867 begin
868 Result := Version.Major >= 2;
869 end;
870
871 function TSphereNode.TexCoordField: TSFNode;
872 begin
873 Result := FdTexCoord;
874 end;
875
876 function TSphereNode.AutoGenerate3DTexCoords: boolean;
877 begin
878 Result := (FdTexCoord.Value = nil) or not FdTexCoord.CurrentChildAllowed;
879 end;
880
881 function TSphereNode.SolidField: TSFBool;
882 begin
883 Result := FdSolid;
884 end;
885
886 function TSphereNode.CalculateSlices: Cardinal;
887 begin
888 // use default in case of 0 or invalid value
889 if FdSlices.Value < MinTriangulationSlices then
890 Result := DefaultTriangulationSlices
891 else
892 Result := FdSlices.Value;
893 end;
894
895 function TSphereNode.CalculateStacks: Cardinal;
896 begin
897 // use default in case of 0 or invalid value
898 if FdStacks.Value < MinTriangulationStacks then
899 Result := DefaultTriangulationStacks
900 else
901 Result := FdStacks.Value;
902 end;
903
904 { routines ------------------------------------------------------------------- }
905
906 procedure RegisterGeometry3DNodes;
907 begin
908 NodesManager.RegisterNodeClasses([
909 TBoxNode,
910 TConeNode,
911 TCylinderNode,
912 TElevationGridNode,
913 TExtrusionNode,
914 TIndexedFaceSetNode,
915 TSphereNode
916 ]);
917 end;
918
919 {$endif read_implementation}