]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - palette.c
added collision_prefernudgedfraction cvar (defaults to 1), which should improve colli...
[xonotic/darkplaces.git] / palette.c
index f18d7e01873b598c0549e17d4687534ae3b3a16e..21974d4de9e8c1b1dd905bb7d141df157ae461d7 100644 (file)
--- a/palette.c
+++ b/palette.c
@@ -2,14 +2,15 @@
 #include "quakedef.h"
 
 unsigned int palette_complete[256];
+unsigned int palette_font[256];
+unsigned int palette_alpha[256];
+unsigned int palette_nocolormap[256];
+unsigned int palette_nocolormapnofullbrights[256];
 unsigned int palette_nofullbrights[256];
 unsigned int palette_onlyfullbrights[256];
-unsigned int palette_nocolormapnofullbrights[256];
-unsigned int palette_nocolormap[256];
 unsigned int palette_pantsaswhite[256];
 unsigned int palette_shirtaswhite[256];
-unsigned int palette_alpha[256];
-unsigned int palette_font[256];
+unsigned int palette_transparent[256];
 
 // John Carmack said the quake palette.lmp can be considered public domain because it is not an important asset to id, so I include it here as a fallback if no external palette file is found.
 unsigned char host_quakepal[768] =
@@ -29,6 +30,7 @@ void Palette_SetupSpecialPalettes(void)
        int pants_start, pants_end;
        int shirt_start, shirt_end;
        int reversed_start, reversed_end;
+       int transparentcolor;
        unsigned char *colormap;
        fs_offset_t filesize;
 
@@ -46,18 +48,21 @@ void Palette_SetupSpecialPalettes(void)
        shirt_end = 32;
        reversed_start = 128;
        reversed_end = 224;
+       transparentcolor = 255;
+
+       for (i = 0;i < 256;i++)
+               palette_transparent[i] = palette_complete[i];
+       palette_transparent[transparentcolor] = 0;
 
        for (i = 0;i < fullbright_start;i++)
                palette_nofullbrights[i] = palette_complete[i];
-       for (i = fullbright_start;i < 255;i++)
+       for (i = fullbright_start;i < fullbright_end;i++)
                palette_nofullbrights[i] = palette_complete[0];
-       palette_nofullbrights[255] = 0;
 
        for (i = 0;i < 256;i++)
                palette_onlyfullbrights[i] = palette_complete[0];
        for (i = fullbright_start;i < fullbright_end;i++)
                palette_onlyfullbrights[i] = palette_complete[i];
-       palette_onlyfullbrights[255] = 0;
 
        for (i = 0;i < 256;i++)
                palette_nocolormapnofullbrights[i] = palette_complete[i];
@@ -67,7 +72,6 @@ void Palette_SetupSpecialPalettes(void)
                palette_nocolormapnofullbrights[i] = palette_complete[0];
        for (i = fullbright_start;i < fullbright_end;i++)
                palette_nocolormapnofullbrights[i] = palette_complete[0];
-       palette_nocolormapnofullbrights[255] = 0;
 
        for (i = 0;i < 256;i++)
                palette_nocolormap[i] = palette_complete[i];
@@ -75,7 +79,6 @@ void Palette_SetupSpecialPalettes(void)
                palette_nocolormap[i] = palette_complete[0];
        for (i = shirt_start;i < shirt_end;i++)
                palette_nocolormap[i] = palette_complete[0];
-       palette_nocolormap[255] = 0;
 
        for (i = 0;i < 256;i++)
                palette_pantsaswhite[i] = palette_complete[0];
@@ -97,67 +100,40 @@ void Palette_SetupSpecialPalettes(void)
                        palette_shirtaswhite[i] = palette_complete[i - shirt_start];
        }
 
-       for (i = 0;i < 255;i++)
+       for (i = 0;i < 256;i++)
                palette_alpha[i] = 0xFFFFFFFF;
-       palette_alpha[255] = 0;
+       palette_alpha[transparentcolor] = 0;
 
-       palette_font[0] = 0;
-       for (i = 1;i < 255;i++)
+       for (i = 0;i < 256;i++)
                palette_font[i] = palette_complete[i];
-       palette_font[255] = 0;
+       palette_font[0] = 0;
 }
 
-void BuildGammaTable8(float prescale, float gamma, float scale, float base, unsigned char *out)
+void BuildGammaTable8(float prescale, float gamma, float scale, float base, unsigned char *out, int rampsize)
 {
        int i, adjusted;
-       double invgamma, d;
+       double invgamma;
 
-       gamma = bound(0.1, gamma, 5.0);
-       if (gamma == 1) // LordHavoc: dodge the math
+       invgamma = 1.0 / gamma;
+       prescale /= (double) (rampsize - 1);
+       for (i = 0;i < rampsize;i++)
        {
-               for (i = 0;i < 256;i++)
-               {
-                       adjusted = (int) (i * prescale * scale + base * 255.0);
-                       out[i] = bound(0, adjusted, 255);
-               }
-       }
-       else
-       {
-               invgamma = 1.0 / gamma;
-               prescale /= 255.0f;
-               for (i = 0;i < 256;i++)
-               {
-                       d = pow((double) i * prescale, invgamma) * scale + base;
-                       adjusted = (int) (255.0 * d);
-                       out[i] = bound(0, adjusted, 255);
-               }
+               adjusted = (int) (255.0 * (pow((double) i * prescale, invgamma) * scale + base) + 0.5);
+               out[i] = bound(0, adjusted, 255);
        }
 }
 
-void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
+void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out, int rampsize)
 {
        int i, adjusted;
-       double invgamma, d;
+       double invgamma;
 
-       gamma = bound(0.1, gamma, 5.0);
-       if (gamma == 1) // LordHavoc: dodge the math
+       invgamma = 1.0 / gamma;
+       prescale /= (double) (rampsize - 1);
+       for (i = 0;i < rampsize;i++)
        {
-               for (i = 0;i < 256;i++)
-               {
-                       adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
-                       out[i] = bound(0, adjusted, 65535);
-               }
-       }
-       else
-       {
-               invgamma = 1.0 / gamma;
-               prescale /= 255.0f;
-               for (i = 0;i < 256;i++)
-               {
-                       d = pow((double) i * prescale, invgamma) * scale + base;
-                       adjusted = (int) (65535.0 * d);
-                       out[i] = bound(0, adjusted, 65535);
-               }
+               adjusted = (int) (65535.0 * (pow((double) i * prescale, invgamma) * scale + base) + 0.5);
+               out[i] = bound(0, adjusted, 65535);
        }
 }
 
@@ -188,7 +164,7 @@ void Palette_Init(void)
        scale = bound(0.01, scale, 10.0);
        base = bound(0, base, 0.95);
 
-       BuildGammaTable8(1.0f, gamma, scale, base, texturegammaramp);
+       BuildGammaTable8(1.0f, gamma, scale, base, texturegammaramp, 256);
 
        palfile = (unsigned char *)FS_LoadFile ("gfx/palette.lmp", tempmempool, false, &filesize);
        if (palfile && filesize >= 768)
@@ -199,14 +175,13 @@ void Palette_Init(void)
                in = host_quakepal;
        }
        out = (unsigned char *) palette_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
-       for (i = 0;i < 255;i++)
+       for (i = 0;i < 256;i++)
        {
                *out++ = texturegammaramp[*in++];
                *out++ = texturegammaramp[*in++];
                *out++ = texturegammaramp[*in++];
                *out++ = 255;
        }
-       palette_complete[255] = 0; // completely transparent black
        if (palfile)
                Mem_Free(palfile);