objfile.cpp 11 KB

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