| 1 |
{ |
| 2 |
Copyright 2008-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 |
{$ifdef read_interface} |
| 17 |
|
| 18 |
type |
| 19 |
{ Collada <source>. This is a named container keeping a series of floats |
| 20 |
in Collada. It may contain only data for some specific purpose (like positions |
| 21 |
or normals or tex coords) or be interleaved with other data. } |
| 22 |
TColladaSource = class |
| 23 |
Name: string; |
| 24 |
Floats: TSingleList; |
| 25 |
Params: TCastleStringList; |
| 26 |
{ Collada <accessor> description of vectors inside Floats. |
| 27 |
Note that Count here refers to count of whole vectors (so it's usually |
| 28 |
< than Floats.Count). } |
| 29 |
Count, Stride, Offset: Integer; |
| 30 |
|
| 31 |
constructor Create; |
| 32 |
destructor Destroy; override; |
| 33 |
|
| 34 |
{ Check components and counts before AssignToVectorXYZ. |
| 35 |
It @false, a warning is done, and some parameters are adjusted. |
| 36 |
You can then abort using this vector |
| 37 |
(if it can be reasonably ignored, e.g. for normals), |
| 38 |
or try AssignToVectorXYZ anyway |
| 39 |
(if it's necessary, e.g. when this is for vertex positions). } |
| 40 |
function CheckXYZ(out XIndex, YIndex, ZIndex: Integer): boolean; |
| 41 |
function CheckST(out SIndex, TIndex: Integer): boolean; |
| 42 |
|
| 43 |
{ Extract from source an array of TVector3. |
| 44 |
Params will be checked to contain at least three vector components |
| 45 |
'X', 'Y', 'Z'. (These should be specified in <param name="..." type="float"/>). |
| 46 |
|
| 47 |
Order of X, Y, Z params (and thus order of components in our Floats) |
| 48 |
may be different, we will reorder them to XYZ anyway. |
| 49 |
This way Collada data may have XYZ or ST in a different order, |
| 50 |
and this method will reorder this suitable for X3D. |
| 51 |
|
| 52 |
Returns if components were fully correct (no warnings). } |
| 53 |
function AssignToVectorXYZ(const Value: TVector3List): boolean; |
| 54 |
|
| 55 |
{ Extract from source an array of TVector2, for texture coordinates. |
| 56 |
Like AssignToVectorXYZ, but for 2D vectors, with component names 'S' and 'T' |
| 57 |
(or 'U' and 'V'). } |
| 58 |
function AssignToVectorST(const Value: TVector2List): boolean; |
| 59 |
end; |
| 60 |
|
| 61 |
TColladaSourceList = class({$ifdef FPC}specialize{$endif} TObjectList<TColladaSource>) |
| 62 |
public |
| 63 |
{ Find a TColladaSource with given Name, @nil if not found. } |
| 64 |
function Find(const Name: string): TColladaSource; |
| 65 |
end; |
| 66 |
|
| 67 |
{$endif read_interface} |
| 68 |
|
| 69 |
{$ifdef read_implementation} |
| 70 |
|
| 71 |
constructor TColladaSource.Create; |
| 72 |
begin |
| 73 |
inherited; |
| 74 |
Params := TCastleStringList.Create; |
| 75 |
Floats := TSingleList.Create; |
| 76 |
end; |
| 77 |
|
| 78 |
destructor TColladaSource.Destroy; |
| 79 |
begin |
| 80 |
FreeAndNil(Params); |
| 81 |
FreeAndNil(Floats); |
| 82 |
inherited; |
| 83 |
end; |
| 84 |
|
| 85 |
function TColladaSource.CheckXYZ(out XIndex, YIndex, ZIndex: Integer): boolean; |
| 86 |
var |
| 87 |
MinCount: Integer; |
| 88 |
begin |
| 89 |
XIndex := Params.IndexOf('X'); |
| 90 |
if XIndex = -1 then |
| 91 |
begin |
| 92 |
WritelnWarning('Collada', 'Missing "X" parameter (for 3D vector) in this <source>'); |
| 93 |
Exit(false); |
| 94 |
end; |
| 95 |
|
| 96 |
YIndex := Params.IndexOf('Y'); |
| 97 |
if YIndex = -1 then |
| 98 |
begin |
| 99 |
WritelnWarning('Collada', 'Missing "Y" parameter (for 3D vector) in this <source>'); |
| 100 |
Exit(false); |
| 101 |
end; |
| 102 |
|
| 103 |
ZIndex := Params.IndexOf('Z'); |
| 104 |
if ZIndex = -1 then |
| 105 |
begin |
| 106 |
WritelnWarning('Collada', 'Missing "Z" parameter (for 3D vector) in this <source>'); |
| 107 |
Exit(false); |
| 108 |
end; |
| 109 |
|
| 110 |
MinCount := Offset + Stride * (Count - 1) + |
| 111 |
{$ifndef FPC}MaxIntValue{$else}MaxIntValue{$endif}([XIndex, YIndex, ZIndex]) + 1; |
| 112 |
Result := Floats.Count >= MinCount; |
| 113 |
if not Result then |
| 114 |
begin |
| 115 |
WritelnWarning('Collada', Format('<accessor> count requires at least %d float values in <float_array>, but only %d are available', |
| 116 |
[MinCount, Floats.Count])); |
| 117 |
{ force Count smaller, also force other params to common values } |
| 118 |
Stride := Max(Stride, 1); |
| 119 |
XIndex := 0; |
| 120 |
YIndex := 1; |
| 121 |
ZIndex := 2; |
| 122 |
Count := (Floats.Count - Offset) div Stride; |
| 123 |
end; |
| 124 |
end; |
| 125 |
|
| 126 |
function TColladaSource.AssignToVectorXYZ(const Value: TVector3List): boolean; |
| 127 |
var |
| 128 |
XIndex, YIndex, ZIndex, I: Integer; |
| 129 |
begin |
| 130 |
Result := CheckXYZ(XIndex, YIndex, ZIndex); |
| 131 |
Value.Count := Count; |
| 132 |
for I := 0 to Count - 1 do |
| 133 |
begin |
| 134 |
Value.L[I].X := Floats.L[Offset + Stride * I + XIndex]; |
| 135 |
Value.L[I].Y := Floats.L[Offset + Stride * I + YIndex]; |
| 136 |
Value.L[I].Z := Floats.L[Offset + Stride * I + ZIndex]; |
| 137 |
end; |
| 138 |
end; |
| 139 |
|
| 140 |
function TColladaSource.CheckST(out SIndex, TIndex: Integer): boolean; |
| 141 |
var |
| 142 |
MinCount: Integer; |
| 143 |
begin |
| 144 |
SIndex := Params.IndexOf('S'); |
| 145 |
if SIndex = -1 then |
| 146 |
begin |
| 147 |
SIndex := Params.IndexOf('U'); |
| 148 |
if SIndex = -1 then |
| 149 |
begin |
| 150 |
WritelnWarning('Collada', 'Missing "S" or "U" parameter (1st component of 2D tex coord) in this <source>'); |
| 151 |
Exit(false); |
| 152 |
end; |
| 153 |
end; |
| 154 |
|
| 155 |
TIndex := Params.IndexOf('T'); |
| 156 |
if TIndex = -1 then |
| 157 |
begin |
| 158 |
TIndex := Params.IndexOf('V'); |
| 159 |
if TIndex = -1 then |
| 160 |
begin |
| 161 |
WritelnWarning('Collada', 'Missing "T" or "V" parameter (2nd component of 2D tex coord) in this <source>'); |
| 162 |
Exit(false); |
| 163 |
end; |
| 164 |
end; |
| 165 |
|
| 166 |
MinCount := Offset + Stride * (Count - 1) + Max(SIndex, TIndex) + 1; |
| 167 |
Result := Floats.Count >= MinCount; |
| 168 |
if not Result then |
| 169 |
begin |
| 170 |
WritelnWarning('Collada', Format('<accessor> count requires at least %d float values in <float_array>, but only %d are available', |
| 171 |
[MinCount, Floats.Count])); |
| 172 |
{ force Count smaller, also force other params to common values } |
| 173 |
Stride := Max(Stride, 1); |
| 174 |
SIndex := 0; |
| 175 |
TIndex := 1; |
| 176 |
Count := (Floats.Count - Offset) div Stride; |
| 177 |
end; |
| 178 |
end; |
| 179 |
|
| 180 |
function TColladaSource.AssignToVectorST(const Value: TVector2List): boolean; |
| 181 |
var |
| 182 |
SIndex, TIndex, I: Integer; |
| 183 |
begin |
| 184 |
Result := CheckST(SIndex, TIndex); |
| 185 |
Value.Count := Count; |
| 186 |
for I := 0 to Count - 1 do |
| 187 |
begin |
| 188 |
Value.L[I].X := Floats.L[Offset + Stride * I + SIndex]; |
| 189 |
Value.L[I].Y := Floats.L[Offset + Stride * I + TIndex]; |
| 190 |
end; |
| 191 |
end; |
| 192 |
|
| 193 |
function TColladaSourceList.Find(const Name: string): TColladaSource; |
| 194 |
var |
| 195 |
I: Integer; |
| 196 |
begin |
| 197 |
for I := 0 to Count - 1 do |
| 198 |
if Items[I].Name = Name then |
| 199 |
Exit(Items[I]); |
| 200 |
Result := nil; |
| 201 |
end; |
| 202 |
|
| 203 |
{$endif read_implementation} |