-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere.c
74 lines (65 loc) · 1.98 KB
/
sphere.c
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
/*
* Filename : sphere.c
* Last Modified: 19 June 2020
* Owner : Group 20
*
* Description:
* to be updated ...
*
* Other:
* This file is formatted with a tab indent size of 4 and a character
* restriction of 80/line.
*/
/*------------------------------Standard Headers------------------------------*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
/*----------------------------User-defined Headers----------------------------*/
#include "sphere.h"
#include "material.h"
/*----------------------------Function Definitions----------------------------*/
/*================================Hit Handler=================================*/
/* Add comment here */
bool sph_hit(sphere *s, ray r, float t_min, float t_max, hit_rec *rec) {
vec oc = sub(r.ogn, s->cen);
float a = dot(r.des, r.des);
float b = dot(oc, r.des);
float c = dot(oc, oc) - s->rad * s->rad;
float d = b*b - a*c;
if (d > 0) {
float temp = (-b - sqrt(d))/a;
if (temp < t_max && temp > t_min) {
rec->t = temp;
rec->pos = point_at(temp, r);
rec->nor = div_c(sub(rec->pos, s->cen), s->rad);
rec->mat = s->mat;
return true;
}
temp = (-b + sqrt(d))/a;
if (temp < t_max && temp > t_min) {
rec->t = temp;
rec->pos = point_at(temp, r);
rec->nor = div_c(sub(rec->pos, s->cen), s->rad);
rec->mat = s->mat;
return true;
}
}
return false;
}
/*===============================Initialisation===============================*/
/* Add comment here */
sphere* sph_set(float x, float y, float z, float rad, material mat) {
sphere *s = calloc(1, sizeof(*s));
vec cen;
vec_set(&cen, x, y, z);
s->cen = cen;
s->rad = rad;
s->mat = mat;
return s;
}
/*=============================Memory Management==============================*/
/* Add comment here */
void sph_free(sphere *s) {
if(!s) return;
free(s);
}