From 665120dd016463d4a49781b351ccecb276b140fd Mon Sep 17 00:00:00 2001 From: Michael Kargas Date: Tue, 27 Sep 2016 22:34:44 +0100 Subject: [PATCH] Working on bump effect. I have more ideas for it, the way I made this, the lightmap is like a canvas where you add lights and I will add some more cool stuff I have in mind. --- dosdemo.vcxproj | 1 + dosdemo.vcxproj.filters | 3 ++ src/bump.c | 129 ++++++++++++++++++++++++++++++++++++++++------- src/plasma.c | 10 ++-- 4 files changed, 121 insertions(+), 22 deletions(-) diff --git a/dosdemo.vcxproj b/dosdemo.vcxproj index f8acfc0..4c16782 100644 --- a/dosdemo.vcxproj +++ b/dosdemo.vcxproj @@ -85,6 +85,7 @@ + diff --git a/dosdemo.vcxproj.filters b/dosdemo.vcxproj.filters index 54c3fd2..d0c6178 100644 --- a/dosdemo.vcxproj.filters +++ b/dosdemo.vcxproj.filters @@ -52,6 +52,9 @@ src + + src + diff --git a/src/bump.c b/src/bump.c index 66c6bbc..de38079 100644 --- a/src/bump.c +++ b/src/bump.c @@ -24,12 +24,27 @@ static struct screen scr = { draw }; +static struct point { + int x, y; +}; + +#define LIGHT_WIDTH 128 +#define LIGHT_HEIGHT LIGHT_WIDTH + static unsigned long startingTime; static unsigned char *heightmap; static unsigned short *lightmap; static int *bumpOffset; +static unsigned short *lightR; +static unsigned short *lightG; +static unsigned short *lightB; +static struct point pointR, pointG, pointB; + +//#define FUNKY_COLORS + + struct screen *bump_screen(void) { return &scr; @@ -37,9 +52,26 @@ struct screen *bump_screen(void) static int init(void) { + int i, j, x, y, c, r, g, b; + const int numBlurs = 3; - int i, j, x, y; - int fb_size = fb_width * fb_height; + const int lightRadius = LIGHT_WIDTH / 2; + + const int lightSize = LIGHT_WIDTH * LIGHT_HEIGHT; + const int fb_size = fb_width * fb_height; + + // Just some parameters to temporary test the colors of 3 lights + #ifdef FUNKY_COLORS + // I see some artifacts if I mix channels, not sure if ORing is fine + const float r0 = 1.0f, g0 = 0.6f, b0 = 0.0f; + const float r1 = 0.5f, g1 = 1.0f, b1 = 0.2f; + const float r2 = 0.6f, g2 = 0.4f, b2 = 1.0f; + #else + // if every light uses it's own channel bits, it's better + const float r0 = 1.0f, g0 = 0.0f, b0 = 0.0f; + const float r1 = 0.0f, g1 = 1.0f, b1 = 0.0f; + const float r2 = 0.0f, g2 = 0.0f, b2 = 1.0f; + #endif initFpsFonts(); @@ -47,8 +79,12 @@ static int init(void) lightmap = malloc(sizeof(*lightmap) * fb_size); bumpOffset = malloc(sizeof(*bumpOffset) * fb_size); - memset(lightmap, 0, fb_size); - memset(bumpOffset, 0, fb_size); + lightR = malloc(sizeof(*lightR) * lightSize); + lightG = malloc(sizeof(*lightG) * lightSize); + lightB = malloc(sizeof(*lightB) * lightSize); + + memset(lightmap, 0, sizeof(*lightmap) * fb_size); + memset(bumpOffset, 0, sizeof(*bumpOffset) * fb_size); // Create random junk for (i = 0; i < fb_size; i++) @@ -83,19 +119,27 @@ static int init(void) } } - // Lightmap test (this will be replaced by code in the main loop later on, the idea is you can render moving lights and other light geometry or sprites in light buffer and the bumpOffset will catch them) + // Generate three lights i = 0; - for (y = 0; y < fb_height; y++) + for (y = 0; y < LIGHT_HEIGHT; y++) { - int yc = y - (fb_height >> 1); - for (x = 0; x < fb_width; x++) + int yc = y - (LIGHT_HEIGHT / 2); + for (x = 0; x < LIGHT_WIDTH; x++) { - int xc = x - (fb_width >> 1); - int c = (int)sqrt(xc * xc + yc * yc) << 1; - int r; - if (c > 255) c = 255; - r = 255 - c; - lightmap[i++] = ((r >> 4) << 11) | ((r >> 3) << 5) | (r >> 3); + int xc = x - (LIGHT_WIDTH / 2); + float d = (float)sqrt(xc * xc + yc * yc); + float invDist = ((float)lightRadius - (float)sqrt(xc * xc + yc * yc)) / (float)lightRadius; + if (invDist < 0.0f) invDist = 0.0f; + + c = (int)(invDist * 63); + r = c >> 1; + g = c; + b = c >> 1; + + lightR[i] = ((int)(r * r0) << 11) | ((int)(g * g0) << 5) | (int)(b * b0); + lightG[i] = ((int)(r * r1) << 11) | ((int)(g * g1) << 5) | (int)(b * b1); + lightB[i] = ((int)(r * r2) << 11) | ((int)(g * g2) << 5) | (int)(b * b2); + i++; } } @@ -115,17 +159,68 @@ static void stop(long trans_time) { } -static void draw(void) + +static void renderLight(struct point *p, unsigned short *light) +{ + // Check for boundaries is missing atm, will add soon + int x, y; + unsigned short *dst = (unsigned short*)lightmap + p->y * fb_width + p->x; + for (y = 0; y < LIGHT_HEIGHT; y++) + { + for (x = 0; x < LIGHT_WIDTH; x++) + { + *dst++ |= *light++; + } + dst += fb_width - LIGHT_WIDTH; + } +} + + +static void renderLights() +{ + memset(lightmap, 0, fb_width * fb_height * sizeof(*lightmap)); + // I will later delete only the regions of lights to speed up + + renderLight(&pointR, lightR); + renderLight(&pointG, lightG); + renderLight(&pointB, lightB); +} + +static void animateLights() +{ + struct point center; + float dt = (float)(time_msec - startingTime) / 1000.0f; + + center.x = (fb_width >> 1) - (LIGHT_WIDTH / 2); + center.y = (fb_height >> 1) - (LIGHT_HEIGHT / 2); + + pointR.x = center.x + sin(1.2f * dt) * 64.0f; + pointR.y = center.y + sin(0.8f * dt) * 48.0f; + + pointG.x = center.x + sin(1.5f * dt) * 56.0f; + pointG.y = center.y + sin(1.1f * dt) * 42.0f; + + pointB.x = center.x + sin(2.0f * dt) * 80.0f; + pointB.y = center.y + sin(1.3f * dt) * 46.0f; +} + +static void renderBump(unsigned short *vram) { int i; - unsigned short *vram = (unsigned short*)fb_pixels; for (i = 0; i < fb_width * fb_height; i++) { unsigned short c = lightmap[bumpOffset[i]]; *vram++ = c; } +} + +static void draw(void) +{ + animateLights(); + renderLights(); + renderBump((unsigned short*)vmem_back); - drawFps((unsigned short*)fb_pixels); + drawFps((unsigned short*)vmem_back); swap_buffers(0); } diff --git a/src/plasma.c b/src/plasma.c index 063fb78..05f68e4 100644 --- a/src/plasma.c +++ b/src/plasma.c @@ -22,15 +22,15 @@ static struct screen scr = { draw }; -unsigned long startingTime; +static unsigned long startingTime; #define PSIN_SIZE 4096 #define PPAL_SIZE 256 -unsigned char *psin1, *psin2, *psin3; -unsigned short *plasmaPal; +static unsigned char *psin1, *psin2, *psin3; +static unsigned short *plasmaPal; -unsigned short myBuffer[320*240]; +static unsigned short myBuffer[320 * 240]; struct screen *plasma_screen(void) @@ -116,7 +116,7 @@ static void draw(void) } } - drawFps((unsigned short*)fb_pixels); + drawFps((unsigned short*)vmem_back); swap_buffers(0); } -- 1.7.10.4