-
Notifications
You must be signed in to change notification settings - Fork 0
/
Raytracer.cs
268 lines (254 loc) · 11.3 KB
/
Raytracer.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using OpenTK;
using System;
using Template;
namespace INFOGR2022Template
{
internal class Raytracer
{
Scene scene;
Camera camera;
//the ray that will be set through the the camera origin and the screen plane
Ray primaryRay = new Ray();
//the intersection
Intersection intersection = new Intersection();
//upleft corner
Vector3 upLeft;
//upRight
Vector3 upRight;
//downRight corner
Vector3 downLeft;
//cap for the mirror recursion
int recursionCap = 10;
/// <summary>
/// initialize the Raytracer using a scene, a camera and a screen.
/// </summary>
/// <param name="scene"> the scene containg all the primitives </param>
/// <param name="camera">the camera with a position, a updirection and a lookatdirection</param>
/// <param name="screen">the screen made of pixels that need to be colored according to the pixel</param>
public Raytracer(Scene scene, Camera camera)
{
this.scene = scene;
this.camera = camera;
primaryRay.position = camera.position;
//calculating the edge points of the plane
Vector3 center = camera.position + camera.lookAtDirection * camera.FOV;
Vector3 rightDirection = Vector3.Cross(camera.upDirection, camera.lookAtDirection);
upLeft = center + camera.upDirection - rightDirection;
upRight = center + camera.upDirection + rightDirection;
downLeft = center - camera.upDirection - rightDirection;
}
/// <summary>
/// render the scene using raytracing
/// </summary>
internal void Render()
{
//the width of the plane
Vector3 horizon = upRight - upLeft;
//the height of the plane
Vector3 vertical = upLeft - downLeft;
//set the pos of the camera
primaryRay.position = camera.position;
//draw the redline, representing the screen to be drawn. We divide by 10, because we assume the 'box' of our scene to be 10 by 10 by 10.
OpenTKApp.Debug.debugScreen.Line(
(int)(OpenTKApp.Debug.debugScreen.width / 2 - (horizon.Length / 2) * (OpenTKApp.Debug.debugScreen.width / 10)),
(int)(OpenTKApp.Debug.debugScreen.height - camera.lookAtDirection.Length * (OpenTKApp.Debug.debugScreen.height / 10) * camera.FOV),
(int)(OpenTKApp.Debug.debugScreen.width / 2 + (horizon.Length / 2) * (OpenTKApp.Debug.debugScreen.width / 10)),
(int)(OpenTKApp.Debug.debugScreen.height - camera.lookAtDirection.Length * (OpenTKApp.Debug.debugScreen.height / 10) * camera.FOV),
0xFF0000);
for (int y = 0; y < OpenTKApp.app.screen.height; y++)
{
for (int x = 0; x < OpenTKApp.app.screen.width; x++)
{
//reset the primary ray and set the direction and normalize it
primaryRay.scalar = 0;
primaryRay.RGB = new Vector3(0.05f);
primaryRay.direction = upLeft + ((float)x / (float)OpenTKApp.app.screen.width) * horizon + ((float)y / (float)OpenTKApp.app.screen.height) * vertical;
primaryRay.direction.Normalize();
//for every object
foreach (Primitives p in scene.objects)
{
primaryRay.scalar = 1;
intersection = new Intersection();
//if it is a sphere
//intersect the ray with the sphere, storing the result in the scalar of the primary ray
//if it hits something
if (Intersect(p, primaryRay))
{
//change the RGB color from background to zero
primaryRay.RGB = new Vector3(0);
//set the scalar to the scalar calculated for the intersection
primaryRay.scalar = intersection.distance.scalar;
//if it is mirror material, calculate it differently
if (p.material == Primitives.materials.mirror)
{
Ray secondaryRay = new Ray();
secondaryRay.position = primaryRay.position + primaryRay.direction * primaryRay.scalar;
secondaryRay.direction = primaryRay.direction - 2 * (primaryRay.direction * p.ReturnNormal(secondaryRay.position)) * p.ReturnNormal(secondaryRay.position);
secondaryRay.direction.Normalize();
primaryRay.RGB = CalculateMirror(p, secondaryRay);
break;
}
//calculate the colour according to lights in scene
primaryRay.RGB = returnColorLight(p, primaryRay);
}
}
//using MixColor store the colour of the ray into the pixel. ofc, the colour cant be less than 0 or more than 255
OpenTKApp.app.screen.Plot(x, y, MixColor((int)(MathHelper.Clamp(primaryRay.RGB.X,0,1) * 255),
(int)(MathHelper.Clamp(primaryRay.RGB.Y, 0, 1) * 255),
(int)(MathHelper.Clamp(primaryRay.RGB.Z, 0, 1) * 255)));
}
}
}
/// <summary>
/// returns the colour after a successful intersection
/// </summary>
/// <param name="p"></param>
/// <param name="calcRay"></param>
/// <returns></returns>
Vector3 returnColorLight(Primitives p, Ray calcRay)
{
//create a shadow ray and set its position
Ray shadowRay = new Ray();
shadowRay.position = calcRay.position + calcRay.direction * calcRay.scalar;
//then for every light
foreach (Light l in scene.lights)
{
//reset the scalar and set the normal direction
shadowRay.scalar = 0;
shadowRay.direction = l.position - shadowRay.position;
shadowRay.direction.Normalize();
bool hitAny = false;
foreach (Primitives toCheck in scene.objects)
{
intersection = new Intersection();
hitAny = Intersect(toCheck, shadowRay);
if (hitAny)
break;
}
//if it doesn't hit any, set the color
if (!hitAny)
{
calcRay.RGB += l.returnColor(p.ReturnNormal(calcRay.position + calcRay.direction * calcRay.scalar),
shadowRay.direction,
p.GetColour(calcRay.position + calcRay.direction * calcRay.scalar),
-camera.lookAtDirection,
p.material);
}
else
{
calcRay.RGB = new Vector3(0.07f);
}
}
return calcRay.RGB;
}
/// <summary>
/// calculates the colour for a pixel on a mirror primitive
/// </summary>
/// <param name="toCheck"></param>
/// <param name="secondaryRay"></param>
/// <returns></returns>
Vector3 CalculateMirror(Primitives toCheck, Ray secondaryRay)
{
if (secondaryRay.amountOfRecursion++ == recursionCap)
return new Vector3(0.07f);
foreach (Primitives p in scene.objects)
{
if (Intersect(p, secondaryRay))
{
if (p.material == Primitives.materials.mirror)
{
secondaryRay.RGB += toCheck.RGB;
secondaryRay.position = secondaryRay.position + secondaryRay.direction * intersection.distance.scalar;
secondaryRay.direction = secondaryRay.direction - 2 * (secondaryRay.direction * p.ReturnNormal(secondaryRay.position)) * p.ReturnNormal(secondaryRay.position);
secondaryRay.direction.Normalize();
return CalculateMirror(p, secondaryRay);
}
else
{
secondaryRay.RGB += returnColorLight(p, secondaryRay);
}
break;
}
}
return secondaryRay.RGB;
}
/// <summary>
/// the general method which encompasses both intersect methods
/// </summary>
/// <param name="toCheck"></param>
/// <param name="ray"></param>
/// <returns></returns>
bool Intersect(Primitives toCheck, Ray ray)
{
bool inter = false;
if (toCheck is Sphere)
inter = IntersectSphere((Sphere)toCheck, ray);
if (toCheck is Plane)
inter = IntersectPlane((Plane)toCheck, ray);
return inter;
}
/// <summary>
/// intersect a ray with the sphere, storing the scalar if it does
/// </summary>
/// <param name="sphere"></param>
/// <param name="ray"></param>
/// <returns></returns>
bool IntersectSphere(Sphere sphere, Ray ray)
{
Vector3 c = sphere.Position - ray.position;
float t = Vector3.Dot(c, ray.direction);
Vector3 q = c - t * ray.direction;
float p2 = q.LengthSquared;
if (p2 > sphere.Radius)
{
return false;
}
t -= (float)Math.Sqrt(sphere.Radius - p2);
if (Math.Abs(t) > 0.001)
{
ray.scalar = t;
intersection.nearestPrimitive = sphere;
intersection.distance = ray;
return true;
}
return false;
}
/// <summary>
/// intersects a vector with a plane
/// </summary>
/// <param name="plane"></param>
/// <param name="ray"></param>
/// <returns></returns>
internal bool IntersectPlane(Plane plane, Ray ray)
{
intersection.nearestPrimitive = plane;
intersection.distance = ray;
float scalar = Vector3.Dot(plane.normal, ray.direction);
if (scalar > 0)
{
Vector3 intersect = plane.distance - ray.position;
var t = Vector3.Dot(intersect, plane.normal) / scalar;
if (t >= 0.0001)
{
intersection.distance.scalar = t;
return true;
}
}
return false;
}
//a ray is defined by a position and a (normalized) direction
internal struct Ray
{
internal Vector3 RGB;
internal Vector3 position;
internal float scalar;
internal Vector3 direction;
internal int amountOfRecursion;
}
//mix a color
int MixColor(int red, int green, int blue)
{
return (red << 16) + (green << 8) + blue;
}
}
}