2 Copyright (C) 1999-2007 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 ){
61 for ( i = 0 ; i < sizeof( tf_triangle ) / 4 ; i++ )
63 ( (int *)tri )[i] = BigLong( ( (int *)tri )[i] );
67 void LoadTriangleList( char *filename, triangle_t **pptri, int *numtriangles ){
70 char name[256], tex[256];
79 *( (unsigned char *)&exitpattern + 0 ) = *( (unsigned char *)&t + 3 );
80 *( (unsigned char *)&exitpattern + 1 ) = *( (unsigned char *)&t + 2 );
81 *( (unsigned char *)&exitpattern + 2 ) = *( (unsigned char *)&t + 1 );
82 *( (unsigned char *)&exitpattern + 3 ) = *( (unsigned char *)&t + 0 );
84 if ( ( input = fopen( filename, "rb" ) ) == 0 ) {
85 Error( "reader: could not open file '%s'", filename );
90 fread( &magic, sizeof( int ), 1, input );
91 if ( BigLong( magic ) != MAGIC ) {
92 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 ) {
103 *(int *)&start = BigLong( *(int *)&start );
104 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" );
183 *numtriangles = ptri - *pptri;