Debian Codegraph @

src/transform/castleinternalgeometryarrays.pas

raw
1 {
2 Copyright 2010-2023 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 { Geometry represented as arrays (TGeometryArrays). }
17 unit CastleInternalGeometryArrays;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses Generics.Collections,
24 CastleUtils, CastleVectors, CastleTriangles, CastleRenderOptions,
25 CastleInternalGLUtils;
26
27 type
28 { Primitive geometry types. Analogous to OpenGL / OpenGLES primitives. }
29 TGeometryPrimitive = (
30 gpTriangles,
31 gpTriangleFan,
32 gpTriangleStrip,
33 gpLineStrip,
34 gpLineLoop,
35 gpLines,
36 gpPoints
37 );
38
39 TTexCoordDimensions = 2..4;
40
41 { Texture coordinate generation methods.
42
43 For their meaning, see the X3D specification about
44 TextureCoordinateGenerator.mode values.
45 We also add some extensions, see
46 [https://castle-engine.io/x3d_extensions.php#section_ext_tex_coord_worldspace] and
47 [https://castle-engine.io/x3d_extensions.php#section_ext_tex_coord_bounds].
48 We also support ProjectedTextureCoordinate, see
49 [https://castle-engine.io/x3d_extensions_shadow_maps.php#section_ext_texture_gen_projective].
50
51 Special value tgExplicit means that texture coordinates are not generated. }
52 TTextureCoordinateGeneration = (
53 tgExplicit,
54 tgBounds2d,
55 tgBounds3d,
56 tgSphereMap,
57 tgCoord,
58 tgCoordEye,
59 tgCameraSpaceNormal,
60 tgWorldSpaceNormal,
61 tgCameraSpaceReflectionVector,
62 tgWorldSpaceReflectionVector,
63 tgProjection,
64 tgMirrorPlane);
65
66 TProjectorMatrixFunction = function: TMatrix4 of object;
67
68 TTextureGenerationVectors = array [0..2] of TVector4;
69
70 { Texture coord array information, for TGeometryArrays.
71 If Generation <> tgExplicit, then the actual array data is not stored. }
72 TGeometryTexCoord = class
73 Generation: TTextureCoordinateGeneration;
74
75 { If Generation is tgBounds2d or tgBounds3d, then
76 these are vectors used to generate
77 texture coords from local (object space) vertex positions.
78 TextureGen[0] says how to generate S texture coord,
79 TextureGen[1] says how to generate T texture coord,
80 and TextureGen[2] (only for tgBounds3d) is fo R tex coord.}
81 GenerationBoundsVector: TTextureGenerationVectors;
82
83 { For Generation = tgProjection, this is the function that generates
84 matrix used for glTexGen }
85 GenerationProjectorMatrix: TProjectorMatrixFunction;
86
87 { Dimensions, only for Generation = tgExplicit. }
88 Dimensions: TTexCoordDimensions;
89
90 { Offset, only for Generation = tgExplicit. }
91 Offset: Integer;
92 end;
93 TGeometryTexCoordList = {$ifdef FPC}specialize{$endif} TObjectList<TGeometryTexCoord>;
94
95 TGeometryAttribType = (atFloat, atVector2, atVector3, atVector4,
96 atMatrix3, atMatrix4);
97
98 TColorPerVertexType = (ctNone, ctRgb, ctRgbAlpha);
99
100 { GLSL attributes array information, for TGeometryArrays. }
101 TGeometryAttrib = class
102 Name: string;
103 { Internal for our engine (as opposed to specified in 3D model file).
104 This is only used to change warnings related to this attribute. }
105 Internal: boolean;
106 AType: TGeometryAttribType;
107 Offset: Integer;
108 end;
109 TGeometryAttribList = class({$ifdef FPC}specialize{$endif} TObjectList<TGeometryAttrib>)
110 public
111 function Find(const Name: string): TGeometryAttrib;
112 end;
113
114 TGeometryIndex = TGLIndex;
115 TGeometryIndexList = TGLIndexList;
116
117 { Geometry represented as arrays of indexes, vertex positions,
118 texture coordinates and such. Many (eventually, all) geometry nodes
119 (TVRMLGeometryNode) can be processed into an instance of this class.
120
121 This can be used to render, as arrays here map very naturally to
122 an efficient OpenGL vertex arrays and VBOs. We use interleaving,
123 storing everything in two arrays: 1st one for positions and normals
124 (stuff that changes during coordinate morphing, most common dynamic shapes,
125 so we specifically think about optimizing it).
126 2nd one for everything else (colors, tex coords, also GLSL attributes).
127 This should allow for the most efficient usage, making use of interleaving
128 and still allowing fast dynamic updates in common cases. }
129 TGeometryArrays = class
130 private
131 FIndexes: TGeometryIndexList;
132 FIndexesCount: Cardinal;
133 FHasIndexes: boolean;
134 FPrimitive: TGeometryPrimitive;
135 FCount: Integer;
136 FCounts: TCardinalList;
137 FDataFreed: boolean;
138
139 FAttributeArray: Pointer;
140 FAttributeSize: Cardinal;
141
142 FCoordinateArray: Pointer;
143 FCoordinateSize: Cardinal;
144 FCoordinatePreserveGeometryOrder: Boolean;
145
146 FHasTangent: Boolean;
147 FTangentOffset: Integer;
148
149 FColorType: TColorPerVertexType;
150 FColorMode: TColorMode;
151 ColorOffset: Integer;
152 FForceUnlit: boolean;
153 FForcedUnlitColor: TVector4;
154
155 FHasFogCoord: boolean;
156 FogCoordOffset: Integer;
157 FFogDirectValues: boolean;
158
159 FTexCoords: TGeometryTexCoordList;
160 FAttribs: TGeometryAttribList;
161
162 FCullFace: boolean;
163 FFrontFaceCcw: boolean;
164
165 FFaces: TFaceIndexesList;
166
167 procedure SetCount(const Value: Integer);
168 procedure AddTexCoord(const Generation: TTextureCoordinateGeneration;
169 const Dimensions: TTexCoordDimensions;
170 const TextureUnit: Cardinal);
171 function AddGLSLAttribute(const AType: TGeometryAttribType;
172 const Name: string; const Internal: boolean): TGeometryAttrib;
173 function GLSLAttributeIndex(const AType: TGeometryAttribType;
174 const Name: string; const Index: Cardinal): PtrUInt; overload;
175 function GLSLAttributeIndex(const AType: TGeometryAttribType;
176 const A: TGeometryAttrib; const Index: Cardinal): PtrUInt; overload;
177 public
178 constructor Create;
179 destructor Destroy; override;
180
181 { Indexes to remaining arrays.
182
183 If non-nil, we will render using these indexes,
184 which means that items on the remaining lists (vertex positions,
185 tex coords etc.) may be used multiple times. This is good
186 (the lists may be possibly shorter, and GPU will be able to reuse
187 more calculation results), but it's also limited: a vertex
188 must always have the same properties in this case (e.g. the same
189 normal vector, so shape must be completely smooth).
190
191 When this is nil, we will simply use all the vertexes in order.
192 So every item of the remaining lists will be processed exactly once,
193 in the given order. This seems dumb, but actually we're often forced
194 to use this: when you use flat (per-face) normals or colors,
195 then the same vertex position must be used many times with different
196 normal/color. If you want to use OpenGL vertex arrays for whole rendering,
197 this vertex position will just have to be duplicated (which is OK,
198 as the calculation results couldn't be shared anyway,
199 since normal/color are different). }
200 property Indexes: TGeometryIndexList read FIndexes write FIndexes;
201
202 (*Information about Indexes.
203
204 Before using FreeData, you could as well use the @link(Indexes) property
205 to get the same information. You can use Indexes[Index], Indexes <> nil,
206 Indexes.Count and such. However, FreeData call (that you should use
207 to conserve memory usage after loading arrays to VBO)
208 releases the Indexes property, while these properties stay the same.
209
210 IndexesPtr may be casted to "^TGeometryIndex", before the FreeData call.
211
212 @groupBegin *)
213 function IndexesPtr(const Index: Cardinal): PtrUInt;
214 property IndexesCount: Cardinal read FIndexesCount;
215 property HasIndexes: boolean read FHasIndexes;
216 { @groupEnd }
217
218 property Primitive: TGeometryPrimitive read FPrimitive write FPrimitive;
219
220 { If this is assigned, then the vertexes are divided into groups.
221 This is the only way to put many triangle strips, triangle fans and such
222 into one TGeometryArrays instance. For normal sets of triangles and quads
223 this has no use, as there's never a need to divide them for rendering.
224
225 Each value of this list specifies to take consecutive number of vertexes
226 for next primitive.
227 If Indexes are assigned, then they are divided into groups.
228 Otherwise, the other arrays (positions, normals etc.)
229 are divided into groups.
230
231 The sum of values must be equal to the Indexes.Count
232 (if Indexes assigned) or arrays Count (if Indexes not assigned). }
233 property Counts: TCardinalList read FCounts write FCounts;
234
235 { Memory containing vertex positions, normals and optionally tangents,
236 that is everything that changes during Coordinate.coord animation.
237 CoordinateSize is size, in bytes, of one item of this array.
238 @groupBegin }
239 property CoordinateArray: Pointer read FCoordinateArray;
240 property CoordinateSize: Cardinal read FCoordinateSize;
241 { @groupEnd }
242
243 { Does the order of data in CoordinateArray preserves the order
244 of geometry (order of vectors in TCoordinateNode.Coord
245 in TAbstractGeometryNode.CoordField). }
246 property CoordinatePreserveGeometryOrder: Boolean
247 read FCoordinatePreserveGeometryOrder
248 write FCoordinatePreserveGeometryOrder default false;
249
250 { Memory containing everything other vertex attribute, like color,
251 texture coordinates and GLSL attributes.
252 AttributeSize is size, in bytes, of one item of this array.
253 @groupBegin }
254 property AttributeArray: Pointer read FAttributeArray;
255 property AttributeSize: Cardinal read FAttributeSize;
256 { @groupEnd }
257
258 function Position: PVector3; overload;
259 function Position(const Index: Cardinal): PVector3; overload;
260
261 { Allocated number of items in vertex positions, normals, colors
262 and such arrays.
263
264 You can only set this once.
265 You must do all necessary AddColor / AddAttribute calls before setting this.
266
267 You can access all Position / Normal etc. pointers only after setting this.
268 Also, IndexesCount and HasIndexes is stored at this point. }
269 property Count: Integer read FCount write SetCount;
270
271 function Normal: PVector3; overload;
272 function Normal(const Index: Cardinal): PVector3; overload;
273
274 { Add tangent information per-vertex to CoordinateArray. }
275 procedure AddTangent;
276 { Was AddTangent used. }
277 property HasTangent: Boolean read FHasTangent;
278 { Access tangent information per-vertex. }
279 function Tangent(const Index: Cardinal = 0): PVector3;
280
281 { Add color-per-vertex attribute.
282 @param AMode Rendering mode, determines what shader does.
283 @param WithAlpha If @true, we have RGBA colors (TVector4), otherwise we have RGB colors (TVector3).
284 }
285 procedure AddColor(const AMode: TColorMode; const WithAlpha: Boolean);
286 { Access attribute created by AddColor(..., false) }
287 function ColorRgb(const Index: Cardinal = 0): PVector3;
288 { Access attribute created by AddColor(..., true) }
289 function ColorRgbAlpha(const Index: Cardinal = 0): PVector4;
290 property ColorType: TColorPerVertexType read FColorType;
291 property ColorMode: TColorMode read FColorMode;
292
293 { When ForceUnlit, the shape must be rendered like with UnlitMaterial,
294 with UnlitMaterial.emissiveColor/alpha = ForcedUnlitColor.
295
296 Note that (as with UnlitMaterial) the colors may be overridden
297 per-vertex using Color array (X3D Color/ColorRGBA nodes).
298 @groupBegin }
299 property ForceUnlit: boolean read FForceUnlit write FForceUnlit default false;
300 property ForcedUnlitColor: TVector4 read FForcedUnlitColor write FForcedUnlitColor;
301 { @groupEnd }
302
303 procedure AddFogCoord;
304 function FogCoord(const Index: Cardinal = 0): PSingle;
305 property HasFogCoord: boolean read FHasFogCoord;
306
307 { If FogCoord present, does it specify direct fog intensities,
308 that should be used to change pixel colors without any further processing.
309 When this is @false, then fog coordinates are understood
310 as distance from the eye, and they are processed by linear/exp equations
311 before being used to blend pixel colors. }
312 property FogDirectValues: boolean
313 read FFogDirectValues write FFogDirectValues default false;
314
315 { Allocated in AttributeArray texture coords.
316 Index is texture unit (counted from renderer first available texture
317 unit). If given item is @nil on this list, then this texture unit
318 is not allocated (just like it would be outside of TexCoords.Count). }
319 property TexCoords: TGeometryTexCoordList read FTexCoords;
320
321 procedure AddTexCoord2D(const TextureUnit: Cardinal);
322 procedure AddTexCoord3D(const TextureUnit: Cardinal);
323 procedure AddTexCoord4D(const TextureUnit: Cardinal);
324 { Add generated texture coord.
325 Such texture coord will not have actual data allocated in the array
326 (you're expected to instead setup and enable glTexGen when rendering).
327 Generation passed here must not be tgExplicit. }
328 procedure AddTexCoordGenerated(const Generation: TTextureCoordinateGeneration;
329 const TextureUnit: Cardinal);
330 { Add texture coord, with configuration copied from existing texture coord. }
331 procedure AddTexCoordCopy(const NewTextureUnit, ExistingTextureUnit: Cardinal);
332
333 function TexCoord(const TextureUnit: Cardinal; const Index: Cardinal = 0): Pointer;
334 function TexCoord2D(const TextureUnit: Cardinal; const Index: Cardinal = 0): PVector2;
335 function TexCoord3D(const TextureUnit: Cardinal; const Index: Cardinal = 0): PVector3;
336 function TexCoord4D(const TextureUnit: Cardinal; const Index: Cardinal = 0): PVector4;
337
338 property Attribs: TGeometryAttribList read FAttribs;
339
340 function AddGLSLAttributeFloat(const Name: string; const Internal: boolean): TGeometryAttrib;
341 function AddGLSLAttributeVector2(const Name: string; const Internal: boolean): TGeometryAttrib;
342 function AddGLSLAttributeVector3(const Name: string; const Internal: boolean): TGeometryAttrib;
343 function AddGLSLAttributeVector4(const Name: string; const Internal: boolean): TGeometryAttrib;
344 function AddGLSLAttributeMatrix3(const Name: string; const Internal: boolean): TGeometryAttrib;
345 function AddGLSLAttributeMatrix4(const Name: string; const Internal: boolean): TGeometryAttrib;
346
347 function GLSLAttribute(const A: TGeometryAttrib; const Offset: PtrUInt = 0): PtrUInt;
348
349 function GLSLAttributeFloat(const Name: string; const Index: Cardinal = 0): PSingle; overload; deprecated 'use GLSLAttributeFloat with TGeometryAttrib parameter, it is faster in the usual case';
350 function GLSLAttributeVector2(const Name: string; const Index: Cardinal = 0): PVector2; overload; deprecated 'use GLSLAttributeVector2 with TGeometryAttrib parameter, it is faster in the usual case';
351 function GLSLAttributeVector3(const Name: string; const Index: Cardinal = 0): PVector3; overload; deprecated 'use GLSLAttributeVector3 with TGeometryAttrib parameter, it is faster in the usual case';
352 function GLSLAttributeVector4(const Name: string; const Index: Cardinal = 0): PVector4; overload; deprecated 'use GLSLAttributeVector4 with TGeometryAttrib parameter, it is faster in the usual case';
353 function GLSLAttributeMatrix3(const Name: string; const Index: Cardinal = 0): PMatrix3; overload; deprecated 'use GLSLAttributeMatrix3 with TGeometryAttrib parameter, it is faster in the usual case';
354 function GLSLAttributeMatrix4(const Name: string; const Index: Cardinal = 0): PMatrix4; overload; deprecated 'use GLSLAttributeMatrix4 with TGeometryAttrib parameter, it is faster in the usual case';
355
356 function GLSLAttributeFloat(const A: TGeometryAttrib; const Index: Cardinal = 0): PSingle; overload;
357 function GLSLAttributeVector2(const A: TGeometryAttrib; const Index: Cardinal = 0): PVector2; overload;
358 function GLSLAttributeVector3(const A: TGeometryAttrib; const Index: Cardinal = 0): PVector3; overload;
359 function GLSLAttributeVector4(const A: TGeometryAttrib; const Index: Cardinal = 0): PVector4; overload;
360 function GLSLAttributeMatrix3(const A: TGeometryAttrib; const Index: Cardinal = 0): PMatrix3; overload;
361 function GLSLAttributeMatrix4(const A: TGeometryAttrib; const Index: Cardinal = 0): PMatrix4; overload;
362
363 { Should we use backface-culling (ignore some faces during rendering).
364
365 Which faces are "back" (and will be culled) is determined by FrontFaceCcw.
366 When FrontFaceCcw = @true, the the faces ordered counter-clockwise are front,
367 and thus the faces ordered clockwise will be culled.
368 When FrontFaceCcw = @false, the faces ordered counter-clockwise
369 will be culled. }
370 property CullFace: boolean
371 read FCullFace write FCullFace default false;
372
373 { Which faces are front, for backface-culling (see @link(CullFace))
374 and for normals data (see @link(Normal)). }
375 property FrontFaceCcw: boolean
376 read FFrontFaceCcw write FFrontFaceCcw default false;
377
378 { Release the allocated memory for arrays (CoordinateArray, AttributeArray,
379 Indexes). Further calls to IndexesPtr, Normal, Color and such will
380 return only an offset relative to the original arrays pointer.
381 This is necessary if you loaded arrays data into GPU memory
382 (like Vertex Buffer Object of OpenGL), and it is also optimal
383 -- you should not need the data anymore, once loaded to VBO. }
384 procedure FreeData;
385
386 { Was FreeData called. }
387 property DataFreed: boolean read FDataFreed;
388
389 { Information about faces. Generated for some geometry types.
390 Generated only when TArraysGenerator.FacesNeeded is @true.
391 Generated only for indexed shapes. When Indexes <> nil,
392 these have the same count as Indexes.Count. Otherwise these
393 have the same count as our @link(Count). }
394 property Faces: TFaceIndexesList read FFaces write FFaces;
395 end;
396
397 implementation
398
399 uses SysUtils, CastleStringUtils;
400
401 { TGeometryAttribList ------------------------------------------------------- }
402
403 function TGeometryAttribList.Find(const Name: string): TGeometryAttrib;
404 var
405 I: Integer;
406 begin
407 for I := 0 to Count - 1 do
408 if Items[I].Name = Name then
409 Exit(Items[I]);
410
411 Result := nil;
412 end;
413
414 { TGeometryArrays ------------------------------------------------------------ }
415
416 constructor TGeometryArrays.Create;
417 begin
418 inherited;
419 FCoordinateSize := SizeOf(TVector3) * 2;
420 FAttributeSize := 0;
421 FTexCoords := TGeometryTexCoordList.Create;
422 FAttribs := TGeometryAttribList.Create;
423 end;
424
425 destructor TGeometryArrays.Destroy;
426 begin
427 FreeAndNil(FIndexes);
428 FreeAndNil(FTexCoords);
429 FreeAndNil(FAttribs);
430 FreeAndNil(FCounts);
431 FreeMemNiling(FCoordinateArray);
432 FreeMemNiling(FAttributeArray);
433 FreeAndNil(FFaces);
434 inherited;
435 end;
436
437 procedure TGeometryArrays.SetCount(const Value: Integer);
438 begin
439 if FCount <> Value then
440 begin
441 FCount := Value;
442 ReallocMem(FCoordinateArray, CoordinateSize * Value);
443 ReallocMem(FAttributeArray, AttributeSize * Value);
444
445 { calculate FHasIndexes, FIndexesCount now }
446 FHasIndexes := Indexes <> nil;
447 if FHasIndexes then
448 FIndexesCount := Indexes.Count else
449 FIndexesCount := 0;
450 end;
451 end;
452
453 function TGeometryArrays.IndexesPtr(const Index: Cardinal): PtrUInt;
454 begin
455 Result := Index * SizeOf(TGeometryIndex);
456 if not DataFreed then
457 PtrUInt(Result) := PtrUInt(Result) + PtrUInt(FIndexes.L);
458 end;
459
460 function TGeometryArrays.Position: PVector3;
461 begin
462 { When DataFreed, FCoordinateArray is already nil }
463 Result := FCoordinateArray;
464 end;
465
466 function TGeometryArrays.Position(const Index: Cardinal): PVector3;
467 begin
468 { When DataFreed, FCoordinateArray is already nil }
469 Result := PVector3(PtrUInt(FCoordinateArray) + CoordinateSize * Index);
470 end;
471
472 function TGeometryArrays.Normal: PVector3;
473 begin
474 { When DataFreed, FCoordinateArray is already nil }
475 Result := PVector3(PtrUInt(PtrUInt(FCoordinateArray) +
476 SizeOf(TVector3)));
477 end;
478
479 function TGeometryArrays.Normal(const Index: Cardinal): PVector3;
480 begin
481 { When DataFreed, FCoordinateArray is already nil }
482 Result := PVector3(PtrUInt(PtrUInt(FCoordinateArray) +
483 SizeOf(TVector3) + CoordinateSize * Index));
484 end;
485
486 procedure TGeometryArrays.AddTangent;
487 begin
488 if not FHasTangent then
489 begin
490 FHasTangent := true;
491 FTangentOffset := FCoordinateSize;
492 FCoordinateSize := FCoordinateSize + SizeOf(TVector3);
493 end;
494 end;
495
496 function TGeometryArrays.Tangent(const Index: Cardinal = 0): PVector3;
497 begin
498 { When DataFreed, FCoordinateArray is already nil }
499 Result := PVector3(PtrUInt(PtrUInt(FCoordinateArray) +
500 FTangentOffset + CoordinateSize * Index));
501 end;
502
503 procedure TGeometryArrays.AddColor(const AMode: TColorMode; const WithAlpha: Boolean);
504 begin
505 Assert(FColorType = ctNone, 'Color attribute already added');
506
507 FColorMode := AMode;
508 ColorOffset := AttributeSize;
509 if WithAlpha then
510 begin
511 FColorType := ctRgbAlpha;
512 FAttributeSize := FAttributeSize + SizeOf(TVector4);
513 end else
514 begin
515 FColorType := ctRgb;
516 FAttributeSize := FAttributeSize + SizeOf(TVector3);
517 end;
518 end;
519
520 function TGeometryArrays.ColorRgbAlpha(const Index: Cardinal): PVector4;
521 begin
522 Assert(FColorType = ctRgbAlpha);
523
524 { When DataFreed, FAttributeArray is already nil }
525 Result := PVector4(PtrUInt(PtrUInt(FAttributeArray) +
526 ColorOffset + Index * AttributeSize));
527 end;
528
529 function TGeometryArrays.ColorRgb(const Index: Cardinal): PVector3;
530 begin
531 Assert(FColorType = ctRgb);
532
533 { When DataFreed, FAttributeArray is already nil }
534 Result := PVector3(PtrUInt(PtrUInt(FAttributeArray) +
535 ColorOffset + Index * AttributeSize));
536 end;
537
538 procedure TGeometryArrays.AddFogCoord;
539 begin
540 if not HasFogCoord then
541 begin
542 FHasFogCoord := true;
543 FogCoordOffset := AttributeSize;
544 FAttributeSize := FAttributeSize + SizeOf(Single);
545 end;
546 end;
547
548 function TGeometryArrays.FogCoord(const Index: Cardinal = 0): PSingle;
549 begin
550 if HasFogCoord then
551 { When DataFreed, FAttributeArray is already nil }
552 Result := PSingle(PtrUInt(PtrUInt(FAttributeArray) +
553 FogCoordOffset + Index * AttributeSize)) else
554 Result := nil;
555 end;
556
557 procedure TGeometryArrays.AddTexCoord(
558 const Generation: TTextureCoordinateGeneration;
559 const Dimensions: TTexCoordDimensions;
560 const TextureUnit: Cardinal);
561 var
562 OldCount, I: Integer;
563 begin
564 if TextureUnit >= TexCoords.Count then
565 begin
566 OldCount := TexCoords.Count;
567 TexCoords.Count := TextureUnit + 1;
568 for I := OldCount to TexCoords.Count - 1 do
569 TexCoords[I] := nil; { make sure new items are nil }
570 end;
571
572 Assert(TextureUnit < TexCoords.Count);
573
574 if TexCoords[TextureUnit] = nil then
575 begin
576 TexCoords[TextureUnit] := TGeometryTexCoord.Create;
577 TexCoords[TextureUnit].Generation := Generation;
578 TexCoords[TextureUnit].Dimensions := Dimensions;
579 if Generation = tgExplicit then
580 begin
581 TexCoords[TextureUnit].Offset := AttributeSize;
582 FAttributeSize := FAttributeSize + SizeOf(Single) * Dimensions;
583 end;
584 end else
585 if TexCoords[TextureUnit].Dimensions <> Dimensions then
586 begin
587 raise Exception.CreateFmt('Texture unit %d is already allocated but for %-dimensional tex coords (while %d requested)',
588 [TextureUnit, TexCoords[TextureUnit].Dimensions, Dimensions]);
589 end else
590 if TexCoords[TextureUnit].Generation <> Generation then
591 begin
592 raise Exception.CreateFmt('Texture unit %d is already allocated but for different tex coords generation method',
593 [TextureUnit]);
594 end
595 end;
596
597 procedure TGeometryArrays.AddTexCoordGenerated(const Generation: TTextureCoordinateGeneration;
598 const TextureUnit: Cardinal);
599 begin
600 Assert(Generation <> tgExplicit);
601 AddTexCoord(Generation, 2 { doesn't matter }, TextureUnit);
602 end;
603
604 procedure TGeometryArrays.AddTexCoord2D(const TextureUnit: Cardinal);
605 begin
606 AddTexCoord(tgExplicit, 2, TextureUnit);
607 end;
608
609 procedure TGeometryArrays.AddTexCoord3D(const TextureUnit: Cardinal);
610 begin
611 AddTexCoord(tgExplicit, 3, TextureUnit);
612 end;
613
614 procedure TGeometryArrays.AddTexCoord4D(const TextureUnit: Cardinal);
615 begin
616 AddTexCoord(tgExplicit, 4, TextureUnit);
617 end;
618
619 procedure TGeometryArrays.AddTexCoordCopy(
620 const NewTextureUnit, ExistingTextureUnit: Cardinal);
621 begin
622 if TexCoords[ExistingTextureUnit].Generation <> tgExplicit then
623 begin
624 AddTexCoordGenerated(TexCoords[ExistingTextureUnit].Generation, NewTextureUnit);
625 TexCoords[NewTextureUnit].GenerationBoundsVector := TexCoords[ExistingTextureUnit].GenerationBoundsVector;
626 TexCoords[NewTextureUnit].GenerationProjectorMatrix := TexCoords[ExistingTextureUnit].GenerationProjectorMatrix;
627 end else
628 case TexCoords[ExistingTextureUnit].Dimensions of
629 2: AddTexCoord2D(NewTextureUnit);
630 3: AddTexCoord3D(NewTextureUnit);
631 4: AddTexCoord4D(NewTextureUnit);
632 {$ifndef COMPILER_CASE_ANALYSIS}
633 else raise EInternalError.Create('TexCoords[ExistingTextureUnit].Dimensions?');
634 {$endif}
635 end;
636 end;
637
638 function TGeometryArrays.TexCoord(const TextureUnit, Index: Cardinal): Pointer;
639 begin
640 if (TextureUnit < TexCoords.Count) and
641 (TexCoords[TextureUnit] <> nil) then
642 begin
643 Assert(TexCoords[TextureUnit].Generation = tgExplicit, 'Texture coords are generated, not explicit, for this unit');
644 { When DataFreed, FAttributeArray is already nil }
645 Result := Pointer(PtrUInt(PtrUInt(FAttributeArray) +
646 TexCoords[TextureUnit].Offset + Index * AttributeSize));
647 end else
648 Result := nil;
649 end;
650
651 function TGeometryArrays.TexCoord2D(const TextureUnit, Index: Cardinal): PVector2;
652 begin
653 Assert(TexCoords[TextureUnit].Dimensions = 2, 'Texture coord allocated but for different dimensions');
654 Result := PVector2(TexCoord(TextureUnit, Index));
655 end;
656
657 function TGeometryArrays.TexCoord3D(const TextureUnit, Index: Cardinal): PVector3;
658 begin
659 Assert(TexCoords[TextureUnit].Dimensions = 3, 'Texture coord allocated but for different dimensions');
660 Result := PVector3(TexCoord(TextureUnit, Index));
661 end;
662
663 function TGeometryArrays.TexCoord4D(const TextureUnit, Index: Cardinal): PVector4;
664 begin
665 Assert(TexCoords[TextureUnit].Dimensions = 4, 'Texture coord allocated but for different dimensions');
666 Result := PVector4(TexCoord(TextureUnit, Index));
667 end;
668
669 const
670 AttribTypeName: array[TGeometryAttribType] of string =
671 ( 'float', 'vec2', 'vec3', 'vec4', 'mat3', 'mat4' );
672
673 function TGeometryArrays.AddGLSLAttribute(const AType: TGeometryAttribType;
674 const Name: string; const Internal: boolean): TGeometryAttrib;
675 const
676 AttribSizes: array[TGeometryAttribType] of Cardinal =
677 ( SizeOf(Single),
678 SizeOf(TVector2),
679 SizeOf(TVector3),
680 SizeOf(TVector4),
681 SizeOf(TMatrix3),
682 SizeOf(TMatrix4)
683 );
684 begin
685 Result := Attribs.Find(Name);
686 if Result <> nil then
687 begin
688 if Result.AType <> AType then
689 raise Exception.CreateFmt('GLSL attribute "%s" is already allocated but for different type (%s) than currently requested (%s)', [
690 Name,
691 AttribTypeName[Result.AType],
692 AttribTypeName[AType]
693 ]);
694 if Result.Internal <> Internal then
695 raise Exception.CreateFmt('GLSL attribute "%s" is already allocated but for different internal (%s) than currently requested (%s)', [
696 Name,
697 BoolToStr(Result.Internal, true),
698 BoolToStr(Internal, true)
699 ]);
700 end else
701 begin
702 Result := TGeometryAttrib.Create;
703 Result.Name := Name;
704 Result.AType := AType;
705 Result.Offset := AttributeSize;
706 Result.Internal := Internal;
707 FAttributeSize := FAttributeSize + AttribSizes[AType];
708
709 Attribs.Add(Result);
710 end;
711 end;
712
713 function TGeometryArrays.AddGLSLAttributeFloat(const Name: string; const Internal: boolean): TGeometryAttrib;
714 begin
715 Result := AddGLSLAttribute(atFloat, Name, Internal);
716 end;
717
718 function TGeometryArrays.AddGLSLAttributeVector2(const Name: string; const Internal: boolean): TGeometryAttrib;
719 begin
720 Result := AddGLSLAttribute(atVector2, Name, Internal);
721 end;
722
723 function TGeometryArrays.AddGLSLAttributeVector3(const Name: string; const Internal: boolean): TGeometryAttrib;
724 begin
725 Result := AddGLSLAttribute(atVector3, Name, Internal);
726 end;
727
728 function TGeometryArrays.AddGLSLAttributeVector4(const Name: string; const Internal: boolean): TGeometryAttrib;
729 begin
730 Result := AddGLSLAttribute(atVector4, Name, Internal);
731 end;
732
733 function TGeometryArrays.AddGLSLAttributeMatrix3(const Name: string; const Internal: boolean): TGeometryAttrib;
734 begin
735 Result := AddGLSLAttribute(atMatrix3, Name, Internal);
736 end;
737
738 function TGeometryArrays.AddGLSLAttributeMatrix4(const Name: string; const Internal: boolean): TGeometryAttrib;
739 begin
740 Result := AddGLSLAttribute(atMatrix4, Name, Internal);
741 end;
742
743 function TGeometryArrays.GLSLAttributeIndex(const AType: TGeometryAttribType;
744 const A: TGeometryAttrib; const Index: Cardinal): PtrUInt;
745 begin
746 Assert(A.AType = AType, Format('GLSL attribute "%s" is allocated but for different type (%s) than currently requested (%s)', [
747 A.Name,
748 AttribTypeName[A.AType],
749 AttribTypeName[AType]
750 ]));
751
752 { When DataFreed, FAttributeArray is already nil }
753 Result := PtrUInt(FAttributeArray) + A.Offset + Index * AttributeSize;
754 end;
755
756 function TGeometryArrays.GLSLAttributeIndex(const AType: TGeometryAttribType;
757 const Name: string; const Index: Cardinal): PtrUInt;
758 var
759 A: TGeometryAttrib;
760 begin
761 A := Attribs.Find(Name);
762
763 if A <> nil then
764 Result := GLSLAttributeIndex(AType, A, Index)
765 else
766 raise Exception.CreateFmt('GLSL attribute "%s" is not allocated', [Name]);
767 end;
768
769 function TGeometryArrays.GLSLAttribute(const A: TGeometryAttrib; const Offset: PtrUInt): PtrUInt;
770 begin
771 { When DataFreed, FAttributeArray is already nil }
772 Result := PtrUInt(FAttributeArray) + A.Offset + Offset;
773 end;
774
775 function TGeometryArrays.GLSLAttributeFloat(const Name: string; const Index: Cardinal = 0): PSingle;
776 begin
777 Result := PSingle(GLSLAttributeIndex(atFloat, Name, Index));
778 end;
779
780 function TGeometryArrays.GLSLAttributeVector2(const Name: string; const Index: Cardinal = 0): PVector2;
781 begin
782 Result := PVector2(GLSLAttributeIndex(atVector2, Name, Index));
783 end;
784
785 function TGeometryArrays.GLSLAttributeVector3(const Name: string; const Index: Cardinal = 0): PVector3;
786 begin
787 Result := PVector3(GLSLAttributeIndex(atVector3, Name, Index));
788 end;
789
790 function TGeometryArrays.GLSLAttributeVector4(const Name: string; const Index: Cardinal = 0): PVector4;
791 begin
792 Result := PVector4(GLSLAttributeIndex(atVector4, Name, Index));
793 end;
794
795 function TGeometryArrays.GLSLAttributeMatrix3(const Name: string; const Index: Cardinal = 0): PMatrix3;
796 begin
797 Result := PMatrix3(GLSLAttributeIndex(atMatrix3, Name, Index));
798 end;
799
800 function TGeometryArrays.GLSLAttributeMatrix4(const Name: string; const Index: Cardinal = 0): PMatrix4;
801 begin
802 Result := PMatrix4(GLSLAttributeIndex(atMatrix4, Name, Index));
803 end;
804
805 function TGeometryArrays.GLSLAttributeFloat(const A: TGeometryAttrib; const Index: Cardinal = 0): PSingle;
806 begin
807 Result := PSingle(GLSLAttributeIndex(atFloat, A, Index));
808 end;
809
810 function TGeometryArrays.GLSLAttributeVector2(const A: TGeometryAttrib; const Index: Cardinal = 0): PVector2;
811 begin
812 Result := PVector2(GLSLAttributeIndex(atVector2, A, Index));
813 end;
814
815 function TGeometryArrays.GLSLAttributeVector3(const A: TGeometryAttrib; const Index: Cardinal = 0): PVector3;
816 begin
817 Result := PVector3(GLSLAttributeIndex(atVector3, A, Index));
818 end;
819
820 function TGeometryArrays.GLSLAttributeVector4(const A: TGeometryAttrib; const Index: Cardinal = 0): PVector4;
821 begin
822 Result := PVector4(GLSLAttributeIndex(atVector4, A, Index));
823 end;
824
825 function TGeometryArrays.GLSLAttributeMatrix3(const A: TGeometryAttrib; const Index: Cardinal = 0): PMatrix3;
826 begin
827 Result := PMatrix3(GLSLAttributeIndex(atMatrix3, A, Index));
828 end;
829
830 function TGeometryArrays.GLSLAttributeMatrix4(const A: TGeometryAttrib; const Index: Cardinal = 0): PMatrix4;
831 begin
832 Result := PMatrix4(GLSLAttributeIndex(atMatrix4, A, Index));
833 end;
834
835 procedure TGeometryArrays.FreeData;
836 begin
837 FDataFreed := true;
838 FreeAndNil(FIndexes);
839 FreeMemNiling(FCoordinateArray);
840 FreeMemNiling(FAttributeArray);
841 FreeAndNil(FFaces);
842 end;
843
844 end.