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
23 // l3dslib.c: library for loading triangles from an Alias triangle file
36 #define MAIN3DS 0x4D4D
37 #define EDIT3DS 0x3D3D // this is the start of the editor config
38 #define EDIT_OBJECT 0x4000
39 #define OBJ_TRIMESH 0x4100
40 #define TRI_VERTEXL 0x4110
41 #define TRI_FACEL1 0x4120
49 float fverts[MAXVERTS][3];
50 tri tris[MAXTRIANGLES];
52 int bytesread, level, numtris, totaltris;
53 int vertsfound, trisfound;
59 void DefaultNodesList(mesh_node_t **nodesList, int *num_mesh_nodes, int *numtriangles)
66 memset(&(*nodesList)[0], 0, sizeof(mesh_node_t));
67 strcpy((*nodesList)[0].name, "default");
69 // set all of the tris to be used for the top node
70 for(i = 0; i < (*numtriangles); i++)
73 bit = 1 << ((i) & 7 );
75 (*nodesList)[0].tris[pos] |= bit;
81 // Alias stores triangles as 3 explicit vertices in .tri files, so even though we
82 // start out with a vertex pool and vertex indices for triangles, we have to convert
83 // to raw, explicit triangles
84 void StoreAliasTriangles (void)
88 if ((totaltris + numtris) > MAXTRIANGLES)
89 Error ("Error: Too many triangles");
91 for (i=0; i<numtris ; i++)
97 ptri[i+totaltris].verts[j][k] = fverts[tris[i].v[j]][k];
102 totaltris += numtris;
109 int ParseVertexL (FILE *input)
111 int i, j, startbytesread, numverts;
112 unsigned short tshort;
115 Error ("Error: Multiple vertex chunks");
118 startbytesread = bytesread;
121 Error ("Error: unexpected end of file");
123 fread(&tshort, sizeof(tshort), 1, input);
124 bytesread += sizeof(tshort);
125 numverts = (int)tshort;
127 if (numverts > MAXVERTS)
128 Error ("Error: Too many vertices");
130 for (i=0 ; i<numverts ; i++)
132 for (j=0 ; j<3 ; j++)
135 Error ("Error: unexpected end of file");
137 fread(&fverts[i][j], sizeof(float), 1, input);
138 bytesread += sizeof(float);
142 if (vertsfound && trisfound)
143 StoreAliasTriangles ();
145 return bytesread - startbytesread;
149 int ParseFaceL1 (FILE *input)
152 int i, j, startbytesread;
153 unsigned short tshort;
156 Error ("Error: Multiple face chunks");
159 startbytesread = bytesread;
162 Error ("Error: unexpected end of file");
164 fread(&tshort, sizeof(tshort), 1, input);
165 bytesread += sizeof(tshort);
166 numtris = (int)tshort;
168 if (numtris > MAXTRIANGLES)
169 Error ("Error: Too many triangles");
171 for (i=0 ; i<numtris ; i++)
173 for (j=0 ; j<4 ; j++)
176 Error ("Error: unexpected end of file");
178 fread(&tshort, sizeof(tshort), 1, input);
179 bytesread += sizeof(tshort);
180 tris[i].v[j] = (int)tshort;
184 if (vertsfound && trisfound)
185 StoreAliasTriangles ();
187 return bytesread - startbytesread;
191 int ParseChunk (FILE *input)
193 #define BLOCK_SIZE 4096
194 char temp[BLOCK_SIZE];
196 int i, length, w, t, retval;
203 Error ("Error: unexpected end of file");
205 fread(&type, sizeof(type), 1, input);
206 bytesread += sizeof(type);
210 Error ("Error: unexpected end of file");
212 fread (&length, sizeof(length), 1, input);
213 bytesread += sizeof(length);
216 // process chunk if we care about it, otherwise skip it
220 w -= ParseVertexL (input);
224 w -= ParseFaceL1 (input);
234 Error ("Error: unexpected end of file");
236 fread (&temp[i], 1, 1, input);
245 // parse through subchunks
249 w -= ParseChunk (input);
265 Error ("Error: unexpected end of file");
267 fread (&temp, t, 1, input);
283 void Load3DSTriangleList (char *filename, triangle_t **pptri, int *numtriangles, mesh_node_t **nodesList, int *num_mesh_nodes)
291 *nodesList = (mesh_node_t *) SafeMalloc(MAX_FM_MESH_NODES * sizeof(mesh_node_t), "Mesh Node List");
301 if ((input = fopen(filename, "rb")) == 0) {
302 fprintf(stderr,"reader: could not open file '%s'\n", filename);
306 fread(&tshort, sizeof(tshort), 1, input);
308 // should only be MAIN3DS, but some files seem to start with EDIT3DS, with
310 if ((tshort != MAIN3DS) && (tshort != EDIT3DS)) {
311 fprintf(stderr,"File is not a 3DS file.\n");
315 // back to top of file so we can parse the first chunk descriptor
316 fseek(input, 0, SEEK_SET);
318 ptri = malloc (MAXTRIANGLES * sizeof(triangle_t));
322 // parse through looking for the relevant chunk tree (MAIN3DS | EDIT3DS | EDIT_OBJECT |
323 // OBJ_TRIMESH | {TRI_VERTEXL, TRI_FACEL1}) and skipping other chunks
326 if (vertsfound || trisfound)
327 Error ("Incomplete triangle set");
329 *numtriangles = totaltris;
333 DefaultNodesList(nodesList,num_mesh_nodes,numtriangles);
336 //==========================================================================
340 //==========================================================================
342 void LoadASC(char *fileName, triangle_t **triList, int *triangleCount, mesh_node_t **nodesList, int *num_mesh_nodes)
360 *nodesList = (mesh_node_t *) SafeMalloc(MAX_FM_MESH_NODES * sizeof(mesh_node_t), "Mesh Node List");
363 TK_OpenSource(fileName);
366 while(goodObject == false)
368 TK_Beyond(TK_C_NAMED);
369 TK_Beyond(TK_OBJECT);
372 TK_BeyondRequire(TK_C_VERTICES, TK_COLON);
373 TK_FetchRequire(TK_INTNUMBER);
374 vertexCount = tk_IntNumber;
380 TK_BeyondRequire(TK_C_FACES, TK_COLON);
381 TK_FetchRequire(TK_INTNUMBER);
382 triCount = tk_IntNumber;
383 if(triCount >= MAXTRIANGLES)
385 Error("Too many triangles in file %s\n", fileName);
387 *triangleCount = triCount;
388 tList = (triangle_t *) SafeMalloc(MAXTRIANGLES*sizeof(triangle_t), "Triangle list");
391 memset(*triList,0,MAXTRIANGLES*sizeof(triangle_t));
392 TK_BeyondRequire(TK_C_VERTEX, TK_LIST);
394 /* rx = ((rotation[0]+90.0)/360.0)*2.0*M_PI;
395 //rx = (rotation[0]/360.0)*2.0*M_PI;
396 ry = (rotation[1]/360.0)*2.0*M_PI;
397 rz = (rotation[2]/360.0)*2.0*M_PI;
399 vList = (void *) SafeMalloc(vertexCount*sizeof vList[0], "Vertex list");
400 for(i = 0; i < vertexCount; i++)
402 TK_BeyondRequire(TK_C_VERTEX, TK_INTNUMBER);
403 if(tk_IntNumber != i)
405 Error("File '%s', line %d:\nVertex index mismatch.\n",
406 tk_SourceName, tk_Line);
408 TK_FetchRequireFetch(TK_COLON);
410 TK_BeyondRequire(TK_COLON, TK_FLOATNUMBER);
412 TK_BeyondRequire(TK_COLON, TK_FLOATNUMBER);
414 TK_BeyondRequire(TK_COLON, TK_FLOATNUMBER);
417 /* x2 = x*cos(rz)+y*sin(rz);
418 y2 = -x*sin(rz)+y*cos(rz);
421 y2 = y*cos(rx)+z*sin(rx);
422 z2 = -y*sin(rx)+z*cos(rx);
425 x2 = x*cos(ry)-z*sin(ry);
426 z2 = x*sin(ry)+z*cos(ry);
434 TK_BeyondRequire(TK_C_FACE, TK_LIST);
435 for(i = 0; i < triCount; i++)
437 TK_BeyondRequire(TK_C_FACE, TK_INTNUMBER);
438 if(tk_IntNumber != i)
440 Error("File '%s', line %d:\nTriangle index mismatch.\n",
441 tk_SourceName, tk_Line);
443 for(j = 0; j < 3; j++)
445 TK_BeyondRequire(TK_IDENTIFIER, TK_COLON);
446 TK_FetchRequire(TK_INTNUMBER);
447 if(tk_IntNumber >= vertexCount)
449 Error("File '%s', line %d:\nVertex number"
450 " > vertexCount: %d\n", tk_SourceName, tk_Line,
453 tList[i].verts[2-j][0] = vList[tk_IntNumber].v[0];
454 tList[i].verts[2-j][1] = vList[tk_IntNumber].v[1];
455 tList[i].verts[2-j][2] = vList[tk_IntNumber].v[2];
457 tList[i].indicies[2-j] = tk_IntNumber;
461 /* printf("Face %i:\n v0: %f, %f, %f\n v1: %f, %f, %f\n"
462 " v2: %f, %f, %f\n", i,
463 tList[i].verts[0][0],
464 tList[i].verts[0][1],
465 tList[i].verts[0][2],
466 tList[i].verts[1][0],
467 tList[i].verts[1][1],
468 tList[i].verts[1][2],
469 tList[i].verts[2][0],
470 tList[i].verts[2][1],
471 tList[i].verts[2][2]);
475 DefaultNodesList(nodesList,num_mesh_nodes,triangleCount);