Working on bump effect. I have more ideas for it, the way I made this, the lightmap...
[dosdemo] / src / bump.c
index 66c6bbc..de38079 100644 (file)
@@ -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);
 }