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 int gen_texture(struct pimage *img, int xsz, int ysz);
21 static struct screen scr = {
30 static float cam_theta, cam_phi = 25;
31 static float cam_dist = 3;
32 static struct g3d_mesh cube, torus;
33 static struct bsptree torus_bsp;
35 static struct pimage tex;
37 static int use_bsp = 1;
39 #define LOWRES_SCALE 10
40 static uint16_t *lowres_pixels;
41 static int lowres_width, lowres_height;
43 struct screen *polytest_screen(void)
52 gen_cube_mesh(&cube, 1.0, 0);
53 gen_torus_mesh(&torus, 1.0, 0.25, 24, 12);
55 for(i=0; i<torus.vcount; i++) {
56 torus.varr[i].u *= 4.0;
57 torus.varr[i].v *= 2.0;
61 if(bsp_add_mesh(&torus_bsp, &torus) == -1) {
62 fprintf(stderr, "failed to construct torus BSP tree\n");
66 gen_texture(&tex, 128, 128);
69 lowres_width = fb_width / LOWRES_SCALE;
70 lowres_height = fb_height / LOWRES_SCALE;
71 lowres_pixels = malloc(lowres_width * lowres_height * 2);
72 scr.draw = draw_debug;
78 static void destroy(void)
84 destroy_bsp(&torus_bsp);
87 static void start(long trans_time)
89 g3d_matrix_mode(G3D_PROJECTION);
91 g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
93 g3d_enable(G3D_CULL_FACE);
94 g3d_enable(G3D_LIGHTING);
95 g3d_enable(G3D_LIGHT0);
97 g3d_polygon_mode(G3D_TEX_GOURAUD);
100 static void update(void)
102 mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
106 static void draw(void)
113 memset(fb_pixels, 0, fb_width * fb_height * 2);
115 g3d_matrix_mode(G3D_MODELVIEW);
117 g3d_translate(0, 0, -cam_dist);
119 g3d_mult_matrix(sball_matrix);
121 g3d_rotate(cam_phi, 1, 0, 0);
122 g3d_rotate(cam_theta, 0, 1, 0);
125 g3d_light_pos(0, -10, 10, 20);
127 g3d_mtl_diffuse(0.4, 0.7, 1.0);
128 g3d_set_texture(tex.width, tex.height, tex.pixels);
131 /* calc world-space view direction */
132 mat = g3d_get_matrix(G3D_MODELVIEW, 0);
133 /* transform (0, 0, -1) with transpose(mat3x3) */
138 draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]);
144 /*draw_mesh(&cube);*/
145 swap_buffers(fb_pixels);
148 static void draw_debug(void)
152 memset(lowres_pixels, 0, lowres_width * lowres_height * 2);
154 g3d_matrix_mode(G3D_MODELVIEW);
156 g3d_translate(0, 0, -cam_dist);
157 g3d_rotate(cam_phi, 1, 0, 0);
158 g3d_rotate(cam_theta, 0, 1, 0);
160 g3d_framebuffer(lowres_width, lowres_height, lowres_pixels);
164 draw_lowres_raster();
167 g3d_framebuffer(fb_width, fb_height, fb_pixels);
169 g3d_polygon_mode(G3D_WIRE);
171 g3d_polygon_mode(G3D_FLAT);
173 swap_buffers(fb_pixels);
177 static void draw_huge_pixel(uint16_t *dest, int dest_width, uint16_t color)
180 uint16_t grid_color = PACK_RGB16(127, 127, 127);
182 for(i=0; i<LOWRES_SCALE; i++) {
183 for(j=0; j<LOWRES_SCALE; j++) {
184 dest[j] = i == 0 || j == 0 ? grid_color : color;
190 static void draw_lowres_raster(void)
193 uint16_t *sptr = lowres_pixels;
194 uint16_t *dptr = fb_pixels;
196 for(i=0; i<lowres_height; i++) {
197 for(j=0; j<lowres_width; j++) {
198 draw_huge_pixel(dptr, fb_width, *sptr++);
199 dptr += LOWRES_SCALE;
201 dptr += fb_width * LOWRES_SCALE - fb_width;
205 static int gen_texture(struct pimage *img, int xsz, int ysz)
210 if(!(img->pixels = malloc(xsz * ysz * sizeof *pix))) {
215 for(i=0; i<ysz; i++) {
216 for(j=0; j<xsz; j++) {
219 *pix++ = PACK_RGB16(val, val, val);