objfile.cpp 12 KB

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