Debian Codegraph @

src/ui/castlecontrols_mask.inc

raw
1 {%MainUnit castlecontrols.pas}
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 {$ifdef read_interface}
18 { Effect of mask over rendering, used by @link(TCastleMask.MaskRendering). }
19 TMaskRendering = (
20 mrChildrenWhereNoMask,
21 mrChildrenWhereMask,
22 mrChildrenIgnoreMask,
23 mrMask
24 );
25
26 { Limit the visibility of children by an arbitrary (defined using an arbitrary UI) mask.
27
28 The children controls are visible only where the @link(Mask) is rendered,
29 or where the mask is not rendered.
30 You can customize the mask effect using @link(MaskRendering).
31
32 Limitation of the current implementation:
33 You cannot have other controls
34 underneath TCastleMask (inside @link(Mask) or inside other children) that
35 deal with stencil buffer.
36 In practice, it means that you cannot use TCastleMask within another TCastleMask,
37 and you cannot use TCastleViewport with shadow volumes within TCastleMask.
38 Set @link(TCastleViewport.ShadowVolumes) to @false to make @link(TCastleViewport)
39 reliable inside a TCastleMask.
40 TODO: This limitation may be removed one day, it's possible by using distinct
41 stencil buffer bits (masks). }
42 TCastleMask = class(TCastleUserInterface)
43 strict private
44 FMask: TCastleUserInterface;
45 FMaskRendering: TMaskRendering;
46 procedure SetMaskRendering(const Value: TMaskRendering);
47 protected
48 procedure InternalRenderLoop(const ViewportRect: TRectangle); override;
49 public
50 constructor Create(AOwner: TComponent); override;
51 destructor Destroy; override;
52 function PropertySections(const PropertyName: String): TPropertySections; override;
53 published
54 { Place in @link(Mask) any UI control (TCastleUserInterface) to limit
55 where the children are rendered.
56
57 The colors of the mask UI don't matter, but the pixels that are touched by the mask matter.
58 So you can e.g. define a mask using an image (TCastleImageControl),
59 make sure to set @link(TCastleImageControl.AlphaChannel) to acTest to make the mask apply
60 where the image is opaque.
61 Or you can define a mask using a viewport with 2D or 3D scenes -- TCastleViewport,
62 with @link(TCastleViewport.Transparent).
63 Or you can define a mask using a shape, like TCastleRectangleControl or TCastleShape.
64
65 The @link(Mask) is a subcomponent with @link(TCastleUserInterface.FullSize) = @true by default,
66 so it fills the entire TCastleMask by default.
67
68 The children in @link(Mask) are never rendered to the color or depth buffers
69 (unless MaskRendering is mrMask).
70 They are only rendered to set the stencil buffer. }
71 property Mask: TCastleUserInterface read FMask;
72
73 { How children of this control are limited by the mask.
74
75 @unorderedList(
76 @item(mrChildrenWhereNoMask (default):
77
78 Children are rendered only where mask is @italic(not) visible.
79
80 This is the default, and it means that when mask is empty
81 (also the default state) then the children are rendered as usual.
82 )
83
84 @item(mrChildrenWhereMask:
85
86 Children are rendered only where mask is visible.)
87
88 @item(mrChildrenIgnoreMask:
89
90 Turns off mask usage, effectively making TCastleMask a regular UI control
91 and the contents of @link(Mask) meaningless.)
92
93 @item(mrMask:
94
95 Render only mask, as a regular UI control.
96 Other children are not rendered.
97 This mode is useful to design mask at design-time, where it may be easier
98 to just see mask UI explicitly.)
99 )
100
101 Note that mrChildrenWhereNoMask and mrChildrenWhereMask values only work when there
102 is a stencil buffer available.
103 If the stencil buffer is not available, they behave as if
104 @link(MaskRendering) = mrChildrenIgnoreMask, so the mask will not do anything.
105 All modern GPUs provide a stencil buffer, and CGE requests stencil buffer by default.
106 Check @code(GLFeatures.StencilBits <> 0) if you want to react to lack of stencil buffer
107 in some particular way in your code. }
108 property MaskRendering: TMaskRendering read FMaskRendering write SetMaskRendering
109 default mrChildrenWhereNoMask;
110 end;
111
112 {$endif read_interface}
113
114 {$ifdef read_implementation}
115
116 { TCastleMask -------------------------------------------------------------- }
117
118 constructor TCastleMask.Create(AOwner: TComponent);
119 begin
120 inherited;
121
122 FMask := TCastleUserInterface.Create(Self);
123 FMask.SetSubComponent(true);
124 FMask.FullSize := true;
125 FMask.Name := 'Mask';
126 InsertFront(FMask);
127 end;
128
129 destructor TCastleMask.Destroy;
130 begin
131 inherited;
132 end;
133
134 function TCastleMask.PropertySections(const PropertyName: String): TPropertySections;
135 begin
136 if ArrayContainsString(PropertyName, [
137 'Mask', 'MaskRendering'
138 ]) then
139 Result := [psBasic]
140 else
141 Result := inherited PropertySections(PropertyName);
142 end;
143
144 procedure TCastleMask.SetMaskRendering(const Value: TMaskRendering);
145 begin
146 if FMaskRendering <> Value then
147 begin
148 FMaskRendering := Value;
149 VisibleChange([chRender]);
150 end;
151 end;
152
153 procedure TCastleMask.InternalRenderLoop(const ViewportRect: TRectangle);
154 var
155 SavedDepthBufferUpdate, SavedInternalClearColorsByDraw: Boolean;
156 SavedColorChannels: TColorChannels;
157 I: Integer;
158 begin
159 { Early test for special case when we render Mask for debug.
160 We do this regardless of GLFeatures.StencilBits, so check this special case first. }
161 if MaskRendering = mrMask then
162 begin
163 Mask.InternalRecursiveRender(ViewportRect);
164 Exit;
165 end;
166
167 { Early test for special case when we don't use Mask. }
168 if (MaskRendering = mrChildrenIgnoreMask) or (GLFeatures.StencilBits = 0) then
169 begin
170 for I := 0 to ControlsCount - 1 do
171 if Controls[I] <> Mask then
172 Controls[I].InternalRecursiveRender(ViewportRect);
173 Exit;
174 end;
175
176 { The usual rendering mode: mrChildrenWhereMask or mrChildrenWhereNoMask.
177 Render mask into stencil buffer, then render the rest with stencil test.
178
179 Note that we don't try to save/restore the OpenGL state of
180 glStencilFunc, glStencilOp.
181 All CGE operations using stencil buffer must set this state
182 before rendering: this applies to TCastleMask and TGLShadowVolumeRenderer. }
183
184 RenderContext.Clear([cbStencil], { ClearColor is meaningless for clearing [cbStencil] } TVector4.Zero);
185
186 glEnable(GL_STENCIL_TEST);
187
188 { render Mask to set stencil buffer }
189
190 SavedDepthBufferUpdate := RenderContext.DepthBufferUpdate;
191 SavedColorChannels := RenderContext.ColorChannels;
192
193 { Don't write anything to depth or color buffers. }
194 RenderContext.DepthBufferUpdate := false;
195 RenderContext.ColorChannels := [];
196
197 glStencilFunc(GL_ALWAYS, 1, 0);
198 { GL_REPLACE means to set stencil to 1 ("ref" argument passed to glStencilFunc)
199 where the stencil and depth test pass }
200 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
201 Mask.InternalRecursiveRender(ViewportRect);
202
203 RenderContext.DepthBufferUpdate := SavedDepthBufferUpdate;
204 RenderContext.ColorChannels := SavedColorChannels;
205
206 { render other children, honoring the stencil buffer }
207
208 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); // do not modify stencil anymore
209 case MaskRendering of
210 mrChildrenWhereMask : glStencilFunc(GL_EQUAL, 1, 1);
211 mrChildrenWhereNoMask: glStencilFunc(GL_NOTEQUAL, 1, 1);
212 else raise EInternalError.Create('MaskRendering?');
213 end;
214
215 { Using InternalClearColorsByDraw = true means that solid-color background done
216 by TCastleViewport (in case it is in TCastleMask children) is properly masked by stencil. }
217 SavedInternalClearColorsByDraw := RenderContext.InternalClearColorsByDraw;
218 RenderContext.InternalClearColorsByDraw := true;
219
220 for I := 0 to ControlsCount - 1 do
221 if Controls[I] <> Mask then
222 Controls[I].InternalRecursiveRender(ViewportRect);
223
224 RenderContext.InternalClearColorsByDraw := SavedInternalClearColorsByDraw;
225
226 glDisable(GL_STENCIL_TEST);
227 end;
228
229 {$endif read_implementation}