1 /* tri2obj.c -- read in a .ele/.node file pair generated by the triangle
2 * program and output a simple Wavefront .obj file.
4 * Written by Curtis Olson, started October 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * (Log is kept at end of this file)
32 #include "../../Src/Include/constants.h"
33 #include "../../Src/Include/types.h"
34 #include "../../Src/Math/fg_geodesy.h"
35 #include "../../Src/Math/mat3.h"
36 #include "../../Src/Math/polar.h"
39 int nodecount, tricount;
40 struct fgCartesianPoint nodes[MAX_NODES];
41 int tris[MAX_TRIS][3];
42 int new_tris[MAX_TRIS][3];
45 /* convert a geodetic point lon(arcsec), lat(arcsec), elev(meter) to
46 * a cartesian point */
47 struct fgCartesianPoint geod_to_cart(double geod[3]) {
48 struct fgCartesianPoint p;
49 double gc_lon, gc_lat, sl_radius;
51 /* printf("A geodetic point is (%.2f, %.2f, %.2f)\n",
52 geod[0], geod[1], geod[2]); */
54 gc_lon = geod[0]*ARCSEC_TO_RAD;
55 fgGeodToGeoc(geod[1]*ARCSEC_TO_RAD, geod[2], &sl_radius, &gc_lat);
57 /* printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon,
58 gc_lat, sl_radius+geod[2]); */
60 p = fgPolarToCart(gc_lon, gc_lat, sl_radius+geod[2]);
62 /* printf("A cart point is (%.8f, %.8f, %.8f)\n", p.x, p.y, p.z); */
68 /* given three points defining a triangle, calculate the normal */
69 void calc_normal(struct fgCartesianPoint p1, struct fgCartesianPoint p2,
70 struct fgCartesianPoint p3, double normal[3])
75 v1[0] = p2.x - p1.x; v1[1] = p2.y - p1.y; v1[2] = p2.z - p1.z;
76 v2[0] = p3.x - p1.x; v2[1] = p3.y - p1.y; v2[2] = p3.z - p1.z;
78 MAT3cross_product(normal, v1, v2);
79 MAT3_NORMALIZE_VEC(normal,temp);
81 /* printf(" Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]); */
85 /* return the index of all triangles containing the specified node */
86 void find_tris(int n, int *t1, int *t2, int *t3, int *t4, int *t5) {
89 *t1 = *t2 = *t3 = *t4 = *t5 = 0;
92 while ( i <= tricount ) {
93 if ( (n == tris[i][0]) || (n == tris[i][1]) || (n == tris[i][2]) ) {
96 } else if ( *t2 == 0 ) {
98 } else if ( *t3 == 0 ) {
100 } else if ( *t4 == 0 ) {
111 /* Initialize a new mesh structure */
112 void triload(char *basename) {
113 char nodename[256], elename[256];
116 int dim, junk1, junk2;
119 strcpy(nodename, basename);
120 strcat(nodename, ".node");
121 strcpy(elename, basename);
122 strcat(elename, ".ele");
124 printf("Loading node file: %s ...\n", nodename);
125 if ( (node = fopen(nodename, "r")) == NULL ) {
126 printf("Cannot open file '%s'\n", nodename);
130 fscanf(node, "%d %d %d %d", &nodecount, &dim, &junk1, &junk2);
132 if ( nodecount > MAX_NODES - 1 ) {
133 printf("Error, too many nodes, need to increase array size\n");
136 printf(" Expecting %d nodes\n", nodecount);
139 for ( i = 1; i <= nodecount; i++ ) {
140 fscanf(node, "%d %lf %lf %lf %d\n", &junk1,
141 &n[0], &n[1], &n[2], &junk2);
142 /* printf("%d %.2f %.2f %.2f\n", junk1, n[0], n[1], n[2]); */
143 nodes[i] = geod_to_cart(n);
144 /* printf("%d %.2f %.2f %.2f\n",
145 junk1, nodes[i].x, nodes[i].y, nodes[i].z); */
150 printf("Loading element file: %s ...\n", elename);
151 if ( (ele = fopen(elename, "r")) == NULL ) {
152 printf("Cannot open file '%s'\n", elename);
156 fscanf(ele, "%d %d %d", &tricount, &junk1, &junk2);
158 if ( tricount > MAX_TRIS - 1 ) {
159 printf("Error, too many elements, need to increase array size\n");
162 printf(" Expecting %d elements\n", tricount);
165 for ( i = 1; i <= tricount; i++ ) {
166 fscanf(ele, "%d %d %d %d\n", &junk1,
167 &tris[i][0], &tris[i][1], &tris[i][2]);
168 /* printf("%d %d %d %d\n", junk1, tris[i][0], tris[i][1], tris[i][2]);*/
175 /* dump in WaveFront .obj format */
176 void dump_obj(char *basename) {
178 double n1[3], n2[3], n3[3], n4[3], n5[3], norm[3], temp;
180 int i, t1, t2, t3, t4, t5, count;
182 strcpy(objname, basename);
183 strcat(objname, ".obj");
185 printf("Dumping to file: %s ...\n", objname);
187 obj = fopen(objname, "w");
190 printf(" writing vertices\n");
191 for ( i = 1; i <= nodecount; i++ ) {
192 fprintf(obj, "v %.2f %.2f %.2f\n",
193 nodes[i].x, nodes[i].y, nodes[i].z);
196 printf(" calculating and writing normals\n");
197 /* calculate and generate normals */
198 for ( i = 1; i <= nodecount; i++ ) {
199 /* printf("Finding normal\n"); */
201 find_tris(i, &t1, &t2, &t3, &t4, &t5);
203 n1[0] = n1[1] = n1[2] = 0.0;
204 n2[0] = n2[1] = n2[2] = 0.0;
205 n3[0] = n3[1] = n3[2] = 0.0;
206 n4[0] = n4[1] = n4[2] = 0.0;
207 n5[0] = n5[1] = n5[2] = 0.0;
210 calc_normal(nodes[tris[t1][0]], nodes[tris[t1][1]], nodes[tris[t1][2]],
214 calc_normal(nodes[tris[t2][0]], nodes[tris[t2][1]],
215 nodes[tris[t2][2]], n2);
220 calc_normal(nodes[tris[t3][0]], nodes[tris[t3][1]],
221 nodes[tris[t3][2]], n3);
226 calc_normal(nodes[tris[t4][0]], nodes[tris[t4][1]],
227 nodes[tris[t4][2]], n4);
232 calc_normal(nodes[tris[t5][0]], nodes[tris[t5][1]],
233 nodes[tris[t5][2]], n5);
237 /* printf(" norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]); */
239 norm[0] = ( n1[0] + n2[0] + n3[0] + n4[0] + n5[0] ) / (double)count;
240 norm[1] = ( n1[1] + n2[1] + n3[1] + n4[1] + n5[1] ) / (double)count;
241 norm[2] = ( n1[2] + n2[2] + n3[2] + n4[2] + n5[2] ) / (double)count;
243 /* printf(" count = %d\n", count); */
244 /* printf(" Ave. normal = %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);*/
245 MAT3_NORMALIZE_VEC(norm, temp);
246 /* printf(" Normalized ave. normal = %.4f %.4f %.4f\n", */
247 /* norm[0], norm[1], norm[2]); */
249 fprintf(obj, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
253 printf(" writing faces\n");
254 for ( i = 1; i <= tricount; i++ ) {
255 fprintf(obj, "f %d %d %d\n", tris[i][0], tris[i][1], tris[i][2]);
261 int main(int argc, char **argv) {
264 strcpy(basename, argv[1]);
266 /* load the input data files */
269 /* dump in WaveFront .obj format */
277 /* Revision 1.7 1998/01/12 02:42:00 curt
278 /* Average up to five triangles per vertex instead of three.
280 * Revision 1.6 1998/01/09 23:03:15 curt
281 * Restructured to split 1deg x 1deg dem's into 64 subsections.
283 * Revision 1.5 1997/12/08 19:17:50 curt
284 * Fixed a type in the normal generation code.
286 * Revision 1.4 1997/12/02 13:13:32 curt
287 * Fixed problem with averaged vertex normals.
289 * Revision 1.3 1997/11/15 18:05:05 curt
292 * Revision 1.2 1997/11/14 00:29:13 curt
293 * Transform scenery coordinates at this point in pipeline when scenery is
294 * being translated to .obj format, not when it is being loaded into the end
295 * renderer. Precalculate normals for each node as average of the normals
296 * of each containing polygon so Garoude shading is now supportable.
298 * Revision 1.1 1997/10/29 23:05:15 curt