9 #include "polyfill.h" /* just for struct pimage */
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);
22 static struct screen scr = {
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;
36 static struct pimage tex;
38 static int use_bsp = 1;
40 #define LOWRES_SCALE 10
41 static uint16_t *lowres_pixels;
42 static int lowres_width, lowres_height;
44 struct screen *polytest_screen(void)
53 gen_cube_mesh(&cube, 1.0, 0);
54 gen_torus_mesh(&torus, 1.0, 0.25, 24, 12);
56 for(i=0; i<torus.vcount; i++) {
57 torus.varr[i].u *= 4.0;
58 torus.varr[i].v *= 2.0;
62 if(bsp_add_mesh(&torus_bsp, &torus) == -1) {
63 fprintf(stderr, "failed to construct torus BSP tree\n");
67 gen_texture(&tex, 128, 128);
70 lowres_width = fb_width / LOWRES_SCALE;
71 lowres_height = fb_height / LOWRES_SCALE;
72 lowres_pixels = malloc(lowres_width * lowres_height * 2);
73 scr.draw = draw_debug;
79 static void destroy(void)
85 destroy_bsp(&torus_bsp);
88 static void start(long trans_time)
90 g3d_matrix_mode(G3D_PROJECTION);
92 g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
94 g3d_enable(G3D_CULL_FACE);
95 g3d_enable(G3D_LIGHTING);
96 g3d_enable(G3D_LIGHT0);
98 g3d_polygon_mode(G3D_TEX_GOURAUD);
101 static void update(void)
103 mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
107 static void draw(void)
114 memset(fb_pixels, 0, fb_width * fb_height * 2);
116 g3d_matrix_mode(G3D_MODELVIEW);
118 g3d_translate(0, 0, -cam_dist);
120 g3d_mult_matrix(sball_matrix);
122 g3d_rotate(cam_phi, 1, 0, 0);
123 g3d_rotate(cam_theta, 0, 1, 0);
126 g3d_light_pos(0, -10, 10, 20);
128 g3d_mtl_diffuse(0.4, 0.7, 1.0);
129 g3d_set_texture(tex.width, tex.height, tex.pixels);
132 /* calc world-space view direction */
133 mat = g3d_get_matrix(G3D_MODELVIEW, 0);
134 /* transform (0, 0, -1) with transpose(mat3x3) */
139 draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]);
145 /*draw_mesh(&cube);*/
146 swap_buffers(fb_pixels);
149 static void draw_debug(void)
153 memset(lowres_pixels, 0, lowres_width * lowres_height * 2);
155 g3d_matrix_mode(G3D_MODELVIEW);
157 g3d_translate(0, 0, -cam_dist);
158 g3d_rotate(cam_phi, 1, 0, 0);
159 g3d_rotate(cam_theta, 0, 1, 0);
161 g3d_framebuffer(lowres_width, lowres_height, lowres_pixels);
165 draw_lowres_raster();
168 g3d_framebuffer(fb_width, fb_height, fb_pixels);
170 g3d_polygon_mode(G3D_WIRE);
172 g3d_polygon_mode(G3D_FLAT);
174 swap_buffers(fb_pixels);
178 static void draw_huge_pixel(uint16_t *dest, int dest_width, uint16_t color)
181 uint16_t grid_color = PACK_RGB16(127, 127, 127);
183 for(i=0; i<LOWRES_SCALE; i++) {
184 for(j=0; j<LOWRES_SCALE; j++) {
185 dest[j] = i == 0 || j == 0 ? grid_color : color;
191 static void draw_lowres_raster(void)
194 uint16_t *sptr = lowres_pixels;
195 uint16_t *dptr = fb_pixels;
197 for(i=0; i<lowres_height; i++) {
198 for(j=0; j<lowres_width; j++) {
199 draw_huge_pixel(dptr, fb_width, *sptr++);
200 dptr += LOWRES_SCALE;
202 dptr += fb_width * LOWRES_SCALE - fb_width;
206 static void keypress(int key)
211 printf("drawing with %s\n", use_bsp ? "BSP tree" : "z-sorting");
216 static int gen_texture(struct pimage *img, int xsz, int ysz)
221 if(!(img->pixels = malloc(xsz * ysz * sizeof *pix))) {
226 for(i=0; i<ysz; i++) {
227 for(j=0; j<xsz; j++) {
230 *pix++ = PACK_RGB16(val, val, val);