]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake2/common/l3dslib.c
eol style
[xonotic/netradiant.git] / tools / quake2 / common / l3dslib.c
index 7a551b11b0705f2b927c43fecdbe1c5e41cd591f..e9b256815b491c158537ee8b8ddbf795ac9eb169 100644 (file)
-/*\r
-Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
-For a list of contributors, see the accompanying CONTRIBUTORS file.\r
-\r
-This file is part of GtkRadiant.\r
-\r
-GtkRadiant is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-GtkRadiant is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with GtkRadiant; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
-*/\r
-//\r
-// l3dslib.c: library for loading triangles from an Alias triangle file\r
-//\r
-\r
-#include <stdio.h>\r
-#include "cmdlib.h"\r
-#include "inout.h"\r
-#include "mathlib.h"\r
-#include "trilib.h"\r
-#include "l3dslib.h"\r
-\r
-#define MAIN3DS       0x4D4D\r
-#define EDIT3DS       0x3D3D  // this is the start of the editor config\r
-#define EDIT_OBJECT   0x4000\r
-#define OBJ_TRIMESH   0x4100\r
-#define TRI_VERTEXL   0x4110\r
-#define TRI_FACEL1    0x4120\r
-\r
-#define MAXVERTS       2000\r
-\r
-typedef struct {\r
-       int     v[4];\r
-} tri;\r
-\r
-float  fverts[MAXVERTS][3];\r
-tri            tris[MAXTRIANGLES];\r
-\r
-int    bytesread, level, numtris, totaltris;\r
-int    vertsfound, trisfound;\r
-\r
-triangle_t     *ptri;\r
-\r
-\r
-// Alias stores triangles as 3 explicit vertices in .tri files, so even though we\r
-// start out with a vertex pool and vertex indices for triangles, we have to convert\r
-// to raw, explicit triangles\r
-void StoreAliasTriangles (void)\r
-{\r
-       int             i, j, k;\r
-\r
-       if ((totaltris + numtris) > MAXTRIANGLES)\r
-               Error ("Error: Too many triangles");\r
-\r
-       for (i=0; i<numtris ; i++)\r
-       {\r
-               for (j=0 ; j<3 ; j++)\r
-               {\r
-                       for (k=0 ; k<3 ; k++)\r
-                       {\r
-                               ptri[i+totaltris].verts[j][k] = fverts[tris[i].v[j]][k];\r
-                       }\r
-               }\r
-       }\r
-       \r
-       totaltris += numtris;\r
-       numtris = 0;\r
-       vertsfound = 0;\r
-       trisfound = 0;\r
-}\r
-\r
-\r
-int ParseVertexL (FILE *input)\r
-{\r
-       int                             i, j, startbytesread, numverts;\r
-       unsigned short  tshort;\r
-\r
-       if (vertsfound)\r
-               Error ("Error: Multiple vertex chunks");\r
-\r
-       vertsfound = 1;\r
-       startbytesread = bytesread;\r
-\r
-       if (feof(input))\r
-               Error ("Error: unexpected end of file");\r
-\r
-       fread(&tshort, sizeof(tshort), 1, input);\r
-       bytesread += sizeof(tshort);\r
-       numverts = (int)tshort;\r
-\r
-       if (numverts > MAXVERTS)\r
-               Error ("Error: Too many vertices");\r
-\r
-       for (i=0 ; i<numverts ; i++)\r
-       {\r
-               for (j=0 ; j<3 ; j++)\r
-               {\r
-                       if (feof(input))\r
-                               Error ("Error: unexpected end of file");\r
-\r
-                       fread(&fverts[i][j], sizeof(float), 1, input);\r
-                       bytesread += sizeof(float);\r
-               }\r
-       }\r
-\r
-       if (vertsfound && trisfound)\r
-               StoreAliasTriangles ();\r
-\r
-       return bytesread - startbytesread;\r
-}\r
-\r
-\r
-int ParseFaceL1 (FILE *input)\r
-{\r
-\r
-       int                             i, j, startbytesread;\r
-       unsigned short  tshort;\r
-\r
-       if (trisfound)\r
-               Error ("Error: Multiple face chunks");\r
-\r
-       trisfound = 1;\r
-       startbytesread = bytesread;\r
-\r
-       if (feof(input))\r
-               Error ("Error: unexpected end of file");\r
-\r
-       fread(&tshort, sizeof(tshort), 1, input);\r
-       bytesread += sizeof(tshort);\r
-       numtris = (int)tshort;\r
-\r
-       if (numtris > MAXTRIANGLES)\r
-               Error ("Error: Too many triangles");\r
-\r
-       for (i=0 ; i<numtris ; i++)\r
-       {\r
-               for (j=0 ; j<4 ; j++)\r
-               {\r
-                       if (feof(input))\r
-                               Error ("Error: unexpected end of file");\r
-\r
-                       fread(&tshort, sizeof(tshort), 1, input);\r
-                       bytesread += sizeof(tshort);\r
-                       tris[i].v[j] = (int)tshort;\r
-               }\r
-       }\r
-\r
-       if (vertsfound && trisfound)\r
-               StoreAliasTriangles ();\r
-\r
-       return bytesread - startbytesread;\r
-}\r
-\r
-\r
-int ParseChunk (FILE *input)\r
-{\r
-#define BLOCK_SIZE     4096\r
-       char                    temp[BLOCK_SIZE];\r
-       unsigned short  type;\r
-       int                             i, length, w, t, retval;\r
-\r
-       level++;\r
-       retval = 0;\r
-\r
-// chunk type\r
-       if (feof(input))\r
-               Error ("Error: unexpected end of file");\r
-\r
-       fread(&type, sizeof(type), 1, input);\r
-       bytesread += sizeof(type);\r
-\r
-// chunk length\r
-       if (feof(input))\r
-               Error ("Error: unexpected end of file");\r
-\r
-       fread (&length, sizeof(length), 1, input);\r
-       bytesread += sizeof(length);\r
-       w = length - 6;\r
-\r
-// process chunk if we care about it, otherwise skip it\r
-       switch (type)\r
-       {\r
-       case TRI_VERTEXL:\r
-               w -= ParseVertexL (input);\r
-               goto ParseSubchunk;\r
-\r
-       case TRI_FACEL1:\r
-               w -= ParseFaceL1 (input);\r
-               goto ParseSubchunk;\r
-\r
-       case EDIT_OBJECT:\r
-       // read the name\r
-               i = 0;\r
-\r
-               do\r
-               {\r
-                       if (feof(input))\r
-                               Error ("Error: unexpected end of file");\r
-\r
-                       fread (&temp[i], 1, 1, input);\r
-                       i++;\r
-                       w--;\r
-                       bytesread++;\r
-               } while (temp[i-1]);\r
-\r
-       case MAIN3DS:\r
-       case OBJ_TRIMESH:\r
-       case EDIT3DS:\r
-       // parse through subchunks\r
-ParseSubchunk:\r
-               while (w > 0)\r
-               {\r
-                       w -= ParseChunk (input);\r
-               }\r
-\r
-               retval = length;\r
-               goto Done;\r
-\r
-       default:\r
-       // skip other chunks\r
-               while (w > 0)\r
-               {\r
-                       t = w;\r
-\r
-                       if (t > BLOCK_SIZE)\r
-                               t = BLOCK_SIZE;\r
-\r
-                       if (feof(input))\r
-                               Error ("Error: unexpected end of file");\r
-\r
-                       fread (&temp, t, 1, input);\r
-                       bytesread += t;\r
-\r
-                       w -= t;\r
-               }\r
-\r
-               retval = length;\r
-               goto Done;\r
-       }\r
-\r
-Done:\r
-       level--;\r
-       return retval;\r
-}\r
-\r
-\r
-void Load3DSTriangleList (char *filename, triangle_t **pptri, int *numtriangles)\r
-{\r
-       FILE        *input;\r
-       short int       tshort;\r
-\r
-       bytesread = 0;\r
-       level = 0;\r
-       numtris = 0;\r
-       totaltris = 0;\r
-       vertsfound = 0;\r
-       trisfound = 0;\r
-\r
-       if ((input = fopen(filename, "rb")) == 0) {\r
-               fprintf(stderr,"reader: could not open file '%s'\n", filename);\r
-               exit(0);\r
-       }\r
-\r
-       fread(&tshort, sizeof(tshort), 1, input);\r
-\r
-// should only be MAIN3DS, but some files seem to start with EDIT3DS, with\r
-// no MAIN3DS\r
-       if ((tshort != MAIN3DS) && (tshort != EDIT3DS)) {\r
-               fprintf(stderr,"File is not a 3DS file.\n");\r
-               exit(0);\r
-       }\r
-\r
-// back to top of file so we can parse the first chunk descriptor\r
-       fseek(input, 0, SEEK_SET);\r
-\r
-       ptri = malloc (MAXTRIANGLES * sizeof(triangle_t));\r
-\r
-       *pptri = ptri;\r
-\r
-// parse through looking for the relevant chunk tree (MAIN3DS | EDIT3DS | EDIT_OBJECT |\r
-// OBJ_TRIMESH | {TRI_VERTEXL, TRI_FACEL1}) and skipping other chunks\r
-       ParseChunk (input);\r
-\r
-       if (vertsfound || trisfound)\r
-               Error ("Incomplete triangle set");\r
-\r
-       *numtriangles = totaltris;\r
-\r
-       fclose (input);\r
-}\r
-\r
+/*
+Copyright (C) 1999-2007 id Software, Inc. and contributors.
+For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+This file is part of GtkRadiant.
+
+GtkRadiant is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+GtkRadiant is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GtkRadiant; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+//
+// l3dslib.c: library for loading triangles from an Alias triangle file
+//
+
+#include <stdio.h>
+#include "cmdlib.h"
+#include "inout.h"
+#include "mathlib.h"
+#include "trilib.h"
+#include "l3dslib.h"
+
+#define MAIN3DS       0x4D4D
+#define EDIT3DS       0x3D3D  // this is the start of the editor config
+#define EDIT_OBJECT   0x4000
+#define OBJ_TRIMESH   0x4100
+#define TRI_VERTEXL   0x4110
+#define TRI_FACEL1    0x4120
+
+#define MAXVERTS       2000
+
+typedef struct {
+       int     v[4];
+} tri;
+
+float  fverts[MAXVERTS][3];
+tri            tris[MAXTRIANGLES];
+
+int    bytesread, level, numtris, totaltris;
+int    vertsfound, trisfound;
+
+triangle_t     *ptri;
+
+
+// Alias stores triangles as 3 explicit vertices in .tri files, so even though we
+// start out with a vertex pool and vertex indices for triangles, we have to convert
+// to raw, explicit triangles
+void StoreAliasTriangles (void)
+{
+       int             i, j, k;
+
+       if ((totaltris + numtris) > MAXTRIANGLES)
+               Error ("Error: Too many triangles");
+
+       for (i=0; i<numtris ; i++)
+       {
+               for (j=0 ; j<3 ; j++)
+               {
+                       for (k=0 ; k<3 ; k++)
+                       {
+                               ptri[i+totaltris].verts[j][k] = fverts[tris[i].v[j]][k];
+                       }
+               }
+       }
+       
+       totaltris += numtris;
+       numtris = 0;
+       vertsfound = 0;
+       trisfound = 0;
+}
+
+
+int ParseVertexL (FILE *input)
+{
+       int                             i, j, startbytesread, numverts;
+       unsigned short  tshort;
+
+       if (vertsfound)
+               Error ("Error: Multiple vertex chunks");
+
+       vertsfound = 1;
+       startbytesread = bytesread;
+
+       if (feof(input))
+               Error ("Error: unexpected end of file");
+
+       fread(&tshort, sizeof(tshort), 1, input);
+       bytesread += sizeof(tshort);
+       numverts = (int)tshort;
+
+       if (numverts > MAXVERTS)
+               Error ("Error: Too many vertices");
+
+       for (i=0 ; i<numverts ; i++)
+       {
+               for (j=0 ; j<3 ; j++)
+               {
+                       if (feof(input))
+                               Error ("Error: unexpected end of file");
+
+                       fread(&fverts[i][j], sizeof(float), 1, input);
+                       bytesread += sizeof(float);
+               }
+       }
+
+       if (vertsfound && trisfound)
+               StoreAliasTriangles ();
+
+       return bytesread - startbytesread;
+}
+
+
+int ParseFaceL1 (FILE *input)
+{
+
+       int                             i, j, startbytesread;
+       unsigned short  tshort;
+
+       if (trisfound)
+               Error ("Error: Multiple face chunks");
+
+       trisfound = 1;
+       startbytesread = bytesread;
+
+       if (feof(input))
+               Error ("Error: unexpected end of file");
+
+       fread(&tshort, sizeof(tshort), 1, input);
+       bytesread += sizeof(tshort);
+       numtris = (int)tshort;
+
+       if (numtris > MAXTRIANGLES)
+               Error ("Error: Too many triangles");
+
+       for (i=0 ; i<numtris ; i++)
+       {
+               for (j=0 ; j<4 ; j++)
+               {
+                       if (feof(input))
+                               Error ("Error: unexpected end of file");
+
+                       fread(&tshort, sizeof(tshort), 1, input);
+                       bytesread += sizeof(tshort);
+                       tris[i].v[j] = (int)tshort;
+               }
+       }
+
+       if (vertsfound && trisfound)
+               StoreAliasTriangles ();
+
+       return bytesread - startbytesread;
+}
+
+
+int ParseChunk (FILE *input)
+{
+#define BLOCK_SIZE     4096
+       char                    temp[BLOCK_SIZE];
+       unsigned short  type;
+       int                             i, length, w, t, retval;
+
+       level++;
+       retval = 0;
+
+// chunk type
+       if (feof(input))
+               Error ("Error: unexpected end of file");
+
+       fread(&type, sizeof(type), 1, input);
+       bytesread += sizeof(type);
+
+// chunk length
+       if (feof(input))
+               Error ("Error: unexpected end of file");
+
+       fread (&length, sizeof(length), 1, input);
+       bytesread += sizeof(length);
+       w = length - 6;
+
+// process chunk if we care about it, otherwise skip it
+       switch (type)
+       {
+       case TRI_VERTEXL:
+               w -= ParseVertexL (input);
+               goto ParseSubchunk;
+
+       case TRI_FACEL1:
+               w -= ParseFaceL1 (input);
+               goto ParseSubchunk;
+
+       case EDIT_OBJECT:
+       // read the name
+               i = 0;
+
+               do
+               {
+                       if (feof(input))
+                               Error ("Error: unexpected end of file");
+
+                       fread (&temp[i], 1, 1, input);
+                       i++;
+                       w--;
+                       bytesread++;
+               } while (temp[i-1]);
+
+       case MAIN3DS:
+       case OBJ_TRIMESH:
+       case EDIT3DS:
+       // parse through subchunks
+ParseSubchunk:
+               while (w > 0)
+               {
+                       w -= ParseChunk (input);
+               }
+
+               retval = length;
+               goto Done;
+
+       default:
+       // skip other chunks
+               while (w > 0)
+               {
+                       t = w;
+
+                       if (t > BLOCK_SIZE)
+                               t = BLOCK_SIZE;
+
+                       if (feof(input))
+                               Error ("Error: unexpected end of file");
+
+                       fread (&temp, t, 1, input);
+                       bytesread += t;
+
+                       w -= t;
+               }
+
+               retval = length;
+               goto Done;
+       }
+
+Done:
+       level--;
+       return retval;
+}
+
+
+void Load3DSTriangleList (char *filename, triangle_t **pptri, int *numtriangles)
+{
+       FILE        *input;
+       short int       tshort;
+
+       bytesread = 0;
+       level = 0;
+       numtris = 0;
+       totaltris = 0;
+       vertsfound = 0;
+       trisfound = 0;
+
+       if ((input = fopen(filename, "rb")) == 0) {
+               fprintf(stderr,"reader: could not open file '%s'\n", filename);
+               exit(0);
+       }
+
+       fread(&tshort, sizeof(tshort), 1, input);
+
+// should only be MAIN3DS, but some files seem to start with EDIT3DS, with
+// no MAIN3DS
+       if ((tshort != MAIN3DS) && (tshort != EDIT3DS)) {
+               fprintf(stderr,"File is not a 3DS file.\n");
+               exit(0);
+       }
+
+// back to top of file so we can parse the first chunk descriptor
+       fseek(input, 0, SEEK_SET);
+
+       ptri = malloc (MAXTRIANGLES * sizeof(triangle_t));
+
+       *pptri = ptri;
+
+// parse through looking for the relevant chunk tree (MAIN3DS | EDIT3DS | EDIT_OBJECT |
+// OBJ_TRIMESH | {TRI_VERTEXL, TRI_FACEL1}) and skipping other chunks
+       ParseChunk (input);
+
+       if (vertsfound || trisfound)
+               Error ("Incomplete triangle set");
+
+       *numtriangles = totaltris;
+
+       fclose (input);
+}
+