~ubuntu-branches/ubuntu/utopic/castle-game-engine/utopic

« back to all changes in this revision

Viewing changes to examples/3d_rendering_processing/fog_culling.lpr

  • Committer: Package Import Robot
  • Author(s): Abou Al Montacir
  • Date: 2013-04-27 18:06:40 UTC
  • Revision ID: package-import@ubuntu.com-20130427180640-eink4nmwzuivez1c
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{
 
2
  Copyright 2003-2013 Michalis Kamburelis.
 
3
 
 
4
  This file is part of "Castle Game Engine".
 
5
 
 
6
  "Castle Game Engine" is free software; see the file COPYING.txt,
 
7
  included in this distribution, for details about the copyright.
 
8
 
 
9
  "Castle Game Engine" is distributed in the hope that it will be useful,
 
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
12
 
 
13
  ----------------------------------------------------------------------------
 
14
}
 
15
 
 
16
{ Demo of fog culling. When rendering with fog turned "on" (the default),
 
17
  we do not render objects outside of the fog visibility radius.
 
18
  This can make a hude speedup if you have dense fog and exterior-like level
 
19
  (where frustum culing leaves too many shapes visible).
 
20
 
 
21
  This always loads models/fog_culling_final.x3dv X3D file.
 
22
  Be sure to run it with proper current directory (examples/3d_rendering_processing/).
 
23
  It's a crafted scene, with some green and dense fog and a lot of
 
24
  spheres scattered around. Fog culling will work best on it.
 
25
 
 
26
  Handles keys:
 
27
    'f' turns fog culling on/off
 
28
    F5 makes a screenshot
 
29
}
 
30
 
 
31
program fog_culling;
 
32
 
 
33
uses SysUtils, CastleVectors, GL, GLU, CastleWindow, CastleStringUtils,
 
34
  CastleClassUtils, CastleUtils, Classes, CastleWarnings,
 
35
  CastleGLUtils, X3DNodes, CastleSceneCore, CastleScene,
 
36
  CastleProgress, CastleProgressConsole, CastleFilesUtils, Castle3D,
 
37
  CastleSceneManager, CastleParameters, CastleRenderingCamera, CastleKeysMouse;
 
38
 
 
39
var
 
40
  Window: TCastleWindowCustom;
 
41
  Scene: TCastleScene;
 
42
 
 
43
type
 
44
  TMySceneManager = class(TCastleSceneManager)
 
45
  private
 
46
    function TestFogVisibility(Shape: TGLShape): boolean;
 
47
  protected
 
48
    procedure Render3D(const Params: TRenderParams); override;
 
49
    procedure RenderFromViewEverything; override;
 
50
  end;
 
51
 
 
52
var
 
53
  SceneManager: TMySceneManager;
 
54
 
 
55
function TMySceneManager.TestFogVisibility(Shape: TGLShape): boolean;
 
56
begin
 
57
  { Test for collision between two spheres.
 
58
    1st is the bounding sphere of Shape.
 
59
    2nd is the sphere around current camera position,
 
60
      with the radius taken from fog scaled visibilityRadius.
 
61
    If there is no collision than we don't have to render given Shape. }
 
62
  Result := PointsDistanceSqr(Shape.BoundingSphereCenter, Camera.GetPosition) <=
 
63
      Sqr(Scene.FogStack.Top.FdVisibilityRange.Value * 
 
64
          Scene.FogStack.Top.TransformScale +
 
65
        Sqrt(Shape.BoundingSphereRadiusSqr));
 
66
end;
 
67
 
 
68
var
 
69
  FogCulling: boolean = true;
 
70
 
 
71
procedure TMySceneManager.Render3D(const Params: TRenderParams);
 
72
begin
 
73
  if FogCulling then
 
74
    Scene.Render(@TestFogVisibility, RenderingCamera.Frustum, Params) else
 
75
    inherited;
 
76
end;
 
77
 
 
78
procedure TMySceneManager.RenderFromViewEverything;
 
79
begin
 
80
  inherited;
 
81
  Writeln(Format('Rendered Shapes: %d / %d (fog culling: %s)',
 
82
    [ Statistics.ShapesRendered, Statistics.ShapesVisible,
 
83
      BoolToStr[FogCulling] ]));
 
84
end;
 
85
 
 
86
procedure Open(Window: TCastleWindowBase);
 
87
begin
 
88
  { We use quite large triangles for fog_culling level demo wall.
 
89
    This means that fog must be correctly rendered,
 
90
    with perspective correction hint, otherwise ugly artifacts
 
91
    will be visible. }
 
92
  glHint(GL_FOG_HINT, GL_NICEST);
 
93
end;
 
94
 
 
95
procedure Press(Window: TCastleWindowBase; const Event: TInputPressRelease);
 
96
var
 
97
  FogNode: TFogNode;
 
98
begin
 
99
  if Event.IsKey(K_F) then
 
100
  begin
 
101
    FogCulling := not FogCulling;
 
102
 
 
103
    { Also, turn on/off actual fog on the model (if any).
 
104
      We do it by changing Fog.VisibilityRange (0 means no fog). }
 
105
    FogNode := Scene.FogStack.Top as TFogNode;
 
106
    if FogNode <> nil then
 
107
      if FogCulling then
 
108
        FogNode.FdVisibilityRange.Send(30) else
 
109
        FogNode.FdVisibilityRange.Send(0);
 
110
 
 
111
    Window.PostRedisplay;
 
112
  end;
 
113
end;
 
114
 
 
115
begin
 
116
  Parameters.CheckHigh(0);
 
117
 
 
118
  Window := TCastleWindowCustom.Create(Application);
 
119
 
 
120
  SceneManager := TMySceneManager.Create(Application);
 
121
  Window.Controls.Add(SceneManager);
 
122
 
 
123
  Scene := TCastleScene.Create(Application);
 
124
  OnWarning := @OnWarningWrite;
 
125
  Scene.Load('models' + PathDelim + 'fog_culling_final.x3dv');
 
126
  SceneManager.MainScene := Scene;
 
127
  SceneManager.Items.Add(Scene);
 
128
 
 
129
  Writeln(Scene.Info(true, true, false));
 
130
 
 
131
  { build octrees }
 
132
  Progress.UserInterface := ProgressConsoleInterface;
 
133
  Scene.TriangleOctreeProgressTitle := 'Building triangle octree';
 
134
  Scene.ShapeOctreeProgressTitle := 'Building Shape octree';
 
135
  Scene.Spatial := [ssRendering, ssDynamicCollisions];
 
136
 
 
137
  Window.OnOpen := @Open;
 
138
  Window.OnPress := @Press;
 
139
  Window.SetDemoOptions(K_F11, CharEscape, true);
 
140
  Window.OpenAndRun;
 
141
end.