10 struct g3d_vertex *varr;
14 static int zsort_cmp(const void *aptr, const void *bptr)
19 const float *m = zsort_cls.xform;
20 const struct g3d_vertex *va = (const struct g3d_vertex*)aptr;
21 const struct g3d_vertex *vb = (const struct g3d_vertex*)bptr;
23 for(i=0; i<zsort_cls.prim; i++) {
24 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
25 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
32 static int zsort_indexed_cmp(const void *aptr, const void *bptr)
37 const uint16_t *a = (const uint16_t*)aptr;
38 const uint16_t *b = (const uint16_t*)bptr;
40 const float *m = zsort_cls.xform;
42 for(i=0; i<zsort_cls.prim; i++) {
43 const struct g3d_vertex *va = zsort_cls.varr + a[i];
44 const struct g3d_vertex *vb = zsort_cls.varr + b[i];
46 za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
47 zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
53 void zsort_mesh(struct g3d_mesh *m)
55 zsort_cls.varr = m->varr;
56 zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
57 zsort_cls.prim = m->prim;
60 int nfaces = m->icount / m->prim;
61 qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_indexed_cmp);
63 int nfaces = m->vcount / m->prim;
64 qsort(m->varr, nfaces, m->prim * sizeof *m->varr, zsort_cmp);
69 void draw_mesh(struct g3d_mesh *mesh)
72 g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
74 g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
78 void apply_mesh_xform(struct g3d_mesh *mesh, const float *xform)
81 struct g3d_vertex *v = mesh->varr;
83 for(i=0; i<mesh->vcount; i++) {
84 float x = xform[0] * v->x + xform[4] * v->y + xform[8] * v->z + xform[12];
85 float y = xform[1] * v->x + xform[5] * v->y + xform[9] * v->z + xform[13];
86 v->z = xform[2] * v->x + xform[6] * v->y + xform[10] * v->z + xform[14];
89 x = xform[0] * v->nx + xform[4] * v->ny + xform[8] * v->nz;
90 y = xform[1] * v->nx + xform[5] * v->ny + xform[9] * v->nz;
91 v->nz = xform[2] * v->nx + xform[6] * v->ny + xform[10] * v->nz;
98 int append_mesh(struct g3d_mesh *ma, struct g3d_mesh *mb)
100 int i, new_vcount, new_icount;
104 if(ma->prim != mb->prim) {
105 fprintf(stderr, "append_mesh failed, primitive mismatch\n");
109 if(ma->iarr || mb->iarr) {
111 if(indexify_mesh(ma) == -1) {
114 } else if(!mb->iarr) {
115 if(indexify_mesh(mb) == -1) {
120 new_icount = ma->icount + mb->icount;
121 if(!(iptr = realloc(ma->iarr, new_icount * sizeof *iptr))) {
122 fprintf(stderr, "append_mesh: failed to allocate combined index buffer (%d indices)\n", new_icount);
128 for(i=0; i<mb->icount; i++) {
129 *iptr++ = mb->iarr[i] + ma->vcount;
131 ma->icount = new_icount;
134 new_vcount = ma->vcount + mb->vcount;
135 if(!(tmp = realloc(ma->varr, new_vcount * sizeof *ma->varr))) {
136 fprintf(stderr, "append_mesh: failed to allocate combined vertex buffer (%d verts)\n", new_vcount);
140 memcpy(ma->varr + ma->vcount, mb->varr, mb->vcount * sizeof *ma->varr);
141 ma->vcount = new_vcount;
145 #define FEQ(a, b) ((a) - (b) < 1e-5 && (b) - (a) < 1e-5)
146 static int cmp_vertex(struct g3d_vertex *a, struct g3d_vertex *b)
148 if(!FEQ(a->x, b->x) || !FEQ(a->y, b->y) || !FEQ(a->z, b->z) || !FEQ(a->w, b->w))
150 if(!FEQ(a->nx, b->nx) || !FEQ(a->ny, b->ny) || !FEQ(a->nz, b->nz))
152 if(!FEQ(a->u, b->u) || !FEQ(a->v, b->v))
154 if(a->r != b->r || a->g != b->g || a->b != b->b || a->a != b->a)
159 static int find_existing(struct g3d_vertex *v, struct g3d_vertex *varr, int vcount)
162 for(i=0; i<vcount; i++) {
163 if(cmp_vertex(v, varr++) == 0) {
170 int indexify_mesh(struct g3d_mesh *mesh)
172 int i, j, nfaces, max_icount, idx;
174 struct g3d_vertex *vin, *vout;
178 fprintf(stderr, "indexify_mesh failed: already indexed\n");
182 nfaces = mesh->vcount / mesh->prim;
183 max_icount = mesh->vcount;
185 if(!(mesh->iarr = malloc(max_icount * sizeof *mesh->iarr))) {
186 fprintf(stderr, "indexify_mesh failed to allocate index buffer of %d indices\n", max_icount);
190 vin = vout = mesh->varr;
193 for(i=0; i<nfaces; i++) {
194 for(j=0; j<mesh->prim; j++) {
195 if((idx = find_existing(vin, mesh->varr, out_vcount)) >= 0) {
198 *iout++ = out_vcount++;
207 /* XXX also shrink buffers? I'll just leave them to max size for now */
211 void normalize_mesh_normals(struct g3d_mesh *mesh)
214 struct g3d_vertex *v = mesh->varr;
216 for(i=0; i<mesh->vcount; i++) {
217 float mag = sqrt(v->nx * v->nx + v->ny * v->ny + v->nz * v->nz);
218 float s = (mag == 0.0f) ? 1.0f : 1.0f / mag;
227 static void sphvec(float *res, float theta, float phi, float rad)
230 res[0] = sin(theta) * sin(phi);
232 res[2] = cos(theta) * sin(phi);
235 int gen_sphere_mesh(struct g3d_mesh *mesh, float rad, int usub, int vsub)
238 int nfaces, uverts, vverts;
239 struct g3d_vertex *vptr;
242 mesh->prim = G3D_QUADS;
244 if(usub < 4) usub = 4;
245 if(vsub < 2) vsub = 2;
250 mesh->vcount = uverts * vverts;
251 nfaces = usub * vsub;
252 mesh->icount = nfaces * 4;
254 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
255 fprintf(stderr, "gen_sphere_mesh: failed to allocate vertex buffer (%d vertices)\n", mesh->vcount);
258 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
259 fprintf(stderr, "gen_sphere_mesh: failed to allocate index buffer (%d indices)\n", mesh->icount);
265 for(i=0; i<uverts; i++) {
266 float u = (float)i / (float)(uverts - 1);
267 float theta = u * 2.0 * M_PI;
269 for(j=0; j<vverts; j++) {
270 float v = (float)j / (float)(vverts - 1);
271 float phi = v * M_PI;
272 int chess = (i & 1) == (j & 1);
274 sphvec(&vptr->x, theta, phi, rad);
277 vptr->nx = vptr->x / rad;
278 vptr->ny = vptr->y / rad;
279 vptr->nz = vptr->z / rad;
282 vptr->r = chess ? 255 : 64;
284 vptr->b = chess ? 64 : 255;
287 if(i < usub && j < vsub) {
288 int idx = i * vverts + j;
291 *iptr++ = idx + vverts + 1;
292 *iptr++ = idx + vverts;
299 int gen_plane_mesh(struct g3d_mesh *m, float width, float height, int usub, int vsub)
302 int nfaces, nverts, nidx, uverts, vverts;
303 float x, y, u, v, du, dv;
304 struct g3d_vertex *vptr;
307 if(usub < 1) usub = 1;
308 if(vsub < 1) vsub = 1;
310 nfaces = usub * vsub;
313 du = (float)width / (float)usub;
314 dv = (float)height / (float)vsub;
316 nverts = uverts * vverts;
319 if(!(m->varr = malloc(nverts * sizeof *m->varr))) {
320 fprintf(stderr, "gen_plane_mesh: failed to allocate vertex buffer (%d vertices)\n", nverts);
323 if(!(m->iarr = malloc(nidx * sizeof *m->iarr))) {
324 fprintf(stderr, "gen_plane_mesh: failed to allocate index buffer (%d indices)\n", nidx);
338 for(i=0; i<vverts; i++) {
339 y = (v - 0.5) * height;
342 for(j=0; j<uverts; j++) {
343 x = (u - 0.5) * width;
354 vptr->r = vptr->g = vptr->b = vptr->a = 255;
362 for(i=0; i<vsub; i++) {
363 for(j=0; j<usub; j++) {
364 int idx = i * uverts + j;
367 *iptr++ = idx + uverts + 1;
368 *iptr++ = idx + uverts;
374 int gen_cube_mesh(struct g3d_mesh *mesh, float sz, int sub)
378 struct g3d_mesh tmpmesh;
379 static float rotface[][4] = {
388 g3d_matrix_mode(G3D_MODELVIEW);
392 m = i > 0 ? &tmpmesh : mesh;
393 if(gen_plane_mesh(m, sz, sz, sub, sub) == -1)
396 g3d_rotate(rotface[i][0], rotface[i][1], rotface[i][2], rotface[i][3]);
397 g3d_translate(0, 0, sz / 2.0f);
398 apply_mesh_xform(m, g3d_get_matrix(G3D_MODELVIEW, 0));
400 if(append_mesh(mesh, m) == -1) {
410 static void torusvec(float *res, float theta, float phi, float mr, float rr)
415 rx = -cos(phi) * rr + mr;
419 res[0] = rx * sin(theta) + rz * cos(theta);
421 res[2] = -rx * cos(theta) + rz * sin(theta);
424 int gen_torus_mesh(struct g3d_mesh *mesh, float rad, float ringrad, int usub, int vsub)
427 int nfaces, uverts, vverts;
428 struct g3d_vertex *vptr;
431 mesh->prim = G3D_QUADS;
433 if(usub < 4) usub = 4;
434 if(vsub < 2) vsub = 2;
439 mesh->vcount = uverts * vverts;
440 nfaces = usub * vsub;
441 mesh->icount = nfaces * 4;
443 printf("generating torus with %d faces (%d vertices)\n", nfaces, mesh->vcount);
445 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
448 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
454 for(i=0; i<uverts; i++) {
455 float u = (float)i / (float)(uverts - 1);
456 float theta = u * 2.0 * M_PI;
459 torusvec(rcent, theta, 0, rad, 0);
461 for(j=0; j<vverts; j++) {
462 float v = (float)j / (float)(vverts - 1);
463 float phi = v * 2.0 * M_PI;
464 int chess = (i & 1) == (j & 1);
466 torusvec(&vptr->x, theta, phi, rad, ringrad);
469 vptr->nx = (vptr->x - rcent[0]) / ringrad;
470 vptr->ny = (vptr->y - rcent[1]) / ringrad;
471 vptr->nz = (vptr->z - rcent[2]) / ringrad;
474 vptr->r = chess ? 255 : 64;
476 vptr->b = chess ? 64 : 255;
479 if(i < usub && j < vsub) {
480 int idx = i * vverts + j;
483 *iptr++ = idx + vverts + 1;
484 *iptr++ = idx + vverts;