objfile.cpp 11 KB

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