mesh.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. static uint32_t objFileLoaderLoadedVertices = 0;
  90. static uint32_t objFileLoaderLoadedVerticesNormal = 0;
  91. static uint32_t objFileLoaderLoadedVerticesTexture = 0;
  92. static uint32_t objFileLoaderLoadedFaces = 0;
  93. static uint32_t objFileLoaderTotalTri = 0;
  94. void loadOBJFile(const char *filepath)
  95. {
  96. FILE *fp;
  97. size_t fileSize;
  98. char *fileBuff;
  99. fp = fopen(filepath, "rt");
  100. if (fp)
  101. {
  102. fseek(fp, 0, SEEK_END);
  103. fileSize = ftell(fp);
  104. /* Add one byte to the size to make sure it is null terminated */
  105. fileBuff = (char *)calloc(fileSize + 1, 1);
  106. fseek(fp, 0, SEEK_SET);
  107. fileSize = fread(fileBuff, 1, fileSize, fp);
  108. fclose(fp);
  109. objFileLoaderIgnoredLines = 0;
  110. objFileLoaderLoadedVertices = 0;
  111. objFileLoaderLoadedVerticesNormal = 0;
  112. objFileLoaderLoadedVerticesTexture = 0;
  113. objFileLoaderLoadedFaces = 0;
  114. objFileLoaderTotalTri = 0;
  115. meshFree();
  116. if (objFileLoaderParser(fileBuff))
  117. {
  118. log(TLOG_ERROR, "OBJLoader", "Errors occurred while opening the file '%s'.", filepath);
  119. }
  120. log(TLOG_VERBOSE, "OBJLoader", "OBJ File '%s' loaded. Some stats:", filepath);
  121. log(TLOG_VERBOSE, "OBJLoader", "Total ignored lines : %lu", objFileLoaderIgnoredLines);
  122. log(TLOG_VERBOSE, "OBJLoader", "Number of vertices : %lu", objFileLoaderLoadedVertices);
  123. log(TLOG_VERBOSE, "OBJLoader", "Number of normal vertices : %lu", objFileLoaderLoadedVerticesNormal);
  124. log(TLOG_VERBOSE, "OBJLoader", "Number of texture vertices : %lu", objFileLoaderLoadedVerticesTexture);
  125. log(TLOG_VERBOSE, "OBJLoader", "Number of faces in file : %lu", objFileLoaderLoadedFaces);
  126. log(TLOG_VERBOSE, "OBJLoader", "Number of created triangles : %lu", objFileLoaderTotalTri);
  127. free(fileBuff);
  128. }
  129. else
  130. {
  131. log(TLOG_ERROR, "OBJLoader", "Can't open/find the file '%s'.", filepath);
  132. }
  133. }
  134. #define MAX_LINE_LENGTH (512)
  135. /* Here start the fun! */
  136. static int objFileLoaderParser(const char *content)
  137. {
  138. /* I don't think we will handle lines of more than 512 characters... */
  139. char lineBuff[MAX_LINE_LENGTH];
  140. uint32_t currentLineNum = 1;
  141. uint32_t totalLength = strlen(content);
  142. /* Need to process line by line */
  143. const char *bufferPos = content;
  144. const char *lineNewline;
  145. while(*bufferPos != '\0')
  146. {
  147. uint32_t lineLength;
  148. lineNewline = strchr(bufferPos, '\n');
  149. if (lineNewline == NULL)
  150. {
  151. /* We are on the last line */
  152. lineLength = strlen(bufferPos);
  153. }
  154. else
  155. {
  156. lineLength = (lineNewline - bufferPos);
  157. }
  158. if (lineLength >= MAX_LINE_LENGTH)
  159. {
  160. log(TLOG_ERROR, "OBJLoader", "Line %d is too long! (%d)", currentLineNum, lineLength);
  161. return -1;
  162. }
  163. memset(lineBuff, 0, MAX_LINE_LENGTH);
  164. strncpy(lineBuff, bufferPos, lineLength);
  165. objFileLoaderLineParser(lineBuff, currentLineNum);
  166. bufferPos += lineLength + 1;
  167. if ((bufferPos - content) >= totalLength)
  168. {
  169. /* We are past the length of the buffer, don't need to continue */
  170. break;
  171. }
  172. currentLineNum++;
  173. }
  174. return 0;
  175. }
  176. #define MAX_ARGS (15)
  177. /* Parse the line into a couple ofr argc/argv using space as argument separator */
  178. static void objFileLoaderLineParser(char *line, uint32_t currentLine)
  179. {
  180. char *argv[MAX_ARGS];
  181. uint32_t argc = 0;
  182. char *buffer = line;
  183. uint32_t lineLength = strlen(line);
  184. uint32_t linePos = 0;
  185. /* First argument */
  186. argv[argc++] = line;
  187. while(linePos < lineLength)
  188. {
  189. char *next = strchr(buffer, ' ');
  190. if (next != NULL)
  191. {
  192. *next = '\0';
  193. linePos = next - line;
  194. buffer = next + 1;
  195. /* Skip empty strings as it mean multiple spaces */
  196. if (strlen(buffer) > 0)
  197. {
  198. argv[argc++] = buffer;
  199. }
  200. }
  201. else
  202. {
  203. linePos = lineLength;
  204. }
  205. }
  206. if (objFileLoaderExecuteLine(argc, argv, currentLine))
  207. {
  208. objFileLoaderIgnoredLines++;
  209. }
  210. }
  211. static int objFileLoaderParseFaceVertex(char *buf, uint32_t *v, uint32_t *vt, uint32_t *vn)
  212. {
  213. uint32_t bufPos = 0;
  214. uint32_t lineLength = strlen(buf);
  215. *vt = INT32_MAX;
  216. *vn = INT32_MAX;
  217. int ret = 0;
  218. int token = 0;
  219. while (bufPos < lineLength)
  220. {
  221. char *next = strchr(buf, '/');
  222. if (next != NULL)
  223. {
  224. *next = '\0';
  225. bufPos = next - buf;
  226. }
  227. else
  228. {
  229. bufPos = lineLength;
  230. }
  231. if (strlen(buf) > 0)
  232. {
  233. switch (token)
  234. {
  235. case 0: *v = atol(buf); break;
  236. case 1: *vt = atol(buf); break;
  237. case 2: *vn = atol(buf); break;
  238. default:
  239. log(TLOG_ERROR, "OBJLoader", "Too many entry for a face vertex!");
  240. ret = 1;
  241. break;
  242. }
  243. }
  244. buf = next + 1;
  245. token++;
  246. }
  247. return ret;
  248. }
  249. /* Actually execute the line */
  250. int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
  251. {
  252. int ret = 1;
  253. if (strncmp(argv[0], "v", 2) == 0)
  254. {
  255. /* Vertice entry */
  256. if (argc != 4)
  257. {
  258. log(TLOG_ERROR, "OBJLoader", "Malformed file at line %d: Vertices expect 3 parameters!", currentLine);
  259. }
  260. else
  261. {
  262. vec3_t vertex = { atof(argv[1]), atof(argv[2]), atof(argv[3]) };
  263. arrayAdd(mesh.vertices, vertex);
  264. objFileLoaderLoadedVertices++;
  265. ret = 0;
  266. }
  267. }
  268. else if (strncmp(argv[0], "vn", 3) == 0)
  269. {
  270. /* Vertice Normal entry */
  271. if (argc != 4)
  272. {
  273. log(TLOG_ERROR, "OBJLoader", "Malformed file at line %d: Vertices normal expect 3 parameters!", currentLine);
  274. }
  275. else
  276. {
  277. vec3_t vertex = { atof(argv[1]), atof(argv[2]), atof(argv[3]) };
  278. arrayAdd(mesh.normalVertices, vertex);
  279. objFileLoaderLoadedVerticesNormal++;
  280. ret = 0;
  281. }
  282. }
  283. else if (strncmp(argv[0], "vt", 3) == 0)
  284. {
  285. /* Vertice Normal entry */
  286. if ((argc < 3) || (argc > 4))
  287. {
  288. log(TLOG_ERROR, "OBJLoader", "Malformed file at line %d: Vertices texture expect 2 or 3 parameters!", currentLine);
  289. }
  290. else
  291. {
  292. vec2_t vertex = { atof(argv[1]), atof(argv[2]) };
  293. arrayAdd(mesh.textureVertices, vertex);
  294. objFileLoaderLoadedVerticesTexture++;
  295. ret = 0;
  296. }
  297. }
  298. else if (strncmp(argv[0], "f", 2) == 0)
  299. {
  300. /* Faces entry */
  301. int i;
  302. uint32_t v[MAX_ARGS], vt[MAX_ARGS], vn[MAX_ARGS];
  303. for(i = 1; i < argc; i++)
  304. {
  305. objFileLoaderParseFaceVertex(argv[i], &v[i], &vt[i], &vn[i]);
  306. }
  307. if (argc == 4)
  308. {
  309. objFileLoaderTotalTri++;
  310. objFileLoaderLoadedFaces++;
  311. face_t face = { v[1], v[2], v[3] };
  312. arrayAdd(mesh.faces, face);
  313. ret = 0;
  314. }
  315. else if (argc > 4)
  316. {
  317. objFileLoaderLoadedFaces++;
  318. /* This is not a triangle, so we need to fan it */
  319. for(i = 2; i < (argc - 1); i++)
  320. {
  321. face_t face = { v[1], v[i], v[i + 1] };
  322. arrayAdd(mesh.faces, face);
  323. objFileLoaderTotalTri++;
  324. }
  325. ret = 0;
  326. }
  327. else
  328. {
  329. log(TLOG_ERROR, "OBJLoader", "Malformed file at line %d: Too few/many parameters!", currentLine);
  330. }
  331. }
  332. /* We ignore groups for now, may use it later. */
  333. #if 0
  334. else if (strncmp(argv[0], "g", 2) == 0)
  335. {
  336. if (argc == 2)
  337. {
  338. this->addGroup(new Group(argv[1]));
  339. }
  340. else
  341. {
  342. log(TLOG_ERROR, "OBJLoader", "Malformed file at line %d: Too few/many parameters!", currentLine);
  343. }
  344. }
  345. #endif
  346. return ret;
  347. }