| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2022 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 |
|
| 19 |
{ Light source instance in the scene. References VRML/X3D |
| 20 |
light source node (see @link(Node)), and keeps track of light source |
| 21 |
transformation in the 3D world. For the sake of speed |
| 22 |
also stores a couple of light's properties already multiplied |
| 23 |
by the transformation. |
| 24 |
|
| 25 |
This record may be initialized only by TAbstractLightNode.CreateLightInstance. |
| 26 |
Update it (when transform changes) by TAbstractLightNode.UpdateLightInstance. } |
| 27 |
TLightInstance = record |
| 28 |
Node: TAbstractLightNode; |
| 29 |
|
| 30 |
Transform: TMatrix4; |
| 31 |
TransformScale: Single; |
| 32 |
|
| 33 |
{ Light location, already transformed by the @link(Transform) matrix. |
| 34 |
For TAbstractPositionalLightNode lights. } |
| 35 |
Location: TVector3; |
| 36 |
|
| 37 |
{ Light direction, already normalized and |
| 38 |
transformed by the @link(Transform) matrix. |
| 39 |
For spot and directional lights. } |
| 40 |
Direction: TVector3; |
| 41 |
|
| 42 |
{ Light radius, already transformed by the @link(Transform) matrix. |
| 43 |
For lights with radius (positional lights in VRML >= 2.0, |
| 44 |
that is TAbstractPositionalLightNode with HasRadius = true). } |
| 45 |
Radius: Single; |
| 46 |
|
| 47 |
{ Are light location, direction (in this class and inside Node fields) |
| 48 |
expressed in world coordinates. If not, they are expressed in scene |
| 49 |
coordinates. |
| 50 |
|
| 51 |
This matters if you render the scene using TCastleScene, |
| 52 |
and transform it (by wrapping it in TCastleTransform, or by directly |
| 53 |
changing TCastleScene transformation properties inherited from |
| 54 |
TCastleTransform). |
| 55 |
By default (WorldCoordinates = false) we assume that light is defined |
| 56 |
in scene space, so it will be transformed by the whole modelview matrix |
| 57 |
(camera matrix with scene transformations). |
| 58 |
When this is true, during rendering we take care to transform this light |
| 59 |
only by camera matrix (not additional scene transformation). } |
| 60 |
WorldCoordinates: boolean; |
| 61 |
|
| 62 |
{ Position expressed in homogeneous coordinates. |
| 63 |
For positional lights, the last component is always 1. |
| 64 |
For directional lights, the last component is always 0. } |
| 65 |
function Position: TVector4; |
| 66 |
|
| 67 |
{ Light contribution to the specified vertex color. |
| 68 |
This can be used by software renderers (ray-tracers etc.) |
| 69 |
to calculate pixel color following VRML/X3D specifications. |
| 70 |
TX3DGraphTraverseState.Emission should be added to |
| 71 |
TLightInstance.Contribution (for each light), |
| 72 |
and resulting color should be processed by TFogNode.ApplyFog. |
| 73 |
|
| 74 |
We do not clamp color components to (0, 1). This would be a waste of time, |
| 75 |
you should clamp only at the end (or never). This also allows |
| 76 |
to multiply / accumulate values outside of the (0, 1) range |
| 77 |
during calculations. OpenGL also clamps only at the end. |
| 78 |
|
| 79 |
MainTextureColor is color of the "main" texture. |
| 80 |
See @link(TAppearanceNode.MainTexture) for definition what is this texture. } |
| 81 |
function Contribution( |
| 82 |
const Point: TVector3; const PointPlaneNormal: TVector3; |
| 83 |
State: TX3DGraphTraverseState; |
| 84 |
const CamPosition: TVector3; |
| 85 |
const MainTextureColor: TCastleColorRGB): TVector3; |
| 86 |
end; |
| 87 |
PLightInstance = ^TLightInstance; |
| 88 |
|
| 89 |
TLightInstancesList = class({$ifdef FPC}specialize{$endif} TStructList<TLightInstance>) |
| 90 |
public |
| 91 |
{ Find given light node. Return -1 if not found. } |
| 92 |
function IndexOfNode(Node: TAbstractLightNode): integer; |
| 93 |
{ Find light with given node name. Return @nil if not found. } |
| 94 |
function FindName(NodeName: string): PLightInstance; |
| 95 |
function Equals(SecondValue: TObject): boolean; {$ifdef TOBJECT_HAS_EQUALS} override; {$endif} |
| 96 |
end; |
| 97 |
|
| 98 |
{$endif read_interface} |
| 99 |
|
| 100 |
{$ifdef read_implementation} |
| 101 |
|
| 102 |
{ TLightInstance ------------------------------------------------------------- } |
| 103 |
|
| 104 |
function TLightInstance.Contribution( |
| 105 |
const Point: TVector3; const PointPlaneNormal: TVector3; |
| 106 |
State: TX3DGraphTraverseState; |
| 107 |
const CamPosition: TVector3; |
| 108 |
const MainTextureColor: TCastleColorRGB): TVector3; |
| 109 |
|
| 110 |
{ Note: In some cases we have to do something different than VRML 2.0/X3D spec: |
| 111 |
|
| 112 |
@unorderedList( |
| 113 |
@item( |
| 114 |
For VRML 1.0 SpotLight, we have to calculate spot light differently |
| 115 |
(because VRML 1.0 SpotLight gives me dropOffRate instead of |
| 116 |
beamWidth), so we use spot factor equation following OpenGL equations.) |
| 117 |
|
| 118 |
@item( |
| 119 |
VRML 97 lighting equations suggest one-sided lighting, only where |
| 120 |
the normal points out. In my opinion, one-sided lighting is not useful, |
| 121 |
and also our OpenGL rendering uses two-sides lighting. |
| 122 |
(One reason for OpenGL rendering is to integrate nicely with flat mirrors, |
| 123 |
where you have to flip normals. So OpenGL renderer always gives |
| 124 |
vectors from CCW, and so uses two-side to not favor any side of the face.)) |
| 125 |
) |
| 126 |
} |
| 127 |
|
| 128 |
var |
| 129 |
{ kierunek od Point do zrodla swiatla, normalized } |
| 130 |
LightDirNorm: TVector3; |
| 131 |
{ wektor normalny do powierzchni (skierowany w strone Light) } |
| 132 |
Normal: TVector3; |
| 133 |
|
| 134 |
function CalculateSpot_1(const SpotLight: TSpotLightNode_1): Single; |
| 135 |
var |
| 136 |
CosSpotAngle, SpotAngle: Single; |
| 137 |
begin |
| 138 |
{ Uzywamy dropOffRate VRMLa 1.0 zeby sprawic by spot byl |
| 139 |
bardziej "skupiony" zgodnie z rownaniami swiatla OpenGLa. |
| 140 |
W VRMLu 97 skupienie spota jest okreslane inaczej, przez beamWidth. } |
| 141 |
CosSpotAngle := TVector3.DotProduct(-LightDirNorm, Direction); |
| 142 |
{ TODO: optimize: no need to calc ArcCos here if we would precalc |
| 143 |
CosCutOffAngle for every light. } |
| 144 |
SpotAngle := ArcCos(CosSpotAngle); |
| 145 |
if SpotAngle <= SpotLight.FdCutOffAngle.Value then |
| 146 |
Result := Power(CosSpotAngle, SpotLight.SpotExponent) else |
| 147 |
Result := 0; |
| 148 |
end; |
| 149 |
|
| 150 |
function CalculateSpot(const SpotLight: TSpotLightNode): Single; |
| 151 |
var |
| 152 |
CosSpotAngle, SpotAngle, SpotCO, SpotBW: Single; |
| 153 |
begin |
| 154 |
CosSpotAngle := TVector3.DotProduct(-LightDirNorm, Direction); |
| 155 |
SpotAngle := ArcCos(CosSpotAngle); |
| 156 |
SpotCO := SpotLight.FdCutOffAngle.Value; |
| 157 |
SpotBW := SpotLight.FdBeamWidth.Value; |
| 158 |
|
| 159 |
if SpotAngle < SpotCO then |
| 160 |
begin |
| 161 |
if SpotAngle > SpotBW then |
| 162 |
Result := (SpotAngle - SpotCO) / (SpotBW - SpotCO) else |
| 163 |
Result := 1; |
| 164 |
end else |
| 165 |
Result := 0; |
| 166 |
end; |
| 167 |
|
| 168 |
function AmbientFactor(const M: TMaterialInfo): TCastleColorRGB; |
| 169 |
var |
| 170 |
MaterialAmbientColor: TCastleColorRGB; |
| 171 |
begin |
| 172 |
if not (M is TPhongMaterialInfo) then |
| 173 |
Result := TVector3.Zero |
| 174 |
else |
| 175 |
begin |
| 176 |
MaterialAmbientColor := TPhongMaterialInfo(M).AmbientColor; |
| 177 |
Result := MaterialAmbientColor * Node.FdAmbientIntensity.Value; |
| 178 |
end; |
| 179 |
end; |
| 180 |
|
| 181 |
function DiffuseFactor(const M: TMaterialInfo): TCastleColorRGB; |
| 182 |
var |
| 183 |
MaterialDiffuseColor: TCastleColorRGB; |
| 184 |
begin |
| 185 |
if M is TPhongMaterialInfo then |
| 186 |
MaterialDiffuseColor := TPhongMaterialInfo(M).DiffuseColor |
| 187 |
else |
| 188 |
if M is TPhysicalMaterialInfo then |
| 189 |
MaterialDiffuseColor := TPhysicalMaterialInfo(M).BaseColor |
| 190 |
else |
| 191 |
Exit(TVector3.Zero); |
| 192 |
|
| 193 |
Result := |
| 194 |
(MaterialDiffuseColor * MainTextureColor) * |
| 195 |
(Node.FdIntensity.Value * TVector3.DotProduct(Normal, LightDirNorm)); |
| 196 |
end; |
| 197 |
|
| 198 |
function SpecularFactor(const M: TMaterialInfo): TCastleColorRGB; |
| 199 |
var |
| 200 |
{ average of "vector to camera pos" and LightDirNorm } |
| 201 |
CameraAndLightHalfway: TVector3; |
| 202 |
MaterialSpecularColor: TVector3; |
| 203 |
MaterialShininessExp: Single; |
| 204 |
begin |
| 205 |
if M is TPhongMaterialInfo then |
| 206 |
begin |
| 207 |
MaterialSpecularColor := TPhongMaterialInfo(M).SpecularColor; |
| 208 |
MaterialShininessExp := TPhongMaterialInfo(M).ShininessExp; |
| 209 |
{ niestety musimy znormalizowac (CamPosition - Point) |
| 210 |
(zeby mial taki sam wklad w sume co LightDirNorm) i wynik |
| 211 |
(ktory musi byc znormalizowany zeby nadawal sie do wyliczania |
| 212 |
cosinusa jako prostego dot product; jak zwykle, nalezy pamietac |
| 213 |
ze suma dwoch wektorow dlugosci 1 niekoniecznie (rzadko) jest wektorem |
| 214 |
dlugosci 2 (wiec skalowanie przez 1/2 nie rozwiazaloby tu problemu)) } |
| 215 |
CameraAndLightHalfway := ( |
| 216 |
(CamPosition - Point).Normalize + LightDirNorm).Normalize; |
| 217 |
Result := |
| 218 |
MaterialSpecularColor * |
| 219 |
(Node.FdIntensity.Value * |
| 220 |
Power( Max(TVector3.DotProduct(Normal, CameraAndLightHalfway), Single(0.0)), |
| 221 |
MaterialShininessExp)); |
| 222 |
end else |
| 223 |
Result := TVector3.Zero; |
| 224 |
end; |
| 225 |
|
| 226 |
var |
| 227 |
Spot: Single; |
| 228 |
Attenuat: Single; |
| 229 |
PosLightNode: TAbstractPositionalLightNode; |
| 230 |
Factors: TVector3; |
| 231 |
M: TMaterialInfo; |
| 232 |
begin |
| 233 |
M := State.MaterialInfo; |
| 234 |
if M = nil then |
| 235 |
{ Material = nil means "implicit unlit material", it has only emissive |
| 236 |
which will be added outside of this function. } |
| 237 |
Exit(TVector3.Zero); |
| 238 |
|
| 239 |
if (not Node.FdOn.Value) then |
| 240 |
Exit(TVector3.Zero); |
| 241 |
|
| 242 |
{ TODO: arg dla attenuation powinien byc w ukladzie transformacji swiatla. |
| 243 |
For now lights with non-trivial attenuation put under some transformation |
| 244 |
that does distance scaling will not render as required by VRML 2.0 spec. |
| 245 |
Czyli musze implementowac InvertTransformation ? } |
| 246 |
if Node is TAbstractPositionalLightNode then |
| 247 |
begin |
| 248 |
PosLightNode := TAbstractPositionalLightNode(Node); |
| 249 |
if PosLightNode.DistanceNeededForAttenuation then |
| 250 |
Attenuat := PosLightNode.CalculateAttenuation( PointsDistance(Point, Location) ) |
| 251 |
else |
| 252 |
Attenuat := PosLightNode.CalculateAttenuation( 0.0 ); |
| 253 |
end else |
| 254 |
Attenuat := 1; |
| 255 |
|
| 256 |
{ LightDirNorm is called "L" in the equation in VRML spec } |
| 257 |
if Node is TAbstractPositionalLightNode then |
| 258 |
LightDirNorm := (Location - Point).Normalize |
| 259 |
else |
| 260 |
LightDirNorm := -Direction; |
| 261 |
|
| 262 |
{ wektory Normal i LightDirNorm musza wychodzic z tej samej strony |
| 263 |
powierzchni TriNormPlaneSingle (musimy przeciez zrobic cos co w OpenGLu |
| 264 |
jest okreslane jako two-sided lighting, czyli wziac albo Normal albo |
| 265 |
-Normal, w zaleznosci od pozycji swiatla.). } |
| 266 |
|
| 267 |
{ Normal is called "N" in the equation in VRML spec } |
| 268 |
Normal := PlaneDirInDirection(PointPlaneNormal, LightDirNorm); |
| 269 |
|
| 270 |
{ oblicz Spot - dla SpotLight okresla on jak bardzo punkt jest w spocie, |
| 271 |
dla innych swiatel jest zawsze = 1. } |
| 272 |
if Node is TSpotLightNode_1 then |
| 273 |
Spot := CalculateSpot_1(TSpotLightNode_1(Node)) |
| 274 |
else |
| 275 |
if Node is TSpotLightNode then |
| 276 |
Spot := CalculateSpot(TSpotLightNode(Node)) |
| 277 |
else |
| 278 |
Spot := 1; |
| 279 |
|
| 280 |
Factors := |
| 281 |
AmbientFactor(M) + |
| 282 |
DiffuseFactor(M) + |
| 283 |
SpecularFactor(M); |
| 284 |
|
| 285 |
Result := (Node.FdColor.Value * (Attenuat * Spot)) * Factors; |
| 286 |
end; |
| 287 |
|
| 288 |
function TLightInstance.Position: TVector4; |
| 289 |
begin |
| 290 |
if Node is TAbstractPositionalLightNode then |
| 291 |
Result := Vector4(Location, 1) else |
| 292 |
Result := Vector4(-Direction, 0); |
| 293 |
end; |
| 294 |
|
| 295 |
{ TLightInstancesList ----------------------------------------------------- } |
| 296 |
|
| 297 |
function TLightInstancesList.IndexOfNode(Node: TAbstractLightNode): integer; |
| 298 |
begin |
| 299 |
for Result := 0 to Count - 1 do |
| 300 |
if L[Result].Node = Node then |
| 301 |
Exit; |
| 302 |
Result := -1; |
| 303 |
end; |
| 304 |
|
| 305 |
function TLightInstancesList.FindName(NodeName: string): PLightInstance; |
| 306 |
var |
| 307 |
I: Integer; |
| 308 |
begin |
| 309 |
for I := 0 to Count - 1 do |
| 310 |
begin |
| 311 |
Result := PLightInstance(Ptr(I)); |
| 312 |
if Result^.Node.X3DName = NodeName then |
| 313 |
Exit; |
| 314 |
end; |
| 315 |
Result := nil; |
| 316 |
end; |
| 317 |
|
| 318 |
function TLightInstancesList.Equals(SecondValue: TObject): boolean; |
| 319 |
|
| 320 |
function LightInstanceEquals(const L1, L2: TLightInstance): boolean; |
| 321 |
begin |
| 322 |
Result := (L1.Node = L2.Node) and |
| 323 |
TMatrix4.PerfectlyEquals(L1.Transform, L2.Transform); |
| 324 |
|
| 325 |
{ No need to compare things like Location or Direction, |
| 326 |
as they are just precalculated based on Node and Transform. } |
| 327 |
end; |
| 328 |
|
| 329 |
var |
| 330 |
I: Integer; |
| 331 |
begin |
| 332 |
Result := |
| 333 |
(SecondValue <> nil) and |
| 334 |
(SecondValue is TLightInstancesList) and |
| 335 |
(TLightInstancesList(SecondValue).Count = Count); |
| 336 |
if Result then |
| 337 |
for I := 0 to Count - 1 do |
| 338 |
if not LightInstanceEquals(L[I], TLightInstancesList(SecondValue).L[I]) then |
| 339 |
Exit(false); |
| 340 |
end; |
| 341 |
|
| 342 |
{$endif read_implementation} |