9 #include "polyfill.h" /* just for struct pimage */
13 struct g3d_vertex *varr;
18 static int init(void);
19 static void destroy(void);
20 static void start(long trans_time);
21 static void draw(void);
22 static void draw_mesh(struct mesh *mesh);
23 static int gen_cube(struct mesh *mesh, float sz);
24 static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub);
25 static void zsort(struct mesh *m);
26 static void draw_lowres_raster(void);
27 static int gen_texture(struct pimage *img, int xsz, int ysz);
29 static struct screen scr = {
37 static float theta, phi = 25;
38 static struct mesh cube, torus;
40 static struct pimage tex;
42 #define LOWRES_SCALE 10
43 static uint16_t *lowres_pixels;
44 static int lowres_width, lowres_height;
46 struct screen *polytest_screen(void)
56 gen_torus(&torus, 1.0, 0.25, 24, 12);
58 for(i=0; i<torus.vcount; i++) {
59 torus.varr[i].u *= 4.0;
60 torus.varr[i].v *= 2.0;
63 gen_texture(&tex, 128, 128);
66 lowres_width = fb_width / LOWRES_SCALE;
67 lowres_height = fb_height / LOWRES_SCALE;
68 lowres_pixels = malloc(lowres_width * lowres_height * 2);
69 scr.draw = draw_debug;
75 static void destroy(void)
83 static void start(long trans_time)
85 g3d_matrix_mode(G3D_PROJECTION);
87 g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
89 g3d_enable(G3D_CULL_FACE);
90 g3d_enable(G3D_LIGHTING);
91 g3d_enable(G3D_LIGHT0);
93 g3d_polygon_mode(G3D_TEX_GOURAUD);
96 static void update(void)
98 static int prev_mx, prev_my;
99 static unsigned int prev_bmask;
102 if((mouse_bmask ^ prev_bmask) == 0) {
103 int dx = mouse_x - prev_mx;
104 int dy = mouse_y - prev_my;
110 if(phi < -90) phi = -90;
111 if(phi > 90) phi = 90;
117 prev_bmask = mouse_bmask;
121 static void draw(void)
125 memset(fb_pixels, 0, fb_width * fb_height * 2);
127 g3d_matrix_mode(G3D_MODELVIEW);
129 g3d_translate(0, 0, -3);
130 g3d_rotate(phi, 1, 0, 0);
131 g3d_rotate(theta, 0, 1, 0);
133 g3d_light_pos(0, -10, 10, 20);
137 g3d_mtl_diffuse(0.4, 0.7, 1.0);
138 g3d_set_texture(tex.width, tex.height, tex.pixels);
142 /*draw_mesh(&cube);*/
143 swap_buffers(fb_pixels);
146 static void draw_debug(void)
150 memset(lowres_pixels, 0, lowres_width * lowres_height * 2);
152 g3d_matrix_mode(G3D_MODELVIEW);
154 g3d_translate(0, 0, -3);
155 g3d_rotate(phi, 1, 0, 0);
156 g3d_rotate(theta, 0, 1, 0);
158 g3d_framebuffer(lowres_width, lowres_height, lowres_pixels);
162 draw_lowres_raster();
165 g3d_framebuffer(fb_width, fb_height, fb_pixels);
167 g3d_polygon_mode(G3D_WIRE);
169 g3d_polygon_mode(G3D_FLAT);
171 swap_buffers(fb_pixels);
174 static void draw_mesh(struct mesh *mesh)
177 g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
179 g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
183 #define NORMAL(vp, x, y, z) do { vp->nx = x; vp->ny = y; vp->nz = z; } while(0)
184 #define COLOR(vp, cr, cg, cb) do { vp->r = cr; vp->g = cg; vp->b = cb; } while(0)
185 #define TEXCOORD(vp, tu, tv) do { vp->u = tu; vp->v = tv; } while(0)
186 #define VERTEX(vp, vx, vy, vz) \
188 vp->x = vx; vp->y = vy; vp->z = vz; vp->w = 1.0f; \
192 static int gen_cube(struct mesh *mesh, float sz)
194 struct g3d_vertex *vptr;
195 float hsz = sz / 2.0;
197 mesh->prim = G3D_QUADS;
202 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
208 NORMAL(vptr, 0, 0, -1);
209 COLOR(vptr, 255, 0, 255);
210 VERTEX(vptr, hsz, -hsz, -hsz);
211 VERTEX(vptr, -hsz, -hsz, -hsz);
212 VERTEX(vptr, -hsz, hsz, -hsz);
213 VERTEX(vptr, hsz, hsz, -hsz);
215 NORMAL(vptr, 0, -1, 0);
216 COLOR(vptr, 0, 255, 255);
217 VERTEX(vptr, -hsz, -hsz, -hsz);
218 VERTEX(vptr, hsz, -hsz, -hsz);
219 VERTEX(vptr, hsz, -hsz, hsz);
220 VERTEX(vptr, -hsz, -hsz, hsz);
222 NORMAL(vptr, -1, 0, 0);
223 COLOR(vptr, 255, 255, 0);
224 VERTEX(vptr, -hsz, -hsz, -hsz);
225 VERTEX(vptr, -hsz, -hsz, hsz);
226 VERTEX(vptr, -hsz, hsz, hsz);
227 VERTEX(vptr, -hsz, hsz, -hsz);
229 NORMAL(vptr, 1, 0, 0);
230 COLOR(vptr, 255, 0, 0);
231 VERTEX(vptr, hsz, -hsz, hsz);
232 VERTEX(vptr, hsz, -hsz, -hsz);
233 VERTEX(vptr, hsz, hsz, -hsz);
234 VERTEX(vptr, hsz, hsz, hsz);
236 NORMAL(vptr, 0, 1, 0);
237 COLOR(vptr, 0, 255, 0);
238 VERTEX(vptr, -hsz, hsz, hsz);
239 VERTEX(vptr, hsz, hsz, hsz);
240 VERTEX(vptr, hsz, hsz, -hsz);
241 VERTEX(vptr, -hsz, hsz, -hsz);
243 NORMAL(vptr, 0, 0, 1);
244 COLOR(vptr, 0, 0, 255);
245 VERTEX(vptr, -hsz, -hsz, hsz);
246 VERTEX(vptr, hsz, -hsz, hsz);
247 VERTEX(vptr, hsz, hsz, hsz);
248 VERTEX(vptr, -hsz, hsz, hsz);
253 static void torusvec(float *res, float theta, float phi, float mr, float rr)
258 rx = -cos(phi) * rr + mr;
262 res[0] = rx * sin(theta) + rz * cos(theta);
264 res[2] = -rx * cos(theta) + rz * sin(theta);
267 static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub)
270 int nfaces, uverts, vverts;
271 struct g3d_vertex *vptr;
274 mesh->prim = G3D_QUADS;
276 if(usub < 4) usub = 4;
277 if(vsub < 2) vsub = 2;
282 mesh->vcount = uverts * vverts;
283 nfaces = usub * vsub;
284 mesh->icount = nfaces * 4;
286 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
289 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
295 for(i=0; i<uverts; i++) {
296 float u = (float)i / (float)(uverts - 1);
297 float theta = u * 2.0 * M_PI;
300 torusvec(rcent, theta, 0, rad, 0);
302 for(j=0; j<vverts; j++) {
303 float v = (float)j / (float)(vverts - 1);
304 float phi = v * 2.0 * M_PI;
305 int chess = (i & 1) == (j & 1);
307 torusvec(&vptr->x, theta, phi, rad, ringrad);
309 vptr->nx = (vptr->x - rcent[0]) / ringrad;
310 vptr->ny = (vptr->y - rcent[1]) / ringrad;
311 vptr->nz = (vptr->z - rcent[2]) / ringrad;
314 vptr->r = chess ? 255 : 64;
316 vptr->b = chess ? 64 : 255;
319 if(i < usub && j < vsub) {
320 int idx = i * vverts + j;
323 *iptr++ = idx + vverts + 1;
324 *iptr++ = idx + vverts;
333 struct g3d_vertex *varr;
337 static int zsort_cmp(const void *aptr, const void *bptr)
339 const int16_t *a = (const int16_t*)aptr;
340 const int16_t *b = (const int16_t*)bptr;
342 const float *m = zsort_cls.xform;
344 const struct g3d_vertex *va = zsort_cls.varr + a[0];
345 const struct g3d_vertex *vb = zsort_cls.varr + b[0];
347 float za = m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
348 float zb = m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
350 va = zsort_cls.varr + a[2];
351 vb = zsort_cls.varr + b[2];
353 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
354 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
359 static void zsort(struct mesh *m)
361 int nfaces = m->icount / m->prim;
363 zsort_cls.varr = m->varr;
364 zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
366 qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_cmp);
369 static void draw_huge_pixel(uint16_t *dest, int dest_width, uint16_t color)
372 uint16_t grid_color = PACK_RGB16(127, 127, 127);
374 for(i=0; i<LOWRES_SCALE; i++) {
375 for(j=0; j<LOWRES_SCALE; j++) {
376 dest[j] = i == 0 || j == 0 ? grid_color : color;
382 static void draw_lowres_raster(void)
385 uint16_t *sptr = lowres_pixels;
386 uint16_t *dptr = fb_pixels;
388 for(i=0; i<lowres_height; i++) {
389 for(j=0; j<lowres_width; j++) {
390 draw_huge_pixel(dptr, fb_width, *sptr++);
391 dptr += LOWRES_SCALE;
393 dptr += fb_width * LOWRES_SCALE - fb_width;
397 static int gen_texture(struct pimage *img, int xsz, int ysz)
402 if(!(img->pixels = malloc(xsz * ysz * sizeof *pix))) {
407 for(i=0; i<ysz; i++) {
408 for(j=0; j<xsz; j++) {
411 *pix++ = PACK_RGB16(val, val, val);