devil1geo.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "devil1geo.h"
  2. #include <stdio.h>
  3. static void printgheader(struct Header*);
  4. static void printmeshheader(struct MeshHeader*);
  5. static void printmeshbatch(struct Batch*);
  6. static void printcoordinate(struct Coordinate*, unsigned int);
  7. static bool getgheader(struct Header**, const char*);
  8. static bool getmeshheader(struct MeshHeader**, unsigned int i, const char * const);
  9. static bool getmeshbatch(struct Batch*, unsigned int offset, const char * const);
  10. static bool getmesh(struct Mesh*, unsigned int i, const char*, unsigned int filesize);
  11. fn_devil1geo const DEVIL1GEO = {printgheader,
  12. printmeshheader,
  13. printmeshbatch,
  14. printcoordinate,
  15. getgheader,
  16. getmeshheader,
  17. getmeshbatch,
  18. getmesh};
  19. static void printgheader(struct Header *gh) {
  20. if (gh != NULL) {
  21. printf("pointer %p\n", gh);
  22. printf("number of meshes %x\n", gh -> numMesh);
  23. printf("unknown number B %x\n", gh -> unknownNumberB);
  24. printf("unknown number C %x\n", gh -> unknownNumberC);
  25. printf("unknown number D %x\n", gh -> unknownNumberD);
  26. printf("padding %x\n", gh -> padding);
  27. printf("unknown offset %lx\n", gh -> unknownOffset);
  28. }
  29. }
  30. static void printmeshheader(struct MeshHeader *mh) {
  31. if (mh == NULL) {
  32. return;
  33. }
  34. printf("number of batches %x\n", mh -> numBatch);
  35. printf("number of vertices %x\n", mh -> numVertex);
  36. printf("unknown %x\n", mh -> u);
  37. printf("batch offset %lx\n", mh -> offsetBatches);
  38. printf("flags %lx\n\n", mh -> flags);
  39. }
  40. static void printmeshbatch(struct Batch *b) {
  41. if (b == NULL) {
  42. return;
  43. }
  44. struct BatchData *bd = b -> bd;
  45. printf("number of vertices %x\n", bd -> numVertex);
  46. printf("unknown byte %x\n", bd -> uB);
  47. printf("padding %x\n", bd -> padding);
  48. printf("offsetPositions %lx\n", bd -> offsetPositions);
  49. printf("offsetNormals %lx\n", bd -> offsetNormals);
  50. printf("offsetUVs %lx\n", bd -> offsetUVs);
  51. printf("offsetBoneIndexes %lx\n", bd -> offsetBoneIndexes);
  52. printf("offsetBoneWeights %lx\n", bd -> offsetBoneWeights);
  53. printf("offsets %lx\n\n", bd -> offsets[0]);
  54. printcoordinate(b -> vd.positions, 3);
  55. }
  56. static void printcoordinate(struct Coordinate *p, unsigned int count) {
  57. if (p == NULL) {
  58. return;
  59. }
  60. unsigned int i;
  61. for (i = 0; i < count; i++) {
  62. printf("(%f, %f, %f)\n", (p[i]).x, (p[i]).y, (p[i]).z);
  63. }
  64. }
  65. static bool getgheader(struct Header** h, const char* filedata) {
  66. if (filedata == NULL) {
  67. return false;
  68. }
  69. (*h) = (struct Header*)filedata;
  70. return true;
  71. }
  72. static bool getmeshheader(struct MeshHeader **hs,
  73. unsigned int i,
  74. const char * const filedata) {
  75. bool done = false;
  76. struct Header *h = NULL;
  77. if (hs == NULL || !(getgheader(&h, filedata))) {
  78. return done;
  79. }
  80. if (h -> numMesh < i) {
  81. return done;
  82. }
  83. unsigned int pos = sizeof(struct MeshHeader) * i + sizeof(struct Header);
  84. (*hs) = (struct MeshHeader*)(filedata + pos);
  85. done = true;
  86. return done;
  87. }
  88. static bool getmeshbatch(struct Batch *b,
  89. unsigned int offset,
  90. const char * const filedata) {
  91. bool done = false;
  92. if (b == NULL || filedata == NULL) {
  93. return done;
  94. }
  95. struct BatchData *d1 = NULL;
  96. struct VertexData d2;
  97. d1 = (struct BatchData*) (filedata + offset);
  98. d2.positions = (struct Coordinate*) (filedata + (d1 -> offsetPositions));
  99. d2.normals = (struct Coordinate*) (filedata + (d1 -> offsetNormals));
  100. d2.u = (struct UVs*) (filedata + (d1 -> offsetUVs));
  101. d2.bi = (struct BoneIndexes*)(filedata + (d1 -> offsetBoneIndexes));
  102. d2.bw = (struct BoneWeights*)(filedata + (d1 -> offsetBoneWeights));
  103. b -> bd = d1;
  104. b -> vd = d2;
  105. done = true;
  106. return done;
  107. }
  108. // assume client has allocated memory for mesh
  109. static bool getmesh(struct Mesh *m,
  110. unsigned int i,
  111. const char * const filedata,
  112. unsigned int filesize) {
  113. bool done = false;
  114. if (m == NULL || filedata == NULL || m -> b == NULL) {
  115. return done;
  116. }
  117. struct MeshHeader *mh = NULL;
  118. bool ok = getmeshheader(&mh, i, filedata);
  119. if (ok) {
  120. unsigned int j;
  121. struct Batch b;
  122. for (j = 0; j < mh -> numBatch; j++) {
  123. unsigned int offset = mh->offsetBatches + j * sizeof(struct BatchData);
  124. if (offset > filesize) {
  125. return done;
  126. }
  127. getmeshbatch(&b, offset, filedata);
  128. // printmeshbatch(&b);
  129. m -> b[j] = b;
  130. }
  131. done = true;
  132. }
  133. return done;
  134. }