1 /*
2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
4 
5 This file is part of Quake 2 Tools source code.
6 
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11 
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, 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.
16 
17 You should have received a copy of the GNU General Public License
18 along with Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 ===========================================================================
21 */
22 
23 //
24 // trilib.c: library for loading triangles from an Alias triangle file
25 //
26 
27 #include <stdio.h>
28 #include "cmdlib.h"
29 #include "mathlib.h"
30 #include "trilib.h"
31 
32 // on disk representation of a face
33 
34 
35 #define	FLOAT_START	99999.0
36 #define	FLOAT_END	-FLOAT_START
37 #define MAGIC       123322
38 
39 //#define NOISY 1
40 
41 typedef struct {
42 	float v[3];
43 } vector;
44 
45 typedef struct
46 {
47 	vector n;    /* normal */
48 	vector p;    /* point */
49 	vector c;    /* color */
50 	float  u;    /* u */
51 	float  v;    /* v */
52 } aliaspoint_t;
53 
54 typedef struct {
55 	aliaspoint_t	pt[3];
56 } tf_triangle;
57 
58 
ByteSwapTri(tf_triangle * tri)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 
LoadTriangleList(char * filename,triangle_t ** pptri,int * numtriangles)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 				/* Start of an object or group of objects. */
108 				i = -1;
109 				do {
110 					/* There are probably better ways to read a string from */
111 					/* a file, but this does allow you to do error checking */
112 					/* (which I'm not doing) on a per character basis.      */
113 					++i;
114 					fread( &(name[i]), sizeof( char ), 1, input);
115 				} while( name[i] != '\0' );
116 
117 //				indent();
118 //				fprintf(stdout,"OBJECT START: %s\n",name);
119 				fread( &count, sizeof(int), 1, input);
120 				count = BigLong(count);
121 				++iLevel;
122 				if (count != 0) {
123 //					indent();
124 //					fprintf(stdout,"NUMBER OF TRIANGLES: %d\n",count);
125 
126 					i = -1;
127 					do {
128 						++i;
129 						fread( &(tex[i]), sizeof( char ), 1, input);
130 					} while( tex[i] != '\0' );
131 
132 //					indent();
133 //					fprintf(stdout,"  Object texture name: '%s'\n",tex);
134 				}
135 
136 				/* Else (count == 0) this is the start of a group, and */
137 				/* no texture name is present. */
138 			}
139 			else if (start == FLOAT_END) {
140 				/* End of an object or group. Yes, the name should be */
141 				/* obvious from context, but it is in here just to be */
142 				/* safe and to provide a little extra information for */
143 				/* those who do not wish to write a recursive reader. */
144 				/* Mia culpa. */
145 				--iLevel;
146 				i = -1;
147 				do {
148 					++i;
149 					fread( &(name[i]), sizeof( char ), 1, input);
150 				} while( name[i] != '\0' );
151 
152 //				indent();
153 //				fprintf(stdout,"OBJECT END: %s\n",name);
154 				continue;
155 			}
156 		}
157 
158 //
159 // read the triangles
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 
188