]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - palette.c
removed model_t mdlmd2data_frames (no longer needed)
[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_pantsaswhite[256];
9 unsigned int palette_shirtaswhite[256];
10 unsigned int palette_alpha[256];
11 unsigned int palette_font[256];
12
13 qbyte host_basepal[768];
14
15 cvar_t v_gamma = {CVAR_SAVE, "v_gamma", "1"};
16 cvar_t v_contrast = {CVAR_SAVE, "v_contrast", "1"};
17 cvar_t v_brightness = {CVAR_SAVE, "v_brightness", "0"};
18 cvar_t v_overbrightbits = {CVAR_SAVE, "v_overbrightbits", "0"};
19 cvar_t v_hwgamma = {0, "v_hwgamma", "1"};
20
21 void Palette_Setup8to24(void)
22 {
23         int i;
24         int fullbright_start, fullbright_end;
25         int pants_start, pants_end;
26         int shirt_start, shirt_end;
27         int reversed_start, reversed_end;
28         qbyte *in, *out;
29         qbyte *colormap;
30
31         in = host_basepal;
32         out = (qbyte *) palette_complete; // palette is accessed as 32bit for speed reasons, but is created as 8bit bytes
33         for (i = 0;i < 255;i++)
34         {
35                 *out++ = *in++;
36                 *out++ = *in++;
37                 *out++ = *in++;
38                 *out++ = 255;
39         }
40         palette_complete[255] = 0; // completely transparent black
41
42         // FIXME: fullbright_start should be read from colormap.lmp
43         colormap = COM_LoadFile("gfx/colormap.lmp", true);
44         if (colormap && com_filesize >= 16385)
45                 fullbright_start = 256 - colormap[16384];
46         else
47                 fullbright_start = 256;
48         if (colormap)
49                 Mem_Free(colormap);
50         fullbright_end = 256;
51         pants_start = 96;
52         pants_end = 112;
53         shirt_start = 16;
54         shirt_end = 32;
55         reversed_start = 128;
56         reversed_end = 224;
57
58         for (i = 0;i < fullbright_start;i++)
59                 palette_nofullbrights[i] = palette_complete[i];
60         for (i = fullbright_start;i < 255;i++)
61                 palette_nofullbrights[i] = palette_complete[0];
62         palette_nofullbrights[255] = 0;
63
64         for (i = 0;i < 256;i++)
65                 palette_onlyfullbrights[i] = palette_complete[0];
66         for (i = fullbright_start;i < fullbright_end;i++)
67                 palette_onlyfullbrights[i] = palette_complete[i];
68         palette_onlyfullbrights[255] = 0;
69
70         for (i = 0;i < 256;i++)
71                 palette_nocolormapnofullbrights[i] = palette_complete[i];
72         for (i = pants_start;i < pants_end;i++)
73                 palette_nocolormapnofullbrights[i] = palette_complete[0];
74         for (i = shirt_start;i < shirt_end;i++)
75                 palette_nocolormapnofullbrights[i] = palette_complete[0];
76         for (i = fullbright_start;i < fullbright_end;i++)
77                 palette_nocolormapnofullbrights[i] = palette_complete[0];
78         palette_nocolormapnofullbrights[255] = 0;
79
80         for (i = 0;i < 256;i++)
81                 palette_pantsaswhite[i] = palette_complete[0];
82         for (i = pants_start;i < pants_end;i++)
83         {
84                 if (i >= reversed_start && i < reversed_end)
85                         palette_pantsaswhite[i] = palette_complete[15 - (i - pants_start)];
86                 else
87                         palette_pantsaswhite[i] = palette_complete[i - pants_start];
88         }
89
90         for (i = 0;i < 256;i++)
91                 palette_shirtaswhite[i] = palette_complete[0];
92         for (i = shirt_start;i < shirt_end;i++)
93         {
94                 if (i >= reversed_start && i < reversed_end)
95                         palette_shirtaswhite[i] = palette_complete[15 - (i - shirt_start)];
96                 else
97                         palette_shirtaswhite[i] = palette_complete[i - shirt_start];
98         }
99
100         for (i = 0;i < 255;i++)
101                 palette_alpha[i] = 0xFFFFFFFF;
102         palette_alpha[255] = 0;
103
104         palette_font[0] = 0;
105         for (i = 1;i < 255;i++)
106                 palette_font[i] = palette_complete[i];
107         palette_font[255] = 0;
108 }
109
110
111 void BuildGammaTable8(float prescale, float gamma, float scale, float base, qbyte *out)
112 {
113         int i, adjusted;
114         double invgamma, d;
115
116         gamma = bound(0.1, gamma, 5.0);
117         if (gamma == 1) // LordHavoc: dodge the math
118         {
119                 for (i = 0;i < 256;i++)
120                 {
121                         adjusted = (int) (i * prescale * scale + base * 255.0);
122                         out[i] = bound(0, adjusted, 255);
123                 }
124         }
125         else
126         {
127                 invgamma = 1.0 / gamma;
128                 prescale /= 255.0f;
129                 for (i = 0;i < 256;i++)
130                 {
131                         d = pow((double) i * prescale, invgamma) * scale + base;
132                         adjusted = (int) (255.0 * d);
133                         out[i] = bound(0, adjusted, 255);
134                 }
135         }
136 }
137
138 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
139 {
140         int i, adjusted;
141         double invgamma, d;
142
143         gamma = bound(0.1, gamma, 5.0);
144         if (gamma == 1) // LordHavoc: dodge the math
145         {
146                 for (i = 0;i < 256;i++)
147                 {
148                         adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
149                         out[i] = bound(0, adjusted, 65535);
150                 }
151         }
152         else
153         {
154                 invgamma = 1.0 / gamma;
155                 prescale /= 255.0f;
156                 for (i = 0;i < 256;i++)
157                 {
158                         d = pow((double) i * prescale, invgamma) * scale + base;
159                         adjusted = (int) (65535.0 * d);
160                         out[i] = bound(0, adjusted, 65535);
161                 }
162         }
163 }
164
165 qboolean hardwaregammasupported = false;
166 void VID_UpdateGamma(qboolean force)
167 {
168         static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1;
169         static int cacheoverbrightbits = -1, cachehwgamma = -1;
170
171         // LordHavoc: don't mess with gamma tables if running dedicated
172         if (cls.state == ca_dedicated)
173                 return;
174
175         if (!force
176          && v_gamma.value == cachegamma
177          && v_contrast.value == cachecontrast
178          && v_brightness.value == cachebrightness
179          && v_overbrightbits.integer == cacheoverbrightbits
180          && v_hwgamma.value == cachehwgamma)
181                 return;
182
183         if (v_gamma.value < 0.1)
184                 Cvar_SetValue("v_gamma", 0.1);
185         if (v_gamma.value > 5.0)
186                 Cvar_SetValue("v_gamma", 5.0);
187
188         if (v_contrast.value < 0.5)
189                 Cvar_SetValue("v_contrast", 0.5);
190         if (v_contrast.value > 5.0)
191                 Cvar_SetValue("v_contrast", 5.0);
192
193         if (v_brightness.value < 0)
194                 Cvar_SetValue("v_brightness", 0);
195         if (v_brightness.value > 0.8)
196                 Cvar_SetValue("v_brightness", 0.8);
197
198         cachegamma = v_gamma.value;
199         cachecontrast = v_contrast.value;
200         cachebrightness = v_brightness.value;
201         cacheoverbrightbits = v_overbrightbits.integer;
202
203         hardwaregammasupported = VID_SetGamma((float) (1 << cacheoverbrightbits), cachegamma, cachecontrast, cachebrightness);
204         if (!hardwaregammasupported)
205         {
206                 Con_Printf("Hardware gamma not supported.\n");
207                 Cvar_SetValue("v_hwgamma", 0);
208         }
209         cachehwgamma = v_hwgamma.integer;
210 }
211
212 void Gamma_Init(void)
213 {
214         Cvar_RegisterVariable(&v_gamma);
215         Cvar_RegisterVariable(&v_brightness);
216         Cvar_RegisterVariable(&v_contrast);
217         Cvar_RegisterVariable(&v_hwgamma);
218         Cvar_RegisterVariable(&v_overbrightbits);
219 }
220
221 void Palette_Init(void)
222 {
223         int i;
224         float gamma, scale, base;
225         qbyte *pal;
226         qbyte temp[256];
227         pal = (qbyte *)COM_LoadFile ("gfx/palette.lmp", false);
228         if (!pal)
229                 Sys_Error ("Couldn't load gfx/palette.lmp");
230         memcpy(host_basepal, pal, 765);
231         Mem_Free(pal);
232         host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
233
234         gamma = 1;
235         scale = 1;
236         base = 0;
237         i = COM_CheckParm("-texgamma");
238         if (i)
239                 gamma = atof(com_argv[i + 1]);
240         i = COM_CheckParm("-texcontrast");
241         if (i)
242                 scale = atof(com_argv[i + 1]);
243         i = COM_CheckParm("-texbrightness");
244         if (i)
245                 base = atof(com_argv[i + 1]);
246         gamma = bound(0.01, gamma, 10.0);
247         scale = bound(0.01, scale, 10.0);
248         base = bound(0, base, 0.95);
249
250         BuildGammaTable8(1.0f, gamma, scale, base, temp);
251         for (i = 3;i < 765;i++)
252                 host_basepal[i] = temp[host_basepal[i]];
253
254         Palette_Setup8to24();
255 }
256