File: build_3d_object_by_code.lpr

package info (click to toggle)
castle-game-engine 6.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 195,044 kB
  • sloc: pascal: 364,622; ansic: 8,606; java: 2,851; objc: 2,601; cpp: 1,412; xml: 851; makefile: 723; sh: 563; php: 26
file content (100 lines) | stat: -rw-r--r-- 3,470 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
99
100
{
  Copyright 2010-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.

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

{ Simple example how to build a 3D object (which is equivalent to
  VRML/X3D nodes graph) by pure ObjectPascal code.

  Check out latest X3D specification,
  http://www.web3d.org/x3d/specifications/ISO-IEC-19775-1.2-X3D-AbstractSpecification/index.html,
  and our
  http://castle-engine.sourceforge.net/x3d_implementation_status.php
  for a complete list of supported VRML/X3D nodes. }

uses SysUtils, CastleVectors, X3DNodes;

var
  Root: TX3DRootNode;
  Sphere: TSphereNode;
  SphereShape: TShapeNode;
  IndexedFaceSetCoordinate: TCoordinateNode;
  IndexedFaceSet: TIndexedFaceSetNode;
  IndexedFaceSetShape: TShapeNode;
  IndexedFaceSetShapeTranslated: TTransformNode;
  Appearance: TAppearanceNode;
begin
  { This will be used as appearance for both sphere and IndexedFaceSet }
  Appearance := TAppearanceNode.Create;
  Appearance.Material := TMaterialNode.Create;

  Sphere := TSphereNode.Create;
  Sphere.Radius := 2;

  SphereShape := TShapeNode.Create;
  SphereShape.Appearance := Appearance;
  SphereShape.Geometry := Sphere;

  IndexedFaceSetCoordinate := TCoordinateNode.Create;
  IndexedFaceSetCoordinate.SetPoint([
    Vector3(0, 0, 0),
    Vector3(1, 0, 0),
    Vector3(1, 1, 0),
    Vector3(0, 1, 0),

    Vector3(0, 0, 0.5),
    Vector3(1, 0, 0.5),
    Vector3(1, 1, 0.5),
    Vector3(0, 1, 0.5)
  ]);

  IndexedFaceSet := TIndexedFaceSetNode.Create;
  IndexedFaceSet.SetCoordIndex(
    { Two quad faces. These are just indexes for
      the array placed in IndexedFaceSetCoordinate.SetPoint . }
    [0, 1, 2, 3, 0, -1, 4, 5, 6, 7, 4]);
  IndexedFaceSet.Coord := IndexedFaceSetCoordinate;

  IndexedFaceSetShape := TShapeNode.Create;
  IndexedFaceSetShape.Appearance := Appearance;
  IndexedFaceSetShape.Geometry := IndexedFaceSet;

  IndexedFaceSetShapeTranslated := TTransformNode.Create;
  IndexedFaceSetShapeTranslated.Translation := Vector3(2, 0, 0);
  IndexedFaceSetShapeTranslated.AddChildren(IndexedFaceSetShape);

  Root := TX3DRootNode.Create;
  try
    Root.AddChildren(SphereShape);
    Root.AddChildren(IndexedFaceSetShapeTranslated);

    { The Root holds now a tree of your VRML/X3D nodes, thus describing
      your 3D object. You can do whatever you want with it:
      example line below trivially saves Root to VRML/X3D file
      (that can be later loaded by our engine). }
    Save3D(Root, 'test.x3d', 'build_3d_object_by_code');

    { You also could create a TCastleSceneCore, or even TCastleScene
      to render this 3D object directly in OpenGL (without saving to any
      file). For example in scene_manager_basic.lpr you could replace
        Scene.Load(ApplicationData('boxes.x3dv'));
      with
        Scene.Load(Root, false);
      to render your constructed 3D object.

      Note that you can even rebuild and change the 3D object *after*
      it's already loaded and rendered by TCastleScene.
      See demos animate_3d_model_by_code.lpr and animate_3d_model_by_code_2.lpr. }

  finally FreeAndNil(Root) end;
end.