2 Copyright (C) 1999-2007 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 ){
79 ** create the 16-to-8 table
81 for ( i = 0; i < 65536; i++ )
84 int g = ( i >> 5 ) & 63;
85 int b = ( i >> 11 ) & 31;
91 inverse16to8table[i] = BestColor( r, g, b, 0, 255 );
95 void Alphalight_Thread( int i ){
99 float distortion, bestdistortion;
102 r = ( i >> 10 ) * ( 1.0 / 16 );
103 g = ( ( i >> 5 ) & 31 ) * ( 1.0 / 16 );
104 b = ( i & 31 ) * ( 1.0 / 16 );
106 bestdistortion = 999999;
107 for ( j = 0 ; j < 16 * 16 * 16 * 16 ; j++ )
109 mr = ( j >> 12 ) * ( 1.0 / 16 );
110 mg = ( ( j >> 8 ) & 15 ) * ( 1.0 / 16 );
111 mb = ( ( j >> 4 ) & 15 ) * ( 1.0 / 16 );
112 ma = ( j & 15 ) * ( 1.0 / 16 );
114 v = r * 0.5 - ( mr * ma + 0.5 * ( 1.0 - ma ) );
116 v = g * 0.5 - ( mg * ma + 0.5 * ( 1.0 - ma ) );
118 v = b * 0.5 - ( mb * ma + 0.5 * ( 1.0 - ma ) );
121 distortion *= 1.0 + ma * 4;
123 if ( distortion < bestdistortion ) {
124 bestdistortion = distortion;
130 void Cmd_Alphalight( void ){
133 GetScriptToken( false );
136 ReleaseFile( token );
140 sprintf( savename, "%s%s", gamedir, token );
141 printf( "Building alphalight table...\n" );
143 RunThreadsOnIndividual( 32 * 32 * 32, true, Alphalight_Thread );
145 SaveFile( savename, (byte *)alphamap, sizeof( alphamap ) );
149 void Cmd_Inverse16Table( void ){
153 sprintf( savename, "pics/16to8.dat" );
154 ReleaseFile( savename );
158 sprintf( savename, "%spics/16to8.dat", gamedir );
159 printf( "Building inverse 16-to-8 table...\n" );
161 Inverse16_BuildTable();
163 SaveFile( savename, (byte *) inverse16to8table, sizeof( inverse16to8table ) );