Debian Codegraph @

src/scene/load/x3dloadinternalgeo.pas

raw
1 {
2 Copyright 2002-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 { Load 3D Videoscape GEO models.
17 See [http://local.wasp.uwa.edu.au/~pbourke/dataformats/geo/].
18 We handle basic geometry, we can open files exported by Blender exporter. }
19 unit X3DLoadInternalGEO;
20
21 {$I castleconf.inc}
22
23 interface
24
25 implementation
26
27 uses SysUtils, Classes,
28 X3DNodes, X3DLoad, CastleVectors, CastleUtils, CastleLog,
29 CastleClassUtils, CastleDownload, CastleUriUtils,
30 CastleFilesUtils, CastleStringUtils, X3DLoadInternalUtils;
31
32 { TObject3DGEO ---------------------------------------------------------------- }
33
34 type
35 { Reader of GEO files.
36 Note that contents of Verts and Faces are read-only for user of this unit. }
37 TObject3DGEO = class
38 public
39 Verts: TVector3List;
40 Faces: TVector3CardinalList;
41 constructor Create(const Stream: TStream);
42 destructor Destroy; override;
43 end;
44
45 constructor TObject3DGEO.Create(const Stream: TStream);
46 type
47 TGEOFormatFlavor = (gfOld, gfMeshColorFaces, gfMeshColorVerts);
48 var
49 Flavor: TGEOFormatFlavor;
50
51 function ReadVertexIndex(const S: string): Cardinal;
52 begin
53 Result := StrToInt(S);
54 { In older format, vertex index is 1-based. }
55 if Flavor = gfOld then Dec(Result);
56 end;
57
58 { Read exactly one line of GEO file, reading new face information.
59 Updates Faces. }
60 procedure ReadGEOFace(const Line: string);
61 var
62 J, ThisPolyCount: Integer;
63 FirstVert, LastVert: Cardinal;
64 CurrentFace: TVector3Cardinal;
65 LineTokens: TCastleStringList;
66 begin
67 LineTokens := CreateTokens(Line);
68 try
69 if LineTokens.Count = 0 then
70 begin
71 WritelnWarning('GEO', 'Empty line');
72 Exit;
73 end;
74
75 ThisPolyCount := StrToInt(LineTokens[0]);
76 if ThisPolyCount < 3 then
77 begin
78 WritelnWarning('GEO', 'Polygon with less than 3 vertexes');
79 Exit;
80 end;
81
82 if LineTokens.Count < ThisPolyCount + 1 then
83 begin
84 WritelnWarning('GEO', 'Not enough vertex indexes on line');
85 Exit;
86 end;
87
88 { the polygon is always at least a triangle, read it }
89 for j := 0 to 2 do
90 CurrentFace.Data[j] := ReadVertexIndex(LineTokens[J + 1]);
91 Faces.Add(CurrentFace);
92
93 FirstVert := CurrentFace.X;
94 LastVert := CurrentFace.Z;
95
96 { for each following vertex, add new triangle by connecting
97 FirstVert, LastVert and new vertexa.
98 Watch out for the order --- make all triangles oriented consistently. }
99 for j := 3 to ThisPolyCount - 1 do
100 begin
101 CurrentFace.X := FirstVert;
102 CurrentFace.Y := LastVert;
103 CurrentFace.Z := ReadVertexIndex(LineTokens[J + 1]);
104 Faces.Add(CurrentFace);
105
106 LastVert := CurrentFace.Z;
107 end;
108 finally FreeAndNil(LineTokens) end;
109 end;
110
111 var
112 Reader: TTextReader;
113 i: Integer;
114 Line: string;
115 VertsCount, PolysCount, VertsInPolysCount: Integer;
116 begin
117 inherited Create;
118 Verts := TVector3List.Create;
119 Faces := TVector3CardinalList.Create;
120
121 Reader := TTextReader.Create(Stream, false);
122 try
123 { Read first line: magic number (or not existent in older GEO format) }
124 Line := Reader.Readln;
125 Line := Trim(Line);
126 if SameText(Line, '3DG1') then
127 Flavor := gfMeshColorFaces else
128 if SameText(Line, 'GOUR') then
129 Flavor := gfMeshColorVerts else
130 Flavor := gfOld;
131
132 if Flavor = gfOld then
133 begin
134 { Use current value of Line, for older format the first line contains
135 these counts. }
136 DeFormat(Line, '%d %d %d', [@VertsCount, @PolysCount, @VertsInPolysCount]);
137
138 { Ile mamy Faces trojkatnych ? Mamy liczbe
139 wielokatow = PolysCount. Mamy sumaryczna liczbe wierzcholkow
140 w nich. Na kazdy polygon przypadaja co najmniej 3 wierzcholki
141 i one daja jeden trojkat. Kazdy nadmiarowy wierzcholek,
142 bez wzgledu na to w ktorym polygonie sie znajdzie, spowoduje
143 utworzenie nowego trojkata. Stad
144 FFacesCount := PolysCount + (VertsInPolysCount - PolysCount * 3);
145 czyli
146 Faces.SetLength(VertsInPolysCount - PolysCount * 2);
147
148 To cooperate with other Flavor, we do not set Faces.Count directly,
149 instead we set only Capacity.
150 }
151 Faces.Capacity := VertsInPolysCount - PolysCount * 2;
152 end else
153 begin
154 { In newer formats, 2nd line contains just VertsCount. }
155 VertsCount := StrToInt(Reader.Readln);
156 PolysCount := -1;
157 end;
158
159 Verts.Count := VertsCount;
160 for i := 0 to Verts.Count-1 do
161 Verts.L[I] := Vector3FromStr(Reader.Readln);
162
163 if PolysCount <> -1 then
164 begin
165 for i := 0 to PolysCount - 1 do
166 ReadGEOFace(Reader.Readln);
167 end else
168 begin
169 { PolysCount not known. So we just read the file as fast as we can. }
170 while not Reader.Eof do
171 ReadGEOFace(Reader.Readln);
172 end;
173 finally FreeAndNil(Reader) end;
174 end;
175
176 destructor TObject3DGEO.Destroy;
177 begin
178 Verts.Free;
179 Faces.Free;
180 inherited;
181 end;
182
183 { LoadGEO -------------------------------------------------------------------- }
184
185 function LoadGEO(const Stream: TStream; const BaseUrl: String): TX3DRootNode;
186 var
187 geo: TObject3DGEO;
188 verts: TCoordinateNode;
189 faces: TIndexedFaceSetNode;
190 Material: TMaterialNode;
191 Appearance: TAppearanceNode;
192 Shape: TShapeNode;
193 i: integer;
194 begin
195 geo := TObject3DGEO.Create(Stream);
196 try
197 result := TX3DRootNode.Create;
198 try
199 Result.HasForceVersion := true;
200 Result.ForceVersion := X3DVersion;
201
202 Material := TMaterialNode.Create;
203
204 Appearance := TAppearanceNode.Create;
205 Appearance.Material := Material;
206
207 Shape := TShapeNode.Create;
208 Shape.Appearance := Appearance;
209
210 result.AddChildren(Shape);
211
212 faces := TIndexedFaceSetNode.Create;
213 Shape.FdGeometry.Value := faces;
214 faces.FdCreaseAngle.Value := NiceCreaseAngle;
215 faces.FdSolid.Value := false;
216 faces.FdCoordIndex.Count := geo.Faces.Count * 4;
217 for i := 0 to geo.Faces.Count-1 do
218 begin
219 faces.FdCoordIndex.Items.L[i * 4 ] := geo.Faces.L[i].X;
220 faces.FdCoordIndex.Items.L[i * 4 + 1] := geo.Faces.L[i].Y;
221 faces.FdCoordIndex.Items.L[i * 4 + 2] := geo.Faces.L[i].Z;
222 faces.FdCoordIndex.Items.L[i * 4 + 3] := -1;
223 end;
224
225 verts := TCoordinateNode.Create;
226 faces.Coord := verts;
227 verts.SetPoint(geo.Verts);
228 except result.Free; raise end;
229 finally geo.Free end;
230 end;
231
232 var
233 ModelFormat: TModelFormat;
234 initialization
235 ModelFormat := TModelFormat.Create;
236 ModelFormat.OnLoad := {$ifdef FPC}@{$endif} LoadGEO;
237 ModelFormat.MimeTypes.Add('application/x-geo');
238 ModelFormat.FileFilterName := 'Videoscape (*.geo)';
239 ModelFormat.Extensions.Add('.geo');
240 RegisterModelFormat(ModelFormat);
241 end.