Debian Codegraph @

src/scene/x3d/x3dnodes_utils_cone_cylinder.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 { Utilities for making cone and cylinder shapes. }
18
19 { Generate circle points.
20 Circle is on const Y = Height, centered on XZ = (0, 0) with given Radius.
21 We generate QuadricSlices points, writing them into Points array.
22 Order of coords is along -Z, then -X, then +Z, then +X.
23
24 When Radius = exactly 0, the points are guaranteed to be exactly equal.
25
26 Assumes QuadricSlices >= 1. }
27 procedure GenerateCircle(const QuadricSlices: Cardinal;
28 const Radius, Height: Single; Points: PVector3);
29 var
30 AngleRad: Single;
31 AngleSin, AngleCos: Single;
32 I: integer;
33 begin
34 if Radius <> 0 then
35 begin
36 Points^ := Vector3(0, Height, -Radius);
37 Inc(Points);
38
39 for I := 1 to QuadricSlices - 1 do
40 begin
41 AngleRad := (I / QuadricSlices) * 2 * Pi;
42 SinCos(AngleRad, AngleSin, AngleCos);
43
44 Points^ := Vector3(-AngleSin * Radius, Height, -AngleCos * Radius);
45 Inc(Points);
46 end;
47 end else
48 begin
49 { The code above would work for this case too,
50 but this is an optimized version. }
51 for I := 0 to QuadricSlices - 1 do
52 begin
53 Points^.X := 0;
54 Points^.Y := Height;
55 Points^.Z := 0;
56 Inc(Points);
57 end;
58 end;
59 end;
60
61 { Generate QuadricSlices points on a 2D circle of radius 1.
62 Shift things such that instead of [-1, 1] x [-1, 1],
63 circle is inside [0, 1] x [0, 1].
64
65 @param(InvertT Allows you to invert texture, suitable for top cap.) }
66 procedure GenerateTexCoordCircle(const QuadricSlices: Cardinal;
67 Points: PVector2; const InvertT: boolean);
68 var
69 AngleRad: Single;
70 AngleSin, AngleCos: Single;
71 I: integer;
72 begin
73 if InvertT then
74 Points^ := Vector2(0.5, 1) else
75 Points^ := Vector2(0.5, 0);
76 Inc(Points);
77
78 for I := 1 to QuadricSlices - 1 do
79 begin
80 AngleRad := (I / QuadricSlices) * 2 * Pi;
81 SinCos(AngleRad, AngleSin, AngleCos);
82
83 AngleSin := -AngleSin;
84 if not InvertT then AngleCos := -AngleCos;
85
86 Points^ := Vector2((AngleSin + 1) / 2, (AngleCos + 1) / 2 );
87 Inc(Points);
88 end;
89 end;
90
91 { Make a cylinder (with bottom and top radius
92 potentially different) or a cone (which is indicated by TopRadius = 0).
93 Generates values suitable e.g. for VRML/X3D IndexedFaceSet.
94 This is a universal utility, for both VRML 1.0 and >= 2.0.
95
96 Just like gluCylinder, it makes sense to implement cone and cylinder
97 at once. Their topology is the same (yes, even at the cone top,
98 see comments in the implementation).
99
100 @param(TexCoord Texture coordinates to generate.
101 Pass @nil if you don't need them.)
102
103 @param(MaterialIndex VRML 1.0 material index, generated like for
104 material binding per part. Will be used with
105 material binding BIND_PER_FACE_INDEXED.
106 We use PER_FACE_INDEXED instead of PER_VERTEX_INDEXED because
107 PER_VERTEX_INDEXED isn't perfectly implemented (see OutsideBeginEnd
108 notes), besides it matches our needs better.
109
110 Pass @nil if you don't need it.)
111
112 @param(TopRadius Radius of the upper cap.
113 May be = 0, then Top does not matter (we'll never make
114 a top cap in this case.)) }
115 procedure CylinderCone_Proxy(CoordIndex: TInt32List;
116 Coord: TVector3List; Normal: TVector3List;
117 TexCoord: TVector2List; MaterialIndex: TInt32List;
118 const BottomRadius, TopRadius, Height: Single;
119 const Bottom, Side, Top: boolean;
120 const QuadricSlices, QuadricStacks: Cardinal);
121
122 { How to make nice normals at the top of the cone?
123
124 It's simply impossible to get nice smooth normals without adding additional
125 stacks (that don't change the shape, so in a sense are wasteful).
126 OpenGL gluCylinder also fails to make smooth cone normals
127 when stacks = 1.
128
129 Ideas:
130 1. We can't use a single vertex (with one normal)
131 at the top of the cone, obviously. This would smooth our normal
132 vector between all side faces.
133
134 2. First real idea for a fix was to use QuadricSlices * top points,
135 and make each cone side face a triangle with different top vertex.
136 However, this doesn't make cone side edges smooth: as each triangle
137 has different normal vector at the top vertex, so normals are not
138 matching on neighboring edges.
139
140 3. Another idea was to cheat: represent each cone face as a quad.
141 This means that each cone side face
142 has 2 top vertexes, which may have different normal vectors.
143
144 This is also what OpenGL GLU quadrics do --- you have to use gluCylinder
145 with top radius 0 to get a cone. And at least SGI implementation
146 (see http://cgit.freedesktop.org/mesa/mesa/tree/src/glu/sgi/libutil/quad.c)
147 will actually use GL_QUAD_STRIP (just like for regular cylinder),
148 and the top vertex will be simply duplicated on each face.
149
150 For a moment I thought this will work, i.e. will make edges smooth.
151 Normals match across neighboring edges, right?
152
153 ... Except it doesn't. Probably because we simply split this
154 quad into 2 triangles (during IndexedFaceSet rendering).
155 And OpenGL quad rendering does the same, at least on chantal GPU.
156 So one triangle is simply empty (rasterization doesn't produce anything),
157 and the edges are still sharp.
158
159 This *would* work if OpenGL would rasterize quads, interpolating
160 across them correctly.
161
162 After all, I use the 2. idea, duplicating top verts but not cheating
163 with quads. Reasons:
164 - since 3. doesn't work anyway, why waste memory
165 (and bandwidth to OpenGL) for sending dummy triangles?
166 - Besides, 3. makes TrianglesCount reported as more than the number
167 of items inside the octree (because degenerate triangles are removed).
168 While this is unavoidable in general (actual IndexedFaceSet may have
169 degenerate triangles), there's no need to cause confusion in this case
170 when we can avoid it.
171
172 Why explicit normals?
173
174 Initially, I thought that explicit normals are needed to set
175 correct normals at the top of the cone. But, eventually,
176 I found out that it's impossible to make them really smooth
177 --- see the above discussion.
178
179 The current reason: because we can. It's easy and already implemented.
180
181 We use normal not NULL, with normalPerVertex = TRUE, and normalIndex empty.
182 So coordIndex will be used to index normals.
183
184 Why not use specific normalIndex?
185
186 Using normalIndex would allow us to have shorter Coordinate array.
187 For example top/bottom circle, and cone top vertexes, would not have
188 to be duplicated in Coordinate array
189 (just because it needs different normals on different faces).
190 However, this is worthless optimization: for OpenGL vertex array rendering,
191 we would have to replicate the vertex coord anyway, because for OpenGL arrays
192 we can pass only one index (there is no separate index for coords, normals etc.).
193 So we would not save anything (and only increase the work needed for
194 OpenGL rendering).
195
196 Besides, texCoordIndex would then have to be equal to normalIndex.
197 }
198
199 { For given angle, calculate side face normal vector.
200 Where Angle is in [0...CalculateQuadricSlices] range,
201 we will automatically adjust it for correct angle in radians. }
202 function SideNormal(const Angle: Single): TVector3;
203 var
204 AngleSin, AngleCos: Single;
205 begin
206 SinCos((Angle / QuadricSlices) * 2 * Pi, AngleSin, AngleCos);
207
208 Result.X := -AngleSin;
209
210 if TopRadius = 0 then
211 { Why? Draw a ortho view of cone, and note that two triangles are
212 similar, and notice a proportion
213 1 / Height = Result.Y / BottomRadius. }
214 Result.Y := BottomRadius / Height else
215 { This assumes that BottomRadius = TopRadius.
216 It's Ok, this procedure is used now only for straight cylinder
217 or a cone. }
218 Result.Y := 0;
219 Result.Z := -AngleCos;
220
221 { When Result.Y is zero (straight cylinder), it's already normalized }
222 if Result.Y <> 0 then
223 Result := Result.Normalize;
224 end;
225
226 var
227 BottomCircleIndex, BottomMiddleIndex, BottomFaceIndex: Integer;
228 TopCircleIndex, TopMiddleIndex, TopFaceIndex: Integer;
229 SideBottomCircleIndex, SideTopCircleIndex, SideFaceIndex: Integer;
230
231 { Add additional stack in the middle.
232 StackHeight should be between 0 = bottom and 1 = top
233 (not inclusive, i.e. don't use this to generate bottom/top,
234 this is done by special code.) }
235 procedure AddStack(const StackHeight: Single);
236 var
237 I: Integer;
238 MiddleCircleIndex, MiddleFaceIndex: Integer;
239 begin
240 MiddleCircleIndex := Coord.Count;
241 Coord.Count := Coord.Count + QuadricSlices + 1;
242 GenerateCircle(QuadricSlices, Lerp(StackHeight, BottomRadius, TopRadius), -Height / 2 + StackHeight * Height, PVector3(Coord.Ptr(MiddleCircleIndex)));
243 Coord.L[Coord.Count - 1] := Coord.L[MiddleCircleIndex];
244 Normal.Count := Normal.Count + QuadricSlices + 1;
245 for I := 0 to QuadricSlices - 1 do
246 Normal.L[MiddleCircleIndex + I] := Normal.L[SideBottomCircleIndex + I];
247 Normal.L[Normal.Count - 1] := Normal.L[MiddleCircleIndex];
248 if TexCoord <> nil then
249 begin
250 TexCoord.Count := TexCoord.Count + QuadricSlices + 1;
251 for I := 0 to QuadricSlices do
252 TexCoord.L[MiddleCircleIndex + I] := Vector2(I / QuadricSlices, StackHeight);
253 end;
254
255 MiddleFaceIndex := CoordIndex.Count;
256 CoordIndex.Count := CoordIndex.Count + QuadricSlices * 5;
257 for I := 0 to QuadricSlices - 1 do
258 begin
259 CoordIndex.L[MiddleFaceIndex + I * 5 ] := MiddleCircleIndex + I;
260 CoordIndex.L[MiddleFaceIndex + I * 5 + 1] := SideBottomCircleIndex + I;
261 CoordIndex.L[MiddleFaceIndex + I * 5 + 2] := SideBottomCircleIndex + I + 1;
262 CoordIndex.L[MiddleFaceIndex + I * 5 + 3] := MiddleCircleIndex + I + 1;
263 CoordIndex.L[MiddleFaceIndex + I * 5 + 4] := -1;
264 end;
265
266 if MaterialIndex <> nil then
267 MaterialIndex.AddDuplicate(0, QuadricSlices);
268
269 { Next stack added. Now replace SideBottomCircleIndex,
270 to say that the the current circle is the bottom now. }
271 SideBottomCircleIndex := MiddleCircleIndex;
272 end;
273
274 var
275 I, J: Integer;
276 begin
277 { For VRML 1.0, some of these MF fields by default have non-empty content.
278 It's safest to just clean them. }
279 CoordIndex.Count := 0;
280 Coord.Count := 0;
281 Normal.Count := 0;
282 if TexCoord <> nil then TexCoord.Count := 0;
283 if MaterialIndex <> nil then MaterialIndex.Count := 0;
284
285 if Bottom then
286 begin
287 BottomCircleIndex := Coord.Count;
288 Coord.Count := Coord.Count + QuadricSlices;
289 GenerateCircle(QuadricSlices, BottomRadius, -Height / 2, PVector3(Coord.Ptr(BottomCircleIndex)));
290 Normal.Count := Normal.Count + QuadricSlices;
291 for I := 0 to QuadricSlices - 1 do
292 Normal.L[BottomCircleIndex + I] := Vector3(0, -1, 0);
293 if TexCoord <> nil then
294 begin
295 TexCoord.Count := TexCoord.Count + QuadricSlices;
296 GenerateTexCoordCircle(QuadricSlices, PVector2(TexCoord.Ptr(BottomCircleIndex)), false);
297 end;
298
299 BottomMiddleIndex := Coord.Count;
300 Coord.Add^ := Vector3(0, -Height / 2, 0);
301 Normal.Add^ := Vector3(0, -1, 0);
302 if TexCoord <> nil then
303 TexCoord.Add^ := Vector2(0.5, 0.5);
304
305 BottomFaceIndex := CoordIndex.Count;
306 CoordIndex.Count := CoordIndex.Count + QuadricSlices * 4;
307 for I := 0 to QuadricSlices - 1 do
308 begin
309 if I <> QuadricSlices - 1 then
310 CoordIndex.L[BottomFaceIndex + I * 4 ] := BottomCircleIndex + I + 1 else
311 CoordIndex.L[BottomFaceIndex + I * 4 ] := BottomCircleIndex;
312 CoordIndex.L[BottomFaceIndex + I * 4 + 1] := BottomCircleIndex + I;
313 CoordIndex.L[BottomFaceIndex + I * 4 + 2] := BottomMiddleIndex;
314 CoordIndex.L[BottomFaceIndex + I * 4 + 3] := -1;
315 end;
316
317 if MaterialIndex <> nil then
318 begin
319 { fill materialIndex for bottom disk.
320 For VRML 1.0 Cone (TopRadius = 0), this is material 1,
321 for VRML 1.0 Cylinder this is material 2. }
322 if TopRadius = 0 then
323 MaterialIndex.AddDuplicate(1, QuadricSlices) else
324 MaterialIndex.AddDuplicate(2, QuadricSlices);
325 end;
326 end;
327
328 if Top and (TopRadius <> 0) then
329 begin
330 TopCircleIndex := Coord.Count;
331 Coord.Count := Coord.Count + QuadricSlices;
332 GenerateCircle(QuadricSlices, TopRadius, Height / 2, PVector3(Coord.Ptr(TopCircleIndex)));
333 Normal.Count := Normal.Count + QuadricSlices;
334 for I := 0 to QuadricSlices - 1 do
335 Normal.L[TopCircleIndex + I] := Vector3(0, 1, 0);
336 if TexCoord <> nil then
337 begin
338 TexCoord.Count := TexCoord.Count + QuadricSlices;
339 GenerateTexCoordCircle(QuadricSlices, PVector2(TexCoord.Ptr(TopCircleIndex)), true);
340 end;
341
342 TopMiddleIndex := Coord.Count;
343 Coord.Add^ := Vector3(0, Height / 2, 0);
344 Normal.Add^ := Vector3(0, 1, 0);
345 if TexCoord <> nil then
346 TexCoord.Add^ := Vector2(0.5, 0.5);
347
348 TopFaceIndex := CoordIndex.Count;
349 CoordIndex.Count := CoordIndex.Count + QuadricSlices * 4;
350 for I := 0 to QuadricSlices - 1 do
351 begin
352 CoordIndex.L[TopFaceIndex + I * 4 ] := TopCircleIndex + I;
353 if I <> QuadricSlices - 1 then
354 CoordIndex.L[TopFaceIndex + I * 4 + 1] := TopCircleIndex + I + 1 else
355 CoordIndex.L[TopFaceIndex + I * 4 + 1] := TopCircleIndex;
356 CoordIndex.L[TopFaceIndex + I * 4 + 2] := TopMiddleIndex;
357 CoordIndex.L[TopFaceIndex + I * 4 + 3] := -1;
358 end;
359
360 if MaterialIndex <> nil then
361 begin
362 { fill materialIndex for top disk (this will be called only for VRML 1.0
363 Cylinder). }
364 MaterialIndex.AddDuplicate(1, QuadricSlices);
365 end;
366 end;
367
368 if Side then
369 begin
370 { Generate another circles, instead of reusing Bottom/TopCircleIndex.
371 That's because these vertices need different normal vectors anyway,
372 so we may as well duplicate their coords.
373
374 We generate QuadricSlices + 1 edges (instead of making QuadricSlices
375 edges, and reusing the first edge for the last edge).
376 Reason: texture coordinates must be different at the last edge
377 (1.0) than the first (0.0).
378 Otherwise texture seam would not be correctly closed. }
379 SideBottomCircleIndex := Coord.Count;
380 Coord.Count := Coord.Count + QuadricSlices + 1;
381 GenerateCircle(QuadricSlices, BottomRadius, -Height / 2, PVector3(Coord.Ptr(SideBottomCircleIndex)));
382 Coord.L[Coord.Count - 1] := Coord.L[SideBottomCircleIndex];
383 Normal.Count := Normal.Count + QuadricSlices + 1;
384 for I := 0 to QuadricSlices - 1 do
385 Normal.L[SideBottomCircleIndex + I] := SideNormal(I);
386 Normal.L[Normal.Count - 1] := Normal.L[SideBottomCircleIndex];
387 if TexCoord <> nil then
388 begin
389 TexCoord.Count := TexCoord.Count + QuadricSlices + 1;
390 for I := 0 to QuadricSlices do
391 TexCoord.L[SideBottomCircleIndex + I] := Vector2(I / QuadricSlices, 0);
392 end;
393
394 { Add additional "stacks" to the geometry when QuadricStacks > 1 }
395 for J := 1 to QuadricStacks - 1 do
396 AddStack(J / QuadricStacks);
397 (* Alt approach, for Cone tip:
398 if ExtraStackNearTop then
399 AddStack(0.9);
400 *)
401
402 SideTopCircleIndex := Coord.Count;
403 Coord.Count := Coord.Count + QuadricSlices + 1;
404 { Even when TopRadius = 0 (we make a cone), we still generate
405 QuadricSlices * top points.
406 See comments at the beginning about "How to make smooth normals...". }
407 GenerateCircle(QuadricSlices, TopRadius, Height / 2, PVector3(Coord.Ptr(SideTopCircleIndex)));
408 Coord.L[Coord.Count - 1] := Coord.L[SideTopCircleIndex];
409 Normal.Count := Normal.Count + QuadricSlices + 1;
410 for I := 0 to QuadricSlices - 1 do
411 Normal.L[SideTopCircleIndex + I] := Normal.L[SideBottomCircleIndex + I];
412 Normal.L[Normal.Count - 1] := Normal.L[SideTopCircleIndex];
413 if TexCoord <> nil then
414 begin
415 TexCoord.Count := TexCoord.Count + QuadricSlices + 1;
416 for I := 0 to QuadricSlices do
417 TexCoord.L[SideTopCircleIndex + I] := Vector2(I / QuadricSlices, 1);
418 end;
419
420 SideFaceIndex := CoordIndex.Count;
421 if TopRadius <> 0 then
422 begin
423 CoordIndex.Count := CoordIndex.Count + QuadricSlices * 5;
424 for I := 0 to QuadricSlices - 1 do
425 begin
426 CoordIndex.L[SideFaceIndex + I * 5 ] := SideTopCircleIndex + I;
427 CoordIndex.L[SideFaceIndex + I * 5 + 1] := SideBottomCircleIndex + I;
428 CoordIndex.L[SideFaceIndex + I * 5 + 2] := SideBottomCircleIndex + I + 1;
429 CoordIndex.L[SideFaceIndex + I * 5 + 3] := SideTopCircleIndex + I + 1;
430 CoordIndex.L[SideFaceIndex + I * 5 + 4] := -1;
431 end;
432 end else
433 begin
434 { We *could* use the above code, that treats each face as a quad,
435 even in case TopRadius = 0. OpenGL rendering works (in practice,
436 rects are converted into two triangles and one of them will not
437 do anything), octree construction works (degenerate triangle is
438 removed). But what's the point in wasting memory (and sending
439 to OpenGL dummy triangles) then? }
440 CoordIndex.Count := CoordIndex.Count + QuadricSlices * 4;
441 for I := 0 to QuadricSlices - 1 do
442 begin
443 CoordIndex.L[SideFaceIndex + I * 4 ] := SideBottomCircleIndex + I;
444 CoordIndex.L[SideFaceIndex + I * 4 + 1] := SideBottomCircleIndex + I + 1;
445 CoordIndex.L[SideFaceIndex + I * 4 + 2] := SideTopCircleIndex + I + 1;
446 CoordIndex.L[SideFaceIndex + I * 4 + 3] := -1;
447 end;
448 end;
449
450 if MaterialIndex <> nil then
451 MaterialIndex.AddDuplicate(0, QuadricSlices);
452 end;
453 end;
454
455 { VRML 2 proxy --------------------------------------------------------------- }
456
457 function TCylinderNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode;
458 var
459 CoordNode: TCoordinateNode;
460 NormalNode: TNormalNode;
461 TexCoordNode: TTextureCoordinateNode;
462 TexCoords: TVector2List;
463 IFS: TIndexedFaceSetNode absolute Result;
464 begin
465 IFS := TIndexedFaceSetNode.Create(X3DName, BaseUrl);
466 try
467 CoordNode := TCoordinateNode.Create('', BaseUrl);
468 IFS.FdCoord.Value := CoordNode;
469
470 NormalNode := TNormalNode.Create('', BaseUrl);
471 IFS.FdNormal.Value := NormalNode;
472 IFS.FdNormalPerVertex.Value := true;
473
474 if (FdTexCoord.Value <> nil) and FdTexCoord.CurrentChildAllowed then
475 begin
476 { No need for CylinderCone_Proxy to create tex coords. }
477 IFS.FdTexCoord.Value := FdTexCoord.Value;
478 TexCoords := nil;
479 end else
480 begin
481 TexCoordNode := TTextureCoordinateNode.Create('', BaseUrl);
482 IFS.FdTexCoord.Value := TexCoordNode;
483 TexCoords := TexCoordNode.FdPoint.Items;
484 end;
485
486 CylinderCone_Proxy(IFS.FdCoordIndex.Items,
487 CoordNode.FdPoint.Items, NormalNode.FdVector.Items, TexCoords, nil,
488 FdRadius.Value, FdRadius.Value, FdHeight.Value,
489 FdBottom.Value, FdSide.Value, FdTop.Value,
490 CalculateSlices, CalculateStacks);
491
492 IFS.FdSolid.Value := FdSolid.Value;
493
494 { Smooth everything. }
495 IFS.FdCreaseAngle.Value := 4;
496 except FreeAndNil(Result); raise end;
497 end;
498
499 function TConeNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode;
500 var
501 CoordNode: TCoordinateNode;
502 NormalNode: TNormalNode;
503 TexCoordNode: TTextureCoordinateNode;
504 TexCoords: TVector2List;
505 IFS: TIndexedFaceSetNode absolute Result;
506 begin
507 IFS := TIndexedFaceSetNode.Create(X3DName, BaseUrl);
508 try
509 CoordNode := TCoordinateNode.Create('', BaseUrl);
510 IFS.FdCoord.Value := CoordNode;
511
512 NormalNode := TNormalNode.Create('', BaseUrl);
513 IFS.FdNormal.Value := NormalNode;
514 IFS.FdNormalPerVertex.Value := true;
515
516 if (FdTexCoord.Value <> nil) and FdTexCoord.CurrentChildAllowed then
517 begin
518 { No need for CylinderCone_Proxy to create tex coords. }
519 IFS.FdTexCoord.Value := FdTexCoord.Value;
520 TexCoords := nil;
521 end else
522 begin
523 TexCoordNode := TTextureCoordinateNode.Create('', BaseUrl);
524 IFS.FdTexCoord.Value := TexCoordNode;
525 TexCoords := TexCoordNode.FdPoint.Items;
526 end;
527
528 CylinderCone_Proxy(IFS.FdCoordIndex.Items,
529 CoordNode.FdPoint.Items, NormalNode.FdVector.Items, TexCoords, nil,
530 FdBottomRadius.Value, 0, FdHeight.Value,
531 FdBottom.Value, FdSide.Value, false,
532 CalculateSlices, CalculateStacks);
533
534 IFS.FdSolid.Value := FdSolid.Value;
535
536 { Smooth everything. }
537 IFS.FdCreaseAngle.Value := 4;
538 except FreeAndNil(Result); raise end;
539 end;
540
541 { VRML 1 proxy --------------------------------------------------------------- }
542
543 {$warnings off} // using deprecated constants to keep supporting VRML 1
544 function TCylinderNode_1.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode;
545 var
546 CoordNode: TCoordinate3Node_1;
547 NormalNode: TNormalNode;
548 NormalBinding: TNormalBindingNode_1;
549 TexCoordNode: TTextureCoordinate2Node_1;
550 ShapeHints: TShapeHintsNode_1;
551 MaterialBinding: TMaterialBindingNode_1;
552 MaterialIndex: TInt32List;
553 IFS: TIndexedFaceSetNode_1 absolute Result;
554 begin
555 IFS := TIndexedFaceSetNode_1.Create(X3DName, BaseUrl);
556 try
557 { we have to modify State, so make a copy of it }
558 State := TX3DGraphTraverseState.CreateCopy(State);
559
560 CoordNode := TCoordinate3Node_1.Create('', BaseUrl);
561 State.VRML1State.SetOwnNode(vsCoordinate3, CoordNode);
562
563 NormalNode := TNormalNode.Create('', BaseUrl);
564 State.VRML1State.SetOwnNode(vsNormal, NormalNode);
565
566 NormalBinding := TNormalBindingNode_1.Create('', BaseUrl);
567 { NormalBinding.value = PER_VERTEX means we use niPerVertexCoordIndexed,
568 so coordIndex chooses the normal. }
569 NormalBinding.FdValue.Value := BIND_PER_VERTEX;
570 State.VRML1State.SetOwnNode(vsNormalBinding, NormalBinding);
571
572 TexCoordNode := TTextureCoordinate2Node_1.Create('', BaseUrl);
573 State.VRML1State.SetOwnNode(vsTextureCoordinate2, TexCoordNode);
574
575 ShapeHints := TShapeHintsNode_1.Create('', BaseUrl);
576 { For VRML 1.0, Cylinder is never solid. }
577 ShapeHints.FdshapeType.Value := SHTYPE_UNKNOWN;
578 ShapeHints.FdvertexOrdering.Value := VERTORDER_COUNTERCLOCKWISE;
579 { Smooth everything. Not really needed, we use explicit normal node now. }
580 ShapeHints.FdCreaseAngle.Value := 4;
581 State.VRML1State.SetOwnNode(vsShapeHints, ShapeHints);
582
583 { calculate MaterialBinding node and MaterialIndex }
584 MaterialBinding := TMaterialBindingNode_1.Create('', BaseUrl);
585 if State.VRML1State.MaterialBinding.FdValue.Value in
586 [BIND_PER_PART, BIND_PER_PART_INDEXED] then
587 begin
588 MaterialIndex := IFS.FdMaterialIndex.Items;
589 MaterialBinding.FdValue.Value := BIND_PER_FACE_INDEXED;
590 end else
591 begin
592 MaterialIndex := nil;
593 MaterialBinding.FdValue.Value := BIND_OVERALL;
594 end;
595 State.VRML1State.SetOwnNode(vsMaterialBinding, MaterialBinding);
596
597 CylinderCone_Proxy(IFS.FdCoordIndex.Items,
598 CoordNode.FdPoint.Items, NormalNode.FdVector.Items,
599 TexCoordNode.FdPoint.Items, MaterialIndex,
600 FdRadius.Value, FdRadius.Value, FdHeight.Value,
601 FdParts.Flags[CYLINDER_PARTS_BOTTOM],
602 FdParts.Flags[CYLINDER_PARTS_SIDES],
603 FdParts.Flags[CYLINDER_PARTS_TOP],
604 DefaultTriangulationSlices,
605 { Just like with TCylinderNode.CalculateStacks, Cylinder in VRML 1 doesn't use stacks }
606 1);
607
608 { For VRML 1.0, unfortunately textureCoordIndex must be set
609 (even though it's exactly equivalent to coordIndex).
610 This is a problem of VRML 1.0 "state" idea --- there is no
611 other way to "turn off" texture than to just use empty textureCoordIndex. }
612 IFS.FdTextureCoordIndex.Items.Assign(IFS.FdCoordIndex.Items);
613 except FreeAndNil(Result); raise end;
614 end;
615
616 function TConeNode_1.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode;
617 var
618 CoordNode: TCoordinate3Node_1;
619 NormalNode: TNormalNode;
620 NormalBinding: TNormalBindingNode_1;
621 TexCoordNode: TTextureCoordinate2Node_1;
622 ShapeHints: TShapeHintsNode_1;
623 MaterialBinding: TMaterialBindingNode_1;
624 MaterialIndex: TInt32List;
625 IFS: TIndexedFaceSetNode_1 absolute Result;
626 begin
627 IFS := TIndexedFaceSetNode_1.Create(X3DName, BaseUrl);
628 try
629 { we have to modify State, so make a copy of it }
630 State := TX3DGraphTraverseState.CreateCopy(State);
631
632 CoordNode := TCoordinate3Node_1.Create('', BaseUrl);
633 State.VRML1State.SetOwnNode(vsCoordinate3, CoordNode);
634
635 NormalNode := TNormalNode.Create('', BaseUrl);
636 State.VRML1State.SetOwnNode(vsNormal, NormalNode);
637
638 NormalBinding := TNormalBindingNode_1.Create('', BaseUrl);
639 { NormalBinding.value = PER_VERTEX means we use niPerVertexCoordIndexed,
640 so coordIndex chooses the normal. }
641 NormalBinding.FdValue.Value := BIND_PER_VERTEX;
642 State.VRML1State.SetOwnNode(vsNormalBinding, NormalBinding);
643
644 TexCoordNode := TTextureCoordinate2Node_1.Create('', BaseUrl);
645 State.VRML1State.SetOwnNode(vsTextureCoordinate2, TexCoordNode);
646
647 ShapeHints := TShapeHintsNode_1.Create('', BaseUrl);
648 { For VRML 1.0, Cone is never solid. }
649 ShapeHints.FdshapeType.Value := SHTYPE_UNKNOWN;
650 ShapeHints.FdvertexOrdering.Value := VERTORDER_COUNTERCLOCKWISE;
651 { Smooth everything. Not really needed, we use explicit normal node now. }
652 ShapeHints.FdCreaseAngle.Value := 4;
653 State.VRML1State.SetOwnNode(vsShapeHints, ShapeHints);
654
655 { calculate MaterialBinding node and MaterialIndex }
656 MaterialBinding := TMaterialBindingNode_1.Create('', BaseUrl);
657 if State.VRML1State.MaterialBinding.FdValue.Value in
658 [BIND_PER_PART, BIND_PER_PART_INDEXED] then
659 begin
660 MaterialIndex := IFS.FdMaterialIndex.Items;
661 MaterialBinding.FdValue.Value := BIND_PER_FACE_INDEXED;
662 end else
663 begin
664 MaterialIndex := nil;
665 MaterialBinding.FdValue.Value := BIND_OVERALL;
666 end;
667 State.VRML1State.SetOwnNode(vsMaterialBinding, MaterialBinding);
668
669 CylinderCone_Proxy(IFS.FdCoordIndex.Items,
670 CoordNode.FdPoint.Items, NormalNode.FdVector.Items,
671 TexCoordNode.FdPoint.Items, MaterialIndex,
672 FdBottomRadius.Value, 0, FdHeight.Value,
673 FdParts.Flags[CONE_PARTS_BOTTOM], FdParts.Flags[CONE_PARTS_SIDES], false,
674 DefaultTriangulationSlices, DefaultTriangulationStacks);
675
676 { For VRML 1.0, unfortunately textureCoordIndex must be set
677 (even though it's exactly equivalent to coordIndex).
678 This is a problem of VRML 1.0 "state" idea --- there is no
679 other way to "turn off" texture than to just use empty textureCoordIndex. }
680 IFS.FdTextureCoordIndex.Items.Assign(IFS.FdCoordIndex.Items);
681 except FreeAndNil(Result); raise end;
682 end;
683 {$warnings on}