7 #include "cgmath/cgmath.h"
10 static int init(void);
11 static void destroy(void);
12 static void start(long trans_time);
13 static void draw(void);
15 static struct screen scr = {
24 enum {LASTX = 1, LASTY = 2};
29 struct { int x, y; } cpos[4]; /* corner coordinates */
30 uint16_t *cptr[4]; /* corner pixels */
34 #define NUM_TILES ((320 / TILESZ) * (240 / TILESZ))
36 static cgm_vec3 raydir[240][320];
37 static struct tile tiles[NUM_TILES];
38 static struct rtscene scn;
40 struct screen *raytrace_screen(void)
48 float z = 1.0f / tan(cgm_deg_to_rad(25.0f));
49 struct tile *tptr = tiles;
51 for(i=0; i<240; i++) {
52 cgm_vec3 *vptr = raydir[i];
53 float y = 1.0f - (float)i / 120.0f;
54 for(j=0; j<320; j++) {
55 vptr->x = ((float)j / 160.0f - 1.0f) * 1.333333f;
60 if(((j & (TILESZ-1)) | (i & (TILESZ-1))) == 0) {
63 if(j + TILESZ >= 320) tptr->flags |= LASTX;
64 if(i + TILESZ >= 240) tptr->flags |= LASTY;
68 tptr->cpos[1].x = j + (tptr->flags & LASTX ? TILESZ - 1 : TILESZ);
71 tptr->cpos[2].y = i + (tptr->flags & LASTY ? TILESZ - 1 : TILESZ);
72 tptr->cpos[3].x = tptr->cpos[1].x;
73 tptr->cpos[3].y = tptr->cpos[2].y;
76 tptr->cptr[k] = fb_pixels + tptr->cpos[k].y * 320 + tptr->cpos[k].x;
86 rt_specular(0.8f, 0.8f, 0.8f);
88 rt_add_sphere(&scn, 0, 0, 0, 1); /* x,y,z, rad */
90 rt_color(0.4, 0.4, 0.4);
93 rt_add_plane(&scn, 0, 1, 0, -1); /* nx,ny,nz, dist */
96 rt_add_light(&scn, -8, 15, -10);
100 static void destroy(void)
105 static void start(long start_time)
109 static uint16_t INLINE rend_pixel(int x, int y)
115 ray.dir = raydir[y][x];
116 cgm_vcons(&ray.origin, 0, 0, -5);
118 if(ray_trace(&ray, &scn, 0, &col)) {
119 r = cround64(col.x * 255.0f);
120 g = cround64(col.y * 255.0f);
121 b = cround64(col.z * 255.0f);
125 return PACK_RGB16(r, g, b);
130 #define FBPTR(x, y) (fb_pixels + ((y) << 8) + ((y) << 6) + (x))
132 static void draw(void)
134 int i, j, xbound, ybound;
139 for(i=0; i<NUM_TILES; i++) {
140 *tptr->cptr[0] = rend_pixel(tptr->cpos[0].x, tptr->cpos[0].y);
141 if(tptr->flags & LASTX) {
142 *tptr->cptr[1] = rend_pixel(tptr->cpos[1].x, tptr->cpos[1].y);
143 if(tptr->flags & LASTY) {
144 *tptr->cptr[3] = rend_pixel(tptr->cpos[3].x, tptr->cpos[3].y);
147 if(tptr->flags & LASTY) {
148 *tptr->cptr[2] = rend_pixel(tptr->cpos[2].x, tptr->cpos[2].y);