display.c 11 KB

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