pscene_projectors.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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 : WWPhys *
  23. * *
  24. * $Archive:: /Commando/Code/wwphys/pscene_projectors.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 1/11/02 3:12p $*
  31. * *
  32. * $Revision:: 50 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "pscene.h"
  38. #include "colmathaabox.h"
  39. #include "rinfo.h"
  40. #include "staticaabtreecull.h"
  41. #include "dynamicaabtreecull.h"
  42. #include "physgridcull.h"
  43. #include "lightcull.h"
  44. #include "staticanimphys.h"
  45. #include "assetmgr.h"
  46. #include "refcount.h"
  47. #include "camera.h"
  48. #include "quat.h"
  49. #include "win.h"
  50. #include "vertmaterial.h"
  51. #include "wwprofile.h"
  52. #include "texture.h"
  53. #include "dx8wrapper.h"
  54. #include "pot.h"
  55. #include "materialeffect.h"
  56. #include "wwmemlog.h"
  57. #include "targa.h"
  58. #include "bitmaphandler.h"
  59. #include "dx8caps.h"
  60. #include "vertmaterial.h"
  61. #define DEBUG_SHADOW_RENDERING 0
  62. #if (DEBUG_SHADOW_RENDERING)
  63. #define SHADOW_WINDOW_STYLE WS_POPUP | WS_CAPTION | WS_BORDER | WS_VISIBLE
  64. #else
  65. #define SHADOW_WINDOW_STYLE WS_POPUP | WS_CAPTION | WS_BORDER
  66. #endif
  67. const int SHADOW_CLIP_FAR = 500;
  68. const int STATIC_PROJECTOR_RESOLUTION = 256; //128;
  69. const float STATIC_SHADOW_INTENSITY = 0.6f;
  70. const float MIN_STATIC_SHADOW_COS_HALF_THETA = cos(DEG_TO_RADF(10.0f)/2.0f); // angle to allow shadow tex re-use
  71. const int DEFAULT_MAX_DYNAMIC_SHADOWS = 6;
  72. const int DEFAULT_DYNAMIC_SHADOW_RESOLUTION = 256;
  73. /**
  74. ** StaticShadowTexMgrClass
  75. ** This object simply manages the list of unique shadow textures being used by the
  76. ** static shadow projectors. For example, all instances of the same tree model
  77. ** re-use the same shadow texture...
  78. */
  79. class StaticShadowTexMgrClass
  80. {
  81. public:
  82. StaticShadowTexMgrClass(void);
  83. virtual ~StaticShadowTexMgrClass(void);
  84. void Reset(void);
  85. TextureClass * Peek_Shadow_Texture(uint32 obj_type_id,const Quaternion & orientation);
  86. void Add_Shadow_Texture(uint32 obj_type_id,const Quaternion & orientation,TextureClass * tex);
  87. void Remove_Shadow_Texture(TextureClass * tex);
  88. private:
  89. class ShadowTexClass
  90. {
  91. public:
  92. ShadowTexClass(void);
  93. ShadowTexClass(uint32 obj_type_id,const Quaternion & orientation,TextureClass * tex);
  94. ShadowTexClass(const ShadowTexClass & that);
  95. ~ShadowTexClass(void);
  96. const ShadowTexClass & operator = (const ShadowTexClass &);
  97. bool operator == (const ShadowTexClass &) { return false; }
  98. bool operator != (const ShadowTexClass & that) { return true; }
  99. uint32 ObjectTypeID; // use the Definition ID to uniquely identify object types
  100. Quaternion ObjectOrientation; // orientation of the object when this shadow was generated
  101. TextureClass * Texture; // texture
  102. };
  103. DynamicVectorClass<ShadowTexClass> ShadowTextures;
  104. };
  105. /**
  106. ** DynamicShadowTexMgrClass
  107. ** This class manages a pool of render target textures which are shared by the currently
  108. ** active shadow projectors.
  109. */
  110. class DynamicShadowTexMgrClass
  111. {
  112. public:
  113. DynamicShadowTexMgrClass(void);
  114. virtual ~DynamicShadowTexMgrClass(void);
  115. // void Reset(); // Jani: Disabling reset. Re-allocating render targets if the device is out of
  116. // free texture memory causes problems on at least TNT2.
  117. void Set_Max_Simultaneous_Shadows(unsigned int max);
  118. unsigned int Get_Max_Simultaneous_Shadows(void);
  119. void Set_Shadow_Resolution(unsigned int size);
  120. unsigned int Get_Shadow_Resolution(void);
  121. void Per_Frame_Reset(void);
  122. void Assign_Render_Target_Texture(TexProjectClass * tex_proj);
  123. private:
  124. TextureClass * Allocate_Render_Target_Texture(void);
  125. unsigned int CurShadow;
  126. unsigned int TextureResolution;
  127. SimpleVecClass<TextureClass *> ShadowTextures;
  128. };
  129. /*
  130. **
  131. ** Instantiate the Shadow Texture Managers.
  132. **
  133. */
  134. static StaticShadowTexMgrClass _StaticShadowTexMgr;
  135. static DynamicShadowTexMgrClass _DynamicShadowTexMgr;
  136. static TextureClass* Create_Projector_Render_Target(unsigned w,unsigned h)
  137. {
  138. WW3DFormat format=WW3D_FORMAT_UNKNOWN;
  139. // Try if 8 or 16 bit render target formats would be supported
  140. if (DX8Wrapper::Get_Current_Caps()->Support_Render_To_Texture_Format(WW3D_FORMAT_R3G3B2)) format=WW3D_FORMAT_R3G3B2;
  141. else if (DX8Wrapper::Get_Current_Caps()->Support_Render_To_Texture_Format(WW3D_FORMAT_R5G6B5)) format=WW3D_FORMAT_R5G6B5;
  142. else if (DX8Wrapper::Get_Current_Caps()->Support_Render_To_Texture_Format(WW3D_FORMAT_A4R4G4B4)) format=WW3D_FORMAT_A4R4G4B4;
  143. else if (DX8Wrapper::Get_Current_Caps()->Support_Render_To_Texture_Format(WW3D_FORMAT_X1R5G5B5)) format=WW3D_FORMAT_X1R5G5B5;
  144. TextureClass* texture = DX8Wrapper::Create_Render_Target(w,h,format);
  145. if (texture) return texture;
  146. // As a last resort, try creating with unknown format (which means using the current display resolution)
  147. if (format!=WW3D_FORMAT_UNKNOWN) {
  148. format=WW3D_FORMAT_UNKNOWN;
  149. return DX8Wrapper::Create_Render_Target(w,h,format);
  150. }
  151. return NULL;
  152. }
  153. /************************************************************************************
  154. **
  155. ** ShadowTexClass Implemenation
  156. **
  157. ************************************************************************************/
  158. StaticShadowTexMgrClass::ShadowTexClass::ShadowTexClass(void) :
  159. ObjectTypeID(0),
  160. ObjectOrientation(1),
  161. Texture(NULL)
  162. {
  163. }
  164. StaticShadowTexMgrClass::ShadowTexClass::ShadowTexClass
  165. (
  166. uint32 obj_type_id,
  167. const Quaternion & orientation,
  168. TextureClass * tex
  169. ) :
  170. ObjectTypeID(obj_type_id),
  171. ObjectOrientation(orientation),
  172. Texture(NULL)
  173. {
  174. REF_PTR_SET(Texture,tex);
  175. }
  176. StaticShadowTexMgrClass::ShadowTexClass::ShadowTexClass(const ShadowTexClass & that) :
  177. ObjectTypeID(0),
  178. ObjectOrientation(1),
  179. Texture(NULL)
  180. {
  181. *this = that;
  182. }
  183. const StaticShadowTexMgrClass::ShadowTexClass &
  184. StaticShadowTexMgrClass::ShadowTexClass::operator = (const ShadowTexClass & that)
  185. {
  186. ObjectTypeID = that.ObjectTypeID;
  187. ObjectOrientation = that.ObjectOrientation;
  188. REF_PTR_SET(Texture,that.Texture);
  189. return *this;
  190. }
  191. StaticShadowTexMgrClass::ShadowTexClass::~ShadowTexClass(void)
  192. {
  193. REF_PTR_RELEASE(Texture);
  194. }
  195. /************************************************************************************
  196. **
  197. ** StaticShadowTexMgrClass Implemenation
  198. **
  199. ************************************************************************************/
  200. StaticShadowTexMgrClass::StaticShadowTexMgrClass(void)
  201. {
  202. }
  203. StaticShadowTexMgrClass::~StaticShadowTexMgrClass(void)
  204. {
  205. }
  206. void StaticShadowTexMgrClass::Reset(void)
  207. {
  208. ShadowTextures.Delete_All();
  209. }
  210. TextureClass * StaticShadowTexMgrClass::Peek_Shadow_Texture
  211. (
  212. uint32 obj_type_id,
  213. const Quaternion & orientation
  214. )
  215. {
  216. for (int i=0; i<ShadowTextures.Count(); i++) {
  217. Quaternion deltaq = ShadowTextures[i].ObjectOrientation * Inverse(orientation);
  218. if ( (ShadowTextures[i].ObjectTypeID == obj_type_id) &&
  219. (deltaq.W > MIN_STATIC_SHADOW_COS_HALF_THETA))
  220. {
  221. return ShadowTextures[i].Texture;
  222. }
  223. }
  224. return NULL;
  225. }
  226. void StaticShadowTexMgrClass::Add_Shadow_Texture
  227. (
  228. uint32 obj_type_id,
  229. const Quaternion & orientation,
  230. TextureClass * tex
  231. )
  232. {
  233. WWASSERT(Peek_Shadow_Texture(obj_type_id,orientation) == NULL);
  234. ShadowTexClass record(obj_type_id,orientation,tex);
  235. ShadowTextures.Add(record);
  236. }
  237. void StaticShadowTexMgrClass::Remove_Shadow_Texture
  238. (
  239. TextureClass * tex
  240. )
  241. {
  242. for (int i=0; i<ShadowTextures.Count(); i++) {
  243. if (ShadowTextures[i].Texture == tex) {
  244. ShadowTextures.Delete(i);
  245. return;
  246. }
  247. }
  248. }
  249. /************************************************************************************
  250. **
  251. ** DynamicShadowTexMgrClass Implementation
  252. **
  253. ************************************************************************************/
  254. DynamicShadowTexMgrClass::DynamicShadowTexMgrClass(void) :
  255. CurShadow(0),
  256. TextureResolution(DEFAULT_DYNAMIC_SHADOW_RESOLUTION)
  257. {
  258. WWMEMLOG(MEM_GAMEDATA);
  259. ShadowTextures.Resize(DEFAULT_MAX_DYNAMIC_SHADOWS);
  260. for (int i=0; i<ShadowTextures.Length(); i++) {
  261. ShadowTextures[i] = NULL;
  262. }
  263. }
  264. DynamicShadowTexMgrClass::~DynamicShadowTexMgrClass(void)
  265. {
  266. // Reset();
  267. for (int i=0; i<ShadowTextures.Length(); i++) {
  268. REF_PTR_RELEASE(ShadowTextures[i]);
  269. }
  270. }
  271. /*
  272. void DynamicShadowTexMgrClass::Reset(void)
  273. {
  274. for (int i=0; i<ShadowTextures.Length(); i++) {
  275. REF_PTR_RELEASE(ShadowTextures[i]);
  276. }
  277. }
  278. */
  279. // Set the maximum number of dynamic render targets. Allocate all
  280. // textures at this point!
  281. void DynamicShadowTexMgrClass::Set_Max_Simultaneous_Shadows(unsigned int max)
  282. {
  283. int curlen = ShadowTextures.Length();
  284. for (int i=max;i<curlen; i++) {
  285. REF_PTR_RELEASE(ShadowTextures[i]);
  286. }
  287. ShadowTextures.Resize(max);
  288. if (curlen>ShadowTextures.Length()) curlen=ShadowTextures.Length();
  289. for (i=0; i<curlen; i++) {
  290. if (!ShadowTextures[i]) {
  291. ShadowTextures[i] = Allocate_Render_Target_Texture();
  292. }
  293. }
  294. for (; i<ShadowTextures.Length(); i++) {
  295. ShadowTextures[i] = Allocate_Render_Target_Texture();
  296. }
  297. }
  298. unsigned int DynamicShadowTexMgrClass::Get_Max_Simultaneous_Shadows(void)
  299. {
  300. return ShadowTextures.Length();
  301. }
  302. void DynamicShadowTexMgrClass::Set_Shadow_Resolution(unsigned int res)
  303. {
  304. unsigned int oksize = ::Find_POT(res);
  305. if (oksize > 256) {
  306. oksize = 256;
  307. }
  308. if (oksize < 16) {
  309. oksize = 16;
  310. }
  311. if (oksize != TextureResolution) {
  312. int i;
  313. for (i=0; i<ShadowTextures.Length(); i++) {
  314. REF_PTR_RELEASE(ShadowTextures[i]);
  315. }
  316. TextureResolution = oksize;
  317. for (i=0; i<ShadowTextures.Length(); i++) {
  318. ShadowTextures[i] = Allocate_Render_Target_Texture();
  319. }
  320. }
  321. }
  322. unsigned int DynamicShadowTexMgrClass::Get_Shadow_Resolution(void)
  323. {
  324. return TextureResolution;
  325. }
  326. void DynamicShadowTexMgrClass::Per_Frame_Reset(void)
  327. {
  328. CurShadow = 0;
  329. }
  330. void DynamicShadowTexMgrClass::Assign_Render_Target_Texture(TexProjectClass * tex_proj)
  331. {
  332. if (CurShadow < (unsigned int)ShadowTextures.Length()) {
  333. if (ShadowTextures[CurShadow] == NULL) {
  334. ShadowTextures[CurShadow] = Allocate_Render_Target_Texture();
  335. }
  336. tex_proj->Set_Render_Target(ShadowTextures[CurShadow]);
  337. CurShadow++;
  338. }
  339. }
  340. TextureClass * DynamicShadowTexMgrClass::Allocate_Render_Target_Texture(void)
  341. {
  342. TextureClass * texture=Create_Projector_Render_Target(TextureResolution,TextureResolution);
  343. if (texture != NULL) {
  344. SET_REF_OWNER(texture);
  345. texture->Set_U_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  346. texture->Set_V_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  347. }
  348. return texture;
  349. }
  350. /************************************************************************************
  351. **
  352. ** PhysicsSceneClass Texture Projection Code
  353. **
  354. ************************************************************************************/
  355. void PhysicsSceneClass::Release_Projector_Resources(void)
  356. {
  357. if (ShadowRenderContext != NULL) {
  358. delete ShadowRenderContext;
  359. ShadowRenderContext=NULL;
  360. }
  361. REF_PTR_RELEASE(ShadowMaterialPass);
  362. REF_PTR_RELEASE(ShadowCamera);
  363. REF_PTR_RELEASE(ShadowBlobTexture);
  364. _StaticShadowTexMgr.Reset();
  365. // _DynamicShadowTexMgr.Reset();
  366. }
  367. void PhysicsSceneClass::Set_Shadow_Resolution(unsigned int res)
  368. {
  369. _DynamicShadowTexMgr.Set_Shadow_Resolution(res);
  370. }
  371. unsigned int PhysicsSceneClass::Get_Shadow_Resolution(void)
  372. {
  373. return _DynamicShadowTexMgr.Get_Shadow_Resolution();
  374. }
  375. void PhysicsSceneClass::Set_Max_Simultaneous_Shadows(unsigned int count)
  376. {
  377. _DynamicShadowTexMgr.Set_Max_Simultaneous_Shadows(count);
  378. }
  379. unsigned int PhysicsSceneClass::Get_Max_Simultaneous_Shadows(void)
  380. {
  381. return _DynamicShadowTexMgr.Get_Max_Simultaneous_Shadows();
  382. }
  383. SpecialRenderInfoClass *
  384. PhysicsSceneClass::Get_Shadow_Render_Context(int width,int height)
  385. {
  386. if (ShadowRenderContext == NULL) {
  387. /*
  388. ** Create a camera for shadow rendering to use
  389. */
  390. if (ShadowCamera == NULL) {
  391. ShadowCamera = NEW_REF(CameraClass,());
  392. ShadowCamera->Set_Clip_Planes(0.2f,SHADOW_CLIP_FAR);
  393. ShadowCamera->Set_View_Plane(DEG_TO_RAD(90.0f),DEG_TO_RAD(90.0f));
  394. ShadowCamera->Set_Viewport(Vector2(0,0),Vector2(1,1));
  395. }
  396. /*
  397. ** Create the render context
  398. */
  399. ShadowRenderContext = new SpecialRenderInfoClass(*ShadowCamera,SpecialRenderInfoClass::RENDER_SHADOW);
  400. }
  401. return ShadowRenderContext;
  402. }
  403. MaterialPassClass * PhysicsSceneClass::Get_Shadow_Material_Pass(void)
  404. {
  405. if (ShadowMaterialPass == NULL) {
  406. VertexMaterialClass * vmtl = NEW_REF(VertexMaterialClass,());
  407. vmtl->Set_Ambient(0,0,0);
  408. vmtl->Set_Diffuse(0,0,0);
  409. vmtl->Set_Specular(0,0,0);
  410. vmtl->Set_Emissive(0,0,0);
  411. vmtl->Set_Lighting(true);
  412. ShaderClass shader = ShaderClass::_PresetOpaqueShader;
  413. shader.Set_Depth_Compare(ShaderClass::PASS_ALWAYS);
  414. shader.Set_Depth_Mask(ShaderClass::DEPTH_WRITE_DISABLE);
  415. shader.Set_Texturing(ShaderClass::TEXTURING_DISABLE);
  416. ShadowMaterialPass = NEW_REF(MaterialPassClass,());
  417. ShadowMaterialPass->Set_Material(vmtl);
  418. ShadowMaterialPass->Set_Shader(shader);
  419. ShadowMaterialPass->Enable_On_Translucent_Meshes(false);
  420. REF_PTR_RELEASE(vmtl);
  421. }
  422. ShadowMaterialPass->Add_Ref();
  423. return ShadowMaterialPass;
  424. }
  425. void PhysicsSceneClass::Enable_Static_Projectors(bool onoff)
  426. {
  427. StaticProjectorsEnabled = onoff;
  428. }
  429. bool PhysicsSceneClass::Are_Static_Projectors_Enabled(void)
  430. {
  431. return StaticProjectorsEnabled;
  432. }
  433. void PhysicsSceneClass::Enable_Dynamic_Projectors(bool onoff)
  434. {
  435. DynamicProjectorsEnabled = onoff;
  436. }
  437. bool PhysicsSceneClass::Are_Dynamic_Projectors_Enabled(void)
  438. {
  439. return DynamicProjectorsEnabled;
  440. }
  441. void PhysicsSceneClass::Set_Shadow_Mode(ShadowEnum shadow_mode)
  442. {
  443. if (((int)shadow_mode >= 0) && ((int)shadow_mode < SHADOW_MODE_COUNT)) {
  444. if (ShadowMode!=shadow_mode) {
  445. ShadowMode = shadow_mode;
  446. switch (ShadowMode) {
  447. default:
  448. case SHADOW_MODE_NONE: // no shadows at all
  449. case SHADOW_MODE_BLOBS: // projected blob shadows
  450. Set_Max_Simultaneous_Shadows(0);
  451. break;
  452. case SHADOW_MODE_BLOBS_PLUS: // projected blobs with main character having a rendered shadow
  453. Set_Max_Simultaneous_Shadows(1);
  454. break;
  455. case SHADOW_MODE_HARDWARE: // use render-to-texture hardware
  456. Set_Max_Simultaneous_Shadows(4);
  457. break;
  458. }
  459. }
  460. }
  461. }
  462. PhysicsSceneClass::ShadowEnum
  463. PhysicsSceneClass::Get_Shadow_Mode(void)
  464. {
  465. return ShadowMode;
  466. }
  467. void PhysicsSceneClass::Set_Shadow_Attenuation(float atten_start_distance,float atten_end_distance)
  468. {
  469. if (atten_start_distance < 0.0f) {
  470. atten_start_distance = 0.0f;
  471. }
  472. if (atten_end_distance < atten_start_distance) {
  473. atten_end_distance = atten_start_distance;
  474. }
  475. ShadowAttenStart = atten_start_distance;
  476. ShadowAttenEnd = atten_end_distance;
  477. }
  478. void PhysicsSceneClass::Get_Shadow_Attenuation(float * set_atten_start,float * set_atten_end)
  479. {
  480. if (set_atten_start != NULL) {
  481. *set_atten_start = ShadowAttenStart;
  482. }
  483. if (set_atten_end != NULL) {
  484. *set_atten_end = ShadowAttenEnd;
  485. }
  486. }
  487. void PhysicsSceneClass::Set_Shadow_Normal_Intensity(float normal_intensity)
  488. {
  489. if (normal_intensity < 0.0f) {
  490. normal_intensity = 0.0f;
  491. }
  492. if (normal_intensity > 1.0f) {
  493. normal_intensity = 1.0f;
  494. }
  495. ShadowNormalIntensity = normal_intensity;
  496. }
  497. float PhysicsSceneClass::Get_Shadow_Normal_Intensity(void)
  498. {
  499. return ShadowNormalIntensity;
  500. }
  501. void PhysicsSceneClass::Add_Static_Texture_Projector(TexProjectClass * newprojector)
  502. {
  503. WWASSERT(newprojector);
  504. WWASSERT(!StaticProjectorList.Is_In_List(newprojector));
  505. StaticProjectorList.Add(newprojector);
  506. StaticProjectorCullingSystem->Add_Object(newprojector);
  507. }
  508. void PhysicsSceneClass::Remove_Static_Texture_Projector(TexProjectClass * projector)
  509. {
  510. WWASSERT(projector);
  511. if (projector->Get_Culling_System() == StaticProjectorCullingSystem) {
  512. WWASSERT(StaticProjectorList.Is_In_List(projector));
  513. StaticProjectorList.Remove(projector);
  514. StaticProjectorCullingSystem->Remove_Object(projector);
  515. }
  516. }
  517. void PhysicsSceneClass::Add_Dynamic_Texture_Projector(TexProjectClass * newprojector)
  518. {
  519. WWASSERT(newprojector);
  520. WWASSERT(!DynamicProjectorList.Is_In_List(newprojector));
  521. DynamicProjectorList.Add(newprojector);
  522. DynamicProjectorCullingSystem->Add_Object(newprojector);
  523. }
  524. void PhysicsSceneClass::Remove_Dynamic_Texture_Projector(TexProjectClass * projector)
  525. {
  526. WWASSERT(projector);
  527. if (projector->Get_Culling_System() == DynamicProjectorCullingSystem) {
  528. WWASSERT(DynamicProjectorList.Is_In_List(projector));
  529. DynamicProjectorList.Remove(projector);
  530. DynamicProjectorCullingSystem->Remove_Object(projector);
  531. }
  532. }
  533. void PhysicsSceneClass::Remove_Texture_Projector(TexProjectClass * projector)
  534. {
  535. WWASSERT(projector);
  536. if (DynamicProjectorList.Is_In_List(projector)) {
  537. DynamicProjectorCullingSystem->Remove_Object(projector);
  538. DynamicProjectorList.Remove(projector);
  539. } else if (StaticProjectorList.Is_In_List(projector)) {
  540. StaticProjectorCullingSystem->Remove_Object(projector);
  541. StaticProjectorList.Remove(projector);
  542. }
  543. }
  544. bool PhysicsSceneClass::Contains(TexProjectClass * projector)
  545. {
  546. return (DynamicProjectorList.Is_In_List(projector) || StaticProjectorList.Is_In_List(projector));
  547. }
  548. float PhysicsSceneClass::Compute_Projector_Attenuation(TexProjectClass * dynamic_projector,const Vector3 & view_pos,const Vector3 & view_dir)
  549. {
  550. Vector3 r;
  551. Vector3::Subtract(dynamic_projector->Get_Bounding_Volume().Center,view_pos,&r);
  552. float dist = Vector3::Dot_Product(r,view_dir);
  553. if (dist > ShadowAttenEnd) {
  554. return 0.0f;
  555. }
  556. if (dist < ShadowAttenStart) {
  557. return 1.0f;
  558. }
  559. return 1.0f - (dist - ShadowAttenStart) / (ShadowAttenEnd - ShadowAttenStart);
  560. }
  561. void PhysicsSceneClass::Apply_Projectors
  562. (
  563. const CameraClass & camera
  564. )
  565. {
  566. WWPROFILE("pscene::Apply_Projectors");
  567. Vector3 view_pos;
  568. Vector3 view_dir;
  569. camera.Get_Transform().Get_Translation(&view_pos);
  570. camera.Get_Transform().Get_Z_Vector(&view_dir);
  571. view_dir = -view_dir;
  572. if (StaticProjectorsEnabled) {
  573. /*
  574. ** collect the visible static projectors
  575. */
  576. StaticProjectorCullingSystem->Reset_Collection();
  577. StaticProjectorCullingSystem->Collect_Objects(camera.Get_Frustum());
  578. TexProjectClass * static_projector = StaticProjectorCullingSystem->Get_First_Collected_Object();
  579. while (static_projector != NULL) {
  580. /*
  581. ** only keep considering this projector if its intensity is above ZERO
  582. */
  583. if (!static_projector->Is_Intensity_Zero()) {
  584. /*
  585. ** attenuate the projector with distance from the camera (if needed)
  586. */
  587. float attenuation = 1.0f;
  588. if (static_projector->Is_Attenuation_Enabled()) {
  589. attenuation = Compute_Projector_Attenuation(static_projector,view_pos,view_dir);
  590. static_projector->Set_Attenuation(attenuation);
  591. }
  592. /*
  593. ** if the projector is completely attenuated, don't process it
  594. */
  595. if (attenuation > 0.0f) {
  596. if (ProjectorDebugDisplayEnabled) {
  597. DEBUG_RENDER_OBBOX(static_projector->Get_Bounding_Volume(),Vector3(0,1,0),0.25f);
  598. }
  599. Apply_Projector_To_Objects(static_projector,camera);
  600. }
  601. }
  602. static_projector = StaticProjectorCullingSystem->Get_Next_Collected_Object(static_projector);
  603. }
  604. }
  605. /*
  606. ** Build a list of the dynamic shadow textures that need to be rendered
  607. */
  608. TexProjListClass rt_projector_list;
  609. unsigned int count = 0;
  610. if (DynamicProjectorsEnabled) {
  611. /*
  612. ** Collect the visible dynamic projectors
  613. */
  614. DynamicProjectorCullingSystem->Reset_Collection();
  615. DynamicProjectorCullingSystem->Collect_Objects(camera.Get_Frustum());
  616. TexProjectClass * dynamic_projector = DynamicProjectorCullingSystem->Get_First_Collected_Object();
  617. while (dynamic_projector != NULL) {
  618. /*
  619. ** only keep considering this projector if its intensity is above ZERO
  620. */
  621. if (!dynamic_projector->Is_Intensity_Zero()) {
  622. /*
  623. ** attenuate the projector with distance from the camera (if needed)
  624. */
  625. float attenuation = 1.0f;
  626. if (dynamic_projector->Is_Attenuation_Enabled()) {
  627. attenuation = Compute_Projector_Attenuation(dynamic_projector,view_pos,view_dir);
  628. dynamic_projector->Set_Attenuation(attenuation);
  629. }
  630. /*
  631. ** if the projector is completely attenuated, don't process it
  632. */
  633. if (attenuation > 0.0f) {
  634. if (ProjectorDebugDisplayEnabled) {
  635. DEBUG_RENDER_OBBOX(dynamic_projector->Get_Bounding_Volume(),Vector3(1,0,0),0.25f);
  636. }
  637. /*
  638. ** If this projector needs to recompute its texture, add it to the list,
  639. ** otherwise set it up now.
  640. */
  641. if (dynamic_projector->Needs_Render_Target()) {
  642. rt_projector_list.Add(dynamic_projector);
  643. count++;
  644. } else {
  645. Apply_Projector_To_Objects(dynamic_projector,camera);
  646. }
  647. }
  648. }
  649. dynamic_projector = DynamicProjectorCullingSystem->Get_Next_Collected_Object(dynamic_projector);
  650. }
  651. }
  652. /*
  653. ** Reject texture projectors until we are below the maximum allowed simultaneous shadows
  654. */
  655. Vector3 cam_pos;
  656. camera.Get_Transform().Get_Translation(&cam_pos);
  657. TexProjListIterator it(&rt_projector_list);
  658. while (count > _DynamicShadowTexMgr.Get_Max_Simultaneous_Shadows()) {
  659. /*
  660. ** Find the projector farthest from the camera
  661. */
  662. it.First();
  663. TexProjectClass * farthest_shadow = it.Peek_Obj();
  664. float farthest_dist = (farthest_shadow->Get_Bounding_Volume().Center - cam_pos).Length2();
  665. it.Next();
  666. while (!it.Is_Done()) {
  667. float dist = (it.Peek_Obj()->Get_Bounding_Volume().Center - cam_pos).Length2();
  668. if (dist > farthest_dist) {
  669. farthest_dist = dist;
  670. farthest_shadow = it.Peek_Obj();
  671. }
  672. it.Next();
  673. }
  674. /*
  675. ** Remove it from the list and disable it.
  676. */
  677. rt_projector_list.Remove(farthest_shadow);
  678. farthest_shadow->Set_Render_Target(NULL);
  679. count--;
  680. }
  681. /*
  682. ** Process the most "important" active dynamic shadows
  683. */
  684. _DynamicShadowTexMgr.Per_Frame_Reset();
  685. it.First();
  686. while (!it.Is_Done()) {
  687. TexProjectClass * projector = it.Peek_Obj();
  688. _DynamicShadowTexMgr.Assign_Render_Target_Texture(projector);
  689. if (projector->Peek_Render_Target() != NULL) {
  690. Apply_Projector_To_Objects(projector,camera);
  691. }
  692. it.Next();
  693. }
  694. DX8Wrapper::Set_Render_Target((IDirect3DSurface8 *)NULL);
  695. }
  696. void PhysicsSceneClass::Apply_Projector_To_Objects
  697. (
  698. TexProjectClass * tex_proj,
  699. const CameraClass & camera
  700. )
  701. {
  702. bool projector_update_needed = false; // Needed if just one mesh uses it
  703. SimpleEffectClass * effect = NEW_REF(SimpleEffectClass,(tex_proj->Peek_Material_Pass()));
  704. effect->Enable_Auto_Remove(true);
  705. /*
  706. ** collect the static objects intersecting the projector's volume
  707. */
  708. if (tex_proj->Is_Affect_Static_Objects_Enabled()) {
  709. StaticCullingSystem->Reset_Collection();
  710. StaticCullingSystem->Collect_Objects(tex_proj->Get_Bounding_Volume());
  711. StaticPhysClass * static_obj = (StaticPhysClass *)StaticCullingSystem->Get_First_Collected_Object();
  712. while (static_obj) {
  713. /*
  714. ** for each static object, if it also in our visible list, add this projector to it
  715. ** since the visible static objects are split into two lists we check both.
  716. */
  717. if (VisibleStaticObjectList.Contains(static_obj) || VisibleWSMeshList.Is_In_List(static_obj)) {
  718. // check if our volume actually intersects the mesh!
  719. if (static_obj->Intersects(tex_proj->Get_Bounding_Volume())) {
  720. static_obj->Add_Effect_To_Me(effect);
  721. projector_update_needed = true;
  722. }
  723. }
  724. static_obj = (StaticPhysClass *)StaticCullingSystem->Get_Next_Collected_Object(static_obj);
  725. }
  726. }
  727. /*
  728. ** collect the dynamic objects intersecting the projector's volume
  729. */
  730. if (tex_proj->Is_Affect_Dynamic_Objects_Enabled()) {
  731. DynamicCullingSystem->Reset_Collection();
  732. DynamicCullingSystem->Collect_Objects(tex_proj->Get_Bounding_Volume());
  733. PhysClass * dyn_obj = DynamicCullingSystem->Get_First_Collected_Object();
  734. while (dyn_obj) {
  735. /*
  736. ** for each dynamic object, if it also in our visible list and is not the
  737. ** object that generated this shadow, add this projector to it
  738. */
  739. if ((VisibleDynamicObjectList.Contains(dyn_obj)) && (dyn_obj != tex_proj->Get_Projection_Object_ID())) {
  740. dyn_obj->Add_Effect_To_Me(effect);
  741. projector_update_needed = true;
  742. }
  743. dyn_obj = DynamicCullingSystem->Get_Next_Collected_Object(dyn_obj);
  744. }
  745. }
  746. // if this projector is needed, update it
  747. if (projector_update_needed) {
  748. tex_proj->Pre_Render_Update(camera.Get_Transform());
  749. }
  750. REF_PTR_RELEASE(effect);
  751. }
  752. static void Create_Render_Target_Test(TextureClass* render_target)
  753. {
  754. Matrix4 old_view_transform;
  755. Matrix4 old_world_transform;
  756. Matrix4 old_projection_transform;
  757. DX8Wrapper::Get_Transform(D3DTS_VIEW,old_view_transform);
  758. DX8Wrapper::Get_Transform(D3DTS_WORLD,old_world_transform);
  759. DX8Wrapper::Get_Transform(D3DTS_PROJECTION,old_projection_transform);
  760. /*
  761. ** Set the render target
  762. */
  763. DX8Wrapper::Set_Render_Target(render_target);
  764. /*
  765. ** Set up the camera
  766. */
  767. // Configure_Camera(context->Camera);
  768. /*
  769. ** Render the object
  770. */
  771. Vector3 color(0.0f,0.0f,0.0f);
  772. WW3D::Begin_Render(true,false,color); // false to zclear as we don't have z-buffer
  773. DX8Wrapper::Set_World_Identity();
  774. DX8Wrapper::Set_View_Identity();
  775. DX8Wrapper::Set_Transform(D3DTS_PROJECTION,Matrix4(true));
  776. const int vertex_count=9;
  777. const int polygon_count=3;
  778. DynamicVBAccessClass vb_access(BUFFER_TYPE_DYNAMIC_DX8,dynamic_fvf_type,vertex_count);
  779. {
  780. DynamicVBAccessClass::WriteLockClass lock(&vb_access);
  781. VertexFormatXYZNDUV2* verts=lock.Get_Formatted_Vertex_Array();
  782. Vector3 vertices[vertex_count]={
  783. Vector3(-1.0f, 1.0f,0.0f),
  784. Vector3(-1.0f,-1.0f,0.0f),
  785. Vector3( 0.0f,-1.0f,0.0f),
  786. Vector3(-1.0f,-1.0f,0.0f),
  787. Vector3( 1.0f,-1.0f,0.0f),
  788. Vector3( 1.0f, 0.0f,0.0f),
  789. Vector3( 1.0f,-1.0f,0.0f),
  790. Vector3( 1.0f, 1.0f,0.0f),
  791. Vector3( 0.0f, 1.0f,0.0f)
  792. };
  793. unsigned colors[vertex_count]={
  794. 0xff0000,
  795. 0xff0000,
  796. 0xff0000,
  797. 0x00ff00,
  798. 0x00ff00,
  799. 0x00ff00,
  800. 0x0000ff,
  801. 0x0000ff,
  802. 0x0000ff
  803. };
  804. for (int v=0;v<vertex_count;++v) {
  805. verts[v].x=vertices[v][0];
  806. verts[v].y=vertices[v][1];
  807. verts[v].z=vertices[v][2];
  808. verts[v].nx=0.0f;
  809. verts[v].ny=0.0f;
  810. verts[v].nz=0.0f;
  811. verts[v].diffuse=colors[v];
  812. verts[v].u1=0.0f;
  813. verts[v].v1=0.0f;
  814. verts[v].u2=0.0f;
  815. verts[v].v2=0.0f;
  816. }
  817. }
  818. DX8Wrapper::Set_Vertex_Buffer(vb_access);
  819. DynamicIBAccessClass ib_access(BUFFER_TYPE_DYNAMIC_DX8,polygon_count*3);
  820. {
  821. DynamicIBAccessClass::WriteLockClass lock(&ib_access);
  822. unsigned short* inds=lock.Get_Index_Array();
  823. for (int i=0;i<polygon_count*3;++i) {
  824. *inds++=short(i);
  825. }
  826. }
  827. ShaderClass shader;
  828. shader.Set_Cull_Mode( ShaderClass::CULL_MODE_DISABLE );
  829. shader.Set_Depth_Mask( ShaderClass::DEPTH_WRITE_DISABLE );
  830. shader.Set_Depth_Compare( ShaderClass::PASS_ALWAYS );
  831. shader.Set_Dst_Blend_Func( ShaderClass::DSTBLEND_ONE );
  832. shader.Set_Src_Blend_Func( ShaderClass::SRCBLEND_ONE );
  833. shader.Set_Fog_Func( ShaderClass::FOG_DISABLE );
  834. shader.Set_Primary_Gradient( ShaderClass::GRADIENT_MODULATE );
  835. shader.Set_Texturing( ShaderClass::TEXTURING_DISABLE );
  836. DX8Wrapper::Set_Index_Buffer(ib_access,0);
  837. DX8Wrapper::Set_Shader(shader);
  838. DX8Wrapper::Set_Texture(0,NULL);
  839. DX8Wrapper::Draw_Triangles(0,polygon_count,0,vertex_count);
  840. // WW3D::Render(*model,*context);
  841. WW3D::End_Render(false);
  842. DX8Wrapper::Set_Render_Target((IDirect3DSurface8 *)NULL);
  843. DX8Wrapper::Set_Transform(D3DTS_PROJECTION,old_projection_transform);
  844. DX8Wrapper::Set_Transform(D3DTS_VIEW,old_view_transform);
  845. DX8Wrapper::Set_Transform(D3DTS_WORLD,old_world_transform);
  846. }
  847. static bool Test_Render_Target_Surface(TextureClass* render_target)
  848. {
  849. SurfaceClass * surf = render_target->Get_Surface_Level();
  850. SurfaceClass::SurfaceDescription desc;
  851. surf->Get_Description(desc);
  852. SurfaceClass * new_surf = NEW_REF(SurfaceClass,(desc.Width,desc.Height,desc.Format));
  853. DX8Wrapper::_Copy_DX8_Rects(surf->Peek_D3D_Surface(),NULL,0,new_surf->Peek_D3D_Surface(),NULL);
  854. REF_PTR_RELEASE(surf);
  855. int pitch;
  856. unsigned char* tmpbits=(unsigned char*)new_surf->Lock(&pitch);
  857. unsigned color1,color2,color3,color4;
  858. BitmapHandlerClass::Read_B8G8R8A8(
  859. color1, tmpbits, desc.Format,
  860. 1*desc.Width/4,
  861. 1*desc.Height/4,
  862. desc.Width, desc.Height, NULL, 0);
  863. BitmapHandlerClass::Read_B8G8R8A8(
  864. color2, tmpbits, desc.Format,
  865. 3*desc.Width/4,
  866. 1*desc.Height/4,
  867. desc.Width, desc.Height, NULL, 0);
  868. BitmapHandlerClass::Read_B8G8R8A8(
  869. color3, tmpbits, desc.Format,
  870. 3*desc.Width/4,
  871. 3*desc.Height/4,
  872. desc.Width, desc.Height, NULL, 0);
  873. BitmapHandlerClass::Read_B8G8R8A8(
  874. color4, tmpbits, desc.Format,
  875. 1*desc.Width/4,
  876. 3*desc.Height/4,
  877. desc.Width, desc.Height, NULL, 0);
  878. new_surf->Unlock();
  879. REF_PTR_RELEASE(new_surf);
  880. WWDEBUG_SAY(("Render target test: 0x%8.8x, 0x%8.8x, 0x%8.8x, 0x%8.8x\n",
  881. color1&0x00ffffff,
  882. color2&0x00ffffff,
  883. color3&0x00ffffff,
  884. color4&0x00ffffff));
  885. // color1 is 0x00000000
  886. if (color1&0x00ffffff) return false;
  887. // color2 is 0x000000ff
  888. if (color2&0x00ffff00) return false;
  889. if (!(color2&0x000000ff)) return false;
  890. // color3 is 0x0000ff00
  891. if (color3&0x00ff00ff) return false;
  892. if (!(color3&0x0000ff00)) return false;
  893. // color4 is 0x00ff0000
  894. if (color4&0x0000ffff) return false;
  895. if (!(color4&0x00ff0000)) return false;
  896. return true;
  897. }
  898. void PhysicsSceneClass::Invalidate_Static_Shadow_Projectors()
  899. {
  900. /*
  901. ** Collect a list of all static objects who want to generate a shadow
  902. ** Tell each to destroy their shadow
  903. */
  904. RefPhysListClass shadow_gen_list;
  905. RefPhysListIterator static_anim_iterator(&StaticAnimList);
  906. for (static_anim_iterator.First(); !static_anim_iterator.Is_Done(); static_anim_iterator.Next()) {
  907. StaticAnimPhysClass * obj = (StaticAnimPhysClass *)static_anim_iterator.Peek_Obj();
  908. if (obj != NULL) {
  909. StaticAnimPhysDefClass * def = obj->Get_StaticAnimPhysDef();
  910. if (def && def->Shadow_Dynamic_Objs()) {
  911. obj->Set_Shadow(NULL);
  912. shadow_gen_list.Add(obj);
  913. }
  914. }
  915. }
  916. StaticProjectorsDirty=true;
  917. /*
  918. ** Release all of the textures we were using for static shadows
  919. */
  920. _StaticShadowTexMgr.Reset();
  921. }
  922. void PhysicsSceneClass::Generate_Static_Shadow_Projectors(void)
  923. {
  924. if (!StaticProjectorsDirty) return;
  925. // Don't operate if the device is lost!
  926. if (DX8Wrapper::Is_Device_Lost() || !DX8Wrapper::Is_Initted()) return;
  927. /*
  928. ** Collect a list of all static objects who want to generate a shadow
  929. ** Tell each to destroy their shadow
  930. */
  931. RefPhysListClass shadow_gen_list;
  932. RefPhysListIterator static_anim_iterator(&StaticAnimList);
  933. for (static_anim_iterator.First(); !static_anim_iterator.Is_Done(); static_anim_iterator.Next()) {
  934. StaticAnimPhysClass * obj = (StaticAnimPhysClass *)static_anim_iterator.Peek_Obj();
  935. if (obj != NULL) {
  936. StaticAnimPhysDefClass * def = obj->Get_StaticAnimPhysDef();
  937. if (def && def->Shadow_Dynamic_Objs()) {
  938. obj->Set_Shadow(NULL);
  939. shadow_gen_list.Add(obj);
  940. }
  941. }
  942. }
  943. /*
  944. ** Release all of the textures we were using for static shadows
  945. */
  946. _StaticShadowTexMgr.Reset();
  947. /*
  948. ** Allocate a render target texture for all of the static shadows to share
  949. */
  950. TextureClass * render_target = Create_Projector_Render_Target(STATIC_PROJECTOR_RESOLUTION,STATIC_PROJECTOR_RESOLUTION);
  951. // TextureClass * render_target = DX8Wrapper::Create_Render_Target(STATIC_PROJECTOR_RESOLUTION,STATIC_PROJECTOR_RESOLUTION);
  952. // Test render target functionality. Some NVidia driver versions have issues with rendering to a texture and
  953. // copying surface to another texture. If this fails, we'll not use static shadow projectors. Dynamic shadow
  954. // projectors may still work however, as they work differently.
  955. if (render_target) {
  956. Create_Render_Target_Test(render_target);
  957. if (!Test_Render_Target_Surface(render_target)) {
  958. WWDEBUG_SAY(("Render target locking doesn't work correctly. Disabling static projectors\n"));
  959. REF_PTR_RELEASE(render_target);
  960. }
  961. /*
  962. char* bits=new char[desc.Width*desc.Height*3];
  963. //memcpy(bits,tmpbits,desc.Width*desc.Height*4);
  964. for (unsigned int y=0;y<desc.Height;++y) {
  965. for (unsigned int x=0;x<desc.Width;++x) {
  966. // index for image
  967. unsigned index=3*(x+y*desc.Width);
  968. // index for fb
  969. unsigned index2=y*pitch+4*x;
  970. bits[index]=*(tmpbits + index2+2);
  971. bits[index+1]=*(tmpbits + index2+1);
  972. bits[index+2]=*(tmpbits + index2+0);
  973. }
  974. }
  975. new_surf->Unlock();
  976. Targa targ;
  977. memset(&targ.Header,0,sizeof(targ.Header));
  978. targ.Header.Width=desc.Width;
  979. targ.Header.Height=desc.Height;
  980. targ.Header.PixelDepth=24;
  981. targ.Header.ImageType=TGA_TRUECOLOR;
  982. targ.SetImage((char*)bits);
  983. targ.YFlip();
  984. const char* filename="shadowtex.tga";
  985. targ.Save(filename,TGAF_IMAGE,false);
  986. delete[] bits;
  987. */
  988. }
  989. if (render_target != NULL) {
  990. SET_REF_OWNER(render_target);
  991. /*
  992. ** Generate a new shadow for each one. Each time we find a new object-lightsource
  993. ** combination, generate a new texture.
  994. */
  995. RefPhysListIterator shadow_gen_iterator(&shadow_gen_list);
  996. for (shadow_gen_iterator.First(); !shadow_gen_iterator.Is_Done(); shadow_gen_iterator.Next()) {
  997. StaticAnimPhysClass * obj = (StaticAnimPhysClass *)shadow_gen_iterator.Peek_Obj();
  998. /*
  999. ** Setup the shadow projector for this object
  1000. */
  1001. Vector3 sunvector;
  1002. Get_Sun_Light_Vector(&sunvector);
  1003. Setup_Static_Directional_Shadow(*obj,sunvector,render_target);
  1004. }
  1005. DX8Wrapper::Set_Render_Target((IDirect3DSurface8 *)NULL);
  1006. REF_PTR_RELEASE(render_target);
  1007. }
  1008. StaticProjectorsDirty=false;
  1009. }
  1010. void PhysicsSceneClass::Setup_Static_Directional_Shadow
  1011. (
  1012. StaticAnimPhysClass & obj,
  1013. const Vector3 & light_dir,
  1014. TextureClass * render_target
  1015. )
  1016. {
  1017. /*
  1018. ** Get the definition for this object
  1019. */
  1020. StaticAnimPhysDefClass * def = obj.Get_StaticAnimPhysDef();
  1021. if (def == NULL) {
  1022. return;
  1023. }
  1024. int type_id = def->Get_ID();
  1025. Quaternion obj_orientation(1);
  1026. if (def->Shadow_Ignores_Z_Rotation() == false) {
  1027. obj_orientation = Build_Quaternion(obj.Get_Transform());
  1028. }
  1029. /*
  1030. ** Create the projector
  1031. */
  1032. PhysTexProjectClass * shadow_projector = NEW_REF(PhysTexProjectClass,());
  1033. shadow_projector->Set_Texture_Size(STATIC_PROJECTOR_RESOLUTION);
  1034. shadow_projector->Set_Intensity(def->Shadow_Intensity(),true);
  1035. if (def->Shadow_Is_Additive()) {
  1036. shadow_projector->Init_Additive();
  1037. } else {
  1038. shadow_projector->Init_Multiplicative();
  1039. /*
  1040. ** (gth) override the static shadow intensity with the global setting.
  1041. */
  1042. shadow_projector->Set_Intensity(Get_Shadow_Normal_Intensity(),true);
  1043. }
  1044. shadow_projector->Compute_Ortho_Projection(&obj,light_dir,def->Shadow_NearZ(),def->Shadow_FarZ());
  1045. shadow_projector->Enable_Attenuation(true);
  1046. shadow_projector->Enable_Depth_Gradient(false);
  1047. shadow_projector->Peek_Material_Pass()->Enable_On_Translucent_Meshes(false);
  1048. /*
  1049. ** See if we already have a suitable texture.
  1050. */
  1051. TextureClass * existing_texture = _StaticShadowTexMgr.Peek_Shadow_Texture(type_id,obj_orientation);
  1052. if (existing_texture != NULL) {
  1053. shadow_projector->Set_Texture(existing_texture);
  1054. }
  1055. /*
  1056. ** If no texture was a available, we have to generate it.
  1057. ** Then add it to the cache so others can use it
  1058. */
  1059. if (existing_texture == NULL) {
  1060. shadow_projector->Set_Render_Target(render_target);
  1061. shadow_projector->Compute_Texture(&obj,def->Shadow_Is_Additive());
  1062. SurfaceClass * surf = render_target->Get_Surface_Level();
  1063. SurfaceClass::SurfaceDescription desc;
  1064. surf->Get_Description(desc);
  1065. SurfaceClass * new_surf = NEW_REF(SurfaceClass,(desc.Width,desc.Height,desc.Format));
  1066. DX8Wrapper::_Copy_DX8_Rects(surf->Peek_D3D_Surface(),NULL,0,new_surf->Peek_D3D_Surface(),NULL);
  1067. TextureClass * new_texture = NEW_REF(TextureClass,(new_surf));
  1068. shadow_projector->Set_Render_Target(NULL);
  1069. shadow_projector->Set_Texture(new_texture);
  1070. REF_PTR_RELEASE(surf);
  1071. REF_PTR_RELEASE(new_surf);
  1072. REF_PTR_RELEASE(new_texture);
  1073. _StaticShadowTexMgr.Add_Shadow_Texture(type_id,obj_orientation,shadow_projector->Peek_Texture());
  1074. }
  1075. /*
  1076. ** Give the projector to the object
  1077. */
  1078. obj.Set_Shadow(shadow_projector);
  1079. /*
  1080. ** Release resources
  1081. */
  1082. REF_PTR_RELEASE(shadow_projector);
  1083. }