objfile.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * OBJ File implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <tuple.h>
  13. #include <ray.h>
  14. #include <shape.h>
  15. #include <objfile.h>
  16. #include <math_helper.h>
  17. #include <group.h>
  18. #define MIN_ALLOC (2)
  19. #define DEFAULT_GROUP (0)
  20. OBJFile::OBJFile() : Shape(SHAPE_OBJFILE), ignoredLines(0)
  21. {
  22. stats.addOBJFile();
  23. this->allocatedFaceGroupCount = MIN_ALLOC;
  24. this->faceGroupList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
  25. this->faceGroupCount = 0;
  26. this->allocatedVertexCount = MIN_ALLOC;
  27. this->vertexList = (Tuple **)calloc(sizeof(Tuple *), MIN_ALLOC);
  28. this->vertexCount = 0;
  29. /* There is always a default group */
  30. this->addGroup(new Group());
  31. };
  32. OBJFile::OBJFile(const char *filepath) : OBJFile()
  33. {
  34. FILE *fp;
  35. size_t fileSize;
  36. char *fileBuff;
  37. fp = fopen(filepath, "rt");
  38. if (fp)
  39. {
  40. fseek(fp, 0, SEEK_END);
  41. fileSize = ftell(fp);
  42. fileBuff = (char *)calloc(fileSize, 1);
  43. fseek(fp, 0, SEEK_SET);
  44. fileSize = fread(fileBuff, 1, fileSize, fp);
  45. fclose(fp);
  46. this->parseOBJFile(fileBuff);
  47. free(fileBuff);
  48. }
  49. else
  50. {
  51. printf("ERROR: Can't open/find the file '%s'.\n", filepath);
  52. }
  53. }
  54. void OBJFile::addGroup(Shape *group)
  55. {
  56. if ((this->faceGroupCount + 1) > this->allocatedFaceGroupCount)
  57. {
  58. this->allocatedFaceGroupCount *= 2;
  59. this->faceGroupList = (Shape **)realloc(this->faceGroupList, sizeof(Shape **) * this->allocatedFaceGroupCount);
  60. }
  61. group->parent = this;
  62. group->updateTransform();
  63. this->faceGroupList[this->faceGroupCount++] = group;
  64. this->bounds | group->getBounds();
  65. }
  66. void OBJFile::addVertex(Tuple *vertex)
  67. {
  68. if ((this->vertexCount + 1) > this->allocatedVertexCount)
  69. {
  70. this->allocatedVertexCount *= 2;
  71. this->vertexList = (Tuple **)realloc(this->vertexList, sizeof(Tuple **) * this->allocatedVertexCount);
  72. }
  73. this->vertexList[this->vertexCount++] = vertex;
  74. }
  75. Intersect OBJFile::intersect(Ray r)
  76. {
  77. Intersect ret;
  78. int i, j;
  79. if (this->faceGroupCount > 0)
  80. {
  81. if (this->bounds.intesectMe(r))
  82. {
  83. for (i = 0 ; i < this->faceGroupCount ; i++)
  84. {
  85. Intersect xs = this->faceGroupList[i]->intersect(r);
  86. if (xs.count() > 0)
  87. {
  88. for (j = 0 ; j < xs.count() ; j++)
  89. {
  90. ret.add(xs[j]);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. return ret;
  97. }
  98. Intersect OBJFile::localIntersect(Ray r)
  99. {
  100. return Intersect();
  101. }
  102. Tuple OBJFile::localNormalAt(Tuple point)
  103. {
  104. return Vector(0, 1, 0);
  105. }
  106. BoundingBox OBJFile::getLocalBounds()
  107. {
  108. return this->bounds;
  109. }
  110. BoundingBox OBJFile::getBounds()
  111. {
  112. if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
  113. return this->bounds;
  114. }
  115. void OBJFile::updateBoundingBox()
  116. {
  117. int i;
  118. this->bounds.reset();
  119. for(i = 0; i < this->faceGroupCount; i++)
  120. {
  121. if (this->faceGroupList[i]->haveFiniteBounds())
  122. {
  123. BoundingBox objB = this->faceGroupList[i]->getBounds();
  124. this->bounds | objB;
  125. }
  126. }
  127. }
  128. void OBJFile::updateTransform()
  129. {
  130. int i;
  131. Shape::updateTransform();
  132. for (i = 0 ; i < this->faceGroupCount ; i++)
  133. {
  134. this->faceGroupList[i]->updateTransform();
  135. }
  136. /* Once the full stack being notified of the changes, let's update the
  137. * bounding box
  138. */
  139. this->updateBoundingBox();
  140. }
  141. void OBJFile::dumpMe(FILE * fp)
  142. {
  143. }
  144. #define MAX_LINE_LENGTH (512)
  145. /* Here start the fun! */
  146. int OBJFile::parseOBJFile(const char *content)
  147. {
  148. /* I don't think we will handle lines of more than 512 characters... */
  149. char lineBuff[MAX_LINE_LENGTH];
  150. uint32_t currentLineNum = 1;
  151. /* Need to process line by line */
  152. const char *bufferPos = content;
  153. const char *lineNewline;
  154. while(*bufferPos != '\0')
  155. {
  156. uint32_t lineLength;
  157. lineNewline = strchr(bufferPos, '\n');
  158. if (lineNewline == nullptr)
  159. {
  160. /* We are on the last line */
  161. lineLength = strlen(bufferPos);
  162. }
  163. else
  164. {
  165. lineLength = (lineNewline - bufferPos);
  166. }
  167. if (lineLength >= MAX_LINE_LENGTH)
  168. {
  169. printf("ERROR: Line %d is too long! (%d)\n", currentLineNum, lineLength);
  170. return -1;
  171. }
  172. memset(lineBuff, 0, MAX_LINE_LENGTH);
  173. strncpy(lineBuff, bufferPos, lineLength);
  174. this->parseLine(lineBuff);
  175. bufferPos += lineLength + 1;
  176. currentLineNum++;
  177. }
  178. return 0;
  179. }
  180. #define MAX_ARGS (15)
  181. /* Parse the line into a couple ofr argc/argv using space as argument separator */
  182. void OBJFile::parseLine(char *line)
  183. {
  184. char *argv[MAX_ARGS];
  185. uint32_t argc = 0;
  186. char *buffer = line;
  187. uint32_t lineLength = strlen(line);
  188. uint32_t linePos = 0;
  189. /* First argument */
  190. argv[argc++] = line;
  191. while(linePos < lineLength)
  192. {
  193. char *next = strchr(buffer, ' ');
  194. if (next != nullptr)
  195. {
  196. *next = '\0';
  197. linePos = next - line;
  198. buffer = next + 1;
  199. argv[argc++] = buffer;
  200. }
  201. else
  202. {
  203. linePos = lineLength;
  204. }
  205. }
  206. if (this->execLine(argc, argv))
  207. {
  208. this->ignoredLines++;
  209. }
  210. }
  211. /* Actually execute the line */
  212. int OBJFile::execLine(int argc, char *argv[])
  213. {
  214. return 1;
  215. }