9 #include "polyfill.h" /* just for struct pimage */
13 static int init(void);
14 static void destroy(void);
15 static void start(long trans_time);
16 static void draw(void);
17 static void draw_lowres_raster(void);
18 static int gen_texture(struct pimage *img, int xsz, int ysz);
20 static struct screen scr = {
28 static float cam_theta, cam_phi = 25;
29 static float cam_dist = 3;
30 static struct g3d_mesh cube, torus;
32 static struct pimage tex;
34 #define LOWRES_SCALE 10
35 static uint16_t *lowres_pixels;
36 static int lowres_width, lowres_height;
38 struct screen *polytest_screen(void)
47 gen_cube_mesh(&cube, 1.0, 0);
48 gen_torus_mesh(&torus, 1.0, 0.25, 24, 12);
50 for(i=0; i<torus.vcount; i++) {
51 torus.varr[i].u *= 4.0;
52 torus.varr[i].v *= 2.0;
55 gen_texture(&tex, 128, 128);
58 lowres_width = fb_width / LOWRES_SCALE;
59 lowres_height = fb_height / LOWRES_SCALE;
60 lowres_pixels = malloc(lowres_width * lowres_height * 2);
61 scr.draw = draw_debug;
67 static void destroy(void)
75 static void start(long trans_time)
77 g3d_matrix_mode(G3D_PROJECTION);
79 g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
81 g3d_enable(G3D_CULL_FACE);
82 g3d_enable(G3D_LIGHTING);
83 g3d_enable(G3D_LIGHT0);
85 g3d_polygon_mode(G3D_TEX_GOURAUD);
88 static void update(void)
90 mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
94 static void draw(void)
98 memset(fb_pixels, 0, fb_width * fb_height * 2);
100 g3d_matrix_mode(G3D_MODELVIEW);
102 g3d_translate(0, 0, -cam_dist);
104 g3d_mult_matrix(sball_matrix);
106 g3d_rotate(cam_phi, 1, 0, 0);
107 g3d_rotate(cam_theta, 0, 1, 0);
110 g3d_light_pos(0, -10, 10, 20);
114 g3d_mtl_diffuse(0.4, 0.7, 1.0);
115 g3d_set_texture(tex.width, tex.height, tex.pixels);
119 /*draw_mesh(&cube);*/
120 swap_buffers(fb_pixels);
123 static void draw_debug(void)
127 memset(lowres_pixels, 0, lowres_width * lowres_height * 2);
129 g3d_matrix_mode(G3D_MODELVIEW);
131 g3d_translate(0, 0, -cam_dist);
132 g3d_rotate(cam_phi, 1, 0, 0);
133 g3d_rotate(cam_theta, 0, 1, 0);
135 g3d_framebuffer(lowres_width, lowres_height, lowres_pixels);
139 draw_lowres_raster();
142 g3d_framebuffer(fb_width, fb_height, fb_pixels);
144 g3d_polygon_mode(G3D_WIRE);
146 g3d_polygon_mode(G3D_FLAT);
148 swap_buffers(fb_pixels);
152 static void draw_huge_pixel(uint16_t *dest, int dest_width, uint16_t color)
155 uint16_t grid_color = PACK_RGB16(127, 127, 127);
157 for(i=0; i<LOWRES_SCALE; i++) {
158 for(j=0; j<LOWRES_SCALE; j++) {
159 dest[j] = i == 0 || j == 0 ? grid_color : color;
165 static void draw_lowres_raster(void)
168 uint16_t *sptr = lowres_pixels;
169 uint16_t *dptr = fb_pixels;
171 for(i=0; i<lowres_height; i++) {
172 for(j=0; j<lowres_width; j++) {
173 draw_huge_pixel(dptr, fb_width, *sptr++);
174 dptr += LOWRES_SCALE;
176 dptr += fb_width * LOWRES_SCALE - fb_width;
180 static int gen_texture(struct pimage *img, int xsz, int ysz)
185 if(!(img->pixels = malloc(xsz * ysz * sizeof *pix))) {
190 for(i=0; i<ysz; i++) {
191 for(j=0; j<xsz; j++) {
194 *pix++ = PACK_RGB16(val, val, val);