Debian Codegraph @

src/scene/load/collada/x3dloadinternalcollada_controllers.inc

raw
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 TColladaController = class
20 Name: string;
21 Source: string;
22 BoundShapeMatrix: TMatrix4;
23 BoundShapeMatrixIdentity: boolean;
24 end;
25
26 { List of Collada controllers. Read from <library_controllers>, used by
27 <instance_controller>. }
28 TColladaControllerList = class({$ifdef FPC}specialize{$endif} TObjectList<TColladaController>)
29 public
30 { Find a TColladaController with given Name, @nil if not found. }
31 function Find(const Name: string): TColladaController;
32 end;
33
34 {$endif read_interface}
35
36 {$ifdef read_implementation}
37
38 function TColladaControllerList.Find(const Name: string): TColladaController;
39 var
40 I: Integer;
41 begin
42 for I := 0 to Count - 1 do
43 if Items[I].Name = Name then
44 Exit(Items[I]);
45
46 Result := nil;
47 end;
48
49 {$endif read_implementation}
50
51 {$ifdef read_implementation_nested}
52 var
53 Controllers: TColladaControllerList;
54
55 { Read <instance_controller>, adding resulting X3D node into
56 ParentGroup. }
57 procedure ReadInstanceController(ParentGroup: TAbstractGroupingNode;
58 InstantiatingElement: TDOMElement);
59 var
60 ControllerId: string;
61 Controller: TColladaController;
62 Group: TAbstractGroupingNode;
63 Geometry: TColladaGeometry;
64 begin
65 if InstantiatingElement.AttributeString('url', ControllerId) and
66 SCharIs(ControllerId, 1, '#') then
67 begin
68 Delete(ControllerId, 1, 1);
69 Controller := Controllers.Find(ControllerId);
70 if Controller = nil then
71 begin
72 WritelnWarning('Collada', Format('<node> instantiates non-existing ' +
73 '<controller> element "%s"', [ControllerId]));
74 end else
75 begin
76 Geometry := Geometries.Find(Controller.Source);
77 if Geometry = nil then
78 begin
79 WritelnWarning('Collada', Format('<controller> references non-existing ' +
80 '<geometry> element "%s"', [Controller.Source]));
81 end else
82 begin
83 if Controller.BoundShapeMatrixIdentity then
84 begin
85 Group := TGroupNode.Create('', BaseUrl);
86 end else
87 begin
88 Group := TMatrixTransformNode.Create('', BaseUrl);
89 TMatrixTransformNode(Group).FdMatrix.Value := Controller.BoundShapeMatrix;
90 end;
91 ParentGroup.AddChildren(Group);
92 AddGeometryInstance(Group, Geometry, InstantiatingElement);
93 end;
94 end;
95 end else
96 WritelnWarning('Collada', Format('Element <%s> missing url attribute (that has to start with #)',
97 [InstantiatingElement.TagName]));
98 end;
99
100 { Read <controller> from Collada 1.4.x }
101 procedure ReadController(ControllerElement: TDOMElement);
102 var
103 Controller: TColladaController;
104 Skin, BindShapeMatrix: TDOMElement;
105 begin
106 Controller := TColladaController.Create;
107 Controllers.Add(Controller);
108 Controller.BoundShapeMatrixIdentity := true;
109
110 if not ControllerElement.AttributeString('id', Controller.Name) then
111 Controller.Name := '';
112
113 Skin := ControllerElement.ChildElement('skin', false);
114 if Skin <> nil then
115 begin
116 if Skin.AttributeString('source', Controller.Source) then
117 begin
118 { this should be true, controller.source is URL }
119 if SCharIs(Controller.Source, 1, '#') then
120 Delete(Controller.Source, 1, 1);
121 end;
122
123 BindShapeMatrix := Skin.ChildElement('bind_shape_matrix', false);
124 if BindShapeMatrix <> nil then
125 begin
126 Controller.BoundShapeMatrixIdentity := false;
127 Controller.BoundShapeMatrix := ReadMatrix(BindShapeMatrix);
128 end;
129 end;
130 end;
131
132 { Read <library_controllers> from Collada 1.4.x }
133 procedure ReadLibraryControllers(LibraryElement: TDOMElement);
134 var
135 I: TXMLElementIterator;
136 begin
137 I := LibraryElement.ChildrenIterator('controller');
138 try
139 while I.GetNext do
140 ReadController(I.Current);
141 { other I.Current.TagName not supported for now }
142 finally FreeAndNil(I) end;
143 end;
144
145 {$endif read_implementation_nested}