Debian Codegraph @

src/scene/castleinternalarraysgenerator_geometry3d.inc

raw
1 {%MainUnit castleinternalarraysgenerator.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 { TVRMLArraysGenerator descendants implementing nodes in X3D "Geometry3D"
18 component. }
19
20 type
21 { Handling IndexedFaceSet for VRML <= 1.0 and >= 2.0.
22
23 Note that this cannot deal with NorImplementation = niNone,
24 you're expected to set NorImplementation to non-none in constructor.
25 A simplest way to do this is to use AutoGenerateNormals. }
26 TAbstractIndexedFaceSetGenerator = class(TAbstractCompleteGenerator)
27 private
28 IFSIndexesCount: Cardinal;
29 IFSNextIndex: Cardinal;
30
31 procedure PrepareIndexesTriangle1(const TriIndices: TVector3Integer);
32 procedure PrepareIndexesTriangle2(const TriIndices: TVector3Integer);
33
34 { Add triangle indexes to IndexesFromCoordIndex, TrivialIndexesFromCoordIndex,
35 increase IFSNextIndex by 3. }
36 procedure AddTriangleIndexes(const T0, T1, T2: UInt32);
37
38 procedure PrepareIndexesCoordsRange1(
39 const RangeNumber: Cardinal; BeginIndex, EndIndex: Integer);
40 procedure PrepareIndexesCoordsRange2(
41 const RangeNumber: Cardinal; BeginIndex, EndIndex: Integer);
42
43 procedure GenerateTriangle(const TriIndices: TVector3Integer);
44 protected
45 { Set these in descendant's constructor. }
46 FaceConvex: boolean;
47
48 procedure GenerateCoordinate; override;
49 procedure GenerateCoordinateEnd; override;
50 procedure GenerateCoordsRange(const RangeNumber: Cardinal;
51 BeginIndex, EndIndex: Integer); override;
52
53 { Auto-generate normals (from NormalsCcw side)
54 and set Normals, NorImplementation properties to use them. }
55 procedure AutoGenerateNormals(const CreaseAngle: Single);
56 procedure PrepareIndexesPrimitives; override;
57 public
58 class function BumpMappingAllowed: boolean; override;
59 end;
60
61 TIndexedFaceSet_1Generator = class(TAbstractIndexedFaceSetGenerator)
62 private
63 VRML1FrontFaceCcw, VRML1CullFace: boolean;
64 protected
65 procedure GenerateCoordinateBegin; override;
66 public
67 constructor Create(AShape: TShape); override;
68 end;
69
70 TIndexedFaceSetGenerator = class(TAbstractIndexedFaceSetGenerator)
71 public
72 constructor Create(AShape: TShape); override;
73 end;
74
75 { TAbstractIndexedFaceSetGenerator ------------------------------------------- }
76
77 class function TAbstractIndexedFaceSetGenerator.BumpMappingAllowed: boolean;
78 begin
79 Result := true;
80 end;
81
82 { TIndexedFaceSetGenerator makes triangles, GL_TRIANGLES in OpenGL.
83 Making one TRIANGLES primitive is fast, and allows us to do everything
84 we want.
85
86 Old notes: I was experimenting also with making TRIANGLE_FAN (or POLYGON)
87 per face.
88
89 Advantages of TRIANGLE_FAN:
90
91 - TRIANGLE_FAN *may* increase speed, because vertices
92 are more shared and specified less times. For TRIANGLES, we waste 2 vertex
93 calls for every vertex (above 3) for each face.
94
95 - TRIANGLE_FAN workarounds NVidia GeForce 5200 bug for material
96 binding PER_FACE_INDEXED, see
97 demo_models/vrml_1/material_per_face_problematic_on_nvidia_gf_5200.wrl
98 Although the problem disappeared by itself after using GL_COLOR_MATERIAL
99 for VRML 1.0 per-face/vertex materials.
100
101 Avantages of TRIANGLES:
102
103 - TRIANGLE_FAN is highly unoptimal on newer GPUs (Radeon on chantal).
104 The problem is you have to call glEnd / glBegin again
105 a lot, and this hurts pipeline performance very badly. And the "sharing"
106 argument for TRIANGLE_FAN is weak, as newer GPUs cache vertex data
107 by index anyway.
108
109 Tests on Radeon X1600 (chantal, fglrx) show
110 TRIANGLES FPS: 165/155
111 TRIANGLE_FAN FPS: 60/55
112 Clearly, TRIANGLES win.
113
114 Although tests on NVidia (kocury, GeForce FX 5200) don't show such
115 drastic difference.
116
117 - TRIANGLES workaround Radeon X1600 Darwin (macOS 10.4.9)
118 OpenGL bugs. (With Linux fglrx on the same hardware there is no bug.)
119 It seems not able to handle GL_TRIANGLE_FAN properly,
120 which is visible on "The Gate" and
121 "Cages" levels of "The Castle": some triangles on these
122 levels (on "The Gate", it concerns that back wall and gate
123 geometry) seem to randomly disappear.
124
125 This is a shortcut of even longer old comments... For the full story,
126 see revision 6432 in SVN repo,
127 and search for TryRenderingFaceAsFan comments.
128 }
129
130 procedure TAbstractIndexedFaceSetGenerator.GenerateCoordinate;
131 begin
132 Geometry.InternalMakeCoordRanges(State,
133 {$ifdef FPC}@{$endif}GenerateCoordsRange);
134 end;
135
136 procedure TAbstractIndexedFaceSetGenerator.AutoGenerateNormals(
137 const CreaseAngle: Single);
138 begin
139 { We could use the same NormalsCreaseAngle for all CreaseAngle values,
140 so the tests for CreaseAngle are not really required below.
141 But this way we're more optimal, as smooth normals allow normals
142 per-vertex (so the shape can be rendered with indexes), flat normals
143 may allow flat shading (may be faster) and such. }
144
145 if Coord = nil then Exit;
146
147 if CreaseAngle >= Pi then
148 begin
149 Normals := Shape.NormalsSmooth(NormalsCcw);
150 NorImplementation := niPerVertexCoordIndexed;
151 end else
152 if CreaseAngle <> 0 then
153 begin
154 Normals := Shape.NormalsCreaseAngle(NormalsCcw, CreaseAngle);
155 NorImplementation := niPerVertexNonIndexed;
156 end else
157 begin
158 Normals := Shape.NormalsFlat(NormalsCcw);
159 NorImplementation := niPerFace;
160 end;
161 end;
162
163 procedure TAbstractIndexedFaceSetGenerator.GenerateCoordinateEnd;
164 begin
165 inherited;
166 end;
167
168 procedure TAbstractIndexedFaceSetGenerator.GenerateTriangle(
169 const TriIndices: TVector3Integer);
170 begin
171 GenerateVertex(TriIndices[0]);
172 GenerateVertex(TriIndices[1]);
173 GenerateVertex(TriIndices[2]);
174 end;
175
176 procedure TAbstractIndexedFaceSetGenerator.GenerateCoordsRange(
177 const RangeNumber: Cardinal; BeginIndex, EndIndex: Integer);
178 var
179 I: Integer;
180 begin
181 inherited;
182
183 if BeginIndex + 2 < EndIndex then
184 CalculateTangentVectors(BeginIndex, BeginIndex + 1, BeginIndex + 2);
185
186 if not FaceConvex then
187 begin
188 TriangulateFace(PInt32Array(CoordIndex.Items.Ptr(BeginIndex)),
189 EndIndex - BeginIndex, PVector3Array(Coord.Items.L), Coord.Count,
190 {$ifdef FPC}@{$endif}GenerateTriangle, BeginIndex);
191 end else
192 begin
193 (* Alternative version:
194 TriangulateConvexFace(EndIndex - BeginIndex, @GenerateVertex, BeginIndex); *)
195
196 for I := BeginIndex to EndIndex - 3 do
197 begin
198 GenerateVertex(BeginIndex);
199 GenerateVertex(I + 1);
200 GenerateVertex(I + 2);
201 end;
202 end;
203 end;
204
205 procedure TAbstractIndexedFaceSetGenerator.PrepareIndexesTriangle1(
206 const TriIndices: TVector3Integer);
207 begin
208 Inc(IFSIndexesCount, 3);
209 end;
210
211 procedure TAbstractIndexedFaceSetGenerator.PrepareIndexesCoordsRange1(
212 const RangeNumber: Cardinal; BeginIndex, EndIndex: Integer);
213 begin
214 if not FaceConvex then
215 begin
216 TriangulateFace(PInt32Array(CoordIndex.Items.Ptr(BeginIndex)),
217 EndIndex - BeginIndex, PVector3Array(Coord.Items.L), Coord.Count,
218 {$ifdef FPC}@{$endif}PrepareIndexesTriangle1, BeginIndex);
219 end else
220 begin
221 IFSIndexesCount := IFSIndexesCount + Max(EndIndex - BeginIndex - 2, 0) * 3;
222 end;
223 end;
224
225 procedure TAbstractIndexedFaceSetGenerator.AddTriangleIndexes(const T0, T1, T2: UInt32);
226 begin
227 TrivialIndexesFromCoordIndex.L[IFSNextIndex] := T0; IndexesFromCoordIndex.L[IFSNextIndex] := CoordIndex.Items.L[T0]; Inc(IFSNextIndex);
228 TrivialIndexesFromCoordIndex.L[IFSNextIndex] := T1; IndexesFromCoordIndex.L[IFSNextIndex] := CoordIndex.Items.L[T1]; Inc(IFSNextIndex);
229 TrivialIndexesFromCoordIndex.L[IFSNextIndex] := T2; IndexesFromCoordIndex.L[IFSNextIndex] := CoordIndex.Items.L[T2]; Inc(IFSNextIndex);
230 end;
231
232 procedure TAbstractIndexedFaceSetGenerator.PrepareIndexesTriangle2(
233 const TriIndices: TVector3Integer);
234 begin
235 AddTriangleIndexes(
236 TriIndices.X,
237 TriIndices.Y,
238 TriIndices.Z
239 );
240 end;
241
242 procedure TAbstractIndexedFaceSetGenerator.PrepareIndexesCoordsRange2(
243 const RangeNumber: Cardinal; BeginIndex, EndIndex: Integer);
244 var
245 I, IFSNextIndexBegin: Integer;
246 begin
247 IFSNextIndexBegin := IFSNextIndex;
248
249 if not FaceConvex then
250 begin
251 TriangulateFace(PInt32Array(CoordIndex.Items.Ptr(BeginIndex)),
252 EndIndex - BeginIndex, PVector3Array(Coord.Items.L), Coord.Count,
253 {$ifdef FPC}@{$endif}PrepareIndexesTriangle2, BeginIndex);
254 end else
255 begin
256 for I := BeginIndex to EndIndex - 3 do
257 begin
258 AddTriangleIndexes(
259 BeginIndex,
260 I + 1,
261 I + 2
262 );
263 end;
264 end;
265
266 if FacesNeeded then
267 { Note that IFSNextIndex may be 0,
268 testcase: tests/data/extrusion_empty_spine_concave.x3dv
269 (makes integer overflow with Delphi from autotests). }
270 for I := IFSNextIndexBegin to Integer(IFSNextIndex) - 1 do
271 begin
272 Arrays.Faces.L[I].IndexBegin := BeginIndex;
273 Arrays.Faces.L[I].IndexEnd := EndIndex;
274 end;
275 end;
276
277 procedure TAbstractIndexedFaceSetGenerator.PrepareIndexesPrimitives;
278 begin
279 { calculate IFSIndexesCount by one iteration over coordIndex }
280 IFSIndexesCount := 0;
281 Geometry.InternalMakeCoordRanges(State,
282 {$ifdef FPC}@{$endif}PrepareIndexesCoordsRange1);
283
284 if FacesNeeded then
285 begin
286 Arrays.Faces := TFaceIndexesList.Create;
287 Arrays.Faces.Count := IFSIndexesCount;
288 end;
289
290 { calculate TrivialIndexesFromCoordIndex, IndexesFromCoordIndex contents: triangulate }
291 IndexesFromCoordIndex := TUInt32List.Create;
292 IndexesFromCoordIndex.Count := IFSIndexesCount;
293 TrivialIndexesFromCoordIndex := TUInt32List.Create;
294 TrivialIndexesFromCoordIndex.Count := IFSIndexesCount;
295 IFSNextIndex := 0;
296 Geometry.InternalMakeCoordRanges(State,
297 {$ifdef FPC}@{$endif}PrepareIndexesCoordsRange2);
298 Assert(IFSNextIndex = IFSIndexesCount);
299 end;
300
301 { TIndexedFaceSet_1Generator -------------------------------------------------- }
302
303 constructor TIndexedFaceSet_1Generator.Create(AShape: TShape);
304 var
305 SH: TShapeHintsNode_1;
306 ANode: TIndexedFaceSetNode_1;
307 begin
308 inherited;
309
310 ANode := Geometry as TIndexedFaceSetNode_1;
311
312 TexCoordIndex := ANode.FdTextureCoordIndex;
313
314 MaterialIndex := ANode.FdMaterialIndex;
315 MaterialBinding := State.VRML1State.MaterialBinding.FdValue.Value;
316 UpdateMat1Implementation;
317
318 SH := State.VRML1State.ShapeHints;
319
320 {$warnings off} // using deprecated to keep supporting VRML 1
321 { At this place, we assume that VERTORDER_UNKNOWN_ORDERING is actually
322 VERTORDER_COUNTERCLOCKWISE.
323 (VRML 1 authors should not provide explicit normals at all if ordering
324 is unknown.) }
325 VRML1FrontFaceCcw := SH.FdVertexOrdering.Value <> VERTORDER_CLOCKWISE;
326
327 VRML1CullFace :=
328 (SH.FdVertexOrdering.Value <> VERTORDER_UNKNOWN) and
329 (SH.FdShapeType.Value = SHTYPE_SOLID);
330
331 FaceConvex := SH.FdFaceType.Value = FACETYPE_CONVEX;
332 {$warnings on}
333
334 NormalIndex := ANode.FdNormalIndex;
335 Normals := State.VRML1State.Normal.FdVector.Items;
336 NormalsCcw := VRML1FrontFaceCcw;
337 NorImplementation := NorImplementationFromVRML1Binding(
338 State.VRML1State.NormalBinding.FdValue.Value);
339 if NorImplementation = niNone then
340 AutoGenerateNormals(SH.FdCreaseAngle.Value);
341 end;
342
343 procedure TIndexedFaceSet_1Generator.GenerateCoordinateBegin;
344 begin
345 inherited;
346 { Already calculated in constructor, pass to Arrays now }
347 Arrays.FrontFaceCcw := VRML1FrontFaceCcw;
348 Arrays.CullFace := VRML1CullFace;
349 end;
350
351 { TIndexedFaceSetGenerator -------------------------------------------------- }
352
353 constructor TIndexedFaceSetGenerator.Create(AShape: TShape);
354 var
355 ANode: TIndexedFaceSetNode;
356 begin
357 inherited;
358
359 ANode := Geometry as TIndexedFaceSetNode;
360
361 TexCoordIndex := ANode.FdTexCoordIndex;
362
363 Normals := ANode.InternalNormal;
364 TangentsFromNode := ANode.InternalTangent;
365 NormalsCcw := ANode.FdCcw.Value;
366 if Normals <> nil then
367 begin
368 NormalIndex := ANode.FdNormalIndex;
369 if ANode.FdNormalPerVertex.Value then
370 begin
371 if NormalIndex.Count > 0 then
372 NorImplementation := niPerVertexNormalIndexed else
373 NorImplementation := niPerVertexCoordIndexed;
374 end else
375 begin
376 if NormalIndex.Count > 0 then
377 NorImplementation := niPerFaceNormalIndexed else
378 NorImplementation := niPerFace;
379 end;
380 end else
381 AutoGenerateNormals(ANode.FdCreaseAngle.Value);
382
383 FaceConvex := ANode.FdConvex.Value;
384
385 { calculate ColorXxx fields }
386 ColorNode := ANode.Color;
387 Color := ANode.InternalColor;
388 ColorRGBA := ANode.InternalColorRGBA;
389 ColorPerVertex := ANode.FdColorPerVertex.Value;
390 ColorIndex := ANode.FdColorIndex;
391 end;