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 = {
30 #define NUM_TILES ((320 / TILESZ) * (240 / TILESZ))
32 static cgm_vec3 raydir[240][320];
33 static struct tile tiles[NUM_TILES];
34 static struct rtscene scn;
36 struct screen *raytrace_screen(void)
44 float z = 1.0f / tan(cgm_deg_to_rad(25.0f));
45 struct tile *tptr = tiles;
47 for(i=0; i<240; i++) {
48 cgm_vec3 *vptr = raydir[i];
49 float y = 1.0f - (float)i / 120.0f;
50 for(j=0; j<320; j++) {
51 vptr->x = ((float)j / 160.0f - 1.0f) * 1.333333f;
56 if(((j & (TILESZ-1)) | (i & (TILESZ-1))) == 0) {
59 tptr->fbptr = fb_pixels + i * 320 + j;
68 rt_specular(0.8f, 0.8f, 0.8f);
70 rt_add_sphere(&scn, 0, 0, 0, 1); /* x,y,z, rad */
72 rt_color(0.4, 0.4, 0.4);
75 rt_add_plane(&scn, 0, 1, 0, -1); /* nx,ny,nz, dist */
78 rt_add_light(&scn, -8, 15, -10);
82 static void destroy(void)
87 static void start(long start_time)
91 static uint16_t INLINE rend_pixel(int x, int y)
97 ray.dir = raydir[y][x];
98 cgm_vcons(&ray.origin, 0, 0, -5);
100 if(ray_trace(&ray, &scn, 0, &col)) {
101 r = cround64(col.x * 255.0f);
102 g = cround64(col.y * 255.0f);
103 b = cround64(col.z * 255.0f);
107 return PACK_RGB16(r, g, b);
112 #define CMPMASK 0xe79c
113 static void rend_tile(uint16_t *fbptr, int x0, int y0, int tsz, unsigned int valid)
116 uint16_t cpix[4], tmp;
117 uint32_t pp0, pp1, pp2, pp3, *fb32;
122 *fbptr = rend_pixel(x0, y0);
132 cptr[1] = fbptr + tsz - 1;
133 cptr[2] = fbptr + (offs << 8) + (offs << 6);
134 cptr[3] = cptr[2] + tsz - 1;
136 cpix[0] = valid & 1 ? *cptr[0] : rend_pixel(x0, y0);
137 cpix[1] = valid & 2 ? *cptr[1] : rend_pixel(x1, y0);
138 cpix[2] = valid & 4 ? *cptr[2] : rend_pixel(x0, y1);
139 cpix[3] = valid & 8 ? *cptr[3] : rend_pixel(x1, y1);
141 tmp = cpix[0] & CMPMASK;
142 if((cpix[1] & CMPMASK) != tmp) goto subdiv;
143 if((cpix[2] & CMPMASK) != tmp) goto subdiv;
144 if((cpix[3] & CMPMASK) != tmp) goto subdiv;
146 pp0 = cpix[0] | ((uint32_t)cpix[0] << 16);
147 pp1 = cpix[1] | ((uint32_t)cpix[1] << 16);
148 pp2 = cpix[2] | ((uint32_t)cpix[2] << 16);
149 pp3 = cpix[3] | ((uint32_t)cpix[3] << 16);
150 fb32 = (uint32_t*)fbptr;
154 fb32[1] = fb32[161] = pp1;
155 fb32[320] = fb32[480] = pp2;
156 fb32[321] = fb32[481] = pp3;
158 fb32[0] = fb32[160] = pp0;
161 fb32[0] = fb32[1] = pp0; fb32[2] = fb32[3] = pp1;
162 fb32[160] = fb32[161] = pp0; fb32[162] = fb32[163] = pp1;
163 fb32[320] = fb32[321] = pp2; fb32[322] = fb32[323] = pp3;
164 fb32[480] = fb32[481] = pp2; fb32[482] = fb32[483] = pp3;
175 rend_tile(fbptr, x0, y0, tsz, 1);
176 rend_tile(fbptr + tsz, x0 + tsz, y0, tsz, 2);
177 fbptr += (tsz << 8) + (tsz << 6);
179 rend_tile(fbptr, x0, y0, tsz, 4);
180 rend_tile(fbptr, x0 + tsz, y0, tsz, 8);
183 static void draw(void)
185 int i, j, xbound, ybound;
190 for(i=0; i<NUM_TILES; i++) {
191 rend_tile(tile->fbptr, tile->x, tile->y, TILESZ, 0);