Debian Codegraph @

tests/code/testcases/testcastleinternalglshadowvolumes.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestCastleInternalGLShadowVolumes" -*-
2 {
3 Copyright 2023-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 unit TestCastleInternalGLShadowVolumes;
18
19 interface
20
21 uses
22 Classes, SysUtils,
23 CastleTester;
24
25 type
26 TTestCastleInternalGLShadowVolumes = class(TCastleTestCase)
27 published
28 procedure TestCasterShadowPossiblyVisible;
29 end;
30
31 implementation
32
33 uses CastleTransform, CastleFrustum, CastleInternalGLShadowVolumes, CastleProjection,
34 CastleVectors, CastleViewport, CastleBoxes;
35
36 procedure TTestCastleInternalGLShadowVolumes.TestCasterShadowPossiblyVisible;
37 var
38 Viewport: TCastleViewport;
39 Camera: TCastleCamera;
40 Projection: TProjection;
41 Frustum: TFrustum;
42 SvRenderer: TGLShadowVolumeRenderer;
43 begin
44 Viewport := TCastleViewport.Create(nil);
45
46 Camera := Viewport.Camera;
47 { stand at (10, 0, 0), looking in -Z, up in +Y }
48 Camera.SetView(
49 Vector3(10, 0, 0),
50 Vector3(0, 0, -1),
51 Vector3(0, 1, 0));
52
53 FillChar(Projection, SizeOf(Projection), #0);
54 Projection.ProjectionType := ptPerspective;
55 Projection.PerspectiveAnglesRad := Vector2(Pi / 3, Pi / 3);
56 Projection.ProjectionNear := 1;
57 Projection.ProjectionFar := ZFarInfinity;
58
59 Frustum.Init(Projection.Matrix(1), Camera.Matrix);
60 AssertTrue(Frustum.FarInfinity);
61
62 SvRenderer := TGLShadowVolumeRenderer.Create;
63 try
64 // point light test
65 SvRenderer.InitFrustumAndLight(Frustum, Vector4(-10, 0, 0, 1));
66 AssertTrue(SvRenderer.GetCasterShadowPossiblyVisible(
67 Box3D(Vector3(-1, -1, -1), Vector3(1, 1, 1)).Transform(TranslationMatrix(0, 0, 0))
68 ));
69 AssertTrue(SvRenderer.GetCasterShadowPossiblyVisible(
70 Box3D(Vector3(-1, -1, -1), Vector3(1, 1, 1)).Transform(TranslationMatrix(-10, 0, 0))
71 ));
72 AssertTrue(SvRenderer.GetCasterShadowPossiblyVisible(
73 Box3D(Vector3(-1, -1, -1), Vector3(1, 1, 1)).Transform(TranslationMatrix(10, 0, 0))
74 ));
75 // too far to the right to cast shadows
76 AssertFalse(SvRenderer.GetCasterShadowPossiblyVisible(
77 Box3D(Vector3(-1, -1, -1), Vector3(1, 1, 1)).Transform(TranslationMatrix(20, 0, 0))
78 ));
79
80 // directional light test
81 SvRenderer.InitFrustumAndLight(Frustum, Vector4(1, 0, 0, 0));
82 AssertTrue(SvRenderer.GetCasterShadowPossiblyVisible(
83 Box3D(Vector3(-1, -1, -1), Vector3(1, 1, 1)).Transform(TranslationMatrix(0, 0, 0))
84 ));
85 AssertTrue(SvRenderer.GetCasterShadowPossiblyVisible(
86 Box3D(Vector3(-1, -1, -1), Vector3(1, 1, 1)).Transform(TranslationMatrix(-10, 0, 0))
87 ));
88 AssertTrue(SvRenderer.GetCasterShadowPossiblyVisible(
89 Box3D(Vector3(-1, -1, -1), Vector3(1, 1, 1)).Transform(TranslationMatrix(10, 0, 0))
90 ));
91 // too far to the right to cast shadows
92 AssertFalse(SvRenderer.GetCasterShadowPossiblyVisible(
93 Box3D(Vector3(-1, -1, -1), Vector3(1, 1, 1)).Transform(TranslationMatrix(20, 0, 0))
94 ));
95 finally
96 FreeAndNil(SvRenderer);
97 FreeAndNil(Viewport);
98 end;
99 end;
100
101 initialization
102 RegisterTest(TTestCastleInternalGLShadowVolumes);
103 end.