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
22 // trilib.c: library for loading triangles from an Alias triangle file
\r
28 #include "mathlib.h"
\r
31 // on disk representation of a face
\r
34 #define FLOAT_START 99999.0
\r
35 #define FLOAT_END -FLOAT_START
\r
36 #define MAGIC 123322
\r
46 vector n; /* normal */
\r
47 vector p; /* point */
\r
48 vector c; /* color */
\r
58 void ByteSwapTri (tf_triangle *tri)
\r
62 for (i=0 ; i<sizeof(tf_triangle)/4 ; i++)
\r
64 ((int *)tri)[i] = BigLong (((int *)tri)[i]);
\r
68 void LoadTriangleList (char *filename, triangle_t **pptri, int *numtriangles)
\r
72 char name[256], tex[256];
\r
73 int i, count, magic;
\r
81 *((unsigned char *)&exitpattern + 0) = *((unsigned char *)&t + 3);
\r
82 *((unsigned char *)&exitpattern + 1) = *((unsigned char *)&t + 2);
\r
83 *((unsigned char *)&exitpattern + 2) = *((unsigned char *)&t + 1);
\r
84 *((unsigned char *)&exitpattern + 3) = *((unsigned char *)&t + 0);
\r
86 if ((input = fopen(filename, "rb")) == 0)
\r
87 Error ("reader: could not open file '%s'", filename);
\r
91 fread(&magic, sizeof(int), 1, input);
\r
92 if (BigLong(magic) != MAGIC)
\r
93 Error ("%s is not a Alias object separated triangle file, magic number is wrong.", filename);
\r
95 ptri = malloc (MAXTRIANGLES * sizeof(triangle_t));
\r
99 while (feof(input) == 0) {
\r
100 if (fread(&start, sizeof(float), 1, input) < 1)
\r
102 *(int *)&start = BigLong(*(int *)&start);
\r
103 if (*(int *)&start != exitpattern)
\r
105 if (start == FLOAT_START) {
\r
106 /* Start of an object or group of objects. */
\r
109 /* There are probably better ways to read a string from */
\r
110 /* a file, but this does allow you to do error checking */
\r
111 /* (which I'm not doing) on a per character basis. */
\r
113 fread( &(name[i]), sizeof( char ), 1, input);
\r
114 } while( name[i] != '\0' );
\r
117 // fprintf(stdout,"OBJECT START: %s\n",name);
\r
118 fread( &count, sizeof(int), 1, input);
\r
119 count = BigLong(count);
\r
123 // fprintf(stdout,"NUMBER OF TRIANGLES: %d\n",count);
\r
128 fread( &(tex[i]), sizeof( char ), 1, input);
\r
129 } while( tex[i] != '\0' );
\r
132 // fprintf(stdout," Object texture name: '%s'\n",tex);
\r
135 /* Else (count == 0) this is the start of a group, and */
\r
136 /* no texture name is present. */
\r
138 else if (start == FLOAT_END) {
\r
139 /* End of an object or group. Yes, the name should be */
\r
140 /* obvious from context, but it is in here just to be */
\r
141 /* safe and to provide a little extra information for */
\r
142 /* those who do not wish to write a recursive reader. */
\r
148 fread( &(name[i]), sizeof( char ), 1, input);
\r
149 } while( name[i] != '\0' );
\r
152 // fprintf(stdout,"OBJECT END: %s\n",name);
\r
158 // read the triangles
\r
160 for (i = 0; i < count; ++i) {
\r
163 fread( &tri, sizeof(tf_triangle), 1, input );
\r
164 ByteSwapTri (&tri);
\r
165 for (j=0 ; j<3 ; j++)
\r
169 for (k=0 ; k<3 ; k++)
\r
171 ptri->verts[j][k] = tri.pt[j].p.v[k];
\r
177 if ((ptri - *pptri) >= MAXTRIANGLES)
\r
178 Error ("Error: too many triangles; increase MAXTRIANGLES\n");
\r
182 *numtriangles = ptri - *pptri;
\r