cc64a0d3c3484c901303a1ae6093246c8f2664cc
[dosdemo] / src / polytest.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include "screen.h"
6 #include "demo.h"
7 #include "3dgfx.h"
8 #include "gfxutil.h"
9 #include "polyfill.h"   /* just for struct pimage */
10
11 struct mesh {
12         int prim;
13         struct g3d_vertex *varr;
14         int16_t *iarr;
15         int vcount, icount;
16 };
17
18 static int init(void);
19 static void destroy(void);
20 static void start(long trans_time);
21 static void draw(void);
22 static void draw_mesh(struct mesh *mesh);
23 static int gen_cube(struct mesh *mesh, float sz);
24 static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub);
25 static void zsort(struct mesh *m);
26 static void draw_lowres_raster(void);
27 static int gen_texture(struct pimage *img, int xsz, int ysz);
28
29 static struct screen scr = {
30         "polytest",
31         init,
32         destroy,
33         start, 0,
34         draw
35 };
36
37 static float cam_theta, cam_phi = 25;
38 static float cam_dist = 3;
39 static struct mesh cube, torus;
40
41 static struct pimage tex;
42
43 #define LOWRES_SCALE    10
44 static uint16_t *lowres_pixels;
45 static int lowres_width, lowres_height;
46
47 struct screen *polytest_screen(void)
48 {
49         return &scr;
50 }
51
52 static int init(void)
53 {
54         int i;
55
56         gen_cube(&cube, 1.0);
57         gen_torus(&torus, 1.0, 0.25, 24, 12);
58         /* scale texcoords */
59         for(i=0; i<torus.vcount; i++) {
60                 torus.varr[i].u *= 4.0;
61                 torus.varr[i].v *= 2.0;
62         }
63
64         gen_texture(&tex, 128, 128);
65
66 #ifdef DEBUG_POLYFILL
67         lowres_width = fb_width / LOWRES_SCALE;
68         lowres_height = fb_height / LOWRES_SCALE;
69         lowres_pixels = malloc(lowres_width * lowres_height * 2);
70         scr.draw = draw_debug;
71 #endif
72
73         return 0;
74 }
75
76 static void destroy(void)
77 {
78         free(lowres_pixels);
79         free(cube.varr);
80         free(torus.varr);
81         free(torus.iarr);
82 }
83
84 static void start(long trans_time)
85 {
86         g3d_matrix_mode(G3D_PROJECTION);
87         g3d_load_identity();
88         g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
89
90         g3d_enable(G3D_CULL_FACE);
91         g3d_enable(G3D_LIGHTING);
92         g3d_enable(G3D_LIGHT0);
93
94         g3d_polygon_mode(G3D_TEX_GOURAUD);
95 }
96
97 static void update(void)
98 {
99         static int prev_mx, prev_my;
100         static unsigned int prev_bmask;
101
102         if(mouse_bmask) {
103                 if((mouse_bmask ^ prev_bmask) == 0) {
104                         int dx = mouse_x - prev_mx;
105                         int dy = mouse_y - prev_my;
106
107                         if(dx || dy) {
108                                 if(mouse_bmask & 1) {
109                                         cam_theta += dx * 1.0;
110                                         cam_phi += dy * 1.0;
111
112                                         if(cam_phi < -90) cam_phi = -90;
113                                         if(cam_phi > 90) cam_phi = 90;
114                                 }
115                                 if(mouse_bmask & 4) {
116                                         cam_dist += dy * 0.5;
117
118                                         if(cam_dist < 0) cam_dist = 0;
119                                 }
120                         }
121                 }
122         }
123         prev_mx = mouse_x;
124         prev_my = mouse_y;
125         prev_bmask = mouse_bmask;
126 }
127
128
129 static void draw(void)
130 {
131         update();
132
133         memset(fb_pixels, 0, fb_width * fb_height * 2);
134
135         g3d_matrix_mode(G3D_MODELVIEW);
136         g3d_load_identity();
137         g3d_translate(0, 0, -cam_dist);
138         g3d_rotate(cam_phi, 1, 0, 0);
139         g3d_rotate(cam_theta, 0, 1, 0);
140
141         g3d_light_pos(0, -10, 10, 20);
142
143         zsort(&torus);
144
145         g3d_mtl_diffuse(0.4, 0.7, 1.0);
146         g3d_set_texture(tex.width, tex.height, tex.pixels);
147
148         draw_mesh(&torus);
149
150         /*draw_mesh(&cube);*/
151         swap_buffers(fb_pixels);
152 }
153
154 static void draw_debug(void)
155 {
156         update();
157
158         memset(lowres_pixels, 0, lowres_width * lowres_height * 2);
159
160         g3d_matrix_mode(G3D_MODELVIEW);
161         g3d_load_identity();
162         g3d_translate(0, 0, -cam_dist);
163         g3d_rotate(cam_phi, 1, 0, 0);
164         g3d_rotate(cam_theta, 0, 1, 0);
165
166         g3d_framebuffer(lowres_width, lowres_height, lowres_pixels);
167         /*zsort(&torus);*/
168         draw_mesh(&cube);
169
170         draw_lowres_raster();
171
172
173         g3d_framebuffer(fb_width, fb_height, fb_pixels);
174
175         g3d_polygon_mode(G3D_WIRE);
176         draw_mesh(&cube);
177         g3d_polygon_mode(G3D_FLAT);
178
179         swap_buffers(fb_pixels);
180 }
181
182 static void draw_mesh(struct mesh *mesh)
183 {
184         if(mesh->iarr) {
185                 g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
186         } else {
187                 g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
188         }
189 }
190
191 #define NORMAL(vp, x, y, z) do { vp->nx = x; vp->ny = y; vp->nz = z; } while(0)
192 #define COLOR(vp, cr, cg, cb) do { vp->r = cr; vp->g = cg; vp->b = cb; } while(0)
193 #define TEXCOORD(vp, tu, tv) do { vp->u = tu; vp->v = tv; } while(0)
194 #define VERTEX(vp, vx, vy, vz) \
195         do { \
196                 vp->x = vx; vp->y = vy; vp->z = vz; vp->w = 1.0f; \
197                 ++vp; \
198         } while(0)
199
200 static int gen_cube(struct mesh *mesh, float sz)
201 {
202         struct g3d_vertex *vptr;
203         float hsz = sz / 2.0;
204
205         mesh->prim = G3D_QUADS;
206         mesh->iarr = 0;
207         mesh->icount = 0;
208
209         mesh->vcount = 24;
210         if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
211                 return -1;
212         }
213         vptr = mesh->varr;
214
215         /* -Z */
216         NORMAL(vptr, 0, 0, -1);
217         COLOR(vptr, 255, 0, 255);
218         VERTEX(vptr, hsz, -hsz, -hsz);
219         VERTEX(vptr, -hsz, -hsz, -hsz);
220         VERTEX(vptr, -hsz, hsz, -hsz);
221         VERTEX(vptr, hsz, hsz, -hsz);
222         /* -Y */
223         NORMAL(vptr, 0, -1, 0);
224         COLOR(vptr, 0, 255, 255);
225         VERTEX(vptr, -hsz, -hsz, -hsz);
226         VERTEX(vptr, hsz, -hsz, -hsz);
227         VERTEX(vptr, hsz, -hsz, hsz);
228         VERTEX(vptr, -hsz, -hsz, hsz);
229         /* -X */
230         NORMAL(vptr, -1, 0, 0);
231         COLOR(vptr, 255, 255, 0);
232         VERTEX(vptr, -hsz, -hsz, -hsz);
233         VERTEX(vptr, -hsz, -hsz, hsz);
234         VERTEX(vptr, -hsz, hsz, hsz);
235         VERTEX(vptr, -hsz, hsz, -hsz);
236         /* +X */
237         NORMAL(vptr, 1, 0, 0);
238         COLOR(vptr, 255, 0, 0);
239         VERTEX(vptr, hsz, -hsz, hsz);
240         VERTEX(vptr, hsz, -hsz, -hsz);
241         VERTEX(vptr, hsz, hsz, -hsz);
242         VERTEX(vptr, hsz, hsz, hsz);
243         /* +Y */
244         NORMAL(vptr, 0, 1, 0);
245         COLOR(vptr, 0, 255, 0);
246         VERTEX(vptr, -hsz, hsz, hsz);
247         VERTEX(vptr, hsz, hsz, hsz);
248         VERTEX(vptr, hsz, hsz, -hsz);
249         VERTEX(vptr, -hsz, hsz, -hsz);
250         /* +Z */
251         NORMAL(vptr, 0, 0, 1);
252         COLOR(vptr, 0, 0, 255);
253         VERTEX(vptr, -hsz, -hsz, hsz);
254         VERTEX(vptr, hsz, -hsz, hsz);
255         VERTEX(vptr, hsz, hsz, hsz);
256         VERTEX(vptr, -hsz, hsz, hsz);
257
258         return 0;
259 }
260
261 static void torusvec(float *res, float theta, float phi, float mr, float rr)
262 {
263         float rx, ry, rz;
264         theta = -theta;
265
266         rx = -cos(phi) * rr + mr;
267         ry = sin(phi) * rr;
268         rz = 0.0f;
269
270         res[0] = rx * sin(theta) + rz * cos(theta);
271         res[1] = ry;
272         res[2] = -rx * cos(theta) + rz * sin(theta);
273 }
274
275 static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub)
276 {
277         int i, j;
278         int nfaces, uverts, vverts;
279         struct g3d_vertex *vptr;
280         int16_t *iptr;
281
282         mesh->prim = G3D_QUADS;
283
284         if(usub < 4) usub = 4;
285         if(vsub < 2) vsub = 2;
286
287         uverts = usub + 1;
288         vverts = vsub + 1;
289
290         mesh->vcount = uverts * vverts;
291         nfaces = usub * vsub;
292         mesh->icount = nfaces * 4;
293
294         printf("generating torus with %d faces (%d vertices)\n", nfaces, mesh->vcount);
295
296         if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
297                 return -1;
298         }
299         if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
300                 return -1;
301         }
302         vptr = mesh->varr;
303         iptr = mesh->iarr;
304
305         for(i=0; i<uverts; i++) {
306                 float u = (float)i / (float)(uverts - 1);
307                 float theta = u * 2.0 * M_PI;
308                 float rcent[3];
309
310                 torusvec(rcent, theta, 0, rad, 0);
311
312                 for(j=0; j<vverts; j++) {
313                         float v = (float)j / (float)(vverts - 1);
314                         float phi = v * 2.0 * M_PI;
315                         int chess = (i & 1) == (j & 1);
316
317                         torusvec(&vptr->x, theta, phi, rad, ringrad);
318
319                         vptr->nx = (vptr->x - rcent[0]) / ringrad;
320                         vptr->ny = (vptr->y - rcent[1]) / ringrad;
321                         vptr->nz = (vptr->z - rcent[2]) / ringrad;
322                         vptr->u = u;
323                         vptr->v = v;
324                         vptr->r = chess ? 255 : 64;
325                         vptr->g = 128;
326                         vptr->b = chess ? 64 : 255;
327                         ++vptr;
328
329                         if(i < usub && j < vsub) {
330                                 int idx = i * vverts + j;
331                                 *iptr++ = idx;
332                                 *iptr++ = idx + 1;
333                                 *iptr++ = idx + vverts + 1;
334                                 *iptr++ = idx + vverts;
335                         }
336                 }
337         }
338         return 0;
339 }
340
341
342 static struct {
343         struct g3d_vertex *varr;
344         const float *xform;
345 } zsort_cls;
346
347 static int zsort_cmp(const void *aptr, const void *bptr)
348 {
349         const int16_t *a = (const int16_t*)aptr;
350         const int16_t *b = (const int16_t*)bptr;
351
352         const float *m = zsort_cls.xform;
353
354         const struct g3d_vertex *va = zsort_cls.varr + a[0];
355         const struct g3d_vertex *vb = zsort_cls.varr + b[0];
356
357         float za = m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
358         float zb = m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
359
360         va = zsort_cls.varr + a[2];
361         vb = zsort_cls.varr + b[2];
362
363         za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
364         zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
365
366         return za - zb;
367 }
368
369 static void zsort(struct mesh *m)
370 {
371         int nfaces = m->icount / m->prim;
372
373         zsort_cls.varr = m->varr;
374         zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
375
376         qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_cmp);
377 }
378
379 static void draw_huge_pixel(uint16_t *dest, int dest_width, uint16_t color)
380 {
381         int i, j;
382         uint16_t grid_color = PACK_RGB16(127, 127, 127);
383
384         for(i=0; i<LOWRES_SCALE; i++) {
385                 for(j=0; j<LOWRES_SCALE; j++) {
386                         dest[j] = i == 0 || j == 0 ? grid_color : color;
387                 }
388                 dest += dest_width;
389         }
390 }
391
392 static void draw_lowres_raster(void)
393 {
394         int i, j;
395         uint16_t *sptr = lowres_pixels;
396         uint16_t *dptr = fb_pixels;
397
398         for(i=0; i<lowres_height; i++) {
399                 for(j=0; j<lowres_width; j++) {
400                         draw_huge_pixel(dptr, fb_width, *sptr++);
401                         dptr += LOWRES_SCALE;
402                 }
403                 dptr += fb_width * LOWRES_SCALE - fb_width;
404         }
405 }
406
407 static int gen_texture(struct pimage *img, int xsz, int ysz)
408 {
409         int i, j;
410         uint16_t *pix;
411
412         if(!(img->pixels = malloc(xsz * ysz * sizeof *pix))) {
413                 return -1;
414         }
415         pix = img->pixels;
416
417         for(i=0; i<ysz; i++) {
418                 for(j=0; j<xsz; j++) {
419                         int val = i ^ j;
420
421                         *pix++ = PACK_RGB16(val, val, val);
422                 }
423         }
424
425         img->width = xsz;
426         img->height = ysz;
427         return 0;
428 }