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