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;
226 int gen_plane_mesh(struct g3d_mesh *m, float width, float height, int usub, int vsub)
229 int nfaces, nverts, nidx, uverts, vverts;
230 float x, y, u, v, du, dv;
231 struct g3d_vertex *vptr;
234 if(usub < 1) usub = 1;
235 if(vsub < 1) vsub = 1;
237 nfaces = usub * vsub;
240 du = (float)width / (float)usub;
241 dv = (float)height / (float)vsub;
243 nverts = uverts * vverts;
246 if(!(m->varr = malloc(nverts * sizeof *m->varr))) {
247 fprintf(stderr, "gen_plane_mesh: failed to allocate vertex buffer (%d vertices)\n", nverts);
250 if(!(m->iarr = malloc(nidx * sizeof *m->iarr))) {
251 fprintf(stderr, "gen_plane_mesh: failed to allocate index buffer (%d indices)\n", nidx);
265 for(i=0; i<vverts; i++) {
266 y = (v - 0.5) * height;
269 for(j=0; j<uverts; j++) {
270 x = (u - 0.5) * width;
281 vptr->r = vptr->g = vptr->b = vptr->a = 255;
289 for(i=0; i<vsub; i++) {
290 for(j=0; j<usub; j++) {
291 int idx = i * uverts + j;
294 *iptr++ = idx + uverts + 1;
295 *iptr++ = idx + uverts;
301 int gen_cube_mesh(struct g3d_mesh *mesh, float sz, int sub)
305 struct g3d_mesh tmpmesh;
306 static float rotface[][4] = {
315 g3d_matrix_mode(G3D_MODELVIEW);
319 m = i > 0 ? &tmpmesh : mesh;
320 if(gen_plane_mesh(m, sz, sz, sub, sub) == -1)
323 g3d_rotate(rotface[i][0], rotface[i][1], rotface[i][2], rotface[i][3]);
324 g3d_translate(0, 0, sz / 2.0f);
325 apply_mesh_xform(m, g3d_get_matrix(G3D_MODELVIEW, 0));
327 if(append_mesh(mesh, m) == -1) {
337 static void torusvec(float *res, float theta, float phi, float mr, float rr)
342 rx = -cos(phi) * rr + mr;
346 res[0] = rx * sin(theta) + rz * cos(theta);
348 res[2] = -rx * cos(theta) + rz * sin(theta);
351 int gen_torus_mesh(struct g3d_mesh *mesh, float rad, float ringrad, int usub, int vsub)
354 int nfaces, uverts, vverts;
355 struct g3d_vertex *vptr;
358 mesh->prim = G3D_QUADS;
360 if(usub < 4) usub = 4;
361 if(vsub < 2) vsub = 2;
366 mesh->vcount = uverts * vverts;
367 nfaces = usub * vsub;
368 mesh->icount = nfaces * 4;
370 printf("generating torus with %d faces (%d vertices)\n", nfaces, mesh->vcount);
372 if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
375 if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
381 for(i=0; i<uverts; i++) {
382 float u = (float)i / (float)(uverts - 1);
383 float theta = u * 2.0 * M_PI;
386 torusvec(rcent, theta, 0, rad, 0);
388 for(j=0; j<vverts; j++) {
389 float v = (float)j / (float)(vverts - 1);
390 float phi = v * 2.0 * M_PI;
391 int chess = (i & 1) == (j & 1);
393 torusvec(&vptr->x, theta, phi, rad, ringrad);
396 vptr->nx = (vptr->x - rcent[0]) / ringrad;
397 vptr->ny = (vptr->y - rcent[1]) / ringrad;
398 vptr->nz = (vptr->z - rcent[2]) / ringrad;
401 vptr->r = chess ? 255 : 64;
403 vptr->b = chess ? 64 : 255;
406 if(i < usub && j < vsub) {
407 int idx = i * vverts + j;
410 *iptr++ = idx + vverts + 1;
411 *iptr++ = idx + vverts;