| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2008-2018 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 |
{ Reading of Collada <library_nodes> and <instance_node>. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
TNodeAvailableInLibrary = record |
| 23 |
Element: TDOMElement; |
| 24 |
Handled: boolean; |
| 25 |
end; |
| 26 |
PNodeAvailableInLibrary = ^TNodeAvailableInLibrary; |
| 27 |
TNodeAvailableInLibraryList = {$ifdef FPC}specialize{$endif} TStructList<TNodeAvailableInLibrary>; |
| 28 |
|
| 29 |
{$endif read_interface} |
| 30 |
|
| 31 |
{$ifdef read_implementation_nested} |
| 32 |
|
| 33 |
var |
| 34 |
{ Used when reading library_nodes, to handle dependencies between nodes. |
| 35 |
Nodes are specified on library_nodes in a wild order, |
| 36 |
not necessarily good for resolving them. |
| 37 |
So ReadInstanceNode may lookup the node in LibraryNodesSwitch, |
| 38 |
or it may be forced to read the node earlier using |
| 39 |
LibraryNodesAvailable. } |
| 40 |
LibraryNodesAvailable: TNodeAvailableInLibraryList; |
| 41 |
LibraryNodesSwitch: TSwitchNode; |
| 42 |
|
| 43 |
{ Read <instance_node>, adding resulting X3D node into ParentGroup. } |
| 44 |
procedure ReadInstanceNode(ParentGroup: TAbstractGroupingNode; |
| 45 |
InstantiatingElement: TDOMElement); |
| 46 |
var |
| 47 |
Id: string; |
| 48 |
Node: TAbstractChildNode; |
| 49 |
J: Integer; |
| 50 |
begin |
| 51 |
if InstantiatingElement.AttributeString('url', Id) and |
| 52 |
SCharIs(Id, 1, '#') then |
| 53 |
begin |
| 54 |
Delete(Id, 1, 1); |
| 55 |
Node := LibraryNodesSwitch.FdChildren.FindName(Id) as TAbstractChildNode; |
| 56 |
|
| 57 |
{ try to load required node on-demand from LibraryNodesAvailable } |
| 58 |
if Node = nil then |
| 59 |
begin |
| 60 |
if LibraryNodesAvailable <> nil then |
| 61 |
begin |
| 62 |
for J := 0 to LibraryNodesAvailable.Count - 1 do |
| 63 |
if (not LibraryNodesAvailable.L[J].Handled) and |
| 64 |
(LibraryNodesAvailable.L[J].Element.AttributeStringDef('id', '') = Id) then |
| 65 |
begin |
| 66 |
LibraryNodesAvailable.L[J].Handled := true; |
| 67 |
ReadNodeElement(LibraryNodesSwitch, LibraryNodesAvailable.L[J].Element); |
| 68 |
Node := LibraryNodesSwitch.FdChildren.FindName(Id) as TAbstractChildNode; |
| 69 |
break; |
| 70 |
end; |
| 71 |
end; |
| 72 |
end; |
| 73 |
|
| 74 |
if Node = nil then |
| 75 |
WritelnWarning('Collada', Format('<node> instantiates non-existing element "%s"', |
| 76 |
[Id])) else |
| 77 |
ParentGroup.AddChildren(Node); |
| 78 |
end else |
| 79 |
WritelnWarning('Collada', Format('Element <%s> missing url attribute (that has to start with #)', |
| 80 |
[InstantiatingElement.TagName])); |
| 81 |
end; |
| 82 |
|
| 83 |
{ Read <library_nodes>. All obtained X3D nodes are added |
| 84 |
to the LibraryNodes list. } |
| 85 |
procedure ReadLibraryNodes(LibraryNodesElement: TDOMElement); |
| 86 |
var |
| 87 |
I: TXMLElementIterator; |
| 88 |
NodeAvail: PNodeAvailableInLibrary; |
| 89 |
J: Integer; |
| 90 |
begin |
| 91 |
if LibraryNodesSwitch = nil then |
| 92 |
begin |
| 93 |
{ Library of nodes is simply a X3D Switch, with each |
| 94 |
node inside as one choice. This way we export to X3D all |
| 95 |
nodes from Collada, even those not chosen as current scene. |
| 96 |
That's good --- it's always nice to keep some data when |
| 97 |
converting. } |
| 98 |
|
| 99 |
LibraryNodesSwitch := TSwitchNode.Create('collada_library_nodes', BaseUrl); |
| 100 |
ResultModel.AddChildren(LibraryNodesSwitch); |
| 101 |
end; |
| 102 |
|
| 103 |
LibraryNodesAvailable := TNodeAvailableInLibraryList.Create; |
| 104 |
try |
| 105 |
{ calculate LibraryNodesAvailable contents } |
| 106 |
I := LibraryNodesElement.ChildrenIterator('node'); |
| 107 |
try |
| 108 |
while I.GetNext do |
| 109 |
begin |
| 110 |
NodeAvail := PNodeAvailableInLibrary(LibraryNodesAvailable.Add); |
| 111 |
NodeAvail^.Handled := false; |
| 112 |
NodeAvail^.Element := I.Current; |
| 113 |
end; |
| 114 |
finally FreeAndNil(I) end; |
| 115 |
|
| 116 |
{ iterate over LibraryNodesAvailable, resolving nodes. |
| 117 |
Note that some nodes may cause resolving of other nodes faster, |
| 118 |
due to ReadInstanceNode requiring some node faster. } |
| 119 |
for J := 0 to LibraryNodesAvailable.Count - 1 do |
| 120 |
if not LibraryNodesAvailable.L[J].Handled then |
| 121 |
begin |
| 122 |
LibraryNodesAvailable.L[J].Handled := true; |
| 123 |
ReadNodeElement(LibraryNodesSwitch, LibraryNodesAvailable.L[J].Element); |
| 124 |
end; |
| 125 |
finally FreeAndNil(LibraryNodesAvailable) end; |
| 126 |
end; |
| 127 |
|
| 128 |
{$endif read_implementation_nested} |