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];
66 static int zsort_indexed_cmp(const void *aptr, const void *bptr)
71 const uint16_t *a = (const uint16_t*)aptr;
72 const uint16_t *b = (const uint16_t*)bptr;
74 const float *m = zsort_cls.xform;
76 for(i=0; i<zsort_cls.prim; i++) {
77 const struct g3d_vertex *va = zsort_cls.varr + a[i];
78 const struct g3d_vertex *vb = zsort_cls.varr + b[i];
80 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
81 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
87 void zsort_mesh(struct g3d_mesh *m)
89 zsort_cls.varr = m->varr;
90 zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
91 zsort_cls.prim = m->prim;
94 int nfaces = m->icount / m->prim;
95 qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_indexed_cmp);
97 int nfaces = m->vcount / m->prim;
98 qsort(m->varr, nfaces, m->prim * sizeof *m->varr, zsort_cmp);
103 void draw_mesh(struct g3d_mesh *mesh)
106 g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
108 g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
112 void apply_mesh_xform(struct g3d_mesh *mesh, const float *xform)
115 struct g3d_vertex *v = mesh->varr;
117 for(i=0; i<mesh->vcount; i++) {
118 float x = xform[0] * v->x + xform[4] * v->y + xform[8] * v->z + xform[12];
119 float y = xform[1] * v->x + xform[5] * v->y + xform[9] * v->z + xform[13];
120 v->z = xform[2] * v->x + xform[6] * v->y + xform[10] * v->z + xform[14];
123 x = xform[0] * v->nx + xform[4] * v->ny + xform[8] * v->nz;
124 y = xform[1] * v->nx + xform[5] * v->ny + xform[9] * v->nz;
125 v->nz = xform[2] * v->nx + xform[6] * v->ny + xform[10] * v->nz;
132 int append_mesh(struct g3d_mesh *ma, struct g3d_mesh *mb)
134 int i, new_vcount, new_icount;
138 if(ma->prim != mb->prim) {
139 fprintf(stderr, "append_mesh failed, primitive mismatch\n");
143 if(ma->iarr || mb->iarr) {
145 if(indexify_mesh(ma) == -1) {
148 } else if(!mb->iarr) {
149 if(indexify_mesh(mb) == -1) {
154 new_icount = ma->icount + mb->icount;
155 if(!(iptr = realloc(ma->iarr, new_icount * sizeof *iptr))) {
156 fprintf(stderr, "append_mesh: failed to allocate combined index buffer (%d indices)\n", new_icount);
162 for(i=0; i<mb->icount; i++) {
163 *iptr++ = mb->iarr[i] + ma->vcount;
165 ma->icount = new_icount;
168 new_vcount = ma->vcount + mb->vcount;
169 if(!(tmp = realloc(ma->varr, new_vcount * sizeof *ma->varr))) {
170 fprintf(stderr, "append_mesh: failed to allocate combined vertex buffer (%d verts)\n", new_vcount);
174 memcpy(ma->varr + ma->vcount, mb->varr, mb->vcount * sizeof *ma->varr);
175 ma->vcount = new_vcount;
179 #define FEQ(a, b) ((a) - (b) < 1e-5 && (b) - (a) < 1e-5)
180 static int cmp_vertex(struct g3d_vertex *a, struct g3d_vertex *b)
182 if(!FEQ(a->x, b->x) || !FEQ(a->y, b->y) || !FEQ(a->z, b->z) || !FEQ(a->w, b->w))
184 if(!FEQ(a->nx, b->nx) || !FEQ(a->ny, b->ny) || !FEQ(a->nz, b->nz))
186 if(!FEQ(a->u, b->u) || !FEQ(a->v, b->v))
188 if(a->r != b->r || a->g != b->g || a->b != b->b || a->a != b->a)
193 static int find_existing(struct g3d_vertex *v, struct g3d_vertex *varr, int vcount)
196 for(i=0; i<vcount; i++) {
197 if(cmp_vertex(v, varr++) == 0) {
204 int indexify_mesh(struct g3d_mesh *mesh)
206 int i, j, nfaces, max_icount, idx;
208 struct g3d_vertex *vin, *vout;
212 fprintf(stderr, "indexify_mesh failed: already indexed\n");
216 nfaces = mesh->vcount / mesh->prim;
217 max_icount = mesh->vcount;
219 if(!(mesh->iarr = malloc(max_icount * sizeof *mesh->iarr))) {
220 fprintf(stderr, "indexify_mesh failed to allocate index buffer of %d indices\n", max_icount);
224 vin = vout = mesh->varr;
227 for(i=0; i<nfaces; i++) {
228 for(j=0; j<mesh->prim; j++) {
229 if((idx = find_existing(vin, mesh->varr, out_vcount)) >= 0) {
232 *iout++ = out_vcount++;
241 /* XXX also shrink buffers? I'll just leave them to max size for now */
245 void normalize_mesh_normals(struct g3d_mesh *mesh)
248 struct g3d_vertex *v = mesh->varr;
250 for(i=0; i<mesh->vcount; i++) {
251 float mag = sqrt(v->nx * v->nx + v->ny * v->ny + v->nz * v->nz);
252 float s = (mag == 0.0f) ? 1.0f : 1.0f / mag;
261 static void sphvec(float *res, float theta, float phi, float rad)
264 res[0] = sin(theta) * sin(phi);
266 res[2] = cos(theta) * sin(phi);
269 int gen_sphere_mesh(struct g3d_mesh *mesh, float rad, int usub, int vsub)
272 int nfaces, uverts, vverts;
273 struct g3d_vertex *vptr;
276 mesh->prim = G3D_QUADS;
278 if(usub < 4) usub = 4;
279 if(vsub < 2) vsub = 2;
284 mesh->vcount = uverts * vverts;
285 nfaces = usub * vsub;
286 mesh->icount = nfaces * 4;
288 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
289 fprintf(stderr, "gen_sphere_mesh: failed to allocate vertex buffer (%d vertices)\n", mesh->vcount);
292 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
293 fprintf(stderr, "gen_sphere_mesh: failed to allocate index buffer (%d indices)\n", mesh->icount);
299 for(i=0; i<uverts; i++) {
300 float u = (float)i / (float)(uverts - 1);
301 float theta = u * 2.0 * M_PI;
303 for(j=0; j<vverts; j++) {
304 float v = (float)j / (float)(vverts - 1);
305 float phi = v * M_PI;
306 int chess = (i & 1) == (j & 1);
308 sphvec(&vptr->x, theta, phi, rad);
311 vptr->nx = vptr->x / rad;
312 vptr->ny = vptr->y / rad;
313 vptr->nz = vptr->z / rad;
316 vptr->r = chess ? 255 : 64;
318 vptr->b = chess ? 64 : 255;
321 if(i < usub && j < vsub) {
322 int idx = i * vverts + j;
325 *iptr++ = idx + vverts + 1;
326 *iptr++ = idx + vverts;
333 int gen_plane_mesh(struct g3d_mesh *m, float width, float height, int usub, int vsub)
336 int nfaces, nverts, nidx, uverts, vverts;
337 float x, y, u, v, du, dv;
338 struct g3d_vertex *vptr;
341 if(usub < 1) usub = 1;
342 if(vsub < 1) vsub = 1;
344 nfaces = usub * vsub;
347 du = (float)width / (float)usub;
348 dv = (float)height / (float)vsub;
350 nverts = uverts * vverts;
353 if(!(m->varr = malloc(nverts * sizeof *m->varr))) {
354 fprintf(stderr, "gen_plane_mesh: failed to allocate vertex buffer (%d vertices)\n", nverts);
357 if(!(m->iarr = malloc(nidx * sizeof *m->iarr))) {
358 fprintf(stderr, "gen_plane_mesh: failed to allocate index buffer (%d indices)\n", nidx);
372 for(i=0; i<vverts; i++) {
373 y = (v - 0.5) * height;
376 for(j=0; j<uverts; j++) {
377 x = (u - 0.5) * width;
388 vptr->r = vptr->g = vptr->b = vptr->a = 255;
396 for(i=0; i<vsub; i++) {
397 for(j=0; j<usub; j++) {
398 int idx = i * uverts + j;
401 *iptr++ = idx + uverts + 1;
402 *iptr++ = idx + uverts;
408 int gen_cube_mesh(struct g3d_mesh *mesh, float sz, int sub)
412 struct g3d_mesh tmpmesh;
413 static float rotface[][4] = {
422 g3d_matrix_mode(G3D_MODELVIEW);
426 m = i > 0 ? &tmpmesh : mesh;
427 if(gen_plane_mesh(m, sz, sz, sub, sub) == -1)
430 g3d_rotate(rotface[i][0], rotface[i][1], rotface[i][2], rotface[i][3]);
431 g3d_translate(0, 0, sz / 2.0f);
432 apply_mesh_xform(m, g3d_get_matrix(G3D_MODELVIEW, 0));
434 if(append_mesh(mesh, m) == -1) {
444 static void torusvec(float *res, float theta, float phi, float mr, float rr)
449 rx = -cos(phi) * rr + mr;
453 res[0] = rx * sin(theta) + rz * cos(theta);
455 res[2] = -rx * cos(theta) + rz * sin(theta);
458 int gen_torus_mesh(struct g3d_mesh *mesh, float rad, float ringrad, int usub, int vsub)
461 int nfaces, uverts, vverts;
462 struct g3d_vertex *vptr;
465 mesh->prim = G3D_QUADS;
467 if(usub < 4) usub = 4;
468 if(vsub < 2) vsub = 2;
473 mesh->vcount = uverts * vverts;
474 nfaces = usub * vsub;
475 mesh->icount = nfaces * 4;
477 printf("generating torus with %d faces (%d vertices)\n", nfaces, mesh->vcount);
479 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
482 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
488 for(i=0; i<uverts; i++) {
489 float u = (float)i / (float)(uverts - 1);
490 float theta = u * 2.0 * M_PI;
493 torusvec(rcent, theta, 0, rad, 0);
495 for(j=0; j<vverts; j++) {
496 float v = (float)j / (float)(vverts - 1);
497 float phi = v * 2.0 * M_PI;
498 int chess = (i & 1) == (j & 1);
500 torusvec(&vptr->x, theta, phi, rad, ringrad);
503 vptr->nx = (vptr->x - rcent[0]) / ringrad;
504 vptr->ny = (vptr->y - rcent[1]) / ringrad;
505 vptr->nz = (vptr->z - rcent[2]) / ringrad;
508 vptr->r = chess ? 255 : 64;
510 vptr->b = chess ? 64 : 255;
513 if(i < usub && j < vsub) {
514 int idx = i * vverts + j;
517 *iptr++ = idx + vverts + 1;
518 *iptr++ = idx + vverts;