aboutsummaryrefslogtreecommitdiff
path: root/src/obj2model.c
blob: 1e160ff426285d1212cb14d4559d5e08e2315e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * obj2model.c
 *
 * Turn Wavefront OBJ files into Thrust3D models
 *
 * (c) 2008 Thomas White <taw27@cam.ac.uk>
 *
 *  thrust3d - a silly game
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "utils.c"

#define MAX_VERTICES 65536

int main(int argc, char *argv[]) {

	FILE *fh;
	FILE *out;
	float vtmp[3*MAX_VERTICES];
	float vntmp[3*MAX_VERTICES];
	int n_vtmp, n_vntmp;
	int nprev;
	
	fh = fopen(argv[1], "r");
	if ( fh == NULL ) {
		fprintf(stderr, "Couldn't open '%s'\n", argv[1]);
		return 1;
	}
	
	/* Zip through and find all the vertices */
	n_vtmp = 0;
	n_vntmp = 0;
	while ( !feof(fh) ) {
		
		char line[1024];
		float x, y, z;
		size_t s;
		
		fgets(line, 1023, fh);
		s = 0;
		for ( ; s<strlen(line); s++ ) {
			if ( line[s] != ' ' ) break;
		}
			
		if ( line[s] == '#' ) {
			continue;
		}
		
		if ( sscanf(line+s, "v %f %f %f\n", &x, &y, &z) == 3 ) {
			vtmp[3*n_vtmp+0] = x;
			vtmp[3*n_vtmp+1] = y;
			vtmp[3*n_vtmp+2] = z;
			n_vtmp++;
			continue;
		}
		
		if ( sscanf(line+s, "vn %f %f %f\n", &x, &y, &z) == 3 ) {
			vntmp[3*n_vntmp+0] = x;
			vntmp[3*n_vntmp+1] = y;
			vntmp[3*n_vntmp+2] = z;
			n_vntmp++;
			continue;
		}
	
	}
	
	/* Go through again and look for faces */
	rewind(fh);
	out = fopen(argv[2], "w");
	fprintf(out, "# %s\n", argv[1]);
	fprintf(out, "\n");
	nprev = 0;
	while ( !feof(fh) ) {
	
		char line[1024];
		char **bits;
		int n, i;
		
		fgets(line, 1023, fh);
		n = assplode(line, " \t\r\n", &bits, ASSPLODE_NONE);
		
		/* Read in a face */
		if ( strcmp(bits[0], "f") == 0 ) {
		
			int nthis;
			
			nthis = n-1;
			if ( nthis != nprev ) {
				if ( nprev != 0 ) fprintf(out, "\n");
				if ( nthis == 4 ) {
					fprintf(out, "QUADS\n");
				} else {
					fprintf(out, "POLYGONS\n");
				}
				nprev = nthis;
			} else {
				fprintf(out, "#\n");
			}
			
			for ( i=1; i<n; i++ ) {
			
				char **sp;
				int np, nnum, j, nslash;
				
				nslash = 0;
				for ( j=0; j<strlen(bits[i]); j++ ) {
					if ( bits[i][j] == '/' ) nslash++;
				}
				if ( nslash == 2 ) {
				
					int vnum;
				
					np = assplode(bits[i], "/", &sp, ASSPLODE_DUPS);
					if ( np != 3 ) {
						continue;
					}
					vnum = atoi(sp[0])-1;
					nnum = atoi(sp[2])-1;
					if ( vnum >= n_vtmp ) {
						fprintf(stderr, "Vertex index is too high (%i/%i)\n", vnum, n_vtmp);
						continue;
					}
					if ( nnum >= n_vntmp ) {
						fprintf(stderr, "Normal index is too high (%i/%i)\n", nnum, n_vntmp);
						continue;
					}
					fprintf(out, "%+8.3f %+8.3f %+8.3f\n", vtmp[3*vnum+0], vtmp[3*vnum+1],
										vtmp[3*vnum+2]);
					free(sp[0]);
					free(sp[1]);
					free(sp[2]);
					free(sp);
				
				} else if ( nslash == 0 ) {
					
					int vnum;
					vnum = atoi(bits[i]);
					fprintf(out, "%+8.3f %+8.3f %+8.3f\n", vtmp[3*vnum+0], vtmp[3*vnum+1],
										vtmp[3*vnum+2]);
				
				}
				
			}
			
		}
		
		for ( i=0; i<n; i++ ) free(bits[i]);
		free(bits);
		
	}
	fprintf(out, "\n");
	
	fclose(fh);
	fclose(out);
	
	return 0;
	
}