display.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * 3D Engine
  3. * display.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 <stdbool.h>
  11. #include <SDL.h>
  12. #include <SDL_ttf.h>
  13. #include <3dengine.h>
  14. #include <log.h>
  15. #include <display.h>
  16. /***********************************************************************************************************************
  17. * Variables
  18. **********************************************************************************************************************/
  19. static SDL_Window *window = NULL;
  20. static SDL_Renderer *renderer = NULL;
  21. static colour_t *frameBuffer = NULL;
  22. static double *zBuffer = NULL;
  23. static SDL_Texture *frameBufferTexture = NULL;
  24. static int32_t windowWidth = 1024;
  25. static int32_t windowHeight = 768;
  26. static TTF_Font *font = NULL;
  27. /***********************************************************************************************************************
  28. * Functions
  29. **********************************************************************************************************************/
  30. #define MAX(_a, _b) ((_a) < (_b)) ? (_b) : (_a)
  31. #define MIN(_a, _b) ((_a) > (_b)) ? (_b) : (_a)
  32. bool initialiseWindow(int32_t width, int32_t height, bool fullScreen)
  33. {
  34. bool ret = false;
  35. int windowFlags = 0;
  36. int fullscreenWidth;
  37. int fullscreenHeight;
  38. if (SDL_Init(SDL_INIT_EVERYTHING))
  39. {
  40. Log(TLOG_PANIC, NULL, "SDL Initialisation error!! error: %s", SDL_GetError());
  41. goto exit;
  42. }
  43. Log(TLOG_DEBUG, NULL, "SDL properly initialised!");
  44. ret = TTF_Init();
  45. if (ret)
  46. {
  47. Log(TLOG_PANIC, NULL, "SDL_ttf Initialisation error!! error: %s", SDL_GetError());
  48. goto exit;
  49. }
  50. windowWidth = width;
  51. windowHeight = height;
  52. if (fullScreen)
  53. {
  54. Log(TLOG_DEBUG, NULL, "Will go fullscreen! Fasten your seatbelts!");
  55. SDL_DisplayMode displayMode;
  56. SDL_GetCurrentDisplayMode(0, &displayMode);
  57. fullscreenWidth = displayMode.w;
  58. fullscreenHeight = displayMode.h;
  59. windowFlags = SDL_WINDOW_BORDERLESS;
  60. }
  61. else
  62. {
  63. fullscreenWidth = width;
  64. fullscreenHeight = height;
  65. }
  66. window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, fullscreenWidth, fullscreenHeight,
  67. windowFlags);
  68. if (window == NULL)
  69. {
  70. Log(TLOG_PANIC, NULL, "SDL Window creation error: %s", SDL_GetError());
  71. goto exit;
  72. }
  73. Log(TLOG_DEBUG, NULL, "SDL Window created!");
  74. renderer = SDL_CreateRenderer(window, -1, 0);
  75. if (renderer == NULL)
  76. {
  77. Log(TLOG_PANIC, NULL, "SDL Renderer creation error: %s", SDL_GetError());
  78. goto exit;
  79. }
  80. Log(TLOG_DEBUG, NULL, "SDL Renderer created!");
  81. font = TTF_OpenFont("assets/pico8.ttf", 8);
  82. if (font == NULL)
  83. {
  84. Log(TLOG_WARNING, NULL, "Cannot open the font for printing...: %s", SDL_GetError());
  85. goto exit;
  86. }
  87. frameBuffer = (uint32_t *)calloc(windowWidth * windowHeight, sizeof(uint32_t));
  88. if (!frameBuffer)
  89. {
  90. Log(TLOG_PANIC, NULL, "Memory allocation error.");
  91. goto exit;
  92. }
  93. zBuffer = (double *)calloc(windowWidth * windowHeight, sizeof(double));
  94. if (!frameBuffer)
  95. {
  96. Log(TLOG_PANIC, NULL, "Memory allocation error.");
  97. goto exit;
  98. }
  99. frameBufferTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STREAMING, windowWidth,
  100. windowHeight);
  101. if (frameBufferTexture == NULL)
  102. {
  103. Log(TLOG_PANIC, NULL, "SDL Texture creation error: %s", SDL_GetError());
  104. goto exit;
  105. }
  106. if (fullScreen)
  107. {
  108. SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
  109. }
  110. ret = true;
  111. exit:
  112. return ret;
  113. }
  114. void getWindowSize(int32_t *width, int32_t *height)
  115. {
  116. *width = windowWidth;
  117. *height = windowHeight;
  118. }
  119. void destroyWindow()
  120. {
  121. if (font != NULL)
  122. {
  123. TTF_CloseFont(font);
  124. }
  125. if (frameBuffer != NULL)
  126. {
  127. free(frameBuffer);
  128. }
  129. if (zBuffer != NULL)
  130. {
  131. free(zBuffer);
  132. }
  133. SDL_DestroyRenderer(renderer);
  134. SDL_DestroyWindow(window);
  135. SDL_Quit();
  136. }
  137. void displayWindowClear()
  138. {
  139. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  140. SDL_RenderClear(renderer);
  141. }
  142. void displayWindowRender()
  143. {
  144. SDL_RenderPresent(renderer);
  145. }
  146. void renderFrameBuffer()
  147. {
  148. SDL_UpdateTexture(frameBufferTexture, NULL, frameBuffer, (uint32_t)(windowWidth * sizeof(uint32_t)));
  149. SDL_RenderCopy(renderer, frameBufferTexture, NULL, NULL);
  150. }
  151. void drawText(int32_t x, int32_t y, colour_t colour, const char *format, ...)
  152. {
  153. va_list va;
  154. uint8_t r = ((colour >> 16) & 0xFF);
  155. uint8_t g = ((colour >> 8) & 0xFF);
  156. uint8_t b = ((colour ) & 0xFF);
  157. SDL_Color c = { r, g, b , 255};
  158. char buffer[512];
  159. va_start(va, format);
  160. vsnprintf(buffer, 512, format, va);
  161. va_end(va);
  162. SDL_Surface *surface = TTF_RenderText_Blended(font, buffer, c);
  163. SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
  164. int labelWidth, labelHeight;
  165. SDL_QueryTexture(texture, NULL, NULL, &labelWidth, &labelHeight);
  166. SDL_Rect destRect =
  167. {
  168. x, y, labelWidth, labelHeight
  169. };
  170. SDL_RenderCopyEx(renderer, texture, NULL, &destRect,0, NULL, SDL_FLIP_NONE);
  171. SDL_FreeSurface(surface);
  172. SDL_DestroyTexture(texture);
  173. }
  174. void drawPixel(int32_t x, int32_t y, colour_t colour)
  175. {
  176. if ((x >= 0) && (x < windowWidth) && (y >= 0) && (y < windowHeight))
  177. {
  178. frameBuffer[x + (y * windowWidth)] = colour;
  179. }
  180. }
  181. void drawVLine(int32_t x, int32_t y0, int32_t y1, colour_t colour)
  182. {
  183. int32_t i;
  184. if (y0 > y1) {
  185. intSwap(&y0, &y1); }
  186. if ( ((x < 0) || (x > windowWidth)) ||
  187. ((y0 < 0) && (y1 < 0)) ||
  188. ((y0 > windowHeight) && (y1 > windowHeight)) )
  189. {
  190. return;
  191. }
  192. y0 = MAX(0, y0);
  193. y1 = MIN(y1, (windowHeight - 1));
  194. uint32_t *currentPos = &frameBuffer[x + (y0 * windowWidth)];
  195. for(i = y0; i <= y1; i++, currentPos+=windowWidth)
  196. {
  197. *currentPos = colour;
  198. }
  199. }
  200. void drawHLine(int32_t x0, int32_t y, int32_t x1, colour_t colour)
  201. {
  202. int32_t i;
  203. if (x0 > x1) {
  204. intSwap(&x0, &x1); }
  205. if ( ((y < 0) || (y > windowHeight)) ||
  206. ((x0 < 0) && (x1 < 0)) ||
  207. ((x0 > windowWidth) && (x1 > windowWidth)) )
  208. {
  209. return;
  210. }
  211. x0 = MAX(0, x0);
  212. x1 = MIN(x1, (windowWidth - 1));
  213. uint32_t *currentPos = &frameBuffer[x0 + (y * windowWidth)];
  214. for(i = x0; i <= x1; i++, currentPos++)
  215. {
  216. *currentPos = colour;
  217. }
  218. }
  219. void clearFrameBuffer(colour_t colour)
  220. {
  221. int32_t x;
  222. for (x = 0 ; x < windowWidth * windowHeight ; x++)
  223. {
  224. frameBuffer[x] = colour;
  225. }
  226. }
  227. void clearZBuffer()
  228. {
  229. int32_t x;
  230. if (doZBuffer)
  231. {
  232. for (x = 0 ; x < windowWidth * windowHeight ; x++)
  233. {
  234. zBuffer[x] = 1.0;
  235. }
  236. }
  237. }
  238. void drawGrid(int spacing, colour_t colour)
  239. {
  240. int32_t x, y;
  241. for (y = 0 ; y < windowHeight ; y++)
  242. {
  243. for (x = 0 ; x < windowWidth ; x++)
  244. {
  245. if (((x % spacing) == 0) || ((y % spacing) == 0))
  246. {
  247. frameBuffer[x + (y * windowWidth)] = colour;
  248. }
  249. }
  250. }
  251. }
  252. void drawRectangle(int32_t x, int32_t y, int32_t w, int32_t h, colour_t colour)
  253. {
  254. int32_t j;
  255. for (j = 0 ; j < h ; j++)
  256. {
  257. drawHLine(x, y + j, x + w, colour);
  258. }
  259. }
  260. /* FIXME: Some out of screen line may render weirdly. Will fix that later */
  261. void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, colour_t colour)
  262. {
  263. int i;
  264. int32_t deltaX = x1 - x0;
  265. int32_t deltaY = y1 - y0;
  266. int32_t sideLength = ( abs(deltaX) >= abs(deltaY) ) ? abs(deltaX) : abs(deltaY);
  267. double incrementX = deltaX / (double)sideLength;
  268. double incrementY = deltaY / (double)sideLength;
  269. if (deltaX == 0)
  270. {
  271. drawVLine(x0, y0, y1, colour);
  272. }
  273. else if (deltaY == 0)
  274. {
  275. drawHLine(x0, y0, x1, colour);
  276. }
  277. else
  278. {
  279. double currentX = x0;
  280. double currentY = y0;
  281. for (i = 0 ; i < sideLength ; i++)
  282. {
  283. drawPixel(round(currentX), round(currentY), colour);
  284. currentX += incrementX;
  285. currentY += incrementY;
  286. }
  287. }
  288. }
  289. static vec3_t barycentricWeights(vec2_t a, vec2_t b, vec2_t c, vec2_t p)
  290. {
  291. vec2_t ab = vec2SubVectors(b, a);
  292. vec2_t bc = vec2SubVectors(c, b);
  293. vec2_t ac = vec2SubVectors(c, a);
  294. vec2_t ap = vec2SubVectors(p, a);
  295. vec2_t bp = vec2SubVectors(p, b);
  296. vec3_t ret;
  297. double areaTriangleABC = (ab.x * ac.y) - (ab.y * ac.x);
  298. double alpha = ((bc.x * bp.y) - (bp.x * bc.y)) / areaTriangleABC;
  299. double beta = ((ap.x * ac.y) - (ac.x * ap.y)) / areaTriangleABC;
  300. double gamma = 1 - alpha - beta;
  301. ret.x = alpha;
  302. ret.y = beta;
  303. ret.z = gamma;
  304. return ret;
  305. }
  306. void drawZPixel(int32_t x, int32_t y, vec4_t a, vec4_t b, vec4_t c, colour_t colour)
  307. {
  308. vec2_t pointP = { x, y };
  309. vec2_t a2 = vec2FromVec4(a);
  310. vec2_t b2 = vec2FromVec4(b);
  311. vec2_t c2 = vec2FromVec4(c);
  312. if ((x < 0) || (y < 0) || (x > windowWidth) ||(y > windowHeight))
  313. {
  314. return;
  315. }
  316. vec3_t weights = barycentricWeights(a2, b2, c2, pointP);
  317. double alpha = weights.x;
  318. double beta = weights.y;
  319. double gamma = weights.z;
  320. double interpolatedReciprocalW;
  321. interpolatedReciprocalW = (1 / a.w) * alpha + (1 / b.w) * beta + (1 / c.w) * gamma;
  322. /* Inverse to get logical W invrementing going further to us */
  323. interpolatedReciprocalW = 1 - interpolatedReciprocalW;
  324. if (interpolatedReciprocalW < zBuffer[(windowWidth * y) + x])
  325. {
  326. drawPixel(x, y, colour);
  327. /* Update Z Buffer */
  328. zBuffer[(windowWidth * y) + x] = interpolatedReciprocalW;
  329. }
  330. }
  331. void drawTexel(int32_t x, int32_t y, vec4_t a, vec4_t b, vec4_t c, tex2_t ta, tex2_t tb, tex2_t tc, texture_t *texture)
  332. {
  333. vec2_t pointP = { x, y };
  334. vec2_t a2 = vec2FromVec4(a);
  335. vec2_t b2 = vec2FromVec4(b);
  336. vec2_t c2 = vec2FromVec4(c);
  337. if ((x < 0) || (y < 0) || (x > windowWidth) ||(y > windowHeight))
  338. {
  339. return;
  340. }
  341. vec3_t weights = barycentricWeights(a2, b2, c2, pointP);
  342. double alpha = weights.x;
  343. double beta = weights.y;
  344. double gamma = weights.z;
  345. double interpolatedU, interpolatedV, interpolatedReciprocalW;
  346. int32_t texX, texY;
  347. interpolatedReciprocalW = (1 / a.w) * alpha + (1 / b.w) * beta + (1 / c.w) * gamma;
  348. if (doPerspectiveCorrection)
  349. {
  350. interpolatedU = (ta.u / a.w) * alpha + (tb.u / b.w) * beta + (tc.u / c.w) * gamma;
  351. interpolatedV = ((1 - ta.v) / a.w) * alpha + ((1 - tb.v) / b.w) * beta + ((1 - tc.v) / c.w) * gamma;
  352. interpolatedU /= interpolatedReciprocalW;
  353. interpolatedV /= interpolatedReciprocalW;
  354. }
  355. else
  356. {
  357. interpolatedU = ta.u * alpha + tb.u * beta + tc.u * gamma;
  358. interpolatedV = (1 - ta.v) * alpha + (1 - tb.v) * beta + (1 - tc.v) * gamma;
  359. }
  360. texX = abs((int32_t)(interpolatedU * texture->width));
  361. texY = abs((int32_t)(interpolatedV * texture->height));
  362. texX = texX % texture->width;
  363. texY = texY % texture->height;
  364. if (doZBuffer)
  365. {
  366. /* Inverse to get logical W invrementing going further to us */
  367. interpolatedReciprocalW = 1 - interpolatedReciprocalW;
  368. if (interpolatedReciprocalW < zBuffer[(windowWidth * y) + x])
  369. {
  370. drawPixel(x, y, texture->data[(texY * texture->width) + texX]);
  371. /* Update Z Buffer */
  372. zBuffer[(windowWidth * y) + x] = interpolatedReciprocalW;
  373. }
  374. }
  375. else
  376. {
  377. drawPixel(x, y, texture->data[(texY * texture->width) + texX]);
  378. }
  379. }