1 /*
2    Copyright (C) 1999-2006 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 
Inverse16_BuildTable(void)75 void Inverse16_BuildTable( void ){
76 	int i;
77 
78 	/*
79 	** create the 16-to-8 table
80 	*/
81 	for ( i = 0; i < 65536; i++ )
82 	{
83 		int r = i & 31;
84 		int g = ( i >> 5 ) & 63;
85 		int b = ( i >> 11 ) & 31;
86 
87 		r <<= 3;
88 		g <<= 2;
89 		b <<= 3;
90 
91 		inverse16to8table[i] = BestColor( r, g, b, 0, 255 );
92 	}
93 }
94 
Alphalight_Thread(int i)95 void Alphalight_Thread( int i ){
96 	int j;
97 	float r, g, b;
98 	float mr, mg, mb, ma;
99 	float distortion, bestdistortion;
100 	float v;
101 
102 	r = ( i >> 10 ) * ( 1.0 / 16 );
103 	g = ( ( i >> 5 ) & 31 )  * ( 1.0 / 16 );
104 	b = ( i & 31 ) * ( 1.0 / 16 );
105 
106 	bestdistortion = 999999;
107 	for ( j = 0 ; j < 16 * 16 * 16 * 16 ; j++ )
108 	{
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 );
113 
114 		v = r * 0.5 - ( mr * ma + 0.5 * ( 1.0 - ma ) );
115 		distortion = v * v;
116 		v = g * 0.5 - ( mg * ma + 0.5 * ( 1.0 - ma ) );
117 		distortion += v * v;
118 		v = b * 0.5 - ( mb * ma + 0.5 * ( 1.0 - ma ) );
119 		distortion += v * v;
120 
121 		distortion *= 1.0 + ma * 4;
122 
123 		if ( distortion < bestdistortion ) {
124 			bestdistortion = distortion;
125 			alphamap[i] = j;
126 		}
127 	}
128 }
129 
Cmd_Alphalight(void)130 void Cmd_Alphalight( void ){
131 	char savename[1024];
132 
133 	GetScriptToken( false );
134 
135 	if ( g_release ) {
136 		ReleaseFile( token );
137 		return;
138 	}
139 
140 	sprintf( savename, "%s%s", gamedir, token );
141 	printf( "Building alphalight table...\n" );
142 
143 	RunThreadsOnIndividual( 32 * 32 * 32, true, Alphalight_Thread );
144 
145 	SaveFile( savename, (byte *)alphamap, sizeof( alphamap ) );
146 }
147 
148 
Cmd_Inverse16Table(void)149 void Cmd_Inverse16Table( void ){
150 	char savename[1024];
151 
152 	if ( g_release ) {
153 		sprintf( savename, "pics/16to8.dat" );
154 		ReleaseFile( savename );
155 		return;
156 	}
157 
158 	sprintf( savename, "%spics/16to8.dat", gamedir );
159 	printf( "Building inverse 16-to-8 table...\n" );
160 
161 	Inverse16_BuildTable();
162 
163 	SaveFile( savename, (byte *) inverse16to8table, sizeof( inverse16to8table ) );
164 }
165