File: combine_multiple_x3d_into_one.lpr

package info (click to toggle)
castle-game-engine 6.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 194,520 kB
  • sloc: pascal: 364,585; ansic: 8,606; java: 2,851; objc: 2,601; cpp: 1,412; xml: 851; makefile: 725; sh: 563; php: 26
file content (98 lines) | stat: -rw-r--r-- 3,203 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
{
  Copyright 2015-2017 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

{ An example how to load multiple 3D model files into a single X3D graph,
  by combining them into a single X3D tree rooted in TX3DRootNode.
  This is useful for example when auto-generating a complex 3D scene
  by code, from multiple small X3D files.

  If you want to render the complex scene fast, it's better to combine
  it, and use a single TCastleScene, than to use multiple TCastleScenes.
  Using a single TCastleScene means that e.g. frustum culling will be
  efficient.
  See http://castle-engine.sourceforge.net/tutorial_transformation_hierarchy.php .
}
program combine_multiple_x3d_into_one;

uses SysUtils, CastleWindow, CastleSceneCore, CastleScene, X3DLoad, X3DNodes,
  CastleFilesUtils, CastleVectors;

var
  MainRoot, ModelBoxes, ModelRaptor: TX3DRootNode;
  TransformBoxes: TTransformNode;
  TransformRaptor: array [0..2] of TTransformNode;
  Window: TCastleWindow;
  Scene: TCastleScene;
  I: Integer;
begin
  { Create an X3D graph like this:

    MainRoot (TX3DRootNode)
    |- TransformBoxes (TTransformNode)
       |- ModelBoxes (TX3DRootNode, loaded from data/boxes.x3dv)

    |- TransformRaptor[0] (TTransformNode)
       |- ModelRaptor (TX3DRootNode, loaded from data/raptor_1.x3d)

    |- TransformRaptor[1] (TTransformNode)
       |- ModelRaptor (TX3DRootNode, loaded from data/raptor_1.x3d)

    |- TransformRaptor[2] (TTransformNode)
       |- ModelRaptor (TX3DRootNode, loaded from data/raptor_1.x3d)

    Note that the same TCastleScene instance "ModelRaptor" is added
    multiple times to the X3D nodes graph (just with different transformations).
    This is fully supported and valid.
  }

  MainRoot := TX3DRootNode.Create;

  { add ModelBoxes and TransformBoxes }

  ModelBoxes := Load3D(ApplicationData('boxes.x3dv'));

  TransformBoxes := TTransformNode.Create;
  TransformBoxes.Translation := Vector3(-5, 0, 0);
  TransformBoxes.AddChildren(ModelBoxes);

  MainRoot.AddChildren(TransformBoxes);

  { add ModelRaptor and TransformRaptor[0..2] }

  ModelRaptor := Load3D(ApplicationData('raptor_1.x3d'));

  for I := 0 to 2 do
  begin
    TransformRaptor[I] := TTransformNode.Create;
    TransformRaptor[I].Translation := Vector3(8, (I -1)  * 5, 0);
    TransformRaptor[I].AddChildren(ModelRaptor);

    MainRoot.AddChildren(TransformRaptor[I]);
  end;

  { now load and display MainRoot model }

  Scene := TCastleScene.Create(Application);
  Scene.Load(MainRoot, true { Scene owns this MainRoot node });
  Scene.Spatial := [ssRendering, ssDynamicCollisions];
  Scene.ProcessEvents := true;

  Window := TCastleWindow.Create(Application);
  Window.SceneManager.Items.Add(Scene);
  Window.SceneManager.MainScene := Scene;

  Window.Open;
  Application.Run;
end.