mesh.c 13 KB

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