2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
5 This file is part of Quake 2 Tools source code.
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
24 // trilib.c: library for loading triangles from an Alias triangle file
32 // on disk representation of a face
35 #define FLOAT_START 99999.0
36 #define FLOAT_END -FLOAT_START
47 vector n; /* normal */
59 void ByteSwapTri (tf_triangle *tri)
63 for (i=0 ; i<sizeof(tf_triangle)/4 ; i++)
65 ((int *)tri)[i] = BigLong (((int *)tri)[i]);
69 void LoadTriangleList (char *filename, triangle_t **pptri, int *numtriangles)
73 char name[256], tex[256];
82 *((unsigned char *)&exitpattern + 0) = *((unsigned char *)&t + 3);
83 *((unsigned char *)&exitpattern + 1) = *((unsigned char *)&t + 2);
84 *((unsigned char *)&exitpattern + 2) = *((unsigned char *)&t + 1);
85 *((unsigned char *)&exitpattern + 3) = *((unsigned char *)&t + 0);
87 if ((input = fopen(filename, "rb")) == 0)
88 Error ("reader: could not open file '%s'", filename);
92 fread(&magic, sizeof(int), 1, input);
93 if (BigLong(magic) != MAGIC)
94 Error ("%s is not a Alias object separated triangle file, magic number is wrong.", filename);
96 ptri = malloc (MAXTRIANGLES * sizeof(triangle_t));
100 while (feof(input) == 0) {
101 if (fread(&start, sizeof(float), 1, input) < 1)
103 *(int *)&start = BigLong(*(int *)&start);
104 if (*(int *)&start != exitpattern)
106 if (start == FLOAT_START) {
107 /* Start of an object or group of objects. */
110 /* There are probably better ways to read a string from */
111 /* a file, but this does allow you to do error checking */
112 /* (which I'm not doing) on a per character basis. */
114 fread( &(name[i]), sizeof( char ), 1, input);
115 } while( name[i] != '\0' );
118 // fprintf(stdout,"OBJECT START: %s\n",name);
119 fread( &count, sizeof(int), 1, input);
120 count = BigLong(count);
124 // fprintf(stdout,"NUMBER OF TRIANGLES: %d\n",count);
129 fread( &(tex[i]), sizeof( char ), 1, input);
130 } while( tex[i] != '\0' );
133 // fprintf(stdout," Object texture name: '%s'\n",tex);
136 /* Else (count == 0) this is the start of a group, and */
137 /* no texture name is present. */
139 else if (start == FLOAT_END) {
140 /* End of an object or group. Yes, the name should be */
141 /* obvious from context, but it is in here just to be */
142 /* safe and to provide a little extra information for */
143 /* those who do not wish to write a recursive reader. */
149 fread( &(name[i]), sizeof( char ), 1, input);
150 } while( name[i] != '\0' );
153 // fprintf(stdout,"OBJECT END: %s\n",name);
159 // read the triangles
161 for (i = 0; i < count; ++i) {
164 fread( &tri, sizeof(tf_triangle), 1, input );
166 for (j=0 ; j<3 ; j++)
170 for (k=0 ; k<3 ; k++)
172 ptri->verts[j][k] = tri.pt[j].p.v[k];
178 if ((ptri - *pptri) >= MAXTRIANGLES)
179 Error ("Error: too many triangles; increase MAXTRIANGLES\n");
183 *numtriangles = ptri - *pptri;