1 | /* |
---|
2 | |
---|
3 | Converts from Paul Ning's polygon file format to text polygon description. |
---|
4 | |
---|
5 | Greg Turk - November 1992 |
---|
6 | |
---|
7 | */ |
---|
8 | |
---|
9 | #include <stdio.h> |
---|
10 | #include <malloc.h> |
---|
11 | #include "mcfile.h" |
---|
12 | #include <math.h> |
---|
13 | #include <string.h> |
---|
14 | #include <Inventor/So.h> |
---|
15 | #include <Inventor/nodes/SoIndexedFaceSet.h> |
---|
16 | #include <Inventor/actions/SoWriteAction.h> |
---|
17 | |
---|
18 | TriangleVertex *verts; |
---|
19 | int npolys; |
---|
20 | |
---|
21 | |
---|
22 | void write_polys_inventor(FILE *fp); |
---|
23 | void write_polys(FILE *fp_out); |
---|
24 | |
---|
25 | |
---|
26 | /****************************************************************************** |
---|
27 | Main routine. |
---|
28 | ******************************************************************************/ |
---|
29 | |
---|
30 | void |
---|
31 | main(int, char **argv) |
---|
32 | { |
---|
33 | int i,j; |
---|
34 | FILE *fp; |
---|
35 | FILE *fp_out; |
---|
36 | char infile[80],outfile[80]; |
---|
37 | mcfile header; |
---|
38 | int index; |
---|
39 | |
---|
40 | strcpy (infile, argv[1]); |
---|
41 | strcpy (outfile, argv[2]); |
---|
42 | |
---|
43 | if (strlen (infile) < 3 || |
---|
44 | strcmp (infile + strlen (infile) - 3, ".mc") != 0) |
---|
45 | strcat (infile, ".mc"); |
---|
46 | |
---|
47 | if (strlen (outfile) < 5 || |
---|
48 | strcmp (outfile + strlen (outfile) - 5, ".poly") != 0) |
---|
49 | strcat (outfile, ".poly"); |
---|
50 | |
---|
51 | printf ("reading polygons from '%s'\n", infile); |
---|
52 | |
---|
53 | /* open the polygon input file */ |
---|
54 | |
---|
55 | if ((fp = fopen(infile, "r")) == NULL) { |
---|
56 | fprintf (stderr, "bad open\n"); |
---|
57 | exit (-1); |
---|
58 | } |
---|
59 | |
---|
60 | /* open the polygon output file */ |
---|
61 | |
---|
62 | if ((fp_out = fopen(outfile, "w+b")) == NULL) { |
---|
63 | fprintf (stderr, "bad open\n"); |
---|
64 | exit (-1); |
---|
65 | } |
---|
66 | |
---|
67 | /* read header info from the polygon file */ |
---|
68 | header = MC_ReadHeader (fp); |
---|
69 | |
---|
70 | /* |
---|
71 | npolys = header.mc_length / (3 * sizeof (sizeof(TriangleVertex))); |
---|
72 | */ |
---|
73 | npolys = header.mc_length / 42; |
---|
74 | printf ("%d polygons\n", npolys); |
---|
75 | verts = (TriangleVertex *) malloc (sizeof (TriangleVertex) * 3 * npolys); |
---|
76 | if (verts == 0) { |
---|
77 | fprintf (stderr, "can't allocate enough space\n"); |
---|
78 | exit (-1); |
---|
79 | } |
---|
80 | |
---|
81 | /* read in the polygons */ |
---|
82 | printf ("reading polygons...\n"); |
---|
83 | for (i = 0; i < npolys; i++) { |
---|
84 | for (j = 0; j < 3; j++) { |
---|
85 | index = i * 3 + j; |
---|
86 | verts[index] = MC_ReadTriangleVertex (fp); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | /* write polygons to output file */ |
---|
91 | printf ("writing polygons...\n"); |
---|
92 | write_polys_inventor (fp_out); |
---|
93 | fclose (fp_out); |
---|
94 | |
---|
95 | printf ("done.\n"); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | /****************************************************************************** |
---|
100 | Write out polygons in Inventor format. |
---|
101 | ******************************************************************************/ |
---|
102 | |
---|
103 | void |
---|
104 | write_polys_inventor(FILE *fp) |
---|
105 | { |
---|
106 | int i,j; |
---|
107 | int index; |
---|
108 | float x,y,z; |
---|
109 | float nx,ny,nz; |
---|
110 | float len; |
---|
111 | |
---|
112 | SoDB::init(); |
---|
113 | |
---|
114 | SoSeparator *root = new SoSeparator; |
---|
115 | root->ref(); |
---|
116 | |
---|
117 | SoCoordinate3 *coord = new SoCoordinate3; |
---|
118 | root->addChild(coord); |
---|
119 | |
---|
120 | coord->point.insertSpace(0, npolys*3); |
---|
121 | |
---|
122 | for (i = 0; i < npolys; i++) { |
---|
123 | for (j = 0; j < 3; j++) { |
---|
124 | index = i * 3 + j; |
---|
125 | x = verts[index].x / 128.0; |
---|
126 | y = verts[index].y / 128.0; |
---|
127 | z = verts[index].z / 128.0; |
---|
128 | coord->point.set1Value(index, x, y, z); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | /* |
---|
133 | SoNormal *norm = new SoNormal; |
---|
134 | root->addChild(norm); |
---|
135 | |
---|
136 | norm->vector.insertSpace(0, npolys*3); |
---|
137 | |
---|
138 | for (i = 0; i < npolys; i++) { |
---|
139 | for (j = 0; j < 3; j++) { |
---|
140 | index = i * 3 + j; |
---|
141 | nx = -verts[index].nx; |
---|
142 | ny = -verts[index].ny; |
---|
143 | nz = -verts[index].nz; |
---|
144 | len = sqrt (nx*nx + ny*ny + nz*nz); |
---|
145 | nx /= len; |
---|
146 | ny /= len; |
---|
147 | nz /= len; |
---|
148 | norm->vector.set1Value(index, nx, ny, nz); |
---|
149 | } |
---|
150 | } |
---|
151 | */ |
---|
152 | |
---|
153 | SoIndexedFaceSet *faceSet = new SoIndexedFaceSet; |
---|
154 | root->addChild(faceSet); |
---|
155 | |
---|
156 | faceSet->coordIndex.insertSpace(0, npolys*4); |
---|
157 | index = 0; |
---|
158 | for (i = 0; i < npolys; i++) { |
---|
159 | SbVec3f v1(verts[i*3].x, verts[i*3].y, verts[i*3].z); |
---|
160 | SbVec3f v2(verts[i*3+1].x, verts[i*3+1].y, verts[i*3+1].z); |
---|
161 | SbVec3f v3(verts[i*3+2].x, verts[i*3+2].y, verts[i*3+2].z); |
---|
162 | |
---|
163 | SbVec3f a = v2 - v1; |
---|
164 | SbVec3f b = v3 - v1; |
---|
165 | SbVec3f c = a.cross(b); |
---|
166 | SbVec3f norm(-verts[i*3].nx, -verts[i*3].ny, -verts[i*3].nz); |
---|
167 | if (c.dot(norm) > 0) { |
---|
168 | faceSet->coordIndex.set1Value(index, i*3); |
---|
169 | faceSet->coordIndex.set1Value(index+1, i*3+1); |
---|
170 | faceSet->coordIndex.set1Value(index+2, i*3+2); |
---|
171 | faceSet->coordIndex.set1Value(index+3, -1); |
---|
172 | } |
---|
173 | else { |
---|
174 | faceSet->coordIndex.set1Value(index, i*3+1); |
---|
175 | faceSet->coordIndex.set1Value(index+1, i*3); |
---|
176 | faceSet->coordIndex.set1Value(index+2, i*3+2); |
---|
177 | faceSet->coordIndex.set1Value(index+3, -1); |
---|
178 | } |
---|
179 | index += 4; |
---|
180 | } |
---|
181 | |
---|
182 | SoWriteAction wa; |
---|
183 | wa.getOutput()->setFilePointer(fp); |
---|
184 | wa.getOutput()->setBinary(TRUE); |
---|
185 | wa.apply(root); |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | /* write out the vertices */ |
---|
190 | |
---|
191 | /* |
---|
192 | fprintf (fp, "#Inventor V1.0 ascii\n"); |
---|
193 | fprintf (fp, "Separator {\n"); |
---|
194 | |
---|
195 | fprintf (fp, "Coordinate3 {\n"); |
---|
196 | fprintf (fp, "point [\n"); |
---|
197 | for (i = 0; i < npolys; i++) { |
---|
198 | for (j = 0; j < 3; j++) { |
---|
199 | index = i * 3 + j; |
---|
200 | x = verts[index].x / 128.0; |
---|
201 | y = verts[index].y / 128.0; |
---|
202 | z = verts[index].z / 128.0; |
---|
203 | fprintf (fp, "%f %f %f,\n", x, y, z); |
---|
204 | } |
---|
205 | } |
---|
206 | fprintf (fp, "]\n"); |
---|
207 | fprintf (fp, "}\n"); |
---|
208 | */ |
---|
209 | |
---|
210 | /* write out the vertex normals */ |
---|
211 | |
---|
212 | /* |
---|
213 | fprintf (fp, "Normal {\n"); |
---|
214 | fprintf (fp, "vector [\n"); |
---|
215 | for (i = 0; i < npolys; i++) { |
---|
216 | for (j = 0; j < 3; j++) { |
---|
217 | index = i * 3 + j; |
---|
218 | nx = verts[index].nx; |
---|
219 | ny = verts[index].ny; |
---|
220 | nz = verts[index].nz; |
---|
221 | len = sqrt (nx*nx + ny*ny + nz*nz); |
---|
222 | nx /= len; |
---|
223 | ny /= len; |
---|
224 | nz /= len; |
---|
225 | fprintf (fp, "%f %f %f,\n", -nx, -ny, -nz); |
---|
226 | } |
---|
227 | } |
---|
228 | fprintf (fp, "]\n"); |
---|
229 | fprintf (fp, "}\n"); |
---|
230 | */ |
---|
231 | |
---|
232 | /* write out the vertex indices for each face */ |
---|
233 | |
---|
234 | /* |
---|
235 | fprintf (fp, "IndexedFaceSet {\n"); |
---|
236 | fprintf (fp, "coordIndex [\n"); |
---|
237 | for (i = 0; i < npolys; i++) { |
---|
238 | fprintf (fp, "%d, %d, %d, -1,\n", i*3, i*3+1, i*3+2); |
---|
239 | } |
---|
240 | fprintf (fp, "]\n"); |
---|
241 | fprintf (fp, "}\n"); |
---|
242 | |
---|
243 | fprintf (fp, "}\n"); |
---|
244 | */ |
---|
245 | |
---|
246 | |
---|
247 | |
---|
248 | /****************************************************************************** |
---|
249 | Write out polygons in my format. |
---|
250 | ******************************************************************************/ |
---|
251 | |
---|
252 | void |
---|
253 | write_polys(FILE *fp_out) |
---|
254 | { |
---|
255 | int i,j; |
---|
256 | int index; |
---|
257 | float x,y,z; |
---|
258 | float nx,ny,nz; |
---|
259 | float len; |
---|
260 | |
---|
261 | fprintf (fp_out, "vertices: %d\n", npolys * 3); |
---|
262 | fprintf (fp_out, "faces: %d\n", npolys); |
---|
263 | |
---|
264 | /* write out the vertices */ |
---|
265 | for (i = 0; i < npolys; i++) { |
---|
266 | for (j = 0; j < 3; j++) { |
---|
267 | index = i * 3 + j; |
---|
268 | x = verts[index].x / 128.0; |
---|
269 | y = verts[index].y / 128.0; |
---|
270 | z = verts[index].z / 128.0; |
---|
271 | nx = verts[index].nx; |
---|
272 | ny = verts[index].ny; |
---|
273 | nz = verts[index].nz; |
---|
274 | len = sqrt (nx*nx + ny*ny + nz*nz); |
---|
275 | nx /= len; |
---|
276 | ny /= len; |
---|
277 | nz /= len; |
---|
278 | fprintf (fp_out, "v %f %f %f %f %f %f\n", x, y, z, nx, ny, nz); |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | /* write out the vertex indices for each face */ |
---|
283 | for (i = 0; i < npolys; i++) { |
---|
284 | fprintf (fp_out, "f %d %d %d\n", i*3+1, i*3+2, i*3+3); |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|