| 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 |
TColladaGeometry = class |
| 20 |
{ Collada geometry id. } |
| 21 |
Name: string; |
| 22 |
Primitives: TColladaPrimitiveList; |
| 23 |
constructor Create; |
| 24 |
destructor Destroy; override; |
| 25 |
end; |
| 26 |
|
| 27 |
TColladaGeometryList = class ({$ifdef FPC}specialize{$endif} TObjectList<TColladaGeometry>) |
| 28 |
public |
| 29 |
{ Find item with given Name, @nil if not found. } |
| 30 |
function Find(const Name: string): TColladaGeometry; |
| 31 |
end; |
| 32 |
|
| 33 |
{$endif read_interface} |
| 34 |
|
| 35 |
{$ifdef read_implementation} |
| 36 |
|
| 37 |
constructor TColladaGeometry.Create; |
| 38 |
begin |
| 39 |
Primitives := TColladaPrimitiveList.Create; |
| 40 |
end; |
| 41 |
|
| 42 |
destructor TColladaGeometry.Destroy; |
| 43 |
begin |
| 44 |
FreeAndNil(Primitives); |
| 45 |
inherited; |
| 46 |
end; |
| 47 |
|
| 48 |
function TColladaGeometryList.Find(const Name: string): TColladaGeometry; |
| 49 |
var |
| 50 |
I: Integer; |
| 51 |
begin |
| 52 |
for I := 0 to Count - 1 do |
| 53 |
if Items[I].Name = Name then |
| 54 |
Exit(Items[I]); |
| 55 |
|
| 56 |
Result := nil; |
| 57 |
end; |
| 58 |
|
| 59 |
{$endif read_implementation} |
| 60 |
|
| 61 |
{$ifdef read_implementation_nested} |
| 62 |
|
| 63 |
var |
| 64 |
{ List of Collada geometries. } |
| 65 |
Geometries: TColladaGeometryList; |
| 66 |
|
| 67 |
{ Add Collada geometry instance (many X3D Shape nodes) to given X3D Group. } |
| 68 |
procedure AddGeometryInstance(Group: TAbstractGroupingNode; |
| 69 |
Geometry: TColladaGeometry; InstantiatingElement: TDOMElement); |
| 70 |
|
| 71 |
{ For Collada material id, return the X3D Appearance (or @nil if not found). } |
| 72 |
function MaterialToX3D(MaterialId: string; |
| 73 |
InstantiatingElement: TDOMElement): TAppearanceNode; |
| 74 |
var |
| 75 |
BindMaterial, Technique: TDOMElement; |
| 76 |
InstanceMaterialSymbol, InstanceMaterialTarget: string; |
| 77 |
I: TXMLElementIterator; |
| 78 |
Effect: TColladaEffect; |
| 79 |
begin |
| 80 |
if MaterialId = '' then Exit(nil); |
| 81 |
|
| 82 |
{ For InstantiatingElement = instance_geometry (Collada 1.4.x), this |
| 83 |
must be present. |
| 84 |
For InstantiatingElement = instance (Collada 1.3.x), this must not |
| 85 |
be present. |
| 86 |
(But we actually don't check these conditions, just handle any case.) } |
| 87 |
BindMaterial := InstantiatingElement.ChildElement('bind_material', false); |
| 88 |
if BindMaterial <> nil then |
| 89 |
begin |
| 90 |
Technique := BindMaterial.ChildElement('technique_common', false); |
| 91 |
if Technique <> nil then |
| 92 |
begin |
| 93 |
{ read <instance_material list inside. |
| 94 |
This may contain multiple materials, but actually we're only |
| 95 |
interested in a single material, so we look for material with |
| 96 |
symbol = MaterialId. } |
| 97 |
I := Technique.ChildrenIterator('instance_material'); |
| 98 |
try |
| 99 |
while I.GetNext do |
| 100 |
if I.Current.AttributeString('symbol', InstanceMaterialSymbol) and |
| 101 |
(InstanceMaterialSymbol = MaterialId) and |
| 102 |
I.Current.AttributeString('target', InstanceMaterialTarget) then |
| 103 |
begin |
| 104 |
{ this should be true, target is URL } |
| 105 |
if SCharIs(InstanceMaterialTarget, 1, '#') then |
| 106 |
Delete(InstanceMaterialTarget, 1, 1); |
| 107 |
|
| 108 |
{ replace MaterialId with what is indicated by |
| 109 |
<instance_material target="..."> } |
| 110 |
MaterialId := InstanceMaterialTarget; |
| 111 |
end; |
| 112 |
finally FreeAndNil(I) end; |
| 113 |
end; |
| 114 |
end; |
| 115 |
|
| 116 |
Materials.TryGetValue(MaterialId, Effect); // Effect will be nil if not found |
| 117 |
if Effect = nil then |
| 118 |
begin |
| 119 |
WritelnWarning('Collada', Format('Referencing non-existing material name "%s"', |
| 120 |
[MaterialId])); |
| 121 |
Result := nil; |
| 122 |
end else |
| 123 |
begin |
| 124 |
Result := Effect.Appearance; |
| 125 |
end; |
| 126 |
end; |
| 127 |
|
| 128 |
var |
| 129 |
Shape: TShapeNode; |
| 130 |
I: Integer; |
| 131 |
Primitive: TColladaPrimitive; |
| 132 |
begin |
| 133 |
for I := 0 to Geometry.Primitives.Count - 1 do |
| 134 |
begin |
| 135 |
Primitive := Geometry.Primitives[I]; |
| 136 |
Shape := TShapeNode.Create('', BaseUrl); |
| 137 |
Group.AddChildren(Shape); |
| 138 |
Shape.Geometry := Primitive.X3DGeometry; |
| 139 |
Shape.Appearance := MaterialToX3D(Primitive.Material, InstantiatingElement); |
| 140 |
end; |
| 141 |
end; |
| 142 |
|
| 143 |
{ Read <instance_geometry>, adding resulting X3D nodes into |
| 144 |
ParentGroup. Actually, this is also for reading <instance> in Collada 1.3.1. } |
| 145 |
procedure ReadInstanceGeometry(ParentGroup: TAbstractGroupingNode; |
| 146 |
InstantiatingElement: TDOMElement); |
| 147 |
var |
| 148 |
GeometryId: string; |
| 149 |
Geometry: TColladaGeometry; |
| 150 |
begin |
| 151 |
if InstantiatingElement.AttributeString('url', GeometryId) and |
| 152 |
SCharIs(GeometryId, 1, '#') then |
| 153 |
begin |
| 154 |
Delete(GeometryId, 1, 1); |
| 155 |
Geometry := Geometries.Find(GeometryId); |
| 156 |
if Geometry = nil then |
| 157 |
WritelnWarning('Collada', Format('<node> instantiates non-existing <geometry> element "%s"', |
| 158 |
[GeometryId])) else |
| 159 |
AddGeometryInstance(ParentGroup, Geometry, InstantiatingElement); |
| 160 |
end else |
| 161 |
WritelnWarning('Collada', Format('Element <%s> missing url attribute (that has to start with #)', |
| 162 |
[InstantiatingElement.TagName])); |
| 163 |
end; |
| 164 |
|
| 165 |
{ Read <geometry>. It is added to the Geometries list. } |
| 166 |
procedure ReadGeometry(GeometryElement: TDOMElement); |
| 167 |
var |
| 168 |
{ Collada Geometry constructed, available to all Read* primitives local procedures. } |
| 169 |
Geometry: TColladaGeometry; |
| 170 |
|
| 171 |
Sources: TColladaSourceList; |
| 172 |
|
| 173 |
{ Read "double sided" geometry property. |
| 174 |
|
| 175 |
There is no standard for this it seems, only some special MAYA markers: |
| 176 |
<extra><technique profile="MAYA"><double_sided>1</double_sided></technique></extra> |
| 177 |
Blender Collada 1.4 exporter, as well as various models from collada.org/owl |
| 178 |
use this, so it seems like a standard in practice. } |
| 179 |
function ReadDoubleSided(GeometryE: TDOMElement): boolean; |
| 180 |
var |
| 181 |
Child: TDOMElement; |
| 182 |
Profile: string; |
| 183 |
begin |
| 184 |
Result := false; |
| 185 |
|
| 186 |
Child := GeometryE.ChildElement('extra', false); |
| 187 |
if Child = nil then Exit; |
| 188 |
|
| 189 |
Child := Child.ChildElement('technique', false); |
| 190 |
if Child = nil then Exit; |
| 191 |
|
| 192 |
if not (Child.AttributeString('profile', Profile) and |
| 193 |
(Profile = 'MAYA')) then |
| 194 |
Exit; |
| 195 |
|
| 196 |
Child := Child.ChildElement('double_sided', false); |
| 197 |
if Child = nil then Exit; |
| 198 |
|
| 199 |
Result := StrToIntDef(Child.TextData, 0) <> 0; |
| 200 |
end; |
| 201 |
|
| 202 |
{ Read <source> within <mesh>. } |
| 203 |
procedure ReadSource(SourceElement: TDOMElement); |
| 204 |
var |
| 205 |
FloatArray, Technique, Accessor: TDOMElement; |
| 206 |
SeekPos, FloatArrayCount, I: Integer; |
| 207 |
FloatArrayContents, Token, AccessorSource, ParamName, ParamType, FloatArrayId: string; |
| 208 |
Source: TColladaSource; |
| 209 |
It: TXMLElementIterator; |
| 210 |
begin |
| 211 |
Source := TColladaSource.Create; |
| 212 |
Sources.Add(Source); |
| 213 |
|
| 214 |
if not SourceElement.AttributeString('id', Source.Name) then |
| 215 |
Source.Name := ''; |
| 216 |
|
| 217 |
FloatArray := SourceElement.ChildElement('float_array', false); |
| 218 |
if FloatArray <> nil then |
| 219 |
begin |
| 220 |
if not FloatArray.AttributeInteger('count', FloatArrayCount) then |
| 221 |
begin |
| 222 |
FloatArrayCount := 0; |
| 223 |
WritelnWarning('Collada', '<float_array> without a count attribute'); |
| 224 |
end; |
| 225 |
|
| 226 |
if not FloatArray.AttributeString('id', FloatArrayId) then |
| 227 |
FloatArrayId := ''; |
| 228 |
|
| 229 |
Source.Floats.Count := FloatArrayCount; |
| 230 |
FloatArrayContents := FloatArray.TextData; |
| 231 |
|
| 232 |
SeekPos := 1; |
| 233 |
for I := 0 to FloatArrayCount - 1 do |
| 234 |
begin |
| 235 |
Token := NextToken(FloatArrayContents, SeekPos); |
| 236 |
if Token = '' then |
| 237 |
begin |
| 238 |
WritelnWarning('Collada', 'Actual number of tokens in <float_array>' + |
| 239 |
' less than declated in the count attribute'); |
| 240 |
Break; |
| 241 |
end; |
| 242 |
Source.Floats.L[I] := StrToFloatDot(Token); |
| 243 |
end; |
| 244 |
|
| 245 |
Technique := SourceElement.ChildElement('technique_common', false); |
| 246 |
if Technique = nil then |
| 247 |
begin |
| 248 |
Technique := SourceElement.ChildElement('technique', false); |
| 249 |
{ TODO: actually, I should search for technique with profile="COMMON" |
| 250 |
in this case, right ? } |
| 251 |
end; |
| 252 |
|
| 253 |
if Technique <> nil then |
| 254 |
begin |
| 255 |
Accessor := Technique.ChildElement('accessor', false); |
| 256 |
|
| 257 |
{ read <accessor> attributes } |
| 258 |
|
| 259 |
if not Accessor.AttributeInteger('count', Source.Count) then |
| 260 |
begin |
| 261 |
WritelnWarning('Collada', '<accessor> has no count attribute'); |
| 262 |
Source.Count := 0; |
| 263 |
end; |
| 264 |
|
| 265 |
if not Accessor.AttributeInteger('stride', Source.Stride) then |
| 266 |
{ default, according to Collada spec } |
| 267 |
Source.Stride := 1; |
| 268 |
|
| 269 |
if not Accessor.AttributeInteger('offset', Source.Offset) then |
| 270 |
{ default, according to Collada spec } |
| 271 |
Source.Offset := 0; |
| 272 |
|
| 273 |
if not Accessor.AttributeString('source', AccessorSource) then |
| 274 |
begin |
| 275 |
WritelnWarning('Collada', '<accessor> has no source attribute'); |
| 276 |
AccessorSource := ''; |
| 277 |
end; |
| 278 |
|
| 279 |
{ We read AccessorSource only to check here } |
| 280 |
if AccessorSource <> '#' + FloatArrayId then |
| 281 |
WritelnWarning('Collada', '<accessor> source does not refer to <float_array> in the same <source>, this is not supported'); |
| 282 |
|
| 283 |
It := Accessor.ChildrenIterator('param'); |
| 284 |
try |
| 285 |
while It.GetNext do |
| 286 |
begin |
| 287 |
if not It.Current.AttributeString('name', ParamName) then |
| 288 |
begin |
| 289 |
WritelnWarning('Collada', 'Missing "name" of <param>'); |
| 290 |
{ TODO: this should not be required? } |
| 291 |
Continue; |
| 292 |
end; |
| 293 |
|
| 294 |
if not It.Current.AttributeString('type', ParamType) then |
| 295 |
begin |
| 296 |
WritelnWarning('Collada', 'Missing "type" of <param>'); |
| 297 |
Continue; |
| 298 |
end; |
| 299 |
|
| 300 |
if ParamType <> 'float' then |
| 301 |
begin |
| 302 |
WritelnWarning('Collada', Format('<param> type "%s" is not supported', |
| 303 |
[ParamType])); |
| 304 |
Continue; |
| 305 |
end; |
| 306 |
|
| 307 |
Source.Params.Add(ParamName); |
| 308 |
end; |
| 309 |
finally FreeAndNil(It) end; |
| 310 |
end; |
| 311 |
end; |
| 312 |
end; |
| 313 |
|
| 314 |
var |
| 315 |
{ We take care to share coordinates, tex coordinates and normal vectors, |
| 316 |
so that they are stored efficiently in memory and can be saved using |
| 317 |
X3D DEF/USE mechanism. Coordinates are always shared by all primitives |
| 318 |
within the geometry. Each primitive may potentially have different tex coords |
| 319 |
or normals arrays, or they may share the same. } |
| 320 |
|
| 321 |
{ Collada coordinates. |
| 322 |
Based on <source> indicated by last <vertices> element. } |
| 323 |
Coord: TCoordinateNode; |
| 324 |
{ Assigning to Coord using AssignToVectorXYZ went without trouble. } |
| 325 |
CoordCorrect: boolean; |
| 326 |
|
| 327 |
{ Collada texture coords used by last primitive. |
| 328 |
Based on <source> indicated by <input semantic="TEXCOORD"> element within |
| 329 |
primitive. Name corresponds to <source> id. } |
| 330 |
LastTexCoord: TTextureCoordinateNode; |
| 331 |
|
| 332 |
{ Collada normal used by last primitive. |
| 333 |
Based on <source> indicated by <input semantic="NORMAL"> element within |
| 334 |
primitive. Name corresponds to <source> id. } |
| 335 |
LastNormal: TNormalNode; |
| 336 |
|
| 337 |
{ DoubleSided, should be used by ReadPrimitiveCommon to set X3D solid field. } |
| 338 |
DoubleSided: boolean; |
| 339 |
|
| 340 |
{ Read <vertices> within <mesh> } |
| 341 |
procedure ReadVertices(VerticesElement: TDOMElement); |
| 342 |
var |
| 343 |
I: TXMLElementIterator; |
| 344 |
InputSemantic, InputSourceName, Id: string; |
| 345 |
InputSource: TColladaSource; |
| 346 |
begin |
| 347 |
if Coord <> nil then |
| 348 |
begin |
| 349 |
WritelnWarning('Collada', '<vertices> specified multiple times within a geometry'); |
| 350 |
FreeIfUnusedAndNil(Coord); |
| 351 |
end; |
| 352 |
|
| 353 |
if not VerticesElement.AttributeString('id', Id) then |
| 354 |
Id := ''; |
| 355 |
|
| 356 |
Coord := TCoordinateNode.Create(Id, BaseUrl); |
| 357 |
CoordCorrect := true; |
| 358 |
|
| 359 |
I := VerticesElement.ChildrenIterator('input'); |
| 360 |
try |
| 361 |
while I.GetNext do |
| 362 |
if I.Current.AttributeString('semantic', InputSemantic) and |
| 363 |
(InputSemantic = 'POSITION') and |
| 364 |
I.Current.AttributeString('source', InputSourceName) and |
| 365 |
SCharIs(InputSourceName, 1, '#') then |
| 366 |
begin |
| 367 |
Delete(InputSourceName, 1, 1); { delete leading '#' char } |
| 368 |
InputSource := Sources.Find(InputSourceName); |
| 369 |
if InputSource <> nil then |
| 370 |
begin |
| 371 |
CoordCorrect := InputSource.AssignToVectorXYZ(Coord.FdPoint.Items); |
| 372 |
Exit; |
| 373 |
end else |
| 374 |
begin |
| 375 |
WritelnWarning('Collada', Format('Source attribute ' + |
| 376 |
'(of <input> element within <vertices>) ' + |
| 377 |
'references non-existing source "%s"', [InputSourceName])); |
| 378 |
end; |
| 379 |
end; |
| 380 |
finally FreeAndNil(I) end; |
| 381 |
|
| 382 |
WritelnWarning('Collada', '<vertices> element has no <input> child' + |
| 383 |
' with semantic="POSITION" and some source attribute'); |
| 384 |
end; |
| 385 |
|
| 386 |
{ Read common things of primitive (<poly*>, <tri*>, <lines*>) within <mesh>. |
| 387 |
- Creates X3D geometry node, initializes it's coordinates |
| 388 |
and other fiels (but leaves *Index fields empty). |
| 389 |
- Adds it to Geometry.Primitives. |
| 390 |
- Returns Indexes instance, to parse indexes of this primitive. } |
| 391 |
function ReadPrimitiveCommon(PrimitiveE: TDOMElement): TColladaIndexes; |
| 392 |
var |
| 393 |
I: TXMLElementIterator; |
| 394 |
InputSemantic, InputSourceId, X3DGeometryName: string; |
| 395 |
Primitive: TColladaPrimitive; |
| 396 |
InputSource: TColladaSource; |
| 397 |
IndexedFaceSet: TIndexedFaceSetNode; |
| 398 |
IndexedLineSet: TIndexedLineSetNode; |
| 399 |
InputsCount, CoordIndexOffset, TexCoordIndexOffset, NormalIndexOffset: Integer; |
| 400 |
begin |
| 401 |
CoordIndexOffset := 0; |
| 402 |
TexCoordIndexOffset := -1; |
| 403 |
NormalIndexOffset := -1; |
| 404 |
|
| 405 |
Primitive := TColladaPrimitive.Create; |
| 406 |
Geometry.Primitives.Add(Primitive); |
| 407 |
|
| 408 |
IndexedFaceSet := nil; |
| 409 |
IndexedLineSet := nil; |
| 410 |
|
| 411 |
X3DGeometryName := Format('%s_collada_primitive_%d', |
| 412 |
{ There may be multiple primitives inside a single Collada <geometry> node, |
| 413 |
so use Geometry.Primitives.Count to name them uniquely for X3D. } |
| 414 |
[Geometry.Name, Geometry.Primitives.Count]); |
| 415 |
|
| 416 |
if (PrimitiveE.TagName = 'lines') or |
| 417 |
(PrimitiveE.TagName = 'linestrips') then |
| 418 |
begin |
| 419 |
IndexedLineSet := TIndexedLineSetNode.Create(X3DGeometryName, BaseUrl); |
| 420 |
IndexedLineSet.FdCoord.Value := Coord; |
| 421 |
Primitive.X3DGeometry := IndexedLineSet; |
| 422 |
end else |
| 423 |
begin |
| 424 |
IndexedFaceSet := TIndexedFaceSetNode.Create(X3DGeometryName, BaseUrl); |
| 425 |
IndexedFaceSet.FdSolid.Value := not DoubleSided; |
| 426 |
{ For VRML >= 2.0, creaseAngle is 0 by default. |
| 427 |
TODO: what is the default normal generation for Collada? } |
| 428 |
IndexedFaceSet.FdCreaseAngle.Value := NiceCreaseAngle; |
| 429 |
IndexedFaceSet.FdCoord.Value := Coord; |
| 430 |
Primitive.X3DGeometry := IndexedFaceSet; |
| 431 |
end; |
| 432 |
|
| 433 |
if PrimitiveE.AttributeString('material', Primitive.Material) then |
| 434 |
begin |
| 435 |
{ Collada 1.4.1 spec says that this is just material name. |
| 436 |
Collada 1.3.1 spec says that this is URL. } |
| 437 |
if (not Version14) and SCharIs(Primitive.Material, 1, '#') then |
| 438 |
Delete(Primitive.Material, 1, 1); |
| 439 |
end; { else leave Primitive.Material as '' } |
| 440 |
|
| 441 |
InputsCount := 0; |
| 442 |
|
| 443 |
I := PrimitiveE.ChildrenIterator('input'); |
| 444 |
try |
| 445 |
while I.GetNext do |
| 446 |
begin |
| 447 |
{ we must count all inputs, since parsing <p> elements depends |
| 448 |
on InputsCount } |
| 449 |
Inc(InputsCount); |
| 450 |
if I.Current.AttributeString('semantic', InputSemantic) then |
| 451 |
begin |
| 452 |
if InputSemantic = 'VERTEX' then |
| 453 |
begin |
| 454 |
if not (I.Current.AttributeString('source', InputSourceId) and |
| 455 |
(Coord <> nil) and |
| 456 |
(InputSourceId = '#' + Coord.X3DName)) then |
| 457 |
WritelnWarning('Collada', '<input> with semantic="VERTEX" (of primitive element within <mesh>) does not reference <vertices> element within the same <mesh>'); |
| 458 |
|
| 459 |
{ Collada requires offset in this case. |
| 460 |
For us, if there's no offset, just leave CoordIndexOffset default. } |
| 461 |
I.Current.AttributeInteger('offset', CoordIndexOffset); |
| 462 |
end else |
| 463 |
if InputSemantic = 'TEXCOORD' then |
| 464 |
begin |
| 465 |
I.Current.AttributeInteger('offset', TexCoordIndexOffset); |
| 466 |
|
| 467 |
{ Only for IndexedFaceSet. |
| 468 |
In case of trouble with coordinates, don't use texCoord, |
| 469 |
they may be invalid (this is for invalid Blender 1.3 exporter) } |
| 470 |
if (IndexedFaceSet = nil) or (not CoordCorrect) then Continue; |
| 471 |
|
| 472 |
if I.Current.AttributeString('source', InputSourceId) then |
| 473 |
begin |
| 474 |
if SCharIs(InputSourceId, 1, '#') then |
| 475 |
Delete(InputSourceId, 1, 1); |
| 476 |
if ((LastTexCoord <> nil) and |
| 477 |
(LastTexCoord.X3DName = InputSourceId)) then |
| 478 |
{ we can reuse last X3D tex coord node } |
| 479 |
IndexedFaceSet.FdTexCoord.Value := LastTexCoord else |
| 480 |
begin |
| 481 |
InputSource := Sources.Find(InputSourceId); |
| 482 |
if InputSource <> nil then |
| 483 |
begin |
| 484 |
{ create and use new X3D tex coord node } |
| 485 |
FreeIfUnusedAndNil(LastTexCoord); |
| 486 |
LastTexCoord := TTextureCoordinateNode.Create(InputSourceId, BaseUrl); |
| 487 |
InputSource.AssignToVectorST(LastTexCoord.FdPoint.Items); |
| 488 |
IndexedFaceSet.FdTexCoord.Value := LastTexCoord; |
| 489 |
end else |
| 490 |
WritelnWarning('Collada', Format('<source> with id "%s" for texture coordinates not found', |
| 491 |
[InputSourceId])); |
| 492 |
end; |
| 493 |
end else |
| 494 |
WritelnWarning('Collada', 'Missing source for <input> with semantic="TEXCOORD". We have texture coord indexes, but they will be ignored, since we have no actual texture coords'); |
| 495 |
end else |
| 496 |
if InputSemantic = 'NORMAL' then |
| 497 |
begin |
| 498 |
I.Current.AttributeInteger('offset', NormalIndexOffset); |
| 499 |
|
| 500 |
{ Only for IndexedFaceSet. |
| 501 |
In case of trouble with coordinates, don't use normals, |
| 502 |
they may be invalid (this is for invalid Blender 1.3 exporter) } |
| 503 |
if (IndexedFaceSet = nil) or (not CoordCorrect) then Continue; |
| 504 |
|
| 505 |
if I.Current.AttributeString('source', InputSourceId) then |
| 506 |
begin |
| 507 |
if SCharIs(InputSourceId, 1, '#') then |
| 508 |
Delete(InputSourceId, 1, 1); |
| 509 |
if ((LastNormal <> nil) and |
| 510 |
(LastNormal.X3DName = InputSourceId)) then |
| 511 |
{ we can reuse last X3D normal node } |
| 512 |
IndexedFaceSet.FdNormal.Value := LastNormal else |
| 513 |
begin |
| 514 |
InputSource := Sources.Find(InputSourceId); |
| 515 |
if InputSource <> nil then |
| 516 |
begin |
| 517 |
{ create and use new X3D normal node } |
| 518 |
FreeIfUnusedAndNil(LastNormal); |
| 519 |
LastNormal := TNormalNode.Create(InputSourceId, BaseUrl); |
| 520 |
InputSource.AssignToVectorXYZ(LastNormal.FdVector.Items); |
| 521 |
IndexedFaceSet.FdNormal.Value := LastNormal; |
| 522 |
end else |
| 523 |
WritelnWarning('Collada', Format('<source> with id "%s" for normals not found', |
| 524 |
[InputSourceId])); |
| 525 |
end; |
| 526 |
end else |
| 527 |
WritelnWarning('Collada', 'Missing source for <input> with semantic="NORMAL". We have normal indexes, but they will be ignored, since we have no actual normals'); |
| 528 |
end; |
| 529 |
end; |
| 530 |
end; |
| 531 |
finally FreeAndNil(I) end; |
| 532 |
|
| 533 |
Result := TColladaIndexes.Create(Primitive.X3DGeometry, |
| 534 |
InputsCount, CoordIndexOffset, TexCoordIndexOffset, NormalIndexOffset); |
| 535 |
end; |
| 536 |
|
| 537 |
{ Read <polygons> within <mesh> } |
| 538 |
procedure ReadPolygons(PrimitiveE: TDOMElement); |
| 539 |
var |
| 540 |
I: TXMLElementIterator; |
| 541 |
Indexes: TColladaIndexes; |
| 542 |
begin |
| 543 |
Indexes := ReadPrimitiveCommon(PrimitiveE); |
| 544 |
try |
| 545 |
I := PrimitiveE.ChildrenIterator('p'); |
| 546 |
try |
| 547 |
while I.GetNext do |
| 548 |
begin |
| 549 |
Indexes.BeginElement(I.Current); |
| 550 |
while Indexes.ReadAddVertex do ; |
| 551 |
Indexes.PolygonEnd; |
| 552 |
end; |
| 553 |
finally FreeAndNil(I) end; |
| 554 |
finally FreeAndNil(Indexes) end; |
| 555 |
end; |
| 556 |
|
| 557 |
{ Read <polylist> within <mesh> } |
| 558 |
procedure ReadPolylist(PrimitiveE: TDOMElement); |
| 559 |
var |
| 560 |
VCountE, P: TDOMElement; |
| 561 |
I: Integer; |
| 562 |
Indexes: TColladaIndexes; |
| 563 |
VCount: TIntegersParser; |
| 564 |
begin |
| 565 |
Indexes := ReadPrimitiveCommon(PrimitiveE); |
| 566 |
try |
| 567 |
VCountE := PrimitiveE.ChildElement('vcount', false); |
| 568 |
P := PrimitiveE.ChildElement('p', false); |
| 569 |
|
| 570 |
if (VCountE <> nil) and (P <> nil) then |
| 571 |
begin |
| 572 |
Indexes.BeginElement(P); |
| 573 |
VCount := TIntegersParser.Create(VCountE); |
| 574 |
try |
| 575 |
{ we will parse both VCount and Indexes now, at the same time } |
| 576 |
while VCount.GetNext do |
| 577 |
begin |
| 578 |
for I := 0 to VCount.Current - 1 do |
| 579 |
if not Indexes.ReadAddVertex then |
| 580 |
begin |
| 581 |
WritelnWarning('Collada', 'Unexpected end of <p> data in <polylist>'); |
| 582 |
Exit; |
| 583 |
end; |
| 584 |
Indexes.PolygonEnd; |
| 585 |
end; |
| 586 |
finally FreeAndNil(VCount) end; |
| 587 |
end; |
| 588 |
finally FreeAndNil(Indexes) end; |
| 589 |
end; |
| 590 |
|
| 591 |
{ Read <triangles> within <mesh> } |
| 592 |
procedure ReadTriangles(PrimitiveE: TDOMElement); |
| 593 |
var |
| 594 |
P: TDOMElement; |
| 595 |
Indexes: TColladaIndexes; |
| 596 |
begin |
| 597 |
Indexes := ReadPrimitiveCommon(PrimitiveE); |
| 598 |
try |
| 599 |
P := PrimitiveE.ChildElement('p', false); |
| 600 |
if P <> nil then |
| 601 |
begin |
| 602 |
Indexes.BeginElement(P); |
| 603 |
repeat |
| 604 |
if not Indexes.ReadAddVertex then Break; |
| 605 |
if not Indexes.ReadAddVertex then Break; |
| 606 |
if not Indexes.ReadAddVertex then Break; |
| 607 |
Indexes.PolygonEnd; |
| 608 |
until false; |
| 609 |
end; |
| 610 |
finally FreeAndNil(Indexes) end; |
| 611 |
end; |
| 612 |
|
| 613 |
{ Read <trifans> within <mesh> } |
| 614 |
procedure ReadTriFans(PrimitiveE: TDOMElement); |
| 615 |
var |
| 616 |
I: TXMLElementIterator; |
| 617 |
Indexes: TColladaIndexes; |
| 618 |
Vertex1, Vertex2, VertexPrevious, VertexNext: TIndex; |
| 619 |
begin |
| 620 |
Indexes := ReadPrimitiveCommon(PrimitiveE); |
| 621 |
try |
| 622 |
I := PrimitiveE.ChildrenIterator('p'); |
| 623 |
try |
| 624 |
while I.GetNext do |
| 625 |
begin |
| 626 |
Indexes.BeginElement(I.Current); |
| 627 |
if Indexes.ReadVertex(Vertex1) and |
| 628 |
Indexes.ReadVertex(Vertex2) and |
| 629 |
Indexes.ReadVertex(VertexPrevious) then |
| 630 |
begin |
| 631 |
Indexes.AddVertex(Vertex1); |
| 632 |
Indexes.AddVertex(Vertex2); |
| 633 |
Indexes.AddVertex(VertexPrevious); |
| 634 |
Indexes.PolygonEnd; |
| 635 |
while Indexes.ReadVertex(VertexNext) do |
| 636 |
begin |
| 637 |
Indexes.AddVertex(Vertex1); |
| 638 |
Indexes.AddVertex(VertexPrevious); |
| 639 |
Indexes.AddVertex(VertexNext); |
| 640 |
Indexes.PolygonEnd; |
| 641 |
VertexPrevious := VertexNext; |
| 642 |
end; |
| 643 |
end; |
| 644 |
end; |
| 645 |
finally FreeAndNil(I) end; |
| 646 |
finally FreeAndNil(Indexes) end; |
| 647 |
end; |
| 648 |
|
| 649 |
{ Read <tristrips> within <mesh> } |
| 650 |
procedure ReadTriStrips(PrimitiveE: TDOMElement); |
| 651 |
var |
| 652 |
I: TXMLElementIterator; |
| 653 |
Indexes: TColladaIndexes; |
| 654 |
Vertex1, Vertex2, Vertex3, VertexNext: TIndex; |
| 655 |
Turn: boolean; |
| 656 |
begin |
| 657 |
Indexes := ReadPrimitiveCommon(PrimitiveE); |
| 658 |
try |
| 659 |
I := PrimitiveE.ChildrenIterator('p'); |
| 660 |
try |
| 661 |
while I.GetNext do |
| 662 |
begin |
| 663 |
Turn := true; |
| 664 |
Indexes.BeginElement(I.Current); |
| 665 |
if Indexes.ReadVertex(Vertex1) and |
| 666 |
Indexes.ReadVertex(Vertex2) and |
| 667 |
Indexes.ReadVertex(Vertex3) then |
| 668 |
begin |
| 669 |
Indexes.AddVertex(Vertex1); |
| 670 |
Indexes.AddVertex(Vertex2); |
| 671 |
Indexes.AddVertex(Vertex3); |
| 672 |
Indexes.PolygonEnd; |
| 673 |
while Indexes.ReadVertex(VertexNext) do |
| 674 |
begin |
| 675 |
Indexes.AddVertex(Vertex3); |
| 676 |
Indexes.AddVertex(Vertex2); |
| 677 |
Indexes.AddVertex(VertexNext); |
| 678 |
Indexes.PolygonEnd; |
| 679 |
if Turn then |
| 680 |
Vertex2 := VertexNext else |
| 681 |
Vertex3 := VertexNext; |
| 682 |
Turn := not Turn; |
| 683 |
end; |
| 684 |
end; |
| 685 |
end; |
| 686 |
finally FreeAndNil(I) end; |
| 687 |
finally FreeAndNil(Indexes) end; |
| 688 |
end; |
| 689 |
|
| 690 |
{ Read <lines> within <mesh> } |
| 691 |
procedure ReadLines(PrimitiveE: TDOMElement); |
| 692 |
var |
| 693 |
P: TDOMElement; |
| 694 |
Indexes: TColladaIndexes; |
| 695 |
begin |
| 696 |
Indexes := ReadPrimitiveCommon(PrimitiveE); |
| 697 |
try |
| 698 |
P := PrimitiveE.ChildElement('p', false); |
| 699 |
if P <> nil then |
| 700 |
begin |
| 701 |
Indexes.BeginElement(P); |
| 702 |
repeat |
| 703 |
if not Indexes.ReadAddVertex then Break; |
| 704 |
if not Indexes.ReadAddVertex then Break; |
| 705 |
Indexes.PolygonEnd; |
| 706 |
until false; |
| 707 |
end; |
| 708 |
finally FreeAndNil(Indexes) end; |
| 709 |
end; |
| 710 |
|
| 711 |
{ TODO: <linestrips> should work fine, but untested } |
| 712 |
{ Read <linestrips> within <mesh> } |
| 713 |
procedure ReadLineStrips(PrimitiveE: TDOMElement); |
| 714 |
var |
| 715 |
I: TXMLElementIterator; |
| 716 |
Indexes: TColladaIndexes; |
| 717 |
begin |
| 718 |
Indexes := ReadPrimitiveCommon(PrimitiveE); |
| 719 |
try |
| 720 |
I := PrimitiveE.ChildrenIterator('p'); |
| 721 |
try |
| 722 |
while I.GetNext do |
| 723 |
begin |
| 724 |
Indexes.BeginElement(I.Current); |
| 725 |
while Indexes.ReadAddVertex do ; |
| 726 |
Indexes.PolygonEnd; |
| 727 |
end; |
| 728 |
finally FreeAndNil(I) end; |
| 729 |
finally FreeAndNil(Indexes) end; |
| 730 |
end; |
| 731 |
|
| 732 |
var |
| 733 |
Mesh: TDOMElement; |
| 734 |
I: TXMLElementIterator; |
| 735 |
GeometryId: string; |
| 736 |
begin |
| 737 |
if not GeometryElement.AttributeString('id', GeometryId) then |
| 738 |
GeometryId := ''; |
| 739 |
|
| 740 |
DoubleSided := ReadDoubleSided(GeometryElement); |
| 741 |
|
| 742 |
Geometry := TColladaGeometry.Create; |
| 743 |
Geometry.Name := GeometryId; |
| 744 |
Geometries.Add(Geometry); |
| 745 |
|
| 746 |
Mesh := GeometryElement.ChildElement('mesh', false); |
| 747 |
if Mesh <> nil then |
| 748 |
begin |
| 749 |
Coord := nil; |
| 750 |
LastTexCoord := nil; |
| 751 |
LastNormal := nil; |
| 752 |
Sources := TColladaSourceList.Create; |
| 753 |
try |
| 754 |
I := Mesh.ChildrenIterator; |
| 755 |
try |
| 756 |
while I.GetNext do |
| 757 |
if I.Current.TagName = 'source' then |
| 758 |
ReadSource(I.Current) else |
| 759 |
if I.Current.TagName = 'vertices' then |
| 760 |
ReadVertices(I.Current) else |
| 761 |
if I.Current.TagName = 'polygons' then |
| 762 |
ReadPolygons(I.Current) else |
| 763 |
if I.Current.TagName = 'polylist' then |
| 764 |
ReadPolylist(I.Current) else |
| 765 |
if I.Current.TagName = 'triangles' then |
| 766 |
ReadTriangles(I.Current) else |
| 767 |
if I.Current.TagName = 'trifans' then |
| 768 |
ReadTriFans(I.Current) else |
| 769 |
if I.Current.TagName = 'tristrips' then |
| 770 |
ReadTriStrips(I.Current) else |
| 771 |
if I.Current.TagName = 'lines' then |
| 772 |
ReadLines(I.Current) else |
| 773 |
if I.Current.TagName = 'linestrips' then |
| 774 |
ReadLineStrips(I.Current) else |
| 775 |
WritelnWarning('Collada', Format('Element "%s" within <mesh> not supported', |
| 776 |
[I.Current.TagName])); |
| 777 |
finally FreeAndNil(I) end; |
| 778 |
finally |
| 779 |
FreeAndNil(Sources); |
| 780 |
FreeIfUnusedAndNil(Coord); |
| 781 |
{ actually LastTexCoord is for sure used by something, |
| 782 |
we don't really have to finalize it here } |
| 783 |
FreeIfUnusedAndNil(LastTexCoord); |
| 784 |
end; |
| 785 |
end; |
| 786 |
end; |
| 787 |
|
| 788 |
{ Read <library_geometries> (Collada >= 1.4.x) or |
| 789 |
<library type="GEOMETRY"> (Collada < 1.4.x). } |
| 790 |
procedure ReadLibraryGeometries(LibraryElement: TDOMElement); |
| 791 |
var |
| 792 |
I: TXMLElementIterator; |
| 793 |
begin |
| 794 |
I := LibraryElement.ChildrenIterator('geometry'); |
| 795 |
try |
| 796 |
while I.GetNext do |
| 797 |
ReadGeometry(I.Current); |
| 798 |
{ other I.Current.TagName not supported for now } |
| 799 |
finally FreeAndNil(I) end; |
| 800 |
end; |
| 801 |
|
| 802 |
{$endif read_implementation_nested} |