| 1 |
{ |
| 2 |
Copyright 2003-2023 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 |
{ Blending management for OpenGL rendering. |
| 17 |
@exclude Internal unit for CastleScene. } |
| 18 |
unit CastleSceneInternalBlending; |
| 19 |
|
| 20 |
{$I castleconf.inc} |
| 21 |
|
| 22 |
interface |
| 23 |
|
| 24 |
uses CastleSceneCore, CastleGLUtils, CastleShapes, CastleSceneInternalShape, |
| 25 |
CastleRenderOptions; |
| 26 |
|
| 27 |
type |
| 28 |
{ Render shapes with blending. |
| 29 |
|
| 30 |
OpenGL state of glDepthMask (RenderContext.DepthBufferUpdate), |
| 31 |
and blending (RenderContext.BlendingEnable, RenderContext.BlendingDisable) |
| 32 |
are controlled by this function. This function will unconditionally |
| 33 |
change (and restore later to original value) this state, |
| 34 |
to perform correct blending (transparency rendering). |
| 35 |
|
| 36 |
To make a correct rendering, always |
| 37 |
render transparent shapes at the end (after all opaque). } |
| 38 |
TBlendingRenderer = class |
| 39 |
private |
| 40 |
Active: Boolean; //< between RenderBegin and RenderEnd |
| 41 |
function DefaultSourceFactor: TBlendingSourceFactor; |
| 42 |
function DefaultDestinationFactor: TBlendingDestinationFactor; |
| 43 |
public |
| 44 |
constructor Create; |
| 45 |
|
| 46 |
{ Start rendering shapes with blending. } |
| 47 |
procedure RenderBegin; |
| 48 |
|
| 49 |
{ Stop rendering shapes with blending. |
| 50 |
It is ignored if RenderBegin was not called earlier. } |
| 51 |
procedure RenderEnd; |
| 52 |
|
| 53 |
{ If we are rendering with blending (between RenderBegin and RenderEnd) |
| 54 |
and this Shape uses blending, |
| 55 |
then determine what blending source/destination factors |
| 56 |
to use for rendering Shape, and set OpenGL state like glBlendFunc. } |
| 57 |
procedure BeforeRenderShape(const Shape: TGLShape; |
| 58 |
const RenderOptions: TCastleRenderOptions); |
| 59 |
end; |
| 60 |
|
| 61 |
{ Fill a TShapeList with only opaque (UseBlending = @false) or |
| 62 |
only transparent shapes (UseBlending = @true). } |
| 63 |
procedure ShapesFilterBlending( |
| 64 |
const Tree: TShapeTree; |
| 65 |
const OnlyActive, OnlyVisible, OnlyCollidable: boolean; |
| 66 |
const TestShapeVisibility: TTestShapeVisibility; |
| 67 |
const FilteredShapes: TShapeList; const UseBlending: boolean); |
| 68 |
|
| 69 |
implementation |
| 70 |
|
| 71 |
uses SysUtils, |
| 72 |
{$ifdef OpenGLES} CastleGLES, {$else} CastleGL, {$endif} |
| 73 |
CastleLog, X3DNodes, CastleScene, CastleTimeUtils, CastleRenderContext, |
| 74 |
CastleColors, CastleVectors; |
| 75 |
|
| 76 |
{ TBlendingRenderer ---------------------------------------------------------- } |
| 77 |
|
| 78 |
constructor TBlendingRenderer.Create; |
| 79 |
begin |
| 80 |
inherited Create; |
| 81 |
end; |
| 82 |
|
| 83 |
function TBlendingRenderer.DefaultSourceFactor: TBlendingSourceFactor; |
| 84 |
begin |
| 85 |
Result := TCastleRenderOptions.DefaultBlendingSourceFactor; |
| 86 |
end; |
| 87 |
|
| 88 |
function TBlendingRenderer.DefaultDestinationFactor: TBlendingDestinationFactor; |
| 89 |
begin |
| 90 |
Result := TCastleRenderOptions.DefaultBlendingDestinationFactor; |
| 91 |
end; |
| 92 |
|
| 93 |
procedure TBlendingRenderer.RenderBegin; |
| 94 |
begin |
| 95 |
Active := true; |
| 96 |
|
| 97 |
RenderContext.DepthBufferUpdate := false; |
| 98 |
RenderContext.BlendingEnable(DefaultSourceFactor, DefaultDestinationFactor); |
| 99 |
end; |
| 100 |
|
| 101 |
procedure TBlendingRenderer.RenderEnd; |
| 102 |
begin |
| 103 |
if not Active then |
| 104 |
Exit; |
| 105 |
Active := false; |
| 106 |
|
| 107 |
{ restore glDepthMask and blending state to default values } |
| 108 |
RenderContext.DepthBufferUpdate := true; |
| 109 |
RenderContext.BlendingDisable; |
| 110 |
end; |
| 111 |
|
| 112 |
procedure TBlendingRenderer.BeforeRenderShape(const Shape: TGLShape; |
| 113 |
const RenderOptions: TCastleRenderOptions); |
| 114 |
|
| 115 |
{ Looks at Scene.RenderOptions.BlendingXxx and Appearance.BlendMode of X3D node. |
| 116 |
If different than currently set, then changes blending mode. } |
| 117 |
|
| 118 |
const |
| 119 |
SrcConstColor = [bsConstantColor, bsOneMinusConstantColor]; |
| 120 |
SrcConstAlpha = [bsConstantAlpha, bsOneMinusConstantAlpha]; |
| 121 |
DestConstColor = [bdConstantColor, bdOneMinusConstantColor]; |
| 122 |
DestConstAlpha = [bdConstantAlpha, bdOneMinusConstantAlpha]; |
| 123 |
var |
| 124 |
B: TBlendModeNode; |
| 125 |
NewSrc: TBlendingSourceFactor; |
| 126 |
NewDest: TBlendingDestinationFactor; |
| 127 |
NewConstColor: TCastleColor; |
| 128 |
begin |
| 129 |
if not Active then |
| 130 |
Exit; |
| 131 |
|
| 132 |
B := Shape.State.BlendMode; |
| 133 |
if B <> nil then |
| 134 |
begin |
| 135 |
NewSrc := B.SrcFactor; |
| 136 |
NewDest := B.DestFactor; |
| 137 |
NewConstColor := Vector4(B.FdColor.Value, 1 - B.FdColorTransparency.Value); |
| 138 |
end else |
| 139 |
begin |
| 140 |
NewSrc := RenderOptions.BlendingSourceFactor; |
| 141 |
NewDest := RenderOptions.BlendingDestinationFactor; |
| 142 |
NewConstColor := White; |
| 143 |
end; |
| 144 |
|
| 145 |
RenderContext.BlendingEnable(NewSrc, NewDest); |
| 146 |
|
| 147 |
{ We track last source/dest factor, but we don't track last constant color/alpha. |
| 148 |
So just set them always, if needed. } |
| 149 |
if ( (NewSrc in SrcConstColor) or (NewDest in DestConstColor) or |
| 150 |
(NewSrc in SrcConstAlpha) or (NewDest in DestConstAlpha) ) and |
| 151 |
GLFeatures.BlendConstant then |
| 152 |
begin |
| 153 |
glBlendColor(NewConstColor.X, NewConstColor.Y, NewConstColor.Z, NewConstColor.W); |
| 154 |
end; |
| 155 |
end; |
| 156 |
|
| 157 |
{ global --------------------------------------------------------------------- } |
| 158 |
|
| 159 |
procedure ShapesFilterBlending( |
| 160 |
const Tree: TShapeTree; |
| 161 |
const OnlyActive, OnlyVisible, OnlyCollidable: boolean; |
| 162 |
const TestShapeVisibility: TTestShapeVisibility; |
| 163 |
const FilteredShapes: TShapeList; const UseBlending: boolean); |
| 164 |
var |
| 165 |
List: TShapeList; |
| 166 |
Shape: TShape; |
| 167 |
I: Integer; |
| 168 |
begin |
| 169 |
FrameProfiler.Start(fmRenderShapesFilterBlending); |
| 170 |
|
| 171 |
{ Use "Count := 0" instead of Clear, this way previous Capacity remains } |
| 172 |
FilteredShapes.Count := 0; |
| 173 |
{ Set Capacity to max value at the beginning, to speed adding items later. } |
| 174 |
FilteredShapes.Capacity := Tree.MaxShapesCount; |
| 175 |
|
| 176 |
List := Tree.TraverseList(OnlyActive, OnlyVisible, OnlyCollidable); |
| 177 |
|
| 178 |
if Assigned(TestShapeVisibility) then |
| 179 |
begin |
| 180 |
for I := 0 to List.Count - 1 do |
| 181 |
begin |
| 182 |
Shape := List[I]; |
| 183 |
if (TGLShape(Shape).UseBlending = UseBlending) and TestShapeVisibility(TGLShape(Shape)) then |
| 184 |
FilteredShapes.Add(Shape); |
| 185 |
end; |
| 186 |
end else |
| 187 |
begin |
| 188 |
for I := 0 to List.Count - 1 do |
| 189 |
begin |
| 190 |
Shape := List[I]; |
| 191 |
if TGLShape(Shape).UseBlending = UseBlending then |
| 192 |
FilteredShapes.Add(Shape); |
| 193 |
end; |
| 194 |
end; |
| 195 |
|
| 196 |
FrameProfiler.Stop(fmRenderShapesFilterBlending); |
| 197 |
end; |
| 198 |
|
| 199 |
end. |