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