source: trunk/Packages/bgrabitmap/lightingclasses3d.inc

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 5.4 KB
Line 
1type
2 { TBGRADirectionalLight3D }
3
4 TBGRADirectionalLight3D = class(TBGRALight3D,IBGRADirectionalLight3D)
5 protected
6 FDirection, FBetweenDirectionAndObserver: TPoint3D_128;
7 public
8 constructor Create(ADirection: TPoint3D);
9 function GetDirection: TPoint3D; override;
10 procedure SetDirection(const AValue: TPoint3D);
11
12 procedure ComputeDiffuseAndSpecularColor(Context: PSceneLightingContext); override;
13 procedure ComputeDiffuseColor(Context: PSceneLightingContext); override;
14 procedure ComputeDiffuseLightness(Context: PSceneLightingContext); override;
15 function IsDirectional: boolean; override;
16 end;
17
18 { TBGRAPointLight3D }
19
20 TBGRAPointLight3D = class(TBGRALight3D,IBGRAPointLight3D)
21 protected
22 FVertex: IBGRAVertex3D;
23 FIntensity: single;
24 public
25 constructor Create(AVertex: IBGRAVertex3D; AIntensity: single);
26 function GetIntensity: single; override;
27 procedure SetIntensity(const AValue: single);
28
29 function GetVertex: IBGRAVertex3D;
30 procedure SetVertex(const AValue: IBGRAVertex3D);
31 function GetPosition: TPoint3D; override;
32
33 procedure ComputeDiffuseAndSpecularColor(Context: PSceneLightingContext); override;
34 procedure ComputeDiffuseLightness(Context: PSceneLightingContext); override;
35 procedure ComputeDiffuseColor(Context: PSceneLightingContext); override;
36 function IsDirectional: boolean; override;
37 end;
38
39{ TBGRAPointLight3D }
40
41constructor TBGRAPointLight3D.Create(AVertex: IBGRAVertex3D; AIntensity: single);
42begin
43 inherited Create;
44 FVertex:= AVertex;
45 FIntensity := AIntensity;
46end;
47
48function TBGRAPointLight3D.GetIntensity: single;
49begin
50 result := FIntensity;
51end;
52
53procedure TBGRAPointLight3D.SetIntensity(const AValue: single);
54begin
55 FIntensity:= AValue;
56end;
57
58function TBGRAPointLight3D.GetVertex: IBGRAVertex3D;
59begin
60 result := FVertex;
61end;
62
63procedure TBGRAPointLight3D.SetVertex(const AValue: IBGRAVertex3D);
64begin
65 FVertex := AValue;
66end;
67
68function TBGRAPointLight3D.GetPosition: TPoint3D;
69begin
70 Result:= FVertex.GetViewCoord;
71end;
72
73procedure TBGRAPointLight3D.ComputeDiffuseAndSpecularColor(Context: PSceneLightingContext);
74 {$DEFINE PARAM_POINTLIGHT}
75 {$i phonglight.inc}
76
77procedure TBGRAPointLight3D.ComputeDiffuseLightness(Context: PSceneLightingContext);
78const maxValue = 100*32768;
79var
80 vect: TPoint3D_128;
81 dist2,intensity: single;
82begin
83 vect := FVertex.ViewCoord_128 - Context^.basic.Position;
84 dist2 := DotProduct3D_128(vect,vect);
85 if dist2 = 0 then
86 TBGRAMaterial3D(Context^.material).ComputeDiffuseLightness(Context,maxValue,FLightness)
87 else
88 begin
89 intensity := DotProduct3D_128(vect, Context^.basic.Normal)/(dist2*sqrt(dist2))*FIntensity;
90 if Context^.LightThrough and (intensity < 0) then intensity := -intensity*Context^.LightThroughFactor;
91 if intensity > 100 then intensity := 100;
92 if intensity < FMinIntensity then intensity := FMinIntensity;
93 TBGRAMaterial3D(Context^.material).ComputeDiffuseLightness(Context,round(intensity*32768),FLightness);
94 end;
95end;
96
97procedure TBGRAPointLight3D.ComputeDiffuseColor(Context: PSceneLightingContext);
98var
99 vect: TPoint3D_128;
100 intensity,dist2: single;
101begin
102 vect := FVertex.ViewCoord_128 - Context^.basic.Position;
103 dist2 := DotProduct3D_128(vect,vect);
104 if dist2 = 0 then
105 intensity := 100
106 else
107 begin
108 intensity := DotProduct3D_128(vect, Context^.basic.Normal)/(dist2*sqrt(dist2))*FIntensity;
109 if Context^.LightThrough and (intensity < 0) then intensity := -intensity*Context^.LightThroughFactor;
110 if intensity > 100 then intensity := 100;
111 if intensity < FMinIntensity then intensity := FMinIntensity;
112 end;
113
114 TBGRAMaterial3D(Context^.material).ComputeDiffuseColor(Context,intensity, FColorInt);
115end;
116
117function TBGRAPointLight3D.IsDirectional: boolean;
118begin
119 result := false;
120end;
121
122{ TBGRADirectionalLight3D }
123
124constructor TBGRADirectionalLight3D.Create(ADirection: TPoint3D);
125begin
126 inherited Create;
127 SetDirection(ADirection);
128end;
129
130function TBGRADirectionalLight3D.GetDirection: TPoint3D;
131begin
132 result := Point3D(-FDirection.x,-FDirection.y,-FDirection.z);
133end;
134
135procedure TBGRADirectionalLight3D.SetDirection(const AValue: TPoint3D);
136begin
137 FDirection := -Point3D_128(AValue.x,AValue.y,AValue.z);
138 Normalize3D_128(FDirection);
139 FBetweenDirectionAndObserver := FDirection + FViewVector;
140 Normalize3D_128(FBetweenDirectionAndObserver);
141end;
142
143procedure TBGRADirectionalLight3D.ComputeDiffuseAndSpecularColor(Context: PSceneLightingContext);
144 {$i phonglight.inc}
145
146procedure TBGRADirectionalLight3D.ComputeDiffuseColor(Context: PSceneLightingContext);
147var
148 intensity: single;
149begin
150 intensity:= DotProduct3D_128(Context^.basic.Normal, FDirection);
151 if Context^.LightThrough and (intensity < 0) then intensity := -intensity*Context^.LightThroughFactor;
152 if intensity < FMinIntensity then intensity := FMinIntensity;
153
154 TBGRAMaterial3D(Context^.material).ComputeDiffuseColor(Context,intensity,FColorInt);
155end;
156
157procedure TBGRADirectionalLight3D.ComputeDiffuseLightness(
158 Context: PSceneLightingContext);
159var
160 intensity: single;
161begin
162 intensity:= DotProduct3D_128(Context^.basic.Normal, FDirection);
163 if Context^.LightThrough and (intensity < 0) then intensity := -intensity*Context^.LightThroughFactor;
164 if intensity < FMinIntensity then intensity := FMinIntensity;
165
166 TBGRAMaterial3D(Context^.material).ComputeDiffuseLightness(Context,round(intensity*32768),FLightness);
167end;
168
169function TBGRADirectionalLight3D.IsDirectional: boolean;
170begin
171 result := true;
172end;
173
Note: See TracBrowser for help on using the repository browser.