-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharealight.cs
81 lines (72 loc) · 3.67 KB
/
arealight.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
namespace Template
{
class arealight : lightsource
{
sphere shape;
int index;
public arealight(float _emittance, sphere _shape)
{
emittance = _emittance;
shape = _shape;
}
//Build GLSL functions
public void AddToArray(List<float> array, float[] colors, StringBuilder normal, StringBuilder light)
{
index = array.Count / 3;
array.Add(shape.position.X);
array.Add(shape.position.Y);
array.Add(shape.position.Z);
light.AppendLine(" light_direction = areaLightsources[" + index + "] - ray_origin;");
light.AppendLine(" tmax = length(light_direction) - 0.0002;");
light.AppendLine(" lightsource_emittance = " + emittance + " / (12.456 * length(light_direction) * length(light_direction));");
light.AppendLine(" light_direction = normalize(light_direction);");
light.AppendLine(" if(normal == vec3(0,0,0))");
light.AppendLine(" angle = 1;");
light.AppendLine(" else");
light.AppendLine(" angle = dot(normal, light_direction);");
light.AppendLine(" point_of_intersection = ray_origin + light_direction * 0.0001;");
light.AppendLine(" collision = false;");
light.AppendLine(" if(angle < 0) collision = true;");
light.AppendLine(" if(!collision){");
light.AppendLine(" collision = calcObjects(point_of_intersection, light_direction, tmax);");
light.AppendLine(" if(!collision){");
light.AppendLine(" color += vec3(" + colors[shape.color * 3] + ", " + colors[shape.color * 3 + 1] + ", " + colors[shape.color * 3 + 2] + ") * lightsource_emittance * energy * angle * absorption * 0.25;");
light.AppendLine(" }");
light.AppendLine(" }");
normal.AppendLine(" d = 2.0 * dot(ray_origin - areaLightsources[" + index + "], ray_direction);");
normal.AppendLine(" discriminant = d * d - 4 * (dot(ray_origin - areaLightsources[" + index + "], ray_origin - areaLightsources[" + index + "]) - " + (shape.radius * shape.radius) + ");");
normal.AppendLine(" if(discriminant >= 0) {");
normal.AppendLine(" s = (-d - sqrt(discriminant)) / 2;");
normal.AppendLine(" s = s < 0 ? (-d + sqrt(discriminant)) / 2 : s;");
normal.AppendLine(" if(s > 0 && s < t) {");
normal.AppendLine(" t = s;");
normal.AppendLine(" col = vec3(" + colors[shape.color * 3] + ", " + colors[shape.color * 3 + 1] + ", " + colors[shape.color * 3 + 2] + ");");
normal.AppendLine(" normal = vec3(0,0,0);");
normal.AppendLine(" absorption = 1;");
normal.AppendLine(" }");
normal.AppendLine(" }");
}
//Move light based on user input
public void move(Vector3 direction, float[] array)
{
shape.position -= direction;
array[index * 3] = shape.position.X;
array[index * 3 + 1] = shape.position.Y;
array[index * 3 + 2] = shape.position.Z;
}
//Rotate light based on user input
public void rotate(Quaternion rotate, float[] array)
{
shape.position = rotate * shape.position;
array[index * 3] = shape.position.X;
array[index * 3 + 1] = shape.position.Y;
array[index * 3 + 2] = shape.position.Z;
}
}
}