mesh.c 13 KB

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