tables.c 3.8 KB

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