mesh.c 12 KB

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