| 1 |
{ |
| 2 |
Copyright 2008-2018 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 |
{$ifdef read_interface} |
| 17 |
|
| 18 |
type |
| 19 |
{ Indexes of a single vertex. Used only together with TColladaIndexes, |
| 20 |
that knows which ones are relevant (Coord is always relevant, |
| 21 |
but TexCoord is relevant only if TexCoordIndexOffset <> -1, |
| 22 |
and Normal is relevant only if NormalIndexOffset <> -1). } |
| 23 |
TIndex = record Coord, TexCoord, Normal: Integer end; |
| 24 |
|
| 25 |
{ Handling Collada <p> (indexes inside primitives) to fill X3D geometry indexes. |
| 26 |
Accepted geometry types are TIndexedFaceSetNode and TIndexedLineSetNode, |
| 27 |
as only such may be created for Collada primitives. } |
| 28 |
TColladaIndexes = class |
| 29 |
strict private |
| 30 |
Ints: TIntegersParser; |
| 31 |
FGeometry: TAbstractGeometryNode; |
| 32 |
FInputsCount, FCoordIndexOffset, FTexCoordIndexOffset, FNormalIndexOffset: Integer; |
| 33 |
public |
| 34 |
constructor Create(const Geometry: TAbstractGeometryNode; |
| 35 |
const InputsCount, CoordIndexOffset, TexCoordIndexOffset, NormalIndexOffset: Integer); |
| 36 |
destructor Destroy; override; |
| 37 |
procedure BeginElement(const PElement: TDOMElement); |
| 38 |
function ReadVertex(out Index: TIndex): boolean; |
| 39 |
function ReadAddVertex(out Index: TIndex): boolean; overload; |
| 40 |
function ReadAddVertex: boolean; overload; |
| 41 |
procedure AddVertex(const Index: TIndex); |
| 42 |
procedure PolygonEnd; |
| 43 |
end; |
| 44 |
|
| 45 |
{$endif read_interface} |
| 46 |
|
| 47 |
{$ifdef read_implementation} |
| 48 |
|
| 49 |
constructor TColladaIndexes.Create( |
| 50 |
const Geometry: TAbstractGeometryNode; |
| 51 |
const InputsCount, CoordIndexOffset, TexCoordIndexOffset, NormalIndexOffset: Integer); |
| 52 |
begin |
| 53 |
inherited Create; |
| 54 |
FGeometry := Geometry; |
| 55 |
FInputsCount := InputsCount; |
| 56 |
FCoordIndexOffset := CoordIndexOffset; |
| 57 |
FTexCoordIndexOffset := TexCoordIndexOffset; |
| 58 |
FNormalIndexOffset := NormalIndexOffset; |
| 59 |
end; |
| 60 |
|
| 61 |
procedure TColladaIndexes.BeginElement(const PElement: TDOMElement); |
| 62 |
begin |
| 63 |
FreeAndNil(Ints); |
| 64 |
Ints := TIntegersParser.Create(PElement); |
| 65 |
end; |
| 66 |
|
| 67 |
destructor TColladaIndexes.Destroy; |
| 68 |
begin |
| 69 |
FreeAndNil(Ints); |
| 70 |
inherited; |
| 71 |
end; |
| 72 |
|
| 73 |
const |
| 74 |
EmptyIndex: TIndex = (Coord: -1; TexCoord: -1; Normal: -1); |
| 75 |
|
| 76 |
function TColladaIndexes.ReadVertex(out Index: TIndex): boolean; |
| 77 |
var |
| 78 |
I: Integer; |
| 79 |
begin |
| 80 |
Index := EmptyIndex; |
| 81 |
|
| 82 |
for I := 0 to FInputsCount - 1 do |
| 83 |
begin |
| 84 |
if not Ints.GetNext then |
| 85 |
begin |
| 86 |
if I <> 0 then |
| 87 |
WritelnWarning('Collada', 'Indexes in <p> suddenly end, in the middle of a vertex'); |
| 88 |
Exit(false); |
| 89 |
end; |
| 90 |
|
| 91 |
if I = FCoordIndexOffset then |
| 92 |
Index.Coord := Ints.Current else |
| 93 |
if I = FTexCoordIndexOffset then |
| 94 |
Index.TexCoord := Ints.Current else |
| 95 |
if I = FNormalIndexOffset then |
| 96 |
Index.Normal := Ints.Current; |
| 97 |
end; |
| 98 |
|
| 99 |
Result := true; |
| 100 |
end; |
| 101 |
|
| 102 |
function TColladaIndexes.ReadAddVertex(out Index: TIndex): boolean; |
| 103 |
begin |
| 104 |
Result := ReadVertex(Index); |
| 105 |
if Result then |
| 106 |
AddVertex(Index); |
| 107 |
end; |
| 108 |
|
| 109 |
function TColladaIndexes.ReadAddVertex: boolean; |
| 110 |
var |
| 111 |
IgnoreIndex: TIndex; |
| 112 |
begin |
| 113 |
Result := ReadAddVertex(IgnoreIndex); |
| 114 |
end; |
| 115 |
|
| 116 |
procedure TColladaIndexes.AddVertex(const Index: TIndex); |
| 117 |
begin |
| 118 |
if FGeometry is TIndexedFaceSetNode then |
| 119 |
begin |
| 120 |
TIndexedFaceSetNode(FGeometry).FdCoordIndex.Items.Add(Index.Coord); |
| 121 |
if FTexCoordIndexOffset <> -1 then |
| 122 |
TIndexedFaceSetNode(FGeometry).FdTexCoordIndex.Items.Add(Index.TexCoord); |
| 123 |
if FNormalIndexOffset <> -1 then |
| 124 |
TIndexedFaceSetNode(FGeometry).FdNormalIndex.Items.Add(Index.Normal); |
| 125 |
end else |
| 126 |
begin |
| 127 |
Assert(FGeometry is TIndexedLineSetNode); |
| 128 |
TIndexedLineSetNode(FGeometry).FdCoordIndex.Items.Add(Index.Coord); |
| 129 |
end; |
| 130 |
end; |
| 131 |
|
| 132 |
procedure TColladaIndexes.PolygonEnd; |
| 133 |
begin |
| 134 |
AddVertex(EmptyIndex); |
| 135 |
end; |
| 136 |
|
| 137 |
{$endif read_implementation} |