]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - palette.c
corrected comment again, indicating that the offset for polygon 3 is in the bottom...
[xonotic/darkplaces.git] / palette.c
1
2 #include "quakedef.h"
3
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];
13
14 qbyte host_basepal[768];
15
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"};
21
22 void Palette_Setup8to24(void)
23 {
24         int i;
25         int fullbright_start, fullbright_end;
26         int pants_start, pants_end;
27         int shirt_start, shirt_end;
28         int reversed_start, reversed_end;
29         qbyte *in, *out;
30         qbyte *colormap;
31
32         in = host_basepal;
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++)
35         {
36                 *out++ = *in++;
37                 *out++ = *in++;
38                 *out++ = *in++;
39                 *out++ = 255;
40         }
41         palette_complete[255] = 0; // completely transparent black
42
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];
47         else
48                 fullbright_start = 256;
49         if (colormap)
50                 Mem_Free(colormap);
51         fullbright_end = 256;
52         pants_start = 96;
53         pants_end = 112;
54         shirt_start = 16;
55         shirt_end = 32;
56         reversed_start = 128;
57         reversed_end = 224;
58
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;
64
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;
70
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;
80
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;
88
89         for (i = 0;i < 256;i++)
90                 palette_pantsaswhite[i] = palette_complete[0];
91         for (i = pants_start;i < pants_end;i++)
92         {
93                 if (i >= reversed_start && i < reversed_end)
94                         palette_pantsaswhite[i] = palette_complete[15 - (i - pants_start)];
95                 else
96                         palette_pantsaswhite[i] = palette_complete[i - pants_start];
97         }
98
99         for (i = 0;i < 256;i++)
100                 palette_shirtaswhite[i] = palette_complete[0];
101         for (i = shirt_start;i < shirt_end;i++)
102         {
103                 if (i >= reversed_start && i < reversed_end)
104                         palette_shirtaswhite[i] = palette_complete[15 - (i - shirt_start)];
105                 else
106                         palette_shirtaswhite[i] = palette_complete[i - shirt_start];
107         }
108
109         for (i = 0;i < 255;i++)
110                 palette_alpha[i] = 0xFFFFFFFF;
111         palette_alpha[255] = 0;
112
113         palette_font[0] = 0;
114         for (i = 1;i < 255;i++)
115                 palette_font[i] = palette_complete[i];
116         palette_font[255] = 0;
117 }
118
119
120 void BuildGammaTable8(float prescale, float gamma, float scale, float base, qbyte *out)
121 {
122         int i, adjusted;
123         double invgamma, d;
124
125         gamma = bound(0.1, gamma, 5.0);
126         if (gamma == 1) // LordHavoc: dodge the math
127         {
128                 for (i = 0;i < 256;i++)
129                 {
130                         adjusted = (int) (i * prescale * scale + base * 255.0);
131                         out[i] = bound(0, adjusted, 255);
132                 }
133         }
134         else
135         {
136                 invgamma = 1.0 / gamma;
137                 prescale /= 255.0f;
138                 for (i = 0;i < 256;i++)
139                 {
140                         d = pow((double) i * prescale, invgamma) * scale + base;
141                         adjusted = (int) (255.0 * d);
142                         out[i] = bound(0, adjusted, 255);
143                 }
144         }
145 }
146
147 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
148 {
149         int i, adjusted;
150         double invgamma, d;
151
152         gamma = bound(0.1, gamma, 5.0);
153         if (gamma == 1) // LordHavoc: dodge the math
154         {
155                 for (i = 0;i < 256;i++)
156                 {
157                         adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
158                         out[i] = bound(0, adjusted, 65535);
159                 }
160         }
161         else
162         {
163                 invgamma = 1.0 / gamma;
164                 prescale /= 255.0f;
165                 for (i = 0;i < 256;i++)
166                 {
167                         d = pow((double) i * prescale, invgamma) * scale + base;
168                         adjusted = (int) (65535.0 * d);
169                         out[i] = bound(0, adjusted, 65535);
170                 }
171         }
172 }
173
174 qboolean hardwaregammasupported = false;
175 void VID_UpdateGamma(qboolean force)
176 {
177         static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1;
178         static int cacheoverbrightbits = -1, cachehwgamma = -1;
179
180         // LordHavoc: don't mess with gamma tables if running dedicated
181         if (cls.state == ca_dedicated)
182                 return;
183
184         if (!force
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)
190                 return;
191
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);
196
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);
201
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);
206
207         cachegamma = v_gamma.value;
208         cachecontrast = v_contrast.value;
209         cachebrightness = v_brightness.value;
210         cacheoverbrightbits = v_overbrightbits.integer;
211
212         hardwaregammasupported = VID_SetGamma((float) (1 << cacheoverbrightbits), cachegamma, cachecontrast, cachebrightness);
213         if (!hardwaregammasupported)
214         {
215                 Con_Printf("Hardware gamma not supported.\n");
216                 Cvar_SetValue("v_hwgamma", 0);
217         }
218         cachehwgamma = v_hwgamma.integer;
219 }
220
221 void Gamma_Init(void)
222 {
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);
228 }
229
230 void Palette_Init(void)
231 {
232         int i;
233         float gamma, scale, base;
234         qbyte *pal;
235         qbyte temp[256];
236         pal = (qbyte *)COM_LoadFile ("gfx/palette.lmp", false);
237         if (!pal)
238                 Sys_Error ("Couldn't load gfx/palette.lmp");
239         memcpy(host_basepal, pal, 765);
240         Mem_Free(pal);
241         host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
242
243         gamma = 1;
244         scale = 1;
245         base = 0;
246         i = COM_CheckParm("-texgamma");
247         if (i)
248                 gamma = atof(com_argv[i + 1]);
249         i = COM_CheckParm("-texcontrast");
250         if (i)
251                 scale = atof(com_argv[i + 1]);
252         i = COM_CheckParm("-texbrightness");
253         if (i)
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);
258
259         BuildGammaTable8(1.0f, gamma, scale, base, temp);
260         for (i = 3;i < 765;i++)
261                 host_basepal[i] = temp[host_basepal[i]];
262
263         Palette_Setup8to24();
264 }
265