9 #include "polyfill.h" /* just for struct pimage */
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);
23 static struct screen scr = {
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;
37 static struct pimage tex;
39 static int use_bsp = 0;
41 #define LOWRES_SCALE 10
42 static uint16_t *lowres_pixels;
43 static int lowres_width, lowres_height;
48 struct screen *polytest_screen(void)
57 gen_cube_mesh(&cube, 1.0, 0);
58 gen_torus_mesh(&torus, 1.0, 0.25, 24, 12);
60 for(i=0; i<torus.vcount; i++) {
61 torus.varr[i].u *= 4.0;
62 torus.varr[i].v *= 2.0;
67 if(bsp_add_mesh(&torus_bsp, &torus) == -1) {
68 fprintf(stderr, "failed to construct torus BSP tree\n");
73 gen_texture(&tex, 64, 64);
76 lowres_width = FB_WIDTH / LOWRES_SCALE;
77 lowres_height = FB_HEIGHT / LOWRES_SCALE;
78 lowres_pixels = malloc(lowres_width * lowres_height * 2);
79 scr.draw = draw_debug;
85 static void destroy(void)
92 destroy_bsp(&torus_bsp);
96 static void start(long trans_time)
98 g3d_matrix_mode(G3D_PROJECTION);
100 g3d_perspective(50.0, 1.3333333, 1.0, 10.0);
102 g3d_enable(G3D_CULL_FACE);
103 g3d_enable(G3D_LIGHTING);
104 g3d_enable(G3D_LIGHT0);
106 g3d_polygon_mode(G3D_GOURAUD);
107 g3d_enable(G3D_TEXTURE_2D);
109 g3d_clear_color(20, 30, 50);
112 static void update(void)
114 mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
118 static void draw(void)
126 g3d_clear(G3D_COLOR_BUFFER_BIT | G3D_DEPTH_BUFFER_BIT);
127 g3d_enable(G3D_DEPTH_TEST);
129 g3d_clear(G3D_COLOR_BUFFER_BIT);
130 g3d_disable(G3D_DEPTH_TEST);
134 g3d_matrix_mode(G3D_MODELVIEW);
136 g3d_translate(0, 0, -cam_dist);
138 g3d_mult_matrix(sball_matrix);
140 g3d_rotate(cam_phi, 1, 0, 0);
141 g3d_rotate(cam_theta, 0, 1, 0);
144 g3d_light_dir(0, -10, 10, 10);
145 g3d_mtl_diffuse(1.0, 1.0, 1.0);
146 g3d_set_texture(tex.width, tex.height, tex.pixels);
149 /* calc world-space view direction */
150 mat = g3d_get_matrix(G3D_MODELVIEW, 0);
151 /* transform (0, 0, -1) with transpose(mat3x3) */
156 draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]);
167 for(i=0; i<FB_WIDTH*FB_HEIGHT; i++) {
168 unsigned int z = pfill_zbuf[i];
169 fb_pixels[i] = (z >> 5) & 0x7e0;
173 cs_dputs(fb_pixels, 140, 0, use_zbuf ? "zbuffer" : "zsort");
175 /*draw_mesh(&cube);*/
176 swap_buffers(fb_pixels);
179 static void draw_debug(void)
183 memset(lowres_pixels, 0, lowres_width * lowres_height * 2);
185 g3d_matrix_mode(G3D_MODELVIEW);
187 g3d_translate(0, 0, -cam_dist);
188 g3d_rotate(cam_phi, 1, 0, 0);
189 g3d_rotate(cam_theta, 0, 1, 0);
191 g3d_framebuffer(lowres_width, lowres_height, lowres_pixels);
195 draw_lowres_raster();
198 g3d_framebuffer(FB_WIDTH, FB_HEIGHT, fb_pixels);
200 g3d_polygon_mode(G3D_WIRE);
202 g3d_polygon_mode(G3D_FLAT);
204 swap_buffers(fb_pixels);
208 static void draw_huge_pixel(uint16_t *dest, int dest_width, uint16_t color)
211 uint16_t grid_color = PACK_RGB16(127, 127, 127);
213 for(i=0; i<LOWRES_SCALE; i++) {
214 for(j=0; j<LOWRES_SCALE; j++) {
215 dest[j] = i == 0 || j == 0 ? grid_color : color;
221 static void draw_lowres_raster(void)
224 uint16_t *sptr = lowres_pixels;
225 uint16_t *dptr = fb_pixels;
227 for(i=0; i<lowres_height; i++) {
228 for(j=0; j<lowres_width; j++) {
229 draw_huge_pixel(dptr, FB_WIDTH, *sptr++);
230 dptr += LOWRES_SCALE;
232 dptr += FB_WIDTH * LOWRES_SCALE - FB_WIDTH;
236 static void keypress(int key)
241 printf("drawing with %s\n", use_bsp ? "BSP tree" : "z-sorting");
245 use_zbuf = !use_zbuf;
246 printf("Z-buffer %s\n", use_zbuf ? "on" : "off");
251 static int gen_texture(struct pimage *img, int xsz, int ysz)
256 if(!(img->pixels = malloc(xsz * ysz * sizeof *pix))) {
261 for(i=0; i<ysz; i++) {
262 for(j=0; j<xsz; j++) {
263 int val = (i << 2) ^ (j << 2);
265 uint16_t g = val << 1;
266 uint16_t b = val << 2;
268 *pix++ = PACK_RGB16(r, g, b);