1
0

READ_COB.C 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /******************************************************************************
  2. Plush Version 1.2
  3. read_cob.c
  4. ASCII COB Object Reader
  5. Copyright (c) 1996-2000, Justin Frankel
  6. ******************************************************************************/
  7. #include "plush.h"
  8. #define PL_COB_MAX_LINELENGTH 1024
  9. pl_Obj *plReadCOBObj(char *fn, pl_Mat *mat) {
  10. FILE *fp = fopen(fn,"rt");
  11. long int p1,m1,p2,m2,p3,m3;
  12. char temp_string[PL_COB_MAX_LINELENGTH];
  13. float TransMatrix[4][4];
  14. pl_Obj *obj;
  15. pl_sInt32 x,i2;
  16. long int numVertices, numMappingVertices, numFaces, i;
  17. pl_sInt32 *MappingVertices = 0;
  18. if (!fp) return 0;
  19. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  20. if (memcmp("Caligari",temp_string,8)) { fclose(fp); return 0; }
  21. do {
  22. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  23. } while (!feof(fp) && memcmp("Transform",temp_string,9));
  24. if (feof(fp)) { fclose(fp); return 0; }
  25. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  26. sscanf(temp_string,"%f %f %f %f",
  27. &TransMatrix[0][0],&TransMatrix[0][1],&TransMatrix[0][2],&TransMatrix[0][3]);
  28. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  29. sscanf(temp_string,"%f %f %f %f",
  30. &TransMatrix[1][0],&TransMatrix[1][1],&TransMatrix[1][2],&TransMatrix[1][3]);
  31. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  32. sscanf(temp_string,"%f %f %f %f",
  33. &TransMatrix[2][0],&TransMatrix[2][1],&TransMatrix[2][2],&TransMatrix[2][3]);
  34. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  35. sscanf(temp_string,"%f %f %f %f",
  36. &TransMatrix[3][0],&TransMatrix[3][1],&TransMatrix[3][2],&TransMatrix[3][3]);
  37. do {
  38. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  39. } while (!feof(fp) && memcmp("World Vertices",temp_string,12));
  40. if (feof(fp) || sscanf(temp_string,"World Vertices %ld",&numVertices) != 1)
  41. { fclose(fp); return 0; }
  42. rewind(fp);
  43. do {
  44. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  45. } while (!feof(fp) && memcmp("Texture Vertices",temp_string,16));
  46. if (feof(fp) ||
  47. sscanf(temp_string,"Texture Vertices %ld",&numMappingVertices) != 1) {
  48. fclose(fp); return 0;
  49. }
  50. rewind(fp);
  51. do {
  52. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  53. } while (!feof(fp) && memcmp("Faces",temp_string,5));
  54. if (feof(fp) || sscanf(temp_string,"Faces %ld",&numFaces) != 1) {
  55. fclose(fp); return 0;
  56. }
  57. for (x = numFaces; x; x--) {
  58. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  59. if (feof(fp) || sscanf(temp_string+4," verts %ld",&i) != 1 || i < 3) {
  60. fclose(fp);
  61. return 0;
  62. }
  63. numFaces += i-3;
  64. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  65. }
  66. obj = plObjCreate(numVertices,numFaces);
  67. if (!obj) { fclose(fp); return 0; }
  68. rewind(fp);
  69. do {
  70. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  71. } while (!feof(fp) && memcmp("World Vertices",temp_string,12));
  72. if (feof(fp)) { plObjDelete(obj); fclose(fp); return 0; }
  73. for (x = 0; x < numVertices; x ++) {
  74. float xp, yp, zp;
  75. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  76. if (feof(fp) ||
  77. sscanf(temp_string,"%f %f %f", &xp, &yp, &zp) != 3) {
  78. plObjDelete(obj); fclose(fp); return 0;
  79. }
  80. obj->Vertices[x].x = (TransMatrix[0][0]*xp+TransMatrix[0][1]*yp+
  81. TransMatrix[0][2]*zp+TransMatrix[0][3]);
  82. obj->Vertices[x].y = (TransMatrix[1][0]*xp+TransMatrix[1][1]*yp+
  83. TransMatrix[1][2]*zp+TransMatrix[1][3]);
  84. obj->Vertices[x].z = (TransMatrix[2][0]*xp+TransMatrix[2][1]*yp+
  85. TransMatrix[2][2]*zp+TransMatrix[2][3]);
  86. }
  87. rewind(fp);
  88. do {
  89. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  90. } while (!feof(fp) && memcmp("Texture Vertices",temp_string,16));
  91. if (!feof(fp)) {
  92. MappingVertices = (pl_sInt32 *)
  93. malloc(sizeof(pl_sInt32) * numMappingVertices * 2);
  94. if (MappingVertices) {
  95. for (x = 0; x < numMappingVertices; x ++) {
  96. float p1, p2;
  97. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  98. if (feof(fp) || sscanf(temp_string,"%f %f", &p1, &p2) != 2) {
  99. free(MappingVertices); plObjDelete(obj); fclose(fp); return 0;
  100. }
  101. MappingVertices[x*2] = (pl_sInt32) (p1*65536.0);
  102. MappingVertices[x*2+1] = (pl_sInt32) (p2*65536.0);
  103. }
  104. }
  105. }
  106. rewind(fp);
  107. do {
  108. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  109. } while (!feof(fp) && memcmp("Faces",temp_string,5));
  110. if (feof(fp)) {
  111. if (MappingVertices) free(MappingVertices);
  112. plObjDelete(obj); fclose(fp); return 0;
  113. }
  114. for (x = 0; x < numFaces; x ++) {
  115. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  116. sscanf(temp_string+4," verts %ld",&i);
  117. fgets(temp_string,PL_COB_MAX_LINELENGTH,fp);
  118. if (i == 3) {
  119. if (feof(fp) || sscanf(temp_string,"<%ld,%ld> <%ld,%ld> <%ld,%ld>",
  120. &p3,&m3,&p2,&m2,&p1,&m1) != 6) {
  121. if (MappingVertices) free(MappingVertices);
  122. plObjDelete(obj); fclose(fp); return 0;
  123. }
  124. obj->Faces[x].Vertices[0] = obj->Vertices + p1;
  125. obj->Faces[x].Vertices[1] = obj->Vertices + p2;
  126. obj->Faces[x].Vertices[2] = obj->Vertices + p3;
  127. if (MappingVertices) {
  128. obj->Faces[x].MappingU[0] = MappingVertices[m1*2];
  129. obj->Faces[x].MappingV[0] = MappingVertices[m1*2+1];
  130. obj->Faces[x].MappingU[1] = MappingVertices[m2*2];
  131. obj->Faces[x].MappingV[1] = MappingVertices[m2*2+1];
  132. obj->Faces[x].MappingU[2] = MappingVertices[m3*2];
  133. obj->Faces[x].MappingV[2] = MappingVertices[m3*2+1];
  134. }
  135. obj->Faces[x].Material = mat;
  136. } else {
  137. long int p[16],m[16];
  138. if (feof(fp)) {
  139. if (MappingVertices) free(MappingVertices);
  140. plObjDelete(obj); fclose(fp); return 0;
  141. }
  142. sscanf(temp_string,
  143. "<%ld,%ld> <%ld,%ld> <%ld,%ld> <%ld,%ld> "
  144. "<%ld,%ld> <%ld,%ld> <%ld,%ld> <%ld,%ld> "
  145. "<%ld,%ld> <%ld,%ld> <%ld,%ld> <%ld,%ld> "
  146. "<%ld,%ld> <%ld,%ld> <%ld,%ld> <%ld,%ld> ",
  147. p+0,m+0,p+1,m+1,p+2,m+2,p+3,m+3,
  148. p+4,m+4,p+5,m+5,p+6,m+6,p+7,m+7,
  149. p+8,m+8,p+9,m+9,p+10,m+10,p+11,m+11,
  150. p+12,m+12,p+13,m+13,p+14,m+14,p+15,m+15);
  151. for (i2 = 1; i2 < (i-1); i2 ++) {
  152. obj->Faces[x].Vertices[0] = obj->Vertices + p[0];
  153. obj->Faces[x].Vertices[1] = obj->Vertices + p[i2+1];
  154. obj->Faces[x].Vertices[2] = obj->Vertices + p[i2];
  155. if (MappingVertices) {
  156. obj->Faces[x].MappingU[0] = MappingVertices[m[0]*2];
  157. obj->Faces[x].MappingV[0] = MappingVertices[m[0]*2+1];
  158. obj->Faces[x].MappingU[1] = MappingVertices[m[i2+1]*2];
  159. obj->Faces[x].MappingV[1] = MappingVertices[m[i2+1]*2+1];
  160. obj->Faces[x].MappingU[2] = MappingVertices[m[i2]*2];
  161. obj->Faces[x].MappingV[2] = MappingVertices[m[i2]*2+1];
  162. }
  163. obj->Faces[x].Material = mat;
  164. x++;
  165. }
  166. x--;
  167. }
  168. }
  169. obj->BackfaceCull = 1;
  170. if (MappingVertices) free(MappingVertices);
  171. plObjCalcNormals(obj);
  172. fclose(fp);
  173. return obj;
  174. }