123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- /*
- * 3D Engine
- * display.c:
- * Based on pikuma.com 3D software renderer in C
- * Copyright (c) 2021 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 01/03/2021.
- */
- #include <stdio.h>
- #include <stdbool.h>
- #include <SDL.h>
- #include <SDL_ttf.h>
- #include <3dengine.h>
- #include <log.h>
- #include <display.h>
- /***********************************************************************************************************************
- * Variables
- **********************************************************************************************************************/
- static SDL_Window *window = NULL;
- static SDL_Renderer *renderer = NULL;
- static colour_t *frameBuffer = NULL;
- static double *zBuffer = NULL;
- static SDL_Texture *frameBufferTexture = NULL;
- static int32_t windowWidth = 1024;
- static int32_t windowHeight = 768;
- static TTF_Font *font = NULL;
- /***********************************************************************************************************************
- * Functions
- **********************************************************************************************************************/
- #define MAX(_a, _b) ((_a) < (_b)) ? (_b) : (_a)
- #define MIN(_a, _b) ((_a) > (_b)) ? (_b) : (_a)
- bool initialiseWindow(int32_t width, int32_t height, bool fullScreen)
- {
- bool ret = false;
- int windowFlags = 0;
- int fullscreenWidth;
- int fullscreenHeight;
- if (SDL_Init(SDL_INIT_EVERYTHING))
- {
- Log(TLOG_PANIC, NULL, "SDL Initialisation error!! error: %s", SDL_GetError());
- goto exit;
- }
- Log(TLOG_DEBUG, NULL, "SDL properly initialised!");
- ret = TTF_Init();
- if (ret)
- {
- Log(TLOG_PANIC, NULL, "SDL_ttf Initialisation error!! error: %s", SDL_GetError());
- goto exit;
- }
- windowWidth = width;
- windowHeight = height;
- if (fullScreen)
- {
- Log(TLOG_DEBUG, NULL, "Will go fullscreen! Fasten your seatbelts!");
- SDL_DisplayMode displayMode;
- SDL_GetCurrentDisplayMode(0, &displayMode);
- fullscreenWidth = displayMode.w;
- fullscreenHeight = displayMode.h;
- windowFlags = SDL_WINDOW_BORDERLESS;
- }
- else
- {
- fullscreenWidth = width;
- fullscreenHeight = height;
- }
- window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, fullscreenWidth, fullscreenHeight,
- windowFlags);
- if (window == NULL)
- {
- Log(TLOG_PANIC, NULL, "SDL Window creation error: %s", SDL_GetError());
- goto exit;
- }
- Log(TLOG_DEBUG, NULL, "SDL Window created!");
- renderer = SDL_CreateRenderer(window, -1, 0);
- if (renderer == NULL)
- {
- Log(TLOG_PANIC, NULL, "SDL Renderer creation error: %s", SDL_GetError());
- goto exit;
- }
- Log(TLOG_DEBUG, NULL, "SDL Renderer created!");
- font = TTF_OpenFont("assets/pico8.ttf", 8);
- if (font == NULL)
- {
- Log(TLOG_WARNING, NULL, "Cannot open the font for printing...: %s", SDL_GetError());
- goto exit;
- }
- frameBuffer = (uint32_t *)calloc(windowWidth * windowHeight, sizeof(uint32_t));
- if (!frameBuffer)
- {
- Log(TLOG_PANIC, NULL, "Memory allocation error.");
- goto exit;
- }
- zBuffer = (double *)calloc(windowWidth * windowHeight, sizeof(double));
- if (!frameBuffer)
- {
- Log(TLOG_PANIC, NULL, "Memory allocation error.");
- goto exit;
- }
- frameBufferTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STREAMING, windowWidth,
- windowHeight);
- if (frameBufferTexture == NULL)
- {
- Log(TLOG_PANIC, NULL, "SDL Texture creation error: %s", SDL_GetError());
- goto exit;
- }
- if (fullScreen)
- {
- SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
- }
- ret = true;
- exit:
- return ret;
- }
- void getWindowSize(int32_t *width, int32_t *height)
- {
- *width = windowWidth;
- *height = windowHeight;
- }
- void destroyWindow()
- {
- if (font != NULL)
- {
- TTF_CloseFont(font);
- }
- if (frameBuffer != NULL)
- {
- free(frameBuffer);
- }
- if (zBuffer != NULL)
- {
- free(zBuffer);
- }
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
- }
- void displayWindowClear()
- {
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
- SDL_RenderClear(renderer);
- }
- void displayWindowRender()
- {
- SDL_RenderPresent(renderer);
- }
- void renderFrameBuffer()
- {
- SDL_UpdateTexture(frameBufferTexture, NULL, frameBuffer, (uint32_t)(windowWidth * sizeof(uint32_t)));
- SDL_RenderCopy(renderer, frameBufferTexture, NULL, NULL);
- }
- void drawText(int32_t x, int32_t y, colour_t colour, const char *format, ...)
- {
- va_list va;
- uint8_t r = ((colour >> 16) & 0xFF);
- uint8_t g = ((colour >> 8) & 0xFF);
- uint8_t b = ((colour ) & 0xFF);
- SDL_Color c = { r, g, b , 255};
- char buffer[512];
- va_start(va, format);
- vsnprintf(buffer, 512, format, va);
- va_end(va);
- SDL_Surface *surface = TTF_RenderText_Blended(font, buffer, c);
- SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
- int labelWidth, labelHeight;
- SDL_QueryTexture(texture, NULL, NULL, &labelWidth, &labelHeight);
- SDL_Rect destRect =
- {
- x, y, labelWidth, labelHeight
- };
- SDL_RenderCopyEx(renderer, texture, NULL, &destRect,0, NULL, SDL_FLIP_NONE);
- SDL_FreeSurface(surface);
- SDL_DestroyTexture(texture);
- }
- void drawPixel(int32_t x, int32_t y, colour_t colour)
- {
- if ((x >= 0) && (x < windowWidth) && (y >= 0) && (y < windowHeight))
- {
- frameBuffer[x + (y * windowWidth)] = colour;
- }
- }
- void drawVLine(int32_t x, int32_t y0, int32_t y1, colour_t colour)
- {
- int32_t i;
- if (y0 > y1) {
- intSwap(&y0, &y1); }
- if ( ((x < 0) || (x > windowWidth)) ||
- ((y0 < 0) && (y1 < 0)) ||
- ((y0 > windowHeight) && (y1 > windowHeight)) )
- {
- return;
- }
- y0 = MAX(0, y0);
- y1 = MIN(y1, (windowHeight - 1));
- uint32_t *currentPos = &frameBuffer[x + (y0 * windowWidth)];
- for(i = y0; i <= y1; i++, currentPos+=windowWidth)
- {
- *currentPos = colour;
- }
- }
- void drawHLine(int32_t x0, int32_t y, int32_t x1, colour_t colour)
- {
- int32_t i;
- if (x0 > x1) {
- intSwap(&x0, &x1); }
- if ( ((y < 0) || (y > windowHeight)) ||
- ((x0 < 0) && (x1 < 0)) ||
- ((x0 > windowWidth) && (x1 > windowWidth)) )
- {
- return;
- }
- x0 = MAX(0, x0);
- x1 = MIN(x1, (windowWidth - 1));
- uint32_t *currentPos = &frameBuffer[x0 + (y * windowWidth)];
- for(i = x0; i <= x1; i++, currentPos++)
- {
- *currentPos = colour;
- }
- }
- void clearFrameBuffer(colour_t colour)
- {
- int32_t x;
- for (x = 0 ; x < windowWidth * windowHeight ; x++)
- {
- frameBuffer[x] = colour;
- }
- }
- void clearZBuffer()
- {
- int32_t x;
- if (doZBuffer)
- {
- for (x = 0 ; x < windowWidth * windowHeight ; x++)
- {
- zBuffer[x] = 1.0;
- }
- }
- }
- void drawGrid(int spacing, colour_t colour)
- {
- int32_t x, y;
- for (y = 0 ; y < windowHeight ; y++)
- {
- for (x = 0 ; x < windowWidth ; x++)
- {
- if (((x % spacing) == 0) || ((y % spacing) == 0))
- {
- frameBuffer[x + (y * windowWidth)] = colour;
- }
- }
- }
- }
- void drawRectangle(int32_t x, int32_t y, int32_t w, int32_t h, colour_t colour)
- {
- int32_t j;
- for (j = 0 ; j < h ; j++)
- {
- drawHLine(x, y + j, x + w, colour);
- }
- }
- /* FIXME: Some out of screen line may render weirdly. Will fix that later */
- void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, colour_t colour)
- {
- int i;
- int32_t deltaX = x1 - x0;
- int32_t deltaY = y1 - y0;
- int32_t sideLength = ( abs(deltaX) >= abs(deltaY) ) ? abs(deltaX) : abs(deltaY);
- double incrementX = deltaX / (double)sideLength;
- double incrementY = deltaY / (double)sideLength;
- if (deltaX == 0)
- {
- drawVLine(x0, y0, y1, colour);
- }
- else if (deltaY == 0)
- {
- drawHLine(x0, y0, x1, colour);
- }
- else
- {
- double currentX = x0;
- double currentY = y0;
- for (i = 0 ; i < sideLength ; i++)
- {
- drawPixel(round(currentX), round(currentY), colour);
- currentX += incrementX;
- currentY += incrementY;
- }
- }
- }
- static vec3_t barycentricWeights(vec2_t a, vec2_t b, vec2_t c, vec2_t p)
- {
- vec2_t ab = vec2SubVectors(b, a);
- vec2_t bc = vec2SubVectors(c, b);
- vec2_t ac = vec2SubVectors(c, a);
- vec2_t ap = vec2SubVectors(p, a);
- vec2_t bp = vec2SubVectors(p, b);
- vec3_t ret;
- double areaTriangleABC = (ab.x * ac.y) - (ab.y * ac.x);
- double alpha = ((bc.x * bp.y) - (bp.x * bc.y)) / areaTriangleABC;
- double beta = ((ap.x * ac.y) - (ac.x * ap.y)) / areaTriangleABC;
- double gamma = 1 - alpha - beta;
- ret.x = alpha;
- ret.y = beta;
- ret.z = gamma;
- return ret;
- }
- void drawZPixel(int32_t x, int32_t y, vec4_t a, vec4_t b, vec4_t c, colour_t colour)
- {
- vec2_t pointP = { x, y };
- vec2_t a2 = vec2FromVec4(a);
- vec2_t b2 = vec2FromVec4(b);
- vec2_t c2 = vec2FromVec4(c);
- if ((x < 0) || (y < 0) || (x > windowWidth) ||(y > windowHeight))
- {
- return;
- }
- vec3_t weights = barycentricWeights(a2, b2, c2, pointP);
- double alpha = weights.x;
- double beta = weights.y;
- double gamma = weights.z;
- double interpolatedReciprocalW;
- interpolatedReciprocalW = (1 / a.w) * alpha + (1 / b.w) * beta + (1 / c.w) * gamma;
- /* Inverse to get logical W invrementing going further to us */
- interpolatedReciprocalW = 1 - interpolatedReciprocalW;
- if (interpolatedReciprocalW < zBuffer[(windowWidth * y) + x])
- {
- drawPixel(x, y, colour);
- /* Update Z Buffer */
- zBuffer[(windowWidth * y) + x] = interpolatedReciprocalW;
- }
- }
- 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)
- {
- vec2_t pointP = { x, y };
- vec2_t a2 = vec2FromVec4(a);
- vec2_t b2 = vec2FromVec4(b);
- vec2_t c2 = vec2FromVec4(c);
- if ((x < 0) || (y < 0) || (x > windowWidth) ||(y > windowHeight))
- {
- return;
- }
- vec3_t weights = barycentricWeights(a2, b2, c2, pointP);
- double alpha = weights.x;
- double beta = weights.y;
- double gamma = weights.z;
- double interpolatedU, interpolatedV, interpolatedReciprocalW;
- int32_t texX, texY;
- interpolatedReciprocalW = (1 / a.w) * alpha + (1 / b.w) * beta + (1 / c.w) * gamma;
- if (doPerspectiveCorrection)
- {
- interpolatedU = (ta.u / a.w) * alpha + (tb.u / b.w) * beta + (tc.u / c.w) * gamma;
- interpolatedV = ((1 - ta.v) / a.w) * alpha + ((1 - tb.v) / b.w) * beta + ((1 - tc.v) / c.w) * gamma;
- interpolatedU /= interpolatedReciprocalW;
- interpolatedV /= interpolatedReciprocalW;
- }
- else
- {
- interpolatedU = ta.u * alpha + tb.u * beta + tc.u * gamma;
- interpolatedV = (1 - ta.v) * alpha + (1 - tb.v) * beta + (1 - tc.v) * gamma;
- }
- texX = abs((int32_t)(interpolatedU * texture->width));
- texY = abs((int32_t)(interpolatedV * texture->height));
- texX = texX % texture->width;
- texY = texY % texture->height;
- if (doZBuffer)
- {
- /* Inverse to get logical W invrementing going further to us */
- interpolatedReciprocalW = 1 - interpolatedReciprocalW;
- if (interpolatedReciprocalW < zBuffer[(windowWidth * y) + x])
- {
- drawPixel(x, y, texture->data[(texY * texture->width) + texX]);
- /* Update Z Buffer */
- zBuffer[(windowWidth * y) + x] = interpolatedReciprocalW;
- }
- }
- else
- {
- drawPixel(x, y, texture->data[(texY * texture->width) + texX]);
- }
- }
|