dx8renderer.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : ww3d *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/dx8renderer.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 2/28/02 5:35p $*
  31. * *
  32. * $Revision:: 111 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. //#define ENABLE_CATEGORY_LOG
  38. //#define ENABLE_STRIPING
  39. #include "dx8renderer.h"
  40. #include "dx8wrapper.h"
  41. #include "dx8polygonrenderer.h"
  42. #include "dx8vertexbuffer.h"
  43. #include "dx8indexbuffer.h"
  44. #include "dx8fvf.h"
  45. #include "dx8caps.h"
  46. #include "dx8rendererdebugger.h"
  47. #include "wwdebug.h"
  48. #include "wwprofile.h"
  49. #include "wwmemlog.h"
  50. #include "rinfo.h"
  51. #include "statistics.h"
  52. #include "meshmdl.h"
  53. #include "vp.h"
  54. #include "decalmsh.h"
  55. #include "matpass.h"
  56. #include "camera.h"
  57. #include "stripoptimizer.h"
  58. #include "meshgeometry.h"
  59. #include "hashtemplate.h"
  60. /*
  61. ** Global Instance of the DX8MeshRender
  62. */
  63. DX8MeshRendererClass TheDX8MeshRenderer;
  64. // ----------------------------------------------------------------------------
  65. static DynamicVectorClass<Vector3> _TempVertexBuffer;
  66. static DynamicVectorClass<Vector3> _TempNormalBuffer;
  67. static TextureCategoryList texture_category_delete_list;
  68. static FVFCategoryList fvf_category_container_delete_list;
  69. // helper data structure
  70. class PolyRemover : public MultiListObjectClass
  71. {
  72. public:
  73. DX8TextureCategoryClass * src;
  74. DX8TextureCategoryClass * dest;
  75. DX8PolygonRendererClass * pr;
  76. };
  77. typedef MultiListClass<PolyRemover> PolyRemoverList;
  78. typedef MultiListIterator<PolyRemover> PolyRemoverListIterator;
  79. /**
  80. ** PolyRenderTaskClass
  81. ** This is a record of a polyrendere that needs to be rendered
  82. ** for this frame. Since MeshClass instances can share meshmodels
  83. ** (and therefore their dx8 polygon renderers) this record contains
  84. ** a pointer to the polygon renderer and the MeshClass instance that
  85. ** it is being rendered for.
  86. */
  87. class PolyRenderTaskClass : public AutoPoolClass<PolyRenderTaskClass, 256>
  88. {
  89. public:
  90. PolyRenderTaskClass(DX8PolygonRendererClass * p_renderer,MeshClass * p_mesh) :
  91. Renderer(p_renderer),
  92. Mesh(p_mesh),
  93. NextVisible(NULL)
  94. {
  95. WWASSERT(Renderer != NULL);
  96. WWASSERT(Mesh != NULL);
  97. Mesh->Add_Ref();
  98. }
  99. ~PolyRenderTaskClass(void)
  100. {
  101. Mesh->Release_Ref();
  102. }
  103. DX8PolygonRendererClass * Peek_Polygon_Renderer(void) { return Renderer; }
  104. MeshClass * Peek_Mesh(void) { return Mesh; }
  105. PolyRenderTaskClass * Get_Next_Visible(void) { return NextVisible; }
  106. void Set_Next_Visible(PolyRenderTaskClass * prtc) { NextVisible = prtc; }
  107. protected:
  108. DX8PolygonRendererClass * Renderer;
  109. MeshClass * Mesh;
  110. PolyRenderTaskClass * NextVisible;
  111. };
  112. DEFINE_AUTO_POOL(PolyRenderTaskClass, 256);
  113. /**
  114. ** MatPassTaskClass
  115. ** This is the record of a material pass that needs to be rendered on
  116. ** a particular mesh. These are linked into the FVF container which
  117. ** contains the mesh model. They are also pooled to remove memory
  118. ** allocation overhead.
  119. */
  120. class MatPassTaskClass : public AutoPoolClass<MatPassTaskClass, 256>
  121. {
  122. public:
  123. MatPassTaskClass(MaterialPassClass * pass,MeshClass * mesh) :
  124. MaterialPass(pass),
  125. Mesh(mesh),
  126. NextVisible(NULL)
  127. {
  128. WWASSERT(MaterialPass != NULL);
  129. WWASSERT(Mesh != NULL);
  130. MaterialPass->Add_Ref();
  131. Mesh->Add_Ref();
  132. }
  133. ~MatPassTaskClass(void)
  134. {
  135. MaterialPass->Release_Ref();
  136. Mesh->Release_Ref();
  137. }
  138. MaterialPassClass * Peek_Material_Pass(void) { return MaterialPass; }
  139. MeshClass * Peek_Mesh(void) { return Mesh; }
  140. MatPassTaskClass * Get_Next_Visible(void) { return NextVisible; }
  141. void Set_Next_Visible(MatPassTaskClass * mpr) { NextVisible = mpr; }
  142. private:
  143. MaterialPassClass * MaterialPass;
  144. MeshClass * Mesh;
  145. MatPassTaskClass * NextVisible;
  146. };
  147. DEFINE_AUTO_POOL(MatPassTaskClass, 256);
  148. // ----------------------------------------------------------------------------
  149. inline static bool Equal_Material(const VertexMaterialClass* mat1,const VertexMaterialClass* mat2)
  150. {
  151. int crc0 = mat1 ? mat1->Get_CRC() : 0;
  152. int crc1 = mat2 ? mat2->Get_CRC() : 0;
  153. return (crc0 == crc1);
  154. }
  155. DX8TextureCategoryClass::DX8TextureCategoryClass(
  156. DX8FVFCategoryContainer* container_,
  157. TextureClass** texs,
  158. ShaderClass shd,
  159. VertexMaterialClass* mat,
  160. int pass_)
  161. :
  162. pass(pass_),
  163. shader(shd),
  164. render_task_head(NULL),
  165. material(mat),
  166. container(container_)
  167. {
  168. WWASSERT(pass>=0);
  169. WWASSERT(pass<DX8FVFCategoryContainer::MAX_PASSES);
  170. for (int a=0;a<MAX_TEXTURE_STAGES;++a) {
  171. textures[a]=NULL;
  172. REF_PTR_SET(textures[a],texs[a]);
  173. }
  174. if (material) material->Add_Ref();
  175. }
  176. DX8TextureCategoryClass::~DX8TextureCategoryClass()
  177. {
  178. // Unregistering the mesh where polygon renderers are connected to kills all polygon renderers
  179. while (DX8PolygonRendererClass* p_renderer=PolygonRendererList.Get_Head()) {
  180. TheDX8MeshRenderer.Unregister_Mesh_Type(p_renderer->Get_Mesh_Class());
  181. }
  182. for (int a=0;a<MAX_TEXTURE_STAGES;++a) {
  183. REF_PTR_RELEASE(textures[a]);
  184. }
  185. REF_PTR_RELEASE(material);
  186. }
  187. void DX8TextureCategoryClass::Add_Render_Task(DX8PolygonRendererClass * p_renderer,MeshClass * p_mesh)
  188. {
  189. PolyRenderTaskClass * new_prt = new PolyRenderTaskClass(p_renderer,p_mesh);
  190. new_prt->Set_Next_Visible(render_task_head);
  191. render_task_head = new_prt;
  192. container->Add_Visible_Texture_Category(this,pass);
  193. }
  194. void DX8TextureCategoryClass::Add_Polygon_Renderer(DX8PolygonRendererClass* p_renderer,DX8PolygonRendererClass* add_after_this)
  195. {
  196. WWASSERT(p_renderer!=NULL);
  197. WWASSERT(!PolygonRendererList.Contains(p_renderer));
  198. if (add_after_this != NULL) {
  199. bool res = PolygonRendererList.Add_After(p_renderer,add_after_this,false);
  200. WWASSERT(res != NULL);
  201. } else {
  202. PolygonRendererList.Add(p_renderer);
  203. }
  204. p_renderer->Set_Texture_Category(this);
  205. }
  206. void DX8TextureCategoryClass::Remove_Polygon_Renderer(DX8PolygonRendererClass* p_renderer)
  207. {
  208. PolygonRendererList.Remove(p_renderer);
  209. p_renderer->Set_Texture_Category(NULL);
  210. if (PolygonRendererList.Peek_Head() == NULL) {
  211. container->Remove_Texture_Category(this);
  212. texture_category_delete_list.Add_Tail(this);
  213. }
  214. }
  215. void DX8FVFCategoryContainer::Remove_Texture_Category(DX8TextureCategoryClass* tex_category)
  216. {
  217. for (unsigned pass=0;pass<passes;++pass) {
  218. texture_category_list[pass].Remove(tex_category);
  219. }
  220. for (pass=0; pass<passes; pass++) {
  221. // If any of the texture category lists has anything in it, no need to delete this container
  222. if (texture_category_list[pass].Peek_Head() != NULL) return;
  223. }
  224. fvf_category_container_delete_list.Add_Tail(this);
  225. }
  226. void DX8FVFCategoryContainer::Add_Visible_Material_Pass(MaterialPassClass * pass,MeshClass * mesh)
  227. {
  228. MatPassTaskClass * new_mpr = new MatPassTaskClass(pass,mesh);
  229. if (visible_matpass_head == NULL) {
  230. WWASSERT(visible_matpass_tail == NULL);
  231. visible_matpass_head = new_mpr;
  232. } else {
  233. WWASSERT(visible_matpass_tail != NULL);
  234. visible_matpass_tail->Set_Next_Visible(new_mpr);
  235. }
  236. visible_matpass_tail = new_mpr;
  237. AnythingToRender=true;
  238. }
  239. void DX8FVFCategoryContainer::Render_Procedural_Material_Passes(void)
  240. {
  241. // additional passes
  242. MatPassTaskClass * mpr = visible_matpass_head;
  243. while (mpr != NULL) {
  244. mpr->Peek_Mesh()->Render_Material_Pass(mpr->Peek_Material_Pass(),index_buffer);
  245. MatPassTaskClass * next_mpr = mpr->Get_Next_Visible();
  246. delete mpr;
  247. mpr = next_mpr;
  248. }
  249. visible_matpass_head = visible_matpass_tail = NULL;
  250. }
  251. void DX8RigidFVFCategoryContainer::Add_Delayed_Visible_Material_Pass(MaterialPassClass * pass, MeshClass * mesh)
  252. {
  253. MatPassTaskClass * new_mpr = new MatPassTaskClass(pass,mesh);
  254. if (delayed_matpass_head == NULL) {
  255. WWASSERT(delayed_matpass_tail == NULL);
  256. delayed_matpass_head = new_mpr;
  257. } else {
  258. WWASSERT(delayed_matpass_tail != NULL);
  259. delayed_matpass_tail->Set_Next_Visible(new_mpr);
  260. }
  261. delayed_matpass_tail = new_mpr;
  262. AnyDelayedPassesToRender=true;
  263. }
  264. void DX8RigidFVFCategoryContainer::Render_Delayed_Procedural_Material_Passes(void)
  265. {
  266. if (!Any_Delayed_Passes_To_Render()) return;
  267. AnyDelayedPassesToRender=false;
  268. DX8Wrapper::Set_Vertex_Buffer(vertex_buffer);
  269. DX8Wrapper::Set_Index_Buffer(index_buffer,0);
  270. SNAPSHOT_SAY(("DX8RigidFVFCategoryContainer::Render_Delayed_Procedural_Material_Passes()\n"));
  271. // additional passes
  272. MatPassTaskClass * mpr = delayed_matpass_head;
  273. while (mpr != NULL) {
  274. mpr->Peek_Mesh()->Render_Material_Pass(mpr->Peek_Material_Pass(),index_buffer);
  275. MatPassTaskClass * next_mpr = mpr->Get_Next_Visible();
  276. delete mpr;
  277. mpr = next_mpr;
  278. }
  279. delayed_matpass_head = delayed_matpass_tail = NULL;
  280. }
  281. void DX8TextureCategoryClass::Log(bool only_visible)
  282. {
  283. #ifdef ENABLE_CATEGORY_LOG
  284. StringClass work(255,true);
  285. work.Format(" DX8TextureCategoryClass\n");
  286. WWDEBUG_SAY((work));
  287. StringClass work2(255,true);
  288. for (int stage=0;stage<MAX_TEXTURE_STAGES;++stage) {
  289. work2.Format(" texture[%d]: %x (%s)\n", stage, textures[stage], textures[stage] ? textures[stage]->Get_Name() : "-");
  290. work+=work2;
  291. }
  292. work2.Format(" material: %x (%s)\n shader: %x\n", material, material ? material->Get_Name() : "-", shader);
  293. work+=work2;
  294. WWDEBUG_SAY((work));
  295. work.Format(" %8s %8s %6s %6s %6s %5s %s\n",
  296. "idx_cnt",
  297. "poly_cnt",
  298. "i_offs",
  299. "min_vi",
  300. "vi_rng",
  301. "ident",
  302. "name");
  303. WWDEBUG_SAY((work));
  304. DX8PolygonRendererListIterator it(&PolygonRendererList);
  305. while (!it.Is_Done()) {
  306. DX8PolygonRendererClass* p_renderer = it.Peek_Obj();
  307. PolyRenderTaskClass * prtc=render_task_head;
  308. while (prtc) {
  309. if (prtc->Peek_Polygon_Renderer()==p_renderer) break;
  310. prtc = prtc->Get_Next_Visible();
  311. }
  312. if (prtc != NULL) {
  313. WWDEBUG_SAY(("+"));
  314. p_renderer->Log();
  315. } else {
  316. if (!only_visible) {
  317. WWDEBUG_SAY(("-"));
  318. p_renderer->Log();
  319. }
  320. }
  321. it.Next();
  322. }
  323. #endif
  324. }
  325. // ----------------------------------------------------------------------------
  326. DX8FVFCategoryContainer::DX8FVFCategoryContainer(unsigned FVF_,bool sorting_)
  327. :
  328. FVF(FVF_),
  329. sorting(sorting_),
  330. visible_matpass_head(NULL),
  331. visible_matpass_tail(NULL),
  332. index_buffer(0),
  333. used_indices(0),
  334. passes(MAX_PASSES),
  335. uv_coordinate_channels(0),
  336. AnythingToRender(false),
  337. AnyDelayedPassesToRender(false)
  338. {
  339. if ((FVF&D3DFVF_TEX1)==D3DFVF_TEX1) uv_coordinate_channels=1;
  340. if ((FVF&D3DFVF_TEX2)==D3DFVF_TEX2) uv_coordinate_channels=2;
  341. if ((FVF&D3DFVF_TEX3)==D3DFVF_TEX3) uv_coordinate_channels=3;
  342. if ((FVF&D3DFVF_TEX4)==D3DFVF_TEX4) uv_coordinate_channels=4;
  343. if ((FVF&D3DFVF_TEX5)==D3DFVF_TEX5) uv_coordinate_channels=5;
  344. if ((FVF&D3DFVF_TEX6)==D3DFVF_TEX6) uv_coordinate_channels=6;
  345. if ((FVF&D3DFVF_TEX7)==D3DFVF_TEX7) uv_coordinate_channels=7;
  346. if ((FVF&D3DFVF_TEX8)==D3DFVF_TEX8) uv_coordinate_channels=8;
  347. }
  348. // ----------------------------------------------------------------------------
  349. DX8FVFCategoryContainer::~DX8FVFCategoryContainer()
  350. {
  351. REF_PTR_RELEASE(index_buffer);
  352. for (unsigned p=0;p<passes;++p) {
  353. while (DX8TextureCategoryClass * tex = texture_category_list[p].Remove_Head()) {
  354. delete tex;
  355. }
  356. }
  357. }
  358. // ----------------------------------------------------------------------------
  359. DX8TextureCategoryClass* DX8FVFCategoryContainer::Find_Matching_Texture_Category(
  360. TextureClass* texture,
  361. unsigned pass,
  362. unsigned stage,
  363. DX8TextureCategoryClass* ref_category)
  364. {
  365. // Find texture category which matches ref_category's properties but has 'texture' on given pass and stage.
  366. DX8TextureCategoryClass* dest_tex_category=NULL;
  367. TextureCategoryListIterator dest_it(&texture_category_list[pass]);
  368. while (!dest_it.Is_Done()) {
  369. if (dest_it.Peek_Obj()->Peek_Texture(stage)==texture) {
  370. // Compare all stage's textures
  371. dest_tex_category=dest_it.Peek_Obj();
  372. bool all_textures_same = true;
  373. for (unsigned int s = 0; s < MeshMatDescClass::MAX_TEX_STAGES; s++) {
  374. if (stage!=s) {
  375. all_textures_same = all_textures_same && (dest_tex_category->Peek_Texture(s) == ref_category->Peek_Texture(s));
  376. }
  377. }
  378. if (all_textures_same &&
  379. Equal_Material(dest_tex_category->Peek_Material(),ref_category->Peek_Material()) &&
  380. dest_tex_category->Get_Shader()==ref_category->Get_Shader()) {
  381. return dest_tex_category;
  382. }
  383. }
  384. dest_it.Next();
  385. }
  386. return NULL;
  387. }
  388. DX8TextureCategoryClass* DX8FVFCategoryContainer::Find_Matching_Texture_Category(
  389. VertexMaterialClass* vmat,
  390. unsigned pass,
  391. DX8TextureCategoryClass* ref_category)
  392. {
  393. // Find texture category which matches ref_category's properties but has 'vmat' on given pass
  394. DX8TextureCategoryClass* dest_tex_category=NULL;
  395. TextureCategoryListIterator dest_it(&texture_category_list[pass]);
  396. while (!dest_it.Is_Done()) {
  397. if (Equal_Material(dest_it.Peek_Obj()->Peek_Material(),vmat)) {
  398. // Compare all stage's textures
  399. dest_tex_category=dest_it.Peek_Obj();
  400. bool all_textures_same = true;
  401. for (unsigned int s = 0; s < MeshMatDescClass::MAX_TEX_STAGES; s++)
  402. all_textures_same = all_textures_same && (dest_tex_category->Peek_Texture(s) == ref_category->Peek_Texture(s));
  403. if (all_textures_same &&
  404. dest_tex_category->Get_Shader()==ref_category->Get_Shader()) {
  405. return dest_tex_category;
  406. }
  407. }
  408. dest_it.Next();
  409. }
  410. return NULL;
  411. }
  412. void DX8FVFCategoryContainer::Change_Polygon_Renderer_Texture(
  413. DX8PolygonRendererList& polygon_renderer_list,
  414. TextureClass* texture,
  415. TextureClass* new_texture,
  416. unsigned pass,
  417. unsigned stage)
  418. {
  419. WWASSERT(pass<passes);
  420. PolyRemoverList prl;
  421. bool foundtexture=false;
  422. if (texture==new_texture) return;
  423. // Find source texture category, then find all polygon renderers who belong to that category
  424. // and move them to destination category.
  425. TextureCategoryListIterator src_it(&texture_category_list[pass]);
  426. while (!src_it.Is_Done()) {
  427. DX8TextureCategoryClass* src_tex_category=src_it.Peek_Obj();
  428. if (src_tex_category->Peek_Texture(stage)==texture) {
  429. foundtexture=true;
  430. DX8PolygonRendererListIterator poly_it(&polygon_renderer_list);
  431. while (!poly_it.Is_Done()) {
  432. // If source texture category contains polygon renderer, move to destination category
  433. DX8PolygonRendererClass* polygon_renderer=poly_it.Peek_Obj();
  434. DX8TextureCategoryClass *prc=polygon_renderer->Get_Texture_Category();
  435. if (prc==src_tex_category) {
  436. DX8TextureCategoryClass* dest_tex_category=Find_Matching_Texture_Category(new_texture,pass,stage,src_tex_category);
  437. if (!dest_tex_category) {
  438. TextureClass * tmp_textures[MeshMatDescClass::MAX_TEX_STAGES];
  439. for (int s=0;s<MeshMatDescClass::MAX_TEX_STAGES;++s) {
  440. tmp_textures[s]=src_tex_category->Peek_Texture(s);
  441. }
  442. tmp_textures[stage]=new_texture;
  443. DX8TextureCategoryClass * new_tex_category=new DX8TextureCategoryClass(
  444. this,
  445. tmp_textures,
  446. src_tex_category->Get_Shader(),
  447. const_cast<VertexMaterialClass*>(src_tex_category->Peek_Material()),
  448. pass);
  449. /*
  450. ** Add the texture category object into the list, immediately after any existing
  451. ** texture category object which uses the same texture. This will result in
  452. ** the list always having matching texture categories next to each other.
  453. */
  454. bool found_similar_category = false;
  455. TextureCategoryListIterator tex_it(&texture_category_list[pass]);
  456. while (!tex_it.Is_Done()) {
  457. // Categorize according to first stage's texture for now
  458. if (tex_it.Peek_Obj()->Peek_Texture(0) == tmp_textures[0]) {
  459. texture_category_list[pass].Add_After(new_tex_category,tex_it.Peek_Obj());
  460. found_similar_category = true;
  461. break;
  462. }
  463. tex_it.Next();
  464. }
  465. if (!found_similar_category) {
  466. texture_category_list[pass].Add_Tail(new_tex_category);
  467. }
  468. dest_tex_category=new_tex_category;
  469. }
  470. PolyRemover *rem=new PolyRemover;
  471. rem->src=src_tex_category;
  472. rem->dest=dest_tex_category;
  473. rem->pr=polygon_renderer;
  474. prl.Add(rem);
  475. }
  476. poly_it.Next();
  477. } // while
  478. } //if src_texture==texture
  479. else
  480. // quit loop if we've got a texture change
  481. if (foundtexture) break;
  482. src_it.Next();
  483. } // while
  484. PolyRemoverListIterator prli(&prl);
  485. while (!prli.Is_Done())
  486. {
  487. PolyRemover *rem=prli.Peek_Obj();
  488. rem->src->Remove_Polygon_Renderer(rem->pr);
  489. rem->dest->Add_Polygon_Renderer(rem->pr);
  490. prli.Remove_Current_Object();
  491. delete rem;
  492. }
  493. }
  494. void DX8FVFCategoryContainer::Change_Polygon_Renderer_Material(
  495. DX8PolygonRendererList& polygon_renderer_list,
  496. VertexMaterialClass* vmat,
  497. VertexMaterialClass* new_vmat,
  498. unsigned pass)
  499. {
  500. WWASSERT(pass<passes);
  501. PolyRemoverList prl;
  502. bool foundtexture=false;
  503. if (vmat==new_vmat) return;
  504. // Find source texture category, then find all polygon renderers who belong to that category
  505. // and move them to destination category.
  506. TextureCategoryListIterator src_it(&texture_category_list[pass]);
  507. while (!src_it.Is_Done()) {
  508. DX8TextureCategoryClass* src_tex_category=src_it.Peek_Obj();
  509. if (src_tex_category->Peek_Material()==vmat) {
  510. DX8PolygonRendererListIterator poly_it(&polygon_renderer_list);
  511. while (!poly_it.Is_Done()) {
  512. // If source texture category contains polygon renderer, move to destination category
  513. DX8PolygonRendererClass* polygon_renderer=poly_it.Peek_Obj();
  514. DX8TextureCategoryClass *prc=polygon_renderer->Get_Texture_Category();
  515. if (prc==src_tex_category) {
  516. foundtexture=true;
  517. DX8TextureCategoryClass* dest_tex_category=Find_Matching_Texture_Category(new_vmat,pass,src_tex_category);
  518. if (!dest_tex_category) {
  519. TextureClass * tmp_textures[MeshMatDescClass::MAX_TEX_STAGES];
  520. for (int s=0;s<MeshMatDescClass::MAX_TEX_STAGES;++s) {
  521. tmp_textures[s]=src_tex_category->Peek_Texture(s);
  522. }
  523. DX8TextureCategoryClass * new_tex_category=new DX8TextureCategoryClass(
  524. this,
  525. tmp_textures,
  526. src_tex_category->Get_Shader(),
  527. const_cast<VertexMaterialClass*>(new_vmat),
  528. pass);
  529. /*
  530. ** Add the texture category object into the list, immediately after any existing
  531. ** texture category object which uses the same texture. This will result in
  532. ** the list always having matching texture categories next to each other.
  533. */
  534. bool found_similar_category = false;
  535. TextureCategoryListIterator tex_it(&texture_category_list[pass]);
  536. while (!tex_it.Is_Done()) {
  537. // Categorize according to first stage's texture for now
  538. if (tex_it.Peek_Obj()->Peek_Texture(0) == tmp_textures[0]) {
  539. texture_category_list[pass].Add_After(new_tex_category,tex_it.Peek_Obj());
  540. found_similar_category = true;
  541. break;
  542. }
  543. tex_it.Next();
  544. }
  545. if (!found_similar_category) {
  546. texture_category_list[pass].Add_Tail(new_tex_category);
  547. }
  548. dest_tex_category=new_tex_category;
  549. }
  550. PolyRemover *rem=new PolyRemover;
  551. rem->src=src_tex_category;
  552. rem->dest=dest_tex_category;
  553. rem->pr=polygon_renderer;
  554. prl.Add(rem);
  555. }
  556. poly_it.Next();
  557. } // while
  558. } // if
  559. else
  560. if (foundtexture) break;
  561. src_it.Next();
  562. } // while
  563. PolyRemoverListIterator prli(&prl);
  564. while (!prli.Is_Done())
  565. {
  566. PolyRemover *rem=prli.Peek_Obj();
  567. rem->src->Remove_Polygon_Renderer(rem->pr);
  568. rem->dest->Add_Polygon_Renderer(rem->pr);
  569. prli.Remove_Current_Object();
  570. delete rem;
  571. }
  572. }
  573. // ----------------------------------------------------------------------------
  574. unsigned DX8FVFCategoryContainer::Define_FVF(MeshModelClass* mmc,unsigned int * user_lighting,bool enable_lighting)
  575. {
  576. if ((!!mmc->Get_Flag(MeshGeometryClass::SORT)) && WW3D::Is_Sorting_Enabled()) {
  577. return dynamic_fvf_type;
  578. }
  579. unsigned fvf=D3DFVF_XYZ;
  580. int tex_coord_count=mmc->Get_UV_Array_Count();
  581. if (mmc->Get_Color_Array(0,false) || (user_lighting != NULL)) {
  582. fvf|=D3DFVF_DIFFUSE;
  583. }
  584. if (mmc->Get_Color_Array(1,false)) {
  585. fvf|=D3DFVF_SPECULAR;
  586. }
  587. switch (tex_coord_count) {
  588. default:
  589. case 0:
  590. break;
  591. case 1: fvf|=D3DFVF_TEX1; break;
  592. case 2: fvf|=D3DFVF_TEX2; break;
  593. case 3: fvf|=D3DFVF_TEX3; break;
  594. case 4: fvf|=D3DFVF_TEX4; break;
  595. case 5: fvf|=D3DFVF_TEX5; break;
  596. case 6: fvf|=D3DFVF_TEX6; break;
  597. case 7: fvf|=D3DFVF_TEX7; break;
  598. case 8: fvf|=D3DFVF_TEX8; break;
  599. }
  600. if (!mmc->Needs_Vertex_Normals()) { //enable_lighting || mmc->Get_Flag(MeshModelClass::PRELIT_MASK)) {
  601. return fvf;
  602. }
  603. fvf|=D3DFVF_NORMAL; // Realtime-lit
  604. return fvf;
  605. }
  606. // ----------------------------------------------------------------------------
  607. DX8RigidFVFCategoryContainer::DX8RigidFVFCategoryContainer(unsigned FVF,bool sorting_)
  608. :
  609. DX8FVFCategoryContainer(FVF,sorting_),
  610. vertex_buffer(0),
  611. used_vertices(0),
  612. delayed_matpass_head(NULL),
  613. delayed_matpass_tail(NULL)
  614. {
  615. }
  616. // ----------------------------------------------------------------------------
  617. DX8RigidFVFCategoryContainer::~DX8RigidFVFCategoryContainer()
  618. {
  619. REF_PTR_RELEASE(vertex_buffer);
  620. }
  621. // ----------------------------------------------------------------------------
  622. void DX8RigidFVFCategoryContainer::Log(bool only_visible)
  623. {
  624. #ifdef ENABLE_CATEGORY_LOG
  625. StringClass work(255,true);
  626. work.Format("DX8RigidFVFCategoryContainer --------------\n");
  627. WWDEBUG_SAY((work));
  628. if (vertex_buffer) {
  629. StringClass fvfname(255,true);
  630. vertex_buffer->FVF_Info().Get_FVF_Name(fvfname);
  631. work.Format("VB size (used/total): %d/%d FVF: %s\n",used_vertices,vertex_buffer->Get_Vertex_Count(),fvfname);
  632. WWDEBUG_SAY((work));
  633. }
  634. else {
  635. WWDEBUG_SAY(("EMPTY VB\n"));
  636. }
  637. if (index_buffer) {
  638. work.Format("IB size (used/total): %d/%d\n",used_indices,index_buffer->Get_Index_Count());
  639. WWDEBUG_SAY((work));
  640. }
  641. else {
  642. WWDEBUG_SAY(("EMPTY IB\n"));
  643. }
  644. for (unsigned p=0;p<passes;++p) {
  645. WWDEBUG_SAY(("Pass: %d\n",p));
  646. TextureCategoryListIterator it(&texture_category_list[p]);
  647. while (!it.Is_Done()) {
  648. it.Peek_Obj()->Log(only_visible);
  649. it.Next();
  650. }
  651. }
  652. #endif
  653. }
  654. // ----------------------------------------------------------------------------
  655. //
  656. // Generic render function for rigid meshes
  657. //
  658. // ----------------------------------------------------------------------------
  659. void DX8RigidFVFCategoryContainer::Render(void)
  660. {
  661. if (!Anything_To_Render()) return;
  662. AnythingToRender=false;
  663. DX8Wrapper::Set_Vertex_Buffer(vertex_buffer);
  664. DX8Wrapper::Set_Index_Buffer(index_buffer,0);
  665. SNAPSHOT_SAY(("DX8RigidFVFCategoryContainer::Render()\n"));
  666. int zbias=0;
  667. DX8Wrapper::Set_DX8_ZBias(zbias);
  668. for (unsigned p=0;p<passes;++p) {
  669. SNAPSHOT_SAY(("Pass: %d\n",p));
  670. while (DX8TextureCategoryClass * tex = visible_texture_category_list[p].Remove_Head()) {
  671. tex->Render();
  672. }
  673. zbias++;
  674. if (zbias>15) zbias=15;
  675. DX8Wrapper::Set_DX8_ZBias(zbias);
  676. }
  677. Render_Procedural_Material_Passes();
  678. DX8Wrapper::Set_DX8_ZBias(0);
  679. }
  680. // ----------------------------------------------------------------------------
  681. bool DX8RigidFVFCategoryContainer::Check_If_Mesh_Fits(MeshModelClass* mmc)
  682. {
  683. if (!vertex_buffer) return true; // No VB created - mesh will fit as a new vb will be created when inserting
  684. unsigned required_vertices=mmc->Get_Vertex_Count();
  685. unsigned available_vertices=vertex_buffer->Get_Vertex_Count()-used_vertices;
  686. unsigned required_polygons=mmc->Get_Polygon_Count();
  687. if (mmc->Get_Gap_Filler()) {
  688. required_polygons+=mmc->Get_Gap_Filler()->Get_Polygon_Count();
  689. }
  690. unsigned required_indices=required_polygons*3*mmc->Get_Pass_Count();
  691. unsigned available_indices=index_buffer->Get_Index_Count()-used_indices;
  692. if (
  693. required_vertices<=available_vertices &&
  694. (required_indices)<=available_indices) {
  695. return true;
  696. }
  697. return false;
  698. }
  699. // ----------------------------------------------------------------------------
  700. class Vertex_Split_Table
  701. {
  702. MeshClass* mesh;
  703. MeshModelClass* mmc;
  704. bool npatch_enable;
  705. unsigned polygon_count;
  706. TriIndex* polygon_array;
  707. bool allocated_polygon_array;
  708. public:
  709. Vertex_Split_Table(MeshClass* mesh_)
  710. :
  711. mesh(mesh_),
  712. mmc(NULL),
  713. npatch_enable(false),
  714. allocated_polygon_array(false)
  715. {
  716. mmc = mesh_->Peek_Model();
  717. if (DX8Wrapper::Get_Current_Caps()->Support_NPatches() && mmc->Needs_Vertex_Normals()) {
  718. if (mmc->Get_Flag(MeshGeometryClass::ALLOW_NPATCHES)) {
  719. npatch_enable=true;
  720. }
  721. }
  722. const GapFillerClass* gap_filler=mmc->Get_Gap_Filler();
  723. polygon_count=mmc->Get_Polygon_Count();
  724. if (gap_filler) polygon_count+=gap_filler->Get_Polygon_Count();
  725. // if (mmc->Get_Gap_Filler_Polygon_Count()) {
  726. allocated_polygon_array=true;
  727. polygon_array=new TriIndex[polygon_count];
  728. memcpy(
  729. polygon_array,
  730. mmc->Get_Polygon_Array(),
  731. mmc->Get_Polygon_Count()*sizeof(TriIndex));
  732. if (gap_filler) {
  733. memcpy(
  734. polygon_array+mmc->Get_Polygon_Count(),
  735. gap_filler->Get_Polygon_Array(),
  736. gap_filler->Get_Polygon_Count()*sizeof(TriIndex));
  737. }
  738. // }
  739. // else {
  740. // polygon_array=const_cast<Vector3i*>(mmc->Get_Polygon_Array());
  741. // }
  742. }
  743. ~Vertex_Split_Table()
  744. {
  745. if (allocated_polygon_array) {
  746. delete[] polygon_array;
  747. }
  748. }
  749. const Vector3* Get_Vertex_Array() const
  750. {
  751. return mmc->Get_Vertex_Array();
  752. }
  753. const Vector3* Get_Vertex_Normal_Array() const
  754. {
  755. return mmc->Get_Vertex_Normal_Array();
  756. }
  757. const unsigned* Get_Color_Array(unsigned index) const
  758. {
  759. if ((index == 0) && (mesh->Get_User_Lighting_Array() != NULL)) {
  760. return mesh->Get_User_Lighting_Array();
  761. }
  762. return mmc->Get_Color_Array(index,false);
  763. }
  764. const Vector2* Get_UV_Array(unsigned uv_array_index) const
  765. {
  766. return mmc->Get_UV_Array_By_Index(uv_array_index);
  767. }
  768. unsigned Get_Vertex_Count() const
  769. {
  770. return mmc->Get_Vertex_Count();
  771. }
  772. unsigned Get_Polygon_Count() const
  773. {
  774. return polygon_count;
  775. }
  776. unsigned Get_Pass_Count() const
  777. {
  778. return mmc->Get_Pass_Count();
  779. }
  780. TextureClass* Peek_Texture(unsigned idx,unsigned pass,unsigned stage)
  781. {
  782. if (mmc->Has_Texture_Array(pass,stage)) {
  783. if (idx>=unsigned(mmc->Get_Polygon_Count())) {
  784. WWASSERT(mmc->Get_Gap_Filler());
  785. return mmc->Get_Gap_Filler()->Get_Texture_Array(pass,stage)[idx-mmc->Get_Polygon_Count()];
  786. }
  787. return mmc->Peek_Texture(idx,pass,stage);
  788. }
  789. return mmc->Peek_Single_Texture(pass,stage);
  790. }
  791. VertexMaterialClass* Peek_Material(unsigned idx,unsigned pass)
  792. {
  793. if (mmc->Has_Material_Array(pass)) {
  794. if (idx>=unsigned(mmc->Get_Polygon_Count())) {
  795. WWASSERT(mmc->Get_Gap_Filler());
  796. return mmc->Get_Gap_Filler()->Get_Material_Array(pass)[idx-mmc->Get_Polygon_Count()];
  797. }
  798. return mmc->Peek_Material(mmc->Get_Polygon_Array()[idx][0],pass);
  799. }
  800. return mmc->Peek_Single_Material(pass);
  801. }
  802. ShaderClass Peek_Shader(unsigned idx,unsigned pass)
  803. {
  804. if (mmc->Has_Shader_Array(pass)) {
  805. ShaderClass shader;
  806. if (idx>=unsigned(mmc->Get_Polygon_Count())) {
  807. WWASSERT(mmc->Get_Gap_Filler());
  808. shader=mmc->Get_Gap_Filler()->Get_Shader_Array(pass)[idx-mmc->Get_Polygon_Count()];
  809. }
  810. else shader=mmc->Get_Shader(idx,pass);
  811. if (npatch_enable) {
  812. shader.Set_NPatch_Enable(ShaderClass::NPATCH_ENABLE);
  813. }
  814. return shader;
  815. }
  816. if (!npatch_enable) return mmc->Get_Single_Shader(pass);
  817. ShaderClass shader=mmc->Get_Single_Shader(pass);
  818. shader.Set_NPatch_Enable(ShaderClass::NPATCH_ENABLE);
  819. return shader;
  820. }
  821. MeshModelClass* Get_Mesh_Model_Class()
  822. {
  823. return mmc;
  824. }
  825. MeshClass * Get_Mesh_Class()
  826. {
  827. return mesh;
  828. }
  829. unsigned short* Get_Polygon_Array(unsigned pass)
  830. {
  831. return (unsigned short*)polygon_array;
  832. }
  833. };
  834. // ----------------------------------------------------------------------------
  835. void DX8RigidFVFCategoryContainer::Add_Mesh(MeshClass* mesh_)
  836. {
  837. MeshModelClass * mmc_ = mesh_->Peek_Model();
  838. WWASSERT(Check_If_Mesh_Fits(mmc_));
  839. Vertex_Split_Table split_table(mesh_);
  840. int needed_vertices=split_table.Get_Vertex_Count();
  841. /*
  842. ** This FVFCategoryContainer doesn't have a vertex buffer yet so allocate one big
  843. ** enough to contain this mesh.
  844. */
  845. if (!vertex_buffer) {
  846. int vb_size=4000;
  847. if (vb_size<needed_vertices) vb_size=needed_vertices;
  848. if (sorting) {
  849. vertex_buffer=NEW_REF(SortingVertexBufferClass,(vb_size));
  850. WWASSERT(vertex_buffer->FVF_Info().Get_FVF()==FVF); // Only one sorting FVF type!
  851. }
  852. else {
  853. vertex_buffer=NEW_REF(DX8VertexBufferClass,(
  854. FVF,
  855. vb_size,
  856. (DX8Wrapper::Get_Current_Caps()->Support_NPatches() && WW3D::Get_NPatches_Level()>1) ? DX8VertexBufferClass::USAGE_NPATCHES : DX8VertexBufferClass::USAGE_DEFAULT));
  857. }
  858. }
  859. /*
  860. ** Append this mesh's vertices to the vertex buffer.
  861. */
  862. VertexBufferClass::AppendLockClass l(vertex_buffer,used_vertices,split_table.Get_Vertex_Count());
  863. const FVFInfoClass fi=vertex_buffer->FVF_Info();
  864. unsigned char *vb=(unsigned char*) l.Get_Vertex_Array();
  865. unsigned int i;
  866. const Vector3 *locs=split_table.Get_Vertex_Array();
  867. const Vector3 *norms=split_table.Get_Vertex_Normal_Array();
  868. const unsigned *diffuse=split_table.Get_Color_Array(0);
  869. const unsigned *specular=split_table.Get_Color_Array(1);
  870. for (i=0; i<split_table.Get_Vertex_Count(); i++)
  871. {
  872. *(Vector3*)(vb+fi.Get_Location_Offset())=locs[i];
  873. if ((FVF&D3DFVF_NORMAL)==D3DFVF_NORMAL && norms) {
  874. *(Vector3*)(vb+fi.Get_Normal_Offset())=norms[i];
  875. }
  876. if ((FVF&D3DFVF_DIFFUSE)==D3DFVF_DIFFUSE) {
  877. if (diffuse) {
  878. *(unsigned int*)(vb+fi.Get_Diffuse_Offset())=diffuse[i];
  879. } else {
  880. *(unsigned int*)(vb+fi.Get_Diffuse_Offset()) = 0xFFFFFFFF;
  881. }
  882. }
  883. if ((FVF&D3DFVF_SPECULAR)==D3DFVF_SPECULAR) {
  884. if (specular) {
  885. *(unsigned int*)(vb+fi.Get_Specular_Offset())=specular[i];
  886. } else {
  887. *(unsigned int*)(vb+fi.Get_Specular_Offset()) = 0xFFFFFFFF;
  888. }
  889. }
  890. vb+=fi.Get_FVF_Size();
  891. }
  892. /*
  893. ** Append the UV coordinates to the vertex buffer
  894. */
  895. int uvcount = 0;
  896. if ((FVF&D3DFVF_TEX1) == D3DFVF_TEX1) {
  897. uvcount = 1;
  898. }
  899. if ((FVF&D3DFVF_TEX2) == D3DFVF_TEX2) {
  900. uvcount = 2;
  901. }
  902. if ((FVF&D3DFVF_TEX3) == D3DFVF_TEX3) {
  903. uvcount = 3;
  904. }
  905. if ((FVF&D3DFVF_TEX4) == D3DFVF_TEX4) {
  906. uvcount = 4;
  907. }
  908. if ((FVF&D3DFVF_TEX5) == D3DFVF_TEX5) {
  909. uvcount = 5;
  910. }
  911. if ((FVF&D3DFVF_TEX6) == D3DFVF_TEX6) {
  912. uvcount = 6;
  913. }
  914. if ((FVF&D3DFVF_TEX7) == D3DFVF_TEX7) {
  915. uvcount = 7;
  916. }
  917. if ((FVF&D3DFVF_TEX8) == D3DFVF_TEX8) {
  918. uvcount = 8;
  919. }
  920. for (int j=0; j<uvcount; j++) {
  921. unsigned char *vb=(unsigned char*) l.Get_Vertex_Array();
  922. const Vector2*uvs=split_table.Get_UV_Array(j);
  923. if (uvs) {
  924. for (i=0; i<split_table.Get_Vertex_Count(); i++)
  925. {
  926. *(Vector2*)(vb+fi.Get_Tex_Offset(j))=uvs[i];
  927. vb+=fi.Get_FVF_Size();
  928. }
  929. }
  930. }
  931. Generate_Texture_Categories(split_table,used_vertices);
  932. used_vertices+=needed_vertices;//vertex_count;
  933. }
  934. void DX8FVFCategoryContainer::Insert_To_Texture_Category(
  935. Vertex_Split_Table& split_table,
  936. TextureClass** texs,
  937. VertexMaterialClass* mat,
  938. ShaderClass shader,
  939. int pass,
  940. unsigned vertex_offset)
  941. {
  942. /*
  943. ** Try to find a DX8TextureCategoryClass in this FVF container which matches the
  944. ** given textures(one per stage), material and shader combination.
  945. */
  946. bool fit_in_existing_category = false;
  947. TextureCategoryListIterator it(&texture_category_list[pass]);
  948. while (!it.Is_Done()) {
  949. DX8TextureCategoryClass * tex_category=it.Peek_Obj();
  950. // Compare all stage's textures
  951. bool all_textures_same = true;
  952. for (unsigned int stage = 0; stage < MeshMatDescClass::MAX_TEX_STAGES; stage++) {
  953. all_textures_same = all_textures_same && (tex_category->Peek_Texture(stage) == texs[stage]);
  954. }
  955. if (all_textures_same && Equal_Material(tex_category->Peek_Material(),mat) && tex_category->Get_Shader()==shader) {
  956. used_indices+=tex_category->Add_Mesh(split_table,vertex_offset,used_indices,index_buffer,pass);
  957. fit_in_existing_category = true;
  958. break;
  959. }
  960. it.Next();
  961. }
  962. if (!fit_in_existing_category) {
  963. DX8TextureCategoryClass * new_tex_category=new DX8TextureCategoryClass(this,texs,shader,mat,pass);
  964. used_indices+=new_tex_category->Add_Mesh(split_table,vertex_offset,used_indices,index_buffer,pass);
  965. /*
  966. ** Add the texture category object into the list, immediately after any existing
  967. ** texture category object which uses the same texture. This will result in
  968. ** the list always having matching texture categories next to each other.
  969. */
  970. bool found_similar_category = false;
  971. TextureCategoryListIterator it(&texture_category_list[pass]);
  972. while (!it.Is_Done()) {
  973. // Categorize according to first stage's texture for now
  974. if (it.Peek_Obj()->Peek_Texture(0) == texs[0]) {
  975. texture_category_list[pass].Add_After(new_tex_category,it.Peek_Obj());
  976. found_similar_category = true;
  977. break;
  978. }
  979. it.Next();
  980. }
  981. if (!found_similar_category) {
  982. texture_category_list[pass].Add_Tail(new_tex_category);
  983. }
  984. }
  985. }
  986. const unsigned MAX_ADDED_TYPE_COUNT=64;
  987. struct Textures_Material_And_Shader_Booking_Struct
  988. {
  989. TextureClass* added_textures[MeshMatDescClass::MAX_TEX_STAGES][MAX_ADDED_TYPE_COUNT];
  990. VertexMaterialClass* added_materials[MAX_ADDED_TYPE_COUNT];
  991. ShaderClass added_shaders[MAX_ADDED_TYPE_COUNT];
  992. unsigned added_type_count;
  993. Textures_Material_And_Shader_Booking_Struct() : added_type_count(0) {}
  994. bool Add_Textures_Material_And_Shader(TextureClass** texs, VertexMaterialClass* mat, ShaderClass shd)
  995. {
  996. for (unsigned a=0;a<added_type_count;++a) {
  997. // Compare textures
  998. bool all_textures_same = true;
  999. for (unsigned int stage = 0; stage < MeshMatDescClass::MAX_TEX_STAGES; stage++) {
  1000. all_textures_same = all_textures_same && (texs[stage] == added_textures[stage][a]);
  1001. }
  1002. if (all_textures_same && Equal_Material(mat,added_materials[a]) && shd==added_shaders[a]) {
  1003. return false;
  1004. }
  1005. }
  1006. WWASSERT(added_type_count<MAX_ADDED_TYPE_COUNT);
  1007. for (unsigned int stage = 0; stage < MeshMatDescClass::MAX_TEX_STAGES; stage++) {
  1008. added_textures[stage][added_type_count]=texs[stage];
  1009. }
  1010. added_materials[added_type_count]=mat;
  1011. added_shaders[added_type_count]=shd;
  1012. added_type_count++;
  1013. return true;
  1014. }
  1015. };
  1016. void DX8FVFCategoryContainer::Generate_Texture_Categories(Vertex_Split_Table& split_table,unsigned vertex_offset)
  1017. {
  1018. int polygon_count=split_table.Get_Polygon_Count();
  1019. int index_count=polygon_count*3*split_table.Get_Pass_Count();
  1020. /*
  1021. ** If we don't have an index buffer yet, allocate one. Make it hold at least 12000 entries,
  1022. ** more if the mesh requires it.
  1023. */
  1024. if (!index_buffer) {
  1025. int ib_size=12000;
  1026. if (ib_size<index_count) ib_size=index_count;
  1027. if (sorting) {
  1028. index_buffer=NEW_REF(SortingIndexBufferClass,(ib_size));
  1029. }
  1030. else {
  1031. index_buffer=NEW_REF(DX8IndexBufferClass,(
  1032. ib_size,
  1033. (DX8Wrapper::Get_Current_Caps()->Support_NPatches() && WW3D::Get_NPatches_Level()>1) ? DX8IndexBufferClass::USAGE_NPATCHES : DX8IndexBufferClass::USAGE_DEFAULT));
  1034. }
  1035. }
  1036. for (unsigned pass=0;pass<split_table.Get_Pass_Count();++pass) {
  1037. Textures_Material_And_Shader_Booking_Struct textures_material_and_shader_booking;
  1038. unsigned old_used_indices=used_indices;
  1039. for (int i=0;i<polygon_count;++i) {
  1040. TextureClass* textures[MeshMatDescClass::MAX_TEX_STAGES];
  1041. WWASSERT(MAX_TEXTURE_STAGES==MeshMatDescClass::MAX_TEX_STAGES);
  1042. for (int stage=0;stage<MeshMatDescClass::MAX_TEX_STAGES;stage++) {
  1043. textures[stage]=split_table.Peek_Texture(i,pass,stage);
  1044. }
  1045. VertexMaterialClass* mat=split_table.Peek_Material(i,pass);
  1046. ShaderClass shader=split_table.Peek_Shader(i,pass);
  1047. if (!textures_material_and_shader_booking.Add_Textures_Material_And_Shader(textures,mat,shader)) continue;
  1048. Insert_To_Texture_Category(split_table,textures,mat,shader,pass,vertex_offset);
  1049. }
  1050. int new_inds=used_indices-old_used_indices;
  1051. WWASSERT(new_inds<=polygon_count*3);
  1052. }
  1053. }
  1054. // ----------------------------------------------------------------------------
  1055. DX8SkinFVFCategoryContainer::DX8SkinFVFCategoryContainer(bool sorting)
  1056. :
  1057. DX8FVFCategoryContainer(DX8_FVF_XYZNUV1,sorting),
  1058. VisibleVertexCount(0),
  1059. VisibleSkinHead(NULL)
  1060. {
  1061. }
  1062. // ----------------------------------------------------------------------------
  1063. DX8SkinFVFCategoryContainer::~DX8SkinFVFCategoryContainer()
  1064. {
  1065. }
  1066. // ----------------------------------------------------------------------------
  1067. void DX8SkinFVFCategoryContainer::Log(bool only_visible)
  1068. {
  1069. #ifdef ENABLE_CATEGORY_LOG
  1070. StringClass work(255,true);
  1071. work.Format("DX8SkinFVFCategoryContainer --------------\n");
  1072. WWDEBUG_SAY((work));
  1073. if (index_buffer) {
  1074. work.Format("IB size (used/total): %d/%d\n",used_indices,index_buffer->Get_Index_Count());
  1075. WWDEBUG_SAY((work));
  1076. }
  1077. else {
  1078. WWDEBUG_SAY(("EMPTY IB\n"));
  1079. }
  1080. for (unsigned pass=0;pass<passes;++pass) {
  1081. TextureCategoryListIterator it(&texture_category_list[pass]);
  1082. while (!it.Is_Done()) {
  1083. it.Peek_Obj()->Log(only_visible);
  1084. it.Next();
  1085. }
  1086. }
  1087. #endif
  1088. }
  1089. // ----------------------------------------------------------------------------
  1090. void DX8SkinFVFCategoryContainer::Render(void)
  1091. {
  1092. SNAPSHOT_SAY(("DX8SkinFVFCategoryContainer::Render()\n"));
  1093. if (!Anything_To_Render()) {
  1094. SNAPSHOT_SAY(("Nothing to render\n"));
  1095. return;
  1096. }
  1097. AnythingToRender=false;
  1098. DX8Wrapper::Set_Vertex_Buffer(NULL); // Free up the reference to the current vertex buffer
  1099. // (in case it is the dynamic, which may have to be resized)
  1100. DynamicVBAccessClass vb(
  1101. sorting ? BUFFER_TYPE_DYNAMIC_SORTING : BUFFER_TYPE_DYNAMIC_DX8,
  1102. dynamic_fvf_type,
  1103. VisibleVertexCount);
  1104. SNAPSHOT_SAY(("DynamicVBAccess - %s - %d vertices\n",sorting ? "sorting" : "non-sorting",VisibleVertexCount));
  1105. unsigned vertex_offset=0;
  1106. {
  1107. DynamicVBAccessClass::WriteLockClass l(&vb);
  1108. VertexFormatXYZNDUV2 * dest_verts = l.Get_Formatted_Vertex_Array();
  1109. MeshClass * mesh = VisibleSkinHead;
  1110. while (mesh != NULL) {
  1111. MeshModelClass * mmc = mesh->Peek_Model();
  1112. int mesh_vertex_count=mmc->Get_Vertex_Count();
  1113. WWASSERT((vertex_offset+mesh_vertex_count)<=VisibleVertexCount);
  1114. DX8_RECORD_SKIN_RENDER(mesh->Get_Num_Polys(),mesh_vertex_count);
  1115. if (_TempVertexBuffer.Length() < mesh_vertex_count) _TempVertexBuffer.Resize(mesh_vertex_count);
  1116. if (_TempNormalBuffer.Length() < mesh_vertex_count) _TempNormalBuffer.Resize(mesh_vertex_count);
  1117. Vector3* loc=&(_TempVertexBuffer[0]);
  1118. Vector3* norm=&(_TempNormalBuffer[0]);
  1119. const Vector2* uv0=mmc->Get_UV_Array_By_Index(0);
  1120. const Vector2* uv1=mmc->Get_UV_Array_By_Index(1);
  1121. const unsigned* diffuse=mmc->Get_Color_Array(0,false);
  1122. VertexFormatXYZNDUV2* verts=dest_verts+vertex_offset;
  1123. // mesh->Compose_Deformed_Vertex_Buffer(verts,uv0,uv1,diffuse);
  1124. mesh->Get_Deformed_Vertices(loc,norm);
  1125. for (int v=0;v<mesh_vertex_count;++v) {
  1126. verts[v].x=(*loc)[0];
  1127. verts[v].y=(*loc)[1];
  1128. verts[v].z=(*loc)[2];
  1129. verts[v].nx=(*norm)[0];
  1130. verts[v].ny=(*norm)[1];
  1131. verts[v].nz=(*norm)[2];
  1132. if (diffuse) {
  1133. verts[v].diffuse=*diffuse++;
  1134. }
  1135. else {
  1136. verts[v].diffuse=0;
  1137. }
  1138. if (uv0) {
  1139. verts[v].u1=(*uv0)[0];
  1140. verts[v].v1=(*uv0)[1];
  1141. uv0++;
  1142. }
  1143. else {
  1144. verts[v].u1=0.0f;
  1145. verts[v].v1=0.0f;
  1146. }
  1147. if (uv1) {
  1148. verts[v].u2=(*uv1)[0];
  1149. verts[v].v2=(*uv1)[1];
  1150. uv1++;
  1151. }
  1152. else {
  1153. verts[v].u2=0.0f;
  1154. verts[v].v2=0.0f;
  1155. }
  1156. loc++;
  1157. norm++;
  1158. }
  1159. mesh->Set_Base_Vertex_Offset(vertex_offset);
  1160. vertex_offset+=mesh_vertex_count;
  1161. mesh = mesh->Peek_Next_Visible_Skin();
  1162. }
  1163. }
  1164. WWASSERT(vertex_offset==VisibleVertexCount);
  1165. SNAPSHOT_SAY(("Set vb: %x ib: %x\n",vb,index_buffer));
  1166. DX8Wrapper::Set_Vertex_Buffer(vb);
  1167. DX8Wrapper::Set_Index_Buffer(index_buffer,0);
  1168. for (unsigned pass=0;pass<passes;++pass) {
  1169. SNAPSHOT_SAY(("Pass: %d\n",pass));
  1170. while (DX8TextureCategoryClass * tex = visible_texture_category_list[pass].Remove_Head()) {
  1171. tex->Render();
  1172. }
  1173. }
  1174. Render_Procedural_Material_Passes();
  1175. VisibleSkinHead = NULL;
  1176. VisibleVertexCount = 0;
  1177. }
  1178. bool DX8SkinFVFCategoryContainer::Check_If_Mesh_Fits(MeshModelClass* mmc)
  1179. {
  1180. if (!index_buffer) return true; // No IB created - mesh will fit as a new ib will be created when inserting
  1181. int required_polygons=mmc->Get_Polygon_Count();
  1182. if (mmc->Get_Gap_Filler()) {
  1183. required_polygons+=mmc->Get_Gap_Filler()->Get_Polygon_Count();
  1184. }
  1185. if ((required_polygons*3*mmc->Get_Pass_Count())<=index_buffer->Get_Index_Count()-used_indices) {
  1186. return true;
  1187. }
  1188. return false;
  1189. }
  1190. void DX8SkinFVFCategoryContainer::Add_Visible_Skin(MeshClass * mesh)
  1191. {
  1192. mesh->Set_Next_Visible_Skin(VisibleSkinHead);
  1193. VisibleSkinHead = mesh;
  1194. VisibleVertexCount += mesh->Peek_Model()->Get_Vertex_Count();
  1195. }
  1196. // ----------------------------------------------------------------------------
  1197. void DX8SkinFVFCategoryContainer::Reset()
  1198. {
  1199. VisibleVertexCount = 0;
  1200. VisibleSkinHead = NULL;
  1201. for (unsigned pass=0;pass<passes;++pass) {
  1202. while (DX8TextureCategoryClass* texture_category=texture_category_list[pass].Peek_Head()) {
  1203. delete texture_category;
  1204. }
  1205. }
  1206. REF_PTR_RELEASE(index_buffer);
  1207. used_indices=0;
  1208. }
  1209. // ----------------------------------------------------------------------------
  1210. void DX8SkinFVFCategoryContainer::Add_Mesh(MeshClass* mesh)
  1211. {
  1212. Vertex_Split_Table split_table(mesh);
  1213. Generate_Texture_Categories(split_table,0);
  1214. }
  1215. // ----------------------------------------------------------------------------
  1216. unsigned DX8TextureCategoryClass::Add_Mesh(
  1217. Vertex_Split_Table& split_table,
  1218. unsigned vertex_offset,
  1219. unsigned index_offset,
  1220. IndexBufferClass* index_buffer,
  1221. unsigned pass)
  1222. {
  1223. int poly_count=split_table.Get_Polygon_Count();
  1224. unsigned index_count=0;
  1225. /*
  1226. ** Count the polygons in the given mesh in the given pass which match this texture category
  1227. */
  1228. unsigned polygons=0;
  1229. for (int i=0;i<poly_count;++i) {
  1230. bool all_textures_same = true;
  1231. for (unsigned int stage = 0; stage < MeshMatDescClass::MAX_TEX_STAGES; stage++) {
  1232. all_textures_same = all_textures_same && (split_table.Peek_Texture(i, pass, stage) == textures[stage]);
  1233. }
  1234. VertexMaterialClass* mat=split_table.Peek_Material(i,pass);
  1235. ShaderClass shd=split_table.Peek_Shader(i,pass);
  1236. if (all_textures_same && Equal_Material(mat,material) && shd==shader) {
  1237. polygons++;
  1238. }
  1239. }
  1240. /*
  1241. ** Add the indices for the polygons that match into this renderer's dx8 index table.
  1242. */
  1243. if (polygons) {
  1244. index_count=polygons*3;
  1245. #ifndef ENABLE_STRIPING
  1246. bool stripify=false;
  1247. #else
  1248. bool stripify=true;
  1249. if (index_buffer->Type()==BUFFER_TYPE_SORTING || index_buffer->Type()==BUFFER_TYPE_DYNAMIC_SORTING) {
  1250. stripify=false;
  1251. }
  1252. #endif;
  1253. const TriIndex* src_indices=(const TriIndex*)split_table.Get_Polygon_Array(pass);//mmc->Get_Polygon_Array();
  1254. if (stripify) {
  1255. int* triangles=new int[index_count];
  1256. int triangle_index_count=0;
  1257. for (int i=0;i<poly_count;++i) {
  1258. bool all_textures_same = true;
  1259. for (unsigned int stage = 0; stage < MeshMatDescClass::MAX_TEX_STAGES; stage++) {
  1260. all_textures_same = all_textures_same && (split_table.Peek_Texture(i, pass, stage) == textures[stage]);
  1261. }
  1262. VertexMaterialClass* mat=split_table.Peek_Material(i,pass);
  1263. ShaderClass shd=split_table.Peek_Shader(i,pass);
  1264. if (all_textures_same && Equal_Material(mat,material) && shd==shader) {
  1265. triangles[triangle_index_count++]=src_indices[i][0]+vertex_offset;
  1266. triangles[triangle_index_count++]=src_indices[i][1]+vertex_offset;
  1267. triangles[triangle_index_count++]=src_indices[i][2]+vertex_offset;
  1268. }
  1269. }
  1270. int* strips=StripOptimizerClass::Stripify(triangles, triangle_index_count/3);
  1271. delete[] triangles;
  1272. int* strip=StripOptimizerClass::Combine_Strips(strips+1,strips[0]);
  1273. delete[] strips;
  1274. if (index_count<unsigned(strip[0])) {
  1275. stripify=false;
  1276. }
  1277. else {
  1278. index_count=strip[0];
  1279. DX8PolygonRendererClass* p_renderer=new DX8PolygonRendererClass(
  1280. index_count,
  1281. split_table.Get_Mesh_Class(),
  1282. this,
  1283. vertex_offset,
  1284. index_offset,
  1285. true);
  1286. PolygonRendererList.Add_Tail(p_renderer);
  1287. {
  1288. IndexBufferClass::AppendLockClass l(index_buffer,index_offset,index_count);
  1289. unsigned short* dst_indices=l.Get_Index_Array();
  1290. unsigned short vmin=0xffff;
  1291. unsigned short vmax=0;
  1292. /*
  1293. ** Iterate over the polys for this pass, adding each one that matches this texture+material+shader
  1294. */
  1295. for (unsigned i=0;i<index_count;++i) {
  1296. unsigned short idx;
  1297. idx=unsigned short(strip[i+1]);
  1298. vmin=MIN(vmin,idx);
  1299. vmax=MAX(vmax,idx);
  1300. *dst_indices++=idx;
  1301. }
  1302. /*
  1303. ** Remember the min and max vertex indices that these polygons used (for optimization)
  1304. */
  1305. p_renderer->Set_Vertex_Index_Range(vmin,vmax-vmin+1);
  1306. }
  1307. }
  1308. delete[] strip;
  1309. }
  1310. // Need to check stripify again as it may be changed to false by the previous statement
  1311. if (!stripify ) {
  1312. DX8PolygonRendererClass* p_renderer=new DX8PolygonRendererClass(
  1313. index_count,
  1314. split_table.Get_Mesh_Class(),
  1315. this,
  1316. vertex_offset,
  1317. index_offset,
  1318. false);
  1319. PolygonRendererList.Add_Tail(p_renderer);
  1320. IndexBufferClass::AppendLockClass l(index_buffer,index_offset,index_count);
  1321. unsigned short* dst_indices=l.Get_Index_Array();
  1322. unsigned short vmin=0xffff;
  1323. unsigned short vmax=0;
  1324. /*
  1325. ** Iterate over the polys for this pass, adding each one that matches this texture+material+shader
  1326. */
  1327. for (int i=0;i<poly_count;++i) {
  1328. bool all_textures_same = true;
  1329. for (unsigned int stage = 0; stage < MeshMatDescClass::MAX_TEX_STAGES; stage++) {
  1330. all_textures_same = all_textures_same && (split_table.Peek_Texture(i, pass, stage) == textures[stage]);
  1331. }
  1332. VertexMaterialClass* mat=split_table.Peek_Material(i,pass);
  1333. ShaderClass shd=split_table.Peek_Shader(i,pass);
  1334. if (all_textures_same && Equal_Material(mat,material) && shd==shader) {
  1335. unsigned short idx;
  1336. idx=unsigned short(src_indices[i][0]+vertex_offset);
  1337. vmin=MIN(vmin,idx);
  1338. vmax=MAX(vmax,idx);
  1339. *dst_indices++=idx;
  1340. // WWDEBUG_SAY(("%d, ",idx));
  1341. idx=unsigned short(src_indices[i][1]+vertex_offset);
  1342. vmin=MIN(vmin,idx);
  1343. vmax=MAX(vmax,idx);
  1344. *dst_indices++=idx;
  1345. // WWDEBUG_SAY(("%d, ",idx));
  1346. idx=unsigned short(src_indices[i][2]+vertex_offset);
  1347. vmin=MIN(vmin,idx);
  1348. vmax=MAX(vmax,idx);
  1349. *dst_indices++=idx;
  1350. // WWDEBUG_SAY(("%d\n",idx));
  1351. }
  1352. }
  1353. WWASSERT((vmax-vmin)<split_table.Get_Mesh_Model_Class()->Get_Vertex_Count());
  1354. /*
  1355. ** Remember the min and max vertex indices that these polygons used (for optimization)
  1356. */
  1357. p_renderer->Set_Vertex_Index_Range(vmin,vmax-vmin+1);
  1358. WWASSERT(index_count<=unsigned(split_table.Get_Polygon_Count()*3));
  1359. }
  1360. }
  1361. return index_count;
  1362. }
  1363. // ----------------------------------------------------------------------------
  1364. void DX8TextureCategoryClass::Render(void)
  1365. {
  1366. #ifdef WWDEBUG
  1367. if (!WW3D::Expose_Prelit()) {
  1368. #endif
  1369. for (unsigned i=0;i<MAX_TEXTURE_STAGES;++i) {
  1370. SNAPSHOT_SAY(("Set_Texture(%d,%s)\n",i,Peek_Texture(i) ? Peek_Texture(i)->Get_Texture_Name() : "NULL"));
  1371. DX8Wrapper::Set_Texture(i,Peek_Texture(i));
  1372. }
  1373. #ifdef WWDEBUG
  1374. }
  1375. #endif
  1376. SNAPSHOT_SAY(("Set_Material(%s)\n",Peek_Material() ? Peek_Material()->Get_Name() : "NULL"));
  1377. DX8Wrapper::Set_Material(Peek_Material());
  1378. SNAPSHOT_SAY(("Set_Shader(0x%x)\n",Get_Shader()));
  1379. DX8Wrapper::Set_Shader(Get_Shader());
  1380. PolyRenderTaskClass * prt = render_task_head;
  1381. while (prt) {
  1382. /*
  1383. ** Dig out the parameters for this render task
  1384. */
  1385. DX8PolygonRendererClass * renderer = prt->Peek_Polygon_Renderer();
  1386. MeshClass * mesh = prt->Peek_Mesh();
  1387. SNAPSHOT_SAY(("mesh = %s\n",mesh->Get_Name()));
  1388. #ifdef WWDEBUG
  1389. // Debug rendering: if it exists, expose prelighting on this mesh by disabling all base textures.
  1390. if (WW3D::Expose_Prelit()) {
  1391. switch (mesh->Peek_Model()->Get_Flag (MeshGeometryClass::PRELIT_MASK)) {
  1392. unsigned i;
  1393. case MeshGeometryClass::PRELIT_VERTEX:
  1394. // Disable texturing on all stages and passes.
  1395. for (i = 0; i < MAX_TEXTURE_STAGES; i++) {
  1396. DX8Wrapper::Set_Texture (i, NULL);
  1397. }
  1398. break;
  1399. case MeshGeometryClass::PRELIT_LIGHTMAP_MULTI_PASS:
  1400. // Disable texturing on all but the last pass.
  1401. if (pass == mesh->Peek_Model()->Get_Pass_Count() - 1) {
  1402. for (i = 0; i < MAX_TEXTURE_STAGES; i++) {
  1403. DX8Wrapper::Set_Texture (i, Peek_Texture (i));
  1404. }
  1405. } else {
  1406. for (i = 0; i < MAX_TEXTURE_STAGES; i++) {
  1407. DX8Wrapper::Set_Texture (i, NULL);
  1408. }
  1409. }
  1410. break;
  1411. case MeshGeometryClass::PRELIT_LIGHTMAP_MULTI_TEXTURE:
  1412. // Disable texturing on all but the zeroth stage of each pass.
  1413. DX8Wrapper::Set_Texture (0, Peek_Texture (0));
  1414. for (i = 1; i < MAX_TEXTURE_STAGES; i++) {
  1415. DX8Wrapper::Set_Texture (i, NULL);
  1416. }
  1417. break;
  1418. default:
  1419. for (i = 0; i < MAX_TEXTURE_STAGES; i++) {
  1420. DX8Wrapper::Set_Texture (i, Peek_Texture (i));
  1421. }
  1422. break;
  1423. }
  1424. }
  1425. #endif
  1426. /*
  1427. ** If the user is not installing LightEnvironmentClasses, we leave the lighting render
  1428. ** states untouched. This way they can set a couple global lights that affect the entire scene.
  1429. */
  1430. LightEnvironmentClass * lenv = mesh->Get_Lighting_Environment();
  1431. if (lenv != NULL) {
  1432. SNAPSHOT_SAY(("LightEnvironment, lights: %d\n",lenv->Get_Light_Count()));
  1433. DX8Wrapper::Set_Light_Environment(lenv);
  1434. }
  1435. else {
  1436. SNAPSHOT_SAY(("No light environment\n"));
  1437. }
  1438. /*
  1439. ** Support for ALIGNED and ORIENTED camera modes
  1440. */
  1441. const Matrix3D* world_transform = &mesh->Get_Transform();
  1442. bool identity=mesh->Is_Transform_Identity();
  1443. Matrix3D tmp_world;
  1444. if (mesh->Peek_Model()->Get_Flag(MeshModelClass::ALIGNED)) {
  1445. SNAPSHOT_SAY(("Camera mode ALIGNED\n"));
  1446. Vector3 mesh_position;
  1447. Vector3 camera_z_vector;
  1448. TheDX8MeshRenderer.Peek_Camera()->Get_Transform().Get_Z_Vector(&camera_z_vector);
  1449. mesh->Get_Transform().Get_Translation(&mesh_position);
  1450. tmp_world.Obj_Look_At(mesh_position,mesh_position + camera_z_vector,0.0f);
  1451. world_transform = &tmp_world;
  1452. } else if (mesh->Peek_Model()->Get_Flag(MeshModelClass::ORIENTED)) {
  1453. SNAPSHOT_SAY(("Camera mode ORIENTED\n"));
  1454. Vector3 mesh_position;
  1455. Vector3 camera_position;
  1456. TheDX8MeshRenderer.Peek_Camera()->Get_Transform().Get_Translation(&camera_position);
  1457. mesh->Get_Transform().Get_Translation(&mesh_position);
  1458. tmp_world.Obj_Look_At(mesh_position,camera_position,0.0f);
  1459. world_transform = &tmp_world;
  1460. } else if (mesh->Peek_Model()->Get_Flag(MeshModelClass::SKIN)) {
  1461. SNAPSHOT_SAY(("Set world identity (for skin)\n"));
  1462. tmp_world.Make_Identity();
  1463. world_transform = &tmp_world;
  1464. identity=true;
  1465. }
  1466. if (identity) {
  1467. SNAPSHOT_SAY(("Set_World_Identity\n"));
  1468. DX8Wrapper::Set_World_Identity();
  1469. }
  1470. else {
  1471. SNAPSHOT_SAY(("Set_World_Transform\n"));
  1472. DX8Wrapper::Set_Transform(D3DTS_WORLD,*world_transform);
  1473. }
  1474. // The mesh renderer debugger can disable mesh rendering
  1475. if (!DX8RendererDebugger::Is_Enabled() || !mesh->Is_Disabled_By_Debugger()) {
  1476. /*
  1477. ** Render mesh using either sorting or immediate pipeline
  1478. */
  1479. if ((!!mesh->Peek_Model()->Get_Flag(MeshGeometryClass::SORT)) && WW3D::Is_Sorting_Enabled()) {
  1480. renderer->Render_Sorted(mesh->Get_Base_Vertex_Offset(),mesh->Get_Bounding_Sphere());
  1481. } else {
  1482. renderer->Render(mesh->Get_Base_Vertex_Offset());
  1483. }
  1484. }
  1485. /*
  1486. ** Move to the next render task. Note that the delete should be fast because prt's are pooled
  1487. */
  1488. PolyRenderTaskClass * next_prt = prt->Get_Next_Visible();
  1489. delete prt;
  1490. prt = next_prt;
  1491. }
  1492. Clear_Render_List();
  1493. }
  1494. DX8MeshRendererClass::DX8MeshRendererClass()
  1495. :
  1496. camera(NULL),
  1497. enable_lighting(true),
  1498. texture_category_container_list_skin(NULL),
  1499. visible_decal_meshes(NULL)
  1500. {
  1501. }
  1502. DX8MeshRendererClass::~DX8MeshRendererClass()
  1503. {
  1504. Invalidate();
  1505. Clear_Pending_Delete_Lists();
  1506. if (texture_category_container_list_skin != NULL) {
  1507. delete texture_category_container_list_skin;
  1508. }
  1509. }
  1510. void DX8MeshRendererClass::Init(void)
  1511. {
  1512. texture_category_container_list_skin = new FVFCategoryList;
  1513. }
  1514. void DX8MeshRendererClass::Shutdown(void)
  1515. {
  1516. Invalidate();
  1517. Clear_Pending_Delete_Lists();
  1518. }
  1519. // ----------------------------------------------------------------------------
  1520. void DX8MeshRendererClass::Clear_Pending_Delete_Lists()
  1521. {
  1522. while (DX8TextureCategoryClass* category=texture_category_delete_list.Remove_Head()) {
  1523. delete category;
  1524. }
  1525. while (DX8FVFCategoryContainer* container=fvf_category_container_delete_list.Remove_Head()) {
  1526. delete container;
  1527. }
  1528. }
  1529. // ----------------------------------------------------------------------------
  1530. static void Add_Rigid_Mesh_To_Container(FVFCategoryList* container_list,unsigned fvf,MeshClass* mesh)
  1531. {
  1532. WWASSERT(container_list);
  1533. DX8FVFCategoryContainer * container = NULL;
  1534. MeshModelClass * mmc = mesh->Peek_Model();
  1535. bool sorting=((!!mmc->Get_Flag(MeshModelClass::SORT)) && WW3D::Is_Sorting_Enabled() && (mmc->Get_Sort_Level() == SORT_LEVEL_NONE));
  1536. FVFCategoryListIterator it(container_list);
  1537. while (!it.Is_Done()) {
  1538. container = it.Peek_Obj();
  1539. if (sorting==container->Is_Sorting() && container->Check_If_Mesh_Fits(mmc)) {
  1540. container->Add_Mesh(mesh);
  1541. return;
  1542. }
  1543. it.Next();
  1544. }
  1545. container=new DX8RigidFVFCategoryContainer(fvf,sorting);
  1546. container_list->Add_Tail(container);
  1547. container->Add_Mesh(mesh);
  1548. }
  1549. // ----------------------------------------------------------------------------
  1550. void DX8MeshRendererClass::Unregister_Mesh_Type(MeshClass* mesh)
  1551. {
  1552. while (DX8PolygonRendererClass* n=mesh->PolygonRendererList.Remove_Head()) {
  1553. delete n;
  1554. }
  1555. _RegisteredMeshTable.Remove(MeshRegKeyStruct(mesh->Peek_Model(),mesh->Get_User_Lighting_Array()),mesh);
  1556. // Also remove the gap filler!
  1557. MeshModelClass * mmc = mesh->Peek_Model();
  1558. if (mmc->GapFiller) {
  1559. GapFillerClass* gf=mmc->GapFiller;
  1560. mmc->GapFiller=NULL;
  1561. delete gf;
  1562. }
  1563. }
  1564. void DX8MeshRendererClass::Register_Mesh_Type(MeshClass* mesh)
  1565. {
  1566. WWMEMLOG(MEM_GEOMETRY);
  1567. MeshModelClass * mmc = mesh->Peek_Model();
  1568. #ifdef ENABLE_CATEGORY_LOG
  1569. WWDEBUG_SAY(("Registering mesh: %s (%d polys, %d verts + %d gap polygons)\n",mmc->Get_Name(),mmc->Get_Polygon_Count(),mmc->Get_Vertex_Count(),mmc->Get_Gap_Filler_Polygon_Count()));
  1570. #endif
  1571. bool skin=(mmc->Get_Flag(MeshModelClass::SKIN) && mmc->VertexBoneLink);
  1572. bool sorting=((!!mmc->Get_Flag(MeshModelClass::SORT)) && WW3D::Is_Sorting_Enabled() && (mmc->Get_Sort_Level() == SORT_LEVEL_NONE));
  1573. if (skin) {
  1574. /*
  1575. ** This mesh is a skin. Add it to a DX8SkinFVFCategoryContainer.
  1576. */
  1577. WWASSERT(texture_category_container_list_skin);
  1578. FVFCategoryListIterator it(texture_category_container_list_skin);
  1579. while (!it.Is_Done()) {
  1580. DX8FVFCategoryContainer * container = it.Peek_Obj();
  1581. if (sorting==container->Is_Sorting() && container->Check_If_Mesh_Fits(mmc)) {
  1582. container->Add_Mesh(mesh);
  1583. return;
  1584. }
  1585. it.Next();
  1586. }
  1587. DX8FVFCategoryContainer * new_container=new DX8SkinFVFCategoryContainer(sorting);
  1588. texture_category_container_list_skin->Add_Tail(new_container);
  1589. new_container->Add_Mesh(mesh);
  1590. } else {
  1591. unsigned int * user_lighting = mesh->Get_User_Lighting_Array();
  1592. MeshClass * existing_mesh = _RegisteredMeshTable.Get(MeshRegKeyStruct(mmc,user_lighting));
  1593. if (existing_mesh != NULL) {
  1594. // We found another instance of this mesh model so we can simply clone the poly renderers
  1595. DX8PolygonRendererListIterator it(&(existing_mesh->PolygonRendererList));
  1596. while (!it.Is_Done()) {
  1597. DX8PolygonRendererClass * src_renderer=it.Peek_Obj();
  1598. DX8PolygonRendererClass * new_renderer = new DX8PolygonRendererClass(*src_renderer,mesh);
  1599. src_renderer->Get_Texture_Category()->Add_Polygon_Renderer(new_renderer);
  1600. it.Next();
  1601. }
  1602. } else {
  1603. // This mesh model either is not registered yet or has been registered with a
  1604. // different user lighting array. Simply add it into the system as if it is a
  1605. // completely unique mesh.
  1606. unsigned fvf=DX8FVFCategoryContainer::Define_FVF(mmc,user_lighting,enable_lighting);
  1607. /*
  1608. ** Search for an existing FVF Category Container that matches this mesh
  1609. */
  1610. for (int i=0;i<texture_category_container_lists_rigid.Count();++i) {
  1611. FVFCategoryList * list=texture_category_container_lists_rigid[i];
  1612. WWASSERT(list);
  1613. DX8FVFCategoryContainer * container=list->Peek_Head();
  1614. if (container && container->Get_FVF()!=fvf) continue;
  1615. Add_Rigid_Mesh_To_Container(list,fvf,mesh);
  1616. break;
  1617. }
  1618. if (i==texture_category_container_lists_rigid.Count()) {
  1619. /*
  1620. ** We couldn't find an existing FVF category container so we have to add one. Future
  1621. ** meshes that use this FVF will also be able to use this container.
  1622. */
  1623. FVFCategoryList * new_fvf_category = new FVFCategoryList();
  1624. texture_category_container_lists_rigid.Add(new_fvf_category);
  1625. Add_Rigid_Mesh_To_Container(new_fvf_category,fvf,mesh);
  1626. }
  1627. /*
  1628. ** Done processing the mesh, add its polygon renderers to the global registered mesh list
  1629. */
  1630. if (mesh->PolygonRendererList.Is_Empty() == false) {
  1631. _RegisteredMeshTable.Insert(MeshRegKeyStruct(mmc,user_lighting),mesh);
  1632. }
  1633. else {
  1634. WWDEBUG_SAY(("Error: Register_Mesh_Type failed! file: %s line: %d\r\n",__FILE__,__LINE__));
  1635. }
  1636. }
  1637. }
  1638. return;
  1639. }
  1640. static unsigned statistics_requested=0;
  1641. void DX8MeshRendererClass::Request_Log_Statistics()
  1642. {
  1643. statistics_requested=WW3D::Get_Frame_Count();
  1644. }
  1645. // ---------------------------------------------------------------------------
  1646. //
  1647. // Render all meshes that are added to visible lists
  1648. //
  1649. // ---------------------------------------------------------------------------
  1650. static void Render_FVF_Category_Container_List(FVFCategoryList& list)
  1651. {
  1652. FVFCategoryListIterator it(&list);
  1653. while (!it.Is_Done()) {
  1654. it.Peek_Obj()->Render();
  1655. it.Next();
  1656. }
  1657. }
  1658. static void Render_FVF_Category_Container_List_Delayed_Passes(FVFCategoryList& list)
  1659. {
  1660. FVFCategoryListIterator it(&list);
  1661. while (!it.Is_Done()) {
  1662. it.Peek_Obj()->Render_Delayed_Procedural_Material_Passes();
  1663. it.Next();
  1664. }
  1665. }
  1666. void DX8MeshRendererClass::Flush(void)
  1667. {
  1668. int i;
  1669. WWPROFILE("DX8MeshRenderer::Flush");
  1670. if (!camera) return;
  1671. Log_Statistics_String(true);
  1672. /*
  1673. ** Render the FVF categories. Note that it is critical that skins be
  1674. ** rendered *after* the rigid meshes. This is caused by the fact that an object may
  1675. ** have its base passes disabled and a translucent procedural material pass rendered
  1676. ** instead. In this case, technically we have to delay rendering of the material pass but
  1677. ** for skins we just render these passes as we go because we can assume that the
  1678. ** bulk of the meshes have already been drawn (there would be extra overhead involved
  1679. ** in solving this for skins)
  1680. */
  1681. for (i=0;i<texture_category_container_lists_rigid.Count();++i) {
  1682. Render_FVF_Category_Container_List(*texture_category_container_lists_rigid[i]);
  1683. }
  1684. Render_FVF_Category_Container_List(*texture_category_container_list_skin);
  1685. Render_Decal_Meshes();
  1686. /*
  1687. ** Render the translucent procedural material passes that were applied to meshes that
  1688. ** had their base passes disabled.
  1689. */
  1690. for (i=0;i<texture_category_container_lists_rigid.Count();++i) {
  1691. Render_FVF_Category_Container_List_Delayed_Passes(*texture_category_container_lists_rigid[i]);
  1692. }
  1693. DX8Wrapper::Set_Vertex_Buffer(NULL);
  1694. DX8Wrapper::Set_Index_Buffer(NULL,0);
  1695. }
  1696. void DX8MeshRendererClass::Add_To_Render_List(DecalMeshClass * decalmesh)
  1697. {
  1698. WWASSERT(decalmesh != NULL);
  1699. decalmesh->Set_Next_Visible(visible_decal_meshes);
  1700. visible_decal_meshes = decalmesh;
  1701. }
  1702. void DX8MeshRendererClass::Render_Decal_Meshes(void)
  1703. {
  1704. DX8Wrapper::Set_DX8_Render_State(D3DRS_ZBIAS,8);
  1705. DecalMeshClass * decal_mesh = visible_decal_meshes;
  1706. while (decal_mesh != NULL) {
  1707. decal_mesh->Render();
  1708. decal_mesh = decal_mesh->Peek_Next_Visible();
  1709. }
  1710. visible_decal_meshes = NULL;
  1711. DX8Wrapper::Set_DX8_Render_State(D3DRS_ZBIAS,0);
  1712. }
  1713. // ----------------------------------------------------------------------------
  1714. static void Log_Container_List(FVFCategoryList& container_list,bool only_visible)
  1715. {
  1716. FVFCategoryListIterator it(&container_list);
  1717. while (!it.Is_Done()) {
  1718. it.Peek_Obj()->Log(only_visible);
  1719. it.Next();
  1720. }
  1721. }
  1722. void DX8MeshRendererClass::Log_Statistics_String(bool only_visible)
  1723. {
  1724. if (statistics_requested!=WW3D::Get_Frame_Count()) return;
  1725. for (int i=0;i<texture_category_container_lists_rigid.Count();++i) {
  1726. Log_Container_List(*texture_category_container_lists_rigid[i],only_visible);
  1727. }
  1728. Log_Container_List(*texture_category_container_list_skin,only_visible);
  1729. }
  1730. static void Invalidate_FVF_Category_Container_List(FVFCategoryList& list)
  1731. {
  1732. while (DX8FVFCategoryContainer* fvf_category=list.Remove_Head()) {
  1733. delete fvf_category;
  1734. }
  1735. }
  1736. void DX8MeshRendererClass::Invalidate()
  1737. {
  1738. WWMEMLOG(MEM_RENDERER);
  1739. _RegisteredMeshTable.Remove_All();
  1740. for (int i=0;i<texture_category_container_lists_rigid.Count();++i) {
  1741. Invalidate_FVF_Category_Container_List(*texture_category_container_lists_rigid[i]);
  1742. delete texture_category_container_lists_rigid[i];
  1743. }
  1744. if (texture_category_container_list_skin) {
  1745. Invalidate_FVF_Category_Container_List(*texture_category_container_list_skin);
  1746. delete texture_category_container_list_skin;
  1747. texture_category_container_list_skin=NULL;
  1748. }
  1749. texture_category_container_list_skin = new FVFCategoryList;
  1750. texture_category_container_lists_rigid.Delete_All();
  1751. }