texproject.cpp 72 KB

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