-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vectors.adb
223 lines (193 loc) · 7.08 KB
/
Vectors.adb
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
with ada.numerics.elementary_functions, Rand;
use ada.numerics.elementary_functions, Rand;
Package body Vectors is
-----------------------------------------------------
Function "+" (A, B: Vec3) return Vec3 is
begin
return ((A.x+B.x),(A.y+B.y),(A.z+B.z));
end "+";
-----------------------------------------------------
Function "-" (A, B: Vec3) return Vec3 is
begin
return ((A.x-B.x),(A.y-B.y),(A.z-B.z));
end "-";
-----------------------------------------------------
Function "*" (A, B: Vec3) return Vec3 is
begin
return ((A.x*B.x), (A.y*B.y), (A.z*B.z));
end "*";
-----------------------------------------------------
Function Dot (A, B: Vec3) return float is
begin
return (A.x*B.x + A.y*B.y + A.z*B.z);
end Dot;
-----------------------------------------------------
Function Norm (A: Vec3) return float is
begin
return Sqrt (A.x**2 + A.y**2 + A.z**2);
end Norm;
-----------------------------------------------------
Procedure Normalize (A: in out Vec3) is
Len: float := Norm (A);
begin
A := ((A.x/Len, A.y/Len, A.z/Len));
exception
when others => A := (0.0,0.0,0.0);
end Normalize;
-----------------------------------------------------
-- Eclaire un pixel Kb d'une lumiere Li.
-----------------------------------------------------
Procedure Light (
Source : Vec3 ;
Li, Kb : Pixel;
Img : in out T_Image
)is
P,Vi,N : Vec3 ;
Cos : float;
begin
for y in Img'range(1) loop
for x in Img'range(2) loop
P := (float(x), float(y), 0.0);
Vi := Source - P; Normalize (Vi);
N := (0.0,0.0,1.0); --Normalize (N);
Cos := Dot (N, Vi) ;
Img(y,x) := Kb * Li * Cos; -- /3.14;
end loop;
end loop;
end Light;
-------------------------------------------------------------------
-- Eclaire tous les pixels d'une image d'une lumière Li.
-------------------------------------------------------------------
-- ( possibilité de mettre deux sources opposées)
Procedure LightBack (
Source : Vec3 ;
Li : Pixel;
Img : in out T_Image;
Phong : boolean := false;
Kd : Pixel := (1.0,1.0,1.0);
Kn : float := 100.0;
Obs : Vec3 := (0.0,0.0,1.0))
is
-- Source2: Vec3 := (Source.x, float(Img'last(1))-Source.y, Source.z);
P,Vi,H : Vec3 ;
N : Vec3 := (0.0,2.0,1.0);
-- Vi2,N2 : Vec3 ;
CosT,
CosA : float;
-- Ratio2 : float;
begin
Normalize (N);
for y in Img'range(1) loop
for x in Img'range(2) loop
P := (float(x), float(y), 0.0);
Vi := Source - P; Normalize (Vi);
-- Vi2 := Source2 - P; Normalize (Vi2);
-- N2 := (0.0,-3.0,1.0); Normalize (N2);
CosT := Dot (N, Vi);
if CosT < 0.0 then CosT := 0.0; end if;
-- Ratio2 := Dot (N2, Vi2);
-- if Ratio2 < 0.0 then Ratio2 := 0.0; end if;
-- Cos := (Cos + Ratio2)/ 2.0;
if Phong then
H := (Vi + Obs); Normalize (H);
CosA := Dot (H, N);
Img(y,x) := (Li * CosT * (Img(y,x) + Kd * (cosA ** Kn)) / (1.0+CosA ** Kn)) * (Rand.flt/2.0+0.5);
-- Img(y,x) := Li * CosT * (Kb/3.14 + (Kd (Kn+2) * (CosA ** Kn)) / 6.28);
else
Img(y,x) := Li * CosT * Img(y,x); -- /3.14;
end if;
end loop;
end loop;
end LightBack;
-------------------------------------------------------------------
-- Met des pixels aléatoires partout ailleurs que dans le cercle.
-------------------------------------------------------------------
Procedure noSphere (
Cercle : T_Cercle;
C : Pixel;
Img : in out T_Image)
is
P : Vec3;
l : float;
begin
for y in Img'range(1) loop
for x in Img'range(2) loop
P := (float (x), float (y), 0.0);
l := Norm (P - Cercle.Centre);
if l > Cercle.Rayon then
img (y,x) := RandomPix;
end if;
end loop;
end loop;
end noSphere;
-------------------------------------------------------------------
-- Simule l'eclairage d'une sphere.
-------------------------------------------------------------------
Procedure Sphere (
Source : Vec3 ;
Cercle : T_Cercle;
Li, Kb : Pixel;
Img : in out T_Image;
Phong : boolean := false;
Kd : Pixel := (1.0,1.0,1.0);
Kn : float := 100.0)
is
P,Vi,H : Vec3;
N : Vec3 := (0.0,0.0,1.0);
CosT,l: float;
CosA: float;
begin
for y in Img'range(1) loop
for x in Img'range(2) loop
P := (float (x), float (y), 0.0);
l := Norm (P - Cercle.Centre);
if l <= Cercle.Rayon then
P := (float (x), float (y), sqrt (Cercle.Rayon**2-l**2));
N := P - Cercle.Centre;
Normalize (N);
Vi := Source - P;
Normalize (Vi);
CosT := Dot (N, Vi);
if CosT < 0.0 then CosT := 0.0; end if;
if Phong then
H := (Vi + N); Normalize (H);
CosA := Dot (H, N);
Img(y,x) := Li * CosT * (Kb + Kd * (CosA ** Kn)) / (1.0+CosA ** Kn);
-- Img(y,x) := Li * CosT * (Kb/3.14 + (Kd + (Kn+2.0) * (CosA ** Kn))/6.28);
else
Img (y,x) := Kb * Li * CosT; -- / 3.14;
end if;
end if;
end loop;
end loop;
end Sphere;
-------------------------------------------------------------------
-- Simule l'eclairage d'une sphere avec fondu sur l'image.
-------------------------------------------------------------------
Procedure SphereFaded (
Source : Vec3 ;
Cercle : T_Cercle;
Li, Kb : Pixel;
Img : in out T_Image)
is
P,Vi,N : Vec3;
CosT,L : float;
begin
for y in Img'range(1) loop
for x in Img'range(2) loop
P := (float (x), float (y), 0.0);
L := Norm (P - Cercle.Centre);
if L <= Cercle.Rayon then
P := (float (x), float (y), sqrt (Cercle.Rayon**2-l**2));
N := P - Cercle.Centre;
Normalize (N);
Vi := Source - P;
Normalize (Vi);
CosT := Dot (N, Vi);
if CosT < 0.0 then CosT := 0.0; end if;
Img (y,x) := Kb * Li * CosT + Img (y,x) * (1.0 - CosT);
end if;
end loop;
end loop;
end SphereFaded;
end Vectors;