| 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_implementation_nested} |
| 17 |
|
| 18 |
var |
| 19 |
Cameras: TX3DChildrenList; |
| 20 |
|
| 21 |
{ Read <library_cameras> (Collada 1.4.x). Fills Cameras list. } |
| 22 |
procedure ReadLibraryCameras(LibraryE: TDOMElement); |
| 23 |
var |
| 24 |
Id: string; |
| 25 |
CameraGroup: TGroupNode; |
| 26 |
|
| 27 |
procedure InitializeNavigationInfo(E: TDOMElement); |
| 28 |
var |
| 29 |
Navigation: TNavigationInfoNode; |
| 30 |
ZNear, ZFar: Float; |
| 31 |
begin |
| 32 |
Navigation := TNavigationInfoNode.Create(Id + '_navigation_info', BaseUrl); |
| 33 |
CameraGroup.AddChildren(Navigation); |
| 34 |
|
| 35 |
if ReadChildFloat(E, 'znear', ZNear) then |
| 36 |
Navigation.FdAvatarSize.Items[0] := ZNear * 2; |
| 37 |
|
| 38 |
if ReadChildFloat(E, 'zfar', ZFar) then |
| 39 |
Navigation.FdVisibilityLimit.Value := ZFar; |
| 40 |
end; |
| 41 |
|
| 42 |
var |
| 43 |
I: TXMLElementIterator; |
| 44 |
Viewpoint: TViewpointNode; |
| 45 |
OrthoViewpoint: TOrthoViewpointNode; |
| 46 |
OpticsE, TechniqueE, PerspectiveE, OrthographicE: TDOMElement; |
| 47 |
XFov, YFov, XMag, YMag, AspectRatio: Float; |
| 48 |
begin |
| 49 |
I := LibraryE.ChildrenIterator('camera'); |
| 50 |
try |
| 51 |
while I.GetNext do |
| 52 |
if I.Current.AttributeString('id', Id) then |
| 53 |
begin |
| 54 |
CameraGroup := TGroupNode.Create(Id, BaseUrl); |
| 55 |
Cameras.Add(CameraGroup); |
| 56 |
|
| 57 |
OpticsE := I.Current.ChildElement('optics', false); |
| 58 |
if OpticsE <> nil then |
| 59 |
begin |
| 60 |
TechniqueE := OpticsE.ChildElement('technique_common', false); |
| 61 |
if TechniqueE <> nil then |
| 62 |
begin |
| 63 |
PerspectiveE := TechniqueE.ChildElement('perspective', false); |
| 64 |
if PerspectiveE <> nil then |
| 65 |
begin |
| 66 |
Viewpoint := TViewpointNode.Create(Id + '_viewpoint', BaseUrl); |
| 67 |
if CastleX3dExtensions then |
| 68 |
Viewpoint.AutoCenterOfRotation := true; |
| 69 |
Viewpoint.FdPosition.Value := TVector3.Zero; |
| 70 |
CameraGroup.AddChildren(Viewpoint); |
| 71 |
|
| 72 |
{ Try to get YFov, and use it as X3D fieldOfView. |
| 73 |
It's not a perfect translation, the idea of fieldOfView |
| 74 |
is just different (X3D doesn't force aspect ratio). } |
| 75 |
if ReadChildFloat(PerspectiveE, 'yfov', YFov) then |
| 76 |
Viewpoint.FdFieldOfView.Value := DegToRad(YFov) else |
| 77 |
if ReadChildFloat(PerspectiveE, 'xfov', XFov) and |
| 78 |
ReadChildFloat(PerspectiveE, 'aspect_ratio', AspectRatio) and |
| 79 |
(AspectRatio > SingleEpsilon) then |
| 80 |
{ aspect_ratio = xfov / yfov, so we can calculate yfov } |
| 81 |
Viewpoint.FdFieldOfView.Value := DegToRad(XFov) / AspectRatio; |
| 82 |
|
| 83 |
InitializeNavigationInfo(PerspectiveE); |
| 84 |
end else |
| 85 |
begin |
| 86 |
OrthographicE := TechniqueE.ChildElement('orthographic', false); |
| 87 |
if OrthographicE <> nil then |
| 88 |
begin |
| 89 |
OrthoViewpoint := TOrthoViewpointNode.Create(Id + '_viewpoint', BaseUrl); |
| 90 |
if CastleX3dExtensions then |
| 91 |
OrthoViewpoint.AutoCenterOfRotation := true; |
| 92 |
OrthoViewpoint.FdPosition.Value := TVector3.Zero; |
| 93 |
CameraGroup.AddChildren(OrthoViewpoint); |
| 94 |
|
| 95 |
{ Translation to X3D cannot be perfect, as fieldOfView |
| 96 |
just works differently, and X3D automatically preserves |
| 97 |
aspect ratio. We concentrate on setting vertical angle |
| 98 |
right (as this one is usually smaller, and so determines |
| 99 |
horizontal angle). } |
| 100 |
if ReadChildFloat(OrthographicE, 'ymag', YMag) then |
| 101 |
begin |
| 102 |
OrthoViewpoint.FdFieldOfView.Items[1] := -YMag; |
| 103 |
OrthoViewpoint.FdFieldOfView.Items[3] := YMag; |
| 104 |
end else |
| 105 |
if ReadChildFloat(OrthographicE, 'xmag', XMag) then |
| 106 |
begin |
| 107 |
if ReadChildFloat(OrthographicE, 'aspect_ratio', AspectRatio) and |
| 108 |
(AspectRatio > SingleEpsilon) then |
| 109 |
begin |
| 110 |
{ aspect_ratio = xmag / ymag, so we can calculate ymag } |
| 111 |
YMag := XMag / AspectRatio; |
| 112 |
OrthoViewpoint.FdFieldOfView.Items[1] := -YMag; |
| 113 |
OrthoViewpoint.FdFieldOfView.Items[3] := YMag; |
| 114 |
end else |
| 115 |
begin |
| 116 |
OrthoViewpoint.FdFieldOfView.Items[0] := -XMag; |
| 117 |
OrthoViewpoint.FdFieldOfView.Items[2] := XMag; |
| 118 |
end; |
| 119 |
end; |
| 120 |
|
| 121 |
InitializeNavigationInfo(OrthographicE); |
| 122 |
end else |
| 123 |
WritelnWarning('Collada', 'No supported camera inside <technique_common>'); |
| 124 |
end; |
| 125 |
end else |
| 126 |
WritelnWarning('Collada', 'No supported camera technique inside <optics>'); |
| 127 |
end else |
| 128 |
WritelnWarning('Collada', 'No <optics> inside camera'); |
| 129 |
end; |
| 130 |
finally FreeAndNil(I) end; |
| 131 |
end; |
| 132 |
|
| 133 |
{$endif read_implementation_nested} |