2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
23 // trilib.c: library for loading triangles from an Alias triangle file
\r
28 #include "mathlib.h"
\r
29 #include "polyset.h"
\r
32 // on disk representation of a face
\r
35 #define FLOAT_START 99999.0
\r
36 #define FLOAT_END -FLOAT_START
\r
37 #define MAGIC 123322
\r
41 #if defined (__linux__) || defined (__APPLE__)
\r
42 #define strlwr strlower
\r
51 vector n; /* normal */
\r
52 vector p; /* point */
\r
53 vector c; /* color */
\r
63 static void ByteSwapTri (tf_triangle *tri)
\r
67 for (i=0 ; i<sizeof(tf_triangle)/4 ; i++)
\r
69 ((int *)tri)[i] = BigLong (((int *)tri)[i]);
\r
73 static void ReadPolysetGeometry( triangle_t *tripool, FILE *input, int count, triangle_t *ptri )
\r
78 for (i = 0; i < count; ++i) {
\r
81 fread( &tri, sizeof(tf_triangle), 1, input );
\r
83 for (j=0 ; j<3 ; j++)
\r
87 for (k=0 ; k<3 ; k++)
\r
89 ptri->verts[j][k] = tri.pt[j].p.v[k];
\r
90 ptri->normals[j][k] = tri.pt[j].n.v[k];
\r
91 // ptri->colors[j][k] = tri.pt[j].c.v[k];
\r
94 ptri->texcoords[j][0] = tri.pt[j].u;
\r
95 ptri->texcoords[j][1] = tri.pt[j].v;
\r
99 if ((ptri - tripool ) >= POLYSET_MAXTRIANGLES)
\r
100 Error ("Error: too many triangles; increase POLYSET_MAXTRIANGLES\n");
\r
104 void TRI_LoadPolysets( const char *filename, polyset_t **ppPSET, int *numpsets )
\r
108 char name[256], tex[256];
\r
109 int i, count, magic, pset = 0;
\r
117 *((unsigned char *)&exitpattern + 0) = *((unsigned char *)&t + 3);
\r
118 *((unsigned char *)&exitpattern + 1) = *((unsigned char *)&t + 2);
\r
119 *((unsigned char *)&exitpattern + 2) = *((unsigned char *)&t + 1);
\r
120 *((unsigned char *)&exitpattern + 3) = *((unsigned char *)&t + 0);
\r
122 if ((input = fopen(filename, "rb")) == 0)
\r
123 Error ("reader: could not open file '%s'", filename);
\r
127 fread(&magic, sizeof(int), 1, input);
\r
128 if (BigLong(magic) != MAGIC)
\r
129 Error ("%s is not a Alias object separated triangle file, magic number is wrong.", filename);
\r
131 pPSET = calloc( 1, POLYSET_MAXPOLYSETS * sizeof( polyset_t ) );
\r
132 ptri = calloc( 1, POLYSET_MAXTRIANGLES * sizeof( triangle_t ) );
\r
136 while (feof(input) == 0) {
\r
137 if (fread(&start, sizeof(float), 1, input) < 1)
\r
139 *(int *)&start = BigLong(*(int *)&start);
\r
140 if (*(int *)&start != exitpattern)
\r
142 if (start == FLOAT_START) {
\r
143 /* Start of an object or group of objects. */
\r
146 /* There are probably better ways to read a string from */
\r
147 /* a file, but this does allow you to do error checking */
\r
148 /* (which I'm not doing) on a per character basis. */
\r
150 fread( &(name[i]), sizeof( char ), 1, input);
\r
151 } while( name[i] != '\0' );
\r
154 strncpy( pPSET[pset].name, name, sizeof( pPSET[pset].name ) - 1 );
\r
156 strcpy( pPSET[pset].name , "(unnamed)" );
\r
157 strlwr( pPSET[pset].name );
\r
160 // fprintf(stdout,"OBJECT START: %s\n",name);
\r
161 fread( &count, sizeof(int), 1, input);
\r
162 count = BigLong(count);
\r
166 // fprintf(stdout,"NUMBER OF TRIANGLES: %d\n",count);
\r
171 fread( &(tex[i]), sizeof( char ), 1, input);
\r
172 } while( tex[i] != '\0' );
\r
176 strncpy( pPSET[pset].texname, tex, sizeof( pPSET[pset].texname ) - 1 );
\r
178 strcpy( pPSET[pset].texname, "(unnamed)" );
\r
179 strlwr( pPSET[pset].texname );
\r
183 // fprintf(stdout," Object texture name: '%s'\n",tex);
\r
186 /* Else (count == 0) this is the start of a group, and */
\r
187 /* no texture name is present. */
\r
189 else if (start == FLOAT_END) {
\r
190 /* End of an object or group. Yes, the name should be */
\r
191 /* obvious from context, but it is in here just to be */
\r
192 /* safe and to provide a little extra information for */
\r
193 /* those who do not wish to write a recursive reader. */
\r
199 fread( &(name[i]), sizeof( char ), 1, input);
\r
200 } while( name[i] != '\0' );
\r
203 strncpy( pPSET[pset].name, name, sizeof( pPSET[pset].name ) - 1 );
\r
205 strcpy( pPSET[pset].name , "(unnamed)" );
\r
207 strlwr( pPSET[pset].name );
\r
210 // fprintf(stdout,"OBJECT END: %s\n",name);
\r
216 // read the triangles
\r
220 pPSET[pset].triangles = ptri;
\r
221 ReadPolysetGeometry( pPSET[0].triangles, input, count, ptri );
\r
223 pPSET[pset].numtriangles = count;
\r
224 if ( ++pset >= POLYSET_MAXPOLYSETS )
\r
226 Error ("Error: too many polysets; increase POLYSET_MAXPOLYSETS\n");
\r