]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - palette.c
fixed some dynamic lighting bugs related to glowing self
[xonotic/darkplaces.git] / palette.c
index 5eb176f13cef59a0442155b584d47c16711852a0..1978eefe1fded65a6595b0cd1144141d8a51079a 100644 (file)
--- a/palette.c
+++ b/palette.c
 
 #include "quakedef.h"
 
-unsigned int d_8to24table[256];
-//byte d_15to8table[32768];
-byte host_basepal[768];
-byte texgamma[256];
+unsigned int palette_complete[256];
+unsigned int palette_nofullbrights[256];
+unsigned int palette_onlyfullbrights[256];
+unsigned int palette_nocolormapnofullbrights[256];
+unsigned int palette_pantsaswhite[256];
+unsigned int palette_shirtaswhite[256];
+unsigned int palette_alpha[256];
+unsigned int palette_font[256];
 
-static float texture_gamma = 1.0;
+qbyte host_basepal[768];
 
-cvar_t vid_gamma = {CVAR_SAVE, "vid_gamma", "1"};
-cvar_t vid_brightness = {CVAR_SAVE, "vid_brightness", "1"};
-cvar_t vid_contrast = {CVAR_SAVE, "vid_contrast", "1"};
+cvar_t v_gamma = {CVAR_SAVE, "v_gamma", "1"};
+cvar_t v_contrast = {CVAR_SAVE, "v_contrast", "1"};
+cvar_t v_brightness = {CVAR_SAVE, "v_brightness", "0"};
+cvar_t v_overbrightbits = {CVAR_SAVE, "v_overbrightbits", "0"};
+cvar_t v_hwgamma = {0, "v_hwgamma", "1"};
 
 void Palette_Setup8to24(void)
 {
-       byte *in, *out;
-       unsigned short i;
+       int i;
+       int fullbright_start, fullbright_end;
+       int pants_start, pants_end;
+       int shirt_start, shirt_end;
+       int reversed_start, reversed_end;
+       qbyte *in, *out;
+       qbyte *colormap;
 
        in = host_basepal;
-       out = (byte *) d_8to24table; // d_8to24table is accessed as 32bit for speed reasons, but is created as 8bit bytes
-       for (i=0 ; i<255 ; i++)
+       out = (qbyte *) palette_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
+       for (i = 0;i < 255;i++)
        {
                *out++ = *in++;
                *out++ = *in++;
                *out++ = *in++;
                *out++ = 255;
        }
-       d_8to24table[255] = 0; // completely transparent black
-}
+       palette_complete[255] = 0; // completely transparent black
 
-/*
-void   Palette_Setup15to8(void)
-{
-       byte    *pal;
-       unsigned r,g,b;
-       unsigned v;
-       int     r1,g1,b1;
-       int             j,k,l;
-       unsigned short i;
-
-       for (i = 0;i < 32768;i++)
+       // FIXME: fullbright_start should be read from colormap.lmp
+       colormap = COM_LoadFile("gfx/colormap.lmp", true);
+       if (colormap && com_filesize >= 16385)
+               fullbright_start = 256 - colormap[16384];
+       else
+               fullbright_start = 256;
+       if (colormap)
+               Mem_Free(colormap);
+       fullbright_end = 256;
+       pants_start = 96;
+       pants_end = 112;
+       shirt_start = 16;
+       shirt_end = 32;
+       reversed_start = 128;
+       reversed_end = 224;
+
+       for (i = 0;i < fullbright_start;i++)
+               palette_nofullbrights[i] = palette_complete[i];
+       for (i = fullbright_start;i < 255;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];
+       for (i = pants_start;i < pants_end;i++)
+               palette_nocolormapnofullbrights[i] = palette_complete[0];
+       for (i = shirt_start;i < shirt_end;i++)
+               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_pantsaswhite[i] = palette_complete[0];
+       for (i = pants_start;i < pants_end;i++)
        {
-               r = ((i & 0x001F) << 3)+4;
-               g = ((i & 0x03E0) >> 2)+4;
-               b = ((i & 0x7C00) >> 7)+4;
-               pal = (unsigned char *)d_8to24table;
-               for (v = 0, k = 0, l = 1000000000;v < 256;v++, pal += 4)
-               {
-                       r1 = r - pal[0];
-                       g1 = g - pal[1];
-                       b1 = b - pal[2];
-                       j = r1*r1+g1*g1+b1*b1;
-                       if (j < l)
-                       {
-                               k = v;
-                               l = j;
-                       }
-               }
-               d_15to8table[i] = k;
+               if (i >= reversed_start && i < reversed_end)
+                       palette_pantsaswhite[i] = palette_complete[15 - (i - pants_start)];
+               else
+                       palette_pantsaswhite[i] = palette_complete[i - pants_start];
+       }
+
+       for (i = 0;i < 256;i++)
+               palette_shirtaswhite[i] = palette_complete[0];
+       for (i = shirt_start;i < shirt_end;i++)
+       {
+               if (i >= reversed_start && i < reversed_end)
+                       palette_shirtaswhite[i] = palette_complete[15 - (i - shirt_start)];
+               else
+                       palette_shirtaswhite[i] = palette_complete[i - shirt_start];
        }
+
+       for (i = 0;i < 255;i++)
+               palette_alpha[i] = 0xFFFFFFFF;
+       palette_alpha[255] = 0;
+
+       palette_font[0] = 0;
+       for (i = 1;i < 255;i++)
+               palette_font[i] = palette_complete[i];
+       palette_font[255] = 0;
 }
-*/
 
-void BuildGammaTable8(float prescale, float gamma, float scale, float base, byte *out)
+
+void BuildGammaTable8(float prescale, float gamma, float scale, float base, qbyte *out)
 {
        int i, adjusted;
        double invgamma, d;
@@ -116,81 +162,95 @@ void BuildGammaTable16(float prescale, float gamma, float scale, float base, uns
        }
 }
 
-void Texture_Gamma (void)
-{
-       int i, adjusted;
-       double invgamma;
-
-       texture_gamma = 1;
-       if ((i = COM_CheckParm("-gamma")))
-               texture_gamma = atof(com_argv[i+1]);
-       texture_gamma = bound(0.1, texture_gamma, 5.0);
-
-       if (texture_gamma == 1) // LordHavoc: dodge the math
-       {
-               for (i = 0;i < 256;i++)
-                       texgamma[i] = i;
-       }
-       else
-       {
-               invgamma = 1.0 / texture_gamma;
-               for (i = 0;i < 256;i++)
-               {
-                       adjusted = (int) ((255.0 * pow((double) i / 255.0, invgamma)) + 0.5);
-                       texgamma[i] = bound(0, adjusted, 255);
-               }
-       }
-}
-
 qboolean hardwaregammasupported = false;
 void VID_UpdateGamma(qboolean force)
 {
-       static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1, cachelighthalf = -1;
-       if (!force && vid_gamma.value == cachegamma && vid_brightness.value == cachebrightness && vid_contrast.value == cachecontrast && lighthalf == cachelighthalf)
+       static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1;
+       static int cacheoverbrightbits = -1, cachehwgamma = -1;
+
+       // LordHavoc: don't mess with gamma tables if running dedicated
+       if (cls.state == ca_dedicated)
+               return;
+
+       if (!force
+        && v_gamma.value == cachegamma
+        && v_contrast.value == cachecontrast
+        && v_brightness.value == cachebrightness
+        && v_overbrightbits.integer == cacheoverbrightbits
+        && v_hwgamma.value == cachehwgamma)
                return;
 
-       if (vid_gamma.value < 0.1)
-               Cvar_SetValue("vid_gamma", 0.1);
-       if (vid_gamma.value > 5.0)
-               Cvar_SetValue("vid_gamma", 5.0);
+       if (v_gamma.value < 0.1)
+               Cvar_SetValue("v_gamma", 0.1);
+       if (v_gamma.value > 5.0)
+               Cvar_SetValue("v_gamma", 5.0);
 
-       if (vid_brightness.value < 1.0)
-               Cvar_SetValue("vid_brightness", 1.0);
-       if (vid_brightness.value > 5.0)
-               Cvar_SetValue("vid_brightness", 5.0);
+       if (v_contrast.value < 0.5)
+               Cvar_SetValue("v_contrast", 0.5);
+       if (v_contrast.value > 5.0)
+               Cvar_SetValue("v_contrast", 5.0);
 
-       if (vid_contrast.value < 0.2)
-               Cvar_SetValue("vid_contrast", 0.2);
-       if (vid_contrast.value > 1)
-               Cvar_SetValue("vid_contrast", 1);
+       if (v_brightness.value < 0)
+               Cvar_SetValue("v_brightness", 0);
+       if (v_brightness.value > 0.8)
+               Cvar_SetValue("v_brightness", 0.8);
 
-       cachegamma = vid_gamma.value;
-       cachebrightness = vid_brightness.value;
-       cachecontrast = vid_contrast.value;
-       cachelighthalf = lighthalf;
+       cachegamma = v_gamma.value;
+       cachecontrast = v_contrast.value;
+       cachebrightness = v_brightness.value;
+       cacheoverbrightbits = v_overbrightbits.integer;
 
-       hardwaregammasupported = VID_SetGamma((cachelighthalf ? 2.0f : 1.0f), cachegamma, cachebrightness * cachecontrast, 1 - cachecontrast);
+       hardwaregammasupported = VID_SetGamma((float) (1 << cacheoverbrightbits), cachegamma, cachecontrast, cachebrightness);
        if (!hardwaregammasupported)
+       {
                Con_Printf("Hardware gamma not supported.\n");
+               Cvar_SetValue("v_hwgamma", 0);
+       }
+       cachehwgamma = v_hwgamma.integer;
 }
 
 void Gamma_Init(void)
 {
-       Cvar_RegisterVariable(&vid_gamma);
-       Cvar_RegisterVariable(&vid_brightness);
-       Cvar_RegisterVariable(&vid_contrast);
+       Cvar_RegisterVariable(&v_gamma);
+       Cvar_RegisterVariable(&v_brightness);
+       Cvar_RegisterVariable(&v_contrast);
+       Cvar_RegisterVariable(&v_hwgamma);
+       Cvar_RegisterVariable(&v_overbrightbits);
 }
 
 void Palette_Init(void)
 {
-       byte *pal;
-       pal = (byte *)COM_LoadMallocFile ("gfx/palette.lmp", false);
+       int i;
+       float gamma, scale, base;
+       qbyte *pal;
+       qbyte temp[256];
+       pal = (qbyte *)COM_LoadFile ("gfx/palette.lmp", false);
        if (!pal)
                Sys_Error ("Couldn't load gfx/palette.lmp");
        memcpy(host_basepal, pal, 765);
-       qfree(pal);
+       Mem_Free(pal);
        host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
+
+       gamma = 1;
+       scale = 1;
+       base = 0;
+       i = COM_CheckParm("-texgamma");
+       if (i)
+               gamma = atof(com_argv[i + 1]);
+       i = COM_CheckParm("-texcontrast");
+       if (i)
+               scale = atof(com_argv[i + 1]);
+       i = COM_CheckParm("-texbrightness");
+       if (i)
+               base = atof(com_argv[i + 1]);
+       gamma = bound(0.01, gamma, 10.0);
+       scale = bound(0.01, scale, 10.0);
+       base = bound(0, base, 0.95);
+
+       BuildGammaTable8(1.0f, gamma, scale, base, temp);
+       for (i = 3;i < 765;i++)
+               host_basepal[i] = temp[host_basepal[i]];
+
        Palette_Setup8to24();
-//     Palette_Setup15to8();
-       Texture_Gamma();
 }
+