-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangle.cs
76 lines (70 loc) · 4.39 KB
/
triangle.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
namespace Template
{
class triangle : @object
{
public int[] vertices;
public triangle(int corner1, int corner2, int corner3, int _color, float _absorption = 1, float _refraction = 0)
{
vertices = new int[3];
vertices[0] = corner1;
vertices[1] = corner2;
vertices[2] = corner3;
color = _color;
absorption = _absorption;
refraction = _refraction;
}
//Build GLSL functions
public void AddToArray(float[] colors, StringBuilder normal, StringBuilder faster)
{
normal.AppendLine(" object_position = normalize(cross(vertices[" + vertices[1] + "] - vertices[" + vertices[0] + "], vertices[" + vertices[2] + "] - vertices[" + vertices[0] + "]));");
normal.AppendLine(" d = -dot(object_position, vertices[" + vertices[0] + "]);");
normal.AppendLine(" if(dot(ray_direction, object_position) > 0){");
normal.AppendLine(" s = -(dot(ray_origin, object_position) + d) / dot(ray_direction, object_position);");
normal.AppendLine(" if(s > 0 && s < t){");
normal.AppendLine(" if(dot(object_position,cross(vertices[" + vertices[1] + "] - vertices[" + vertices[0] + "], ray_origin + s * ray_direction - vertices[" + vertices[0] + "])) >= 0){");
normal.AppendLine(" if(dot(object_position,cross(vertices[" + vertices[2] + "] - vertices[" + vertices[1] + "], ray_origin + s * ray_direction - vertices[" + vertices[1] + "])) >= 0){");
normal.AppendLine(" if(dot(object_position,cross(vertices[" + vertices[0] + "] - vertices[" + vertices[2] + "], ray_origin + s * ray_direction - vertices[" + vertices[2] + "])) >= 0){");
normal.AppendLine(" t = s;");
normal.AppendLine(" col = vec3(" + colors[color * 3] + ", " + colors[color * 3 + 1] + ", " + colors[color * 3 + 2] + "); ");
normal.AppendLine(" normal = -object_position;");
normal.AppendLine(" refraction = " + refraction + ";");
normal.AppendLine(" absorption = " + absorption + ";");
normal.AppendLine(" }");
normal.AppendLine(" }");
normal.AppendLine(" }");
normal.AppendLine(" }");
normal.AppendLine(" }");
if(refraction == 0)
{
faster.AppendLine(" object_position = normalize(cross(vertices[" + vertices[1] + "] - vertices[" + vertices[0] + "], vertices[" + vertices[2] + "] - vertices[" + vertices[0] + "]));");
faster.AppendLine(" d = -dot(object_position, vertices[" + vertices[0] + "]);");
faster.AppendLine(" if(dot(ray_direction, object_position) > 0){");
faster.AppendLine(" s = -(dot(ray_origin, object_position) + d) / dot(ray_direction, object_position);");
faster.AppendLine(" if(s > 0 && s < tmax){");
faster.AppendLine(" if(dot(object_position,cross(vertices[" + vertices[1] + "] - vertices[" + vertices[0] + "], ray_origin + s * ray_direction - vertices[" + vertices[0] + "])) >= 0){");
faster.AppendLine(" if(dot(object_position,cross(vertices[" + vertices[2] + "] - vertices[" + vertices[1] + "], ray_origin + s * ray_direction - vertices[" + vertices[1] + "])) >= 0){");
faster.AppendLine(" if(dot(object_position,cross(vertices[" + vertices[0] + "] - vertices[" + vertices[2] + "], ray_origin + s * ray_direction - vertices[" + vertices[2] + "])) >= 0){");
faster.AppendLine(" return true;");
faster.AppendLine(" }");
faster.AppendLine(" }");
faster.AppendLine(" }");
faster.AppendLine(" }");
faster.AppendLine(" }");
}
}
//Get start index of shape in vertices list
public void changeIndex(int index)
{
for(int i = 0; i < 3; ++i)
{
vertices[i] += index;
}
}
}
}