2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 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 GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 // trilib.c: library for loading triangles from an Alias triangle file
31 // on disk representation of a face
34 #define FLOAT_START 99999.0
35 #define FLOAT_END -FLOAT_START
46 vector n; /* normal */
58 void ByteSwapTri (tf_triangle *tri)
62 for (i=0 ; i<sizeof(tf_triangle)/4 ; i++)
64 ((int *)tri)[i] = BigLong (((int *)tri)[i]);
68 void LoadTriangleList (char *filename, triangle_t **pptri, int *numtriangles)
72 char name[256], tex[256];
81 *((unsigned char *)&exitpattern + 0) = *((unsigned char *)&t + 3);
82 *((unsigned char *)&exitpattern + 1) = *((unsigned char *)&t + 2);
83 *((unsigned char *)&exitpattern + 2) = *((unsigned char *)&t + 1);
84 *((unsigned char *)&exitpattern + 3) = *((unsigned char *)&t + 0);
86 if ((input = fopen(filename, "rb")) == 0)
87 Error ("reader: could not open file '%s'", filename);
91 fread(&magic, sizeof(int), 1, input);
92 if (BigLong(magic) != MAGIC)
93 Error ("%s is not a Alias object separated triangle file, magic number is wrong.", filename);
95 ptri = malloc (MAXTRIANGLES * sizeof(triangle_t));
99 while (feof(input) == 0) {
100 if (fread(&start, sizeof(float), 1, input) < 1)
102 *(int *)&start = BigLong(*(int *)&start);
103 if (*(int *)&start != exitpattern)
105 if (start == FLOAT_START) {
106 /* Start of an object or group of objects. */
109 /* There are probably better ways to read a string from */
110 /* a file, but this does allow you to do error checking */
111 /* (which I'm not doing) on a per character basis. */
113 fread( &(name[i]), sizeof( char ), 1, input);
114 } while( name[i] != '\0' );
117 // fprintf(stdout,"OBJECT START: %s\n",name);
118 fread( &count, sizeof(int), 1, input);
119 count = BigLong(count);
123 // fprintf(stdout,"NUMBER OF TRIANGLES: %d\n",count);
128 fread( &(tex[i]), sizeof( char ), 1, input);
129 } while( tex[i] != '\0' );
132 // fprintf(stdout," Object texture name: '%s'\n",tex);
135 /* Else (count == 0) this is the start of a group, and */
136 /* no texture name is present. */
138 else if (start == FLOAT_END) {
139 /* End of an object or group. Yes, the name should be */
140 /* obvious from context, but it is in here just to be */
141 /* safe and to provide a little extra information for */
142 /* those who do not wish to write a recursive reader. */
148 fread( &(name[i]), sizeof( char ), 1, input);
149 } while( name[i] != '\0' );
152 // fprintf(stdout,"OBJECT END: %s\n",name);
158 // read the triangles
160 for (i = 0; i < count; ++i) {
163 fread( &tri, sizeof(tf_triangle), 1, input );
165 for (j=0 ; j<3 ; j++)
169 for (k=0 ; k<3 ; k++)
171 ptri->verts[j][k] = tri.pt[j].p.v[k];
177 if ((ptri - *pptri) >= MAXTRIANGLES)
178 Error ("Error: too many triangles; increase MAXTRIANGLES\n");
182 *numtriangles = ptri - *pptri;