-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.c
67 lines (60 loc) · 1.85 KB
/
camera.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
/*
* Filename : camera.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 <stdio.h>
#include <stdbool.h>
#include <float.h>
#include <stdlib.h>
#include <math.h>
/*----------------------------User-defined Headers----------------------------*/
#include "ray.h"
#include "camera.h"
/*----------------------------Function Definitions----------------------------*/
/* Add comment here */
void camera_set(camera *cam, vec lf, vec la, vec up, float vfov,
float ar, float apt, float fd) {
float theta = vfov * M_PI/180.0; /* conversion to radians */
float half_height = 2 * tan(theta/2);
float half_width = ar * half_height;
cam->rad = apt/2;
cam->w = unit(sub(lf, la));
cam->u = unit(cross(up, cam->w));
cam->v = cross(cam->w, cam->u);
cam->ogn = lf;
cam->hor = mul_c(cam->u, half_width * fd);
cam->ver = mul_c(cam->v, half_height * fd);
vec tmp1 = div_c(cam->hor, 2);
vec tmp2 = div_c(cam->ver, 2);
vec tmp3 = add(tmp1, add(tmp2, mul_c(cam->w, fd)));
cam->llc = sub(cam->ogn, tmp3);
}
/* Add comment here */
ray get_ray(camera *cam, float s, float t) {
vec rd = mul_c(rndm_unit_disk(), cam->rad),
off = add(mul_c(cam->u, rd.x), mul_c(cam->v, rd.y)),
tmp = add(mul_c(cam->hor, s), mul_c(cam->ver, t)),
ogn = add(cam->ogn, off);
ray r;
ray_set(&r, ogn, sub(add(cam->llc, tmp), ogn));
return r;
}
vec rndm_unit_disk() {
vec p, tmp;
vec_set(&tmp, 1, 1, 0);
do {
vec rand;
vec_set(&rand, drand48(), drand48(), 0);
p = sub(mul_c(rand, 2), tmp);
} while (dot(p, p) >= 1);
return p;
}