main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * 3D Engine
  3. * main.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 01/03/2021.
  8. */
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <stdbool.h>
  12. #include <SDL.h>
  13. #include <log.h>
  14. #include <array.h>
  15. #include <triangle.h>
  16. #include <display.h>
  17. #include <vector.h>
  18. #include <mesh.h>
  19. bool isRunning = true;
  20. int previousFrameTime = 0;
  21. typedef enum displayMode_t
  22. {
  23. DISPLAY_MODE_WIREFRAME,
  24. DISPLAY_MODE_SOLID,
  25. DISPLAY_MODE_BOTH,
  26. } displayMode_t;
  27. bool displayVertex = false;
  28. bool doNotCull = false;
  29. enum displayMode_t currentDisplayMode = DISPLAY_MODE_SOLID;
  30. vec3_t cameraPosition =
  31. {
  32. .x = 0,
  33. .y = 0,
  34. .z = 0,
  35. };
  36. triangle_t *projectedTriangles = NULL;
  37. double fovFactor = 640;
  38. void setup()
  39. {
  40. frameBuffer = (uint32_t *)calloc(windowWidth * windowHeight, sizeof(uint32_t));
  41. if (!frameBuffer)
  42. {
  43. log(TLOG_PANIC, NULL, "Memory allocation error.");
  44. goto exit;
  45. }
  46. frameBufferTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, windowWidth,
  47. windowHeight);
  48. if (frameBufferTexture == NULL)
  49. {
  50. log(TLOG_PANIC, NULL, "SDL Texture creation error: %s", SDL_GetError());
  51. goto exit;
  52. }
  53. loadOBJFile("assets/f22.obj");
  54. exit:
  55. return;
  56. }
  57. void processInput()
  58. {
  59. SDL_Event event;
  60. SDL_PollEvent(&event);
  61. switch (event.type)
  62. {
  63. case SDL_QUIT:isRunning = false;
  64. break;
  65. case SDL_KEYDOWN:
  66. switch(event.key.keysym.sym)
  67. {
  68. case SDLK_ESCAPE:
  69. isRunning = false;
  70. break;
  71. case SDLK_1:
  72. displayVertex = true;
  73. currentDisplayMode = DISPLAY_MODE_WIREFRAME;
  74. break;
  75. case SDLK_2:
  76. displayVertex = false;
  77. currentDisplayMode = DISPLAY_MODE_WIREFRAME;
  78. break;
  79. case SDLK_3:
  80. displayVertex = false;
  81. currentDisplayMode = DISPLAY_MODE_SOLID;
  82. break;
  83. case SDLK_4:
  84. displayVertex = false;
  85. currentDisplayMode = DISPLAY_MODE_BOTH;
  86. break;
  87. case SDLK_c:
  88. doNotCull = false;
  89. break;
  90. case SDLK_d:
  91. doNotCull = true;
  92. break;
  93. }
  94. break;
  95. default:
  96. break;
  97. }
  98. }
  99. vec2_t orthographicPointProjection(vec3_t point)
  100. {
  101. vec2_t ret;
  102. ret.x = point.x * fovFactor;
  103. ret.y = point.y * fovFactor;
  104. return ret;
  105. }
  106. vec2_t perspectivePointProjection(vec3_t point)
  107. {
  108. vec2_t ret;
  109. double divisor = point.z;
  110. if (divisor == 0) { divisor += 0.00001; }
  111. ret.x = (point.x / divisor) * (fovFactor/2.);
  112. ret.y = (point.y / divisor) * (fovFactor/2.);
  113. return ret;
  114. }
  115. void update()
  116. {
  117. uint32_t i, j;
  118. uint32_t faceCount;
  119. uint32_t timeToWait = FRAME_TARGET_TIME - (SDL_GetTicks() - previousFrameTime);
  120. if (timeToWait <= FRAME_TARGET_TIME)
  121. {
  122. SDL_Delay(timeToWait);
  123. }
  124. previousFrameTime = SDL_GetTicks();
  125. mesh.rotation.x += 0.01;
  126. mesh.rotation.y += 0.01;
  127. mesh.rotation.z += 0.02;
  128. arrayEmpty(projectedTriangles);
  129. faceCount = arrayGetSize(mesh.faces);
  130. for(i = 0; i < faceCount; i++)
  131. {
  132. triangle_t currentTriangle;
  133. vec3_t transformed[3];
  134. vec3_t vertices[3] =
  135. {
  136. mesh.vertices[mesh.faces[i].a - 1],
  137. mesh.vertices[mesh.faces[i].b - 1],
  138. mesh.vertices[mesh.faces[i].c - 1],
  139. };
  140. for(j = 0; j < 3; j++)
  141. {
  142. transformed[j] = vertices[j];
  143. transformed[j] = vec3RotateX(transformed[j], mesh.rotation.x);
  144. transformed[j] = vec3RotateY(transformed[j], mesh.rotation.y);
  145. transformed[j] = vec3RotateZ(transformed[j], mesh.rotation.z);
  146. transformed[j].z += 5;
  147. }
  148. if (!doNotCull)
  149. {
  150. vec3_t vectorBA = vec3SubVectors(transformed[1], transformed[0]);
  151. vec3_t vectorCA = vec3SubVectors(transformed[2], transformed[0]);
  152. vec3Normalize(&vectorBA);
  153. vec3Normalize(&vectorCA);
  154. vec3_t triangleNormal = vec3Cross(vectorBA, vectorCA);
  155. vec3Normalize(&triangleNormal);
  156. vec3_t cameraRay = vec3SubVectors(cameraPosition, transformed[0]);
  157. vec3Normalize(&cameraRay);
  158. double cameraDotTriangle = vec3Dot(triangleNormal, cameraRay);
  159. if (cameraDotTriangle < 0)
  160. {
  161. continue;
  162. }
  163. }
  164. for (j = 0 ; j < 3 ; j++)
  165. {
  166. vec2_t projected = perspectivePointProjection(transformed[j]);
  167. projected.x += (windowWidth / 2);
  168. projected.y += (windowHeight / 2);
  169. currentTriangle.points[j] = projected;
  170. }
  171. arrayAdd(projectedTriangles, currentTriangle);
  172. }
  173. }
  174. void render()
  175. {
  176. int i;
  177. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  178. SDL_RenderClear(renderer);
  179. #if 1
  180. int triangleCount = arrayGetSize(projectedTriangles);
  181. for(i = 0; i < triangleCount; i++)
  182. {
  183. switch(currentDisplayMode)
  184. {
  185. case DISPLAY_MODE_SOLID:
  186. drawFilledTriangle(projectedTriangles[i].points[0].x, projectedTriangles[i].points[0].y,
  187. projectedTriangles[i].points[1].x, projectedTriangles[i].points[1].y,
  188. projectedTriangles[i].points[2].x, projectedTriangles[i].points[2].y,
  189. 0xFFFFFFFF);
  190. break;
  191. case DISPLAY_MODE_WIREFRAME:
  192. drawTriangle(projectedTriangles[i].points[0].x, projectedTriangles[i].points[0].y,
  193. projectedTriangles[i].points[1].x, projectedTriangles[i].points[1].y,
  194. projectedTriangles[i].points[2].x, projectedTriangles[i].points[2].y,
  195. 0xFFFFFFFF);
  196. break;
  197. case DISPLAY_MODE_BOTH:
  198. drawFilledTriangle(projectedTriangles[i].points[0].x, projectedTriangles[i].points[0].y,
  199. projectedTriangles[i].points[1].x, projectedTriangles[i].points[1].y,
  200. projectedTriangles[i].points[2].x, projectedTriangles[i].points[2].y,
  201. 0xFFFFFFFF);
  202. drawTriangle(projectedTriangles[i].points[0].x, projectedTriangles[i].points[0].y,
  203. projectedTriangles[i].points[1].x, projectedTriangles[i].points[1].y,
  204. projectedTriangles[i].points[2].x, projectedTriangles[i].points[2].y,
  205. 0xFF000000);
  206. break;
  207. }
  208. if (displayVertex)
  209. {
  210. int j;
  211. for(j = 0; j < 3; j++)
  212. {
  213. drawRectangle(projectedTriangles[i].points[j].x - 1,
  214. projectedTriangles[i].points[j].y - 1,
  215. 3, 3,
  216. 0xFFFF0000);
  217. }
  218. }
  219. }
  220. #else
  221. drawTriangle(300, 100, 50, 400, 500, 700, 0xFF00FF00);
  222. drawFilledTriangle(300, 100, 50, 400, 500, 700, 0xFFFF0000);
  223. #endif
  224. renderFrameBuffer();
  225. clearFrameBuffer(0xFF000000);
  226. SDL_RenderPresent(renderer);
  227. }
  228. int main(int argc, char *argv[])
  229. {
  230. int param_i;
  231. bool fullScreen = false;
  232. MAX_DEBUG_LEVEL = TLOG_DEBUG;
  233. log(TLOG_ALWAYS, NULL, "Booting 3D Engine (version %s)!", VERSION);
  234. for (param_i = 1 ; (param_i < argc) && (argv[param_i][0] == '-') ; param_i++)
  235. {
  236. switch (argv[param_i][1])
  237. {
  238. default: exit(-1); /* Option not recognized */
  239. case 'f': fullScreen = true; break;
  240. case 'w': windowWidth = atoi(argv[++param_i]);
  241. case 'h': windowHeight = atoi(argv[++param_i]); break;
  242. #ifdef DYNA_LOG_LEVEL
  243. case 'l': MAX_DEBUG_LEVEL = atoi(argv[++param_i]); break;
  244. #endif
  245. case '-': goto no_more_params; /* Could use break, but this is more clear */
  246. }
  247. }
  248. no_more_params:
  249. isRunning = initialiseWindow(fullScreen);
  250. setup();
  251. while (isRunning)
  252. {
  253. processInput();
  254. update();
  255. render();
  256. }
  257. arrayFree(projectedTriangles);
  258. meshFree();
  259. destroyWindow();
  260. return 0;
  261. }