objfile.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include <triangle.h>
  19. #define MIN_ALLOC (2)
  20. #define DEFAULT_GROUP (0)
  21. OBJFile::OBJFile() : Shape(SHAPE_OBJFILE), ignoredLines(0)
  22. {
  23. stats.addOBJFile();
  24. this->allocatedFaceGroupCount = MIN_ALLOC;
  25. this->faceGroupList = (Group **)calloc(sizeof(Group **), MIN_ALLOC);
  26. this->faceGroupCount = 0;
  27. this->allocatedVertexCount = MIN_ALLOC;
  28. this->vertexList = (Point **)calloc(sizeof(Point **), MIN_ALLOC);
  29. this->vertexCount = 0;
  30. /* There is always a default group */
  31. this->addGroup(new Group());
  32. };
  33. OBJFile::OBJFile(const char *filepath) : OBJFile()
  34. {
  35. FILE *fp;
  36. size_t fileSize;
  37. char *fileBuff;
  38. fp = fopen(filepath, "rt");
  39. if (fp)
  40. {
  41. fseek(fp, 0, SEEK_END);
  42. fileSize = ftell(fp);
  43. fileBuff = (char *)calloc(fileSize, 1);
  44. fseek(fp, 0, SEEK_SET);
  45. fileSize = fread(fileBuff, 1, fileSize, fp);
  46. fclose(fp);
  47. this->parseOBJFile(fileBuff);
  48. free(fileBuff);
  49. }
  50. else
  51. {
  52. printf("ERROR: Can't open/find the file '%s'.\n", filepath);
  53. }
  54. }
  55. void OBJFile::addGroup(Group *group)
  56. {
  57. if ((this->faceGroupCount + 1) > this->allocatedFaceGroupCount)
  58. {
  59. this->allocatedFaceGroupCount *= 2;
  60. this->faceGroupList = (Group **)realloc(this->faceGroupList, sizeof(Group **) * this->allocatedFaceGroupCount);
  61. }
  62. group->parent = this;
  63. group->updateTransform();
  64. this->faceGroupList[this->faceGroupCount++] = group;
  65. this->bounds | group->getBounds();
  66. }
  67. void OBJFile::addVertex(Point *vertex)
  68. {
  69. if ((this->vertexCount + 1) > this->allocatedVertexCount)
  70. {
  71. this->allocatedVertexCount *= 2;
  72. this->vertexList = (Point **)realloc(this->vertexList, sizeof(Point **) * this->allocatedVertexCount);
  73. }
  74. this->vertexList[this->vertexCount++] = vertex;
  75. }
  76. Intersect OBJFile::intersect(Ray r)
  77. {
  78. Intersect ret;
  79. int i, j;
  80. if (this->faceGroupCount > 0)
  81. {
  82. //if (this->bounds.intesectMe(r))
  83. {
  84. for (i = 0 ; i < this->faceGroupCount ; i++)
  85. {
  86. Intersect xs = this->faceGroupList[i]->intersect(r);
  87. if (xs.count() > 0)
  88. {
  89. for (j = 0 ; j < xs.count() ; j++)
  90. {
  91. ret.add(xs[j]);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. return ret;
  98. }
  99. Intersect OBJFile::localIntersect(Ray r)
  100. {
  101. return Intersect();
  102. }
  103. Tuple OBJFile::localNormalAt(Tuple point)
  104. {
  105. return Vector(0, 1, 0);
  106. }
  107. BoundingBox OBJFile::getLocalBounds()
  108. {
  109. return this->bounds;
  110. }
  111. BoundingBox OBJFile::getBounds()
  112. {
  113. if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
  114. return this->bounds;
  115. }
  116. void OBJFile::updateBoundingBox()
  117. {
  118. int i;
  119. this->bounds.reset();
  120. for(i = 0; i < this->faceGroupCount; i++)
  121. {
  122. if (this->faceGroupList[i]->haveFiniteBounds())
  123. {
  124. BoundingBox objB = this->faceGroupList[i]->getBounds();
  125. this->bounds | objB;
  126. }
  127. }
  128. }
  129. void OBJFile::updateTransform()
  130. {
  131. int i;
  132. Shape::updateTransform();
  133. for (i = 0 ; i < this->faceGroupCount ; i++)
  134. {
  135. this->faceGroupList[i]->updateTransform();
  136. }
  137. /* Once the full stack being notified of the changes, let's update the
  138. * bounding box
  139. */
  140. this->updateBoundingBox();
  141. }
  142. void OBJFile::dumpMe(FILE * fp)
  143. {
  144. int i;
  145. fprintf(fp, "\"Type\": \"OBJFile\",\n");
  146. fprintf(fp, "\"Objects\": {\n");
  147. for(i = 0; i < this->faceGroupCount; i++)
  148. {
  149. fprintf(fp, "\"%d\": {\n", i);
  150. this->faceGroupList[i]->dumpMe(fp);
  151. fprintf(fp, "},\n");
  152. }
  153. fprintf(fp, "},\n");
  154. Shape::dumpMe(fp);
  155. }
  156. #define MAX_LINE_LENGTH (512)
  157. /* Here start the fun! */
  158. int OBJFile::parseOBJFile(const char *content)
  159. {
  160. /* I don't think we will handle lines of more than 512 characters... */
  161. char lineBuff[MAX_LINE_LENGTH];
  162. uint32_t currentLineNum = 1;
  163. /* Need to process line by line */
  164. const char *bufferPos = content;
  165. const char *lineNewline;
  166. while(*bufferPos != '\0')
  167. {
  168. uint32_t lineLength;
  169. lineNewline = strchr(bufferPos, '\n');
  170. if (lineNewline == nullptr)
  171. {
  172. /* We are on the last line */
  173. lineLength = strlen(bufferPos);
  174. }
  175. else
  176. {
  177. lineLength = (lineNewline - bufferPos);
  178. }
  179. if (lineLength >= MAX_LINE_LENGTH)
  180. {
  181. printf("ERROR: Line %d is too long! (%d)\n", currentLineNum, lineLength);
  182. return -1;
  183. }
  184. memset(lineBuff, 0, MAX_LINE_LENGTH);
  185. strncpy(lineBuff, bufferPos, lineLength);
  186. this->parseLine(lineBuff, currentLineNum);
  187. bufferPos += lineLength + 1;
  188. currentLineNum++;
  189. }
  190. return 0;
  191. }
  192. #define MAX_ARGS (15)
  193. /* Parse the line into a couple ofr argc/argv using space as argument separator */
  194. void OBJFile::parseLine(char *line, uint32_t currentLine)
  195. {
  196. char *argv[MAX_ARGS];
  197. uint32_t argc = 0;
  198. char *buffer = line;
  199. uint32_t lineLength = strlen(line);
  200. uint32_t linePos = 0;
  201. /* First argument */
  202. argv[argc++] = line;
  203. while(linePos < lineLength)
  204. {
  205. char *next = strchr(buffer, ' ');
  206. if (next != nullptr)
  207. {
  208. *next = '\0';
  209. linePos = next - line;
  210. buffer = next + 1;
  211. /* Skip empty strings as it mean multiple spaces */
  212. if (strlen(buffer) > 0)
  213. {
  214. argv[argc++] = buffer;
  215. }
  216. }
  217. else
  218. {
  219. linePos = lineLength;
  220. }
  221. }
  222. if (this->execLine(argc, argv, currentLine))
  223. {
  224. this->ignoredLines++;
  225. }
  226. }
  227. /* Actually execute the line */
  228. int OBJFile::execLine(int argc, char *argv[], uint32_t currentLine)
  229. {
  230. int ret = 1;
  231. if (strncmp(argv[0], "v", 1) == 0)
  232. {
  233. /* Vertice entry */
  234. if (argc != 4)
  235. {
  236. printf("ERROR: Malformed file at line %d: Vertices expect 3 parameters!\n", currentLine);
  237. }
  238. else
  239. {
  240. this->addVertex(new Point(atof(argv[1]), atof(argv[2]), atof(argv[3])));
  241. ret = 0;
  242. }
  243. }
  244. else if (strncmp(argv[0], "f", 1) == 0)
  245. {
  246. /* Vertice entry */
  247. if (argc == 4)
  248. {
  249. Shape *t = new Triangle(this->vertices(atoi(argv[1])),
  250. this->vertices(atoi(argv[2])),
  251. this->vertices(atoi(argv[3])));
  252. this->faceGroupList[this->faceGroupCount - 1]->addObject(t);
  253. ret = 0;
  254. }
  255. else if (argc > 4)
  256. {
  257. int i;
  258. for(i = 2; i < (argc - 1); i++)
  259. {
  260. Shape *t = new Triangle(this->vertices(atoi(argv[1])),
  261. this->vertices(atoi(argv[i])),
  262. this->vertices(atoi(argv[i+1])));
  263. this->faceGroupList[this->faceGroupCount - 1]->addObject(t);
  264. }
  265. ret = 0;
  266. }
  267. else
  268. {
  269. printf("ERROR: Malformed file at line %d: Too few/many parameters!\n", currentLine);
  270. }
  271. }
  272. else if (strncmp(argv[0], "g", 1) == 0)
  273. {
  274. if (argc == 2)
  275. {
  276. this->addGroup(new Group());
  277. }
  278. else
  279. {
  280. printf("ERROR: Malformed file at line %d: Too few/many parameters!\n", currentLine);
  281. }
  282. }
  283. return ret;
  284. }