]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - palette.c
colors for DrawQ_Mesh are now float rather than byte, and vertices are padded to...
[xonotic/darkplaces.git] / palette.c
1
2 #include "quakedef.h"
3
4 unsigned int d_8to24table[256];
5 qbyte host_basepal[768];
6
7 cvar_t v_gamma = {CVAR_SAVE, "v_gamma", "1"};
8 cvar_t v_contrast = {CVAR_SAVE, "v_contrast", "1"};
9 cvar_t v_brightness = {CVAR_SAVE, "v_brightness", "0"};
10 cvar_t v_overbrightbits = {CVAR_SAVE, "v_overbrightbits", "0"};
11 cvar_t v_hwgamma = {0, "v_hwgamma", "1"};
12
13 void Palette_Setup8to24(void)
14 {
15         int i;
16         qbyte *in, *out;
17
18         in = host_basepal;
19         out = (qbyte *) d_8to24table; // d_8to24table is accessed as 32bit for speed reasons, but is created as 8bit bytes
20         for (i=0 ; i<255 ; i++)
21         {
22                 *out++ = *in++;
23                 *out++ = *in++;
24                 *out++ = *in++;
25                 *out++ = 255;
26         }
27         d_8to24table[255] = 0; // completely transparent black
28 }
29
30
31 void BuildGammaTable8(float prescale, float gamma, float scale, float base, qbyte *out)
32 {
33         int i, adjusted;
34         double invgamma, d;
35
36         gamma = bound(0.1, gamma, 5.0);
37         if (gamma == 1) // LordHavoc: dodge the math
38         {
39                 for (i = 0;i < 256;i++)
40                 {
41                         adjusted = (int) (i * prescale * scale + base * 255.0);
42                         out[i] = bound(0, adjusted, 255);
43                 }
44         }
45         else
46         {
47                 invgamma = 1.0 / gamma;
48                 prescale /= 255.0f;
49                 for (i = 0;i < 256;i++)
50                 {
51                         d = pow((double) i * prescale, invgamma) * scale + base;
52                         adjusted = (int) (255.0 * d);
53                         out[i] = bound(0, adjusted, 255);
54                 }
55         }
56 }
57
58 void BuildGammaTable16(float prescale, float gamma, float scale, float base, unsigned short *out)
59 {
60         int i, adjusted;
61         double invgamma, d;
62
63         gamma = bound(0.1, gamma, 5.0);
64         if (gamma == 1) // LordHavoc: dodge the math
65         {
66                 for (i = 0;i < 256;i++)
67                 {
68                         adjusted = (int) (i * 256.0 * prescale * scale + base * 65535.0);
69                         out[i] = bound(0, adjusted, 65535);
70                 }
71         }
72         else
73         {
74                 invgamma = 1.0 / gamma;
75                 prescale /= 255.0f;
76                 for (i = 0;i < 256;i++)
77                 {
78                         d = pow((double) i * prescale, invgamma) * scale + base;
79                         adjusted = (int) (65535.0 * d);
80                         out[i] = bound(0, adjusted, 65535);
81                 }
82         }
83 }
84
85 qboolean hardwaregammasupported = false;
86 void VID_UpdateGamma(qboolean force)
87 {
88         static float cachegamma = -1, cachebrightness = -1, cachecontrast = -1;
89         static int cacheoverbrightbits = -1, cachehwgamma = -1;
90
91         // LordHavoc: don't mess with gamma tables if running dedicated
92         if (cls.state == ca_dedicated)
93                 return;
94
95         if (!force
96          && v_gamma.value == cachegamma
97          && v_contrast.value == cachecontrast
98          && v_brightness.value == cachebrightness
99          && v_overbrightbits.integer == cacheoverbrightbits
100          && v_hwgamma.value == cachehwgamma)
101                 return;
102
103         if (v_gamma.value < 0.1)
104                 Cvar_SetValue("v_gamma", 0.1);
105         if (v_gamma.value > 5.0)
106                 Cvar_SetValue("v_gamma", 5.0);
107
108         if (v_contrast.value < 0.5)
109                 Cvar_SetValue("v_contrast", 0.5);
110         if (v_contrast.value > 5.0)
111                 Cvar_SetValue("v_contrast", 5.0);
112
113         if (v_brightness.value < 0)
114                 Cvar_SetValue("v_brightness", 0);
115         if (v_brightness.value > 0.8)
116                 Cvar_SetValue("v_brightness", 0.8);
117
118         cachegamma = v_gamma.value;
119         cachecontrast = v_contrast.value;
120         cachebrightness = v_brightness.value;
121         cacheoverbrightbits = v_overbrightbits.integer;
122
123         hardwaregammasupported = VID_SetGamma((float) (1 << cacheoverbrightbits), cachegamma, cachecontrast, cachebrightness);
124         if (!hardwaregammasupported)
125         {
126                 Con_Printf("Hardware gamma not supported.\n");
127                 Cvar_SetValue("v_hwgamma", 0);
128         }
129         cachehwgamma = v_hwgamma.integer;
130 }
131
132 void Gamma_Init(void)
133 {
134         Cvar_RegisterVariable(&v_gamma);
135         Cvar_RegisterVariable(&v_brightness);
136         Cvar_RegisterVariable(&v_contrast);
137         Cvar_RegisterVariable(&v_hwgamma);
138         Cvar_RegisterVariable(&v_overbrightbits);
139 }
140
141 void Palette_Init(void)
142 {
143         int i;
144         float gamma, scale, base;
145         qbyte *pal;
146         qbyte temp[256];
147         pal = (qbyte *)COM_LoadFile ("gfx/palette.lmp", false);
148         if (!pal)
149                 Sys_Error ("Couldn't load gfx/palette.lmp");
150         memcpy(host_basepal, pal, 765);
151         Mem_Free(pal);
152         host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
153
154         gamma = 1;
155         scale = 1;
156         base = 0;
157         i = COM_CheckParm("-texgamma");
158         if (i)
159                 gamma = atof(com_argv[i + 1]);
160         i = COM_CheckParm("-texcontrast");
161         if (i)
162                 scale = atof(com_argv[i + 1]);
163         i = COM_CheckParm("-texbrightness");
164         if (i)
165                 base = atof(com_argv[i + 1]);
166         gamma = bound(0.01, gamma, 10.0);
167         scale = bound(0.01, scale, 10.0);
168         base = bound(0, base, 0.95);
169
170         BuildGammaTable8(1.0f, gamma, scale, base, temp);
171         for (i = 3;i < 765;i++)
172                 host_basepal[i] = temp[host_basepal[i]];
173
174         Palette_Setup8to24();
175 }
176