| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2008-2024 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 |
{ Save routines, common for both XML and classic encoding. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
{$endif read_interface} |
| 22 |
|
| 23 |
{$ifdef read_implementation} |
| 24 |
|
| 25 |
type |
| 26 |
{ TX3DWriter extended with NodeNames. } |
| 27 |
TX3DWriterNames = class(TX3DWriter) |
| 28 |
{ Record known node names. |
| 29 |
This allows to write correct DEF / USE when saving. } |
| 30 |
NodeNames: TX3DNodeNames; |
| 31 |
|
| 32 |
constructor Create(AStream: TStream; |
| 33 |
const AVersion: TX3DVersion; const AEncoding: TX3DEncoding); |
| 34 |
destructor Destroy; override; |
| 35 |
end; |
| 36 |
|
| 37 |
constructor TX3DWriterNames.Create(AStream: TStream; |
| 38 |
const AVersion: TX3DVersion; const AEncoding: TX3DEncoding); |
| 39 |
begin |
| 40 |
inherited; |
| 41 |
NodeNames := TX3DNodeNames.Create(false); |
| 42 |
end; |
| 43 |
|
| 44 |
destructor TX3DWriterNames.Destroy; |
| 45 |
begin |
| 46 |
FreeAndNil(NodeNames); |
| 47 |
inherited; |
| 48 |
end; |
| 49 |
|
| 50 |
const |
| 51 |
{ Used when we have to save as X3D, but profile information is missing. } |
| 52 |
DefaultX3DSaveProfile = 'Interchange'; |
| 53 |
|
| 54 |
{ global routines ------------------------------------------------------------ } |
| 55 |
|
| 56 |
{ Write model to a VRML or X3D file. |
| 57 |
Generates a complete file, with header lines (XML headers, |
| 58 |
or #VRML / #X3D in classic encoding) and everything. |
| 59 |
|
| 60 |
Generator and Source, if not empty, will be used to set appropriate |
| 61 |
META values of X3D root file node. This way you can indicate inside X3D file |
| 62 |
the generator (your program) name and source (original 3D model file name). |
| 63 |
If this is not an X3D root file node, we will record it inside a comment. |
| 64 |
|
| 65 |
To force convering to X3D, use @link(TX3DRootNode.ForceSaveAsX3D) |
| 66 |
before calling this. } |
| 67 |
procedure InternalSaveVrmlX3D(Node: TX3DNode; |
| 68 |
const Stream: TStream; const Generator, Source: string; |
| 69 |
const Encoding: TX3DEncoding); |
| 70 |
var |
| 71 |
Version: TX3DVersion; // Version to save |
| 72 |
|
| 73 |
{ Which VRML/X3D version should be used to save this 3D model. } |
| 74 |
function GetSaveVersion(const Node: TX3DNode): TX3DVersion; |
| 75 |
begin |
| 76 |
if (Node is TX3DRootNode) and |
| 77 |
TX3DRootNode(Node).HasForceVersion then |
| 78 |
Result := TX3DRootNode(Node).ForceVersion |
| 79 |
else |
| 80 |
{ Use latest X3D. We should promote latest, best version of VRML/X3D. |
| 81 |
Also, this avoids conversion to X3D when saving it to XML encoding. } |
| 82 |
Result := X3DVersion; |
| 83 |
end; |
| 84 |
|
| 85 |
var |
| 86 |
Writer: TX3DWriterNames; |
| 87 |
|
| 88 |
procedure Classic; |
| 89 |
|
| 90 |
procedure SaveProfile(Root: TX3DRootNode); |
| 91 |
begin |
| 92 |
if Root.Profile <> '' then |
| 93 |
Writer.Writeln('PROFILE ' + Root.Profile + NL) else |
| 94 |
Writer.Writeln('PROFILE ' + DefaultX3DSaveProfile + NL); |
| 95 |
end; |
| 96 |
|
| 97 |
procedure SaveComponents(Root: TX3DRootNode); |
| 98 |
var |
| 99 |
ComponentLevel: {$ifdef FPC}TStringIntegerMap.TDictionaryPair{$else}TPair<string, Integer>{$endif}; |
| 100 |
begin |
| 101 |
for ComponentLevel in Root.Components do |
| 102 |
Writer.Writeln(Format('COMPONENT %s:%d', |
| 103 |
[ ComponentLevel.Key, |
| 104 |
ComponentLevel.Value ])); |
| 105 |
if Root.Components.Count <> 0 then |
| 106 |
Writer.Write(NL); |
| 107 |
end; |
| 108 |
|
| 109 |
procedure SaveMetas(Root: TX3DRootNode); |
| 110 |
var |
| 111 |
MetaPair: {$ifdef FPC}TStringStringMap.TDictionaryPair{$else}TPair<string, string>{$endif}; |
| 112 |
begin |
| 113 |
for MetaPair in Root.Meta do |
| 114 |
Writer.Writeln(Format('META %s %s', |
| 115 |
[ StringToX3DClassic(MetaPair.Key), |
| 116 |
StringToX3DClassic(MetaPair.Value)])); |
| 117 |
if Root.Meta.Count <> 0 then |
| 118 |
Writer.Write(NL); |
| 119 |
end; |
| 120 |
|
| 121 |
const |
| 122 |
VRML10Header = '#VRML V1.0 ascii'; |
| 123 |
VRML20Header = '#VRML V2.0 utf8'; |
| 124 |
X3DHeader = '#X3D V%d.%d utf8'; |
| 125 |
var |
| 126 |
VRMLHeader, PrecedingComment: string; |
| 127 |
begin |
| 128 |
if Version.Major <= 1 then |
| 129 |
VRMLHeader := VRML10Header else |
| 130 |
if Version.Major = 2 then |
| 131 |
VRMLHeader := VRML20Header else |
| 132 |
if Version.Major >= 3 then |
| 133 |
VRMLHeader := Format(X3DHeader, [Version.Major, Version.Minor]); |
| 134 |
|
| 135 |
Writer.Writeln(VRMLHeader + NL { yes, one more NL, to look good }); |
| 136 |
|
| 137 |
if (Version.Major >= 3) and (Node is TX3DRootNode) then |
| 138 |
begin |
| 139 |
if Generator <> '' then |
| 140 |
TX3DRootNode(Node).Meta.PutPreserve('generator', Generator); |
| 141 |
if Source <> '' then |
| 142 |
TX3DRootNode(Node).Meta.PutPreserve('source', Source); |
| 143 |
SaveProfile(TX3DRootNode(Node)); |
| 144 |
SaveComponents(TX3DRootNode(Node)); |
| 145 |
SaveMetas(TX3DRootNode(Node)); |
| 146 |
end else |
| 147 |
if Version.Major >= 3 then |
| 148 |
begin |
| 149 |
{ X3D, but no TX3DRootNode. Use default X3D profile. } |
| 150 |
Writer.Writeln('PROFILE ' + DefaultX3DSaveProfile); |
| 151 |
if Generator <> '' then |
| 152 |
Writer.Writeln(Format('META %s %s', |
| 153 |
[ StringToX3DClassic('generator'), |
| 154 |
StringToX3DClassic(Generator)])); |
| 155 |
if Source <> '' then |
| 156 |
Writer.Writeln(Format('META %s %s', |
| 157 |
[ StringToX3DClassic('source'), |
| 158 |
StringToX3DClassic(Source)])); |
| 159 |
Writer.Writeln; |
| 160 |
end else |
| 161 |
if (Generator <> '') or (Source <> '') then |
| 162 |
begin |
| 163 |
{ write Generator and Source as comment } |
| 164 |
PrecedingComment := ''; |
| 165 |
if Generator <> '' then |
| 166 |
PrecedingComment := PrecedingComment + ('# Generated by ' + SReplaceChars(Generator, [#10, #13], ' ') + NL); |
| 167 |
if Source <> '' then |
| 168 |
PrecedingComment := PrecedingComment + ('# Source model: ' + SReplaceChars(Source, [#10, #13], ' ') + NL); |
| 169 |
Writer.Writeln(PrecedingComment); |
| 170 |
end; |
| 171 |
|
| 172 |
{ Node may be TX3DRootNode here, that's OK, |
| 173 |
TX3DRootNode.SaveToStream will magically handle this right. } |
| 174 |
Node.SaveToStream(Writer); |
| 175 |
end; |
| 176 |
|
| 177 |
procedure Xml; |
| 178 |
|
| 179 |
function Profile: string; |
| 180 |
begin |
| 181 |
if (Node is TX3DRootNode) and |
| 182 |
(TX3DRootNode(Node).Profile <> '') then |
| 183 |
Result := TX3DRootNode(Node).Profile else |
| 184 |
Result := DefaultX3DSaveProfile; |
| 185 |
end; |
| 186 |
|
| 187 |
procedure SaveComponents(Root: TX3DRootNode); |
| 188 |
var |
| 189 |
ComponentLevel: {$ifdef FPC}TStringIntegerMap.TDictionaryPair{$else}TPair<string, Integer>{$endif}; |
| 190 |
begin |
| 191 |
for ComponentLevel in Root.Components do |
| 192 |
Writer.WritelnIndent(Format('<component name=%s level="%d" />', |
| 193 |
[ StringToX3DXml(ComponentLevel.Key), |
| 194 |
ComponentLevel.Value ])); |
| 195 |
end; |
| 196 |
|
| 197 |
procedure SaveMetas(Root: TX3DRootNode); |
| 198 |
var |
| 199 |
MetaPair: {$ifdef FPC}TStringStringMap.TDictionaryPair{$else}TPair<string, string>{$endif}; |
| 200 |
begin |
| 201 |
for MetaPair in Root.Meta do |
| 202 |
Writer.WritelnIndent(Format('<meta name=%s content=%s />', |
| 203 |
[ StringToX3DXml(MetaPair.Key), |
| 204 |
StringToX3DXml(MetaPair.Value)])); |
| 205 |
end; |
| 206 |
|
| 207 |
begin |
| 208 |
Writer.Writeln(Format( |
| 209 |
'<?xml version="1.0" encoding="UTF-8"?>' + NL + |
| 210 |
'<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D %d.%d//EN" "http://www.web3d.org/specifications/x3d-%0:d.%1:d.dtd">' + NL+ |
| 211 |
'<X3D profile=%2:s version="%0:d.%1:d"' + NL+ |
| 212 |
' xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"' + NL+ |
| 213 |
' xsd:noNamespaceSchemaLocation="http://www.web3d.org/specifications/x3d-%0:d.%1:d.xsd">' + NL+ |
| 214 |
'<head>', |
| 215 |
[Version.Major, Version.Minor, StringToX3DXml(Profile)])); |
| 216 |
Writer.IncIndent; |
| 217 |
|
| 218 |
if Node is TX3DRootNode then |
| 219 |
begin |
| 220 |
if Generator <> '' then |
| 221 |
TX3DRootNode(Node).Meta.PutPreserve('generator', Generator); |
| 222 |
if Source <> '' then |
| 223 |
TX3DRootNode(Node).Meta.PutPreserve('source', Source); |
| 224 |
SaveComponents(TX3DRootNode(Node)); |
| 225 |
SaveMetas(TX3DRootNode(Node)); |
| 226 |
end else |
| 227 |
begin |
| 228 |
{ write Generator etc. directly, as we don't have TX3DRootNode.Meta available } |
| 229 |
if Generator <> '' then |
| 230 |
Writer.WritelnIndent(Format('<meta name="generator" content=%s />', |
| 231 |
[StringToX3DXml(Generator)])); |
| 232 |
if Source <> '' then |
| 233 |
Writer.WritelnIndent(Format('<meta name="source" content=%s />', |
| 234 |
[StringToX3DXml(Source)])); |
| 235 |
end; |
| 236 |
|
| 237 |
Writer.DecIndent; |
| 238 |
Writer.Writeln('</head>' + NL + '<Scene>'); |
| 239 |
|
| 240 |
{ Node may be TX3DRootNode here, that's OK, |
| 241 |
TX3DRootNode.SaveToStream will magically handle this right. } |
| 242 |
Writer.IncIndent; |
| 243 |
Node.SaveToStream(Writer); |
| 244 |
Writer.DecIndent; |
| 245 |
|
| 246 |
Writer.Writeln('</Scene>' +NL + '</X3D>'); |
| 247 |
end; |
| 248 |
|
| 249 |
begin |
| 250 |
Version := GetSaveVersion(Node); |
| 251 |
|
| 252 |
Writer := TX3DWriterNames.Create(Stream, Version, Encoding); |
| 253 |
try |
| 254 |
case Encoding of |
| 255 |
xeClassic: Classic; |
| 256 |
xeXML: Xml; |
| 257 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 258 |
else raise EInternalError.Create('Saving Encoding?'); |
| 259 |
{$endif} |
| 260 |
end; |
| 261 |
finally FreeAndNil(Writer) end; |
| 262 |
end; |
| 263 |
|
| 264 |
procedure SaveX3DXml(const Node: TX3DRootNode; |
| 265 |
const Stream: TStream; const Generator, Source: string); |
| 266 |
begin |
| 267 |
// optionally call ForceSaveToX3D, when converting VRML (<= 2) -> X3D (>= 3) |
| 268 |
if Node.HasForceVersion and |
| 269 |
(Node.ForceVersion.Major <= 2) then |
| 270 |
Node.ForceSaveAsX3D; |
| 271 |
|
| 272 |
InternalSaveVrmlX3D(Node, Stream, Generator, Source, xeXML); |
| 273 |
end; |
| 274 |
|
| 275 |
{ Saves classic encoding, suitable to save all VRML and X3D versions. } |
| 276 |
procedure SaveX3DClassic(const Node: TX3DRootNode; |
| 277 |
const Stream: TStream; const Generator, Source: string); |
| 278 |
begin |
| 279 |
InternalSaveVrmlX3D(Node, Stream, Generator, Source, xeClassic); |
| 280 |
end; |
| 281 |
|
| 282 |
{ Like SaveX3DClassic, |
| 283 |
but optionally calls ForceSaveToX3D if we convert from VRML <= 2, |
| 284 |
thus it will upgrade VRML <= 2 to X3D >= 3. } |
| 285 |
procedure SaveX3DClassicForceX3D(const Node: TX3DRootNode; |
| 286 |
const Stream: TStream; const Generator, Source: string); |
| 287 |
begin |
| 288 |
// optionally call ForceSaveToX3D, when converting VRML (<= 2) -> X3D (>= 3) |
| 289 |
if Node.HasForceVersion and |
| 290 |
(Node.ForceVersion.Major <= 2) then |
| 291 |
Node.ForceSaveAsX3D; |
| 292 |
SaveX3DClassic(Node, Stream, Generator, Source); |
| 293 |
end; |
| 294 |
|
| 295 |
{$endif read_implementation} |