texproject.cpp 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/texproject.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Jani_p $*
  29. * *
  30. * $Modtime:: 11/29/01 1:00p $*
  31. * *
  32. * $Revision:: 20 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * TexProjectClass::TexProjectClass -- Constructor *
  37. * TexProjectClass::~TexProjectClass -- Destructor *
  38. * TexProjectClass::Set_Texture_Size -- Set the size of texture to use *
  39. * TexProjectClass::Get_Texture_Size -- Returns the stored texture size *
  40. * TexProjectClass::Set_Flag -- Turn specified flag on or off *
  41. * TexProjectClass::Get_Flag -- Get the current state of specified flag *
  42. * TexProjectClass::Set_Intensity -- Set the intensity of this projector *
  43. * TexProjectClass::Get_Intensity -- returns the current "desired" intensity *
  44. * TexProjectClass::Set_Attenuation -- Set the attenuation factor *
  45. * TexProjectClass::Get_Attenuation -- Returns the attenuation value *
  46. * TexProjectClass::Enable_Attenuation -- Set the state of the ATTENUATE flag *
  47. * TexProjectClass::Is_Attenuation_Enabled -- Get the state of the ATTENUATE flag *
  48. * TexProjectClass::Is_Depth_Gradient_Enabled -- returns whether the depth gradient is enabl *
  49. * TexProjectClass::Enable_Depth_Gradient -- enable/disable depth gradient *
  50. * TexProjectClass::Init_Multiplicative -- Initialize this to be a multiplicative texture pr *
  51. * TexProjectClass::Is_Intensity_Zero -- check if we can eliminate this projector *
  52. * TexProjectClass::Init_Additive -- Set up the projector to be additive *
  53. * TexProjectClass::Set_Perspective_Projection -- Set up a perspective projection *
  54. * TexProjectClass::Set_Ortho_Projection -- Set up an orthographic projection *
  55. * TexProjectClass::Set_Texture -- Set the texture to be projected *
  56. * TexProjectClass::Get_Texture -- Returns the texture being projected *
  57. * TexProjectClass::Peek_Texture -- Returns the texture being projected *
  58. * TexProjectClass::Peek_Material_Pass -- Returns the material pass object *
  59. * TexProjectClass::Compute_Perspective_Projection -- Set up a perspective projection of an *
  60. * TexProjectClass::Compute_Perspective_Projection -- Set up a perspective projection of an *
  61. * TexProjectClass::Compute_Ortho_Projection -- Automatic Orthographic projection *
  62. * TexProjectClass::Compute_Ortho_Projection -- Automatic Orthographic projection *
  63. * TexProjectClass::Compute_Texture -- Generates texture by rendering an object *
  64. * TexProjectClass::Configure_Camera -- setup a camera to match this projector *
  65. * TexProjectClass::Pre_Render_Update -- Prepare the projector for rendering *
  66. * TexProjectClass::Update_WS_Bounding_Volume -- Recalculate the world-space bounding box *
  67. * TexProjectClass::Get_Surface -- Returns pointer to the texture surface *
  68. * TexProjectClass::Set_Render_Target -- Install a render target for this projector to use *
  69. * TexProjectClass::Needs_Render_Target -- returns wheter this projector needs a render targ *
  70. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  71. #include "texproject.h"
  72. #include "vertmaterial.h"
  73. #include "shader.h"
  74. #include "texture.h"
  75. #include "rendobj.h"
  76. #include "rinfo.h"
  77. #include "camera.h"
  78. #include "matpass.h"
  79. #include "bwrender.h"
  80. #include "assetmgr.h"
  81. #include "dx8wrapper.h"
  82. // DEBUG DEBUG
  83. #include "mpu.h"
  84. #define DEBUG_SHADOW_RENDERING 0
  85. //#define DEFAULT_TEXTURE_SIZE 64
  86. const float INTENSITY_RATE_OF_CHANGE = 1.0f; // change in intensity per second
  87. /*
  88. **
  89. ** Shadow mapping: from pre-projected view coordinates back to world space
  90. ** then to shadow space
  91. **
  92. ** (1) Vshadow = PShadow * Mwrld-shadow * Vwrld
  93. **
  94. ** (2) Vview = Mwrld-camera * Vwrld
  95. **
  96. ** Using (2) to solve for Vwrld in terms of Vview
  97. ** -1
  98. ** (3) Mwrld-camera * Vview = Vwrld
  99. **
  100. ** Substituting (3) into (1)
  101. ** -1
  102. ** (4) Vshadow = Pshadow * Mwrld-shadow * Mwrld-camera * Vview
  103. **
  104. ** ---------------------------------------------------------------------------------
  105. **
  106. ** Shadow mapping: from pre-projected view space to world space, to shadow space,
  107. ** then projecting.
  108. **
  109. ** (1) Vshadow = Mwrld-shadow * Vwrld
  110. **
  111. ** (2) Vview = Mwrld-camera * Vwrld
  112. **
  113. ** solving (2) for Vwrld:
  114. ** -1
  115. ** (3) Mwrld-camera * Vview = Vwrld
  116. **
  117. ** substitute into (1)
  118. ** -1
  119. ** (4) Vshadow = Mwrld-shadow * Mwrld-camera * Vview
  120. **
  121. ** project shadow
  122. ** -1
  123. ** (5) Vproj-shadow = Pshadow * (Mwrld-shadow * Mwrld-camera ) * Vview
  124. **
  125. ** aaah! same thing :-)
  126. **
  127. ** ---------------------------------------------------------------------------------
  128. **
  129. ** Shadow mapping: from pre-projected view coordinates back to obj space
  130. ** then to shadow space
  131. **
  132. ** (1) Vshadow = PShadow * Mwrld-shadow * Mobj-wrld * Vobj
  133. **
  134. ** (2) Vview = Mwrld-camera * Mobj-wrld * Vobj
  135. ** -1 -1
  136. ** (3) Mobj-wrld * Mwrld-camera * Vview = Vobj
  137. ** -1 -1
  138. ** (4) Vshadow = PShadow * Mwrld-shadow * Mobj-wrld * Mobj-wrld * Mwrld-camera * Vview
  139. ** -1
  140. ** (5) Vshadow = PShadow * Mwrld-shadow * Mvrld-camera * Vview
  141. **
  142. **
  143. ** Ideas:
  144. ** - Use Texture Projectors to implement spot lights and stained glass windows
  145. ** - Attenuate texture projections with distance from the projector
  146. ** - Should be able to handle lots of pre-calculated static texture projectors. They
  147. ** should cull well and we can pre-generate the textures.
  148. **
  149. ** Ideas maybe used in conjunction with texture projections:
  150. ** - Light volumes: the problem is when the volume is cliped it looks funny, we can
  151. ** fill in the clipped face of a non-translucent object by rendering the backfaces
  152. ** - Use the backface-fill to make the camera slicing through the commando look like
  153. ** its *really* slicing through the commando :-)
  154. ** - The back-face-fill trick might be able to use el-cheapo screen mapping!
  155. **
  156. */
  157. /***********************************************************************************************
  158. * TexProjectClass::TexProjectClass -- Constructor *
  159. * *
  160. * INPUT: *
  161. * *
  162. * OUTPUT: *
  163. * *
  164. * WARNINGS: *
  165. * *
  166. * HISTORY: *
  167. * 1/4/00 gth : Created. *
  168. *=============================================================================================*/
  169. TexProjectClass::TexProjectClass(void) :
  170. Flags(DEFAULT_FLAGS),
  171. DesiredIntensity(1.0f),
  172. Intensity(1.0f),
  173. Attenuation(1.0f),
  174. MaterialPass(NULL),
  175. Mapper1(NULL),
  176. RenderTarget(NULL),
  177. HFov(90.0f),
  178. VFov(90.0f),
  179. XMin(-10.0f),
  180. XMax(10.0f),
  181. YMin(-10.0f),
  182. YMax(10.0f),
  183. ZNear(1.0f),
  184. ZFar(1000.0f)
  185. {
  186. // create a material pass class
  187. MaterialPass = NEW_REF(MaterialPassClass,());
  188. MaterialPass->Set_Cull_Volume(&WorldBoundingVolume);
  189. // create a vertex material
  190. VertexMaterialClass * vmtl = NEW_REF(VertexMaterialClass,());
  191. WWASSERT(vmtl != NULL);
  192. // Plug our parent's mapper into our vertex material
  193. // the mapper for stage1 will be allocated as needed
  194. vmtl->Set_Mapper(Mapper);
  195. MaterialPass->Set_Material(vmtl);
  196. vmtl->Release_Ref();
  197. vmtl = NULL;
  198. // by default init our material pass to be multiplicative (shadow)
  199. Init_Multiplicative();
  200. }
  201. /***********************************************************************************************
  202. * TexProjectClass::~TexProjectClass -- Destructor *
  203. * *
  204. * INPUT: *
  205. * *
  206. * OUTPUT: *
  207. * *
  208. * WARNINGS: *
  209. * *
  210. * HISTORY: *
  211. * 1/4/00 gth : Created. *
  212. *=============================================================================================*/
  213. TexProjectClass::~TexProjectClass(void)
  214. {
  215. REF_PTR_RELEASE(Mapper1);
  216. REF_PTR_RELEASE(MaterialPass);
  217. REF_PTR_RELEASE(RenderTarget);
  218. }
  219. /***********************************************************************************************
  220. * TexProjectClass::Set_Texture_Size -- Set the size of texture to use *
  221. * *
  222. * This function stores the desired texture size in this TexProjectClass. Note that *
  223. * this size is only used if you have the TexProjectClass generate a texture. *
  224. * *
  225. * INPUT: *
  226. * size - dimension of the texture in pixels *
  227. * *
  228. * OUTPUT: *
  229. * *
  230. * WARNINGS: *
  231. * *
  232. * HISTORY: *
  233. * 1/4/00 gth : Created. *
  234. *=============================================================================================*/
  235. void TexProjectClass::Set_Texture_Size(int size)
  236. {
  237. WWASSERT(size > 0);
  238. WWASSERT(size <= 512);
  239. Flags &= ~SIZE_MASK;
  240. Flags |= (size << SIZE_SHIFT);
  241. }
  242. /***********************************************************************************************
  243. * TexProjectClass::Get_Texture_Size -- Returns the stored texture size *
  244. * *
  245. * Returns the size stored in the flags variable. This can be different than the actual *
  246. * texture's size if you manually installed a texture. This size is only used when *
  247. * automatically generating a texture as is the case when creating shadows. *
  248. * *
  249. * INPUT: *
  250. * *
  251. * OUTPUT: *
  252. * *
  253. * WARNINGS: *
  254. * *
  255. * HISTORY: *
  256. * 1/4/00 gth : Created. *
  257. *=============================================================================================*/
  258. int TexProjectClass::Get_Texture_Size(void)
  259. {
  260. return (Flags & SIZE_MASK) >> SIZE_SHIFT;
  261. }
  262. /***********************************************************************************************
  263. * TexProjectClass::Set_Flag -- Turn specified flag on or off *
  264. * *
  265. * See the TexProjectClass header file for valid flags *
  266. * *
  267. * INPUT: *
  268. * *
  269. * OUTPUT: *
  270. * *
  271. * WARNINGS: *
  272. * *
  273. * HISTORY: *
  274. * 1/4/00 gth : Created. *
  275. *=============================================================================================*/
  276. void TexProjectClass::Set_Flag(uint32 flag,bool onoff)
  277. {
  278. if (onoff) {
  279. Flags |= flag;
  280. } else {
  281. Flags &= ~flag;
  282. }
  283. }
  284. /***********************************************************************************************
  285. * TexProjectClass::Get_Flag -- Get the current state of specified flag *
  286. * *
  287. * See the TexProjectClass header file for valid flags *
  288. * *
  289. * INPUT: *
  290. * *
  291. * OUTPUT: *
  292. * *
  293. * WARNINGS: *
  294. * *
  295. * HISTORY: *
  296. * 1/4/00 gth : Created. *
  297. *=============================================================================================*/
  298. bool TexProjectClass::Get_Flag(uint32 flag) const
  299. {
  300. return (Flags & flag) == flag;
  301. }
  302. /***********************************************************************************************
  303. * TexProjectClass::Set_Intensity -- Set the intensity of this projector *
  304. * *
  305. * The "intensity" is a value between 0 and 1. At 0, the projector will have no effect *
  306. * At 1, the projector will be at its highest strength. The intensity will automatically *
  307. * smoothly interpolate towards the value specified unless you use the immediate option. *
  308. * *
  309. * INPUT: *
  310. * intensity - desired intensity, 0 <= intensity <= 1 *
  311. * immediate - should the intensity be immediately set to the desired value? (default = false) *
  312. * *
  313. * OUTPUT: *
  314. * *
  315. * WARNINGS: *
  316. * *
  317. * HISTORY: *
  318. * 1/4/00 gth : Created. *
  319. *=============================================================================================*/
  320. void TexProjectClass::Set_Intensity(float intensity,bool immediate)
  321. {
  322. WWASSERT(intensity <= 1.0f);
  323. WWASSERT(intensity >= 0.0f);
  324. DesiredIntensity = intensity;
  325. if (immediate) {
  326. Intensity = DesiredIntensity;
  327. }
  328. }
  329. /***********************************************************************************************
  330. * TexProjectClass::Get_Intensity -- returns the current "desired" intensity *
  331. * *
  332. * This will return the last value that was sent into this object through Set_Intensity. *
  333. * Note however that the actual intensity used in rendering may not have arrived at *
  334. * that value yet. *
  335. * *
  336. * INPUT: *
  337. * *
  338. * OUTPUT: *
  339. * *
  340. * WARNINGS: *
  341. * *
  342. * HISTORY: *
  343. * 1/4/00 gth : Created. *
  344. *=============================================================================================*/
  345. float TexProjectClass::Get_Intensity(void)
  346. {
  347. return DesiredIntensity;
  348. }
  349. /***********************************************************************************************
  350. * TexProjectClass::Is_Intensity_Zero -- check if we can eliminate this projector *
  351. * *
  352. * Only returns true if the current intensity is zero AND the target intensity is zero *
  353. * *
  354. * INPUT: *
  355. * *
  356. * OUTPUT: *
  357. * *
  358. * WARNINGS: *
  359. * *
  360. * HISTORY: *
  361. * 1/4/00 gth : Created. *
  362. *=============================================================================================*/
  363. bool TexProjectClass::Is_Intensity_Zero(void)
  364. {
  365. return ((Intensity == 0.0f) && (DesiredIntensity == 0.0f));
  366. }
  367. /***********************************************************************************************
  368. * TexProjectClass::Set_Attenuation -- Set the attenuation factor *
  369. * *
  370. * Attenuation scales the intensity. I use attenuation to make shadows fade away as they *
  371. * get farther away from the light source or from the viewer. *
  372. * *
  373. * INPUT: *
  374. * *
  375. * OUTPUT: *
  376. * *
  377. * WARNINGS: *
  378. * *
  379. * HISTORY: *
  380. * 1/4/00 gth : Created. *
  381. *=============================================================================================*/
  382. void TexProjectClass::Set_Attenuation(float attenuation)
  383. {
  384. WWASSERT(attenuation >= 0.0f);
  385. WWASSERT(attenuation <= 1.0f);
  386. Attenuation = attenuation;
  387. }
  388. /***********************************************************************************************
  389. * TexProjectClass::Get_Attenuation -- Returns the attenuation value *
  390. * *
  391. * INPUT: *
  392. * *
  393. * OUTPUT: *
  394. * *
  395. * WARNINGS: *
  396. * *
  397. * HISTORY: *
  398. * 1/4/00 gth : Created. *
  399. *=============================================================================================*/
  400. float TexProjectClass::Get_Attenuation(void)
  401. {
  402. return Attenuation;
  403. }
  404. /***********************************************************************************************
  405. * TexProjectClass::Enable_Attenuation -- Set the state of the ATTENUATE flag *
  406. * *
  407. * INPUT: *
  408. * *
  409. * OUTPUT: *
  410. * *
  411. * WARNINGS: *
  412. * *
  413. * HISTORY: *
  414. * 1/4/00 gth : Created. *
  415. *=============================================================================================*/
  416. void TexProjectClass::Enable_Attenuation(bool onoff)
  417. {
  418. Set_Flag(ATTENUATE,onoff);
  419. }
  420. /***********************************************************************************************
  421. * TexProjectClass::Is_Attenuation_Enabled -- Get the state of the ATTENUATE flag *
  422. * *
  423. * INPUT: *
  424. * *
  425. * OUTPUT: *
  426. * *
  427. * WARNINGS: *
  428. * *
  429. * HISTORY: *
  430. * 1/4/00 gth : Created. *
  431. *=============================================================================================*/
  432. bool TexProjectClass::Is_Attenuation_Enabled(void)
  433. {
  434. return Get_Flag(ATTENUATE);
  435. }
  436. /***********************************************************************************************
  437. * TexProjectClass::Enable_Depth_Gradient -- enable/disable depth gradient *
  438. * *
  439. * INPUT: *
  440. * *
  441. * OUTPUT: *
  442. * *
  443. * WARNINGS: *
  444. * *
  445. * HISTORY: *
  446. * 2/25/2000 gth : Created. *
  447. *=============================================================================================*/
  448. void TexProjectClass::Enable_Depth_Gradient(bool onoff)
  449. {
  450. Set_Flag(USE_DEPTH_GRADIENT,onoff);
  451. // re-setup the shader settings
  452. if (Get_Flag(ADDITIVE)) {
  453. Init_Additive();
  454. } else {
  455. Init_Multiplicative();
  456. }
  457. }
  458. /***********************************************************************************************
  459. * TexProjectClass::Is_Depth_Gradient_Enabled -- returns whether the depth gradient is enabled *
  460. * *
  461. * INPUT: *
  462. * *
  463. * OUTPUT: *
  464. * *
  465. * WARNINGS: *
  466. * *
  467. * HISTORY: *
  468. * 2/25/2000 gth : Created. *
  469. *=============================================================================================*/
  470. bool TexProjectClass::Is_Depth_Gradient_Enabled(bool onoff)
  471. {
  472. return Get_Flag(USE_DEPTH_GRADIENT);
  473. }
  474. /***********************************************************************************************
  475. * TexProjectClass::Init_Multiplicative -- Initialize this to be a multiplicative texture proj *
  476. * *
  477. * Set up the internal materials so that the texture is multiplied with whatever it is *
  478. * projected onto. *
  479. * *
  480. * INPUT: *
  481. * *
  482. * OUTPUT: *
  483. * *
  484. * WARNINGS: *
  485. * *
  486. * HISTORY: *
  487. * 1/4/00 gth : Created. *
  488. *=============================================================================================*/
  489. void TexProjectClass::Init_Multiplicative(void)
  490. {
  491. Set_Flag(ADDITIVE,false);
  492. /*
  493. ** Set up the shader
  494. */
  495. static ShaderClass mult_shader( SHADE_CNST( ShaderClass::PASS_LEQUAL, //depth_compare,
  496. ShaderClass::DEPTH_WRITE_DISABLE, //depth_mask,
  497. ShaderClass::COLOR_WRITE_ENABLE, //color_mask,
  498. ShaderClass::SRCBLEND_ZERO, //src_blend,
  499. ShaderClass::DSTBLEND_SRC_COLOR, //dst_blend,
  500. ShaderClass::FOG_DISABLE, //fog,
  501. ShaderClass::GRADIENT_ADD, //pri_grad,
  502. ShaderClass::SECONDARY_GRADIENT_DISABLE, //sec_grad,
  503. ShaderClass::TEXTURING_ENABLE, //texture,
  504. ShaderClass::ALPHATEST_DISABLE, //alpha_test,
  505. ShaderClass::CULL_MODE_ENABLE, //cull mode
  506. 0, //post_det_color,
  507. 0) ); //post_det_alpha
  508. if (WW3DAssetManager::Get_Instance()->Get_Activate_Fog_On_Load()) {
  509. mult_shader.Enable_Fog ("TexProjectClass");
  510. }
  511. if (Get_Flag(USE_DEPTH_GRADIENT)) {
  512. /*
  513. ** enable multi-texturing
  514. */
  515. mult_shader.Set_Post_Detail_Color_Func(ShaderClass::DETAILCOLOR_ADD);
  516. /*
  517. ** plug the gradient texture into the second stage
  518. */
  519. TextureClass * grad_tex = WW3DAssetManager::Get_Instance()->Get_Texture("MultProjectorGradient.tga");
  520. if (grad_tex) {
  521. grad_tex->Set_U_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  522. grad_tex->Set_V_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  523. MaterialPass->Set_Texture(grad_tex,1);
  524. grad_tex->Release_Ref();
  525. } else {
  526. WWDEBUG_SAY(("Could not find texture: MultProjectorGradient.tga!\n"));
  527. }
  528. } else {
  529. /*
  530. ** disable multi-texturing
  531. */
  532. mult_shader.Set_Post_Detail_Color_Func(ShaderClass::DETAILCOLOR_DISABLE);
  533. /*
  534. ** remove the texture from the second stage
  535. */
  536. MaterialPass->Set_Texture(NULL,1);
  537. }
  538. #if (DEBUG_SHADOW_RENDERING)
  539. // invert the shader so we can see what polygons it is hitting
  540. mult_shader.Set_Dst_Blend_Func(ShaderClass::DSTBLEND_ONE);
  541. mult_shader.Set_Src_Blend_Func(ShaderClass::SRCBLEND_ONE);
  542. #endif
  543. MaterialPass->Set_Shader(mult_shader);
  544. /*
  545. ** Set up the Vertex Material parameters
  546. */
  547. VertexMaterialClass * vmtl = MaterialPass->Peek_Material();
  548. vmtl->Set_Ambient(0,0,0);
  549. vmtl->Set_Diffuse(0,0,0);
  550. vmtl->Set_Specular(0,0,0);
  551. vmtl->Set_Emissive(0.0f,0.0f,0.0f);
  552. vmtl->Set_Opacity(1.0f);
  553. vmtl->Set_Lighting(true); // I need the emissive value to scale the intensity of the shadow
  554. /*
  555. ** Set up some mapper settings related to depth gradient
  556. */
  557. if (Get_Flag(USE_DEPTH_GRADIENT)) {
  558. if (Mapper1 == NULL) {
  559. Mapper1 = NEW_REF(MatrixMapperClass,(1));
  560. }
  561. Mapper1->Set_Type(MatrixMapperClass::DEPTH_GRADIENT);
  562. vmtl->Set_Mapper(Mapper1,1);
  563. } else {
  564. vmtl->Set_Mapper(NULL,1);
  565. }
  566. }
  567. /***********************************************************************************************
  568. * TexProjectClass::Init_Additive -- Set up the projector to be additive *
  569. * *
  570. * INPUT: *
  571. * *
  572. * OUTPUT: *
  573. * *
  574. * WARNINGS: *
  575. * *
  576. * HISTORY: *
  577. * 1/11/00 gth : Created. *
  578. *=============================================================================================*/
  579. void TexProjectClass::Init_Additive(void)
  580. {
  581. Set_Flag(ADDITIVE,true);
  582. /*
  583. ** Set up the shader
  584. */
  585. static ShaderClass add_shader( SHADE_CNST( ShaderClass::PASS_LEQUAL, //depth_compare,
  586. ShaderClass::DEPTH_WRITE_DISABLE, //depth_mask,
  587. ShaderClass::COLOR_WRITE_ENABLE, //color_mask,
  588. ShaderClass::SRCBLEND_ONE, //src_blend,
  589. ShaderClass::DSTBLEND_ONE, //dst_blend,
  590. ShaderClass::FOG_DISABLE, //fog,
  591. ShaderClass::GRADIENT_MODULATE, //pri_grad,
  592. ShaderClass::SECONDARY_GRADIENT_DISABLE, //sec_grad,
  593. ShaderClass::TEXTURING_ENABLE, //texture,
  594. ShaderClass::ALPHATEST_DISABLE, //alpha_test,
  595. ShaderClass::CULL_MODE_ENABLE, //cullmode,
  596. ShaderClass::DETAILCOLOR_DISABLE, //post_det_color,
  597. ShaderClass::DETAILALPHA_DISABLE) ); //post_det_alpha
  598. if (WW3DAssetManager::Get_Instance()->Get_Activate_Fog_On_Load()) {
  599. add_shader.Enable_Fog ("TexProjectClass");
  600. }
  601. /*
  602. ** Additive projectors always use the normal gradient so they need multi-texturing
  603. */
  604. add_shader.Set_Post_Detail_Color_Func(ShaderClass::DETAILCOLOR_SCALE);
  605. /*
  606. ** plug in the gradient texture
  607. */
  608. TextureClass * grad_tex = WW3DAssetManager::Get_Instance()->Get_Texture("AddProjectorGradient.tga");
  609. if (grad_tex) {
  610. grad_tex->Set_U_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  611. grad_tex->Set_V_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  612. MaterialPass->Set_Texture(grad_tex,1);
  613. grad_tex->Release_Ref();
  614. } else {
  615. WWDEBUG_SAY(("Could not find texture: AddProjectorGradient.tga!\n"));
  616. }
  617. #if (DEBUG_SHADOW_RENDERING)
  618. // invert the shader so we can see what polygons it is hitting
  619. add_shader.Set_Dst_Blend_Func(ShaderClass::DSTBLEND_SRC_COLOR);
  620. add_shader.Set_Src_Blend_Func(ShaderClass::SRCBLEND_ZERO);
  621. #endif
  622. MaterialPass->Set_Shader(add_shader);
  623. /*
  624. ** Set up the Vertex Material parameters
  625. */
  626. VertexMaterialClass * vmtl = MaterialPass->Peek_Material();
  627. vmtl->Set_Ambient(0,0,0);
  628. vmtl->Set_Diffuse(0,0,0);
  629. vmtl->Set_Specular(0,0,0);
  630. vmtl->Set_Emissive(1,1,1);
  631. vmtl->Set_Opacity(1.0f);
  632. vmtl->Set_Lighting(true); //need emissive to scale the intensity of the projector
  633. /*
  634. ** Set up some mapper settings related to depth gradient
  635. ** Additive texture projections always use the normal gradient
  636. */
  637. if (Mapper1 == NULL) {
  638. Mapper1 = NEW_REF(MatrixMapperClass,(1));
  639. }
  640. Mapper1->Set_Type(MatrixMapperClass::NORMAL_GRADIENT);
  641. vmtl->Set_Mapper(Mapper1,1);
  642. }
  643. /***********************************************************************************************
  644. * TexProjectClass::Set_Texture -- Set the texture to be projected *
  645. * *
  646. * INPUT: *
  647. * *
  648. * OUTPUT: *
  649. * *
  650. * WARNINGS: *
  651. * *
  652. * HISTORY: *
  653. * 1/11/00 gth : Created. *
  654. *=============================================================================================*/
  655. void TexProjectClass::Set_Texture(TextureClass * texture)
  656. {
  657. if (texture != NULL) {
  658. texture->Set_U_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  659. texture->Set_V_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  660. MaterialPass->Set_Texture(texture);
  661. }
  662. }
  663. /***********************************************************************************************
  664. * TexProjectClass::Get_Texture -- Returns the texture being projected *
  665. * *
  666. * INPUT: *
  667. * *
  668. * OUTPUT: *
  669. * *
  670. * WARNINGS: *
  671. * the pointer is ref-counted! make sure to keep track of your references *
  672. * *
  673. * HISTORY: *
  674. * 1/11/00 gth : Created. *
  675. *=============================================================================================*/
  676. TextureClass * TexProjectClass::Get_Texture(void) const
  677. {
  678. return MaterialPass->Get_Texture();
  679. }
  680. /***********************************************************************************************
  681. * TexProjectClass::Peek_Texture -- Returns the texture being projected *
  682. * *
  683. * INPUT: *
  684. * *
  685. * OUTPUT: *
  686. * *
  687. * WARNINGS: *
  688. * *
  689. * HISTORY: *
  690. * 1/11/00 gth : Created. *
  691. *=============================================================================================*/
  692. TextureClass * TexProjectClass::Peek_Texture(void) const
  693. {
  694. return MaterialPass->Peek_Texture();
  695. }
  696. /***********************************************************************************************
  697. * TexProjectClass::Peek_Material_Pass -- Returns the material pass object *
  698. * *
  699. * INPUT: *
  700. * *
  701. * OUTPUT: *
  702. * *
  703. * WARNINGS: *
  704. * *
  705. * HISTORY: *
  706. * 1/11/00 gth : Created. *
  707. *=============================================================================================*/
  708. MaterialPassClass * TexProjectClass::Peek_Material_Pass(void)
  709. {
  710. return MaterialPass;
  711. }
  712. /***********************************************************************************************
  713. * TexProjectClass::Set_Perspective_Projection -- set up a perspective projection *
  714. * *
  715. * INPUT: *
  716. * *
  717. * OUTPUT: *
  718. * *
  719. * WARNINGS: *
  720. * *
  721. * HISTORY: *
  722. * 1/27/00 gth : Created. *
  723. *=============================================================================================*/
  724. void TexProjectClass::Set_Perspective_Projection(float hfov,float vfov,float znear,float zfar)
  725. {
  726. HFov = hfov;
  727. VFov = vfov;
  728. ZNear = znear;
  729. ZFar = zfar;
  730. ProjectorClass::Set_Perspective_Projection(hfov,vfov,znear,zfar);
  731. Set_Flag(PERSPECTIVE,true);
  732. }
  733. /***********************************************************************************************
  734. * TexProjectClass::Set_Ortho_Projection -- set up an orthographic projection *
  735. * *
  736. * INPUT: *
  737. * *
  738. * OUTPUT: *
  739. * *
  740. * WARNINGS: *
  741. * *
  742. * HISTORY: *
  743. * 1/27/00 gth : Created. *
  744. *=============================================================================================*/
  745. void TexProjectClass::Set_Ortho_Projection(float xmin,float xmax,float ymin,float ymax,float znear,float zfar)
  746. {
  747. XMin = xmin;
  748. XMax = xmax;
  749. YMin = ymin;
  750. YMax = ymax;
  751. ZNear = znear;
  752. ZFar = zfar;
  753. ProjectorClass::Set_Ortho_Projection(xmin,xmax,ymin,ymax,znear,zfar);
  754. Set_Flag(PERSPECTIVE,false);
  755. }
  756. /***********************************************************************************************
  757. * TexProjectClass::Compute_Perspective_Projection -- Set up a perspective projection of an ob *
  758. * *
  759. * This function automates the process of generating the perspective parameters needed to *
  760. * tightly bound the projection of an object. *
  761. * *
  762. * INPUT: *
  763. * model - object which we are created a projection of *
  764. * lightpos - positional light source *
  765. * znear - distance to near clipping plane for the projection (if -1.0, will be generated) *
  766. * zfar - distance to far clipping plane for the projection (if -1.0, will be generated) *
  767. * *
  768. * OUTPUT: *
  769. * *
  770. * WARNINGS: *
  771. * *
  772. * HISTORY: *
  773. * 1/11/00 gth : Created. *
  774. *=============================================================================================*/
  775. bool TexProjectClass::Compute_Perspective_Projection
  776. (
  777. RenderObjClass * model,
  778. const Vector3 & lightpos,
  779. float znear,
  780. float zfar
  781. )
  782. {
  783. if (model == NULL) {
  784. WWDEBUG_SAY(("Attempting to generate projection for a NULL model\r\n"));
  785. return false;
  786. }
  787. AABoxClass box;
  788. model->Get_Obj_Space_Bounding_Box(box);
  789. const Matrix3D & tm = model->Get_Transform();
  790. return Compute_Perspective_Projection(box,tm,lightpos,znear,zfar);
  791. }
  792. /***********************************************************************************************
  793. * TexProjectClass::Compute_Perspective_Projection -- Set up a perspective projection of an ob *
  794. * *
  795. * This function automates the process of generating the perspective parameters needed to *
  796. * tightly bound the projection of an object. *
  797. * *
  798. * INPUT: *
  799. * obj_box - object space bounding box of the object we are projecting *
  800. * tm - transform of the object we are projecting *
  801. * lightpos - positional light source *
  802. * user_znear - distance to near clipping plane for the projection (if -1.0, will be generated)*
  803. * user_zfar - distance to far clipping plane for the projection (if -1.0, will be generated) *
  804. * *
  805. * OUTPUT: *
  806. * *
  807. * WARNINGS: *
  808. * *
  809. * HISTORY: *
  810. * 1/11/00 gth : Created. *
  811. *=============================================================================================*/
  812. bool TexProjectClass::Compute_Perspective_Projection
  813. (
  814. const AABoxClass & obj_box,
  815. const Matrix3D & tm,
  816. const Vector3 & lightpos,
  817. float user_znear,
  818. float user_zfar
  819. )
  820. {
  821. /*
  822. ** Compute the center of the box in world-space
  823. */
  824. Vector3 wrld_center;
  825. Matrix3D::Transform_Vector(tm,obj_box.Center,&wrld_center);
  826. /*
  827. ** Create a camera transform looking at the object.
  828. ** Set this as our transform later in this routine.
  829. */
  830. Matrix3D texture_tm,texture_tm_inv;
  831. texture_tm.Look_At(lightpos,wrld_center,0.0f);
  832. texture_tm.Get_Orthogonal_Inverse(texture_tm_inv);
  833. /*
  834. ** Calculate the axis-aligned bounding box of the model in the camera's coordinate system.
  835. */
  836. AABoxClass box = obj_box;
  837. Matrix3D obj_to_world = tm;
  838. Matrix3D obj_to_texture;
  839. Matrix3D::Multiply(texture_tm_inv,obj_to_world,&obj_to_texture);
  840. box.Transform(obj_to_texture);
  841. /*
  842. ** If the box is behind the viewpoint or the viewpoint is inside the box
  843. ** our FOV will be > 180 degrees. Have to give up
  844. */
  845. if ((box.Center.Z > 0.0f) || (box.Extent.Z > WWMath::Fabs(box.Center.Z))) {
  846. return false;
  847. }
  848. /*
  849. ** Compute the frustum parameters. Remember that our z coordinates are negative but the
  850. ** projection code needs positive z distances.
  851. */
  852. float znear = -box.Center.Z; //-(box.Center.Z + obj_box.Extent.Quick_Length());
  853. float zfar = -(box.Center.Z - obj_box.Extent.Quick_Length()) * 2.0f;
  854. if (user_znear != -1.0f) {
  855. znear = box.Center.Z + user_znear;
  856. }
  857. if (user_zfar != -1.0f) {
  858. zfar = box.Center.Z + user_zfar;
  859. }
  860. float tan_hfov2 = WWMath::Fabs(box.Extent.X / (box.Center.Z + box.Extent.Z));
  861. float tan_vfov2 = WWMath::Fabs(box.Extent.Y / (box.Center.Z + box.Extent.Z));
  862. float hfov = 2.0f * WWMath::Atan(tan_hfov2);
  863. float vfov = 2.0f * WWMath::Atan(tan_vfov2);
  864. /*
  865. ** Plug in the results.
  866. */
  867. Set_Perspective_Projection(hfov,vfov,znear,zfar);
  868. Set_Transform(texture_tm);
  869. return true;
  870. }
  871. /***********************************************************************************************
  872. * TexProjectClass::Compute_Ortho_Projection -- Automatic Orthographic projection *
  873. * *
  874. * Generates the orthographic projection parameters to tightly bound an object *
  875. * *
  876. * INPUT: *
  877. * model - object which we are created a projection of *
  878. * lightdir - directional light source *
  879. * znear - distance to near clipping plane for the projection (if -1.0, will be generated) *
  880. * zfar - distance to far clipping plane for the projection (if -1.0, will be generated) *
  881. * *
  882. * OUTPUT: *
  883. * *
  884. * WARNINGS: *
  885. * *
  886. * HISTORY: *
  887. * 1/11/00 gth : Created. *
  888. *=============================================================================================*/
  889. bool TexProjectClass::Compute_Ortho_Projection
  890. (
  891. RenderObjClass * model,
  892. const Vector3 & lightdir,
  893. float znear,
  894. float zfar
  895. )
  896. {
  897. if (model == NULL) {
  898. WWDEBUG_SAY(("Attempting to generate projection for a NULL model\r\n"));
  899. return false;
  900. }
  901. AABoxClass box;
  902. model->Get_Obj_Space_Bounding_Box(box);
  903. const Matrix3D & tm = model->Get_Transform();
  904. return Compute_Ortho_Projection(box,tm,lightdir,znear,zfar);
  905. }
  906. /***********************************************************************************************
  907. * TexProjectClass::Compute_Ortho_Projection -- Automatic Orthographic projection *
  908. * *
  909. * Generates the orthographic projection parameters to tightly bound an object *
  910. * *
  911. * INPUT: *
  912. * obj_box - object space bounding box of the object we are projecting *
  913. * tm - transform of the object we are projecting *
  914. * lightdir - directional light *
  915. * user_znear - distance to near clipping plane for the projection (if -1.0, will be generated)*
  916. * user_zfar - distance to far clipping plane for the projection (if -1.0, will be generated) *
  917. * *
  918. * OUTPUT: *
  919. * *
  920. * WARNINGS: *
  921. * *
  922. * HISTORY: *
  923. * 1/11/00 gth : Created. *
  924. *=============================================================================================*/
  925. bool TexProjectClass::Compute_Ortho_Projection
  926. (
  927. const AABoxClass & obj_box,
  928. const Matrix3D & tm,
  929. const Vector3 & lightdir,
  930. float user_znear,
  931. float user_zfar
  932. )
  933. {
  934. /*
  935. ** Compute the center of the box in world-space
  936. */
  937. AABoxClass wrldbox = obj_box;
  938. wrldbox.Transform(tm);
  939. /*
  940. ** Create a camera transform looking at the object.
  941. ** Set this as our transform later in this routine.
  942. */
  943. Vector3 camera_target = wrldbox.Center;
  944. Vector3 camera_position = camera_target - 2.0f * wrldbox.Extent.Length() * lightdir;
  945. Matrix3D texture_tm,texture_tm_inv;
  946. texture_tm.Look_At(camera_position,camera_target,0.0f);
  947. texture_tm.Get_Orthogonal_Inverse(texture_tm_inv);
  948. /*
  949. ** Calculate the axis-aligned bounding box of the model in the camera's coordinate system.
  950. */
  951. AABoxClass box = obj_box;
  952. Matrix3D obj_to_world = tm;
  953. Matrix3D obj_to_texture;
  954. Matrix3D::Multiply(texture_tm_inv,obj_to_world,&obj_to_texture);
  955. box.Transform(obj_to_texture);
  956. /*
  957. ** Expand the box to help with bounding box errors
  958. */
  959. box.Extent *= 1.0f;
  960. /*
  961. ** Compute the frustum parameters. Note that znear and zfar are supposed to
  962. ** be positive distances for the projection code.
  963. */
  964. float znear = -box.Center.Z; //-(box.Center.Z + obj_box.Extent.Quick_Length());
  965. float zfar = -(box.Center.Z - obj_box.Extent.Quick_Length()) * 2.0f;
  966. if (user_znear != -1.0f) {
  967. znear = -box.Center.Z + user_znear;
  968. }
  969. if (user_zfar != -1.0f) {
  970. zfar = -box.Center.Z + user_zfar;
  971. }
  972. /*
  973. ** All done!
  974. */
  975. Set_Ortho_Projection( box.Center.X - box.Extent.X,
  976. box.Center.X + box.Extent.X,
  977. box.Center.Y - box.Extent.Y,
  978. box.Center.Y + box.Extent.Y,
  979. znear,
  980. zfar );
  981. Set_Transform(texture_tm);
  982. return true;
  983. }
  984. /***********************************************************************************************
  985. * TexProjectClass::Compute_Texture -- Generates texture by rendering an object *
  986. * *
  987. * INPUT: *
  988. * model - pointer to the render object to generate a shadow texture for *
  989. * context - shadow render context which has been initialized to <TextureSize> resolution *
  990. * *
  991. * OUTPUT: *
  992. * *
  993. * WARNINGS: *
  994. * *
  995. * HISTORY: *
  996. * 1/11/00 gth : Created. *
  997. *=============================================================================================*/
  998. bool TexProjectClass::Compute_Texture(RenderObjClass * model,SpecialRenderInfoClass * context)
  999. {
  1000. if ((model == NULL) || (context == NULL)) {
  1001. return false;
  1002. }
  1003. /*
  1004. ** Render to texture
  1005. */
  1006. TextureClass * rtarget = Peek_Render_Target();
  1007. if (rtarget != NULL) {
  1008. /*
  1009. ** Set the render target
  1010. */
  1011. DX8Wrapper::Set_Render_Target(rtarget);
  1012. /*
  1013. ** Set up the camera
  1014. */
  1015. Configure_Camera(context->Camera);
  1016. /*
  1017. ** Render the object
  1018. */
  1019. Vector3 color(0.0f,0.0f,0.0f);
  1020. if (Get_Flag(ADDITIVE) == false) {
  1021. color.Set(1.0f,1.0f,1.0f);
  1022. }
  1023. WW3D::Begin_Render(true,false,color); // false to zclear as we don't have z-buffer
  1024. WW3D::Render(*model,*context);
  1025. WW3D::End_Render(false);
  1026. DX8Wrapper::Set_Render_Target((IDirect3DSurface8 *)NULL);
  1027. }
  1028. #if 0
  1029. /*
  1030. ** Render the object with the BW Renderer into our color surface
  1031. */
  1032. BWRenderClass bwr((unsigned char*)shadow_surface->getDataPtr(),tex_size);
  1033. bwr.Fill(0xff);
  1034. context->BWRenderer = &bwr;
  1035. model->Special_Render(*context);
  1036. context->BWRenderer = NULL;
  1037. #endif
  1038. return true;
  1039. }
  1040. /***********************************************************************************************
  1041. * TexProjectClass::Needs_Render_Target -- returns wheter this projector needs a render target *
  1042. * *
  1043. * INPUT: *
  1044. * *
  1045. * OUTPUT: *
  1046. * *
  1047. * WARNINGS: *
  1048. * *
  1049. * HISTORY: *
  1050. * 4/17/2001 gth : Created. *
  1051. *=============================================================================================*/
  1052. bool TexProjectClass::Needs_Render_Target(void)
  1053. {
  1054. return Get_Flag(TEXTURE_DIRTY);
  1055. }
  1056. /***********************************************************************************************
  1057. * TexProjectClass::Set_Render_Target -- Install a render target for this projector to use *
  1058. * *
  1059. * INPUT: *
  1060. * *
  1061. * OUTPUT: *
  1062. * *
  1063. * WARNINGS: *
  1064. * *
  1065. * HISTORY: *
  1066. * 4/16/2001 gth : Created. *
  1067. *=============================================================================================*/
  1068. void TexProjectClass::Set_Render_Target(TextureClass * render_target)
  1069. {
  1070. REF_PTR_SET(RenderTarget,render_target);
  1071. Set_Texture(RenderTarget);
  1072. }
  1073. /***********************************************************************************************
  1074. * TexProjectClass::Peek_Render_Target -- Returns pointer to the render target if we have one *
  1075. * *
  1076. * INPUT: *
  1077. * *
  1078. * OUTPUT: *
  1079. * *
  1080. * WARNINGS: *
  1081. * *
  1082. * HISTORY: *
  1083. * 4/5/2001 gth : Created. *
  1084. *=============================================================================================*/
  1085. TextureClass * TexProjectClass::Peek_Render_Target(void)
  1086. {
  1087. return RenderTarget;
  1088. }
  1089. /***********************************************************************************************
  1090. * TexProjectClass::Configure_Camera -- set up a camera to match this projector *
  1091. * *
  1092. * INPUT: *
  1093. * *
  1094. * OUTPUT: *
  1095. * *
  1096. * WARNINGS: *
  1097. * *
  1098. * HISTORY: *
  1099. * 1/11/00 gth : Created. *
  1100. *=============================================================================================*/
  1101. void TexProjectClass::Configure_Camera(CameraClass & camera)
  1102. {
  1103. camera.Set_Transform(Transform);
  1104. camera.Set_Clip_Planes(0.01f,ZFar);
  1105. if (Get_Flag(PERSPECTIVE)) {
  1106. camera.Set_Projection_Type(CameraClass::PERSPECTIVE);
  1107. camera.Set_View_Plane(HFov,VFov);
  1108. } else {
  1109. camera.Set_Projection_Type(CameraClass::ORTHO);
  1110. camera.Set_View_Plane(Vector2(XMin,YMin),Vector2(XMax,YMax));
  1111. }
  1112. // Set one-pixel borders to the texture to avoid "flooding" shadows...
  1113. float size=Get_Texture_Size();
  1114. float inv_size=1.0f/size;
  1115. Vector2 vmin(1.0f*inv_size,1.0f*inv_size);
  1116. Vector2 vmax((size-1.0f)*inv_size,(size-1.0f)*inv_size);
  1117. camera.Set_Viewport(vmin,vmax);
  1118. }
  1119. /***********************************************************************************************
  1120. * TexProjectClass::Pre_Render_Update -- Prepare the projector for rendering *
  1121. * *
  1122. * INPUT: *
  1123. * *
  1124. * OUTPUT: *
  1125. * *
  1126. * WARNINGS: *
  1127. * *
  1128. * HISTORY: *
  1129. * 1/11/00 gth : Created. *
  1130. *=============================================================================================*/
  1131. void TexProjectClass::Pre_Render_Update(const Matrix3D & camera)
  1132. {
  1133. /*
  1134. ** Mview-texture = PShadow * Mwrld-texture * Mcamera-vrld
  1135. */
  1136. Matrix3D world_to_texture;
  1137. Matrix3D tmp;
  1138. Matrix4 view_to_texture;
  1139. Transform.Get_Orthogonal_Inverse(world_to_texture);
  1140. Matrix3D::Multiply(world_to_texture,camera,&tmp);
  1141. Matrix4::Multiply(Projection,tmp,&view_to_texture);
  1142. /*
  1143. ** update the current intensity by iterating it towards the desired intensity
  1144. */
  1145. float frame_time = (float)WW3D::Get_Frame_Time() / 1000.0f;
  1146. float intensity_delta = DesiredIntensity - Intensity;
  1147. float max_intensity_delta = INTENSITY_RATE_OF_CHANGE * frame_time;
  1148. if (intensity_delta > max_intensity_delta) {
  1149. Intensity += max_intensity_delta;
  1150. } else if (intensity_delta < -max_intensity_delta) {
  1151. Intensity -= max_intensity_delta;
  1152. } else {
  1153. Intensity = DesiredIntensity;
  1154. }
  1155. float actual_intensity = Intensity * Attenuation;
  1156. /*
  1157. ** install the current intensity
  1158. */
  1159. VertexMaterialClass * vmat = MaterialPass->Peek_Material();
  1160. if (Get_Flag(ADDITIVE)) {
  1161. vmat->Set_Emissive(actual_intensity,actual_intensity,actual_intensity);
  1162. } else {
  1163. vmat->Set_Emissive(1.0f - actual_intensity,1.0f - actual_intensity,1.0f - actual_intensity);
  1164. }
  1165. /*
  1166. ** update the mappers
  1167. */
  1168. if (Get_Flag(PERSPECTIVE)) {
  1169. Mapper->Set_Type(MatrixMapperClass::PERSPECTIVE_PROJECTION);
  1170. } else {
  1171. Mapper->Set_Type(MatrixMapperClass::ORTHO_PROJECTION);
  1172. }
  1173. if (Get_Texture_Size() == 0) {
  1174. // SurfaceClass::SurfaceDescription surface_desc;
  1175. // MaterialPass->Peek_Texture()->Get_Level_Description(surface_desc);
  1176. Set_Texture_Size(MaterialPass->Peek_Texture()->Get_Width());
  1177. WWASSERT(Get_Texture_Size() != 0);
  1178. }
  1179. Mapper->Set_Texture_Transform(view_to_texture,Get_Texture_Size());
  1180. if (Mapper1) {
  1181. Mapper1->Set_Texture_Transform(view_to_texture,Get_Texture_Size());
  1182. }
  1183. }
  1184. /***********************************************************************************************
  1185. * TexProjectClass::Update_WS_Bounding_Volume -- Recalculate the world-space bounding box *
  1186. * *
  1187. * INPUT: *
  1188. * *
  1189. * OUTPUT: *
  1190. * *
  1191. * WARNINGS: *
  1192. * *
  1193. * HISTORY: *
  1194. * 1/11/00 gth : Created. *
  1195. *=============================================================================================*/
  1196. void TexProjectClass::Update_WS_Bounding_Volume(void)
  1197. {
  1198. ProjectorClass::Update_WS_Bounding_Volume();
  1199. /*
  1200. ** Tell our culling system that we've changed
  1201. */
  1202. Vector3 extent;
  1203. WorldBoundingVolume.Compute_Axis_Aligned_Extent(&extent);
  1204. Set_Cull_Box(AABoxClass(WorldBoundingVolume.Center,extent));
  1205. }