8 void free_mesh(struct g3d_mesh *mesh)
14 void destroy_mesh(struct g3d_mesh *mesh)
20 int copy_mesh(struct g3d_mesh *dest, struct g3d_mesh *src)
22 dest->prim = src->prim;
24 if(!(dest->varr = malloc(src->vcount * sizeof *src->varr))) {
27 memcpy(dest->varr, src->varr, src->vcount * sizeof *src->varr);
29 dest->vcount = src->vcount;
31 if(!(dest->iarr = malloc(src->icount * sizeof *src->iarr))) {
36 memcpy(dest->iarr, src->iarr, src->icount * sizeof *src->iarr);
38 dest->icount = src->icount;
44 struct g3d_vertex *varr;
48 static int zsort_cmp(const void *aptr, const void *bptr)
53 const float *m = zsort_cls.xform;
54 const struct g3d_vertex *va = (const struct g3d_vertex*)aptr;
55 const struct g3d_vertex *vb = (const struct g3d_vertex*)bptr;
57 for(i=0; i<zsort_cls.prim; i++) {
58 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
59 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
68 static int zsort_indexed_cmp(const void *aptr, const void *bptr)
73 const uint16_t *a = (const uint16_t*)aptr;
74 const uint16_t *b = (const uint16_t*)bptr;
76 const float *m = zsort_cls.xform;
78 for(i=0; i<zsort_cls.prim; i++) {
79 const struct g3d_vertex *va = zsort_cls.varr + a[i];
80 const struct g3d_vertex *vb = zsort_cls.varr + b[i];
82 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
83 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
91 void zsort_mesh(struct g3d_mesh *m)
93 zsort_cls.varr = m->varr;
94 zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
95 zsort_cls.prim = m->prim;
98 int nfaces = m->icount / m->prim;
99 qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_indexed_cmp);
101 int nfaces = m->vcount / m->prim;
102 qsort(m->varr, nfaces, m->prim * sizeof *m->varr, zsort_cmp);
107 void draw_mesh(struct g3d_mesh *mesh)
110 g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
112 g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
116 void apply_mesh_xform(struct g3d_mesh *mesh, const float *xform)
119 struct g3d_vertex *v = mesh->varr;
121 for(i=0; i<mesh->vcount; i++) {
122 float x = xform[0] * v->x + xform[4] * v->y + xform[8] * v->z + xform[12];
123 float y = xform[1] * v->x + xform[5] * v->y + xform[9] * v->z + xform[13];
124 v->z = xform[2] * v->x + xform[6] * v->y + xform[10] * v->z + xform[14];
127 x = xform[0] * v->nx + xform[4] * v->ny + xform[8] * v->nz;
128 y = xform[1] * v->nx + xform[5] * v->ny + xform[9] * v->nz;
129 v->nz = xform[2] * v->nx + xform[6] * v->ny + xform[10] * v->nz;
136 int append_mesh(struct g3d_mesh *ma, struct g3d_mesh *mb)
138 int i, new_vcount, new_icount;
142 if(ma->prim != mb->prim) {
143 fprintf(stderr, "append_mesh failed, primitive mismatch\n");
147 if(ma->iarr || mb->iarr) {
149 if(indexify_mesh(ma) == -1) {
152 } else if(!mb->iarr) {
153 if(indexify_mesh(mb) == -1) {
158 new_icount = ma->icount + mb->icount;
159 if(!(iptr = realloc(ma->iarr, new_icount * sizeof *iptr))) {
160 fprintf(stderr, "append_mesh: failed to allocate combined index buffer (%d indices)\n", new_icount);
166 for(i=0; i<mb->icount; i++) {
167 *iptr++ = mb->iarr[i] + ma->vcount;
169 ma->icount = new_icount;
172 new_vcount = ma->vcount + mb->vcount;
173 if(!(tmp = realloc(ma->varr, new_vcount * sizeof *ma->varr))) {
174 fprintf(stderr, "append_mesh: failed to allocate combined vertex buffer (%d verts)\n", new_vcount);
178 memcpy(ma->varr + ma->vcount, mb->varr, mb->vcount * sizeof *ma->varr);
179 ma->vcount = new_vcount;
183 #define FEQ(a, b) ((a) - (b) < 1e-5 && (b) - (a) < 1e-5)
184 static int cmp_vertex(struct g3d_vertex *a, struct g3d_vertex *b)
186 if(!FEQ(a->x, b->x) || !FEQ(a->y, b->y) || !FEQ(a->z, b->z) || !FEQ(a->w, b->w))
188 if(!FEQ(a->nx, b->nx) || !FEQ(a->ny, b->ny) || !FEQ(a->nz, b->nz))
190 if(!FEQ(a->u, b->u) || !FEQ(a->v, b->v))
192 if(a->r != b->r || a->g != b->g || a->b != b->b || a->a != b->a)
197 static int find_existing(struct g3d_vertex *v, struct g3d_vertex *varr, int vcount)
200 for(i=0; i<vcount; i++) {
201 if(cmp_vertex(v, varr++) == 0) {
208 int indexify_mesh(struct g3d_mesh *mesh)
210 int i, j, nfaces, max_icount, idx;
212 struct g3d_vertex *vin, *vout;
216 fprintf(stderr, "indexify_mesh failed: already indexed\n");
220 nfaces = mesh->vcount / mesh->prim;
221 max_icount = mesh->vcount;
223 if(!(mesh->iarr = malloc(max_icount * sizeof *mesh->iarr))) {
224 fprintf(stderr, "indexify_mesh failed to allocate index buffer of %d indices\n", max_icount);
228 vin = vout = mesh->varr;
231 for(i=0; i<nfaces; i++) {
232 for(j=0; j<mesh->prim; j++) {
233 if((idx = find_existing(vin, mesh->varr, out_vcount)) >= 0) {
236 *iout++ = out_vcount++;
245 /* XXX also shrink buffers? I'll just leave them to max size for now */
249 void normalize_mesh_normals(struct g3d_mesh *mesh)
252 struct g3d_vertex *v = mesh->varr;
254 for(i=0; i<mesh->vcount; i++) {
255 float mag = sqrt(v->nx * v->nx + v->ny * v->ny + v->nz * v->nz);
256 float s = (mag == 0.0f) ? 1.0f : 1.0f / mag;
265 void calc_mesh_centroid(struct g3d_mesh *mesh, float *cent)
268 float s = 1.0f / (float)mesh->vcount;
269 cent[0] = cent[1] = cent[2] = 0.0f;
271 for(i=0; i<mesh->vcount; i++) {
272 cent[0] += mesh->varr[i].x;
273 cent[1] += mesh->varr[i].y;
274 cent[2] += mesh->varr[i].z;
281 static void sphvec(float *res, float theta, float phi, float rad)
284 res[0] = sin(theta) * sin(phi);
286 res[2] = cos(theta) * sin(phi);
289 int gen_sphere_mesh(struct g3d_mesh *mesh, float rad, int usub, int vsub)
292 int nfaces, uverts, vverts;
293 struct g3d_vertex *vptr;
296 mesh->prim = G3D_QUADS;
298 if(usub < 4) usub = 4;
299 if(vsub < 2) vsub = 2;
304 mesh->vcount = uverts * vverts;
305 nfaces = usub * vsub;
306 mesh->icount = nfaces * 4;
308 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
309 fprintf(stderr, "gen_sphere_mesh: failed to allocate vertex buffer (%d vertices)\n", mesh->vcount);
312 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
313 fprintf(stderr, "gen_sphere_mesh: failed to allocate index buffer (%d indices)\n", mesh->icount);
319 for(i=0; i<uverts; i++) {
320 float u = (float)i / (float)(uverts - 1);
321 float theta = u * 2.0 * M_PI;
323 for(j=0; j<vverts; j++) {
324 float v = (float)j / (float)(vverts - 1);
325 float phi = v * M_PI;
326 int chess = (i & 1) == (j & 1);
328 sphvec(&vptr->x, theta, phi, rad);
331 vptr->nx = vptr->x / rad;
332 vptr->ny = vptr->y / rad;
333 vptr->nz = vptr->z / rad;
336 vptr->r = chess ? 255 : 64;
338 vptr->b = chess ? 64 : 255;
341 if(i < usub && j < vsub) {
342 int idx = i * vverts + j;
345 *iptr++ = idx + vverts + 1;
346 *iptr++ = idx + vverts;
353 int gen_plane_mesh(struct g3d_mesh *m, float width, float height, int usub, int vsub)
356 int nfaces, nverts, nidx, uverts, vverts;
357 float x, y, u, v, du, dv;
358 struct g3d_vertex *vptr;
361 if(usub < 1) usub = 1;
362 if(vsub < 1) vsub = 1;
364 nfaces = usub * vsub;
367 du = (float)width / (float)usub;
368 dv = (float)height / (float)vsub;
370 nverts = uverts * vverts;
373 if(!(m->varr = malloc(nverts * sizeof *m->varr))) {
374 fprintf(stderr, "gen_plane_mesh: failed to allocate vertex buffer (%d vertices)\n", nverts);
377 if(!(m->iarr = malloc(nidx * sizeof *m->iarr))) {
378 fprintf(stderr, "gen_plane_mesh: failed to allocate index buffer (%d indices)\n", nidx);
392 for(i=0; i<vverts; i++) {
393 y = (v - 0.5) * height;
396 for(j=0; j<uverts; j++) {
397 x = (u - 0.5) * width;
408 vptr->r = vptr->g = vptr->b = vptr->a = 255;
416 for(i=0; i<vsub; i++) {
417 for(j=0; j<usub; j++) {
418 int idx = i * uverts + j;
421 *iptr++ = idx + uverts + 1;
422 *iptr++ = idx + uverts;
428 int gen_cube_mesh(struct g3d_mesh *mesh, float sz, int sub)
433 struct g3d_mesh tmpmesh;
434 static float rotface[][4] = {
446 g3d_matrix_mode(G3D_MODELVIEW);
450 m = i > 0 ? &tmpmesh : mesh;
451 if(gen_plane_mesh(m, sz, sz, sub, sub) == -1)
454 g3d_rotate(rotface[i][0], rotface[i][1], rotface[i][2], rotface[i][3]);
455 g3d_translate(0, 0, offs / 2.0f);
456 apply_mesh_xform(m, g3d_get_matrix(G3D_MODELVIEW, 0));
458 if(append_mesh(mesh, m) == -1) {
468 static void torusvec(float *res, float theta, float phi, float mr, float rr)
473 rx = -cos(phi) * rr + mr;
477 res[0] = rx * sin(theta) + rz * cos(theta);
479 res[2] = -rx * cos(theta) + rz * sin(theta);
482 int gen_torus_mesh(struct g3d_mesh *mesh, float rad, float ringrad, int usub, int vsub)
485 int nfaces, uverts, vverts;
486 struct g3d_vertex *vptr;
489 mesh->prim = G3D_QUADS;
491 if(usub < 4) usub = 4;
492 if(vsub < 2) vsub = 2;
497 mesh->vcount = uverts * vverts;
498 nfaces = usub * vsub;
499 mesh->icount = nfaces * 4;
501 printf("generating torus with %d faces (%d vertices)\n", nfaces, mesh->vcount);
503 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
506 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
512 for(i=0; i<uverts; i++) {
513 float u = (float)i / (float)(uverts - 1);
514 float theta = u * 2.0 * M_PI;
517 torusvec(rcent, theta, 0, rad, 0);
519 for(j=0; j<vverts; j++) {
520 float v = (float)j / (float)(vverts - 1);
521 float phi = v * 2.0 * M_PI;
522 int chess = (i & 1) == (j & 1);
524 torusvec(&vptr->x, theta, phi, rad, ringrad);
527 vptr->nx = (vptr->x - rcent[0]) / ringrad;
528 vptr->ny = (vptr->y - rcent[1]) / ringrad;
529 vptr->nz = (vptr->z - rcent[2]) / ringrad;
532 vptr->r = chess ? 255 : 64;
534 vptr->b = chess ? 64 : 255;
537 if(i < usub && j < vsub) {
538 int idx = i * vverts + j;
541 *iptr++ = idx + vverts + 1;
542 *iptr++ = idx + vverts;