aboutsummaryrefslogtreecommitdiffstats
path: root/common/l3dslib.c
blob: 1499e751b30a329358868ebc48033b9a8881e70f (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.

This file is part of Quake III Arena source code.

Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/
//
// l3dslib.c: library for loading triangles from an Alias triangle file
//

#include <stdio.h>
#include "cmdlib.h"
#include "mathlib.h"
#include "trilib.h"
#include "l3dslib.h"

#define MAIN3DS       0x4D4D
#define EDIT3DS       0x3D3D  // this is the start of the editor config
#define EDIT_OBJECT   0x4000
#define OBJ_TRIMESH   0x4100
#define TRI_VERTEXL   0x4110
#define TRI_FACEL1    0x4120

#define MAXVERTS	2000

typedef struct {
	int	v[4];
} tri;

float	fverts[MAXVERTS][3];
tri		tris[MAXTRIANGLES];

int	bytesread, level, numtris, totaltris;
int	vertsfound, trisfound;

triangle_t	*ptri;


// Alias stores triangles as 3 explicit vertices in .tri files, so even though we
// start out with a vertex pool and vertex indices for triangles, we have to convert
// to raw, explicit triangles
void StoreAliasTriangles (void)
{
	int		i, j, k;

	if ((totaltris + numtris) > MAXTRIANGLES)
		Error ("Error: Too many triangles");

	for (i=0; i<numtris ; i++)
	{
		for (j=0 ; j<3 ; j++)
		{
			for (k=0 ; k<3 ; k++)
			{
				ptri[i+totaltris].verts[j][k] = fverts[tris[i].v[j]][k];
			}
		}
	}
	
	totaltris += numtris;
	numtris = 0;
	vertsfound = 0;
	trisfound = 0;
}


int ParseVertexL (FILE *input)
{
	int				i, j, startbytesread, numverts;
	unsigned short	tshort;

	if (vertsfound)
		Error ("Error: Multiple vertex chunks");

	vertsfound = 1;
	startbytesread = bytesread;

	if (feof(input))
		Error ("Error: unexpected end of file");

	fread(&tshort, sizeof(tshort), 1, input);
	bytesread += sizeof(tshort);
	numverts = (int)tshort;

	if (numverts > MAXVERTS)
		Error ("Error: Too many vertices");

	for (i=0 ; i<numverts ; i++)
	{
		for (j=0 ; j<3 ; j++)
		{
			if (feof(input))
				Error ("Error: unexpected end of file");

			fread(&fverts[i][j], sizeof(float), 1, input);
			bytesread += sizeof(float);
		}
	}

	if (vertsfound && trisfound)
		StoreAliasTriangles ();

	return bytesread - startbytesread;
}


int ParseFaceL1 (FILE *input)
{

	int				i, j, startbytesread;
	unsigned short	tshort;

	if (trisfound)
		Error ("Error: Multiple face chunks");

	trisfound = 1;
	startbytesread = bytesread;

	if (feof(input))
		Error ("Error: unexpected end of file");

	fread(&tshort, sizeof(tshort), 1, input);
	bytesread += sizeof(tshort);
	numtris = (int)tshort;

	if (numtris > MAXTRIANGLES)
		Error ("Error: Too many triangles");

	for (i=0 ; i<numtris ; i++)
	{
		for (j=0 ; j<4 ; j++)
		{
			if (feof(input))
				Error ("Error: unexpected end of file");

			fread(&tshort, sizeof(tshort), 1, input);
			bytesread += sizeof(tshort);
			tris[i].v[j] = (int)tshort;
		}
	}

	if (vertsfound && trisfound)
		StoreAliasTriangles ();

	return bytesread - startbytesread;
}


int ParseChunk (FILE *input)
{
#define BLOCK_SIZE	4096
	char			temp[BLOCK_SIZE];
	unsigned short	type;
	int				i, length, w, t, retval;

	level++;
	retval = 0;

// chunk type
	if (feof(input))
		Error ("Error: unexpected end of file");

	fread(&type, sizeof(type), 1, input);
	bytesread += sizeof(type);

// chunk length
	if (feof(input))
		Error ("Error: unexpected end of file");

	fread (&length, sizeof(length), 1, input);
	bytesread += sizeof(length);
	w = length - 6;

// process chunk if we care about it, otherwise skip it
	switch (type)
	{
	case TRI_VERTEXL:
		w -= ParseVertexL (input);
		goto ParseSubchunk;

	case TRI_FACEL1:
		w -= ParseFaceL1 (input);
		goto ParseSubchunk;

	case EDIT_OBJECT:
	// read the name
		i = 0;

		do
		{
			if (feof(input))
				Error ("Error: unexpected end of file");

			fread (&temp[i], 1, 1, input);
			i++;
			w--;
			bytesread++;
		} while (temp[i-1]);

	case MAIN3DS:
	case OBJ_TRIMESH:
	case EDIT3DS:
	// parse through subchunks
ParseSubchunk:
		while (w > 0)
		{
			w -= ParseChunk (input);
		}

		retval = length;
		goto Done;

	default:
	// skip other chunks
		while (w > 0)
		{
			t = w;

			if (t > BLOCK_SIZE)
				t = BLOCK_SIZE;

			if (feof(input))
				Error ("Error: unexpected end of file");

			fread (&temp, t, 1, input);
			bytesread += t;

			w -= t;
		}

		retval = length;
		goto Done;
	}

Done:
	level--;
	return retval;
}


void Load3DSTriangleList (char *filename, triangle_t **pptri, int *numtriangles)
{
	FILE        *input;
	short int	tshort;

	bytesread = 0;
	level = 0;
	numtris = 0;
	totaltris = 0;
	vertsfound = 0;
	trisfound = 0;

	if ((input = fopen(filename, "rb")) == 0) {
		fprintf(stderr,"reader: could not open file '%s'\n", filename);
		exit(0);
	}

	fread(&tshort, sizeof(tshort), 1, input);

// should only be MAIN3DS, but some files seem to start with EDIT3DS, with
// no MAIN3DS
	if ((tshort != MAIN3DS) && (tshort != EDIT3DS)) {
		fprintf(stderr,"File is not a 3DS file.\n");
		exit(0);
	}

// back to top of file so we can parse the first chunk descriptor
	fseek(input, 0, SEEK_SET);

	ptri = malloc (MAXTRIANGLES * sizeof(triangle_t));

	*pptri = ptri;

// parse through looking for the relevant chunk tree (MAIN3DS | EDIT3DS | EDIT_OBJECT |
// OBJ_TRIMESH | {TRI_VERTEXL, TRI_FACEL1}) and skipping other chunks
	ParseChunk (input);

	if (vertsfound || trisfound)
		Error ("Incomplete triangle set");

	*numtriangles = totaltris;

	fclose (input);
}