| 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_nested} |
| 17 |
|
| 18 |
procedure ReadNodeElement(ParentGroup: TAbstractGroupingNode; |
| 19 |
NodeElement: TDOMElement); forward; |
| 20 |
|
| 21 |
{$endif read_interface_nested} |
| 22 |
|
| 23 |
{$ifdef read_implementation_nested} |
| 24 |
|
| 25 |
{ Read <node> element, add it to ParentGroup. } |
| 26 |
procedure ReadNodeElement(ParentGroup: TAbstractGroupingNode; |
| 27 |
NodeElement: TDOMElement); |
| 28 |
var |
| 29 |
{ This is either TTransformNode or TMatrixTransformNode. } |
| 30 |
NodeTransform: TAbstractGroupingNode; |
| 31 |
|
| 32 |
{ Create new Transform node, place it as a child of current Transform node |
| 33 |
and switch current Transform node to the new one. |
| 34 |
|
| 35 |
The idea is that each |
| 36 |
Collada transformation creates new nested X3D Transform node |
| 37 |
(since Collada transformations may represent any transformation, |
| 38 |
not necessarily representable by a single X3D Transform node). |
| 39 |
|
| 40 |
Returns NodeTransform, typecasted to TTransformNode, for your comfort. } |
| 41 |
function NestedTransform: TTransformNode; |
| 42 |
var |
| 43 |
NewNodeTransform: TTransformNode; |
| 44 |
begin |
| 45 |
NewNodeTransform := TTransformNode.Create('', BaseUrl); |
| 46 |
NodeTransform.AddChildren(NewNodeTransform); |
| 47 |
|
| 48 |
NodeTransform := NewNodeTransform; |
| 49 |
Result := NewNodeTransform; |
| 50 |
end; |
| 51 |
|
| 52 |
function NestedMatrixTransform: TMatrixTransformNode; |
| 53 |
var |
| 54 |
NewNodeTransform: TMatrixTransformNode; |
| 55 |
begin |
| 56 |
NewNodeTransform := TMatrixTransformNode.Create('', BaseUrl); |
| 57 |
NodeTransform.AddChildren(NewNodeTransform); |
| 58 |
|
| 59 |
NodeTransform := NewNodeTransform; |
| 60 |
Result := NewNodeTransform; |
| 61 |
end; |
| 62 |
|
| 63 |
var |
| 64 |
I: TXMLElementIterator; |
| 65 |
NodeId: string; |
| 66 |
V3: TVector3; |
| 67 |
V4: TVector4; |
| 68 |
begin |
| 69 |
if not NodeElement.AttributeString('id', NodeId) then |
| 70 |
NodeId := ''; |
| 71 |
|
| 72 |
NodeTransform := TTransformNode.Create(NodeId, BaseUrl); |
| 73 |
{ Add NodeTransform to ParentGroup (which may be LibraryNodes) first. |
| 74 |
This means that if instance_node will create a cycle, we will recreate |
| 75 |
this cycle in VRML/X3D graph, without hanging the importer code. } |
| 76 |
ParentGroup.AddChildren(NodeTransform); |
| 77 |
|
| 78 |
{ First iterate to gather all transformations. |
| 79 |
|
| 80 |
For Collada 1.4, this shouldn't be needed (spec says that |
| 81 |
transforms must be before all instantiations). |
| 82 |
|
| 83 |
But Collada 1.3.1 specification doesn't say anything about the order. |
| 84 |
And e.g. Blender Collada 1.3 exporter in fact generates files |
| 85 |
with <instantiate> first, then <matrix>, and yes: it expects matrix |
| 86 |
should affect instantiated object. So the bottom line is that for |
| 87 |
Collada 1.3, I must first gather all transforms, then do instantiations. } |
| 88 |
|
| 89 |
I := NodeElement.ChildrenIterator; |
| 90 |
try |
| 91 |
while I.GetNext do |
| 92 |
begin |
| 93 |
if I.Current.TagName = 'matrix' then |
| 94 |
begin |
| 95 |
NestedMatrixTransform.FdMatrix.Value := ReadMatrix(I.Current); |
| 96 |
end else |
| 97 |
if I.Current.TagName = 'rotate' then |
| 98 |
begin |
| 99 |
V4 := Vector4FromStr(I.Current.TextData); |
| 100 |
if V4[3] <> 0.0 then |
| 101 |
begin |
| 102 |
NestedTransform.FdRotation.ValueDeg := V4; |
| 103 |
end; |
| 104 |
end else |
| 105 |
if I.Current.TagName = 'scale' then |
| 106 |
begin |
| 107 |
V3 := Vector3FromStr(I.Current.TextData); |
| 108 |
if not TVector3.PerfectlyEquals(V3, Vector3(1, 1, 1)) then |
| 109 |
begin |
| 110 |
NestedTransform.FdScale.Value := V3; |
| 111 |
end; |
| 112 |
end else |
| 113 |
if I.Current.TagName = 'lookat' then |
| 114 |
begin |
| 115 |
NestedMatrixTransform.FdMatrix.Value := ReadLookAt(I.Current); |
| 116 |
end else |
| 117 |
if I.Current.TagName = 'skew' then |
| 118 |
begin |
| 119 |
{ TODO } |
| 120 |
end else |
| 121 |
if I.Current.TagName = 'translate' then |
| 122 |
begin |
| 123 |
V3 := Vector3FromStr(I.Current.TextData); |
| 124 |
if not TVector3.PerfectlyEquals(V3, TVector3.Zero) then |
| 125 |
begin |
| 126 |
NestedTransform.FdTranslation.Value := V3; |
| 127 |
end; |
| 128 |
end; |
| 129 |
end; |
| 130 |
finally FreeAndNil(I); end; |
| 131 |
|
| 132 |
{ Now iterate to read instantiations and recursive nodes. } |
| 133 |
|
| 134 |
I := NodeElement.ChildrenIterator; |
| 135 |
try |
| 136 |
while I.GetNext do |
| 137 |
begin |
| 138 |
if (I.Current.TagName = 'instance') or |
| 139 |
(I.Current.TagName = 'instance_geometry') then |
| 140 |
ReadInstanceGeometry(NodeTransform, I.Current) else |
| 141 |
if I.Current.TagName = 'instance_controller' then |
| 142 |
ReadInstanceController(NodeTransform, I.Current) else |
| 143 |
if I.Current.TagName = 'instance_camera' then |
| 144 |
ReadInstance(NodeTransform, I.Current, Cameras) else |
| 145 |
if I.Current.TagName = 'instance_light' then |
| 146 |
ReadInstance(NodeTransform, I.Current, Lights) else |
| 147 |
if I.Current.TagName = 'instance_node' then |
| 148 |
ReadInstanceNode(NodeTransform, I.Current) else |
| 149 |
if I.Current.TagName = 'node' then |
| 150 |
ReadNodeElement(NodeTransform, I.Current); |
| 151 |
end; |
| 152 |
finally FreeAndNil(I) end; |
| 153 |
end; |
| 154 |
|
| 155 |
{ Read <node> and <instance_node> sequence within given SceneElement, |
| 156 |
adding nodes to Group. |
| 157 |
This is used to handle <visual_scene> for Collada 1.4.x |
| 158 |
and <scene> for Collada 1.3.x. } |
| 159 |
procedure ReadNodesSequence(Group: TAbstractGroupingNode; |
| 160 |
SceneElement: TDOMElement); |
| 161 |
var |
| 162 |
I: TXMLElementIterator; |
| 163 |
begin |
| 164 |
I := SceneElement.ChildrenIterator; |
| 165 |
try |
| 166 |
while I.GetNext do |
| 167 |
if I.Current.TagName = 'node' then |
| 168 |
ReadNodeElement(Group, I.Current) |
| 169 |
|
| 170 |
// TODO: untested, search collada tests for instance_node |
| 171 |
// ; else |
| 172 |
// if I.Current.TagName = 'instance_node' then |
| 173 |
// ReadInstanceNode(Group, I.Current); |
| 174 |
finally FreeAndNil(I) end; |
| 175 |
end; |
| 176 |
|
| 177 |
{$endif read_implementation_nested} |