-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRay.cs
More file actions
69 lines (63 loc) · 2.53 KB
/
Copy pathRay.cs
File metadata and controls
69 lines (63 loc) · 2.53 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
using System;
using System.Collections.Generic;
using System.Drawing;
using WindowsFormsRayTracing;
namespace utils
{
class Ray
{
public vec3 direction;
public vec3 origin;
public Ray(vec3 rd, vec3 ro)
{
direction = rd;
origin = ro;
}
public Color Trace(ref List<GameObject> gameObjects, vec3 light)
{
GameObject obj = null;
float minDist = 100;
float itDist;
vec3 n = new vec3();
foreach (var gameObject in gameObjects)
{
itDist = gameObject.Interspect(Utils.sub(origin, gameObject.position), direction);
if (itDist > 0 && itDist < minDist)
{
minDist = itDist;
vec3 dot = Utils.add(origin, Utils.multS(direction, itDist));
n = gameObject.getNormal(dot);
obj = gameObject;
}
}
if (minDist >= 100 || n.IsNan() || obj == null)
{
vec3 lightDir2 = light;
float diffuse2 = Math.Max(0, Utils.dot(lightDir2, direction)) * 0.2f;
float specular2 = (float)Math.Pow(Math.Max(0, Utils.dot(direction, lightDir2)), 8);
diffuse2 += specular2;
diffuse2 = Math.Max(0, diffuse2);
diffuse2 = diffuse2 > 1 ? 1 : diffuse2;
Color c2;
if (diffuse2 > 0.98f)
c2 = Color.FromArgb((int)(Color.Yellow.R * diffuse2), (int)(Color.Yellow.G * diffuse2), (int)(Color.Yellow.B * diffuse2));
else
c2 = Color.LightSkyBlue;
return c2;
}
/*else if (Utils.dot(direction, light) <= 0)
{
return obj.color;
}*/
vec3 lightDir = light;// Utils.normilize(new vec3(0, 0, -1));
float diffuse = Math.Max(0, Utils.dot(lightDir, n)) * 0.5f + 0.1f;
vec3 reflected = Utils.sub(direction, Utils.multS(n, 2 * Utils.dot(n, direction)));
float specular = (float)Math.Pow(Math.Max(0, Utils.dot(reflected, lightDir)), 16);
diffuse += specular;
diffuse = Math.Max(0.1f, diffuse);
diffuse = diffuse > 1 ? 1 : diffuse;
Color c = Color.FromArgb((int)(obj.color.R * diffuse), (int)(obj.color.G * diffuse), (int)(obj.color.B * diffuse));
return c;
}
}
}