objfile.cpp 12 KB

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