4 unsigned int palette_complete[256];
5 unsigned int palette_nofullbrights[256];
6 unsigned int palette_onlyfullbrights[256];
7 unsigned int palette_nocolormapnofullbrights[256];
8 unsigned int palette_nocolormap[256];
9 unsigned int palette_pantsaswhite[256];
10 unsigned int palette_shirtaswhite[256];
11 unsigned int palette_alpha[256];
12 unsigned int palette_font[256];
14 qbyte host_basepal[768];
16 cvar_t v_gamma = {CVAR_SAVE, "v_gamma", "1"};
17 cvar_t v_contrast = {CVAR_SAVE, "v_contrast", "1"};
18 cvar_t v_brightness = {CVAR_SAVE, "v_brightness", "0"};
19 cvar_t v_overbrightbits = {CVAR_SAVE, "v_overbrightbits", "0"};
20 cvar_t v_hwgamma = {0, "v_hwgamma", "1"};
22 void Palette_Setup8to24(void)
25 int fullbright_start, fullbright_end;
26 int pants_start, pants_end;
27 int shirt_start, shirt_end;
28 int reversed_start, reversed_end;
33 out = (qbyte *) palette_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
34 for (i = 0;i < 255;i++)
41 palette_complete[255] = 0; // completely transparent black
43 // FIXME: fullbright_start should be read from colormap.lmp
44 colormap = COM_LoadFile("gfx/colormap.lmp", true);
45 if (colormap && com_filesize >= 16385)
46 fullbright_start = 256 - colormap[16384];
48 fullbright_start = 256;
59 for (i = 0;i < fullbright_start;i++)
60 palette_nofullbrights[i] = palette_complete[i];
61 for (i = fullbright_start;i < 255;i++)
62 palette_nofullbrights[i] = palette_complete[0];
63 palette_nofullbrights[255] = 0;
65 for (i = 0;i < 256;i++)
66 palette_onlyfullbrights[i] = palette_complete[0];
67 for (i = fullbright_start;i < fullbright_end;i++)
68 palette_onlyfullbrights[i] = palette_complete[i];
69 palette_onlyfullbrights[255] = 0;
71 for (i = 0;i < 256;i++)
72 palette_nocolormapnofullbrights[i] = palette_complete[i];
73 for (i = pants_start;i < pants_end;i++)
74 palette_nocolormapnofullbrights[i] = palette_complete[0];
75 for (i = shirt_start;i < shirt_end;i++)
76 palette_nocolormapnofullbrights[i] = palette_complete[0];
77 for (i = fullbright_start;i < fullbright_end;i++)
78 palette_nocolormapnofullbrights[i] = palette_complete[0];
79 palette_nocolormapnofullbrights[255] = 0;
81 for (i = 0;i < 256;i++)
82 palette_nocolormap[i] = palette_complete[i];
83 for (i = pants_start;i < pants_end;i++)
84 palette_nocolormap[i] = palette_complete[0];
85 for (i = shirt_start;i < shirt_end;i++)
86 palette_nocolormap[i] = palette_complete[0];
87 palette_nocolormap[255] = 0;
89 for (i = 0;i < 256;i++)
90 palette_pantsaswhite[i] = palette_complete[0];
91 for (i = pants_start;i < pants_end;i++)
93 if (i >= reversed_start && i < reversed_end)
94 palette_pantsaswhite[i] = palette_complete[15 - (i - pants_start)];
96 palette_pantsaswhite[i] = palette_complete[i - pants_start];
99 for (i = 0;i < 256;i++)
100 palette_shirtaswhite[i] = palette_complete[0];
101 for (i = shirt_start;i < shirt_end;i++)
103 if (i >= reversed_start && i < reversed_end)
104 palette_shirtaswhite[i] = palette_complete[15 - (i - shirt_start)];
106 palette_shirtaswhite[i] = palette_complete[i - shirt_start];
109 for (i = 0;i < 255;i++)
110 palette_alpha[i] = 0xFFFFFFFF;
111 palette_alpha[255] = 0;
114 for (i = 1;i < 255;i++)
115 palette_font[i] = palette_complete[i];
116 palette_font[255] = 0;
120 void BuildGammaTable8(float prescale, float gamma, float scale, float base, qbyte *out)
125 gamma = bound(0.1, gamma, 5.0);
126 if (gamma == 1) // LordHavoc: dodge the math
128 for (i = 0;i < 256;i++)
130 adjusted = (int) (i * prescale * scale + base * 255.0);
131 out[i] = bound(0, adjusted, 255);
136 invgamma = 1.0 / gamma;
138 for (i = 0;i < 256;i++)
140 d = pow((double) i * prescale, invgamma) * scale + base;
141 adjusted = (int) (255.0 * d);
142 out[i] = bound(0, adjusted, 255);
147 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
152 gamma = bound(0.1, gamma, 5.0);
153 if (gamma == 1) // LordHavoc: dodge the math
155 for (i = 0;i < 256;i++)
157 adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
158 out[i] = bound(0, adjusted, 65535);
163 invgamma = 1.0 / gamma;
165 for (i = 0;i < 256;i++)
167 d = pow((double) i * prescale, invgamma) * scale + base;
168 adjusted = (int) (65535.0 * d);
169 out[i] = bound(0, adjusted, 65535);
174 qboolean hardwaregammasupported = false;
175 void VID_UpdateGamma(qboolean force)
177 static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1;
178 static int cacheoverbrightbits = -1, cachehwgamma = -1;
180 // LordHavoc: don't mess with gamma tables if running dedicated
181 if (cls.state == ca_dedicated)
185 && v_gamma.value == cachegamma
186 && v_contrast.value == cachecontrast
187 && v_brightness.value == cachebrightness
188 && v_overbrightbits.integer == cacheoverbrightbits
189 && v_hwgamma.value == cachehwgamma)
192 if (v_gamma.value < 0.1)
193 Cvar_SetValue("v_gamma", 0.1);
194 if (v_gamma.value > 5.0)
195 Cvar_SetValue("v_gamma", 5.0);
197 if (v_contrast.value < 0.5)
198 Cvar_SetValue("v_contrast", 0.5);
199 if (v_contrast.value > 5.0)
200 Cvar_SetValue("v_contrast", 5.0);
202 if (v_brightness.value < 0)
203 Cvar_SetValue("v_brightness", 0);
204 if (v_brightness.value > 0.8)
205 Cvar_SetValue("v_brightness", 0.8);
207 cachegamma = v_gamma.value;
208 cachecontrast = v_contrast.value;
209 cachebrightness = v_brightness.value;
210 cacheoverbrightbits = v_overbrightbits.integer;
212 hardwaregammasupported = VID_SetGamma((float) (1 << cacheoverbrightbits), cachegamma, cachecontrast, cachebrightness);
213 if (!hardwaregammasupported)
215 Con_Printf("Hardware gamma not supported.\n");
216 Cvar_SetValue("v_hwgamma", 0);
218 cachehwgamma = v_hwgamma.integer;
221 void Gamma_Init(void)
223 Cvar_RegisterVariable(&v_gamma);
224 Cvar_RegisterVariable(&v_brightness);
225 Cvar_RegisterVariable(&v_contrast);
226 Cvar_RegisterVariable(&v_hwgamma);
227 Cvar_RegisterVariable(&v_overbrightbits);
230 void Palette_Init(void)
233 float gamma, scale, base;
236 pal = (qbyte *)COM_LoadFile ("gfx/palette.lmp", false);
238 Sys_Error ("Couldn't load gfx/palette.lmp");
239 memcpy(host_basepal, pal, 765);
241 host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
246 i = COM_CheckParm("-texgamma");
248 gamma = atof(com_argv[i + 1]);
249 i = COM_CheckParm("-texcontrast");
251 scale = atof(com_argv[i + 1]);
252 i = COM_CheckParm("-texbrightness");
254 base = atof(com_argv[i + 1]);
255 gamma = bound(0.01, gamma, 10.0);
256 scale = bound(0.01, scale, 10.0);
257 base = bound(0, base, 0.95);
259 BuildGammaTable8(1.0f, gamma, scale, base, temp);
260 for (i = 3;i < 765;i++)
261 host_basepal[i] = temp[host_basepal[i]];
263 Palette_Setup8to24();