]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/common/trilib.c
eol style
[xonotic/netradiant.git] / tools / quake2 / common / trilib.c
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
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.
11
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.
16
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
20 */
21 //
22 // trilib.c: library for loading triangles from an Alias triangle file
23 //
24
25 #include <stdio.h>
26 #include "cmdlib.h"
27 #include "inout.h"
28 #include "mathlib.h"
29 #include "trilib.h"
30
31 // on disk representation of a face
32
33
34 #define FLOAT_START     99999.0
35 #define FLOAT_END       -FLOAT_START
36 #define MAGIC       123322
37
38 //#define NOISY 1
39
40 typedef struct {
41         float v[3];
42 } vector;
43
44 typedef struct
45 {
46         vector n;    /* normal */
47         vector p;    /* point */
48         vector c;    /* color */
49         float  u;    /* u */
50         float  v;    /* 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 {
60         int             i;
61         
62         for (i=0 ; i<sizeof(tf_triangle)/4 ; i++)
63         {
64                 ((int *)tri)[i] = BigLong (((int *)tri)[i]);
65         }
66 }
67
68 void LoadTriangleList (char *filename, triangle_t **pptri, int *numtriangles)
69 {
70         FILE        *input;
71         float       start;
72         char        name[256], tex[256];
73         int         i, count, magic;
74         tf_triangle     tri;
75         triangle_t      *ptri;
76         int                     iLevel;
77         int                     exitpattern;
78         float           t;
79
80         t = -FLOAT_START;
81         *((unsigned char *)&exitpattern + 0) = *((unsigned char *)&t + 3);
82         *((unsigned char *)&exitpattern + 1) = *((unsigned char *)&t + 2);
83         *((unsigned char *)&exitpattern + 2) = *((unsigned char *)&t + 1);
84         *((unsigned char *)&exitpattern + 3) = *((unsigned char *)&t + 0);
85
86         if ((input = fopen(filename, "rb")) == 0)
87                 Error ("reader: could not open file '%s'", filename);
88
89         iLevel = 0;
90
91         fread(&magic, sizeof(int), 1, input);
92         if (BigLong(magic) != MAGIC)
93                 Error ("%s is not a Alias object separated triangle file, magic number is wrong.", filename);
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                 *(int *)&start = BigLong(*(int *)&start);
103                 if (*(int *)&start != exitpattern)
104                 {
105                         if (start == FLOAT_START) {
106                                 /* Start of an object or group of objects. */
107                                 i = -1;
108                                 do {
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.      */
112                                         ++i;
113                                         fread( &(name[i]), sizeof( char ), 1, input);
114                                 } while( name[i] != '\0' );
115         
116 //                              indent();
117 //                              fprintf(stdout,"OBJECT START: %s\n",name);
118                                 fread( &count, sizeof(int), 1, input);
119                                 count = BigLong(count);
120                                 ++iLevel;
121                                 if (count != 0) {
122 //                                      indent();
123 //                                      fprintf(stdout,"NUMBER OF TRIANGLES: %d\n",count);
124         
125                                         i = -1;
126                                         do {
127                                                 ++i;
128                                                 fread( &(tex[i]), sizeof( char ), 1, input);
129                                         } while( tex[i] != '\0' );
130         
131 //                                      indent();
132 //                                      fprintf(stdout,"  Object texture name: '%s'\n",tex);
133                                 }
134         
135                                 /* Else (count == 0) this is the start of a group, and */
136                                 /* no texture name is present. */
137                         }
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. */
143                                 /* Mia culpa. */
144                                 --iLevel;
145                                 i = -1;
146                                 do {
147                                         ++i;
148                                         fread( &(name[i]), sizeof( char ), 1, input);
149                                 } while( name[i] != '\0' );
150         
151 //                              indent();
152 //                              fprintf(stdout,"OBJECT END: %s\n",name);
153                                 continue;
154                         }
155                 }
156
157 //
158 // read the triangles
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         *numtriangles = ptri - *pptri;
183
184         fclose (input);
185 }
186