]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/qdata/tables.c
ported over the 1.5 branch version of q3map2 which is newer
[xonotic/netradiant.git] / tools / quake2 / qdata / tables.c
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #include "qdata.h"
23
24 /*
25 =============================================================================
26
27 ALPHALIGHT GENERATION
28
29 Find alphamap values that best match modulated lightmap values
30
31 This isn't used anymore, but I'm keeping it around...
32 =============================================================================
33 */
34
35 unsigned short  alphamap[32*32*32];
36 unsigned char   inverse16to8table[65536];
37
38 /*
39 static int FindNearestColor( unsigned int color )
40 {
41         int i;
42         int closest_so_far = 0;
43         float closest_distance_so_far = 100000000;
44         float d;
45         float r[2], g[2], b[2];
46
47         // incoming color is assumed to be in 0xRRGGBB format
48         r[0] = ( color & 31 ) << 3;
49         g[0] = ( ( color >> 5 ) & 63 ) << 2;
50         b[0] = ( ( color >> 11 ) & 31 ) << 3;
51
52         for ( i = 0; i < 256; i++ )
53         {
54                 r[1] = ( d_8to24table[i] >> 0 ) & 0xFF;
55                 g[1] = ( d_8to24table[i] >> 8 ) & 0xFF;
56                 b[1] = ( d_8to24table[i] >> 16 ) & 0xFF;
57
58                 d = ( r[1] - r[0] ) * ( r[1] - r[0] ) +
59                         ( g[1] - g[0] ) * ( g[1] - g[0] ) +
60                         ( b[1] - b[0] ) * ( b[1] - b[0] );
61
62                 if ( d < closest_distance_so_far )
63                 {
64                         closest_distance_so_far = d;
65                         closest_so_far = i;
66                 }
67         }
68
69         return closest_so_far;
70 }
71 */
72
73 extern byte BestColor( int, int, int, int, int );
74
75 void Inverse16_BuildTable( void )
76 {
77         int i;
78
79         /*
80         ** create the 16-to-8 table
81         */
82         for ( i = 0; i < 65536; i++ )
83         {
84                 int r = i & 31;
85                 int g = ( i >> 5 ) & 63;
86                 int b = ( i >> 11 ) & 31;
87
88                 r <<= 3;
89                 g <<= 2;
90                 b <<= 3;
91
92                 inverse16to8table[i] = BestColor( r, g, b, 0, 255 );
93         }
94 }
95
96 void Alphalight_Thread (int i)
97 {
98         int             j;
99         float   r, g, b;
100         float   mr, mg, mb, ma;
101         float   distortion, bestdistortion;
102         float   v;
103
104         r = (i>>10) * (1.0/16);
105         g = ((i>>5)&31)  * (1.0/16);
106         b = (i&31) * (1.0/16);
107
108         bestdistortion = 999999;
109         for (j=0 ; j<16*16*16*16 ; j++)
110         {
111                 mr = (j>>12) * (1.0/16);
112                 mg = ((j>>8)&15) * (1.0/16);
113                 mb = ((j>>4)&15) * (1.0/16);
114                 ma = (j&15) * (1.0/16);
115
116                 v = r * 0.5 - (mr*ma + 0.5*(1.0-ma));
117                 distortion = v*v;
118                 v = g * 0.5 - (mg*ma + 0.5*(1.0-ma));
119                 distortion += v*v;
120                 v = b * 0.5 - (mb*ma + 0.5*(1.0-ma));
121                 distortion += v*v;
122
123                 distortion *= 1.0 + ma*4;
124
125                 if (distortion < bestdistortion)
126                 {
127                         bestdistortion = distortion;
128                         alphamap[i] = j;
129                 }
130         }
131 }
132
133 void Cmd_Alphalight (void)
134 {
135         char    savename[1024];
136
137         GetToken (false);
138
139         if (g_release)
140         {
141                 ReleaseFile (token);
142                 return;
143         }
144
145         sprintf (savename, "%s%s", gamedir, token);
146         printf ("Building alphalight table...\n");
147
148         RunThreadsOnIndividual (32*32*32, true, Alphalight_Thread);
149
150         SaveFile (savename, (byte *)alphamap, sizeof(alphamap));
151 }
152
153
154 void Cmd_Inverse16Table( void )
155 {
156         char savename[1024];
157
158         if ( g_release )
159         {
160                 sprintf (savename, "pics/16to8.dat");
161                 ReleaseFile( savename );
162                 return;
163         }
164
165         sprintf (savename, "%spics/16to8.dat", gamedir);
166         printf ("Building inverse 16-to-8 table...\n");
167
168         Inverse16_BuildTable();
169
170         SaveFile( savename, (byte *) inverse16to8table, sizeof( inverse16to8table ) );
171 }