-
Notifications
You must be signed in to change notification settings - Fork 0
/
Light.cs
63 lines (52 loc) · 1.83 KB
/
Light.cs
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
using System;
using System.Numerics;
namespace RayTracer
{
public class Light
{
public Vector3 pos; // light position
public Vector3 color; //color of light
public Light(Vector3 pos, Vector3 color)
{
this.pos = pos;
this.color = color;
}
//Construct the shadowray from Intersection.
public virtual Ray ShadowRay(Intersection I, out bool doesExist)
{
float t = (this.pos - I.point).Length(); //length from point to light source
Vector3 dir = (this.pos - I.point);
//fix for shadow acne
t -= 2 * 1E-3f;
dir = Vector3.Normalize(dir);
doesExist = true;
return new Ray(I.point += I.normal*1E-3f, dir, t);
}
}
public class SpotLight : Light
{
public float angle;
public Vector3 dir;
public SpotLight(Vector3 pos, Vector3 color, Vector3 dir, float angle) : base(pos, color)
{
this.angle = angle; // the width of the cone
this.pos = pos;
this.color = color;
this.dir = Vector3.Normalize(dir);
}
public override Ray ShadowRay(Intersection I, out bool doesExist)
{
Vector3 v1 = Vector3.Normalize(pos - I.point);
float theta = (float)Math.Acos(Vector3.Dot(dir, v1)) * 180 / (float)Math.PI;
// If the angle between the intersection point and light position is
// greater than the spotlight angle, the shadowray doesn't exist.
if (theta > angle)
{
doesExist = false;
return new Ray();
}
else
return base.ShadowRay(I, out doesExist);
}
}
}