Debian Codegraph @

tools/internal/teapot-to-pascal/teapot_to_pascal.lpr

raw
1 {
2 Copyright 2010-2022 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 { Convert teapot VRML file teapot.wrl into Pascal source file
17 with 4 Teapot constants.
18 This way we get Utah teapot coordinate data in Pascal. }
19
20 uses SysUtils, X3DNodes, X3DLoad;
21
22 var
23 Model: TX3DNode;
24
25 procedure HandleCoords(const BlenderName, PascalName: string);
26 var
27 G: TGroupNode;
28 IFS: TIndexedFaceSetNode;
29 C: TCoordinateNode;
30 I: Integer;
31 Merged: Cardinal;
32 begin
33 { We know how a VRML file generated by Blender looks like, so we simply assume
34 in the code below that it's as expected (1st child of the Group is a Shape etc.).
35 In case of problems, we can simply fail with an exception. }
36 G := Model.FindNodeByName(TGroupNode, 'ME_' + BlenderName, false) as TGroupNode;
37 IFS := (G.FdChildren[0] as TShapeNode).FdGeometry.Value as TIndexedFaceSetNode;
38 C := IFS.FdCoord.Value as TCoordinateNode;
39
40 Merged := C.FdPoint.Items.MergeCloseVertexes(0.001);
41 Writeln(ErrOutput, 'Merged close vertexes on mesh ', BlenderName, ': ', Merged, ' changed.');
42
43 Writeln('Teapot' + PascalName + 'Coord: array [0..', C.FdPoint.Count - 1, '] of TVector3 = (');
44 for I := 0 to C.FdPoint.Count - 1 do
45 begin
46 Write(Format('(X: %g; Y: %g; Z: %g))', [
47 C.FdPoint.Items.L[I][0],
48 C.FdPoint.Items.L[I][1],
49 C.FdPoint.Items.L[I][2] ]));
50 if I < C.FdPoint.Count - 1 then Write(',');
51 Writeln;
52 end;
53 Writeln(');');
54
55 Writeln('Teapot' + PascalName + 'CoordIndex: array [0..', IFS.FdCoordIndex.Count - 1, '] of Int32 = (');
56 for I := 0 to IFS.FdCoordIndex.Count - 1 do
57 begin
58 Write(IFS.FdCoordIndex.Items[I]);
59 if I < IFS.FdCoordIndex.Count - 1 then Write(', ');
60 if IFS.FdCoordIndex.Items[I] < 0 then Writeln;
61 end;
62 Writeln(');');
63 end;
64
65 begin
66 Model := LoadNode('teapot.wrl');
67 try
68 HandleCoords('TeapotManifold', 'Manifold');
69 HandleCoords('Teapot', '');
70 finally FreeAndNil(Model) end;
71 end.