objfile.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. #include <smoothtriangle.h>
  20. #define MIN_ALLOC (2)
  21. #define DEFAULT_GROUP (0)
  22. OBJFile::OBJFile() : Shape(SHAPE_OBJFILE), ignoredLines(0)
  23. {
  24. stats.addOBJFile();
  25. this->allocatedFaceGroupCount = MIN_ALLOC;
  26. this->faceGroupList = (Group **)calloc(sizeof(Group **), MIN_ALLOC);
  27. this->faceGroupCount = 0;
  28. this->allocatedVertexCount = MIN_ALLOC;
  29. this->vertexList = (Point **)calloc(sizeof(Point **), MIN_ALLOC);
  30. this->vertexCount = 0;
  31. this->allocatedVertexNormalCount = MIN_ALLOC;
  32. this->vertexNormalList = (Vector **)calloc(sizeof(Vector **), MIN_ALLOC);
  33. this->vertexNormalCount = 0;
  34. /* There is always a default group */
  35. this->addGroup(new Group());
  36. };
  37. OBJFile::OBJFile(const char *filepath) : OBJFile()
  38. {
  39. FILE *fp;
  40. size_t fileSize;
  41. char *fileBuff;
  42. fp = fopen(filepath, "rt");
  43. if (fp)
  44. {
  45. fseek(fp, 0, SEEK_END);
  46. fileSize = ftell(fp);
  47. /* Add one byte to the size to make sure it is null terminated */
  48. fileBuff = (char *)calloc(fileSize + 1, 1);
  49. fseek(fp, 0, SEEK_SET);
  50. fileSize = fread(fileBuff, 1, fileSize, fp);
  51. fclose(fp);
  52. this->parseOBJFile(fileBuff);
  53. free(fileBuff);
  54. }
  55. else
  56. {
  57. printf("ERROR: Can't open/find the file '%s'.\n", filepath);
  58. }
  59. }
  60. void OBJFile::addGroup(Group *group)
  61. {
  62. if ((this->faceGroupCount + 1) > this->allocatedFaceGroupCount)
  63. {
  64. this->allocatedFaceGroupCount *= 2;
  65. this->faceGroupList = (Group **)realloc(this->faceGroupList, sizeof(Group **) * this->allocatedFaceGroupCount);
  66. }
  67. group->parent = this;
  68. group->updateTransform();
  69. this->faceGroupList[this->faceGroupCount++] = group;
  70. this->bounds | group->getBounds();
  71. }
  72. void OBJFile::addVertex(Point *vertex)
  73. {
  74. if ((this->vertexCount + 1) > this->allocatedVertexCount)
  75. {
  76. this->allocatedVertexCount *= 2;
  77. this->vertexList = (Point **)realloc(this->vertexList, sizeof(Point **) * this->allocatedVertexCount);
  78. }
  79. this->vertexList[this->vertexCount++] = vertex;
  80. }
  81. void OBJFile::addVertexNormal(Vector *vertexNormal)
  82. {
  83. if ((this->vertexNormalCount + 1) > this->allocatedVertexNormalCount)
  84. {
  85. this->allocatedVertexNormalCount *= 2;
  86. this->vertexNormalList = (Vector **)realloc(this->vertexNormalList, sizeof(Vector **) * this->allocatedVertexNormalCount);
  87. }
  88. this->vertexNormalList[this->vertexNormalCount++] = vertexNormal;
  89. }
  90. Intersect OBJFile::intersect(Ray r)
  91. {
  92. Intersect ret;
  93. int i, j;
  94. if (this->faceGroupCount > 0)
  95. {
  96. if (this->bounds.intesectMe(r))
  97. {
  98. for (i = 0 ; i < this->faceGroupCount ; i++)
  99. {
  100. Intersect xs = this->faceGroupList[i]->intersect(r);
  101. if (xs.count() > 0)
  102. {
  103. for (j = 0 ; j < xs.count() ; j++)
  104. {
  105. ret.add(xs[j]);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. return ret;
  112. }
  113. bool OBJFile::includes(Shape *b)
  114. {
  115. int i;
  116. if (this->faceGroupCount > 0)
  117. {
  118. for (i = 0 ; i < this->faceGroupCount ; i++)
  119. {
  120. if (this->faceGroupList[i] == b)
  121. {
  122. return true;
  123. }
  124. }
  125. }
  126. return false;
  127. }
  128. Intersect OBJFile::localIntersect(Ray r)
  129. {
  130. return Intersect();
  131. }
  132. Tuple OBJFile::localNormalAt(Tuple point, Intersection *hit)
  133. {
  134. return Vector(0, 1, 0);
  135. }
  136. BoundingBox OBJFile::getLocalBounds()
  137. {
  138. return this->bounds;
  139. }
  140. BoundingBox OBJFile::getBounds()
  141. {
  142. if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
  143. return this->bounds;
  144. }
  145. void OBJFile::updateBoundingBox()
  146. {
  147. int i;
  148. this->bounds.reset();
  149. for(i = 0; i < this->faceGroupCount; i++)
  150. {
  151. if (this->faceGroupList[i]->haveFiniteBounds())
  152. {
  153. BoundingBox objB = this->faceGroupList[i]->getBounds();
  154. this->bounds | objB;
  155. }
  156. }
  157. }
  158. void OBJFile::updateTransform()
  159. {
  160. int i;
  161. Shape::updateTransform();
  162. for (i = 0 ; i < this->faceGroupCount ; i++)
  163. {
  164. this->faceGroupList[i]->updateTransform();
  165. }
  166. /* Once the full stack being notified of the changes, let's update the
  167. * bounding box
  168. */
  169. this->updateBoundingBox();
  170. }
  171. void OBJFile::dumpMe(FILE * fp)
  172. {
  173. int i;
  174. fprintf(fp, "\"Type\": \"OBJFile\",\n");
  175. fprintf(fp, "\"Objects\": {\n");
  176. for(i = 0; i < this->faceGroupCount; i++)
  177. {
  178. fprintf(fp, "\"%d\": {\n", i);
  179. this->faceGroupList[i]->dumpMe(fp);
  180. fprintf(fp, "},\n");
  181. }
  182. fprintf(fp, "},\n");
  183. Shape::dumpMe(fp);
  184. }
  185. #define MAX_LINE_LENGTH (512)
  186. /* Here start the fun! */
  187. int OBJFile::parseOBJFile(const char *content)
  188. {
  189. /* I don't think we will handle lines of more than 512 characters... */
  190. char lineBuff[MAX_LINE_LENGTH];
  191. uint32_t currentLineNum = 1;
  192. uint32_t totalLength = strlen(content);
  193. /* Need to process line by line */
  194. const char *bufferPos = content;
  195. const char *lineNewline;
  196. while(*bufferPos != '\0')
  197. {
  198. uint32_t lineLength;
  199. lineNewline = strchr(bufferPos, '\n');
  200. if (lineNewline == nullptr)
  201. {
  202. /* We are on the last line */
  203. lineLength = strlen(bufferPos);
  204. }
  205. else
  206. {
  207. lineLength = (lineNewline - bufferPos);
  208. }
  209. if (lineLength >= MAX_LINE_LENGTH)
  210. {
  211. printf("ERROR: Line %d is too long! (%d)\n", currentLineNum, lineLength);
  212. return -1;
  213. }
  214. memset(lineBuff, 0, MAX_LINE_LENGTH);
  215. strncpy(lineBuff, bufferPos, lineLength);
  216. this->parseLine(lineBuff, currentLineNum);
  217. bufferPos += lineLength + 1;
  218. if ((bufferPos - content) >= totalLength)
  219. {
  220. /* We are past the length of the buffer, don't need to continue */
  221. break;
  222. }
  223. currentLineNum++;
  224. }
  225. return 0;
  226. }
  227. #define MAX_ARGS (15)
  228. /* Parse the line into a couple ofr argc/argv using space as argument separator */
  229. void OBJFile::parseLine(char *line, uint32_t currentLine)
  230. {
  231. char *argv[MAX_ARGS];
  232. uint32_t argc = 0;
  233. char *buffer = line;
  234. uint32_t lineLength = strlen(line);
  235. uint32_t linePos = 0;
  236. /* First argument */
  237. argv[argc++] = line;
  238. while(linePos < lineLength)
  239. {
  240. char *next = strchr(buffer, ' ');
  241. if (next != nullptr)
  242. {
  243. *next = '\0';
  244. linePos = next - line;
  245. buffer = next + 1;
  246. /* Skip empty strings as it mean multiple spaces */
  247. if (strlen(buffer) > 0)
  248. {
  249. argv[argc++] = buffer;
  250. }
  251. }
  252. else
  253. {
  254. linePos = lineLength;
  255. }
  256. }
  257. if (this->execLine(argc, argv, currentLine))
  258. {
  259. this->ignoredLines++;
  260. }
  261. }
  262. static int parseFaceVertex(char *buf, uint32_t &v, uint32_t &vt, uint32_t &vn)
  263. {
  264. uint32_t bufPos = 0;
  265. uint32_t lineLength = strlen(buf);
  266. vt = INT32_MAX;
  267. vn = INT32_MAX;
  268. int ret = 0;
  269. int token = 0;
  270. while(bufPos < lineLength)
  271. {
  272. char *next = strchr(buf, '/');
  273. if (next != nullptr)
  274. {
  275. *next = '\0';
  276. bufPos = next - buf;
  277. }
  278. else
  279. {
  280. bufPos = lineLength;
  281. }
  282. if (strlen(buf) > 0)
  283. {
  284. switch(token)
  285. {
  286. case 0: v = atol(buf); break;
  287. case 1: vt = atol(buf); break;
  288. case 2: vn = atol(buf); break;
  289. default: printf("ERROR: Too many entry for a face vertice!"); ret = 1;
  290. }
  291. }
  292. buf = next + 1;
  293. token++;
  294. }
  295. return ret;
  296. }
  297. /* Actually execute the line */
  298. int OBJFile::execLine(int argc, char *argv[], uint32_t currentLine)
  299. {
  300. int ret = 1;
  301. if (strncmp(argv[0], "v", 2) == 0)
  302. {
  303. /* Vertice entry */
  304. if (argc != 4)
  305. {
  306. printf("ERROR: Malformed file at line %d: Vertices expect 3 parameters!\n", currentLine);
  307. }
  308. else
  309. {
  310. this->addVertex(new Point(atof(argv[1]), atof(argv[2]), atof(argv[3])));
  311. ret = 0;
  312. }
  313. }
  314. else if (strncmp(argv[0], "vn", 3) == 0)
  315. {
  316. /* Vertice Normal entry */
  317. if (argc != 4)
  318. {
  319. printf("ERROR: Malformed file at line %d: Vertices normal expect 3 parameters!\n", currentLine);
  320. }
  321. else
  322. {
  323. this->addVertexNormal(new Vector(atof(argv[1]), atof(argv[2]), atof(argv[3])));
  324. ret = 0;
  325. }
  326. }
  327. else if (strncmp(argv[0], "f", 2) == 0)
  328. {
  329. /* Faces entry */
  330. int i;
  331. uint32_t v[MAX_ARGS], vt[MAX_ARGS], vn[MAX_ARGS];
  332. for(i = 1; i < argc; i++)
  333. {
  334. parseFaceVertex(argv[i], v[i], vt[i], vn[i]);
  335. }
  336. if (argc == 4)
  337. {
  338. Shape *t;
  339. if (vn[1] == INT32_MAX)
  340. {
  341. t = new Triangle(this->vertices(v[1]),
  342. this->vertices(v[2]),
  343. this->vertices(v[3]));
  344. }
  345. else
  346. {
  347. t = new SmoothTriangle(this->vertices(v[1]),
  348. this->vertices(v[2]),
  349. this->vertices(v[3]),
  350. this->verticesNormal(vn[1]),
  351. this->verticesNormal(vn[2]),
  352. this->verticesNormal(vn[3]));
  353. }
  354. this->faceGroupList[this->faceGroupCount - 1]->addObject(t);
  355. ret = 0;
  356. }
  357. else if (argc > 4)
  358. {
  359. for(i = 2; i < (argc - 1); i++)
  360. {
  361. Shape *t;
  362. if (vn[1] == INT32_MAX)
  363. {
  364. t = new Triangle(this->vertices(v[1]),
  365. this->vertices(v[i]),
  366. this->vertices(v[i + 1]));
  367. }
  368. else
  369. {
  370. t = new SmoothTriangle(this->vertices(v[1]),
  371. this->vertices(v[i]),
  372. this->vertices(v[i + 1]),
  373. this->verticesNormal(vn[1]),
  374. this->verticesNormal(vn[i]),
  375. this->verticesNormal(vn[i + 1]));
  376. }
  377. this->faceGroupList[this->faceGroupCount - 1]->addObject(t);
  378. }
  379. ret = 0;
  380. }
  381. else
  382. {
  383. printf("ERROR: Malformed file at line %d: Too few/many parameters!\n", currentLine);
  384. }
  385. }
  386. else if (strncmp(argv[0], "g", 2) == 0)
  387. {
  388. if (argc == 2)
  389. {
  390. this->addGroup(new Group());
  391. }
  392. else
  393. {
  394. printf("ERROR: Malformed file at line %d: Too few/many parameters!\n", currentLine);
  395. }
  396. }
  397. return ret;
  398. }