2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
25 =============================================================================
29 Find alphamap values that best match modulated lightmap values
31 This isn't used anymore, but I'm keeping it around...
32 =============================================================================
35 unsigned short alphamap[32*32*32];
36 unsigned char inverse16to8table[65536];
39 static int FindNearestColor( unsigned int color )
42 int closest_so_far = 0;
43 float closest_distance_so_far = 100000000;
45 float r[2], g[2], b[2];
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;
52 for ( i = 0; i < 256; i++ )
54 r[1] = ( d_8to24table[i] >> 0 ) & 0xFF;
55 g[1] = ( d_8to24table[i] >> 8 ) & 0xFF;
56 b[1] = ( d_8to24table[i] >> 16 ) & 0xFF;
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] );
62 if ( d < closest_distance_so_far )
64 closest_distance_so_far = d;
69 return closest_so_far;
73 extern byte BestColor( int, int, int, int, int );
75 void Inverse16_BuildTable( void )
80 ** create the 16-to-8 table
82 for ( i = 0; i < 65536; i++ )
85 int g = ( i >> 5 ) & 63;
86 int b = ( i >> 11 ) & 31;
92 inverse16to8table[i] = BestColor( r, g, b, 0, 255 );
96 void Alphalight_Thread (int i)
100 float mr, mg, mb, ma;
101 float distortion, bestdistortion;
104 r = (i>>10) * (1.0/16);
105 g = ((i>>5)&31) * (1.0/16);
106 b = (i&31) * (1.0/16);
108 bestdistortion = 999999;
109 for (j=0 ; j<16*16*16*16 ; j++)
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);
116 v = r * 0.5 - (mr*ma + 0.5*(1.0-ma));
118 v = g * 0.5 - (mg*ma + 0.5*(1.0-ma));
120 v = b * 0.5 - (mb*ma + 0.5*(1.0-ma));
123 distortion *= 1.0 + ma*4;
125 if (distortion < bestdistortion)
127 bestdistortion = distortion;
133 void Cmd_Alphalight (void)
137 GetScriptToken (false);
145 sprintf (savename, "%s%s", gamedir, token);
146 printf ("Building alphalight table...\n");
148 RunThreadsOnIndividual (32*32*32, true, Alphalight_Thread);
150 SaveFile (savename, (byte *)alphamap, sizeof(alphamap));
154 void Cmd_Inverse16Table( void )
160 sprintf (savename, "pics/16to8.dat");
161 ReleaseFile( savename );
165 sprintf (savename, "%spics/16to8.dat", gamedir);
166 printf ("Building inverse 16-to-8 table...\n");
168 Inverse16_BuildTable();
170 SaveFile( savename, (byte *) inverse16to8table, sizeof( inverse16to8table ) );