mesh.c 13 KB

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