File: testcastleopeningandrendering3d.pas

package info (click to toggle)
castle-game-engine 7.0~alpha.3%2Bdfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 969,476 kB
  • sloc: pascal: 911,523; javascript: 28,186; cpp: 14,157; xml: 9,939; ansic: 9,229; java: 3,653; objc: 2,737; sh: 1,214; makefile: 657; php: 65; lisp: 21; ruby: 8
file content (212 lines) | stat: -rw-r--r-- 6,538 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// -*- compile-command: "./test_single_testcase.sh TTestOpeningAndRendering3D" -*-
{
  Copyright 2010-2023 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.

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

{ Test TCastleScene opening and rendering various files. }
unit TestCastleOpeningAndRendering3D;

{ Test also all models found in

    ../../demo-models
    ../../castle/data
    ../../www/htdocs

  This will make the test take *much* longer time,
  TestOpeningAndRendering3D will then open + render a lot of models. }
{.$define CASTLE_TEST_DEMO_MODELS}

interface

uses CastleTester, CastleFilesUtils, CastleFindFiles,
  CastleWindow, CastleSceneCore, CastleScene, CastleViewport;

type
  TTestOpeningAndRendering3D = class(TCastleTestCase)
  private
    { Available only during Test1 }
    Window: TCastleWindow;
    Viewport: TCastleAutoNavigationViewport;
    Scene: TCastleScene;
    RecreateSceneEachTime: boolean;

    { FileName empty means to load empty scene. }
    procedure TestScene(const FileName: string);
    procedure TestSceneFromEnum(const FileInfo: TFileInfo; var StopSearch: boolean);
    { If RecreateSceneEachTime, Scene will be destroyed and then created
      again before each load.

      Both values make sense for testing:

      false checks that pure "Load" properly deals (clears) with
      previously loaded content, so it mostly checks BeforeNodesFree
      and ChangedAll.

      true checks that destructions properly deals with a scene. }
    procedure TestOpenAndRender(const ARecreateSceneEachTime: boolean);
  published
    procedure Test1;
  end;

implementation

uses SysUtils, StrUtils,
  CastleUtils, CastleGLUtils, CastleGLVersion, CastleLog, CastleApplicationProperties,
  CastleTransform, CastleInternalGLUtils, CastleStringUtils;

procedure TTestOpeningAndRendering3D.TestScene(const FileName: string);
begin
  if RecreateSceneEachTime then
  begin
    //Write('Recreating scene... ');

    FreeAndNil(Scene);
    AssertTrue(Viewport.Items.MainScene = nil);
    { the only remaining things should be TCastleCamera }
    AssertEquals(1, Viewport.Items.Count);
    AssertTrue(Viewport.Items[0] is TCastleCamera);

    Scene := TCastleScene.Create(Window);
    Scene.PreciseCollisions := true;
    Scene.ProcessEvents := true;

    Viewport := TCastleSceneManager.Create(Window);
    Viewport.Items.Add(Scene);
    Viewport.Items.MainScene := Scene;
  end;

  //Writeln('Testing "' + FileName + '"');

  if FileName = '' then
    Scene.Load(nil, true) else
    Scene.Load(FileName);

  Viewport.Navigation.Free;
  // camera should be nil now (thanks to free notification),
  // and no new camera should be automatically created yet.
  AssertTrue(Viewport.Navigation = nil);
  Viewport.RequiredNavigation;

  Viewport.Navigation := nil;
  AssertTrue(Viewport.Navigation = nil);

  { Force preparing and using OpenGL resources for the scene.
    This way we also check that next Load frees them Ok. }
  Window.Container.EventBeforeRender;
  Window.Container.EventRender;

  { Check OpenGL errors now, otherwise they could be detected by an unrelated
    CheckGLErrors call later. This way we know what 3D filename caused
    the error. }
  CheckGLErrors;
end;


procedure TTestOpeningAndRendering3D.TestSceneFromEnum(const FileInfo: TFileInfo; var StopSearch: boolean);
var
  ParentDirName: string;
begin
  { While our masks do not allow such files,
    but searching on Windows can find xxx.x3dv~ when only *.x3dv is requested.
    So explicitly avoid them (as they will fail to load in CGE, as unrecognized).

    TODO: This is now fixed at CastleFindFiles level, see DOUBLE_CHECK_WILDCARD
    define.
    Extra check here shall not be necessary. }
  if IsWild(FileInfo.Name, '*~', true) then Exit;

  { do not check files in "errors" subdir, these are known to cause trouble }
  ParentDirName := ExtractFileName(ExclPathDelim(ExtractFileDir(FileInfo.AbsoluteName)));
  if ParentDirName = 'errors' then Exit;

  TestScene(FileInfo.AbsoluteName);
end;

procedure TTestOpeningAndRendering3D.TestOpenAndRender(const ARecreateSceneEachTime: boolean);

  procedure TestScenesInDir(const Path: string);

    procedure DoMask(const Mask: string);
    begin
      FindFiles(Path, Mask, false, {$ifdef FPC}@{$endif}TestSceneFromEnum, [ffRecursive]);
    end;

  begin
    DoMask('*.wrl');
    DoMask('*.wrz');
    DoMask('*.wrl.gz');
    DoMask('*.x3d');
    DoMask('*.x3dz');
    DoMask('*.x3d.gz');
    DoMask('*.x3dv');
    DoMask('*.x3dvz');
    DoMask('*.x3dv.gz');
    DoMask('*.3ds');
    DoMask('*.dae');
  end;

begin
  if not CanCreateWindowForTest then
    Exit;

  RecreateSceneEachTime := ARecreateSceneEachTime;

  Window := CreateWindowForTest;
  try
    Scene := TCastleScene.Create(Window);
    Scene.PreciseCollisions := true;
    Scene.ProcessEvents := true;

    Viewport := TCastleAutoNavigationViewport.Create(Window);
    Viewport.Items.Add(Scene);
    Viewport.Items.MainScene := Scene;

    Window.Controls.InsertFront(Viewport);
    Window.Open;

    TestScene('');
    TestScenesInDir('data');

    {$ifdef CASTLE_TEST_DEMO_MODELS}
    TestScenesInDir('..' + PathDelim + '..' + PathDelim + 'demo-models');
    TestScenesInDir('..' + PathDelim + '..' + PathDelim + 'castle' + PathDelim + 'data');
    TestScenesInDir('..' + PathDelim + '..' + PathDelim + 'www' + PathDelim + 'htdocs');
    {$endif CASTLE_TEST_DEMO_MODELS}

    ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException);
    try
      { e.g. tests that auto_normals_indexed_geometry.x3dv makes no warnings when generating
        arrays for rendering. }
      TestScene('castle-data:/auto_normals_indexed_geometry.x3dv');
      TestScene('castle-data:/auto_normals_indexed_geometry_full.obj');
    finally
      ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException);
    end;

    Window.Close;
  finally DestroyWindowForTest(Window) end;
end;

procedure TTestOpeningAndRendering3D.Test1;
begin
  if not CanCreateWindowForTest then
    Exit;

  TestOpenAndRender(false);
  TestOpenAndRender(true);
end;

initialization
  RegisterTest(TTestOpeningAndRendering3D);
end.