objfile.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #define MIN_ALLOC (2)
  19. #define DEFAULT_GROUP (0)
  20. OBJFile::OBJFile() : Shape(SHAPE_OBJFILE), ignoredLines(0)
  21. {
  22. stats.addOBJFile();
  23. this->allocatedFaceGroupCount = MIN_ALLOC;
  24. this->faceGroupList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
  25. this->faceGroupCount = 0;
  26. this->allocatedVertexCount = MIN_ALLOC;
  27. this->vertexList = (Tuple **)calloc(sizeof(Tuple *), MIN_ALLOC);
  28. this->vertexCount = 0;
  29. /* There is always a default group */
  30. this->addGroup(new Group());
  31. };
  32. OBJFile::OBJFile(const char *filepath) : OBJFile()
  33. {
  34. FILE *fp;
  35. size_t fileSize;
  36. char *fileBuff;
  37. fp = fopen(filepath, "rt");
  38. if (fp)
  39. {
  40. fseek(fp, 0, SEEK_END);
  41. fileSize = ftell(fp);
  42. fileBuff = (char *)calloc(fileSize, 1);
  43. fseek(fp, 0, SEEK_SET);
  44. fileSize = fread(fileBuff, 1, fileSize, fp);
  45. fclose(fp);
  46. this->parseOBJFile(fileBuff);
  47. free(fileBuff);
  48. }
  49. else
  50. {
  51. printf("ERROR: Can't open/find the file '%s'.\n", filepath);
  52. }
  53. }
  54. void OBJFile::addGroup(Shape *group)
  55. {
  56. if ((this->faceGroupCount + 1) > this->allocatedFaceGroupCount)
  57. {
  58. this->allocatedFaceGroupCount *= 2;
  59. this->faceGroupList = (Shape **)realloc(this->faceGroupList, sizeof(Shape **) * this->allocatedFaceGroupCount);
  60. }
  61. group->parent = this;
  62. group->updateTransform();
  63. this->faceGroupList[this->faceGroupCount++] = group;
  64. this->bounds | group->getBounds();
  65. }
  66. void OBJFile::addVertex(Tuple *vertex)
  67. {
  68. if ((this->vertexCount + 1) > this->allocatedVertexCount)
  69. {
  70. this->allocatedVertexCount *= 2;
  71. this->vertexList = (Tuple **)realloc(this->vertexList, sizeof(Tuple **) * this->allocatedVertexCount);
  72. }
  73. this->vertexList[this->vertexCount++] = vertex;
  74. }
  75. Intersect OBJFile::localIntersect(Ray r)
  76. {
  77. }
  78. Tuple OBJFile::localNormalAt(Tuple point)
  79. {
  80. }
  81. BoundingBox OBJFile::getLocalBounds()
  82. {
  83. }
  84. #define MAX_LINE_LENGTH (512)
  85. /* Here start the fun! */
  86. int OBJFile::parseOBJFile(const char *content)
  87. {
  88. /* I don't think we will handle lines of more than 512 characters... */
  89. char lineBuff[MAX_LINE_LENGTH];
  90. uint32_t currentLineNum = 1;
  91. /* Need to process line by line */
  92. const char *bufferPos = content;
  93. const char *lineNewline;
  94. while(*bufferPos != '\0')
  95. {
  96. uint32_t lineLength;
  97. lineNewline = strchr(bufferPos, '\n');
  98. if (lineNewline == nullptr)
  99. {
  100. /* We are on the last line */
  101. lineLength = strlen(bufferPos);
  102. }
  103. else
  104. {
  105. lineLength = (lineNewline - bufferPos);
  106. }
  107. if (lineLength >= MAX_LINE_LENGTH)
  108. {
  109. printf("ERROR: Line %d is too long! (%d)\n", currentLineNum);
  110. }
  111. memset(lineBuff, 0, MAX_LINE_LENGTH);
  112. strncpy(lineBuff, bufferPos, lineLength);
  113. printf("line %d = %s\n", currentLineNum, lineBuff);
  114. bufferPos += lineLength + 1;
  115. currentLineNum++;
  116. }
  117. }