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