7b0314d67e5ea8ceac4b68a074aa5d1f81701c5b
[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 struct tile {
25         int x, y;
26         uint16_t *fbptr;
27 };
28
29 #define TILESZ          8
30 #define NUM_TILES       ((320 / TILESZ) * (240 / TILESZ))
31
32 static cgm_vec3 raydir[240][320];
33 static struct tile tiles[NUM_TILES];
34 static struct rtscene scn;
35
36 struct screen *raytrace_screen(void)
37 {
38         return &scr;
39 }
40
41 static int init(void)
42 {
43         int i, j, k;
44         float z = 1.0f / tan(cgm_deg_to_rad(25.0f));
45         struct tile *tptr = tiles;
46
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;
52                         vptr->y = y;
53                         vptr->z = z;
54                         vptr++;
55
56                         if(((j & (TILESZ-1)) | (i & (TILESZ-1))) == 0) {
57                                 tptr->x = j;
58                                 tptr->y = i;
59                                 tptr->fbptr = fb_pixels + i * 320 + j;
60                                 tptr++;
61                         }
62                 }
63         }
64
65         rt_init(&scn);
66
67         rt_color(1, 0, 0);
68         rt_specular(0.8f, 0.8f, 0.8f);
69         rt_shininess(30.0f);
70         rt_add_sphere(&scn, 0, 0, 0, 1);        /* x,y,z, rad */
71
72         rt_color(0.4, 0.4, 0.4);
73         rt_specular(0, 0, 0);
74         rt_shininess(1);
75         rt_add_plane(&scn, 0, 1, 0, -1);                /* nx,ny,nz, dist */
76
77         rt_color(1, 1, 1);
78         rt_add_light(&scn, -8, 15, -10);
79         return 0;
80 }
81
82 static void destroy(void)
83 {
84         rt_destroy(&scn);
85 }
86
87 static void start(long start_time)
88 {
89 }
90
91 static uint16_t INLINE rend_pixel(int x, int y)
92 {
93         int r, g, b;
94         cgm_ray ray;
95         cgm_vec3 col;
96
97         ray.dir = raydir[y][x];
98         cgm_vcons(&ray.origin, 0, 0, -5);
99
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);
104                 if(r > 255) r = 255;
105                 if(g > 255) g = 255;
106                 if(b > 255) b = 255;
107                 return PACK_RGB16(r, g, b);
108         }
109         return 0;
110 }
111
112 #define CMPMASK         0xe79c
113 static void rend_tile(uint16_t *fbptr, int x0, int y0, int tsz, unsigned int valid)
114 {
115         uint16_t *cptr[4];
116         uint16_t cpix[4], tmp;
117         uint32_t pp0, pp1, pp2, pp3, *fb32;
118         int x1, y1, offs;
119
120         if(tsz <= 1) {
121                 if(!valid) {
122                         *fbptr = 0xffff;//rend_pixel(x0, y0);
123                 }
124                 return;
125         }
126
127         offs = tsz - 1;
128         x1 = x0 + offs;
129         y1 = y0 + offs;
130
131         cptr[0] = fbptr;
132         cptr[1] = fbptr + tsz - 1;
133         cptr[2] = fbptr + (offs << 8) + (offs << 6);
134         cptr[3] = cptr[2] + tsz - 1;
135
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);
140
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;
145
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;
151
152         switch(tsz) {
153         case 2:
154 #ifdef SUBDBG
155                 pp0 = 0x18ff;
156 #endif
157                 fb32[0] = fb32[160] = pp0;
158                 break;
159         case 4:
160 #ifdef SUBDBG
161                 pp0 = pp1 = pp2 = pp3 = 0x03800380;
162 #endif
163                 fb32[0] = fb32[160] = pp0;
164                 fb32[1] = fb32[161] = pp1;
165                 fb32[320] = fb32[480] = pp2;
166                 fb32[321] = fb32[481] = pp3;
167                 break;
168         case 8:
169 #ifdef SUBDBG
170                 pp1 = pp0 = pp2 = pp3 = 0xe00fe00f;
171 #endif
172                 fb32[0] = fb32[1] = pp0; fb32[2] = fb32[3] = pp1;
173                 fb32[160] = fb32[161] = pp0; fb32[162] = fb32[163] = pp1;
174                 fb32[320] = fb32[321] = pp0; fb32[322] = fb32[323] = pp1;
175                 fb32[480] = fb32[481] = pp0; fb32[482] = fb32[483] = pp1;
176                 fb32[640] = fb32[641] = pp2; fb32[642] = fb32[643] = pp3;
177                 fb32[800] = fb32[801] = pp2; fb32[802] = fb32[803] = pp3;
178                 fb32[960] = fb32[961] = pp2; fb32[962] = fb32[963] = pp3;
179                 fb32[1120] = fb32[1121] = pp2; fb32[1122] = fb32[1123] = pp3;
180                 break;
181         }
182         return;
183
184 subdiv:
185         *cptr[0] = cpix[0];
186         *cptr[1] = cpix[1];
187         *cptr[2] = cpix[2];
188         *cptr[3] = cpix[3];
189
190         tsz >>= 1;
191         rend_tile(fbptr, x0, y0, tsz, 1);
192         rend_tile(fbptr + tsz, x0 + tsz, y0, tsz, 2);
193         fbptr += (tsz << 8) + (tsz << 6);
194         y0 += tsz;
195         rend_tile(fbptr, x0, y0, tsz, 4);
196         rend_tile(fbptr + tsz, x0 + tsz, y0, tsz, 8);
197 }
198
199 static void draw(void)
200 {
201         int i, j, xbound, ybound;
202         uint16_t *fbptr;
203         struct tile *tile;
204
205         tile = tiles;
206         for(i=0; i<NUM_TILES; i++) {
207                 rend_tile(tile->fbptr, tile->x, tile->y, TILESZ, 0);
208                 tile++;
209         }
210
211         swap_buffers(0);
212 }