major bsp bugs fixed
[dosdemo] / src / mesh.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include "mesh.h"
6 #include "3dgfx.h"
7
8 static struct {
9         int prim;
10         struct g3d_vertex *varr;
11         const float *xform;
12 } zsort_cls;
13
14 static int zsort_cmp(const void *aptr, const void *bptr)
15 {
16         int i;
17         float za = 0.0f;
18         float zb = 0.0f;
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;
22
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];
26                 ++va;
27                 ++vb;
28         }
29         return za - zb;
30 }
31
32 static int zsort_indexed_cmp(const void *aptr, const void *bptr)
33 {
34         int i;
35         float za = 0.0f;
36         float zb = 0.0f;
37         const uint16_t *a = (const uint16_t*)aptr;
38         const uint16_t *b = (const uint16_t*)bptr;
39
40         const float *m = zsort_cls.xform;
41
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];
45
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];
48         }
49         return za - zb;
50 }
51
52
53 void zsort_mesh(struct g3d_mesh *m)
54 {
55         zsort_cls.varr = m->varr;
56         zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
57         zsort_cls.prim = m->prim;
58
59         if(m->iarr) {
60                 int nfaces = m->icount / m->prim;
61                 qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_indexed_cmp);
62         } else {
63                 int nfaces = m->vcount / m->prim;
64                 qsort(m->varr, nfaces, m->prim * sizeof *m->varr, zsort_cmp);
65         }
66 }
67
68
69 void draw_mesh(struct g3d_mesh *mesh)
70 {
71         if(mesh->iarr) {
72                 g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
73         } else {
74                 g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
75         }
76 }
77
78 void apply_mesh_xform(struct g3d_mesh *mesh, const float *xform)
79 {
80         int i;
81         struct g3d_vertex *v = mesh->varr;
82
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];
87                 v->x = x;
88                 v->y = y;
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;
92                 v->nx = x;
93                 v->ny = y;
94                 ++v;
95         }
96 }
97
98 int append_mesh(struct g3d_mesh *ma, struct g3d_mesh *mb)
99 {
100         int i, new_vcount, new_icount;
101         void *tmp;
102         uint16_t *iptr;
103
104         if(ma->prim != mb->prim) {
105                 fprintf(stderr, "append_mesh failed, primitive mismatch\n");
106                 return -1;
107         }
108
109         if(ma->iarr || mb->iarr) {
110                 if(!ma->iarr) {
111                         if(indexify_mesh(ma) == -1) {
112                                 return -1;
113                         }
114                 } else if(!mb->iarr) {
115                         if(indexify_mesh(mb) == -1) {
116                                 return -1;
117                         }
118                 }
119
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);
123                         return -1;
124                 }
125                 ma->iarr = iptr;
126
127                 iptr += ma->icount;
128                 for(i=0; i<mb->icount; i++) {
129                         *iptr++ = mb->iarr[i] + ma->vcount;
130                 }
131                 ma->icount = new_icount;
132         }
133
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);
137                 return -1;
138         }
139         ma->varr = tmp;
140         memcpy(ma->varr + ma->vcount, mb->varr, mb->vcount * sizeof *ma->varr);
141         ma->vcount = new_vcount;
142         return 0;
143 }
144
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)
147 {
148         if(!FEQ(a->x, b->x) || !FEQ(a->y, b->y) || !FEQ(a->z, b->z) || !FEQ(a->w, b->w))
149                 return -1;
150         if(!FEQ(a->nx, b->nx) || !FEQ(a->ny, b->ny) || !FEQ(a->nz, b->nz))
151                 return -1;
152         if(!FEQ(a->u, b->u) || !FEQ(a->v, b->v))
153                 return -1;
154         if(a->r != b->r || a->g != b->g || a->b != b->b || a->a != b->a)
155                 return -1;
156         return 0;
157 }
158
159 static int find_existing(struct g3d_vertex *v, struct g3d_vertex *varr, int vcount)
160 {
161         int i;
162         for(i=0; i<vcount; i++) {
163                 if(cmp_vertex(v, varr++) == 0) {
164                         return i;
165                 }
166         }
167         return -1;
168 }
169
170 int indexify_mesh(struct g3d_mesh *mesh)
171 {
172         int i, j, nfaces, max_icount, idx;
173         int out_vcount = 0;
174         struct g3d_vertex *vin, *vout;
175         uint16_t *iout;
176
177         if(mesh->iarr) {
178                 fprintf(stderr, "indexify_mesh failed: already indexed\n");
179                 return -1;
180         }
181
182         nfaces = mesh->vcount / mesh->prim;
183         max_icount = mesh->vcount;
184
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);
187                 return -1;
188         }
189
190         vin = vout = mesh->varr;
191         iout = mesh->iarr;
192
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) {
196                                 *iout++ = idx;
197                         } else {
198                                 *iout++ = out_vcount++;
199                                 if(vin != vout) {
200                                         *vout++ = *vin;
201                                 }
202                         }
203                         ++vin;
204                 }
205         }
206
207         /* XXX also shrink buffers? I'll just leave them to max size for now */
208         return 0;
209 }
210
211 void normalize_mesh_normals(struct g3d_mesh *mesh)
212 {
213         int i;
214         struct g3d_vertex *v = mesh->varr;
215
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;
219                 v->nx *= s;
220                 v->ny *= s;
221                 v->nz *= s;
222                 ++v;
223         }
224 }
225
226 int gen_plane_mesh(struct g3d_mesh *m, float width, float height, int usub, int vsub)
227 {
228         int i, j;
229         int nfaces, nverts, nidx, uverts, vverts;
230         float x, y, u, v, du, dv;
231         struct g3d_vertex *vptr;
232         uint16_t *iptr;
233
234         if(usub < 1) usub = 1;
235         if(vsub < 1) vsub = 1;
236
237         nfaces = usub * vsub;
238         uverts = usub + 1;
239         vverts = vsub + 1;
240         du = (float)width / (float)usub;
241         dv = (float)height / (float)vsub;
242
243         nverts = uverts * vverts;
244         nidx = nfaces * 4;
245
246         if(!(m->varr = malloc(nverts * sizeof *m->varr))) {
247                 fprintf(stderr, "gen_plane_mesh: failed to allocate vertex buffer (%d vertices)\n", nverts);
248                 return -1;
249         }
250         if(!(m->iarr = malloc(nidx * sizeof *m->iarr))) {
251                 fprintf(stderr, "gen_plane_mesh: failed to allocate index buffer (%d indices)\n", nidx);
252                 free(m->varr);
253                 m->varr = 0;
254                 return -1;
255         }
256
257         m->prim = G3D_QUADS;
258         m->vcount = nverts;
259         m->icount = nidx;
260
261         vptr = m->varr;
262         iptr = m->iarr;
263
264         v = 0.0f;
265         for(i=0; i<vverts; i++) {
266                 y = (v - 0.5) * height;
267                 u = 0.0f;
268
269                 for(j=0; j<uverts; j++) {
270                         x = (u - 0.5) * width;
271
272                         vptr->x = x;
273                         vptr->y = y;
274                         vptr->z = 0.0f;
275                         vptr->w = 1.0f;
276                         vptr->nx = 0.0f;
277                         vptr->ny = 0.0f;
278                         vptr->nz = 1.0f;
279                         vptr->u = u;
280                         vptr->v = v;
281                         vptr->r = vptr->g = vptr->b = vptr->a = 255;
282                         ++vptr;
283
284                         u += du;
285                 }
286                 v += dv;
287         }
288
289         for(i=0; i<vsub; i++) {
290                 for(j=0; j<usub; j++) {
291                         int idx = i * uverts + j;
292                         *iptr++ = idx;
293                         *iptr++ = idx + 1;
294                         *iptr++ = idx + uverts + 1;
295                         *iptr++ = idx + uverts;
296                 }
297         }
298         return 0;
299 }
300
301 int gen_cube_mesh(struct g3d_mesh *mesh, float sz, int sub)
302 {
303         int i;
304         struct g3d_mesh *m;
305         struct g3d_mesh tmpmesh;
306         static float rotface[][4] = {
307                 {0, 0, 1, 0},
308                 {90, 0, 1, 0},
309                 {180, 0, 1, 0},
310                 {270, 0, 1, 0},
311                 {90, 1, 0, 0},
312                 {-90, 1, 0, 0}
313         };
314
315         g3d_matrix_mode(G3D_MODELVIEW);
316         g3d_push_matrix();
317
318         for(i=0; i<6; i++) {
319                 m = i > 0 ? &tmpmesh : mesh;
320                 if(gen_plane_mesh(m, sz, sz, sub, sub) == -1)
321                         return -1;
322                 g3d_load_identity();
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));
326                 if(i > 0) {
327                         if(append_mesh(mesh, m) == -1) {
328                                 return -1;
329                         }
330                 }
331         }
332
333         g3d_pop_matrix();
334         return 0;
335 }
336
337 static void torusvec(float *res, float theta, float phi, float mr, float rr)
338 {
339         float rx, ry, rz;
340         theta = -theta;
341
342         rx = -cos(phi) * rr + mr;
343         ry = sin(phi) * rr;
344         rz = 0.0f;
345
346         res[0] = rx * sin(theta) + rz * cos(theta);
347         res[1] = ry;
348         res[2] = -rx * cos(theta) + rz * sin(theta);
349 }
350
351 int gen_torus_mesh(struct g3d_mesh *mesh, float rad, float ringrad, int usub, int vsub)
352 {
353         int i, j;
354         int nfaces, uverts, vverts;
355         struct g3d_vertex *vptr;
356         uint16_t *iptr;
357
358         mesh->prim = G3D_QUADS;
359
360         if(usub < 4) usub = 4;
361         if(vsub < 2) vsub = 2;
362
363         uverts = usub + 1;
364         vverts = vsub + 1;
365
366         mesh->vcount = uverts * vverts;
367         nfaces = usub * vsub;
368         mesh->icount = nfaces * 4;
369
370         printf("generating torus with %d faces (%d vertices)\n", nfaces, mesh->vcount);
371
372         if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
373                 return -1;
374         }
375         if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
376                 return -1;
377         }
378         vptr = mesh->varr;
379         iptr = mesh->iarr;
380
381         for(i=0; i<uverts; i++) {
382                 float u = (float)i / (float)(uverts - 1);
383                 float theta = u * 2.0 * M_PI;
384                 float rcent[3];
385
386                 torusvec(rcent, theta, 0, rad, 0);
387
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);
392
393                         torusvec(&vptr->x, theta, phi, rad, ringrad);
394                         vptr->w = 1.0f;
395
396                         vptr->nx = (vptr->x - rcent[0]) / ringrad;
397                         vptr->ny = (vptr->y - rcent[1]) / ringrad;
398                         vptr->nz = (vptr->z - rcent[2]) / ringrad;
399                         vptr->u = u;
400                         vptr->v = v;
401                         vptr->r = chess ? 255 : 64;
402                         vptr->g = 128;
403                         vptr->b = chess ? 64 : 255;
404                         ++vptr;
405
406                         if(i < usub && j < vsub) {
407                                 int idx = i * vverts + j;
408                                 *iptr++ = idx;
409                                 *iptr++ = idx + 1;
410                                 *iptr++ = idx + vverts + 1;
411                                 *iptr++ = idx + vverts;
412                         }
413                 }
414         }
415         return 0;
416 }
417