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