-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere.cs
75 lines (69 loc) · 3.25 KB
/
sphere.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL;
using OpenTK;
namespace Template
{
class sphere : @object
{
public float radius;
public sphere(Vector3 _position, float _radius, int _color, float _absorption = 1, float _refraction = 0)
{
position = _position;
radius = _radius;
color = _color;
absorption = _absorption;
refraction = _refraction;
}
//Build GLSL functions
public void AddToArray(List<float> array, float[] colors, StringBuilder normal, StringBuilder faster)
{
index = array.Count / 3;
array.Add(position.X);
array.Add(position.Y);
array.Add(position.Z);
normal.AppendLine(" d = 2.0 * dot(ray_origin - spheres[" + index + "], ray_direction);");
normal.AppendLine(" discriminant = d * d - 4 * (dot(ray_origin - spheres[" + index + "], ray_origin - spheres[" + index + "]) - " + (radius * 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[color*3] + ", " + colors[color * 3 + 1] + ", " + colors[color * 3 + 2]+"); ");
normal.AppendLine(" normal = normalize(ray_origin + s * ray_direction - spheres[" + index + "]);");
normal.AppendLine(" refraction = " + refraction + ";");
normal.AppendLine(" absorption = " + absorption + ";");
normal.AppendLine(" }");
normal.AppendLine(" }");
if (refraction == 0)
{
faster.AppendLine(" d = 2.0 * dot(ray_origin - spheres[" + index + "], ray_direction);");
faster.AppendLine(" discriminant = d * d - 4 * (dot(ray_origin - spheres[" + index + "], ray_origin - spheres[" + index + "]) - " + (radius * radius) + ");");
faster.AppendLine(" if(discriminant >= 0) {");
faster.AppendLine(" s = (-d - sqrt(discriminant)) / 2;");
faster.AppendLine(" s = s < 0 ? (-d + sqrt(discriminant)) / 2 : s;");
faster.AppendLine(" if(s > 0 && s < tmax) return true;");
faster.AppendLine(" }");
}
}
//Move sphere based on user input
public override void move(Vector3 direction, float[] array)
{
position -= direction;
array[index * 3] = position.X;
array[index * 3 + 1] = position.Y;
array[index * 3 + 2] = position.Z;
}
//Rotate sphere based on user input
public override void rotate(Quaternion rotate, float[] array)
{
position = rotate * position;
array[index * 3] = position.X;
array[index * 3 + 1] = position.Y;
array[index * 3 + 2] = position.Z;
}
}
}