/usr/src/castle-game-engine-5.2.0/x3d/opengl/castlerendererinternallights.pas is in castle-game-engine-src 5.2.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | {
Copyright 2003-2014 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ VRML/X3D lights OpenGL rendering. Internal for CastleRenderer. @exclude }
unit CastleRendererInternalLights;
{$I castleconf.inc}
interface
uses CastleVectors, CastleGLUtils, X3DNodes, CastleRendererInternalShader;
type
{ Modify light's properties of the light right before it's rendered.
Currently, you can modify only the "on" state.
By default, LightOn is the value of Light.LightNode.FdOn field.
You can change it if you want. }
TLightRenderEvent = procedure (const Light: TLightInstance;
var LightOn: boolean) of object;
{ Render lights to OpenGL.
Sets OpenGL lights properties, enabling and disabling them as needed.
This is smart, avoiding to configure the same light many times.
Remembers what VRML/X3D light is set on which OpenGL light,
and this way avoids needless reconfiguring of OpenGL lights.
This may speed up rendering, avoiding changing OpenGL state when not
necessary.
Assumes that nothing changes OpenGL light properties during our
lifetime. }
TVRMLGLLightsRenderer = class
private
FLightRenderEvent: TLightRenderEvent;
LightsKnown: boolean;
LightsDone: array of PLightInstance;
function NeedRenderLight(Index: Integer; Light: PLightInstance): boolean;
public
{ Statistics of how many OpenGL light setups were done
(Statistics[true]) vs how many were avoided (Statistics[false]).
The second number should usually be much higher, prooving
that using a cache of LightsDone inside this class was useful. }
Statistics: array [boolean] of Cardinal;
constructor Create(const ALightRenderEvent: TLightRenderEvent);
{ Set OpenGL lights properties.
Sets OpenGL fixed-function pipeline lights,
enabling and disabling them as needed.
Lights are also passed to TShader, calling appropriate
TShader.EnableLight methods. So shader pipeline is also dealt with.
Lights1 and Lights2 lists are simply glued inside.
Lights2 may be @nil (equal to being empty). }
procedure Render(const Lights1, Lights2: TLightInstancesList;
const Shader: TShader);
{ Process light source properties right before rendering the light.
This event, if assigned, must be deterministic,
based purely on light properties. For example, it's Ok to
make LightRenderEvent that turns off lights that have shadowVolumes = TRUE.
It is @italic(not Ok) to make LightRenderEvent that sets LightOn to
a random boolean value. That because caching here assumes that
for the same Light values, LightRenderEvent will set LightOn the same. }
property LightRenderEvent: TLightRenderEvent read FLightRenderEvent;
end;
implementation
uses SysUtils, CastleUtils, Math, CastleRenderingCamera, CastleGL;
{ Set and enable OpenGL light properties based on VRML/X3D light.
Requires that current OpenGL matrix is modelview.
Always preserves the matrix value (by using up to one modelview
matrix stack slot).
We do not examine Light.LightNode.FdOn.Value here.
We make no assumptions about the previous state of this OpenGL light.
We simply always set all the parameters to fully define the required
light behavior. Some light parameters may not be set, but only because
they are not used --- for example, if a light is not a spot light,
then we set GL_SPOT_CUTOFF to 180 (indicates that light has no spot),
but don't necessarily set GL_SPOT_DIRECTION or GL_SPOT_EXPONENT
(as OpenGL will not use them anyway). }
procedure glLightFromVRMLLight(glLightNum: Integer; const Light: TLightInstance);
{$ifndef OpenGLES}
procedure SetupDirectionalLight(LightNode: TAbstractDirectionalLightNode);
begin
glLighti(glLightNum, GL_SPOT_CUTOFF, 180);
end;
procedure SetupPointLight(LightNode: TAbstractPointLightNode);
begin
glLighti(glLightNum, GL_SPOT_CUTOFF, 180);
end;
procedure SetupSpotLight_1(LightNode: TSpotLightNode_1);
begin
glLightv(glLightNum, GL_SPOT_DIRECTION, LightNode.FdDirection.Value);
glLightf(glLightNum, GL_SPOT_EXPONENT, LightNode.SpotExponent);
glLightf(glLightNum, GL_SPOT_CUTOFF, LightNode.SpotCutoffDeg);
end;
procedure SetupSpotLight(LightNode: TSpotLightNode);
begin
glLightv(glLightNum, GL_SPOT_DIRECTION, LightNode.FdDirection.Value);
glLightf(glLightNum, GL_SPOT_EXPONENT, LightNode.SpotExponentApproximate);
glLightf(glLightNum, GL_SPOT_CUTOFF, LightNode.SpotCutoffDeg);
end;
var
SetNoAttenuation: boolean;
Attenuat: TVector3Single;
Color3, AmbientColor3: TVector3f;
Color4, AmbientColor4: TVector4f;
begin
glLightNum += GL_LIGHT0;
glPushMatrix;
try
if Light.WorldCoordinates then
glLoadMatrix(RenderingCamera.Matrix);
glMultMatrix(Light.Transform);
glLightv(glLightNum, GL_POSITION, Light.Node.Position);
if Light.Node is TAbstractDirectionalLightNode then
SetupDirectionalLight(TAbstractDirectionalLightNode(Light.Node)) else
if Light.Node is TAbstractPointLightNode then
SetupPointLight(TAbstractPointLightNode(Light.Node)) else
if Light.Node is TSpotLightNode_1 then
SetupSpotLight_1(TSpotLightNode_1(Light.Node)) else
if Light.Node is TSpotLightNode then
SetupSpotLight(TSpotLightNode(Light.Node)) else
raise EInternalError.Create('Unknown light node class');
{ setup attenuation for OpenGL light }
SetNoAttenuation := true;
if (Light.Node is TAbstractPositionalLightNode) then
begin
Attenuat := TAbstractPositionalLightNode(Light.Node).FdAttenuation.Value;
if not ZeroVector(Attenuat) then
begin
SetNoAttenuation := false;
glLightf(glLightNum, GL_CONSTANT_ATTENUATION, Attenuat[0]);
glLightf(glLightNum, GL_LINEAR_ATTENUATION, Attenuat[1]);
glLightf(glLightNum, GL_QUADRATIC_ATTENUATION, Attenuat[2]);
end;
end;
if SetNoAttenuation then
begin
{ lights with no Attenuation field or with Attenuation = (0, 0, 0)
get default Attenuation = (1, 0, 0) }
glLightf(glLightNum, GL_CONSTANT_ATTENUATION, 1);
glLightf(glLightNum, GL_LINEAR_ATTENUATION, 0);
glLightf(glLightNum, GL_QUADRATIC_ATTENUATION, 0);
end;
finally glPopMatrix end;
{ calculate Color4 = light color * light intensity }
Color3 := Light.Node.FdColor.Value * Light.Node.FdIntensity.Value;
Color4 := Vector4Single(Color3, 1);
{ calculate AmbientColor4 = light color * light ambient intensity }
if Light.Node.FdAmbientIntensity.Value < 0 then
AmbientColor4 := Color4 else
begin
AmbientColor3 := Light.Node.FdColor.Value *
Light.Node.FdAmbientIntensity.Value;
AmbientColor4 := Vector4Single(AmbientColor3, 1);
end;
glLightv(glLightNum, GL_AMBIENT, AmbientColor4);
glLightv(glLightNum, GL_DIFFUSE, Color4);
glLightv(glLightNum, GL_SPECULAR, Color4);
glEnable(glLightNum);
{$else}
begin
{ We pass light information to shaders in our own uniform variables,
this is handled by CastleRendererShader. }
{$endif}
end;
{ TVRMLGLLightsRenderer ----------------------------------------------- }
constructor TVRMLGLLightsRenderer.Create(
const ALightRenderEvent: TLightRenderEvent);
begin
inherited Create;
FLightRenderEvent := ALightRenderEvent;
LightsKnown := false;
SetLength(LightsDone, GLFeatures.MaxLights);
end;
function TVRMLGLLightsRenderer.NeedRenderLight(Index: Integer; Light: PLightInstance): boolean;
begin
Result := not (
LightsKnown and
( { Light Index is currently disabled, and we want it disabled: Ok. }
( (LightsDone[Index] = nil) and
(Light = nil) )
or
{ Light Index is currently enabled, and we want it enabled,
with the same LightNode and Transform: Ok.
(Other TLightInstance record properties are calculated from
LightNode and Transform, so no need to compare them). }
( (LightsDone[Index] <> nil) and
(Light <> nil) and
(LightsDone[Index]^.Node = Light^.Node) and
(MatricesPerfectlyEqual(
LightsDone[Index]^.Transform, Light^.Transform)) )
));
if Result then
{ Update LightsDone[Index], if change required. }
LightsDone[Index] := Light;
Inc(Statistics[Result]);
end;
procedure TVRMLGLLightsRenderer.Render(
const Lights1, Lights2: TLightInstancesList;
const Shader: TShader);
var
LightsEnabled: Cardinal;
procedure AddList(Lights: TLightInstancesList);
var
I: Integer;
LightOn: boolean;
Light: PLightInstance;
begin
for I := 0 to Lights.Count - 1 do
begin
Light := Addr(Lights.L[I]);
LightOn := Light^.Node.FdOn.Value;
if Assigned(LightRenderEvent) then
LightRenderEvent(Light^, LightOn);
if LightOn then
begin
if NeedRenderLight(LightsEnabled, Light) then
glLightFromVRMLLight(LightsEnabled, Light^);
Shader.EnableLight(LightsEnabled, Light);
Inc(LightsEnabled);
if LightsEnabled >= GLFeatures.MaxLights then Exit;
end;
end;
end;
{$ifndef OpenGLES}
var
I: Integer;
{$endif}
begin
LightsEnabled := 0;
if LightsEnabled >= GLFeatures.MaxLights then Exit;
AddList(Lights1);
if LightsEnabled >= GLFeatures.MaxLights then Exit;
if Lights2 <> nil then
begin
AddList(Lights2);
if LightsEnabled >= GLFeatures.MaxLights then Exit;
end;
{$ifndef OpenGLES}
{ Disable remaining light for fixed-function pipeline }
for I := LightsEnabled to GLFeatures.MaxLights - 1 do
if NeedRenderLight(I, nil) then
glDisable(GL_LIGHT0 + I);
{$endif}
LightsKnown := true;
end;
end.
|