mesh.c 12 KB

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