-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsphere.pas
More file actions
69 lines (59 loc) · 1.46 KB
/
Copy pathsphere.pas
File metadata and controls
69 lines (59 loc) · 1.46 KB
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
UNIT Sphere;
INTERFACE
{$IFDEF SSE}
{$CODEALIGN LOCALMIN=16}
{$CODEALIGN VARMIN=16}
{$CODEALIGN RECORDMIN=16}
{$ALIGN 16}
{$ENDIF}
USES
Utils, Vector, Ray;
CONST
SurfaceType_Diffuse = 0;
SurfaceType_Specular = 1;
SurfaceType_Refractive = 2;
TYPE
TSphere = CLASS
PUBLIC
SurfaceType : INTEGER;
Position : TVector;
Emission : TVector;
Colour : TVector;
Radius, r2 : FloatType;
PUBLIC
CONSTRUCTOR Create(Radius : FloatType; Position, Emission, Colour : TVector; SurfaceType : INTEGER);
FUNCTION Intersect(VAR Ray : TRay) : FloatType;
END;
IMPLEMENTATION
CONSTRUCTOR TSphere.Create(Radius : FloatType; Position, Emission, Colour : TVector; SurfaceType : INTEGER);
BEGIN
self.Radius := Radius;
self.R2 := Radius * Radius;
self.Position := Position;
self.Emission := Emission;
self.Colour := Colour;
self.SurfaceType := SurfaceType;
END;
FUNCTION TSphere.Intersect(VAR Ray : TRay) : FloatType;
VAR
op : TVector;
eps : FloatType;
b : FloatType;
det : FloatType;
BEGIN
Result := 0;
Vector_Sub(op, self.Position, Ray.Origin);
eps := 1e-4;
b := Vector_Dot(op, Ray.Direction);
det := (b * b) - Vector_DotDot(op) + (self.R2);
IF (det < 0.0) THEN
exit
ELSE
det := sqrt(det);
IF (b - det > eps) THEN
Result := b - det
ELSE
IF (b + det > eps) THEN
Result := b + det;
END;
END.