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