12 struct g3d_vertex *varr;
17 static int init(void);
18 static void destroy(void);
19 static void start(long trans_time);
20 static void draw(void);
21 static void draw_mesh(struct mesh *mesh);
22 static int gen_cube(struct mesh *mesh, float sz);
23 static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub);
24 static void zsort(struct mesh *m);
25 static void draw_lowres_raster(void);
27 static struct screen scr = {
35 static float theta, phi = 25;
36 static struct mesh cube, torus;
38 #define LOWRES_SCALE 10
39 static uint16_t *lowres_pixels;
40 static int lowres_width, lowres_height;
42 struct screen *polytest_screen(void)
50 gen_torus(&torus, 1.0, 0.25, 24, 12);
53 lowres_width = fb_width / LOWRES_SCALE;
54 lowres_height = fb_height / LOWRES_SCALE;
55 lowres_pixels = malloc(lowres_width * lowres_height * 2);
56 scr.draw = draw_debug;
62 static void destroy(void)
70 static void start(long trans_time)
72 g3d_matrix_mode(G3D_PROJECTION);
74 g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
76 g3d_enable(G3D_CULL_FACE);
79 static void update(void)
81 static int prev_mx, prev_my;
82 static unsigned int prev_bmask;
85 if((mouse_bmask ^ prev_bmask) == 0) {
86 int dx = mouse_x - prev_mx;
87 int dy = mouse_y - prev_my;
93 if(phi < -90) phi = -90;
94 if(phi > 90) phi = 90;
100 prev_bmask = mouse_bmask;
104 static void draw(void)
108 memset(fb_pixels, 0, fb_width * fb_height * 2);
110 g3d_matrix_mode(G3D_MODELVIEW);
112 g3d_translate(0, 0, -3);
113 g3d_rotate(phi, 1, 0, 0);
114 g3d_rotate(theta, 0, 1, 0);
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, -3);
132 g3d_rotate(phi, 1, 0, 0);
133 g3d_rotate(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);
151 static void draw_mesh(struct mesh *mesh)
154 g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
156 g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
160 #define NORMAL(vp, x, y, z) do { vp->nx = x; vp->ny = y; vp->nz = z; } while(0)
161 #define COLOR(vp, cr, cg, cb) do { vp->r = cr; vp->g = cg; vp->b = cb; } while(0)
162 #define TEXCOORD(vp, tu, tv) do { vp->u = tu; vp->v = tv; } while(0)
163 #define VERTEX(vp, vx, vy, vz) \
165 vp->x = vx; vp->y = vy; vp->z = vz; vp->w = 1.0f; \
169 static int gen_cube(struct mesh *mesh, float sz)
171 struct g3d_vertex *vptr;
172 float hsz = sz / 2.0;
174 mesh->prim = G3D_QUADS;
179 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
185 NORMAL(vptr, 0, 0, -1);
186 COLOR(vptr, 255, 0, 255);
187 VERTEX(vptr, hsz, -hsz, -hsz);
188 VERTEX(vptr, -hsz, -hsz, -hsz);
189 VERTEX(vptr, -hsz, hsz, -hsz);
190 VERTEX(vptr, hsz, hsz, -hsz);
192 NORMAL(vptr, 0, -1, 0);
193 COLOR(vptr, 0, 255, 255);
194 VERTEX(vptr, -hsz, -hsz, -hsz);
195 VERTEX(vptr, hsz, -hsz, -hsz);
196 VERTEX(vptr, hsz, -hsz, hsz);
197 VERTEX(vptr, -hsz, -hsz, hsz);
199 NORMAL(vptr, -1, 0, 0);
200 COLOR(vptr, 255, 255, 0);
201 VERTEX(vptr, -hsz, -hsz, -hsz);
202 VERTEX(vptr, -hsz, -hsz, hsz);
203 VERTEX(vptr, -hsz, hsz, hsz);
204 VERTEX(vptr, -hsz, hsz, -hsz);
206 NORMAL(vptr, 1, 0, 0);
207 COLOR(vptr, 255, 0, 0);
208 VERTEX(vptr, hsz, -hsz, hsz);
209 VERTEX(vptr, hsz, -hsz, -hsz);
210 VERTEX(vptr, hsz, hsz, -hsz);
211 VERTEX(vptr, hsz, hsz, hsz);
213 NORMAL(vptr, 0, 1, 0);
214 COLOR(vptr, 0, 255, 0);
215 VERTEX(vptr, -hsz, hsz, hsz);
216 VERTEX(vptr, hsz, hsz, hsz);
217 VERTEX(vptr, hsz, hsz, -hsz);
218 VERTEX(vptr, -hsz, hsz, -hsz);
220 NORMAL(vptr, 0, 0, 1);
221 COLOR(vptr, 0, 0, 255);
222 VERTEX(vptr, -hsz, -hsz, hsz);
223 VERTEX(vptr, hsz, -hsz, hsz);
224 VERTEX(vptr, hsz, hsz, hsz);
225 VERTEX(vptr, -hsz, hsz, hsz);
230 static void torusvec(float *res, float theta, float phi, float mr, float rr)
235 rx = -cos(phi) * rr + mr;
239 res[0] = rx * sin(theta) + rz * cos(theta);
241 res[2] = -rx * cos(theta) + rz * sin(theta);
244 static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub)
247 int nfaces, uverts, vverts;
248 struct g3d_vertex *vptr;
251 mesh->prim = G3D_QUADS;
253 if(usub < 4) usub = 4;
254 if(vsub < 2) vsub = 2;
259 mesh->vcount = uverts * vverts;
260 nfaces = usub * vsub;
261 mesh->icount = nfaces * 4;
263 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
266 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
272 for(i=0; i<uverts; i++) {
273 float u = (float)i / (float)(uverts - 1);
274 float theta = u * 2.0 * M_PI;
277 torusvec(rcent, theta, 0, rad, 0);
279 for(j=0; j<vverts; j++) {
280 float v = (float)j / (float)(vverts - 1);
281 float phi = v * 2.0 * M_PI;
282 int chess = (i & 1) == (j & 1);
284 torusvec(&vptr->x, theta, phi, rad, ringrad);
286 vptr->nx = (vptr->x - rcent[0]) / ringrad;
287 vptr->ny = (vptr->y - rcent[1]) / ringrad;
288 vptr->nz = (vptr->z - rcent[2]) / ringrad;
291 vptr->r = chess ? 255 : 64;
293 vptr->b = chess ? 64 : 255;
296 if(i < usub && j < vsub) {
297 int idx = i * vverts + j;
300 *iptr++ = idx + vverts + 1;
301 *iptr++ = idx + vverts;
310 struct g3d_vertex *varr;
314 static int zsort_cmp(const void *aptr, const void *bptr)
316 const int16_t *a = (const int16_t*)aptr;
317 const int16_t *b = (const int16_t*)bptr;
319 const float *m = zsort_cls.xform;
321 const struct g3d_vertex *va = zsort_cls.varr + a[0];
322 const struct g3d_vertex *vb = zsort_cls.varr + b[0];
324 float za = m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
325 float zb = m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
327 va = zsort_cls.varr + a[2];
328 vb = zsort_cls.varr + b[2];
330 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
331 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
336 static void zsort(struct mesh *m)
338 int nfaces = m->icount / m->prim;
340 zsort_cls.varr = m->varr;
341 zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
343 qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_cmp);
346 static void draw_huge_pixel(uint16_t *dest, int dest_width, uint16_t color)
349 uint16_t grid_color = PACK_RGB16(127, 127, 127);
351 for(i=0; i<LOWRES_SCALE; i++) {
352 for(j=0; j<LOWRES_SCALE; j++) {
353 dest[j] = i == 0 || j == 0 ? grid_color : color;
359 static void draw_lowres_raster(void)
362 uint16_t *sptr = lowres_pixels;
363 uint16_t *dptr = fb_pixels;
365 for(i=0; i<lowres_height; i++) {
366 for(j=0; j<lowres_width; j++) {
367 draw_huge_pixel(dptr, fb_width, *sptr++);
368 dptr += LOWRES_SCALE;
370 dptr += fb_width * LOWRES_SCALE - fb_width;