| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include "cmdlib.h" |
| 27 |
#include "inout.h" |
| 28 |
#include "mathlib.h" |
| 29 |
#include "trilib.h" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#define FLOAT_START 99999.0 |
| 35 |
#define FLOAT_END -FLOAT_START |
| 36 |
#define MAGIC 123322 |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
typedef struct { |
| 41 |
float v[3]; |
| 42 |
} vector; |
| 43 |
|
| 44 |
typedef struct |
| 45 |
{ |
| 46 |
vector n; |
| 47 |
vector p; |
| 48 |
vector c; |
| 49 |
float u; |
| 50 |
float v; |
| 51 |
} aliaspoint_t; |
| 52 |
|
| 53 |
typedef struct { |
| 54 |
aliaspoint_t pt[3]; |
| 55 |
} tf_triangle; |
| 56 |
|
| 57 |
|
| 58 |
void ByteSwapTri( tf_triangle *tri ){ |
| 59 |
int i; |
| 60 |
|
| 61 |
for ( i = 0 ; i < sizeof( tf_triangle ) / 4 ; i++ ) |
| 62 |
{ |
| 63 |
( (int *)tri )[i] = BigLong( ( (int *)tri )[i] ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
void LoadTriangleList( char *filename, triangle_t **pptri, int *numtriangles ){ |
| 68 |
FILE *input; |
| 69 |
float start; |
| 70 |
char name[256], tex[256]; |
| 71 |
int i, count, magic; |
| 72 |
tf_triangle tri; |
| 73 |
triangle_t *ptri; |
| 74 |
int iLevel; |
| 75 |
int exitpattern; |
| 76 |
float t; |
| 77 |
|
| 78 |
t = -FLOAT_START; |
| 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 ); |
| 83 |
|
| 84 |
if ( ( input = fopen( filename, "rb" ) ) == 0 ) { |
| 85 |
Error( "reader: could not open file '%s'", filename ); |
| 86 |
} |
| 87 |
|
| 88 |
iLevel = 0; |
| 89 |
|
| 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 ); |
| 93 |
} |
| 94 |
|
| 95 |
ptri = malloc( MAXTRIANGLES * sizeof( triangle_t ) ); |
| 96 |
|
| 97 |
*pptri = ptri; |
| 98 |
|
| 99 |
while ( feof( input ) == 0 ) { |
| 100 |
if ( fread( &start, sizeof( float ), 1, input ) < 1 ) { |
| 101 |
break; |
| 102 |
} |
| 103 |
*(int *)&start = BigLong( *(int *)&start ); |
| 104 |
if ( *(int *)&start != exitpattern ) { |
| 105 |
if ( start == FLOAT_START ) { |
| 106 |
|
| 107 |
i = -1; |
| 108 |
do { |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
++i; |
| 113 |
fread( &( name[i] ), sizeof( char ), 1, input ); |
| 114 |
} while ( name[i] != '\0' ); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
fread( &count, sizeof( int ), 1, input ); |
| 119 |
count = BigLong( count ); |
| 120 |
++iLevel; |
| 121 |
if ( count != 0 ) { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
i = -1; |
| 126 |
do { |
| 127 |
++i; |
| 128 |
fread( &( tex[i] ), sizeof( char ), 1, input ); |
| 129 |
} while ( tex[i] != '\0' ); |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
} |
| 138 |
else if ( start == FLOAT_END ) { |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
--iLevel; |
| 145 |
i = -1; |
| 146 |
do { |
| 147 |
++i; |
| 148 |
fread( &( name[i] ), sizeof( char ), 1, input ); |
| 149 |
} while ( name[i] != '\0' ); |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
continue; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
for ( i = 0; i < count; ++i ) { |
| 161 |
int j; |
| 162 |
|
| 163 |
fread( &tri, sizeof( tf_triangle ), 1, input ); |
| 164 |
ByteSwapTri( &tri ); |
| 165 |
for ( j = 0 ; j < 3 ; j++ ) |
| 166 |
{ |
| 167 |
int k; |
| 168 |
|
| 169 |
for ( k = 0 ; k < 3 ; k++ ) |
| 170 |
{ |
| 171 |
ptri->verts[j][k] = tri.pt[j].p.v[k]; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
ptri++; |
| 176 |
|
| 177 |
if ( ( ptri - *pptri ) >= MAXTRIANGLES ) { |
| 178 |
Error( "Error: too many triangles; increase MAXTRIANGLES\n" ); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
*numtriangles = ptri - *pptri; |
| 184 |
|
| 185 |
fclose( input ); |
| 186 |
} |