Debian Codegraph @

tests/code/testcases/testcastleopeningandrendering3d.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestOpeningAndRendering3D" -*-
2 {
3 Copyright 2010-2023 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 { Test TCastleScene opening and rendering various files. }
18 unit TestCastleOpeningAndRendering3D;
19
20 { Test also all models found in
21
22 ../../demo-models
23 ../../castle/data
24 ../../www/htdocs
25
26 This will make the test take *much* longer time,
27 TestOpeningAndRendering3D will then open + render a lot of models. }
28 {.$define CASTLE_TEST_DEMO_MODELS}
29
30 interface
31
32 uses CastleTester, CastleFilesUtils, CastleFindFiles,
33 CastleWindow, CastleSceneCore, CastleScene, CastleViewport;
34
35 type
36 TTestOpeningAndRendering3D = class(TCastleTestCase)
37 private
38 { Available only during Test1 }
39 Window: TCastleWindow;
40 Viewport: TCastleAutoNavigationViewport;
41 Scene: TCastleScene;
42 RecreateSceneEachTime: boolean;
43
44 { FileName empty means to load empty scene. }
45 procedure TestScene(const FileName: string);
46 procedure TestSceneFromEnum(const FileInfo: TFileInfo; var StopSearch: boolean);
47 { If RecreateSceneEachTime, Scene will be destroyed and then created
48 again before each load.
49
50 Both values make sense for testing:
51
52 false checks that pure "Load" properly deals (clears) with
53 previously loaded content, so it mostly checks BeforeNodesFree
54 and ChangedAll.
55
56 true checks that destructions properly deals with a scene. }
57 procedure TestOpenAndRender(const ARecreateSceneEachTime: boolean);
58 published
59 procedure Test1;
60 end;
61
62 implementation
63
64 uses SysUtils, StrUtils,
65 CastleUtils, CastleGLUtils, CastleGLVersion, CastleLog, CastleApplicationProperties,
66 CastleTransform, CastleInternalGLUtils, CastleStringUtils;
67
68 procedure TTestOpeningAndRendering3D.TestScene(const FileName: string);
69 begin
70 if RecreateSceneEachTime then
71 begin
72 //Write('Recreating scene... ');
73
74 FreeAndNil(Scene);
75 AssertTrue(Viewport.Items.MainScene = nil);
76 { the only remaining things should be TCastleCamera }
77 AssertEquals(1, Viewport.Items.Count);
78 AssertTrue(Viewport.Items[0] is TCastleCamera);
79
80 Scene := TCastleScene.Create(Window);
81 Scene.PreciseCollisions := true;
82 Scene.ProcessEvents := true;
83
84 Viewport := TCastleSceneManager.Create(Window);
85 Viewport.Items.Add(Scene);
86 Viewport.Items.MainScene := Scene;
87 end;
88
89 //Writeln('Testing "' + FileName + '"');
90
91 if FileName = '' then
92 Scene.Load(nil, true) else
93 Scene.Load(FileName);
94
95 Viewport.Navigation.Free;
96 // camera should be nil now (thanks to free notification),
97 // and no new camera should be automatically created yet.
98 AssertTrue(Viewport.Navigation = nil);
99 Viewport.RequiredNavigation;
100
101 Viewport.Navigation := nil;
102 AssertTrue(Viewport.Navigation = nil);
103
104 { Force preparing and using OpenGL resources for the scene.
105 This way we also check that next Load frees them Ok. }
106 Window.Container.EventBeforeRender;
107 Window.Container.EventRender;
108
109 { Check OpenGL errors now, otherwise they could be detected by an unrelated
110 CheckGLErrors call later. This way we know what 3D filename caused
111 the error. }
112 CheckGLErrors;
113 end;
114
115
116 procedure TTestOpeningAndRendering3D.TestSceneFromEnum(const FileInfo: TFileInfo; var StopSearch: boolean);
117 var
118 ParentDirName: string;
119 begin
120 { While our masks do not allow such files,
121 but searching on Windows can find xxx.x3dv~ when only *.x3dv is requested.
122 So explicitly avoid them (as they will fail to load in CGE, as unrecognized).
123
124 TODO: This is now fixed at CastleFindFiles level, see DOUBLE_CHECK_WILDCARD
125 define.
126 Extra check here shall not be necessary. }
127 if IsWild(FileInfo.Name, '*~', true) then Exit;
128
129 { do not check files in "errors" subdir, these are known to cause trouble }
130 ParentDirName := ExtractFileName(ExclPathDelim(ExtractFileDir(FileInfo.AbsoluteName)));
131 if ParentDirName = 'errors' then Exit;
132
133 TestScene(FileInfo.AbsoluteName);
134 end;
135
136 procedure TTestOpeningAndRendering3D.TestOpenAndRender(const ARecreateSceneEachTime: boolean);
137
138 procedure TestScenesInDir(const Path: string);
139
140 procedure DoMask(const Mask: string);
141 begin
142 FindFiles(Path, Mask, false, {$ifdef FPC}@{$endif}TestSceneFromEnum, [ffRecursive]);
143 end;
144
145 begin
146 DoMask('*.wrl');
147 DoMask('*.wrz');
148 DoMask('*.wrl.gz');
149 DoMask('*.x3d');
150 DoMask('*.x3dz');
151 DoMask('*.x3d.gz');
152 DoMask('*.x3dv');
153 DoMask('*.x3dvz');
154 DoMask('*.x3dv.gz');
155 DoMask('*.3ds');
156 DoMask('*.dae');
157 end;
158
159 begin
160 if not CanCreateWindowForTest then
161 Exit;
162
163 RecreateSceneEachTime := ARecreateSceneEachTime;
164
165 Window := CreateWindowForTest;
166 try
167 Scene := TCastleScene.Create(Window);
168 Scene.PreciseCollisions := true;
169 Scene.ProcessEvents := true;
170
171 Viewport := TCastleAutoNavigationViewport.Create(Window);
172 Viewport.Items.Add(Scene);
173 Viewport.Items.MainScene := Scene;
174
175 Window.Controls.InsertFront(Viewport);
176 Window.Open;
177
178 TestScene('');
179 TestScenesInDir('data');
180
181 {$ifdef CASTLE_TEST_DEMO_MODELS}
182 TestScenesInDir('..' + PathDelim + '..' + PathDelim + 'demo-models');
183 TestScenesInDir('..' + PathDelim + '..' + PathDelim + 'castle' + PathDelim + 'data');
184 TestScenesInDir('..' + PathDelim + '..' + PathDelim + 'www' + PathDelim + 'htdocs');
185 {$endif CASTLE_TEST_DEMO_MODELS}
186
187 ApplicationProperties.OnWarning.Add({$ifdef FPC}@{$endif}OnWarningRaiseException);
188 try
189 { e.g. tests that auto_normals_indexed_geometry.x3dv makes no warnings when generating
190 arrays for rendering. }
191 TestScene('castle-data:/auto_normals_indexed_geometry.x3dv');
192 TestScene('castle-data:/auto_normals_indexed_geometry_full.obj');
193 finally
194 ApplicationProperties.OnWarning.Remove({$ifdef FPC}@{$endif}OnWarningRaiseException);
195 end;
196
197 Window.Close;
198 finally DestroyWindowForTest(Window) end;
199 end;
200
201 procedure TTestOpeningAndRendering3D.Test1;
202 begin
203 if not CanCreateWindowForTest then
204 Exit;
205
206 TestOpenAndRender(false);
207 TestOpenAndRender(true);
208 end;
209
210 initialization
211 RegisterTest(TTestOpeningAndRendering3D);
212 end.