zbuffer done
[dosdemo] / src / scr / 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 #include "cfgopt.h"
11 #include "mesh.h"
12 #include "bsptree.h"
13 #include "util.h"
14
15 static int init(void);
16 static void destroy(void);
17 static void start(long trans_time);
18 static void draw(void);
19 static void draw_lowres_raster(void);
20 static void keypress(int key);
21 static int gen_texture(struct pimage *img, int xsz, int ysz);
22
23 static struct screen scr = {
24         "polytest",
25         init,
26         destroy,
27         start, 0,
28         draw,
29         keypress
30 };
31
32 static float cam_theta, cam_phi = 25;
33 static float cam_dist = 3;
34 static struct g3d_mesh cube, torus;
35 static struct bsptree torus_bsp;
36
37 static struct pimage tex;
38
39 static int use_bsp = 0;
40
41 #define LOWRES_SCALE    10
42 static uint16_t *lowres_pixels;
43 static int lowres_width, lowres_height;
44
45 static int use_zbuf;
46
47
48 struct screen *polytest_screen(void)
49 {
50         return &scr;
51 }
52
53 static int init(void)
54 {
55         int i;
56
57         gen_cube_mesh(&cube, 1.0, 0);
58         gen_torus_mesh(&torus, 1.0, 0.25, 24, 12);
59         /* scale texcoords */
60         for(i=0; i<torus.vcount; i++) {
61                 torus.varr[i].u *= 4.0;
62                 torus.varr[i].v *= 2.0;
63         }
64
65         /*
66         init_bsp(&torus_bsp);
67         if(bsp_add_mesh(&torus_bsp, &torus) == -1) {
68                 fprintf(stderr, "failed to construct torus BSP tree\n");
69                 return -1;
70         }
71         */
72
73         gen_texture(&tex, 64, 64);
74
75 #ifdef DEBUG_POLYFILL
76         lowres_width = FB_WIDTH / LOWRES_SCALE;
77         lowres_height = FB_HEIGHT / LOWRES_SCALE;
78         lowres_pixels = malloc(lowres_width * lowres_height * 2);
79         scr.draw = draw_debug;
80 #endif
81
82         return 0;
83 }
84
85 static void destroy(void)
86 {
87         free(lowres_pixels);
88         free(cube.varr);
89         free(torus.varr);
90         free(torus.iarr);
91         /*
92         destroy_bsp(&torus_bsp);
93         */
94 }
95
96 static void start(long trans_time)
97 {
98         g3d_matrix_mode(G3D_PROJECTION);
99         g3d_load_identity();
100         g3d_perspective(50.0, 1.3333333, 1.0, 10.0);
101
102         g3d_enable(G3D_CULL_FACE);
103         g3d_enable(G3D_LIGHTING);
104         g3d_enable(G3D_LIGHT0);
105
106         g3d_polygon_mode(G3D_GOURAUD);
107         g3d_enable(G3D_TEXTURE_2D);
108
109         g3d_clear_color(20, 30, 50);
110 }
111
112 static void update(void)
113 {
114         mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
115 }
116
117
118 static void draw(void)
119 {
120         float vdir[3];
121         const float *mat;
122
123         update();
124
125         if(use_zbuf) {
126                 g3d_clear(G3D_COLOR_BUFFER_BIT | G3D_DEPTH_BUFFER_BIT);
127                 g3d_enable(G3D_DEPTH_TEST);
128         } else {
129                 g3d_clear(G3D_COLOR_BUFFER_BIT);
130                 g3d_disable(G3D_DEPTH_TEST);
131         }
132
133
134         g3d_matrix_mode(G3D_MODELVIEW);
135         g3d_load_identity();
136         g3d_translate(0, 0, -cam_dist);
137         if(opt.sball) {
138                 g3d_mult_matrix(sball_matrix);
139         } else {
140                 g3d_rotate(cam_phi, 1, 0, 0);
141                 g3d_rotate(cam_theta, 0, 1, 0);
142         }
143
144         g3d_light_dir(0, -10, 10, 10);
145         g3d_mtl_diffuse(1.0, 1.0, 1.0);
146         g3d_set_texture(tex.width, tex.height, tex.pixels);
147
148         if(use_bsp) {
149                 /* calc world-space view direction */
150                 mat = g3d_get_matrix(G3D_MODELVIEW, 0);
151                 /* transform (0, 0, -1) with transpose(mat3x3) */
152                 vdir[0] = -mat[2];
153                 vdir[1] = -mat[6];
154                 vdir[2] = -mat[10];
155
156                 draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]);
157         } else {
158                 if(!use_zbuf) {
159                         zsort_mesh(&torus);
160                 }
161                 draw_mesh(&torus);
162         }
163
164         /* show zbuffer */
165         /*{
166                 int i;
167                 for(i=0; i<FB_WIDTH*FB_HEIGHT; i++) {
168                         unsigned int z = pfill_zbuf[i];
169                         fb_pixels[i] = (z >> 5) & 0x7e0;
170                 }
171         }*/
172
173         cs_dputs(fb_pixels, 140, 0, use_zbuf ? "zbuffer" : "zsort");
174
175         /*draw_mesh(&cube);*/
176         swap_buffers(fb_pixels);
177 }
178
179 static void draw_debug(void)
180 {
181         update();
182
183         memset(lowres_pixels, 0, lowres_width * lowres_height * 2);
184
185         g3d_matrix_mode(G3D_MODELVIEW);
186         g3d_load_identity();
187         g3d_translate(0, 0, -cam_dist);
188         g3d_rotate(cam_phi, 1, 0, 0);
189         g3d_rotate(cam_theta, 0, 1, 0);
190
191         g3d_framebuffer(lowres_width, lowres_height, lowres_pixels);
192         /*zsort(&torus);*/
193         draw_mesh(&cube);
194
195         draw_lowres_raster();
196
197
198         g3d_framebuffer(FB_WIDTH, FB_HEIGHT, fb_pixels);
199
200         g3d_polygon_mode(G3D_WIRE);
201         draw_mesh(&cube);
202         g3d_polygon_mode(G3D_FLAT);
203
204         swap_buffers(fb_pixels);
205 }
206
207
208 static void draw_huge_pixel(uint16_t *dest, int dest_width, uint16_t color)
209 {
210         int i, j;
211         uint16_t grid_color = PACK_RGB16(127, 127, 127);
212
213         for(i=0; i<LOWRES_SCALE; i++) {
214                 for(j=0; j<LOWRES_SCALE; j++) {
215                         dest[j] = i == 0 || j == 0 ? grid_color : color;
216                 }
217                 dest += dest_width;
218         }
219 }
220
221 static void draw_lowres_raster(void)
222 {
223         int i, j;
224         uint16_t *sptr = lowres_pixels;
225         uint16_t *dptr = fb_pixels;
226
227         for(i=0; i<lowres_height; i++) {
228                 for(j=0; j<lowres_width; j++) {
229                         draw_huge_pixel(dptr, FB_WIDTH, *sptr++);
230                         dptr += LOWRES_SCALE;
231                 }
232                 dptr += FB_WIDTH * LOWRES_SCALE - FB_WIDTH;
233         }
234 }
235
236 static void keypress(int key)
237 {
238         switch(key) {
239         case 'b':
240                 use_bsp = !use_bsp;
241                 printf("drawing with %s\n", use_bsp ? "BSP tree" : "z-sorting");
242                 break;
243
244         case 'z':
245                 use_zbuf = !use_zbuf;
246                 printf("Z-buffer %s\n", use_zbuf ? "on" : "off");
247                 break;
248         }
249 }
250
251 static int gen_texture(struct pimage *img, int xsz, int ysz)
252 {
253         int i, j;
254         uint16_t *pix;
255
256         if(!(img->pixels = malloc(xsz * ysz * sizeof *pix))) {
257                 return -1;
258         }
259         pix = img->pixels;
260
261         for(i=0; i<ysz; i++) {
262                 for(j=0; j<xsz; j++) {
263                         int val = (i << 2) ^ (j << 2);
264                         uint16_t r = val;
265                         uint16_t g = val << 1;
266                         uint16_t b = val << 2;
267
268                         *pix++ = PACK_RGB16(r, g, b);
269                 }
270         }
271
272         img->width = xsz;
273         img->height = ysz;
274         return 0;
275 }