]> git.mxchange.org Git - flightgear.git/blob - Tri2obj/tri2obj.c
Initial revision.
[flightgear.git] / Tri2obj / tri2obj.c
1 /* tri2obj.c -- read in a .ele/.node file pair generated by the triangle 
2  *              program and output a simple Wavefront .obj file.
3  *
4  * Written by Curtis Olson, started October 1997.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7  *
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.
12  *
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.
17  *
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.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  */
25
26
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "tri2obj.h"
31
32
33 int nodecount, tricount;
34 double nodes[MAX_NODES][3];
35 int tris[MAX_TRIS][3];
36 int new_tris[MAX_TRIS][3];
37
38
39 /* Initialize a new mesh structure */
40 void triload(char *basename) {
41     char nodename[256], elename[256];
42     FILE *node, *ele;
43     int dim, junk1, junk2;
44     int i;
45
46     strcpy(nodename, basename);
47     strcat(nodename, ".node");
48     strcpy(elename, basename);
49     strcat(elename, ".ele");
50
51     printf("Loading node file:  %s ...\n", nodename);
52     if ( (node = fopen(nodename, "r")) == NULL ) {
53         printf("Cannot open file '%s'\n", nodename);
54         exit(-1);
55     }
56
57     fscanf(node, "%d %d %d %d", &nodecount, &dim, &junk1, &junk2);
58
59     if ( nodecount > MAX_NODES - 1 ) {
60         printf("Error, too many nodes, need to increase array size\n");
61         exit(-1);
62     } else {
63         printf("    Expecting %d nodes\n", nodecount);
64     }
65
66     for ( i = 1; i <= nodecount; i++ ) {
67         fscanf(node, "%d %lf %lf %lf %d\n", &junk1, 
68                &nodes[i][0], &nodes[i][1], &nodes[i][2], &junk2);
69         /* printf("%d %.2f %.2f %.2f\n", 
70                junk1, nodes[i][0], nodes[i][1], nodes[i][2]); */
71     }
72
73     fclose(node);
74
75     printf("Loading element file:  %s ...\n", elename);
76     if ( (ele = fopen(elename, "r")) == NULL ) {
77         printf("Cannot open file '%s'\n", elename);
78         exit(-1);
79     }
80
81     fscanf(ele, "%d %d %d", &tricount, &junk1, &junk2);
82
83     if ( tricount > MAX_TRIS - 1 ) {
84         printf("Error, too many elements, need to increase array size\n");
85         exit(-1);
86     } else {
87         printf("    Expecting %d elements\n", tricount);
88     }
89
90     for ( i = 1; i <= tricount; i++ ) {
91         fscanf(ele, "%d %d %d %d\n", &junk1, 
92                &tris[i][0], &tris[i][1], &tris[i][2]);
93         /* printf("%d %d %d %d\n", junk1, tris[i][0], tris[i][1], tris[i][2]);*/
94     }
95
96     fclose(ele);
97 }
98
99
100 /* dump in WaveFront .obj format */
101 void dump_obj(char *basename) {
102     char objname[256];
103     FILE *obj;
104     int i;
105
106     strcpy(objname, basename);
107     strcat(objname, ".obj");
108
109     printf("Dumping to file:  %s ...\n", objname);
110
111     obj = fopen(objname, "w");
112
113     /* dump vertices */
114     for ( i = 1; i <= nodecount; i++ ) {
115         fprintf(obj, "v %.2f %.2f %.2f\n", 
116                 nodes[i][0], nodes[i][1], nodes[i][2]);
117     }
118
119     /* dump faces */
120     for ( i = 1; i <= tricount; i++ ) {
121         fprintf(obj, "f %d %d %d\n", tris[i][0], tris[i][1], tris[i][2]);
122     }
123
124     fclose(obj);
125 }
126
127 int main(int argc, char **argv) {
128     char basename[256];
129
130     strcpy(basename, argv[1]);
131
132     /* load the input data files */
133     triload(basename);
134
135     /* dump in WaveFront .obj format */
136     dump_obj(basename);
137
138     return(0);
139 }
140
141
142 /* $Log$
143 /* Revision 1.1  1997/10/29 23:05:15  curt
144 /* Initial revision.
145 /*
146  */