8 void free_mesh(struct g3d_mesh *mesh)
14 void destroy_mesh(struct g3d_mesh *mesh)
22 struct g3d_vertex *varr;
26 static int zsort_cmp(const void *aptr, const void *bptr)
31 const float *m = zsort_cls.xform;
32 const struct g3d_vertex *va = (const struct g3d_vertex*)aptr;
33 const struct g3d_vertex *vb = (const struct g3d_vertex*)bptr;
35 for(i=0; i<zsort_cls.prim; i++) {
36 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
37 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
44 static int zsort_indexed_cmp(const void *aptr, const void *bptr)
49 const uint16_t *a = (const uint16_t*)aptr;
50 const uint16_t *b = (const uint16_t*)bptr;
52 const float *m = zsort_cls.xform;
54 for(i=0; i<zsort_cls.prim; i++) {
55 const struct g3d_vertex *va = zsort_cls.varr + a[i];
56 const struct g3d_vertex *vb = zsort_cls.varr + b[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];
65 void zsort_mesh(struct g3d_mesh *m)
67 zsort_cls.varr = m->varr;
68 zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
69 zsort_cls.prim = m->prim;
72 int nfaces = m->icount / m->prim;
73 qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_indexed_cmp);
75 int nfaces = m->vcount / m->prim;
76 qsort(m->varr, nfaces, m->prim * sizeof *m->varr, zsort_cmp);
81 void draw_mesh(struct g3d_mesh *mesh)
84 g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
86 g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
90 void apply_mesh_xform(struct g3d_mesh *mesh, const float *xform)
93 struct g3d_vertex *v = mesh->varr;
95 for(i=0; i<mesh->vcount; i++) {
96 float x = xform[0] * v->x + xform[4] * v->y + xform[8] * v->z + xform[12];
97 float y = xform[1] * v->x + xform[5] * v->y + xform[9] * v->z + xform[13];
98 v->z = xform[2] * v->x + xform[6] * v->y + xform[10] * v->z + xform[14];
101 x = xform[0] * v->nx + xform[4] * v->ny + xform[8] * v->nz;
102 y = xform[1] * v->nx + xform[5] * v->ny + xform[9] * v->nz;
103 v->nz = xform[2] * v->nx + xform[6] * v->ny + xform[10] * v->nz;
110 int append_mesh(struct g3d_mesh *ma, struct g3d_mesh *mb)
112 int i, new_vcount, new_icount;
116 if(ma->prim != mb->prim) {
117 fprintf(stderr, "append_mesh failed, primitive mismatch\n");
121 if(ma->iarr || mb->iarr) {
123 if(indexify_mesh(ma) == -1) {
126 } else if(!mb->iarr) {
127 if(indexify_mesh(mb) == -1) {
132 new_icount = ma->icount + mb->icount;
133 if(!(iptr = realloc(ma->iarr, new_icount * sizeof *iptr))) {
134 fprintf(stderr, "append_mesh: failed to allocate combined index buffer (%d indices)\n", new_icount);
140 for(i=0; i<mb->icount; i++) {
141 *iptr++ = mb->iarr[i] + ma->vcount;
143 ma->icount = new_icount;
146 new_vcount = ma->vcount + mb->vcount;
147 if(!(tmp = realloc(ma->varr, new_vcount * sizeof *ma->varr))) {
148 fprintf(stderr, "append_mesh: failed to allocate combined vertex buffer (%d verts)\n", new_vcount);
152 memcpy(ma->varr + ma->vcount, mb->varr, mb->vcount * sizeof *ma->varr);
153 ma->vcount = new_vcount;
157 #define FEQ(a, b) ((a) - (b) < 1e-5 && (b) - (a) < 1e-5)
158 static int cmp_vertex(struct g3d_vertex *a, struct g3d_vertex *b)
160 if(!FEQ(a->x, b->x) || !FEQ(a->y, b->y) || !FEQ(a->z, b->z) || !FEQ(a->w, b->w))
162 if(!FEQ(a->nx, b->nx) || !FEQ(a->ny, b->ny) || !FEQ(a->nz, b->nz))
164 if(!FEQ(a->u, b->u) || !FEQ(a->v, b->v))
166 if(a->r != b->r || a->g != b->g || a->b != b->b || a->a != b->a)
171 static int find_existing(struct g3d_vertex *v, struct g3d_vertex *varr, int vcount)
174 for(i=0; i<vcount; i++) {
175 if(cmp_vertex(v, varr++) == 0) {
182 int indexify_mesh(struct g3d_mesh *mesh)
184 int i, j, nfaces, max_icount, idx;
186 struct g3d_vertex *vin, *vout;
190 fprintf(stderr, "indexify_mesh failed: already indexed\n");
194 nfaces = mesh->vcount / mesh->prim;
195 max_icount = mesh->vcount;
197 if(!(mesh->iarr = malloc(max_icount * sizeof *mesh->iarr))) {
198 fprintf(stderr, "indexify_mesh failed to allocate index buffer of %d indices\n", max_icount);
202 vin = vout = mesh->varr;
205 for(i=0; i<nfaces; i++) {
206 for(j=0; j<mesh->prim; j++) {
207 if((idx = find_existing(vin, mesh->varr, out_vcount)) >= 0) {
210 *iout++ = out_vcount++;
219 /* XXX also shrink buffers? I'll just leave them to max size for now */
223 void normalize_mesh_normals(struct g3d_mesh *mesh)
226 struct g3d_vertex *v = mesh->varr;
228 for(i=0; i<mesh->vcount; i++) {
229 float mag = sqrt(v->nx * v->nx + v->ny * v->ny + v->nz * v->nz);
230 float s = (mag == 0.0f) ? 1.0f : 1.0f / mag;
239 static void sphvec(float *res, float theta, float phi, float rad)
242 res[0] = sin(theta) * sin(phi);
244 res[2] = cos(theta) * sin(phi);
247 int gen_sphere_mesh(struct g3d_mesh *mesh, float rad, int usub, int vsub)
250 int nfaces, uverts, vverts;
251 struct g3d_vertex *vptr;
254 mesh->prim = G3D_QUADS;
256 if(usub < 4) usub = 4;
257 if(vsub < 2) vsub = 2;
262 mesh->vcount = uverts * vverts;
263 nfaces = usub * vsub;
264 mesh->icount = nfaces * 4;
266 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
267 fprintf(stderr, "gen_sphere_mesh: failed to allocate vertex buffer (%d vertices)\n", mesh->vcount);
270 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
271 fprintf(stderr, "gen_sphere_mesh: failed to allocate index buffer (%d indices)\n", mesh->icount);
277 for(i=0; i<uverts; i++) {
278 float u = (float)i / (float)(uverts - 1);
279 float theta = u * 2.0 * M_PI;
281 for(j=0; j<vverts; j++) {
282 float v = (float)j / (float)(vverts - 1);
283 float phi = v * M_PI;
284 int chess = (i & 1) == (j & 1);
286 sphvec(&vptr->x, theta, phi, rad);
289 vptr->nx = vptr->x / rad;
290 vptr->ny = vptr->y / rad;
291 vptr->nz = vptr->z / rad;
294 vptr->r = chess ? 255 : 64;
296 vptr->b = chess ? 64 : 255;
299 if(i < usub && j < vsub) {
300 int idx = i * vverts + j;
303 *iptr++ = idx + vverts + 1;
304 *iptr++ = idx + vverts;
311 int gen_plane_mesh(struct g3d_mesh *m, float width, float height, int usub, int vsub)
314 int nfaces, nverts, nidx, uverts, vverts;
315 float x, y, u, v, du, dv;
316 struct g3d_vertex *vptr;
319 if(usub < 1) usub = 1;
320 if(vsub < 1) vsub = 1;
322 nfaces = usub * vsub;
325 du = (float)width / (float)usub;
326 dv = (float)height / (float)vsub;
328 nverts = uverts * vverts;
331 if(!(m->varr = malloc(nverts * sizeof *m->varr))) {
332 fprintf(stderr, "gen_plane_mesh: failed to allocate vertex buffer (%d vertices)\n", nverts);
335 if(!(m->iarr = malloc(nidx * sizeof *m->iarr))) {
336 fprintf(stderr, "gen_plane_mesh: failed to allocate index buffer (%d indices)\n", nidx);
350 for(i=0; i<vverts; i++) {
351 y = (v - 0.5) * height;
354 for(j=0; j<uverts; j++) {
355 x = (u - 0.5) * width;
366 vptr->r = vptr->g = vptr->b = vptr->a = 255;
374 for(i=0; i<vsub; i++) {
375 for(j=0; j<usub; j++) {
376 int idx = i * uverts + j;
379 *iptr++ = idx + uverts + 1;
380 *iptr++ = idx + uverts;
386 int gen_cube_mesh(struct g3d_mesh *mesh, float sz, int sub)
390 struct g3d_mesh tmpmesh;
391 static float rotface[][4] = {
400 g3d_matrix_mode(G3D_MODELVIEW);
404 m = i > 0 ? &tmpmesh : mesh;
405 if(gen_plane_mesh(m, sz, sz, sub, sub) == -1)
408 g3d_rotate(rotface[i][0], rotface[i][1], rotface[i][2], rotface[i][3]);
409 g3d_translate(0, 0, sz / 2.0f);
410 apply_mesh_xform(m, g3d_get_matrix(G3D_MODELVIEW, 0));
412 if(append_mesh(mesh, m) == -1) {
422 static void torusvec(float *res, float theta, float phi, float mr, float rr)
427 rx = -cos(phi) * rr + mr;
431 res[0] = rx * sin(theta) + rz * cos(theta);
433 res[2] = -rx * cos(theta) + rz * sin(theta);
436 int gen_torus_mesh(struct g3d_mesh *mesh, float rad, float ringrad, int usub, int vsub)
439 int nfaces, uverts, vverts;
440 struct g3d_vertex *vptr;
443 mesh->prim = G3D_QUADS;
445 if(usub < 4) usub = 4;
446 if(vsub < 2) vsub = 2;
451 mesh->vcount = uverts * vverts;
452 nfaces = usub * vsub;
453 mesh->icount = nfaces * 4;
455 printf("generating torus with %d faces (%d vertices)\n", nfaces, mesh->vcount);
457 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
460 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
466 for(i=0; i<uverts; i++) {
467 float u = (float)i / (float)(uverts - 1);
468 float theta = u * 2.0 * M_PI;
471 torusvec(rcent, theta, 0, rad, 0);
473 for(j=0; j<vverts; j++) {
474 float v = (float)j / (float)(vverts - 1);
475 float phi = v * 2.0 * M_PI;
476 int chess = (i & 1) == (j & 1);
478 torusvec(&vptr->x, theta, phi, rad, ringrad);
481 vptr->nx = (vptr->x - rcent[0]) / ringrad;
482 vptr->ny = (vptr->y - rcent[1]) / ringrad;
483 vptr->nz = (vptr->z - rcent[2]) / ringrad;
486 vptr->r = chess ? 255 : 64;
488 vptr->b = chess ? 64 : 255;
491 if(i < usub && j < vsub) {
492 int idx = i * vverts + j;
495 *iptr++ = idx + vverts + 1;
496 *iptr++ = idx + vverts;