1 |
greg |
2.1 |
#ifndef lint |
2 |
schorsch |
2.5 |
static const char RCSid[] = "$Id: writemesh.c,v 2.4 2004/01/30 00:08:31 greg Exp $"; |
3 |
greg |
2.1 |
#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 |
schorsch |
2.5 |
static void putfullnode(OCTREE fn, FILE *fp); |
15 |
|
|
static void puttree(OCTREE ot, FILE *fp); |
16 |
|
|
static void putpatch(MESHPATCH *pp, FILE *fp); |
17 |
|
|
|
18 |
|
|
|
19 |
greg |
2.1 |
static void |
20 |
schorsch |
2.5 |
putfullnode( /* write out a full node */ |
21 |
|
|
OCTREE fn, |
22 |
|
|
FILE *fp |
23 |
|
|
) |
24 |
greg |
2.1 |
{ |
25 |
|
|
OBJECT oset[MAXSET+1]; |
26 |
|
|
register int i; |
27 |
|
|
|
28 |
|
|
objset(oset, fn); |
29 |
|
|
for (i = 0; i <= oset[0]; i++) |
30 |
|
|
putint((long)oset[i], sizeof(OBJECT), fp); |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
static void |
35 |
schorsch |
2.5 |
puttree( /* write octree to fp in pre-order form */ |
36 |
|
|
register OCTREE ot, |
37 |
|
|
FILE *fp |
38 |
|
|
) |
39 |
greg |
2.1 |
{ |
40 |
|
|
|
41 |
|
|
if (istree(ot)) { |
42 |
|
|
register int i; |
43 |
|
|
putc(OT_TREE, fp); /* indicate tree */ |
44 |
|
|
for (i = 0; i < 8; i++) /* write tree */ |
45 |
|
|
puttree(octkid(ot, i), fp); |
46 |
|
|
return; |
47 |
|
|
} |
48 |
|
|
if (isfull(ot)) { |
49 |
|
|
putc(OT_FULL, fp); /* indicate fullnode */ |
50 |
|
|
putfullnode(ot, fp); /* write fullnode */ |
51 |
|
|
return; |
52 |
|
|
} |
53 |
|
|
putc(OT_EMPTY, fp); /* indicate empty */ |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
|
57 |
|
|
static void |
58 |
schorsch |
2.5 |
putpatch( /* write out a mesh patch */ |
59 |
|
|
register MESHPATCH *pp, |
60 |
|
|
FILE *fp |
61 |
|
|
) |
62 |
greg |
2.1 |
{ |
63 |
|
|
int flags = MT_V; |
64 |
|
|
int i, j; |
65 |
|
|
/* vertex flags */ |
66 |
|
|
if (pp->norm != NULL) |
67 |
|
|
flags |= MT_N; |
68 |
|
|
if (pp->uv != NULL) |
69 |
|
|
flags |= MT_UV; |
70 |
|
|
putint((long)flags, 1, fp); |
71 |
|
|
/* number of vertices */ |
72 |
|
|
putint((long)pp->nverts, 2, fp); |
73 |
|
|
/* vertex xyz locations */ |
74 |
|
|
for (i = 0; i < pp->nverts; i++) |
75 |
|
|
for (j = 0; j < 3; j++) |
76 |
|
|
putint((long)pp->xyz[i][j], 4, fp); |
77 |
|
|
/* vertex normals */ |
78 |
|
|
if (flags & MT_N) |
79 |
|
|
for (i = 0; i < pp->nverts; i++) |
80 |
|
|
putint((long)pp->norm[i], 4, fp); |
81 |
|
|
/* uv coordinates */ |
82 |
|
|
if (flags & MT_UV) |
83 |
|
|
for (i = 0; i < pp->nverts; i++) |
84 |
|
|
for (j = 0; j < 2; j++) |
85 |
greg |
2.4 |
putint((long)pp->uv[i][j], 4, fp); |
86 |
greg |
2.1 |
/* local triangles */ |
87 |
|
|
putint((long)pp->ntris, 2, fp); |
88 |
|
|
for (i = 0; i < pp->ntris; i++) { |
89 |
|
|
putint((long)pp->tri[i].v1, 1, fp); |
90 |
|
|
putint((long)pp->tri[i].v2, 1, fp); |
91 |
|
|
putint((long)pp->tri[i].v3, 1, fp); |
92 |
|
|
} |
93 |
greg |
2.2 |
/* local triangle material(s) */ |
94 |
|
|
if (pp->trimat == NULL) { |
95 |
|
|
putint(1L, 2, fp); |
96 |
|
|
putint((long)pp->solemat, 2, fp); |
97 |
|
|
} else { |
98 |
|
|
putint((long)pp->ntris, 2, fp); |
99 |
|
|
for (i = 0; i < pp->ntris; i++) |
100 |
|
|
putint((long)pp->trimat[i], 2, fp); |
101 |
|
|
} |
102 |
greg |
2.1 |
/* joiner triangles */ |
103 |
|
|
putint((long)pp->nj1tris, 2, fp); |
104 |
|
|
for (i = 0; i < pp->nj1tris; i++) { |
105 |
|
|
putint((long)pp->j1tri[i].v1j, 4, fp); |
106 |
|
|
putint((long)pp->j1tri[i].v2, 1, fp); |
107 |
|
|
putint((long)pp->j1tri[i].v3, 1, fp); |
108 |
greg |
2.2 |
putint((long)pp->j1tri[i].mat, 2, fp); |
109 |
greg |
2.1 |
} |
110 |
|
|
/* double joiner triangles */ |
111 |
|
|
putint((long)pp->nj2tris, 2, fp); |
112 |
|
|
for (i = 0; i < pp->nj2tris; i++) { |
113 |
|
|
putint((long)pp->j2tri[i].v1j, 4, fp); |
114 |
|
|
putint((long)pp->j2tri[i].v2j, 4, fp); |
115 |
|
|
putint((long)pp->j2tri[i].v3, 1, fp); |
116 |
greg |
2.2 |
putint((long)pp->j2tri[i].mat, 2, fp); |
117 |
greg |
2.1 |
} |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
|
121 |
|
|
void |
122 |
|
|
writemesh(mp, fp) /* write mesh structures to fp */ |
123 |
|
|
MESH *mp; |
124 |
|
|
FILE *fp; |
125 |
|
|
{ |
126 |
greg |
2.2 |
char *err; |
127 |
greg |
2.1 |
char sbuf[64]; |
128 |
|
|
int i; |
129 |
greg |
2.2 |
/* do we have everything? */ |
130 |
greg |
2.1 |
if ((mp->ldflags & (IO_SCENE|IO_TREE|IO_BOUNDS)) != |
131 |
|
|
(IO_SCENE|IO_TREE|IO_BOUNDS)) |
132 |
|
|
error(INTERNAL, "missing data in writemesh"); |
133 |
greg |
2.2 |
/* validate mesh data */ |
134 |
|
|
if ((err = checkmesh(mp)) != NULL) |
135 |
|
|
error(USER, err); |
136 |
greg |
2.1 |
/* write format number */ |
137 |
|
|
putint((long)(MESHMAGIC+sizeof(OBJECT)), 2, fp); |
138 |
|
|
/* write boundaries */ |
139 |
|
|
for (i = 0; i < 3; i++) { |
140 |
|
|
sprintf(sbuf, "%.12g", mp->mcube.cuorg[i]); |
141 |
|
|
putstr(sbuf, fp); |
142 |
|
|
} |
143 |
|
|
sprintf(sbuf, "%.12g", mp->mcube.cusize); |
144 |
|
|
putstr(sbuf, fp); |
145 |
|
|
for (i = 0; i < 2; i++) { |
146 |
|
|
putflt(mp->uvlim[0][i], fp); |
147 |
|
|
putflt(mp->uvlim[1][i], fp); |
148 |
|
|
} |
149 |
|
|
/* write the octree */ |
150 |
|
|
puttree(mp->mcube.cutree, fp); |
151 |
greg |
2.2 |
/* write the materials */ |
152 |
|
|
writescene(mp->mat0, mp->nmats, fp); |
153 |
greg |
2.1 |
/* write the patches */ |
154 |
|
|
putint((long)mp->npatches, 4, fp); |
155 |
|
|
for (i = 0; i < mp->npatches; i++) |
156 |
|
|
putpatch(&mp->patch[i], fp); |
157 |
|
|
if (ferror(fp)) |
158 |
|
|
error(SYSTEM, "write error in writemesh"); |
159 |
|
|
} |