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