11 struct g3d_vertex *varr;
16 static int init(void);
17 static void destroy(void);
18 static void start(long trans_time);
19 static void draw(void);
20 static void draw_mesh(struct mesh *mesh);
21 static int gen_cube(struct mesh *mesh, float sz);
22 static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub);
23 static void zsort(struct mesh *m);
25 static struct screen scr = {
33 static struct mesh cube, torus;
35 struct screen *polytest_screen(void)
43 gen_torus(&torus, 1.0, 0.25, 24, 12);
47 static void destroy(void)
54 static void start(long trans_time)
56 g3d_matrix_mode(G3D_PROJECTION);
58 g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
60 g3d_enable(G3D_CULL_FACE);
63 static void draw(void)
65 static int prev_mx, prev_my;
66 static float theta, phi = 25;
68 int dx = mouse_x - prev_mx;
69 int dy = mouse_y - prev_my;
77 if(phi < -90) phi = -90;
78 if(phi > 90) phi = 90;
81 /*float angle = (float)time_msec / 50.0;*/
83 memset(fb_pixels, 0, fb_width * fb_height * 2);
85 g3d_matrix_mode(G3D_MODELVIEW);
87 g3d_translate(0, 0, -5);
88 g3d_rotate(phi, 1, 0, 0);
89 g3d_rotate(theta, 0, 1, 0);
94 swap_buffers(fb_pixels);
97 static void draw_mesh(struct mesh *mesh)
100 g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
102 g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
106 #define NORMAL(vp, x, y, z) do { vp->nx = x; vp->ny = y; vp->nz = z; } while(0)
107 #define COLOR(vp, cr, cg, cb) do { vp->r = cr; vp->g = cg; vp->b = cb; } while(0)
108 #define TEXCOORD(vp, tu, tv) do { vp->u = tu; vp->v = tv; } while(0)
109 #define VERTEX(vp, vx, vy, vz) \
111 vp->x = vx; vp->y = vy; vp->z = vz; vp->w = 1.0f; \
115 static int gen_cube(struct mesh *mesh, float sz)
117 struct g3d_vertex *vptr;
118 float hsz = sz / 2.0;
120 mesh->prim = G3D_QUADS;
125 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
131 NORMAL(vptr, 0, 0, -1);
132 COLOR(vptr, 255, 0, 255);
133 VERTEX(vptr, hsz, -hsz, -hsz);
134 VERTEX(vptr, -hsz, -hsz, -hsz);
135 VERTEX(vptr, -hsz, hsz, -hsz);
136 VERTEX(vptr, hsz, hsz, -hsz);
138 NORMAL(vptr, 0, -1, 0);
139 COLOR(vptr, 0, 255, 255);
140 VERTEX(vptr, -hsz, -hsz, -hsz);
141 VERTEX(vptr, hsz, -hsz, -hsz);
142 VERTEX(vptr, hsz, -hsz, hsz);
143 VERTEX(vptr, -hsz, -hsz, hsz);
145 NORMAL(vptr, -1, 0, 0);
146 COLOR(vptr, 255, 255, 0);
147 VERTEX(vptr, -hsz, -hsz, -hsz);
148 VERTEX(vptr, -hsz, -hsz, hsz);
149 VERTEX(vptr, -hsz, hsz, hsz);
150 VERTEX(vptr, -hsz, hsz, -hsz);
152 NORMAL(vptr, 1, 0, 0);
153 COLOR(vptr, 255, 0, 0);
154 VERTEX(vptr, hsz, -hsz, hsz);
155 VERTEX(vptr, hsz, -hsz, -hsz);
156 VERTEX(vptr, hsz, hsz, -hsz);
157 VERTEX(vptr, hsz, hsz, hsz);
159 NORMAL(vptr, 0, 1, 0);
160 COLOR(vptr, 0, 255, 0);
161 VERTEX(vptr, -hsz, hsz, hsz);
162 VERTEX(vptr, hsz, hsz, hsz);
163 VERTEX(vptr, hsz, hsz, -hsz);
164 VERTEX(vptr, -hsz, hsz, -hsz);
166 NORMAL(vptr, 0, 0, 1);
167 COLOR(vptr, 0, 0, 255);
168 VERTEX(vptr, -hsz, -hsz, hsz);
169 VERTEX(vptr, hsz, -hsz, hsz);
170 VERTEX(vptr, hsz, hsz, hsz);
171 VERTEX(vptr, -hsz, hsz, hsz);
176 static void torusvec(float *res, float theta, float phi, float mr, float rr)
181 rx = -cos(phi) * rr + mr;
185 res[0] = rx * sin(theta) + rz * cos(theta);
187 res[2] = -rx * cos(theta) + rz * sin(theta);
190 static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub)
193 int nfaces, uverts, vverts;
194 struct g3d_vertex *vptr;
197 mesh->prim = G3D_QUADS;
199 if(usub < 4) usub = 4;
200 if(vsub < 2) vsub = 2;
205 mesh->vcount = uverts * vverts;
206 nfaces = usub * vsub;
207 mesh->icount = nfaces * 4;
209 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
212 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
218 for(i=0; i<uverts; i++) {
219 float u = (float)i / (float)(uverts - 1);
220 float theta = u * 2.0 * M_PI;
223 torusvec(rcent, theta, 0, rad, 0);
225 for(j=0; j<vverts; j++) {
226 float v = (float)j / (float)(vverts - 1);
227 float phi = v * 2.0 * M_PI;
228 int chess = (i & 1) == (j & 1);
230 torusvec(&vptr->x, theta, phi, rad, ringrad);
232 vptr->nx = (vptr->x - rcent[0]) / ringrad;
233 vptr->ny = (vptr->y - rcent[1]) / ringrad;
234 vptr->nz = (vptr->z - rcent[2]) / ringrad;
237 vptr->r = chess ? 255 : 64;
239 vptr->b = chess ? 64 : 255;
242 if(i < usub && j < vsub) {
243 int idx = i * vverts + j;
246 *iptr++ = idx + vverts + 1;
247 *iptr++ = idx + vverts;
256 struct g3d_vertex *varr;
260 static int zsort_cmp(const void *aptr, const void *bptr)
262 const int16_t *a = (const int16_t*)aptr;
263 const int16_t *b = (const int16_t*)bptr;
265 const float *m = zsort_cls.xform;
267 const struct g3d_vertex *va = zsort_cls.varr + a[0];
268 const struct g3d_vertex *vb = zsort_cls.varr + b[0];
270 float za = m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
271 float zb = m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
273 va = zsort_cls.varr + a[2];
274 vb = zsort_cls.varr + b[2];
276 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
277 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
282 static void zsort(struct mesh *m)
284 int nfaces = m->icount / m->prim;
286 zsort_cls.varr = m->varr;
287 zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
289 qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_cmp);