mesh.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * 3D Engine
  3. * mesh.c:
  4. * Based on pikuma.com 3D software renderer in C
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 04/03/2021.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <log.h>
  14. #include <array.h>
  15. #include <mesh.h>
  16. #define N_CUBE_VERTICES (8)
  17. extern vec3_t cubeVertices[N_CUBE_VERTICES];
  18. #define N_CUBE_FACES (6 * 2)
  19. extern face_t cubeFaces[N_CUBE_FACES];
  20. vec3_t cubeVertices[N_CUBE_VERTICES] =
  21. {
  22. [0] = { .x = -1, .y = -1, .z = -1},
  23. [1] = { .x = -1, .y = 1, .z = -1},
  24. [2] = { .x = 1, .y = 1, .z = -1},
  25. [3] = { .x = 1, .y = -1, .z = -1},
  26. [4] = { .x = 1, .y = 1, .z = 1},
  27. [5] = { .x = 1, .y = -1, .z = 1},
  28. [6] = { .x = -1, .y = 1, .z = 1},
  29. [7] = { .x = -1, .y = -1, .z = 1},
  30. };
  31. face_t cubeFaces[N_CUBE_FACES] =
  32. {
  33. /* Front */
  34. { .a = 1, .b = 2, .c = 3 },
  35. { .a = 1, .b = 3, .c = 4 },
  36. /* Right */
  37. { .a = 4, .b = 3, .c = 5 },
  38. { .a = 4, .b = 5, .c = 6 },
  39. /* Back */
  40. { .a = 6, .b = 5, .c = 7 },
  41. { .a = 6, .b = 7, .c = 8 },
  42. /* Left */
  43. { .a = 8, .b = 7, .c = 2 },
  44. { .a = 8, .b = 2, .c = 1 },
  45. /* Top */
  46. { .a = 2, .b = 7, .c = 5 },
  47. { .a = 2, .b = 5, .c = 3 },
  48. /* Bottom */
  49. { .a = 6, .b = 8, .c = 1 },
  50. { .a = 6, .b = 1, .c = 4 },
  51. };
  52. mesh_t mesh =
  53. {
  54. .vertices = NULL,
  55. .faces = NULL,
  56. .rotation = { 0, 0, 0 },
  57. };
  58. void loadCubeMeshData()
  59. {
  60. int i;
  61. meshFree();
  62. for(i = 0; i < N_CUBE_VERTICES; i++)
  63. {
  64. arrayAdd(mesh.vertices, cubeVertices[i]);
  65. }
  66. for(i = 0; i < N_CUBE_FACES; i++)
  67. {
  68. arrayAdd(mesh.faces, cubeFaces[i]);
  69. }
  70. mesh.rotation.x = 0;
  71. mesh.rotation.y = 0;
  72. mesh.rotation.z = 0;
  73. }
  74. void meshFree()
  75. {
  76. arrayEmpty(mesh.normalVertices);
  77. arrayEmpty(mesh.textureVertices);
  78. arrayFree(mesh.faces);
  79. arrayFree(mesh.vertices);
  80. }
  81. /***********************************************************************************************************************
  82. * OBJ File interpreter based on my project DoRayMe ( https://trac.godzil.net/Godzil/DoRayMe )
  83. **********************************************************************************************************************/
  84. /* Function used to load OBJ files */
  85. static int objFileLoaderParser(const char *content);
  86. static void objFileLoaderLineParser(char *line, uint32_t currentLine);
  87. static int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine);
  88. static uint32_t objFileLoaderIgnoredLines = 0;
  89. void loadOBJFile(const char *filepath)
  90. {
  91. FILE *fp;
  92. size_t fileSize;
  93. char *fileBuff;
  94. fp = fopen(filepath, "rt");
  95. if (fp)
  96. {
  97. fseek(fp, 0, SEEK_END);
  98. fileSize = ftell(fp);
  99. /* Add one byte to the size to make sure it is null terminated */
  100. fileBuff = (char *)calloc(fileSize + 1, 1);
  101. fseek(fp, 0, SEEK_SET);
  102. fileSize = fread(fileBuff, 1, fileSize, fp);
  103. fclose(fp);
  104. objFileLoaderIgnoredLines = 0;
  105. meshFree();
  106. if (objFileLoaderParser(fileBuff))
  107. {
  108. log(TLOG_ERROR, "OBJLoader", "Errors occurred while opening the file '%s'.\n", filepath);
  109. }
  110. free(fileBuff);
  111. }
  112. else
  113. {
  114. log(TLOG_ERROR, "OBJLoader", "Can't open/find the file '%s'.\n", filepath);
  115. }
  116. }
  117. #define MAX_LINE_LENGTH (512)
  118. /* Here start the fun! */
  119. static int objFileLoaderParser(const char *content)
  120. {
  121. /* I don't think we will handle lines of more than 512 characters... */
  122. char lineBuff[MAX_LINE_LENGTH];
  123. uint32_t currentLineNum = 1;
  124. uint32_t totalLength = strlen(content);
  125. /* Need to process line by line */
  126. const char *bufferPos = content;
  127. const char *lineNewline;
  128. while(*bufferPos != '\0')
  129. {
  130. uint32_t lineLength;
  131. lineNewline = strchr(bufferPos, '\n');
  132. if (lineNewline == NULL)
  133. {
  134. /* We are on the last line */
  135. lineLength = strlen(bufferPos);
  136. }
  137. else
  138. {
  139. lineLength = (lineNewline - bufferPos);
  140. }
  141. if (lineLength >= MAX_LINE_LENGTH)
  142. {
  143. log(TLOG_ERROR, "OBJLoader", "Line %d is too long! (%d)", currentLineNum, lineLength);
  144. return -1;
  145. }
  146. memset(lineBuff, 0, MAX_LINE_LENGTH);
  147. strncpy(lineBuff, bufferPos, lineLength);
  148. objFileLoaderLineParser(lineBuff, currentLineNum);
  149. bufferPos += lineLength + 1;
  150. if ((bufferPos - content) >= totalLength)
  151. {
  152. /* We are past the length of the buffer, don't need to continue */
  153. break;
  154. }
  155. currentLineNum++;
  156. }
  157. return 0;
  158. }
  159. #define MAX_ARGS (15)
  160. /* Parse the line into a couple ofr argc/argv using space as argument separator */
  161. static void objFileLoaderLineParser(char *line, uint32_t currentLine)
  162. {
  163. char *argv[MAX_ARGS];
  164. uint32_t argc = 0;
  165. char *buffer = line;
  166. uint32_t lineLength = strlen(line);
  167. uint32_t linePos = 0;
  168. /* First argument */
  169. argv[argc++] = line;
  170. while(linePos < lineLength)
  171. {
  172. char *next = strchr(buffer, ' ');
  173. if (next != NULL)
  174. {
  175. *next = '\0';
  176. linePos = next - line;
  177. buffer = next + 1;
  178. /* Skip empty strings as it mean multiple spaces */
  179. if (strlen(buffer) > 0)
  180. {
  181. argv[argc++] = buffer;
  182. }
  183. }
  184. else
  185. {
  186. linePos = lineLength;
  187. }
  188. }
  189. if (objFileLoaderExecuteLine(argc, argv, currentLine))
  190. {
  191. objFileLoaderIgnoredLines++;
  192. }
  193. }
  194. static int objFileLoaderParseFaceVertex(char *buf, uint32_t *v, uint32_t *vt, uint32_t *vn)
  195. {
  196. uint32_t bufPos = 0;
  197. uint32_t lineLength = strlen(buf);
  198. *vt = INT32_MAX;
  199. *vn = INT32_MAX;
  200. int ret = 0;
  201. int token = 0;
  202. while (bufPos < lineLength)
  203. {
  204. char *next = strchr(buf, '/');
  205. if (next != NULL)
  206. {
  207. *next = '\0';
  208. bufPos = next - buf;
  209. }
  210. else
  211. {
  212. bufPos = lineLength;
  213. }
  214. if (strlen(buf) > 0)
  215. {
  216. switch (token)
  217. {
  218. case 0: *v = atol(buf); break;
  219. case 1: *vt = atol(buf); break;
  220. case 2: *vn = atol(buf); break;
  221. default:
  222. log(TLOG_ERROR, "OBJLoader", "Too many entry for a face vertex!");
  223. ret = 1;
  224. break;
  225. }
  226. }
  227. buf = next + 1;
  228. token++;
  229. }
  230. return ret;
  231. }
  232. /* Actually execute the line */
  233. int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
  234. {
  235. int ret = 1;
  236. if (strncmp(argv[0], "v", 2) == 0)
  237. {
  238. /* Vertice entry */
  239. if (argc != 4)
  240. {
  241. log(TLOG_ERROR, "OBJLoader", "Malformed file at line %d: Vertices expect 3 parameters!\n", currentLine);
  242. }
  243. else
  244. {
  245. vec3_t vertex = { atof(argv[1]), atof(argv[2]), atof(argv[3]) };
  246. arrayAdd(mesh.vertices, vertex);
  247. ret = 0;
  248. }
  249. }
  250. else if (strncmp(argv[0], "vn", 3) == 0)
  251. {
  252. /* Vertice Normal entry */
  253. if (argc != 4)
  254. {
  255. log(TLOG_ERROR, "OBJLoader", "Malformed file at line %d: Vertices normal expect 3 parameters!\n", currentLine);
  256. }
  257. else
  258. {
  259. vec3_t vertex = { atof(argv[1]), atof(argv[2]), atof(argv[3]) };
  260. arrayAdd(mesh.normalVertices, vertex);
  261. ret = 0;
  262. }
  263. }
  264. else if (strncmp(argv[0], "vt", 3) == 0)
  265. {
  266. /* Vertice Normal entry */
  267. if (argc != 3)
  268. {
  269. log(TLOG_ERROR, "OBJLoader", "Malformed file at line %d: Vertices normal expect 2 parameters!\n", currentLine);
  270. }
  271. else
  272. {
  273. vec2_t vertex = { atof(argv[1]), atof(argv[2]) };
  274. arrayAdd(mesh.textureVertices, vertex);
  275. ret = 0;
  276. }
  277. }
  278. else if (strncmp(argv[0], "f", 2) == 0)
  279. {
  280. /* Faces entry */
  281. int i;
  282. uint32_t v[MAX_ARGS], vt[MAX_ARGS], vn[MAX_ARGS];
  283. for(i = 1; i < argc; i++)
  284. {
  285. objFileLoaderParseFaceVertex(argv[i], &v[i], &vt[i], &vn[i]);
  286. }
  287. if (argc == 4)
  288. {
  289. face_t face = {v[1], v[2], v[3] };
  290. arrayAdd(mesh.faces, face);
  291. ret = 0;
  292. }
  293. else if (argc > 4)
  294. {
  295. /* This is not a triangle, so we need to fan it */
  296. for(i = 2; i < (argc - 1); i++)
  297. {
  298. face_t face = {v[1], v[2], v[3] };
  299. arrayAdd(mesh.faces, face);
  300. }
  301. ret = 0;
  302. }
  303. else
  304. {
  305. log(TLOG_ERROR, "OBJLoader", "Malformed file at line %d: Too few/many parameters!\n", currentLine);
  306. }
  307. }
  308. /* We ignore groups for now, may use it later. */
  309. #if 0
  310. else if (strncmp(argv[0], "g", 2) == 0)
  311. {
  312. if (argc == 2)
  313. {
  314. this->addGroup(new Group(argv[1]));
  315. }
  316. else
  317. {
  318. log(TLOG_ERROR, "OBJLoader", "Malformed file at line %d: Too few/many parameters!\n", currentLine);
  319. }
  320. }
  321. #endif
  322. return ret;
  323. }