1 |
greg |
2.1 |
#ifndef lint |
2 |
|
|
static const char RCSid[] = "$Id$"; |
3 |
|
|
#endif |
4 |
|
|
/* |
5 |
|
|
* Routines for writing compiled mesh to a file stream |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "standard.h" |
9 |
|
|
#include "octree.h" |
10 |
|
|
#include "object.h" |
11 |
|
|
#include "mesh.h" |
12 |
|
|
|
13 |
|
|
|
14 |
|
|
static void |
15 |
|
|
putfullnode(fn, fp) /* write out a full node */ |
16 |
|
|
OCTREE fn; |
17 |
|
|
FILE *fp; |
18 |
|
|
{ |
19 |
|
|
OBJECT oset[MAXSET+1]; |
20 |
|
|
register int i; |
21 |
|
|
|
22 |
|
|
objset(oset, fn); |
23 |
|
|
for (i = 0; i <= oset[0]; i++) |
24 |
|
|
putint((long)oset[i], sizeof(OBJECT), fp); |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
|
28 |
|
|
static void |
29 |
|
|
puttree(ot, fp) /* write octree to fp in pre-order form */ |
30 |
|
|
register OCTREE ot; |
31 |
|
|
FILE *fp; |
32 |
|
|
{ |
33 |
|
|
|
34 |
|
|
if (istree(ot)) { |
35 |
|
|
register int i; |
36 |
|
|
putc(OT_TREE, fp); /* indicate tree */ |
37 |
|
|
for (i = 0; i < 8; i++) /* write tree */ |
38 |
|
|
puttree(octkid(ot, i), fp); |
39 |
|
|
return; |
40 |
|
|
} |
41 |
|
|
if (isfull(ot)) { |
42 |
|
|
putc(OT_FULL, fp); /* indicate fullnode */ |
43 |
|
|
putfullnode(ot, fp); /* write fullnode */ |
44 |
|
|
return; |
45 |
|
|
} |
46 |
|
|
putc(OT_EMPTY, fp); /* indicate empty */ |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
|
50 |
|
|
static void |
51 |
|
|
putpatch(pp, fp) /* write out a mesh patch */ |
52 |
|
|
register MESHPATCH *pp; |
53 |
|
|
FILE *fp; |
54 |
|
|
{ |
55 |
|
|
int flags = MT_V; |
56 |
|
|
int i, j; |
57 |
|
|
/* vertex flags */ |
58 |
|
|
if (pp->norm != NULL) |
59 |
|
|
flags |= MT_N; |
60 |
|
|
if (pp->uv != NULL) |
61 |
|
|
flags |= MT_UV; |
62 |
|
|
putint((long)flags, 1, fp); |
63 |
|
|
/* number of vertices */ |
64 |
|
|
putint((long)pp->nverts, 2, fp); |
65 |
|
|
/* vertex xyz locations */ |
66 |
|
|
for (i = 0; i < pp->nverts; i++) |
67 |
|
|
for (j = 0; j < 3; j++) |
68 |
|
|
putint((long)pp->xyz[i][j], 4, fp); |
69 |
|
|
/* vertex normals */ |
70 |
|
|
if (flags & MT_N) |
71 |
|
|
for (i = 0; i < pp->nverts; i++) |
72 |
|
|
putint((long)pp->norm[i], 4, fp); |
73 |
|
|
/* uv coordinates */ |
74 |
|
|
if (flags & MT_UV) |
75 |
|
|
for (i = 0; i < pp->nverts; i++) |
76 |
|
|
for (j = 0; j < 2; j++) |
77 |
|
|
putint((long)pp->uv[i][j], 4, fp); |
78 |
|
|
/* local triangles */ |
79 |
|
|
putint((long)pp->ntris, 2, fp); |
80 |
|
|
for (i = 0; i < pp->ntris; i++) { |
81 |
|
|
putint((long)pp->tri[i].v1, 1, fp); |
82 |
|
|
putint((long)pp->tri[i].v2, 1, fp); |
83 |
|
|
putint((long)pp->tri[i].v3, 1, fp); |
84 |
|
|
} |
85 |
|
|
/* joiner triangles */ |
86 |
|
|
putint((long)pp->nj1tris, 2, fp); |
87 |
|
|
for (i = 0; i < pp->nj1tris; i++) { |
88 |
|
|
putint((long)pp->j1tri[i].v1j, 4, fp); |
89 |
|
|
putint((long)pp->j1tri[i].v2, 1, fp); |
90 |
|
|
putint((long)pp->j1tri[i].v3, 1, fp); |
91 |
|
|
} |
92 |
|
|
/* double joiner triangles */ |
93 |
|
|
putint((long)pp->nj2tris, 2, fp); |
94 |
|
|
for (i = 0; i < pp->nj2tris; i++) { |
95 |
|
|
putint((long)pp->j2tri[i].v1j, 4, fp); |
96 |
|
|
putint((long)pp->j2tri[i].v2j, 4, fp); |
97 |
|
|
putint((long)pp->j2tri[i].v3, 1, fp); |
98 |
|
|
} |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
|
102 |
|
|
void |
103 |
|
|
writemesh(mp, fp) /* write mesh structures to fp */ |
104 |
|
|
MESH *mp; |
105 |
|
|
FILE *fp; |
106 |
|
|
{ |
107 |
|
|
char sbuf[64]; |
108 |
|
|
int i; |
109 |
|
|
/* check that we have everything */ |
110 |
|
|
if ((mp->ldflags & (IO_SCENE|IO_TREE|IO_BOUNDS)) != |
111 |
|
|
(IO_SCENE|IO_TREE|IO_BOUNDS)) |
112 |
|
|
error(INTERNAL, "missing data in writemesh"); |
113 |
|
|
/* write format number */ |
114 |
|
|
putint((long)(MESHMAGIC+sizeof(OBJECT)), 2, fp); |
115 |
|
|
/* write boundaries */ |
116 |
|
|
for (i = 0; i < 3; i++) { |
117 |
|
|
sprintf(sbuf, "%.12g", mp->mcube.cuorg[i]); |
118 |
|
|
putstr(sbuf, fp); |
119 |
|
|
} |
120 |
|
|
sprintf(sbuf, "%.12g", mp->mcube.cusize); |
121 |
|
|
putstr(sbuf, fp); |
122 |
|
|
for (i = 0; i < 2; i++) { |
123 |
|
|
putflt(mp->uvlim[0][i], fp); |
124 |
|
|
putflt(mp->uvlim[1][i], fp); |
125 |
|
|
} |
126 |
|
|
/* write the octree */ |
127 |
|
|
puttree(mp->mcube.cutree, fp); |
128 |
|
|
/* write the patches */ |
129 |
|
|
putint((long)mp->npatches, 4, fp); |
130 |
|
|
for (i = 0; i < mp->npatches; i++) |
131 |
|
|
putpatch(&mp->patch[i], fp); |
132 |
|
|
if (ferror(fp)) |
133 |
|
|
error(SYSTEM, "write error in writemesh"); |
134 |
|
|
} |