mesh.c 11 KB

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