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