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 int dump_obj(const char *fname, struct mesh *m);
24 static void zsort(struct mesh *m);
26 static struct screen scr = {
34 static struct mesh cube, torus;
36 struct screen *polytest_screen(void)
44 gen_torus(&torus, 1.0, 0.25, 24, 12);
45 dump_obj("torus.obj", &torus);
49 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);
95 static void draw_mesh(struct mesh *mesh)
98 g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
100 g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
104 #define NORMAL(vp, x, y, z) do { vp->nx = x; vp->ny = y; vp->nz = z; } while(0)
105 #define COLOR(vp, cr, cg, cb) do { vp->r = cr; vp->g = cg; vp->b = cb; } while(0)
106 #define TEXCOORD(vp, tu, tv) do { vp->u = tu; vp->v = tv; } while(0)
107 #define VERTEX(vp, vx, vy, vz) \
109 vp->x = vx; vp->y = vy; vp->z = vz; vp->w = 1.0f; \
113 static int gen_cube(struct mesh *mesh, float sz)
115 struct g3d_vertex *vptr;
116 float hsz = sz / 2.0;
118 mesh->prim = G3D_QUADS;
123 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
129 NORMAL(vptr, 0, 0, -1);
130 COLOR(vptr, 255, 0, 255);
131 VERTEX(vptr, hsz, -hsz, -hsz);
132 VERTEX(vptr, -hsz, -hsz, -hsz);
133 VERTEX(vptr, -hsz, hsz, -hsz);
134 VERTEX(vptr, hsz, hsz, -hsz);
136 NORMAL(vptr, 0, -1, 0);
137 COLOR(vptr, 0, 255, 255);
138 VERTEX(vptr, -hsz, -hsz, -hsz);
139 VERTEX(vptr, hsz, -hsz, -hsz);
140 VERTEX(vptr, hsz, -hsz, hsz);
141 VERTEX(vptr, -hsz, -hsz, hsz);
143 NORMAL(vptr, -1, 0, 0);
144 COLOR(vptr, 255, 255, 0);
145 VERTEX(vptr, -hsz, -hsz, -hsz);
146 VERTEX(vptr, -hsz, -hsz, hsz);
147 VERTEX(vptr, -hsz, hsz, hsz);
148 VERTEX(vptr, -hsz, hsz, -hsz);
150 NORMAL(vptr, 1, 0, 0);
151 COLOR(vptr, 255, 0, 0);
152 VERTEX(vptr, hsz, -hsz, hsz);
153 VERTEX(vptr, hsz, -hsz, -hsz);
154 VERTEX(vptr, hsz, hsz, -hsz);
155 VERTEX(vptr, hsz, hsz, hsz);
157 NORMAL(vptr, 0, 1, 0);
158 COLOR(vptr, 0, 255, 0);
159 VERTEX(vptr, -hsz, hsz, hsz);
160 VERTEX(vptr, hsz, hsz, hsz);
161 VERTEX(vptr, hsz, hsz, -hsz);
162 VERTEX(vptr, -hsz, hsz, -hsz);
164 NORMAL(vptr, 0, 0, 1);
165 COLOR(vptr, 0, 0, 255);
166 VERTEX(vptr, -hsz, -hsz, hsz);
167 VERTEX(vptr, hsz, -hsz, hsz);
168 VERTEX(vptr, hsz, hsz, hsz);
169 VERTEX(vptr, -hsz, hsz, hsz);
174 static void torusvec(float *res, float theta, float phi, float mr, float rr)
179 rx = -cos(phi) * rr + mr;
183 res[0] = rx * sin(theta) + rz * cos(theta);
185 res[2] = -rx * cos(theta) + rz * sin(theta);
188 static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub)
191 int nfaces, uverts, vverts;
192 struct g3d_vertex *vptr;
195 mesh->prim = G3D_QUADS;
197 if(usub < 4) usub = 4;
198 if(vsub < 2) vsub = 2;
203 mesh->vcount = uverts * vverts;
204 nfaces = usub * vsub;
205 mesh->icount = nfaces * 4;
207 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
210 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
216 for(i=0; i<uverts; i++) {
217 float u = (float)i / (float)(uverts - 1);
218 float theta = u * 2.0 * M_PI;
221 torusvec(rcent, theta, 0, rad, 0);
223 for(j=0; j<vverts; j++) {
224 float v = (float)j / (float)(vverts - 1);
225 float phi = v * 2.0 * M_PI;
226 int chess = (i & 1) == (j & 1);
228 torusvec(&vptr->x, theta, phi, rad, ringrad);
230 vptr->nx = (vptr->x - rcent[0]) / ringrad;
231 vptr->ny = (vptr->y - rcent[1]) / ringrad;
232 vptr->nz = (vptr->z - rcent[2]) / ringrad;
235 vptr->r = chess ? 255 : 64;
237 vptr->b = chess ? 64 : 255;
240 if(i < usub && j < vsub) {
241 int idx = i * vverts + j;
244 *iptr++ = idx + vverts + 1;
245 *iptr++ = idx + vverts;
252 static int dump_obj(const char *fname, struct mesh *m)
256 struct g3d_vertex *vptr;
259 if(!(fp = fopen(fname, "wb"))) {
263 nfaces = m->icount / m->prim;
264 printf("dumping obj: %s - %d vertices / %d faces (%d indices)\n", fname,
265 m->vcount, nfaces, m->icount);
268 for(i=0; i<m->vcount; i++) {
269 fprintf(fp, "v %f %f %f\n", vptr->x, vptr->y, vptr->z);
273 for(i=0; i<m->vcount; i++) {
274 fprintf(fp, "vn %f %f %f\n", vptr->nx, vptr->ny, vptr->nz);
278 for(i=0; i<m->vcount; i++) {
279 fprintf(fp, "vt %f %f\n", vptr->u, vptr->v);
284 for(i=0; i<m->icount; i += m->prim) {
286 for(j=0; j<m->prim; j++) {
287 int idx = *iptr++ + 1;
288 fprintf(fp, " %d/%d/%d", idx, idx, idx);
298 struct g3d_vertex *varr;
302 static int zsort_cmp(const void *aptr, const void *bptr)
304 const int16_t *a = (const int16_t*)aptr;
305 const int16_t *b = (const int16_t*)bptr;
307 const float *m = zsort_cls.xform;
309 const struct g3d_vertex *va = zsort_cls.varr + a[0];
310 const struct g3d_vertex *vb = zsort_cls.varr + b[0];
312 float za = m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
313 float zb = m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
315 va = zsort_cls.varr + a[2];
316 vb = zsort_cls.varr + b[2];
318 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
319 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
324 static void zsort(struct mesh *m)
326 int nfaces = m->icount / m->prim;
328 zsort_cls.varr = m->varr;
329 zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
331 qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_cmp);