Debian Codegraph @

src/transform/castletransform_renderparams.inc

raw
1 {%MainUnit castletransform.pas}
2 {
3 Copyright 2010-2024 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 { Rendering parameters (TRenderParams and friends). }
17
18 {$ifdef read_interface}
19
20 { Statistics about what was rendered during last frame.
21 You will usually access this by viewport property,
22 see @link(TCastleViewport.Statistics). }
23 TRenderStatistics = record
24 { How many shapes were rendered (send to OpenGL)
25 versus all shapes that were potentially visible.
26 Potentially visible shapes are the ones with
27 TShape.Visible inside a TCastleTransform with TCastleTransform.Exists.
28
29 When ShapesRendered is much smaller than ShapesVisible,
30 it means that various algorithm(s) designed to reject invisible
31 scene parts do a good job.
32 This includes per-shape frustum culling (@link(TCastleScene.ShapeFrustumCulling)),
33 distance culling (@link(TCastleScene.DistanceCulling)),
34 occlusion culling (see @link(TCastleViewport.OcclusionCulling)). }
35 ShapesRendered, ShapesVisible: Cardinal;
36
37 { Subset of shapes counted in ShapesRendered that have been rendered using
38 blending (partial transparency). }
39 ShapesRenderedBlending: Cardinal;
40
41 { The number of shapes that were not rendered,
42 but their bounding box was rendered to check occlusion culling.
43 This is always zero when not using occlusion culling
44 (@link(TCastleViewport.OcclusionCulling)).
45 Basically, this measures the "invisible overhead" of occlusion culling. }
46 BoxesOcclusionQueriedCount: Cardinal;
47
48 { TCastleScene rendered vs all visible. }
49 ScenesRendered, ScenesVisible: Cardinal;
50
51 { Draw calls done, in case of OpenGL this literally counts the number of calls
52 to glDrawElements and glDrawArrays. }
53 DrawCalls: Cardinal;
54
55 { Present the statistics information as a string (without any newlines).
56 Display it e.g. by assigning to TCastleLabel.Caption. }
57 function ToString: String;
58 end;
59
60 PRenderStatistics = ^TRenderStatistics;
61
62 { @exclude Internal.
63
64 List of lights.
65 Always TLightInstancesList, but we cannot declare it here as such. }
66 TAbstractLightInstancesList = TObject;
67
68 { @exclude Internal.
69
70 Fog node. Always TFogNode, but we cannot declare it here as such. }
71 TAbstractFogNode = TObject;
72
73 { @exclude Internal.
74
75 Indicates rendering target for @link(TRenderingCamera.Target). }
76 TRenderTarget = (
77 { Normal rendering. }
78 rtScreen,
79 { Rendering color buffer contents to a texture inside RenderedTexture node. }
80 rfRenderedTexture,
81 { Rendering color buffer contents to a texture inside GeneratedCubeMap node. }
82 rtCubeMapEnvironment,
83 { Rendering depth buffer contents to shadow map inside GeneratedShadowMap node. }
84 rtShadowMap,
85 { Rendering with a special VSM shader to capture shadow map texture
86 (in the normal color buffer). }
87 rtVarianceShadowMap
88 );
89
90 { @exclude Internal.
91 Rendering pass, for multi-pass rendering, like for shadow volumes. }
92 TInternalRenderingPass = 0..1;
93
94 { Rendering pass, for user purposes.
95 Useful to keep shaders cached when you render the same scene multiple times
96 in the same frame (under different lighting conditions or other things
97 that change shaders). }
98 TUserRenderingPass = 0..2;
99
100 { @exclude Internal.
101
102 Current camera used for rendering, used for @link(TRenderParams.RenderingCamera).
103 This is part of TRenderParams information,
104 and (like the rest of TRenderParams) it is "mostly" internal.
105 If you're using the engine in a normal way, you should never need
106 to deal with this class.
107
108 This is interesting to you only if you write custom rendering code.
109
110 In normal applications, you shoud only get/set camera using TCastleCamera
111 descendants, through @link(TCastleViewport.Camera) and related properties.
112 Do not use the TRenderingCamera class in normal applications. }
113 TRenderingCamera = class
114 strict private
115 FTarget: TRenderTarget;
116 public
117 Camera: TCastleCamera;
118
119 { Current camera position, direction and up.
120 In the world coordinates (coordinates of whole TCastleViewport.Items). }
121 View: TViewVectors;
122
123 { Current camera matrix. Transforms from world space (normal 3D space)
124 to camera space (camera space is the space where you're always
125 standing on zero point, looking in -Z, and so on).
126
127 This is needed for various things, like
128 TextureCoordinateGenerator.mode = "WORLDSPACE*" or generating
129 Viewpoint.camera[Inverse]Matrix event.
130
131 Always after changing this, change also all other camera
132 fields. }
133 Matrix: TMatrix4;
134
135 { Inverse of @link(Matrix).
136
137 Always call InverseMatrixNeeded before using it,
138 InverseMatrixNeeded will check InverseMatrixDone
139 and eventually will calculate inverse and set InverseMatrixDone to
140 @true. }
141 InverseMatrix: TMatrix4;
142 InverseMatrixDone: boolean;
143
144 { Camera rotation matrix. That is, this is like @link(Matrix) but
145 it doesn't move the camera, only rotates it.
146
147 It's guaranteed that this is actually only 3x3 matrix,
148 the 4th row and 4th column are all zero except the lowest right item
149 which is 1.0. }
150 RotationMatrix: TMatrix4;
151
152 { Use RotationMatrix as camera matrix for rendering. }
153 RotationOnly: boolean;
154
155 { Inverse of RotationMatrix.
156
157 Always call RotationInverseMatrixNeeded before using it,
158 RotationInverseMatrixNeeded will check RotationInverseMatrixDone
159 and eventually will calculate inverse and set RotationInverseMatrixDone to
160 @true. }
161 RotationInverseMatrix: TMatrix4;
162 RotationInverseMatrixDone: boolean;
163
164 Frustum: TFrustum;
165
166 procedure InverseMatrixNeeded;
167 procedure RotationInverseMatrixNeeded;
168
169 { Camera rotation matrix, as a 3x3 matrix. }
170 function RotationMatrix3: TMatrix3;
171 function RotationInverseMatrix3: TMatrix3;
172
173 { Set all properties (except Target)
174 from TCastleCamera instance in ACamera.
175 See @link(FromMatrix) for comments about @link(Target) property. }
176 procedure FromCameraObject(const ACamera: TCastleCamera);
177
178 { Set all properties (except Target) to reflect given
179 camera translation (position), direction, up.
180 ProjectionMatrix is needed to calculate frustum.
181 Sets @link(Camera) to @nil. }
182 procedure FromViewVectors(
183 const ATranslation, ADirection, AUp: TVector3;
184 const ProjectionMatrix: TMatrix4); overload;
185 procedure FromViewVectors(
186 const AViewVectors: TViewVectors;
187 const ProjectionMatrix: TMatrix4); overload;
188
189 property Target: TRenderTarget read FTarget write FTarget;
190
191 procedure Assign(const Source: TRenderingCamera);
192
193 { Current matrix to use for rendering. Either @link(Matrix) or @link(RotationMatrix),
194 depending on @link(RotationOnly). }
195 function CurrentMatrix: TMatrix4;
196 end;
197
198 { Information that a TCastleTransform object needs to render.
199 Read-only for @link(TCastleTransform.LocalRender)
200 (except @link(Statistics), which should be updated during rendering).
201
202 @bold(This is mostly an internal class).
203
204 You should not need to create it,
205 you should not need to read anything inside or deal with this class otherwise,
206 and actually you should not need to override
207 @link(TCastleTransform.LocalRender) during normal engine usage.
208 But it may be useful for special customized rendering. }
209 TRenderParams = class
210 { Which parts should be rendered: opaque (@false) or transparent (@true).
211 This should "filter" the rendered parts by @link(TCastleTransform.LocalRender). }
212 Transparent: boolean;
213
214 { Should we render parts that may receive shadow volumes, or ones that don't, or both.
215 This should never be [].
216 During rendering, simply check does it match TCastleScene.ReceiveShadowVolumes.
217 This should "filter" the rendered parts by @link(TCastleTransform.LocalRender). }
218 ShadowVolumesReceivers: set of Boolean;
219
220 { If @true, means that we're using multi-pass
221 shadowing technique (like shadow volumes),
222 and currently doing the "shadowed" pass.
223
224 Which means that most lights (ones with shadowVolumes = TRUE)
225 should be turned off, see [https://castle-engine.io/x3d_extensions.php#section_ext_shadows].) }
226 InShadow: boolean;
227
228 { Value > 0 means we're inside some stencil test (like for
229 InShadow = @false pass of shadow volumes). }
230 StencilTest: Cardinal;
231
232 { Rendering pass number, for internal multi-pass rendering,
233 like for shadow volumes. }
234 InternalPass: TInternalRenderingPass;
235
236 { Rendering pass number, for user purposes.
237 Multiplied with InternalPass to get final pass number,
238 that determines shaders used. }
239 UserPass: TUserRenderingPass;
240
241 { Transformation that should be applied to the rendered result. }
242 Transformation: PTransformation;
243
244 { Current rendering statistics, should be updated by each
245 @link(TCastleTransform.LocalRender) call. }
246 Statistics: TRenderStatistics;
247
248 { Fog that affects all scenes. }
249 GlobalFog: TAbstractFogNode;
250
251 { Camera frustum in local coordinates. Local for the TCastleTransform instance
252 receiving this TRenderParams as @link(TCastleTransform.LocalRender)
253 parameter, so it may be different from RenderingCamera.Frustum.
254
255 As a special exception, it can be @nil, but only when TCastleScene.LocalRender
256 is called (this happens from TBackgroundScene.Render).
257 The TCastleScene.LocalRender should ignore frustum when it is @nil. }
258 Frustum: PFrustum;
259
260 { Camera information for renderer. }
261 RenderingCamera: TRenderingCamera;
262
263 { Are we traversing now only inside parents with MeaningfulParent,
264 i.e. outside of TCastleTransformReference.Reference. }
265 InMeaningfulParent: Boolean;
266
267 { At design-time, cameras update their own projection, to show camera gizmo.
268 These are parameters to do this. }
269 ProjectionBoxWithoutGizmos, ProjectionBoxWithGizmos: TBox3DEvent;
270 ProjectionViewportWidth, ProjectionViewportHeight: Single;
271
272 { Always TShapesCollector at actual rendering. }
273 Collector: TObject;
274
275 { Always TRenderer, to prepare shapes.
276 This is useful to prepare shapes (rendering them with renderer
277 creates VBOs, shaders, and makes them "ready" for next fast rendering).
278
279 Note that shapes and scenes *should not* get associated with this TRenderer
280 instance when being prepared.
281 Shapes and scenes should remain independent of TRenderer instance,
282 to easily change them to a different renderer (like a differe TCastleViewport).
283 So this TRenderer instance is provided to be used, but do not keep
284 anywhere in shapes / scenes a reference to it.
285
286 See CastleInternalRenderer for more comments how are things associated. }
287 RendererToPrepareShapes: TObject;
288
289 { Depth range that should be used for rendering. }
290 DepthRange: TDepthRange;
291
292 constructor Create;
293
294 { Lights that shine on object (headlight and lights from all scenes with CastGlobalLights). }
295 function GlobalLights: TAbstractLightInstancesList; virtual; abstract;
296 end;
297
298 { Information that a TCastleTransform object needs to prepare rendering.
299
300 This is an internal class.
301
302 You should not need to create it,
303 you should not need to read anything inside or deal with this class otherwise.
304
305 The only usage allowed, but deprecated, is to pass an instance of this class
306 taken from @link(TCastleViewport.PrepareParams)
307 as a parameter to @link(TCastleTransform.PrepareResources).
308 You should treat this class as a "black box" in normal applications. }
309 TPrepareParams = class
310 { Include a headlight and all lights that shine on all
311 scenes (thanks to @link(TCastleScene.CastGlobalLights)).
312
313 It is not necessary to define this (it may be @nil).
314 And all the lighting is dynamic, so of course you can always
315 turn on / off things like a headlight during the game.
316 However, passing here the appropriate lights will mean that the shaders
317 are immediately prepared for the current lighting. }
318 GlobalLights: TAbstractLightInstancesList;
319
320 { World fog, in any, to prepare for. }
321 GlobalFog: TAbstractFogNode;
322
323 { Always TRenderer, to prepare shapes.
324 This is useful to prepare shapes (rendering them with renderer
325 creates VBOs, shaders, and makes them "ready" for next fast rendering).
326
327 Note that shapes and scenes *should not* get associated with this TRenderer
328 instance when being prepared.
329 See @link(TRenderParams.RendererToPrepareShapes). }
330 RendererToPrepareShapes: TObject;
331 end;
332
333 {$endif read_interface}
334
335 {$ifdef read_implementation}
336
337 { TRenderStatistics ---------------------------------------------------------- }
338
339 function TRenderStatistics.ToString: String;
340 var
341 OcclusionStats: String;
342 begin
343 if BoxesOcclusionQueriedCount <> 0 then
344 OcclusionStats := Format(' (+ %d boxes for occlusion query)', [
345 BoxesOcclusionQueriedCount
346 ])
347 else
348 OcclusionStats := '';
349
350 Result := Format('Shapes %d (with blending: %d)%s / %d, Scenes: %d / %d, Draw calls: %d', [
351 ShapesRendered,
352 ShapesRenderedBlending,
353 OcclusionStats,
354 ShapesVisible,
355 ScenesRendered,
356 ScenesVisible,
357 DrawCalls
358 ]);
359 end;
360
361 { TRenderingCamera --------------------------------------------------------------- }
362
363 procedure TRenderingCamera.InverseMatrixNeeded;
364 begin
365 if not InverseMatrixDone then
366 begin
367 if not Matrix.TryInverse(InverseMatrix) then
368 begin
369 InverseMatrix := TMatrix4.Identity;
370 WritelnLogMultiline('Camera', 'Camera matrix cannot be inverted, conversions between world and camera space will not be done. Camera matrix is: ' +
371 Matrix.ToRawString(' '));
372 end;
373 InverseMatrixDone := true;
374 end;
375 end;
376
377 procedure TRenderingCamera.RotationInverseMatrixNeeded;
378 begin
379 if not RotationInverseMatrixDone then
380 begin
381 if not RotationMatrix.TryInverse(RotationInverseMatrix) then
382 begin
383 RotationInverseMatrix := TMatrix4.Identity;
384 WritelnLogMultiline('Camera', 'Camera rotation matrix cannot be inverted, conversions between world and camera space will not be done. Camera matrix is: ' +
385 RotationMatrix.ToRawString(' '));
386 end;
387 RotationInverseMatrixDone := true;
388 end;
389 end;
390
391 function TRenderingCamera.RotationMatrix3: TMatrix3;
392 begin
393 Move(RotationMatrix.Data[0], Result.Data[0], SizeOf(Single) * 3);
394 Move(RotationMatrix.Data[1], Result.Data[1], SizeOf(Single) * 3);
395 Move(RotationMatrix.Data[2], Result.Data[2], SizeOf(Single) * 3);
396 end;
397
398 function TRenderingCamera.RotationInverseMatrix3: TMatrix3;
399 begin
400 Move(RotationInverseMatrix.Data[0], Result.Data[0], SizeOf(Single) * 3);
401 Move(RotationInverseMatrix.Data[1], Result.Data[1], SizeOf(Single) * 3);
402 Move(RotationInverseMatrix.Data[2], Result.Data[2], SizeOf(Single) * 3);
403 end;
404
405 procedure TRenderingCamera.FromCameraObject(const ACamera: TCastleCamera);
406 begin
407 Camera := ACamera;
408 View := ACamera.WorldView;
409 Matrix := ACamera.Matrix;
410 InverseMatrixDone := false;
411 RotationMatrix := ACamera.RotationMatrix;
412 RotationInverseMatrixDone := false;
413 Frustum := ACamera.Frustum;
414 end;
415
416 procedure TRenderingCamera.FromViewVectors(
417 const ATranslation, ADirection, AUp: TVector3;
418 const ProjectionMatrix: TMatrix4);
419 var
420 V: TViewVectors;
421 begin
422 V.Translation := ATranslation;
423 V.Direction := ADirection;
424 V.Up := AUp;
425 FromViewVectors(V, ProjectionMatrix);
426 end;
427
428 procedure TRenderingCamera.FromViewVectors(
429 const AViewVectors: TViewVectors;
430 const ProjectionMatrix: TMatrix4);
431 begin
432 Camera := nil;
433 View := AViewVectors;
434 Matrix := View.Matrix;
435 InverseMatrixDone := false;
436 RotationMatrix := View.RotationMatrix;
437 RotationInverseMatrixDone := false;
438 Frustum.Init(ProjectionMatrix, Matrix);
439 end;
440
441 procedure TRenderingCamera.Assign(const Source: TRenderingCamera);
442 begin
443 FTarget := Source.FTarget ;
444 Matrix := Source.Matrix ;
445 InverseMatrix := Source.InverseMatrix ;
446 InverseMatrixDone := Source.InverseMatrixDone ;
447 RotationMatrix := Source.RotationMatrix ;
448 RotationOnly := Source.RotationOnly ;
449 RotationInverseMatrix := Source.RotationInverseMatrix ;
450 RotationInverseMatrixDone := Source.RotationInverseMatrixDone;
451 Frustum := Source.Frustum ;
452 end;
453
454 function TRenderingCamera.CurrentMatrix: TMatrix4;
455 begin
456 if RotationOnly then
457 Result := RotationMatrix
458 else
459 Result := Matrix;
460 end;
461
462 { TRenderParams -------------------------------------------------------------- }
463
464 var
465 GlobalIdentityTransform: TTransformation;
466
467 constructor TRenderParams.Create;
468 begin
469 inherited;
470 Transformation := @GlobalIdentityTransform;
471 InMeaningfulParent := true;
472 end;
473
474 {$endif read_implementation}