trilib.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. //
  19. // trilib.c: library for loading triangles from an Alias triangle file
  20. //
  21. #include <stdio.h>
  22. #include "cmdlib.h"
  23. #include "mathlib.h"
  24. #include "trilib.h"
  25. // on disk representation of a face
  26. #define FLOAT_START 99999.0
  27. #define FLOAT_END -FLOAT_START
  28. #define MAGIC 123322
  29. //#define NOISY 1
  30. typedef struct {
  31. float v[3];
  32. } vector;
  33. typedef struct
  34. {
  35. vector n; /* normal */
  36. vector p; /* point */
  37. vector c; /* color */
  38. float u; /* u */
  39. float v; /* v */
  40. } aliaspoint_t;
  41. typedef struct {
  42. aliaspoint_t pt[3];
  43. } tf_triangle;
  44. void ByteSwapTri (tf_triangle *tri)
  45. {
  46. int i;
  47. for (i=0 ; i<sizeof(tf_triangle)/4 ; i++)
  48. {
  49. ((int *)tri)[i] = BigLong (((int *)tri)[i]);
  50. }
  51. }
  52. void LoadTriangleList (char *filename, triangle_t **pptri, int *numtriangles)
  53. {
  54. FILE *input;
  55. float start;
  56. char name[256], tex[256];
  57. int i, count, magic;
  58. tf_triangle tri;
  59. triangle_t *ptri;
  60. int iLevel;
  61. int exitpattern;
  62. float t;
  63. t = -FLOAT_START;
  64. *((unsigned char *)&exitpattern + 0) = *((unsigned char *)&t + 3);
  65. *((unsigned char *)&exitpattern + 1) = *((unsigned char *)&t + 2);
  66. *((unsigned char *)&exitpattern + 2) = *((unsigned char *)&t + 1);
  67. *((unsigned char *)&exitpattern + 3) = *((unsigned char *)&t + 0);
  68. if ((input = fopen(filename, "rb")) == 0)
  69. Error ("reader: could not open file '%s'", filename);
  70. iLevel = 0;
  71. fread(&magic, sizeof(int), 1, input);
  72. if (BigLong(magic) != MAGIC)
  73. Error ("%s is not a Alias object separated triangle file, magic number is wrong.", filename);
  74. ptri = malloc (MAXTRIANGLES * sizeof(triangle_t));
  75. *pptri = ptri;
  76. while (feof(input) == 0) {
  77. if (fread(&start, sizeof(float), 1, input) < 1)
  78. break;
  79. *(int *)&start = BigLong(*(int *)&start);
  80. if (*(int *)&start != exitpattern)
  81. {
  82. if (start == FLOAT_START) {
  83. /* Start of an object or group of objects. */
  84. i = -1;
  85. do {
  86. /* There are probably better ways to read a string from */
  87. /* a file, but this does allow you to do error checking */
  88. /* (which I'm not doing) on a per character basis. */
  89. ++i;
  90. fread( &(name[i]), sizeof( char ), 1, input);
  91. } while( name[i] != '\0' );
  92. // indent();
  93. // fprintf(stdout,"OBJECT START: %s\n",name);
  94. fread( &count, sizeof(int), 1, input);
  95. count = BigLong(count);
  96. ++iLevel;
  97. if (count != 0) {
  98. // indent();
  99. // fprintf(stdout,"NUMBER OF TRIANGLES: %d\n",count);
  100. i = -1;
  101. do {
  102. ++i;
  103. fread( &(tex[i]), sizeof( char ), 1, input);
  104. } while( tex[i] != '\0' );
  105. // indent();
  106. // fprintf(stdout," Object texture name: '%s'\n",tex);
  107. }
  108. /* Else (count == 0) this is the start of a group, and */
  109. /* no texture name is present. */
  110. }
  111. else if (start == FLOAT_END) {
  112. /* End of an object or group. Yes, the name should be */
  113. /* obvious from context, but it is in here just to be */
  114. /* safe and to provide a little extra information for */
  115. /* those who do not wish to write a recursive reader. */
  116. /* Mia culpa. */
  117. --iLevel;
  118. i = -1;
  119. do {
  120. ++i;
  121. fread( &(name[i]), sizeof( char ), 1, input);
  122. } while( name[i] != '\0' );
  123. // indent();
  124. // fprintf(stdout,"OBJECT END: %s\n",name);
  125. continue;
  126. }
  127. }
  128. //
  129. // read the triangles
  130. //
  131. for (i = 0; i < count; ++i) {
  132. int j;
  133. fread( &tri, sizeof(tf_triangle), 1, input );
  134. ByteSwapTri (&tri);
  135. for (j=0 ; j<3 ; j++)
  136. {
  137. int k;
  138. for (k=0 ; k<3 ; k++)
  139. {
  140. ptri->verts[j][k] = tri.pt[j].p.v[k];
  141. }
  142. }
  143. ptri++;
  144. if ((ptri - *pptri) >= MAXTRIANGLES)
  145. Error ("Error: too many triangles; increase MAXTRIANGLES\n");
  146. }
  147. }
  148. *numtriangles = ptri - *pptri;
  149. fclose (input);
  150. }