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);
77 g3d_enable(G3D_LIGHTING);
78 g3d_enable(G3D_LIGHT0);
80 g3d_polygon_mode(G3D_GOURAUD);
83 static void update(void)
85 static int prev_mx, prev_my;
86 static unsigned int prev_bmask;
89 if((mouse_bmask ^ prev_bmask) == 0) {
90 int dx = mouse_x - prev_mx;
91 int dy = mouse_y - prev_my;
97 if(phi < -90) phi = -90;
98 if(phi > 90) phi = 90;
104 prev_bmask = mouse_bmask;
108 static void draw(void)
112 memset(fb_pixels, 0, fb_width * fb_height * 2);
114 g3d_matrix_mode(G3D_MODELVIEW);
116 g3d_translate(0, 0, -3);
117 g3d_rotate(phi, 1, 0, 0);
118 g3d_rotate(theta, 0, 1, 0);
120 g3d_light_pos(0, -10, 10, 20);
124 g3d_mtl_diffuse(0.3, 0.6, 1.0);
127 /*draw_mesh(&cube);*/
128 swap_buffers(fb_pixels);
131 static void draw_debug(void)
135 memset(lowres_pixels, 0, lowres_width * lowres_height * 2);
137 g3d_matrix_mode(G3D_MODELVIEW);
139 g3d_translate(0, 0, -3);
140 g3d_rotate(phi, 1, 0, 0);
141 g3d_rotate(theta, 0, 1, 0);
143 g3d_framebuffer(lowres_width, lowres_height, lowres_pixels);
147 draw_lowres_raster();
150 g3d_framebuffer(fb_width, fb_height, fb_pixels);
152 g3d_polygon_mode(G3D_WIRE);
154 g3d_polygon_mode(G3D_FLAT);
156 swap_buffers(fb_pixels);
159 static void draw_mesh(struct mesh *mesh)
162 g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
164 g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
168 #define NORMAL(vp, x, y, z) do { vp->nx = x; vp->ny = y; vp->nz = z; } while(0)
169 #define COLOR(vp, cr, cg, cb) do { vp->r = cr; vp->g = cg; vp->b = cb; } while(0)
170 #define TEXCOORD(vp, tu, tv) do { vp->u = tu; vp->v = tv; } while(0)
171 #define VERTEX(vp, vx, vy, vz) \
173 vp->x = vx; vp->y = vy; vp->z = vz; vp->w = 1.0f; \
177 static int gen_cube(struct mesh *mesh, float sz)
179 struct g3d_vertex *vptr;
180 float hsz = sz / 2.0;
182 mesh->prim = G3D_QUADS;
187 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
193 NORMAL(vptr, 0, 0, -1);
194 COLOR(vptr, 255, 0, 255);
195 VERTEX(vptr, hsz, -hsz, -hsz);
196 VERTEX(vptr, -hsz, -hsz, -hsz);
197 VERTEX(vptr, -hsz, hsz, -hsz);
198 VERTEX(vptr, hsz, hsz, -hsz);
200 NORMAL(vptr, 0, -1, 0);
201 COLOR(vptr, 0, 255, 255);
202 VERTEX(vptr, -hsz, -hsz, -hsz);
203 VERTEX(vptr, hsz, -hsz, -hsz);
204 VERTEX(vptr, hsz, -hsz, hsz);
205 VERTEX(vptr, -hsz, -hsz, hsz);
207 NORMAL(vptr, -1, 0, 0);
208 COLOR(vptr, 255, 255, 0);
209 VERTEX(vptr, -hsz, -hsz, -hsz);
210 VERTEX(vptr, -hsz, -hsz, hsz);
211 VERTEX(vptr, -hsz, hsz, hsz);
212 VERTEX(vptr, -hsz, hsz, -hsz);
214 NORMAL(vptr, 1, 0, 0);
215 COLOR(vptr, 255, 0, 0);
216 VERTEX(vptr, hsz, -hsz, hsz);
217 VERTEX(vptr, hsz, -hsz, -hsz);
218 VERTEX(vptr, hsz, hsz, -hsz);
219 VERTEX(vptr, hsz, hsz, hsz);
221 NORMAL(vptr, 0, 1, 0);
222 COLOR(vptr, 0, 255, 0);
223 VERTEX(vptr, -hsz, hsz, hsz);
224 VERTEX(vptr, hsz, hsz, hsz);
225 VERTEX(vptr, hsz, hsz, -hsz);
226 VERTEX(vptr, -hsz, hsz, -hsz);
228 NORMAL(vptr, 0, 0, 1);
229 COLOR(vptr, 0, 0, 255);
230 VERTEX(vptr, -hsz, -hsz, hsz);
231 VERTEX(vptr, hsz, -hsz, hsz);
232 VERTEX(vptr, hsz, hsz, hsz);
233 VERTEX(vptr, -hsz, hsz, hsz);
238 static void torusvec(float *res, float theta, float phi, float mr, float rr)
243 rx = -cos(phi) * rr + mr;
247 res[0] = rx * sin(theta) + rz * cos(theta);
249 res[2] = -rx * cos(theta) + rz * sin(theta);
252 static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub)
255 int nfaces, uverts, vverts;
256 struct g3d_vertex *vptr;
259 mesh->prim = G3D_QUADS;
261 if(usub < 4) usub = 4;
262 if(vsub < 2) vsub = 2;
267 mesh->vcount = uverts * vverts;
268 nfaces = usub * vsub;
269 mesh->icount = nfaces * 4;
271 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
274 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
280 for(i=0; i<uverts; i++) {
281 float u = (float)i / (float)(uverts - 1);
282 float theta = u * 2.0 * M_PI;
285 torusvec(rcent, theta, 0, rad, 0);
287 for(j=0; j<vverts; j++) {
288 float v = (float)j / (float)(vverts - 1);
289 float phi = v * 2.0 * M_PI;
290 int chess = (i & 1) == (j & 1);
292 torusvec(&vptr->x, theta, phi, rad, ringrad);
294 vptr->nx = (vptr->x - rcent[0]) / ringrad;
295 vptr->ny = (vptr->y - rcent[1]) / ringrad;
296 vptr->nz = (vptr->z - rcent[2]) / ringrad;
299 vptr->r = chess ? 255 : 64;
301 vptr->b = chess ? 64 : 255;
304 if(i < usub && j < vsub) {
305 int idx = i * vverts + j;
308 *iptr++ = idx + vverts + 1;
309 *iptr++ = idx + vverts;
318 struct g3d_vertex *varr;
322 static int zsort_cmp(const void *aptr, const void *bptr)
324 const int16_t *a = (const int16_t*)aptr;
325 const int16_t *b = (const int16_t*)bptr;
327 const float *m = zsort_cls.xform;
329 const struct g3d_vertex *va = zsort_cls.varr + a[0];
330 const struct g3d_vertex *vb = zsort_cls.varr + b[0];
332 float za = m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
333 float zb = m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
335 va = zsort_cls.varr + a[2];
336 vb = zsort_cls.varr + b[2];
338 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
339 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
344 static void zsort(struct mesh *m)
346 int nfaces = m->icount / m->prim;
348 zsort_cls.varr = m->varr;
349 zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
351 qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_cmp);
354 static void draw_huge_pixel(uint16_t *dest, int dest_width, uint16_t color)
357 uint16_t grid_color = PACK_RGB16(127, 127, 127);
359 for(i=0; i<LOWRES_SCALE; i++) {
360 for(j=0; j<LOWRES_SCALE; j++) {
361 dest[j] = i == 0 || j == 0 ? grid_color : color;
367 static void draw_lowres_raster(void)
370 uint16_t *sptr = lowres_pixels;
371 uint16_t *dptr = fb_pixels;
373 for(i=0; i<lowres_height; i++) {
374 for(j=0; j<lowres_width; j++) {
375 draw_huge_pixel(dptr, fb_width, *sptr++);
376 dptr += LOWRES_SCALE;
378 dptr += fb_width * LOWRES_SCALE - fb_width;