Image_intrinsic.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "tr_local.h"
  23. #define DEFAULT_SIZE 16
  24. /*
  25. ==================
  26. idImage::MakeDefault
  27. the default image will be grey with a white box outline
  28. to allow you to see the mapping coordinates on a surface
  29. ==================
  30. */
  31. void idImage::MakeDefault() {
  32. int x, y;
  33. byte data[DEFAULT_SIZE][DEFAULT_SIZE][4];
  34. if ( com_developer.GetBool() ) {
  35. // grey center
  36. for ( y = 0 ; y < DEFAULT_SIZE ; y++ ) {
  37. for ( x = 0 ; x < DEFAULT_SIZE ; x++ ) {
  38. data[y][x][0] = 32;
  39. data[y][x][1] = 32;
  40. data[y][x][2] = 32;
  41. data[y][x][3] = 255;
  42. }
  43. }
  44. // white border
  45. for ( x = 0 ; x < DEFAULT_SIZE ; x++ ) {
  46. data[0][x][0] =
  47. data[0][x][1] =
  48. data[0][x][2] =
  49. data[0][x][3] = 255;
  50. data[x][0][0] =
  51. data[x][0][1] =
  52. data[x][0][2] =
  53. data[x][0][3] = 255;
  54. data[DEFAULT_SIZE-1][x][0] =
  55. data[DEFAULT_SIZE-1][x][1] =
  56. data[DEFAULT_SIZE-1][x][2] =
  57. data[DEFAULT_SIZE-1][x][3] = 255;
  58. data[x][DEFAULT_SIZE-1][0] =
  59. data[x][DEFAULT_SIZE-1][1] =
  60. data[x][DEFAULT_SIZE-1][2] =
  61. data[x][DEFAULT_SIZE-1][3] = 255;
  62. }
  63. } else {
  64. for ( y = 0 ; y < DEFAULT_SIZE ; y++ ) {
  65. for ( x = 0 ; x < DEFAULT_SIZE ; x++ ) {
  66. data[y][x][0] = 0;
  67. data[y][x][1] = 0;
  68. data[y][x][2] = 0;
  69. data[y][x][3] = 0;
  70. }
  71. }
  72. }
  73. GenerateImage( (byte *)data,
  74. DEFAULT_SIZE, DEFAULT_SIZE,
  75. TF_DEFAULT, TR_REPEAT, TD_DEFAULT );
  76. defaulted = true;
  77. }
  78. static void R_DefaultImage( idImage *image ) {
  79. image->MakeDefault();
  80. }
  81. static void R_WhiteImage( idImage *image ) {
  82. byte data[DEFAULT_SIZE][DEFAULT_SIZE][4];
  83. // solid white texture
  84. memset( data, 255, sizeof( data ) );
  85. image->GenerateImage( (byte *)data, DEFAULT_SIZE, DEFAULT_SIZE,
  86. TF_DEFAULT, TR_REPEAT, TD_DEFAULT );
  87. }
  88. static void R_BlackImage( idImage *image ) {
  89. byte data[DEFAULT_SIZE][DEFAULT_SIZE][4];
  90. // solid black texture
  91. memset( data, 0, sizeof( data ) );
  92. image->GenerateImage( (byte *)data, DEFAULT_SIZE, DEFAULT_SIZE,
  93. TF_DEFAULT, TR_REPEAT, TD_DEFAULT );
  94. }
  95. static void R_RGBA8Image( idImage *image ) {
  96. byte data[DEFAULT_SIZE][DEFAULT_SIZE][4];
  97. memset( data, 0, sizeof( data ) );
  98. data[0][0][0] = 16;
  99. data[0][0][1] = 32;
  100. data[0][0][2] = 48;
  101. data[0][0][3] = 96;
  102. image->GenerateImage( (byte *)data, DEFAULT_SIZE, DEFAULT_SIZE, TF_DEFAULT, TR_REPEAT, TD_LOOKUP_TABLE_RGBA );
  103. }
  104. static void R_DepthImage( idImage *image ) {
  105. byte data[DEFAULT_SIZE][DEFAULT_SIZE][4];
  106. memset( data, 0, sizeof( data ) );
  107. data[0][0][0] = 16;
  108. data[0][0][1] = 32;
  109. data[0][0][2] = 48;
  110. data[0][0][3] = 96;
  111. image->GenerateImage( (byte *)data, DEFAULT_SIZE, DEFAULT_SIZE, TF_NEAREST, TR_CLAMP, TD_DEPTH );
  112. }
  113. static void R_AlphaNotchImage( idImage *image ) {
  114. byte data[2][4];
  115. // this is used for alpha test clip planes
  116. data[0][0] = data[0][1] = data[0][2] = 255;
  117. data[0][3] = 0;
  118. data[1][0] = data[1][1] = data[1][2] = 255;
  119. data[1][3] = 255;
  120. image->GenerateImage( (byte *)data, 2, 1, TF_NEAREST, TR_CLAMP, TD_LOOKUP_TABLE_ALPHA );
  121. }
  122. static void R_FlatNormalImage( idImage *image ) {
  123. byte data[DEFAULT_SIZE][DEFAULT_SIZE][4];
  124. // flat normal map for default bunp mapping
  125. for ( int i = 0 ; i < 4 ; i++ ) {
  126. data[0][i][0] = 128;
  127. data[0][i][1] = 128;
  128. data[0][i][2] = 255;
  129. data[0][i][3] = 255;
  130. }
  131. image->GenerateImage( (byte *)data, 2, 2, TF_DEFAULT, TR_REPEAT, TD_BUMP );
  132. }
  133. /*
  134. ================
  135. R_CreateNoFalloffImage
  136. This is a solid white texture that is zero clamped.
  137. ================
  138. */
  139. static void R_CreateNoFalloffImage( idImage *image ) {
  140. int x,y;
  141. byte data[16][FALLOFF_TEXTURE_SIZE][4];
  142. memset( data, 0, sizeof( data ) );
  143. for (x=1 ; x<FALLOFF_TEXTURE_SIZE-1 ; x++) {
  144. for (y=1 ; y<15 ; y++) {
  145. data[y][x][0] = 255;
  146. data[y][x][1] = 255;
  147. data[y][x][2] = 255;
  148. data[y][x][3] = 255;
  149. }
  150. }
  151. image->GenerateImage( (byte *)data, FALLOFF_TEXTURE_SIZE, 16, TF_DEFAULT, TR_CLAMP_TO_ZERO, TD_LOOKUP_TABLE_MONO );
  152. }
  153. /*
  154. ================
  155. R_FogImage
  156. We calculate distance correctly in two planes, but the
  157. third will still be projection based
  158. ================
  159. */
  160. const int FOG_SIZE = 128;
  161. void R_FogImage( idImage *image ) {
  162. int x,y;
  163. byte data[FOG_SIZE][FOG_SIZE][4];
  164. int b;
  165. float step[256];
  166. int i;
  167. float remaining = 1.0;
  168. for ( i = 0 ; i < 256 ; i++ ) {
  169. step[i] = remaining;
  170. remaining *= 0.982f;
  171. }
  172. for (x=0 ; x<FOG_SIZE ; x++) {
  173. for (y=0 ; y<FOG_SIZE ; y++) {
  174. float d;
  175. d = idMath::Sqrt( (x - FOG_SIZE/2) * (x - FOG_SIZE/2)
  176. + (y - FOG_SIZE/2) * (y - FOG_SIZE / 2) );
  177. d /= FOG_SIZE/2-1;
  178. b = (byte)(d * 255);
  179. if ( b <= 0 ) {
  180. b = 0;
  181. } else if ( b > 255 ) {
  182. b = 255;
  183. }
  184. b = (byte)(255 * ( 1.0 - step[b] ));
  185. if ( x == 0 || x == FOG_SIZE-1 || y == 0 || y == FOG_SIZE-1 ) {
  186. b = 255; // avoid clamping issues
  187. }
  188. data[y][x][0] =
  189. data[y][x][1] =
  190. data[y][x][2] = 255;
  191. data[y][x][3] = b;
  192. }
  193. }
  194. image->GenerateImage( (byte *)data, FOG_SIZE, FOG_SIZE, TF_LINEAR, TR_CLAMP, TD_LOOKUP_TABLE_ALPHA );
  195. }
  196. /*
  197. ================
  198. FogFraction
  199. Height values below zero are inside the fog volume
  200. ================
  201. */
  202. static const float RAMP_RANGE = 8;
  203. static const float DEEP_RANGE = -30;
  204. static float FogFraction( float viewHeight, float targetHeight ) {
  205. float total = idMath::Fabs( targetHeight - viewHeight );
  206. // return targetHeight >= 0 ? 0 : 1.0;
  207. // only ranges that cross the ramp range are special
  208. if ( targetHeight > 0 && viewHeight > 0 ) {
  209. return 0.0;
  210. }
  211. if ( targetHeight < -RAMP_RANGE && viewHeight < -RAMP_RANGE ) {
  212. return 1.0;
  213. }
  214. float above;
  215. if ( targetHeight > 0 ) {
  216. above = targetHeight;
  217. } else if ( viewHeight > 0 ) {
  218. above = viewHeight;
  219. } else {
  220. above = 0;
  221. }
  222. float rampTop, rampBottom;
  223. if ( viewHeight > targetHeight ) {
  224. rampTop = viewHeight;
  225. rampBottom = targetHeight;
  226. } else {
  227. rampTop = targetHeight;
  228. rampBottom = viewHeight;
  229. }
  230. if ( rampTop > 0 ) {
  231. rampTop = 0;
  232. }
  233. if ( rampBottom < -RAMP_RANGE ) {
  234. rampBottom = -RAMP_RANGE;
  235. }
  236. float rampSlope = 1.0 / RAMP_RANGE;
  237. if ( !total ) {
  238. return -viewHeight * rampSlope;
  239. }
  240. float ramp = ( 1.0 - ( rampTop * rampSlope + rampBottom * rampSlope ) * -0.5 ) * ( rampTop - rampBottom );
  241. float frac = ( total - above - ramp ) / total;
  242. // after it gets moderately deep, always use full value
  243. float deepest = viewHeight < targetHeight ? viewHeight : targetHeight;
  244. float deepFrac = deepest / DEEP_RANGE;
  245. if ( deepFrac >= 1.0 ) {
  246. return 1.0;
  247. }
  248. frac = frac * ( 1.0 - deepFrac ) + deepFrac;
  249. return frac;
  250. }
  251. /*
  252. ================
  253. R_FogEnterImage
  254. Modulate the fog alpha density based on the distance of the
  255. start and end points to the terminator plane
  256. ================
  257. */
  258. void R_FogEnterImage( idImage *image ) {
  259. int x,y;
  260. byte data[FOG_ENTER_SIZE][FOG_ENTER_SIZE][4];
  261. int b;
  262. for (x=0 ; x<FOG_ENTER_SIZE ; x++) {
  263. for (y=0 ; y<FOG_ENTER_SIZE ; y++) {
  264. float d;
  265. d = FogFraction( x - (FOG_ENTER_SIZE / 2), y - (FOG_ENTER_SIZE / 2) );
  266. b = (byte)(d * 255);
  267. if ( b <= 0 ) {
  268. b = 0;
  269. } else if ( b > 255 ) {
  270. b = 255;
  271. }
  272. data[y][x][0] =
  273. data[y][x][1] =
  274. data[y][x][2] = 255;
  275. data[y][x][3] = b;
  276. }
  277. }
  278. // if mipmapped, acutely viewed surfaces fade wrong
  279. image->GenerateImage( (byte *)data, FOG_ENTER_SIZE, FOG_ENTER_SIZE, TF_LINEAR, TR_CLAMP, TD_LOOKUP_TABLE_ALPHA );
  280. }
  281. /*
  282. ================
  283. R_QuadraticImage
  284. ================
  285. */
  286. static const int QUADRATIC_WIDTH = 32;
  287. static const int QUADRATIC_HEIGHT = 4;
  288. void R_QuadraticImage( idImage *image ) {
  289. int x,y;
  290. byte data[QUADRATIC_HEIGHT][QUADRATIC_WIDTH][4];
  291. int b;
  292. for (x=0 ; x<QUADRATIC_WIDTH ; x++) {
  293. for (y=0 ; y<QUADRATIC_HEIGHT ; y++) {
  294. float d;
  295. d = x - (QUADRATIC_WIDTH/2 - 0.5);
  296. d = idMath::Fabs( d );
  297. d -= 0.5;
  298. d /= QUADRATIC_WIDTH/2;
  299. d = 1.0 - d;
  300. d = d * d;
  301. b = (byte)(d * 255);
  302. if ( b <= 0 ) {
  303. b = 0;
  304. } else if ( b > 255 ) {
  305. b = 255;
  306. }
  307. data[y][x][0] =
  308. data[y][x][1] =
  309. data[y][x][2] = b;
  310. data[y][x][3] = 255;
  311. }
  312. }
  313. image->GenerateImage( (byte *)data, QUADRATIC_WIDTH, QUADRATIC_HEIGHT, TF_DEFAULT, TR_CLAMP, TD_LOOKUP_TABLE_RGB1 );
  314. }
  315. /*
  316. ================
  317. idImageManager::CreateIntrinsicImages
  318. ================
  319. */
  320. void idImageManager::CreateIntrinsicImages() {
  321. // create built in images
  322. defaultImage = ImageFromFunction( "_default", R_DefaultImage );
  323. whiteImage = ImageFromFunction( "_white", R_WhiteImage );
  324. blackImage = ImageFromFunction( "_black", R_BlackImage );
  325. flatNormalMap = ImageFromFunction( "_flat", R_FlatNormalImage );
  326. alphaNotchImage = ImageFromFunction( "_alphaNotch", R_AlphaNotchImage );
  327. fogImage = ImageFromFunction( "_fog", R_FogImage );
  328. fogEnterImage = ImageFromFunction( "_fogEnter", R_FogEnterImage );
  329. noFalloffImage = ImageFromFunction( "_noFalloff", R_CreateNoFalloffImage );
  330. ImageFromFunction( "_quadratic", R_QuadraticImage );
  331. // scratchImage is used for screen wipes/doublevision etc..
  332. scratchImage = ImageFromFunction("_scratch", R_RGBA8Image );
  333. scratchImage2 = ImageFromFunction("_scratch2", R_RGBA8Image );
  334. accumImage = ImageFromFunction("_accum", R_RGBA8Image );
  335. currentRenderImage = ImageFromFunction("_currentRender", R_RGBA8Image );
  336. currentDepthImage = ImageFromFunction("_currentDepth", R_DepthImage );
  337. // save a copy of this for material comparison, because currentRenderImage may get
  338. // reassigned during stereo rendering
  339. originalCurrentRenderImage = currentRenderImage;
  340. loadingIconImage = ImageFromFile("textures/loadingicon2", TF_DEFAULT, TR_CLAMP, TD_DEFAULT, CF_2D );
  341. hellLoadingIconImage = ImageFromFile("textures/loadingicon3", TF_DEFAULT, TR_CLAMP, TD_DEFAULT, CF_2D );
  342. release_assert( loadingIconImage->referencedOutsideLevelLoad );
  343. release_assert( hellLoadingIconImage->referencedOutsideLevelLoad );
  344. }