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