8 int load_mesh(struct g3d_mesh *mesh, const char *fname)
14 struct g3d_vertex *varr;
18 static int zsort_cmp(const void *aptr, const void *bptr)
20 const float *m = zsort_cls.xform;
22 const struct g3d_vertex *va = (const struct g3d_vertex*)aptr;
23 const struct g3d_vertex *vb = (const struct g3d_vertex*)bptr;
25 float za = m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
26 float zb = m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
31 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
32 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
37 static int zsort_indexed_cmp(const void *aptr, const void *bptr)
39 const int16_t *a = (const int16_t*)aptr;
40 const int16_t *b = (const int16_t*)bptr;
42 const float *m = zsort_cls.xform;
44 const struct g3d_vertex *va = zsort_cls.varr + a[0];
45 const struct g3d_vertex *vb = zsort_cls.varr + b[0];
47 float za = m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
48 float zb = m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
50 va = zsort_cls.varr + a[2];
51 vb = zsort_cls.varr + b[2];
53 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
54 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
60 void zsort_mesh(struct g3d_mesh *m)
62 zsort_cls.varr = m->varr;
63 zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
66 int nfaces = m->icount / m->prim;
67 qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_indexed_cmp);
69 int nfaces = m->vcount / m->prim;
70 qsort(m->varr, nfaces, m->prim * sizeof *m->varr, zsort_cmp);
75 void draw_mesh(struct g3d_mesh *mesh)
78 g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
80 g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
84 void apply_mesh_xform(struct g3d_mesh *mesh, const float *xform)
87 struct g3d_vertex *v = mesh->varr;
89 for(i=0; i<mesh->vcount; i++) {
90 float x = xform[0] * v->x + xform[4] * v->y + xform[8] * v->z + xform[12];
91 float y = xform[1] * v->x + xform[5] * v->y + xform[9] * v->z + xform[13];
92 v->z = xform[2] * v->x + xform[6] * v->y + xform[10] * v->z + xform[14];
95 x = xform[0] * v->nx + xform[4] * v->ny + xform[8] * v->nz;
96 y = xform[1] * v->nx + xform[5] * v->ny + xform[9] * v->nz;
97 v->nz = xform[2] * v->nx + xform[6] * v->ny + xform[10] * v->nz;
104 int append_mesh(struct g3d_mesh *ma, struct g3d_mesh *mb)
106 int i, new_vcount, new_icount;
110 if(ma->prim != mb->prim) {
111 fprintf(stderr, "append_mesh failed, primitive mismatch\n");
115 if(ma->iarr || mb->iarr) {
117 if(indexify_mesh(ma) == -1) {
120 } else if(!mb->iarr) {
121 if(indexify_mesh(mb) == -1) {
126 new_icount = ma->icount + mb->icount;
127 if(!(iptr = realloc(ma->iarr, new_icount * sizeof *iptr))) {
128 fprintf(stderr, "append_mesh: failed to allocate combined index buffer (%d indices)\n", new_icount);
134 for(i=0; i<mb->icount; i++) {
135 *iptr++ = mb->iarr[i] + ma->vcount;
137 ma->icount = new_icount;
140 new_vcount = ma->vcount + mb->vcount;
141 if(!(tmp = realloc(ma->varr, new_vcount * sizeof *ma->varr))) {
142 fprintf(stderr, "append_mesh: failed to allocate combined vertex buffer (%d verts)\n", new_vcount);
146 memcpy(ma->varr + ma->vcount, mb->varr, mb->vcount * sizeof *ma->varr);
147 ma->vcount = new_vcount;
151 #define FEQ(a, b) ((a) - (b) < 1e-5 && (b) - (a) < 1e-5)
152 static int cmp_vertex(struct g3d_vertex *a, struct g3d_vertex *b)
154 if(!FEQ(a->x, b->x) || !FEQ(a->y, b->y) || !FEQ(a->z, b->z) || !FEQ(a->w, b->w))
156 if(!FEQ(a->nx, b->nx) || !FEQ(a->ny, b->ny) || !FEQ(a->nz, b->nz))
158 if(!FEQ(a->u, b->u) || !FEQ(a->v, b->v))
160 if(a->r != b->r || a->g != b->g || a->b != b->b || a->a != b->a)
165 static int find_existing(struct g3d_vertex *v, struct g3d_vertex *varr, int vcount)
168 for(i=0; i<vcount; i++) {
169 if(cmp_vertex(v, varr++) == 0) {
176 int indexify_mesh(struct g3d_mesh *mesh)
178 int i, j, nfaces, max_icount, idx;
180 struct g3d_vertex *vin, *vout;
184 fprintf(stderr, "indexify_mesh failed: already indexed\n");
188 nfaces = mesh->vcount / mesh->prim;
189 max_icount = mesh->vcount;
191 if(!(mesh->iarr = malloc(max_icount * sizeof *mesh->iarr))) {
192 fprintf(stderr, "indexify_mesh failed to allocate index buffer of %d indices\n", max_icount);
196 vin = vout = mesh->varr;
199 for(i=0; i<nfaces; i++) {
200 for(j=0; j<mesh->prim; j++) {
201 if((idx = find_existing(vin, mesh->varr, out_vcount)) >= 0) {
204 *iout++ = out_vcount++;
213 /* XXX also shrink buffers? I'll just leave them to max size for now */
217 int gen_plane_mesh(struct g3d_mesh *m, float width, float height, int usub, int vsub)
220 int nfaces, nverts, nidx, uverts, vverts;
221 float x, y, u, v, du, dv;
222 struct g3d_vertex *vptr;
225 if(usub < 1) usub = 1;
226 if(vsub < 1) vsub = 1;
228 nfaces = usub * vsub;
231 du = (float)width / (float)usub;
232 dv = (float)height / (float)vsub;
234 nverts = uverts * vverts;
237 if(!(m->varr = malloc(nverts * sizeof *m->varr))) {
238 fprintf(stderr, "gen_plane_mesh: failed to allocate vertex buffer (%d vertices)\n", nverts);
241 if(!(m->iarr = malloc(nidx * sizeof *m->iarr))) {
242 fprintf(stderr, "gen_plane_mesh: failed to allocate index buffer (%d indices)\n", nidx);
256 for(i=0; i<vverts; i++) {
257 y = (v - 0.5) * height;
260 for(j=0; j<uverts; j++) {
261 x = (u - 0.5) * width;
272 vptr->r = vptr->g = vptr->b = vptr->a = 255;
280 for(i=0; i<vsub; i++) {
281 for(j=0; j<usub; j++) {
282 int idx = i * uverts + j;
285 *iptr++ = idx + uverts + 1;
286 *iptr++ = idx + uverts;
292 int gen_cube_mesh(struct g3d_mesh *mesh, float sz, int sub)
296 struct g3d_mesh tmpmesh;
298 static float rotface[][4] = {
307 g3d_matrix_mode(G3D_MODELVIEW);
311 m = i > 0 ? &tmpmesh : mesh;
312 if(gen_plane_mesh(m, sz, sz, sub, sub) == -1)
315 g3d_rotate(rotface[i][0], rotface[i][1], rotface[i][2], rotface[i][3]);
316 g3d_translate(0, 0, sz / 2.0f);
317 g3d_get_matrix(G3D_MODELVIEW, xform);
318 apply_mesh_xform(m, xform);
320 if(append_mesh(mesh, m) == -1) {
330 static void torusvec(float *res, float theta, float phi, float mr, float rr)
335 rx = -cos(phi) * rr + mr;
339 res[0] = rx * sin(theta) + rz * cos(theta);
341 res[2] = -rx * cos(theta) + rz * sin(theta);
344 int gen_torus_mesh(struct g3d_mesh *mesh, float rad, float ringrad, int usub, int vsub)
347 int nfaces, uverts, vverts;
348 struct g3d_vertex *vptr;
351 mesh->prim = G3D_QUADS;
353 if(usub < 4) usub = 4;
354 if(vsub < 2) vsub = 2;
359 mesh->vcount = uverts * vverts;
360 nfaces = usub * vsub;
361 mesh->icount = nfaces * 4;
363 printf("generating torus with %d faces (%d vertices)\n", nfaces, mesh->vcount);
365 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
368 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
374 for(i=0; i<uverts; i++) {
375 float u = (float)i / (float)(uverts - 1);
376 float theta = u * 2.0 * M_PI;
379 torusvec(rcent, theta, 0, rad, 0);
381 for(j=0; j<vverts; j++) {
382 float v = (float)j / (float)(vverts - 1);
383 float phi = v * 2.0 * M_PI;
384 int chess = (i & 1) == (j & 1);
386 torusvec(&vptr->x, theta, phi, rad, ringrad);
388 vptr->nx = (vptr->x - rcent[0]) / ringrad;
389 vptr->ny = (vptr->y - rcent[1]) / ringrad;
390 vptr->nz = (vptr->z - rcent[2]) / ringrad;
393 vptr->r = chess ? 255 : 64;
395 vptr->b = chess ? 64 : 255;
398 if(i < usub && j < vsub) {
399 int idx = i * vverts + j;
402 *iptr++ = idx + vverts + 1;
403 *iptr++ = idx + vverts;