]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - palette.c
some whitespace changes
[xonotic/darkplaces.git] / palette.c
index f43efcfc82a9b946bfe0f3bc2a83170c7da0283f..b574d8029a649a12083d45edc22555da48600a28 100644 (file)
--- a/palette.c
+++ b/palette.c
@@ -4,8 +4,13 @@
 unsigned int d_8to24table[256];
 //byte d_15to8table[32768];
 byte host_basepal[768];
-byte qgamma[256];
-static float vid_gamma = 1.0;
+byte texgamma[256];
+
+static float texture_gamma = 1.0;
+
+cvar_t vid_gamma = {"vid_gamma", "1", true};
+cvar_t vid_brightness = {"vid_brightness", "1", true};
+cvar_t vid_contrast = {"vid_contrast", "1", true};
 
 void Palette_Setup8to24()
 {
@@ -57,32 +62,125 @@ void       Palette_Setup15to8()
 }
 */
 
-void Palette_Gamma ()
+void BuildGammaTable8(float prescale, float gamma, float scale, float base, byte *out)
+{
+       int i, adjusted;
+       double invgamma, d;
+
+       gamma = bound(0.1, gamma, 5.0);
+       if (gamma == 1) // LordHavoc: dodge the math
+       {
+               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);
+               }
+       }
+}
+
+void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
 {
-       float   inf;
-       int             i;
+       int i, adjusted;
+       double invgamma, d;
 
-       vid_gamma = 1;
+       gamma = bound(0.1, gamma, 5.0);
+       if (gamma == 1) // LordHavoc: dodge the math
+       {
+               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);
+               }
+       }
+}
+
+void Texture_Gamma ()
+{
+       int i, adjusted;
+       double invgamma;
+
+       texture_gamma = 1;
        if ((i = COM_CheckParm("-gamma")))
-               vid_gamma = atof(com_argv[i+1]);
+               texture_gamma = atof(com_argv[i+1]);
+       texture_gamma = bound(0.1, texture_gamma, 5.0);
 
-       if (vid_gamma == 1) // LordHavoc: dodge the math
+       if (texture_gamma == 1) // LordHavoc: dodge the math
        {
                for (i = 0;i < 256;i++)
-                       qgamma[i] = i;
+                       texgamma[i] = i;
        }
        else
        {
+               invgamma = 1.0 / texture_gamma;
                for (i = 0;i < 256;i++)
                {
-                       inf = pow((i+1)/256.0, vid_gamma)*255 + 0.5;
-                       if (inf < 0) inf = 0;
-                       if (inf > 255) inf = 255;
-                       qgamma[i] = inf;
+                       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)
+               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 (vid_brightness.value < 1.0)
+               Cvar_SetValue("vid_brightness", 1.0);
+       if (vid_brightness.value > 5.0)
+               Cvar_SetValue("vid_brightness", 5.0);
+
+       if (vid_contrast.value < 0.2)
+               Cvar_SetValue("vid_contrast", 0.2);
+       if (vid_contrast.value > 1)
+               Cvar_SetValue("vid_contrast", 1);
+
+       cachegamma = vid_gamma.value;
+       cachebrightness = vid_brightness.value;
+       cachecontrast = vid_contrast.value;
+       cachelighthalf = lighthalf;
+
+       hardwaregammasupported = VID_SetGamma((cachelighthalf ? 2.0f : 1.0f), cachegamma, cachebrightness * cachecontrast, 1 - cachecontrast);
+       if (!hardwaregammasupported)
+               Con_Printf("Hardware gamma not supported.\n");
+}
+
+void Gamma_Init()
+{
+       Cvar_RegisterVariable(&vid_gamma);
+       Cvar_RegisterVariable(&vid_brightness);
+       Cvar_RegisterVariable(&vid_contrast);
+}
+
 void Palette_Init()
 {
        byte *pal;
@@ -94,5 +192,5 @@ void Palette_Init()
        host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
        Palette_Setup8to24();
 //     Palette_Setup15to8();
-       Palette_Gamma();
+       Texture_Gamma();
 }