primary rays without jitter
[cyberay] / src / rt.c
1 #include "rt.h"
2
3 struct framebuffer fb;
4 struct thread_pool *tpool;
5 float view_xform[16];
6 float vfov = M_PI / 4;
7
8 static float aspect;
9
10 static void ray_trace(cgm_vec3 *color, cgm_ray *ray);
11 static void primary_ray(cgm_ray *ray, int x, int y, int sample);
12
13 int fbsize(int width, int height)
14 {
15         void *tmp;
16
17         if(!(tmp = malloc(width * height * sizeof *fb.pixels))) {
18                 return -1;
19         }
20
21         free(fb.pixels);
22         fb.pixels = tmp;
23         fb.width = width;
24         fb.height = height;
25
26         aspect = (float)fb.width / (float)fb.height;
27
28         return 0;
29 }
30
31 void render(void)
32 {
33         int i, j;
34         cgm_ray ray;
35         cgm_vec3 *fbptr = fb.pixels;
36
37         for(i=0; i<fb.height; i++) {
38                 for(j=0; j<fb.width; j++) {
39                         primary_ray(&ray, j, i, 0);
40                         ray_trace(fbptr, &ray);
41                         fbptr++;
42                 }
43         }
44 }
45
46 static void ray_trace(cgm_vec3 *color, cgm_ray *ray)
47 {
48         color->x = ray->dir.x * 0.5f + 0.5f;
49         color->y = ray->dir.y * 0.5f + 0.5f;
50         color->z = 0.0f;
51 }
52
53 static void primary_ray(cgm_ray *ray, int x, int y, int sample)
54 {
55         ray->origin.x = ray->origin.y = ray->origin.z = 0.0f;
56         ray->dir.x = (2.0f * (float)x / (float)fb.width - 1.0f) * aspect;
57         ray->dir.y = 1.0f - 2.0f * (float)y / (float)fb.height;
58         ray->dir.z = -1.0f / tan(vfov / 2.0f);
59         cgm_vnormalize(&ray->dir);
60
61         /* TODO jitter */
62
63         cgm_rmul_mr(ray, view_xform);
64 }