Linux GLX, a lot of code shrinkage/cleanup, assembly support.
[divverent/darkplaces.git] / vid_shared.c
1
2 #include "quakedef.h"
3
4 unsigned d_8to24table[256];
5 unsigned char d_15to8table[32768]; // LordHavoc: was 64k elements, now 32k like it should be
6
7 void    VID_SetPalette (unsigned char *palette)
8 {
9         byte    *out;
10         unsigned short i;
11
12         out = (byte *) d_8to24table; // d_8to24table is accessed as 32bit for speed reasons, but is created as 8bit bytes
13         for (i=0 ; i<255 ; i++)
14         {
15                 *out++ = *palette++;
16                 *out++ = *palette++;
17                 *out++ = *palette++;
18                 *out++ = 255;
19         }
20         d_8to24table[255] = 0;
21 }
22
23 void    VID_Setup15to8Palette ()
24 {
25         byte    *pal;
26         unsigned r,g,b;
27         unsigned v;
28         int     r1,g1,b1;
29         int             j,k,l;
30         unsigned short i;
31
32         // JACK: 3D distance calcs - k is last closest, l is the distance.
33         // FIXME: Precalculate this and cache to disk.
34         for (i = 0;i < 32768;i++)
35         {
36                 /* Maps
37                         000000000000000
38                         000000000011111 = Red  = 0x001F
39                         000001111100000 = Blue = 0x03E0
40                         111110000000000 = Grn  = 0x7C00
41                 */
42                 r = ((i & 0x001F) << 3)+4;
43                 g = ((i & 0x03E0) >> 2)+4;
44                 b = ((i & 0x7C00) >> 7)+4;
45                 pal = (unsigned char *)d_8to24table;
46                 for (v = 0, k = 0, l = 1000000000;v < 256;v++, pal += 4)
47                 {
48                         r1 = r - pal[0];
49                         g1 = g - pal[1];
50                         b1 = b - pal[2];
51                         j = (r1*r1*2)+(g1*g1*3)+(b1*b1); // LordHavoc: weighting to tune for human eye (added *2 and *3)
52                         if (j < l)
53                         {
54                                 k = v;
55                                 l = j;
56                         }
57                 }
58                 d_15to8table[i] = k;
59         }
60 }
61
62 // LordHavoc: gamma correction does not belong in gl_vidnt.c
63 byte qgamma[256];
64 static float vid_gamma = 1.0;
65
66 void Check_Gamma (unsigned char *pal)
67 {
68         float   inf;
69         int             i;
70
71         if ((i = COM_CheckParm("-gamma")))
72                 vid_gamma = atof(com_argv[i+1]);
73         else
74         {
75 //              if ((gl_renderer && strstr(gl_renderer, "Voodoo")) ||
76 //                      (gl_vendor && strstr(gl_vendor, "3Dfx")))
77                         vid_gamma = 1;
78 //              else if (gl_vendor && strstr(gl_vendor, "ATI"))
79 //                      vid_gamma = 1;
80 //              else
81 //                      vid_gamma = 0.7;
82         }
83
84         if (vid_gamma == 1) // LordHavoc: dodge the math
85         {
86                 for (i = 0;i < 256;i++)
87                         qgamma[i] = i;
88         }
89         else
90         {
91                 for (i = 0;i < 256;i++)
92                 {
93                         inf = pow((i+1)/256.0, vid_gamma)*255 + 0.5;
94                         if (inf < 0) inf = 0;
95                         if (inf > 255) inf = 255;
96                         qgamma[i] = inf;
97                 }
98         }
99
100         // gamma correct the palette
101         for (i=0 ; i<768 ; i++)
102                 pal[i] = qgamma[pal[i]];
103         // note: 32bit uploads are corrected by the upload functions
104 }