Debian Codegraph @

examples/viewport_and_scenes/mesh_update/code/gameviewmain.pas

raw
1 {
2 Copyright 2024-2024 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 { Main view, where most of the application logic takes place. }
17 unit GameViewMain;
18
19 interface
20
21 uses Classes,
22 CastleVectors, CastleComponentSerialize, CastleTimeUtils,
23 CastleUIControls, CastleControls, CastleKeysMouse, CastleScene,
24 X3DNodes, X3DFields;
25
26 type
27 { Main view, where most of the application logic takes place. }
28 TViewMain = class(TCastleView)
29 published
30 { Components designed using CGE editor.
31 These fields will be automatically initialized at Start. }
32 LabelFps: TCastleLabel;
33 LabelMeshInfo: TCastleLabel;
34 MainScene: TCastleScene;
35 CheckboxWireframe: TCastleCheckbox;
36 CheckboxShader: TCastleCheckbox;
37 private
38 const
39 GridWidth = 100;
40 GridHeight = 100;
41 var
42 CoordinateNode: TCoordinateNode;
43 Time: TFloatTime;
44 EffectAnimateMesh: TEffectNode;
45 EffectAnimateMeshTime: TSFTime;
46 { Update CoordinateNode vertexes by CoordinateNode.SetPoint,
47 which can be called every frame and the new coordinates are efficiently
48 updated on the mesh. }
49 procedure UpdateCoordinateNode;
50 procedure WireframeChange(Sender: TObject);
51 procedure ShaderChange(Sender: TObject);
52 public
53 constructor Create(AOwner: TComponent); override;
54 procedure Start; override;
55 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
56 end;
57
58 var
59 ViewMain: TViewMain;
60
61 implementation
62
63 uses SysUtils,
64 CastleUtils, CastleRenderOptions;
65
66 { TViewMain ----------------------------------------------------------------- }
67
68 constructor TViewMain.Create(AOwner: TComponent);
69 begin
70 inherited;
71 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
72 end;
73
74 procedure TViewMain.UpdateCoordinateNode;
75 var
76 Vertexes: TVector3List;
77 X, Z: Integer;
78 H: Single;
79 begin
80 Vertexes := TVector3List.Create;
81 try
82 Vertexes.Count := (GridWidth + 1) * (GridHeight + 1);
83 for X := 0 to GridWidth do
84 for Z := 0 to GridHeight do
85 begin
86 if CheckboxShader.Checked then
87 begin
88 { When using shaders, the H value doens't matter for display.
89 Our shader will calculate the vertex Y.
90 But it matters for collision detection, so we set it to 0. }
91 H := 0;
92 end else
93 begin
94 // the numbers below are nothing special, just chosen experimentally
95 H := ( Sin((X + Time * 20.0) * 0.5) +
96 Sin((Z + Time * 15.0) * 0.3) ) * 0.5;
97 end;
98 Vertexes[(GridWidth + 1) * Z + X] := Vector3(X, H, Z);
99 end;
100 CoordinateNode.SetPoint(Vertexes);
101 finally FreeAndNil(Vertexes) end;
102 end;
103
104 procedure TViewMain.Start;
105
106 { Add shader effect to Appearance, to be able to animate assosiated shape
107 using shaders.
108 These nodes are only useful when CheckboxShader.Checked.
109 By default they do nothing because we set EffectAnimateMesh.Enabled := false. }
110 procedure AddShaderEffect(const Appearance: TAppearanceNode);
111 var
112 EffectVertexShaderPart: TEffectPartNode;
113 begin
114 { Effect is a container for
115 - shader source code (vertex, geometry, fragment shaders -- though
116 here we will use only vertex shader)
117 - associated "uniforms" (variables that you can set from Pascal code
118 and are accessible in the shader code).
119
120 Effect can be enabled / disabled, and added to particular X3D appearance,
121 or to a group of nodes.
122
123 For more about shader effects :
124 - https://castle-engine.io/compositing_shaders.php
125 - https://castle-engine.io/compositing_shaders_doc/html/
126 - Another example: examples/viewport_and_scenes/shader_effects/ }
127
128 EffectAnimateMesh := TEffectNode.Create;
129 EffectAnimateMesh.Language := slGLSL;
130 EffectAnimateMesh.Enabled := CheckboxShader.Checked; // initially disabled
131
132 EffectAnimateMeshTime := TSFTime.Create(EffectAnimateMesh, true, 'time', Time);
133 EffectAnimateMesh.AddCustomField(EffectAnimateMeshTime);
134
135 EffectVertexShaderPart := TEffectPartNode.Create;
136 EffectVertexShaderPart.ShaderType := stVertex;
137 EffectVertexShaderPart.SetUrl(['castle-data:/animate_mesh.vs']);
138 EffectAnimateMesh.SetParts([EffectVertexShaderPart]);
139
140 Appearance.SetEffects([EffectAnimateMesh]);
141 end;
142
143 { Create X3D nodes that define a grid.
144
145 In particular initialize CoordinateNode that can be later updated
146 as often as we want by "CoordinateNode.SetPoint"
147 (done from our method "UpdateCoordinateNode"). }
148 procedure BuildMainScene;
149 var
150 RootNode: TX3DRootNode;
151 Triangles: TIndexedTriangleSetNode;
152 Indexes: TInt32List;
153 Material: TPhysicalMaterialNode;
154 Appearance: TAppearanceNode;
155 Shape: TShapeNode;
156 X, Z: Integer;
157 begin
158 CoordinateNode := TCoordinateNode.Create;
159 UpdateCoordinateNode;
160
161 { Define triangles to form a grid.
162 We have GridWidth * GridHeight quads, each split into 2 triangles. }
163 Indexes := TInt32List.Create;
164 for X := 1 to GridWidth do
165 for Z := 1 to GridHeight do
166 begin
167 Indexes.Add((GridWidth + 1) * Z + X);
168 Indexes.Add((GridWidth + 1) * (Z - 1) + X);
169 Indexes.Add((GridWidth + 1) * (Z - 1) + X - 1);
170
171 Indexes.Add((GridWidth + 1) * Z + X);
172 Indexes.Add((GridWidth + 1) * (Z - 1) + X - 1);
173 Indexes.Add((GridWidth + 1) * Z + X - 1);
174 end;
175
176 Triangles := TIndexedTriangleSetNode.Create;
177 Triangles.Coord := CoordinateNode;
178 Triangles.SetIndex(Indexes);
179
180 Material := TPhysicalMaterialNode.Create;
181
182 Appearance := TAppearanceNode.Create;
183 Appearance.Material := Material;
184
185 Shape := TShapeNode.Create;
186 Shape.Geometry := Triangles;
187 Shape.Appearance := Appearance;
188
189 RootNode := TX3DRootNode.Create;
190 RootNode.AddChildren(Shape);
191
192 // Adds nodes useful only when CheckboxShader.Checked
193 AddShaderEffect(Appearance);
194
195 MainScene.Load(RootNode, true);
196 MainScene.Translation := Vector3(-GridWidth / 2, 0, -GridHeight / 2);
197
198 Assert(MainScene.TrianglesCount = Indexes.Count div 3);
199 Assert(MainScene.TrianglesCount = GridWidth * GridHeight * 2);
200 end;
201
202 begin
203 inherited;
204
205 CheckboxWireframe.OnChange := {$ifdef FPC}@{$endif} WireframeChange;
206 CheckboxShader.OnChange := {$ifdef FPC}@{$endif} ShaderChange;
207
208 BuildMainScene;
209
210 // show triangles count
211 LabelMeshInfo.Caption := Format('Grid size %d x %d, triangles: %d', [
212 GridWidth,
213 GridHeight,
214 MainScene.TrianglesCount
215 ]);
216 end;
217
218 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
219 begin
220 inherited;
221 { This virtual method is executed every frame (many times per second). }
222 Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code');
223 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
224
225 Time := Time + SecondsPassed;
226
227 if CheckboxShader.Checked then
228 begin
229 { Set the uniform variable "time" accessed by GLSL. }
230 EffectAnimateMeshTime.Send(Time);
231 end else
232 begin
233 { This is called only when CheckboxShader.Checked = false.
234 When the vertexes are calculated by the shader, there's no point in doing
235 UpdateCoordinateNode every frame, there's no point in doing
236 "CoordinateNode.SetPoint " every frame. The mesh stays unmodified from the
237 point of view of the CGE data structures. }
238 UpdateCoordinateNode;
239 end;
240 end;
241
242 procedure TViewMain.WireframeChange(Sender: TObject);
243 begin
244 if CheckboxWireframe.Checked then
245 MainScene.RenderOptions.WireframeEffect := weSolidWireframe
246 else
247 MainScene.RenderOptions.WireframeEffect := weNormal;
248 end;
249
250 procedure TViewMain.ShaderChange(Sender: TObject);
251 begin
252 EffectAnimateMesh.Enabled := CheckboxShader.Checked;
253
254 if CheckboxShader.Checked then
255 begin
256 { When checkbox changed from "no shader" to "shader",
257 call UpdateCoordinateNode once, to make the ground flat
258 (all vertexes at height = 0) from the perspective of the CGE data structures.
259 Only GPU will calculate the actual height of the vertexes. }
260 UpdateCoordinateNode;
261 { Set the initial value for uniform variable "time" accessed by GLSL. }
262 EffectAnimateMeshTime.Send(Time);
263 end;
264 end;
265
266 end.