bc9d65431493d01302f16fbb16501f4c0c950a1c
[dosdemo] / src / scr / raytrace.c
1 #include <stdio.h>
2 #include <math.h>
3 #include "demo.h"
4 #include "screen.h"
5 #include "gfxutil.h"
6 #include "util.h"
7 #include "cgmath/cgmath.h"
8 #include "rt.h"
9
10 static int init(void);
11 static void destroy(void);
12 static void start(long trans_time);
13 static void draw(void);
14
15 static struct screen scr = {
16         "raytrace",
17         init,
18         destroy,
19         start,
20         0,
21         draw
22 };
23
24 static cgm_vec3 raydir[120][160];
25 static struct rtscene scn;
26
27 struct screen *raytrace_screen(void)
28 {
29         return &scr;
30 }
31
32 static int init(void)
33 {
34         int i, j;
35         float z = 1.0f / tan(cgm_deg_to_rad(25.0f));
36
37         for(i=0; i<120; i++) {
38                 cgm_vec3 *vptr = raydir[i];
39                 float y = 1.0f - (float)i / 60.0f;
40                 for(j=0; j<160; j++) {
41                         vptr->x = ((float)j / 80.0f - 1.0f) * 1.333333f;
42                         vptr->y = y;
43                         vptr->z = z;
44                         vptr++;
45                 }
46         }
47
48         rt_init(&scn);
49
50         rt_color(1, 0, 0);
51         rt_specular(0.8f, 0.8f, 0.8f);
52         rt_shininess(30.0f);
53         rt_add_sphere(&scn, 0, 0, 0, 1);        /* x,y,z, rad */
54
55         rt_color(0.4, 0.4, 0.4);
56         rt_specular(0, 0, 0);
57         rt_shininess(1);
58         rt_add_plane(&scn, 0, 1, 0, -1);                /* nx,ny,nz, dist */
59
60         rt_color(1, 1, 1);
61         rt_add_light(&scn, -8, 15, -10);
62         return 0;
63 }
64
65 static void destroy(void)
66 {
67         rt_destroy(&scn);
68 }
69
70 static void start(long start_time)
71 {
72 }
73
74 static void draw(void)
75 {
76         int i, j, r, g, b;
77         uint16_t pix, *fbptr = fb_pixels;
78
79         for(i=0; i<120; i++) {
80                 for(j=0; j<160; j++) {
81                         cgm_ray ray;
82                         cgm_vec3 col;
83                         ray.dir = raydir[i][j];
84                         cgm_vcons(&ray.origin, 0, 0, -5);
85
86                         if(ray_trace(&ray, &scn, 0, &col)) {
87                                 r = cround64(col.x * 255.0f);
88                                 g = cround64(col.y * 255.0f);
89                                 b = cround64(col.z * 255.0f);
90                                 if(r > 255) r = 255;
91                                 if(g > 255) g = 255;
92                                 if(b > 255) b = 255;
93                                 pix = PACK_RGB16(r, g, b);
94                         } else {
95                                 pix = 0;
96                         }
97                         fbptr[0] = fbptr[1] = fbptr[320] = fbptr[321] = pix;
98                         fbptr += 2;
99                 }
100                 fbptr += 320;
101         }
102
103         swap_buffers(0);
104 }