MLRTriangleClipping.hpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #if !defined(MLR_MLRCLIPTRICK_HPP)
  5. #include <MLR\MLRClipTrick.hpp>
  6. #endif
  7. extern DWORD gEnableTextureSort, gShowClippedPolys, gEnableDetailTexture;
  8. extern unsigned short *indexOffset; // [MidLevelRenderer::Max_Number_Vertices_Per_Mesh]
  9. #define HUNT_CLIP_ERROR 0
  10. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. //
  12. void
  13. CLASSNAME::TransformNoClip(Matrix4D *mat, GOSVertexPool *vt, bool db)
  14. {
  15. Check_Object(this);
  16. Check_Object(vt);
  17. Verify(index.GetLength() > 0);
  18. Start_Timer(Transform_Time);
  19. // unsigned short stride;
  20. int stride;
  21. bool textureAnimation = false;
  22. Scalar deltaU=0.0f, deltaV=0.0f;
  23. if(state.GetTextureHandle())
  24. {
  25. MLRTexture *texture = (*MLRTexturePool::Instance)[state.GetTextureHandle()];
  26. Check_Object(texture);
  27. textureAnimation = texture->GetAnimateTexture();
  28. if(textureAnimation)
  29. {
  30. Stuff::AffineMatrix4D &textureMatrix = texture->GetTextureMatrix();
  31. deltaU = textureMatrix(3, 0);
  32. deltaV = textureMatrix(3, 1);
  33. }
  34. }
  35. int i, j;
  36. #ifdef I_SAY_YES_TO_TERRAIN
  37. Scalar terrainUV[2];
  38. #endif // I_SAY_YES_TO_TERRAIN
  39. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  40. int m;
  41. gos_PushCurrentHeap(StaticHeap);
  42. int tex2count[Limits::Max_Number_Of_Multitextures];
  43. gos_PopCurrentHeap();
  44. for(m=0;m<currentNrOfPasses;m++)
  45. {
  46. tex2count[m] = 0;
  47. }
  48. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  49. int numVertices = GetNumVertices();
  50. gos_vertices = vt->GetActualVertexPool(db);
  51. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  52. int tex2count = 0;
  53. #endif // I_SAY_YES_TO_DUAL_TEXTURES
  54. #ifdef I_SAY_YES_TO_DETAIL_TEXTURES
  55. Scalar detailTexCoords[2];
  56. #endif // I_SAY_YES_TO_DETAIL_TEXTURES
  57. #if defined(I_SAY_YES_TO_DUAL_TEXTURES) || defined(I_SAY_YES_TO_DETAIL_TEXTURES)
  58. gos_vertices2uv = vt->GetActualVertexPool2UV(db);
  59. #endif
  60. numGOSVertices = 0;
  61. Verify(index.GetLength() > 0);
  62. Verify(coords.GetLength() == visibleIndexedVertices.GetLength());
  63. if(visibleIndexedVerticesKey == false)
  64. {
  65. FindVisibleVertices();
  66. }
  67. #ifdef _ARMOR
  68. memset(indexOffset, 0xff, Limits::Max_Number_Vertices_Per_Mesh*sizeof(unsigned short));
  69. #endif // _ARMOR
  70. for(j=0,stride=0;j<numVertices;j++)
  71. {
  72. if(visibleIndexedVertices[j] == 0)
  73. {
  74. stride++;
  75. }
  76. else
  77. {
  78. #ifdef LAB_ONLY
  79. Set_Statistic(TransformedVertices, TransformedVertices+1);
  80. if(db==false)
  81. {
  82. Verify (vt->GetLast() + numGOSVertices < Limits::Max_Number_Vertices_Per_Frame);
  83. }
  84. else
  85. {
  86. Verify (numGOSVertices < 2*Limits::Max_Number_Vertices_Per_Mesh);
  87. }
  88. #endif // LAB_ONLY
  89. *(int *)(&indexOffset[j]) = (j - stride);
  90. #ifdef I_SAY_YES_TO_TERRAIN
  91. terrainUV[0] = borderPixelFun + (coords[j].x - minX)*xUVFac;
  92. terrainUV[1] = borderPixelFun + (coords[j].z - minZ)*zUVFac;
  93. #endif // I_SAY_YES_TO_TERRAIN
  94. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  95. if(MLRState::GetMultitextureLightMap() == true && state.GetMultiTextureMode()!=MLRState::MultiTextureOffMode)
  96. {
  97. gos_vertices2uv[numGOSVertices].GOSTransformNoClip(
  98. coords[j],
  99. *mat,
  100. &texCoords[j][0],
  101. &texCoords[numVertices + j][0]
  102. #if FOG_HACK
  103. ,state.GetFogMode()
  104. #endif // FOG_HACK
  105. );
  106. #ifdef I_SAY_YES_TO_COLOR
  107. #if COLOR_AS_DWORD
  108. gos_vertices2uv[numGOSVertices].argb = (*actualColors)[j];
  109. #else // COLOR_AS_DWORD
  110. gos_vertices2uv[numGOSVertices].argb = GOSCopyColor(&(*actualColors)[j]);
  111. #endif // COLOR_AS_DWORD
  112. #else // I_SAY_YES_TO_COLOR
  113. gos_vertices2uv[numGOSVertices].argb = 0xffffffff;
  114. #endif // I_SAY_YES_TO_COLOR
  115. }
  116. else
  117. {
  118. gos_vertices[numGOSVertices].GOSTransformNoClip(
  119. coords[j],
  120. *mat,
  121. &texCoords[j][0]
  122. #if FOG_HACK
  123. ,state.GetFogMode()
  124. #endif // FOG_HACK
  125. );
  126. (*texCoords2)[tex2count++] = texCoords[numVertices + j];
  127. }
  128. #else // I_SAY_YES_TO_DUAL_TEXTURES
  129. #ifdef I_SAY_YES_TO_DETAIL_TEXTURES
  130. if( MLRState::GetMultitextureLightMap() == true &&
  131. state.GetMultiTextureMode()!=MLRState::MultiTextureOffMode &&
  132. gEnableDetailTexture==1
  133. )
  134. {
  135. #ifdef I_SAY_YES_TO_TERRAIN2
  136. detailTexCoords[0] = texCoords[j][0]*xScale;
  137. detailTexCoords[1] = texCoords[j][1]*yScale;
  138. #else
  139. detailTexCoords[0] = texCoords[j][0]*xScale + xOffset;
  140. detailTexCoords[1] = texCoords[j][1]*yScale + yOffset;
  141. #endif
  142. Verify( MLRState::GetHasMaxUVs() ? (detailTexCoords[0]>=-MLRState::GetMaxUV() && detailTexCoords[0]<=MLRState::GetMaxUV()) : 1);
  143. Verify( MLRState::GetHasMaxUVs() ? (detailTexCoords[1]>=-MLRState::GetMaxUV() && detailTexCoords[1]<=MLRState::GetMaxUV()) : 1);
  144. gos_vertices2uv[numGOSVertices].GOSTransformNoClip(
  145. coords[j],
  146. *mat,
  147. &texCoords[j][0],
  148. &detailTexCoords[0]
  149. #if FOG_HACK
  150. ,state.GetFogMode()
  151. #endif // FOG_HACK
  152. );
  153. #ifdef I_SAY_YES_TO_COLOR
  154. #if COLOR_AS_DWORD
  155. gos_vertices2uv[numGOSVertices].argb = (*actualColors)[j];
  156. #else // COLOR_AS_DWORD
  157. gos_vertices2uv[numGOSVertices].argb = GOSCopyColor(&(*actualColors)[j]);
  158. #endif // COLOR_AS_DWORD
  159. #else // I_SAY_YES_TO_COLOR
  160. gos_vertices2uv[numGOSVertices].argb = 0xffffffff;
  161. #endif // I_SAY_YES_TO_COLOR
  162. if(gos_vertices[j].rhw < fadeDetailEnd)
  163. {
  164. gos_vertices[j].argb &= 0x00ffffff;
  165. }
  166. else
  167. {
  168. if(gos_vertices[j].rhw<fadeDetailStart)
  169. {
  170. gos_vertices[j].argb &= ((Stuff::Truncate_Float_To_Word((gos_vertices[j].rhw - fadeDetailEnd)*fadeMultiplicator)) << 24) | 0x00ffffff;
  171. }
  172. }
  173. }
  174. else
  175. #endif // I_SAY_YES_TO_DETAIL_TEXTURES
  176. {
  177. gos_vertices[numGOSVertices].GOSTransformNoClip(
  178. coords[j],
  179. *mat,
  180. #ifdef I_SAY_YES_TO_TERRAIN
  181. terrainUV
  182. #else // I_SAY_YES_TO_TERRAIN
  183. &texCoords[j][0]
  184. #endif // I_SAY_YES_TO_TERRAIN
  185. #if FOG_HACK
  186. , state.GetFogMode()
  187. #endif // FOG_HACK
  188. );
  189. #ifdef I_SAY_YES_TO_COLOR
  190. #if COLOR_AS_DWORD
  191. gos_vertices[numGOSVertices].argb = (*actualColors)[j];
  192. #else // COLOR_AS_DWORD
  193. gos_vertices[numGOSVertices].argb = GOSCopyColor(&(*actualColors)[j]);
  194. #endif // COLOR_AS_DWORD
  195. #else // I_SAY_YES_TO_COLOR
  196. gos_vertices[numGOSVertices].argb = 0xffffffff;
  197. #endif // I_SAY_YES_TO_COLOR
  198. }
  199. #endif // I_SAY_YES_TO_DUAL_TEXTURES
  200. if(textureAnimation)
  201. {
  202. gos_vertices[numGOSVertices].u += deltaU;
  203. gos_vertices[numGOSVertices].v += deltaV;
  204. }
  205. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  206. for(m=1;m<currentNrOfPasses;m++)
  207. {
  208. extraMultiTexCoords[m][tex2count[m]++] = multiTexCoordsPointers[m][j];
  209. }
  210. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  211. numGOSVertices++;
  212. }
  213. }
  214. #ifdef LAB_ONLY
  215. Set_Statistic(NonClippedVertices, NonClippedVertices+numGOSVertices);
  216. #endif // LAB_ONLY
  217. Check_Object(vt);
  218. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  219. if(MLRState::GetMultitextureLightMap() == false || state.GetMultiTextureMode()==MLRState::MultiTextureOffMode)
  220. {
  221. Verify (tex2count == numGOSVertices);
  222. if(db==false)
  223. {
  224. Verify (vt->GetLast() + 2*numGOSVertices < vt->GetLength());
  225. }
  226. else
  227. {
  228. Verify (2*numGOSVertices < 2*Limits::Max_Number_Vertices_Per_Mesh);
  229. }
  230. if(state2.GetTextureHandle())
  231. {
  232. MLRTexture *texture = (*MLRTexturePool::Instance)[state.GetTextureHandle()];
  233. Verify(texture);
  234. textureAnimation = texture->GetAnimateTexture();
  235. if(textureAnimation)
  236. {
  237. Stuff::AffineMatrix4D &textureMatrix = texture->GetTextureMatrix();
  238. deltaU = textureMatrix(3, 0);
  239. deltaV = textureMatrix(3, 1);
  240. }
  241. }
  242. memcpy(gos_vertices + numGOSVertices, gos_vertices, numGOSVertices * sizeof(GOSVertex));
  243. if(textureAnimation)
  244. {
  245. for(i=0,j=numGOSVertices;i<numGOSVertices;i++,j++)
  246. {
  247. gos_vertices[j].u = (*texCoords2)[i][0] + deltaU;
  248. gos_vertices[j].v = (*texCoords2)[i][1] + deltaV;
  249. }
  250. }
  251. else
  252. {
  253. for(i=0,j=numGOSVertices;i<numGOSVertices;i++,j++)
  254. {
  255. gos_vertices[j].u = (*texCoords2)[i][0];
  256. gos_vertices[j].v = (*texCoords2)[i][1];
  257. }
  258. }
  259. if(db==false)
  260. {
  261. vt->Increase(2*numGOSVertices);
  262. }
  263. }
  264. else
  265. {
  266. if(db==false)
  267. {
  268. vt->Increase2UV(numGOSVertices);
  269. }
  270. }
  271. #else // I_SAY_YES_TO_DUAL_TEXTURES
  272. #ifdef I_SAY_YES_TO_DETAIL_TEXTURES
  273. if( gEnableDetailTexture==1)
  274. {
  275. if(MLRState::GetMultitextureLightMap() == false || state.GetMultiTextureMode()==MLRState::MultiTextureOffMode )
  276. {
  277. if(db==false)
  278. {
  279. Verify (vt->GetLast() + 2*numGOSVertices < vt->GetLength());
  280. }
  281. else
  282. {
  283. Verify (2*numGOSVertices < 2*Limits::Max_Number_Vertices_Per_Mesh );
  284. }
  285. memcpy(gos_vertices + numGOSVertices, gos_vertices, numGOSVertices * sizeof(GOSVertex));
  286. stride = 0;
  287. for(i=0,j=numGOSVertices;i<numGOSVertices;i++,j++)
  288. {
  289. #ifdef I_SAY_YES_TO_TERRAIN2
  290. gos_vertices[j].u = gos_vertices[i].u*xScale;
  291. gos_vertices[j].v = gos_vertices[i].v*yScale;
  292. #else
  293. gos_vertices[j].u = gos_vertices[i].u*xScale + xOffset;
  294. gos_vertices[j].v = gos_vertices[i].v*yScale + yOffset;
  295. #endif
  296. Verify( MLRState::GetHasMaxUVs() ? (gos_vertices[j].u>=-MLRState::GetMaxUV() && gos_vertices[j].u<=MLRState::GetMaxUV()) : 1);
  297. Verify( MLRState::GetHasMaxUVs() ? (gos_vertices[j].v>=-MLRState::GetMaxUV() && gos_vertices[j].v<=MLRState::GetMaxUV()) : 1);
  298. if(gos_vertices[j].rhw < fadeDetailEnd)
  299. {
  300. gos_vertices[j].argb &= 0x00ffffff;
  301. }
  302. else
  303. {
  304. stride++;
  305. if(gos_vertices[j].rhw<fadeDetailStart)
  306. {
  307. gos_vertices[j].argb &= ((Stuff::Truncate_Float_To_Word((gos_vertices[j].rhw - fadeDetailEnd)*fadeMultiplicator)) << 24) | 0x00ffffff;
  308. }
  309. }
  310. }
  311. if(stride==0)
  312. {
  313. detTextureVisible=false;
  314. }
  315. else
  316. {
  317. detTextureVisible=true;
  318. }
  319. if(db==false)
  320. {
  321. vt->Increase(2*numGOSVertices);
  322. }
  323. }
  324. else
  325. {
  326. if(db==false)
  327. {
  328. vt->Increase2UV(numGOSVertices);
  329. }
  330. }
  331. }
  332. else
  333. {
  334. if(db==false)
  335. {
  336. vt->Increase(numGOSVertices);
  337. }
  338. }
  339. #else // I_SAY_YES_TO_DETAIL_TEXTURES
  340. if(db==false)
  341. {
  342. vt->Increase(numGOSVertices);
  343. }
  344. #endif // I_SAY_YES_TO_DETAIL_TEXTURES
  345. #endif // I_SAY_YES_TO_DUAL_TEXTURES
  346. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  347. for(m=1;m<currentNrOfPasses;m++)
  348. {
  349. Verify (tex2count[m] == numGOSVertices);
  350. if(db==false)
  351. {
  352. Verify (vt->GetLast() + numGOSVertices < vt->GetLength());
  353. }
  354. else
  355. {
  356. Verify (numGOSVertices < 2*Limits::Max_Number_Vertices_Per_Mesh);
  357. }
  358. memcpy(gos_vertices + numGOSVertices, gos_vertices, numGOSVertices * sizeof(GOSVertex));
  359. for(i=0,j=numGOSVertices;i<numGOSVertices;i++,j++)
  360. {
  361. gos_vertices[j].u = extraMultiTexCoords[m][i][0];
  362. gos_vertices[j].v = extraMultiTexCoords[m][i][1];
  363. }
  364. if(db==false)
  365. {
  366. vt->Increase(numGOSVertices);
  367. }
  368. }
  369. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  370. gos_indices = vt->GetActualIndexPool(db);
  371. numGOSIndices = 0;
  372. int ngi = 0;
  373. for(i=0,j=0;i<numOfTriangles;j+=3,++i)
  374. {
  375. if(testList[i] == 0)
  376. {
  377. continue;
  378. }
  379. if(db==false)
  380. {
  381. Verify((vt->GetLastIndex() + 3 + numGOSIndices) < vt->GetLength());
  382. }
  383. else
  384. {
  385. Verify(3 + numGOSIndices < 2*Limits::Max_Number_Vertices_Per_Mesh);
  386. }
  387. Verify(indexOffset[index[j]] < numGOSVertices);
  388. gos_indices[ngi++] = indexOffset[index[j]];
  389. Verify(indexOffset[index[j+2]] < numGOSVertices);
  390. gos_indices[ngi++] = indexOffset[index[j+2]];
  391. Verify(indexOffset[index[j+1]] < numGOSVertices);
  392. gos_indices[ngi++] = indexOffset[index[j+1]];
  393. }
  394. numGOSIndices = (unsigned short)ngi;
  395. Check_Object(vt);
  396. if(db==false)
  397. {
  398. vt->IncreaseIndex(numGOSIndices);
  399. }
  400. visible = numGOSVertices ? (unsigned char)1 : (unsigned char)0;
  401. Stop_Timer(Transform_Time);
  402. }
  403. static MLRClippingState theAnd, theOr, theTest;
  404. // extern void _stdcall CheckVertices( gos_VERTEX* pVertexArray, DWORD NumberVertices, bool PointsLines );
  405. // extern void _stdcall CheckVertices1( gos_VERTEX_2UV* pVertexArray, DWORD NumberVertices );
  406. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  407. // Now it gets serious
  408. int
  409. CLASSNAME::TransformAndClip(Matrix4D *mat, MLRClippingState clippingFlags, GOSVertexPool *vt, bool db)
  410. {
  411. Check_Object(this);
  412. Start_Timer(Transform_Time);
  413. Verify(numOfTriangles == testList.GetLength());
  414. Verify(clippingFlags.GetClippingState() != 0);
  415. //
  416. //--------------------------------------
  417. // See if we don't have to draw anything
  418. //--------------------------------------
  419. //
  420. if(numOfTriangles <= 0)
  421. {
  422. visible = 0;
  423. Stop_Timer(Transform_Time);
  424. return visible;
  425. }
  426. Verify(index.GetLength() > 0);
  427. unsigned short l;
  428. int i, j, k, ret = 0;
  429. int len = coords.GetLength();
  430. if(visibleIndexedVerticesKey == false)
  431. {
  432. FindVisibleVertices();
  433. }
  434. Stuff::Vector4D *v4d = transformedCoords->GetData();
  435. Stuff::Point3D *p3d = coords.GetData();
  436. int *cs = (int *)clipPerVertex->GetData();
  437. unsigned char *viv = visibleIndexedVertices.GetData();
  438. for(i=0;i<len;i++,p3d++,v4d++,cs++,viv++)
  439. {
  440. if(*viv == 0)
  441. {
  442. continue;
  443. }
  444. *viv = 0;
  445. v4d->MultiplySetClip(*p3d, *mat, cs);
  446. #ifdef LAB_ONLY
  447. Set_Statistic(TransformedVertices, TransformedVertices+1);
  448. #endif // LAB_ONLY
  449. //
  450. //--------------------------------------------------------
  451. // I claims all vertices are in. lets check it. who knows
  452. //--------------------------------------------------------
  453. //
  454. #ifdef LAB_ONLY
  455. if( (*cs)==0)
  456. {
  457. #if defined(_ARMOR)
  458. if(ArmorLevel > 3)
  459. {
  460. //
  461. //--------------------------------------------------------
  462. // I claims all vertices are in. lets check it. who knows
  463. //--------------------------------------------------------
  464. //
  465. Verify(v4d->x >= 0.0f && v4d->x <= v4d->w );
  466. Verify(v4d->y >= 0.0f && v4d->y <= v4d->w );
  467. Verify(v4d->z >= 0.0f && v4d->z <= v4d->w );
  468. }
  469. #endif // _ARMOR
  470. Set_Statistic(NonClippedVertices, NonClippedVertices+1);
  471. }
  472. else
  473. {
  474. Set_Statistic(ClippedVertices, ClippedVertices+1);
  475. }
  476. #endif // LAB_ONLY
  477. }
  478. Stop_Timer(Transform_Time);
  479. Start_Timer(Clipping_Time);
  480. int mask, k0, k1, ct=0;
  481. Scalar a=0.0f;
  482. bool textureAnimation = false;
  483. Scalar deltaU=0.0f, deltaV=0.0f;
  484. if(state.GetTextureHandle())
  485. {
  486. MLRTexture *texture = (*MLRTexturePool::Instance)[state.GetTextureHandle()];
  487. Check_Object(texture);
  488. textureAnimation = texture->GetAnimateTexture();
  489. if(textureAnimation)
  490. {
  491. Stuff::AffineMatrix4D &textureMatrix = texture->GetTextureMatrix();
  492. deltaU = textureMatrix(3, 0);
  493. deltaV = textureMatrix(3, 1);
  494. }
  495. }
  496. #if defined(I_SAY_YES_TO_DUAL_TEXTURES) || defined(I_SAY_YES_TO_DETAIL_TEXTURES)
  497. gos_vertices2uv = vt->GetActualVertexPool2UV(db);
  498. bool textureAnimation2 = false;
  499. Scalar deltaU2=0.0f, deltaV2=0.0f;
  500. if(state2.GetTextureHandle())
  501. {
  502. MLRTexture *texture = (*MLRTexturePool::Instance)[state2.GetTextureHandle()];
  503. Verify(texture);
  504. textureAnimation2 = texture->GetAnimateTexture();
  505. if(textureAnimation2)
  506. {
  507. Stuff::AffineMatrix4D &textureMatrix = texture->GetTextureMatrix();
  508. deltaU2 = textureMatrix(3, 0);
  509. deltaV2 = textureMatrix(3, 1);
  510. }
  511. }
  512. #endif // I_SAY_YES_TO_DUAL_TEXTURES || I_SAY_YES_TO_DETAIL_TEXTURES
  513. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  514. int tex2count = 0;
  515. int numVertices = GetNumVertices();
  516. #endif // I_SAY_YES_TO_DUAL_TEXTURES
  517. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  518. int m;
  519. gos_PushCurrentHeap(StaticHeap);
  520. int tex2count[Limits::Max_Number_Of_Multitextures];
  521. gos_PopCurrentHeap();
  522. for(m=0;m<currentNrOfPasses;m++)
  523. {
  524. tex2count[m] = 0;
  525. }
  526. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  527. int myNumberUsedClipVertex, myNumberUsedClipIndex, myNumberUsedClipLength;
  528. myNumberUsedClipVertex = 0;
  529. myNumberUsedClipIndex = 0;
  530. myNumberUsedClipLength = 0;
  531. Verify(index.GetLength() > 0);
  532. //
  533. //------------------------
  534. // Handle the indexed case
  535. //------------------------
  536. //
  537. //
  538. //-----------------------------------------------------------------
  539. // Step through each polygon, making sure that we don't try to clip
  540. // backfaced polygons
  541. //-----------------------------------------------------------------
  542. //
  543. cs = (int *)clipPerVertex->GetData();
  544. int index0, index1, index2;
  545. for(i=0,j=0;i<numOfTriangles;j+=3,++i)
  546. {
  547. // if(testList[i] == 0)
  548. // {
  549. // continue;
  550. // }
  551. index0 = index[j];
  552. index1 = index[j+1];
  553. index2 = index[j+2];
  554. //
  555. //---------------------------------------------------------------
  556. // Test each vertex of the polygon against the allowed clipping
  557. // planes, and accumulate status for which planes always clip and
  558. // which planes clipped at least once
  559. //---------------------------------------------------------------
  560. //
  561. theAnd = cs[index0];
  562. theOr = theAnd;
  563. theAnd &= (cs[index1] & cs[index2]);
  564. theOr |= (cs[index1] | cs[index2]);
  565. //
  566. //-------------------------------------------------------------------
  567. // If any bit is set for all vertices, then the polygon is completely
  568. // outside the viewing space and we don't have to draw it. On the
  569. // other hand, if no bits at all were ever set, we can do a trivial
  570. // accept of the polygon
  571. //-------------------------------------------------------------------
  572. //
  573. theAnd = theOr = 0; //ASSUME NO CLIPPING NEEDED FOR MC2. Its just not done here!
  574. if (theAnd != 0)
  575. {
  576. testList[i] = 0;
  577. #ifdef LAB_ONLY
  578. Set_Statistic(PolysClippedButOutside, PolysClippedButOutside+1);
  579. #endif // LAB_ONLY
  580. }
  581. else if (theOr == 0)
  582. {
  583. ret++;
  584. visibleIndexedVertices[index0] = 1;
  585. visibleIndexedVertices[index1] = 1;
  586. visibleIndexedVertices[index2] = 1;
  587. #ifdef I_SAY_YES_TO_TERRAIN
  588. (*clipTexCoords)[index0][0] = borderPixelFun + (coords[index0].x - minX)*xUVFac;
  589. (*clipTexCoords)[index0][1] = borderPixelFun + (coords[index0].z - minZ)*zUVFac;
  590. (*clipTexCoords)[index1][0] = borderPixelFun + (coords[index1].x - minX)*xUVFac;
  591. (*clipTexCoords)[index1][1] = borderPixelFun + (coords[index1].z - minZ)*zUVFac;
  592. (*clipTexCoords)[index2][0] = borderPixelFun + (coords[index2].x - minX)*xUVFac;
  593. (*clipTexCoords)[index2][1] = borderPixelFun + (coords[index2].z - minZ)*zUVFac;
  594. #endif // I_SAY_YES_TO_TERRAIN
  595. #ifdef LAB_ONLY
  596. Set_Statistic(PolysClippedButInside, PolysClippedButInside+1);
  597. #endif // LAB_ONLY
  598. }
  599. //
  600. //-----------------------------------------------------------------
  601. // It is not a trivial case, so we must now do real clipping on the
  602. // polygon
  603. //-----------------------------------------------------------------
  604. //
  605. else
  606. {
  607. unsigned short numberVerticesPerPolygon = 0;
  608. #ifdef I_SAY_YES_TO_TERRAIN
  609. (*clipTexCoords)[index0][0] = borderPixelFun + (coords[index0].x - minX)*xUVFac;
  610. (*clipTexCoords)[index0][1] = borderPixelFun + (coords[index0].z - minZ)*zUVFac;
  611. (*clipTexCoords)[index1][0] = borderPixelFun + (coords[index1].x - minX)*xUVFac;
  612. (*clipTexCoords)[index1][1] = borderPixelFun + (coords[index1].z - minZ)*zUVFac;
  613. (*clipTexCoords)[index2][0] = borderPixelFun + (coords[index2].x - minX)*xUVFac;
  614. (*clipTexCoords)[index2][1] = borderPixelFun + (coords[index2].z - minZ)*zUVFac;
  615. #endif // I_SAY_YES_TO_TERRAIN
  616. //
  617. //---------------------------------------------------------------
  618. // Handle the case of a single clipping plane by stepping through
  619. // the vertices and finding the edge it originates
  620. //---------------------------------------------------------------
  621. //
  622. bool firstIsIn;
  623. if (theOr.GetNumberOfSetBits() == 1)
  624. {
  625. #ifdef LAB_ONLY
  626. Set_Statistic(PolysClippedButOnePlane, PolysClippedButOnePlane+1);
  627. #endif // LAB_ONLY
  628. for(k=j;k<j+3;k++)
  629. {
  630. k0 = index[k];
  631. k1 = index[(k+1) < j+3 ? k+1 : j];
  632. //
  633. //----------------------------------------------------
  634. // If this vertex is inside the viewing space, copy it
  635. // directly to the clipping buffer
  636. //----------------------------------------------------
  637. //
  638. int clipped_index =
  639. myNumberUsedClipVertex + numberVerticesPerPolygon;
  640. theTest = cs[k0];
  641. if(theTest == 0)
  642. {
  643. firstIsIn = true;
  644. (*clipExtraCoords)[clipped_index] = (*transformedCoords)[k0];
  645. #ifdef I_SAY_YES_TO_COLOR
  646. Verify((*actualColors).GetLength() > 0);
  647. (*clipExtraColors)[clipped_index] = (*actualColors)[k0];
  648. #endif // I_SAY_YES_TO_COLOR
  649. #ifdef I_SAY_YES_TO_TERRAIN
  650. (*clipExtraTexCoords)[clipped_index] = (*clipTexCoords)[k0];
  651. #else // I_SAY_YES_TO_TERRAIN
  652. Verify(texCoords.GetLength() > 0);
  653. (*clipExtraTexCoords)[clipped_index] = texCoords[k0];
  654. #endif // I_SAY_YES_TO_TERRAIN
  655. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  656. (*clipExtraTexCoords2)[clipped_index] = texCoords[k0+numVertices];
  657. #endif
  658. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  659. for(m=1;m<currentNrOfPasses;m++)
  660. {
  661. clipExtraMultiTexCoords[m][clipped_index] = multiTexCoordsPointers[m][k0];
  662. }
  663. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  664. numberVerticesPerPolygon++;
  665. clipped_index++;
  666. //
  667. //-------------------------------------------------------
  668. // We don't need to clip this edge if the next vertex is
  669. // also in the viewing space, so just move on to the next
  670. // vertex
  671. //-------------------------------------------------------
  672. //
  673. if(cs[k1] == 0)
  674. {
  675. continue;
  676. }
  677. }
  678. //
  679. //---------------------------------------------------------
  680. // This vertex is outside the viewing space, so if the next
  681. // vertex is also outside the viewing space, no clipping is
  682. // needed and we throw this vertex away. Since only one
  683. // clipping plane is involved, it must be in the same space
  684. // as the first vertex
  685. //---------------------------------------------------------
  686. //
  687. else
  688. {
  689. firstIsIn = false;
  690. if(cs[k1] != 0)
  691. {
  692. Verify(cs[k1] == cs[k0]);
  693. continue;
  694. }
  695. }
  696. //
  697. //--------------------------------------------------
  698. // We now find the distance along the edge where the
  699. // clipping plane will intersect
  700. //--------------------------------------------------
  701. //
  702. mask = 1;
  703. theTest |= cs[k1];
  704. //
  705. //-----------------------------------------------------
  706. // Find the boundary conditions that match our clipping
  707. // plane
  708. //-----------------------------------------------------
  709. //
  710. for (l=0; l<MLRClippingState::NextBit; l++)
  711. {
  712. if(theTest.IsClipped(mask))
  713. {
  714. // GetDoubleBC(l, bc0, bc1, transformedCoords[k0], transformedCoords[k1]);
  715. //
  716. //-------------------------------------------
  717. // Find the clipping interval from bc0 to bc1
  718. //-------------------------------------------
  719. //
  720. if(firstIsIn==true)
  721. {
  722. a = GetLerpFactor(l, (*transformedCoords)[k0], (*transformedCoords)[k1]);
  723. }
  724. else
  725. {
  726. a = GetLerpFactor(l, (*transformedCoords)[k1], (*transformedCoords)[k0]);
  727. }
  728. Verify(a >= 0.0f && a <= 1.0f);
  729. ct = l;
  730. break;
  731. }
  732. mask <<= 1;
  733. }
  734. //
  735. //------------------------------
  736. // Lerp the homogeneous position
  737. //------------------------------
  738. //
  739. if(firstIsIn==true)
  740. {
  741. (*clipExtraCoords)[clipped_index].Lerp(
  742. (*transformedCoords)[k0],
  743. (*transformedCoords)[k1],
  744. a
  745. );
  746. DoClipTrick((*clipExtraCoords)[clipped_index], ct);
  747. //
  748. //----------------------------------------------------------
  749. // If there are colors, lerp them in screen space for now as
  750. // most cards do that anyway
  751. //----------------------------------------------------------
  752. //
  753. #ifdef I_SAY_YES_TO_COLOR
  754. Verify((*actualColors).GetLength() > 0);
  755. #if COLOR_AS_DWORD
  756. (*clipExtraColors)[clipped_index] = Color_DWORD_Lerp (
  757. (*actualColors)[k0],
  758. (*actualColors)[k1],
  759. a
  760. );
  761. #else // COLOR_AS_DWORD
  762. (*clipExtraColors)[clipped_index].Lerp(
  763. (*actualColors)[k0],
  764. (*actualColors)[k1],
  765. a
  766. );
  767. #endif // COLOR_AS_DWORD
  768. #endif // I_SAY_YES_TO_COLOR
  769. //
  770. //-----------------------------------------------------
  771. // If there are texture uv's, we need to lerp them in a
  772. // perspective correct manner
  773. //-----------------------------------------------------
  774. //
  775. #ifdef I_SAY_YES_TO_TERRAIN
  776. (*clipExtraTexCoords)[clipped_index].Lerp
  777. (
  778. (*clipTexCoords)[k0],
  779. (*clipTexCoords)[k1],
  780. a
  781. );
  782. #else // I_SAY_YES_TO_TERRAIN
  783. Verify(texCoords.GetLength() > 0);
  784. (*clipExtraTexCoords)[clipped_index].Lerp
  785. (
  786. texCoords[k0],
  787. texCoords[k1],
  788. a
  789. );
  790. #endif // I_SAY_YES_TO_TERRAIN
  791. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  792. (*clipExtraTexCoords2)[clipped_index].Lerp
  793. (
  794. texCoords[k0+numVertices],
  795. texCoords[k1+numVertices],
  796. a
  797. );
  798. #endif // I_SAY_YES_TO_DUAL_TEXTURES
  799. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  800. for(m=1;m<currentNrOfPasses;m++)
  801. {
  802. (*clipExtraMultiTexCoords)[m][clipped_index] .Lerp
  803. (
  804. multiTexCoordsPointers[m][k0],
  805. multiTexCoordsPointers[m][k1],
  806. a
  807. );
  808. }
  809. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  810. }
  811. else
  812. {
  813. (*clipExtraCoords)[clipped_index].Lerp(
  814. (*transformedCoords)[k1],
  815. (*transformedCoords)[k0],
  816. a
  817. );
  818. DoClipTrick((*clipExtraCoords)[clipped_index], ct);
  819. //
  820. //----------------------------------------------------------
  821. // If there are colors, lerp them in screen space for now as
  822. // most cards do that anyway
  823. //----------------------------------------------------------
  824. //
  825. #ifdef I_SAY_YES_TO_COLOR
  826. Verify((*actualColors).GetLength() > 0);
  827. #if COLOR_AS_DWORD
  828. (*clipExtraColors)[clipped_index] = Color_DWORD_Lerp (
  829. (*actualColors)[k1],
  830. (*actualColors)[k0],
  831. a
  832. );
  833. #else // COLOR_AS_DWORD
  834. (*clipExtraColors)[clipped_index].Lerp(
  835. (*actualColors)[k1],
  836. (*actualColors)[k0],
  837. a
  838. );
  839. #endif // COLOR_AS_DWORD
  840. #endif // I_SAY_YES_TO_COLOR
  841. //
  842. //-----------------------------------------------------
  843. // If there are texture uv's, we need to lerp them in a
  844. // perspective correct manner
  845. //-----------------------------------------------------
  846. //
  847. #ifdef I_SAY_YES_TO_TERRAIN
  848. (*clipExtraTexCoords)[clipped_index].Lerp
  849. (
  850. (*clipTexCoords)[k1],
  851. (*clipTexCoords)[k0],
  852. a
  853. );
  854. #else // I_SAY_YES_TO_TERRAIN
  855. Verify(texCoords.GetLength() > 0);
  856. (*clipExtraTexCoords)[clipped_index].Lerp
  857. (
  858. texCoords[k1],
  859. texCoords[k0],
  860. a
  861. );
  862. #endif // I_SAY_YES_TO_TERRAIN
  863. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  864. (*clipExtraTexCoords2)[clipped_index].Lerp
  865. (
  866. texCoords[k1+numVertices],
  867. texCoords[k0+numVertices],
  868. a
  869. );
  870. #endif // I_SAY_YES_TO_DUAL_TEXTURES
  871. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  872. for(m=1;m<currentNrOfPasses;m++)
  873. {
  874. (*clipExtraMultiTexCoords)[m][clipped_index] .Lerp
  875. (
  876. multiTexCoordsPointers[m][k1],
  877. multiTexCoordsPointers[m][k0],
  878. a
  879. );
  880. }
  881. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  882. }
  883. //
  884. //--------------------------------
  885. // Bump the polygon's vertex count
  886. //--------------------------------
  887. //
  888. numberVerticesPerPolygon++;
  889. }
  890. (*clipExtraLength)[myNumberUsedClipLength] = numberVerticesPerPolygon;
  891. #ifdef _ARMOR
  892. (*clipExtraLength)[myNumberUsedClipLength] &= ~0x8000;
  893. #endif // _ARMOR
  894. }
  895. //
  896. //---------------------------------------------------------------
  897. // We have to handle multiple planes. We do this by creating two
  898. // buffers and we switch between them as we clip plane by plane
  899. //---------------------------------------------------------------
  900. //
  901. else
  902. {
  903. #ifdef LAB_ONLY
  904. Set_Statistic(PolysClippedButGOnePlane, PolysClippedButGOnePlane+1);
  905. #endif // LAB_ONLY
  906. ClipData2 srcPolygon, dstPolygon;
  907. int dstBuffer = 1;
  908. #ifndef I_SAY_YES_TO_TERRAIN
  909. Verify(texCoords.GetLength() > 0);
  910. #endif // I_SAY_YES_TO_TERRAIN
  911. srcPolygon.coords = clipBuffer[dstBuffer].coords.GetData();
  912. #ifdef I_SAY_YES_TO_COLOR
  913. Verify((*actualColors).GetLength() > 0);
  914. srcPolygon.colors = clipBuffer[dstBuffer].colors.GetData();
  915. #endif // I_SAY_YES_TO_COLOR
  916. srcPolygon.texCoords = clipBuffer[dstBuffer].texCoords.GetData();
  917. srcPolygon.clipPerVertex = clipBuffer[dstBuffer].clipPerVertex.GetData();
  918. //
  919. //----------------------------------------------------------
  920. // unravel and copy the original data into the source buffer
  921. //----------------------------------------------------------
  922. //
  923. for(k=j,l=0;k<j+3;k++,l++)
  924. {
  925. int indexK = index[k];
  926. srcPolygon.coords[l] = (*transformedCoords)[indexK];
  927. #ifdef I_SAY_YES_TO_COLOR
  928. srcPolygon.colors[l] = (*actualColors)[indexK];
  929. #endif // I_SAY_YES_TO_COLOR
  930. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  931. srcPolygon.texCoords[2*l] = texCoords[indexK];
  932. srcPolygon.texCoords[2*l+1] = texCoords[indexK + numVertices];
  933. #else // I_SAY_YES_TO_DUAL_TEXTURES
  934. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  935. for(m=0;m<currentNrOfPasses;m++)
  936. {
  937. srcPolygon.texCoords[currentNrOfPasses*l+m] = multiTexCoordsPointers[m][indexK];
  938. }
  939. #else // I_SAY_YES_TO_MULTI_TEXTURES
  940. #ifdef I_SAY_YES_TO_TERRAIN
  941. srcPolygon.texCoords[l] = (*clipTexCoords)[indexK];
  942. #else // I_SAY_YES_TO_TERRAIN
  943. srcPolygon.texCoords[l] = texCoords[indexK];
  944. #endif // I_SAY_YES_TO_TERRAIN
  945. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  946. #endif // I_SAY_YES_TO_DUAL_TEXTURES
  947. srcPolygon.clipPerVertex[l] = (*clipPerVertex)[indexK];
  948. }
  949. srcPolygon.length = l;
  950. //
  951. //--------------------------------
  952. // Point to the destination buffer
  953. //--------------------------------
  954. //
  955. dstBuffer = 0;
  956. dstPolygon.coords = clipBuffer[dstBuffer].coords.GetData();
  957. #ifdef I_SAY_YES_TO_COLOR
  958. dstPolygon.colors = clipBuffer[dstBuffer].colors.GetData();
  959. #endif // I_SAY_YES_TO_COLOR
  960. dstPolygon.texCoords = clipBuffer[dstBuffer].texCoords.GetData();
  961. dstPolygon.clipPerVertex = clipBuffer[dstBuffer].clipPerVertex.GetData();
  962. dstPolygon.length = 0;
  963. //
  964. //-----------------------------------------------------------
  965. // Spin through each plane that clipped the primitive and use
  966. // it to actually clip the primitive
  967. //-----------------------------------------------------------
  968. //
  969. mask = 1;
  970. MLRClippingState theNewOr(0);
  971. int loop = 4;
  972. #if HUNT_CLIP_ERROR
  973. for(k=0;k<srcPolygon.length;k++)
  974. {
  975. DEBUG_STREAM << setiosflags( ios::scientific) << setprecision(20)
  976. << srcPolygon.coords[k].x << " "
  977. << srcPolygon.coords[k].y << " "
  978. << srcPolygon.coords[k].z << " "
  979. << srcPolygon.coords[k].w << '\n';
  980. }
  981. DEBUG_STREAM << "TheOriginalOR: " << hex << theOr.GetClippingState() << dec << '\n';
  982. #endif // HUNT_CLIP_ERROR
  983. do
  984. {
  985. for(l=0; l<MLRClippingState::NextBit; l++)
  986. {
  987. if(theOr.IsClipped(mask))
  988. {
  989. //
  990. //-----------------------------------
  991. // Clip each vertex against the plane
  992. //-----------------------------------
  993. //
  994. #if HUNT_CLIP_ERROR
  995. DEBUG_STREAM << l << ": " << '\n';
  996. #endif // HUNT_CLIP_ERROR
  997. for(k=0;k<srcPolygon.length;k++)
  998. {
  999. k1 = (k+1) < srcPolygon.length ? k+1 : 0;
  1000. theTest = srcPolygon.clipPerVertex[k];
  1001. //
  1002. //----------------------------------------------------
  1003. // If this vertex is inside the viewing space, copy it
  1004. // directly to the clipping buffer
  1005. //----------------------------------------------------
  1006. //
  1007. if(theTest.IsClipped(mask) == 0)
  1008. {
  1009. firstIsIn = true;
  1010. dstPolygon.coords[dstPolygon.length] =
  1011. srcPolygon.coords[k];
  1012. #if HUNT_CLIP_ERROR
  1013. DEBUG_STREAM << k << " goes " << setiosflags( ios::scientific) << setprecision(20)
  1014. << srcPolygon.coords[k].x << " "
  1015. << srcPolygon.coords[k].y << " "
  1016. << srcPolygon.coords[k].z << " "
  1017. << srcPolygon.coords[k].w << '\n';
  1018. #endif // HUNT_CLIP_ERROR
  1019. dstPolygon.clipPerVertex[dstPolygon.length] =
  1020. srcPolygon.clipPerVertex[k];
  1021. #ifdef I_SAY_YES_TO_COLOR
  1022. dstPolygon.colors[dstPolygon.length] =
  1023. srcPolygon.colors[k];
  1024. #endif // I_SAY_YES_TO_COLOR
  1025. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  1026. dstPolygon.texCoords[2*dstPolygon.length] =
  1027. srcPolygon.texCoords[2*k];
  1028. dstPolygon.texCoords[2*dstPolygon.length+1] =
  1029. srcPolygon.texCoords[2*k+1];
  1030. #else // I_SAY_YES_TO_DUAL_TEXTURES
  1031. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  1032. for(m=0;m<currentNrOfPasses;m++)
  1033. {
  1034. dstPolygon.texCoords[currentNrOfPasses*dstPolygon.length+m] =
  1035. srcPolygon.texCoords[currentNrOfPasses*k+m];
  1036. }
  1037. #else // I_SAY_YES_TO_MULTI_TEXTURES
  1038. dstPolygon.texCoords[dstPolygon.length] =
  1039. srcPolygon.texCoords[k];
  1040. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  1041. #endif // I_SAY_YES_TO_DUAL_TEXTURES
  1042. dstPolygon.length++;
  1043. //
  1044. //-------------------------------------------------------
  1045. // We don't need to clip this edge if the next vertex is
  1046. // also in the viewing space, so just move on to the next
  1047. // vertex
  1048. //-------------------------------------------------------
  1049. //
  1050. if(srcPolygon.clipPerVertex[k1].IsClipped(mask) == 0)
  1051. {
  1052. continue;
  1053. }
  1054. }
  1055. //
  1056. //---------------------------------------------------------
  1057. // This vertex is outside the viewing space, so if the next
  1058. // vertex is also outside the viewing space, no clipping is
  1059. // needed and we throw this vertex away. Since only one
  1060. // clipping plane is involved, it must be in the same space
  1061. // as the first vertex
  1062. //---------------------------------------------------------
  1063. //
  1064. else
  1065. {
  1066. firstIsIn = false;
  1067. if(srcPolygon.clipPerVertex[k1].IsClipped(mask) != 0)
  1068. {
  1069. Verify(
  1070. srcPolygon.clipPerVertex[k1].IsClipped(mask)
  1071. == srcPolygon.clipPerVertex[k].IsClipped(mask)
  1072. );
  1073. continue;
  1074. }
  1075. }
  1076. //
  1077. //-------------------------------------------
  1078. // Find the clipping interval from bc0 to bc1
  1079. //-------------------------------------------
  1080. //
  1081. if(firstIsIn == true)
  1082. {
  1083. a = GetLerpFactor (l, srcPolygon.coords[k], srcPolygon.coords[k1]);
  1084. Verify(a >= 0.0f && a <= 1.0f);
  1085. //
  1086. //------------------------------
  1087. // Lerp the homogeneous position
  1088. //------------------------------
  1089. //
  1090. dstPolygon.coords[dstPolygon.length].Lerp(
  1091. srcPolygon.coords[k],
  1092. srcPolygon.coords[k1],
  1093. a
  1094. );
  1095. #if HUNT_CLIP_ERROR
  1096. DEBUG_STREAM << "True " << a << " " << k << " " << k1 << " we get " << dstPolygon.length << '\n';
  1097. DEBUG_STREAM << setiosflags( ios::scientific) << setprecision(20)
  1098. << dstPolygon.coords[dstPolygon.length].x << " "
  1099. << dstPolygon.coords[dstPolygon.length].y << " "
  1100. << dstPolygon.coords[dstPolygon.length].z << " "
  1101. << dstPolygon.coords[dstPolygon.length].w << '\n';
  1102. #endif // HUNT_CLIP_ERROR
  1103. DoClipTrick(dstPolygon.coords[dstPolygon.length], l);
  1104. //
  1105. //----------------------------------------------------------
  1106. // If there are colors, lerp them in screen space for now as
  1107. // most cards do that anyway
  1108. //----------------------------------------------------------
  1109. //
  1110. #ifdef I_SAY_YES_TO_COLOR
  1111. #if COLOR_AS_DWORD
  1112. dstPolygon.colors[dstPolygon.length] = Color_DWORD_Lerp(
  1113. srcPolygon.colors[k],
  1114. srcPolygon.colors[k1],
  1115. a
  1116. );
  1117. #else // COLOR_AS_DWORD
  1118. dstPolygon.colors[dstPolygon.length].Lerp(
  1119. srcPolygon.colors[k],
  1120. srcPolygon.colors[k1],
  1121. a
  1122. );
  1123. #endif // COLOR_AS_DWORD
  1124. #endif // I_SAY_YES_TO_COLOR
  1125. //
  1126. //-----------------------------------------------------
  1127. // If there are texture uv's, we need to lerp them in a
  1128. // perspective correct manner
  1129. //-----------------------------------------------------
  1130. //
  1131. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  1132. dstPolygon.texCoords[2*dstPolygon.length].Lerp
  1133. (
  1134. srcPolygon.texCoords[2*k],
  1135. srcPolygon.texCoords[2*k1],
  1136. a
  1137. );
  1138. dstPolygon.texCoords[2*dstPolygon.length+1].Lerp
  1139. (
  1140. srcPolygon.texCoords[2*k+1],
  1141. srcPolygon.texCoords[2*k1+1],
  1142. a
  1143. );
  1144. #else // I_SAY_YES_TO_DUAL_TEXTURES
  1145. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  1146. for(m=0;m<currentNrOfPasses;m++)
  1147. {
  1148. dstPolygon.texCoords[currentNrOfPasses*dstPolygon.length+m].Lerp
  1149. (
  1150. srcPolygon.texCoords[currentNrOfPasses*k+m],
  1151. srcPolygon.texCoords[currentNrOfPasses*k1+m],
  1152. a
  1153. );
  1154. }
  1155. #else // I_SAY_YES_TO_MULTI_TEXTURES
  1156. dstPolygon.texCoords[dstPolygon.length].Lerp
  1157. (
  1158. srcPolygon.texCoords[k],
  1159. srcPolygon.texCoords[k1],
  1160. a
  1161. );
  1162. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  1163. #endif // I_SAY_YES_TO_DUAL_TEXTURES
  1164. }
  1165. else
  1166. {
  1167. a = GetLerpFactor (l, srcPolygon.coords[k1], srcPolygon.coords[k]);
  1168. Verify(a >= 0.0f && a <= 1.0f);
  1169. //
  1170. //------------------------------
  1171. // Lerp the homogeneous position
  1172. //------------------------------
  1173. //
  1174. dstPolygon.coords[dstPolygon.length].Lerp(
  1175. srcPolygon.coords[k1],
  1176. srcPolygon.coords[k],
  1177. a
  1178. );
  1179. #if HUNT_CLIP_ERROR
  1180. DEBUG_STREAM << "False " << a << " " << k << " " << k1 << " we get " << dstPolygon.length << '\n';
  1181. DEBUG_STREAM << setiosflags( ios::scientific) << setprecision(20)
  1182. << dstPolygon.coords[dstPolygon.length].x << " "
  1183. << dstPolygon.coords[dstPolygon.length].y << " "
  1184. << dstPolygon.coords[dstPolygon.length].z << " "
  1185. << dstPolygon.coords[dstPolygon.length].w << '\n';
  1186. #endif // HUNT_CLIP_ERROR
  1187. DoClipTrick(dstPolygon.coords[dstPolygon.length], l);
  1188. //
  1189. //----------------------------------------------------------
  1190. // If there are colors, lerp them in screen space for now as
  1191. // most cards do that anyway
  1192. //----------------------------------------------------------
  1193. //
  1194. #ifdef I_SAY_YES_TO_COLOR
  1195. #if COLOR_AS_DWORD
  1196. dstPolygon.colors[dstPolygon.length] = Color_DWORD_Lerp(
  1197. srcPolygon.colors[k1],
  1198. srcPolygon.colors[k],
  1199. a
  1200. );
  1201. #else // COLOR_AS_DWORD
  1202. dstPolygon.colors[dstPolygon.length].Lerp(
  1203. srcPolygon.colors[k1],
  1204. srcPolygon.colors[k],
  1205. a
  1206. );
  1207. #endif // COLOR_AS_DWORD
  1208. #endif // I_SAY_YES_TO_COLOR
  1209. //
  1210. //-----------------------------------------------------
  1211. // If there are texture uv's, we need to lerp them in a
  1212. // perspective correct manner
  1213. //-----------------------------------------------------
  1214. //
  1215. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  1216. dstPolygon.texCoords[2*dstPolygon.length].Lerp
  1217. (
  1218. srcPolygon.texCoords[2*k1],
  1219. srcPolygon.texCoords[2*k],
  1220. a
  1221. );
  1222. dstPolygon.texCoords[2*dstPolygon.length+1].Lerp
  1223. (
  1224. srcPolygon.texCoords[2*k1+1],
  1225. srcPolygon.texCoords[2*k+1],
  1226. a
  1227. );
  1228. #else // I_SAY_YES_TO_DUAL_TEXTURES
  1229. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  1230. for(m=0;m<currentNrOfPasses;m++)
  1231. {
  1232. dstPolygon.texCoords[currentNrOfPasses*dstPolygon.length+m].Lerp
  1233. (
  1234. srcPolygon.texCoords[currentNrOfPasses*k1+m],
  1235. srcPolygon.texCoords[currentNrOfPasses*k+m],
  1236. a
  1237. );
  1238. }
  1239. #else // I_SAY_YES_TO_MULTI_TEXTURES
  1240. dstPolygon.texCoords[dstPolygon.length].Lerp
  1241. (
  1242. srcPolygon.texCoords[k1],
  1243. srcPolygon.texCoords[k],
  1244. a
  1245. );
  1246. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  1247. #endif // I_SAY_YES_TO_DUAL_TEXTURES
  1248. }
  1249. //
  1250. //-------------------------------------
  1251. // We have to generate a new clip state
  1252. //-------------------------------------
  1253. //
  1254. dstPolygon.clipPerVertex[dstPolygon.length].Clip4dVertex(&dstPolygon.coords[dstPolygon.length]);
  1255. //
  1256. //----------------------------------
  1257. // Bump the new polygon vertex count
  1258. //----------------------------------
  1259. //
  1260. dstPolygon.length++;
  1261. }
  1262. //
  1263. //-----------------------------------------------
  1264. // Swap source and destination buffer pointers in
  1265. // preparation for the next plane test
  1266. //-----------------------------------------------
  1267. //
  1268. srcPolygon.coords = clipBuffer[dstBuffer].coords.GetData();
  1269. #ifdef I_SAY_YES_TO_COLOR
  1270. srcPolygon.colors = clipBuffer[dstBuffer].colors.GetData();
  1271. #endif // I_SAY_YES_TO_COLOR
  1272. srcPolygon.texCoords = clipBuffer[dstBuffer].texCoords.GetData();
  1273. srcPolygon.clipPerVertex = clipBuffer[dstBuffer].clipPerVertex.GetData();
  1274. srcPolygon.length = dstPolygon.length;
  1275. dstBuffer = !dstBuffer;
  1276. dstPolygon.coords = clipBuffer[dstBuffer].coords.GetData();
  1277. #ifdef I_SAY_YES_TO_COLOR
  1278. dstPolygon.colors = clipBuffer[dstBuffer].colors.GetData();
  1279. #endif // I_SAY_YES_TO_COLOR
  1280. dstPolygon.texCoords = clipBuffer[dstBuffer].texCoords.GetData();
  1281. dstPolygon.clipPerVertex = clipBuffer[dstBuffer].clipPerVertex.GetData();
  1282. dstPolygon.length = 0;
  1283. }
  1284. mask = mask << 1;
  1285. }
  1286. theNewOr = 0;
  1287. for(k=0;k<srcPolygon.length;k++)
  1288. {
  1289. theNewOr |= srcPolygon.clipPerVertex[k];
  1290. }
  1291. #if HUNT_CLIP_ERROR
  1292. DEBUG_STREAM << "TheOR: " << hex << theNewOr.GetClippingState() << dec << '\n';
  1293. #endif // HUNT_CLIP_ERROR
  1294. theOr = theNewOr;
  1295. } while (theNewOr != 0 && loop--);
  1296. //
  1297. //--------------------------------------------------
  1298. // could not clip this rare case, just ignore it
  1299. //--------------------------------------------------
  1300. //
  1301. if(theNewOr != 0)
  1302. {
  1303. testList[i] = 0;
  1304. continue;
  1305. }
  1306. //
  1307. //--------------------------------------------------
  1308. // Move the most recent polygon into the clip buffer
  1309. //--------------------------------------------------
  1310. //
  1311. #if HUNT_CLIP_ERROR
  1312. DEBUG_STREAM << "Final: " << srcPolygon.length << '\n';
  1313. #endif // HUNT_CLIP_ERROR
  1314. for(k=0;k<srcPolygon.length;k++)
  1315. {
  1316. int clipped_index = myNumberUsedClipVertex + k;
  1317. #if HUNT_CLIP_ERROR
  1318. DEBUG_STREAM << setiosflags( ios::scientific) << setprecision(20)
  1319. << srcPolygon.coords[k].x << " "
  1320. << srcPolygon.coords[k].y << " "
  1321. << srcPolygon.coords[k].z << " "
  1322. << srcPolygon.coords[k].w << '\n';
  1323. #endif // HUNT_CLIP_ERROR
  1324. (*clipExtraCoords)[clipped_index] = srcPolygon.coords[k];
  1325. #ifdef I_SAY_YES_TO_COLOR
  1326. (*clipExtraColors)[clipped_index] = srcPolygon.colors[k];
  1327. #endif // I_SAY_YES_TO_COLOR
  1328. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  1329. (*clipExtraTexCoords)[clipped_index] = srcPolygon.texCoords[2*k];
  1330. (*clipExtraTexCoords2)[clipped_index] = srcPolygon.texCoords[2*k+1];
  1331. #else // I_SAY_YES_TO_DUAL_TEXTURES
  1332. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  1333. for(m=0;m<currentNrOfPasses;m++)
  1334. {
  1335. (*clipExtraMultiTexCoords)[m][clipped_index] = srcPolygon.texCoords[currentNrOfPasses*k+m];
  1336. }
  1337. #else // I_SAY_YES_TO_MULTI_TEXTURES
  1338. (*clipExtraTexCoords)[clipped_index] = srcPolygon.texCoords[k];
  1339. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  1340. #endif // I_SAY_YES_TO_DUAL_TEXTURES
  1341. }
  1342. numberVerticesPerPolygon = srcPolygon.length;
  1343. #if HUNT_CLIP_ERROR
  1344. DEBUG_STREAM << "---" << '\n';
  1345. #endif // HUNT_CLIP_ERROR
  1346. (*clipExtraLength)[myNumberUsedClipLength] = numberVerticesPerPolygon;
  1347. #ifdef _ARMOR
  1348. (*clipExtraLength)[myNumberUsedClipLength] |= 0x8000;
  1349. #endif // _ARMOR
  1350. }
  1351. myNumberUsedClipVertex += numberVerticesPerPolygon;
  1352. myNumberUsedClipLength++;
  1353. ret++;
  1354. // clip
  1355. // dont draw the original
  1356. testList[i] = 0;
  1357. }
  1358. }
  1359. Check_Object(vt);
  1360. gos_vertices = vt->GetActualVertexPool(db);
  1361. numGOSVertices = 0;
  1362. gos_indices = vt->GetActualIndexPool(db);
  1363. numGOSIndices = 0;
  1364. k = visibleIndexedVertices.GetLength();
  1365. unsigned short strideIndex;
  1366. for(j=0,strideIndex=0;j<k;j++)
  1367. {
  1368. if(visibleIndexedVertices[j] == 0)
  1369. {
  1370. strideIndex++;
  1371. }
  1372. else
  1373. {
  1374. indexOffset[j] = static_cast<unsigned short>(j-strideIndex);
  1375. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  1376. if(MLRState::GetMultitextureLightMap() == true && state.GetMultiTextureMode()!=MLRState::MultiTextureOffMode)
  1377. {
  1378. GOSCopyData(
  1379. &gos_vertices2uv[numGOSVertices],
  1380. transformedCoords->GetData(),
  1381. #ifdef I_SAY_YES_TO_COLOR
  1382. actualColors->GetData(),
  1383. #endif // I_SAY_YES_TO_COLOR
  1384. #ifdef I_SAY_YES_TO_TERRAIN
  1385. clipTexCoords->GetData(),
  1386. #else // I_SAY_YES_TO_TERRAIN
  1387. texCoords.GetData(),
  1388. #endif // I_SAY_YES_TO_TERRAIN
  1389. texCoords.GetData() + numVertices,
  1390. j
  1391. #if FOG_HACK
  1392. , state.GetFogMode()
  1393. #endif // FOG_HACK
  1394. );
  1395. // CheckVertices1( gos_vertices2uv+numGOSVertices, 1 );
  1396. }
  1397. else
  1398. {
  1399. GOSCopyData(
  1400. &gos_vertices[numGOSVertices],
  1401. transformedCoords->GetData(),
  1402. #ifdef I_SAY_YES_TO_COLOR
  1403. actualColors->GetData(),
  1404. #endif // I_SAY_YES_TO_COLOR
  1405. #ifdef I_SAY_YES_TO_TERRAIN
  1406. clipTexCoords->GetData(),
  1407. #else // I_SAY_YES_TO_TERRAIN
  1408. texCoords.GetData(),
  1409. #endif // I_SAY_YES_TO_TERRAIN
  1410. j
  1411. #if FOG_HACK
  1412. , state.GetFogMode()
  1413. #endif // FOG_HACK
  1414. );
  1415. (*texCoords2)[tex2count++] = texCoords[numVertices + j];
  1416. }
  1417. #else // I_SAY_YES_TO_DUAL_TEXTURES
  1418. #ifdef I_SAY_YES_TO_DETAIL_TEXTURES
  1419. if( MLRState::GetMultitextureLightMap() == true &&
  1420. state.GetMultiTextureMode()!=MLRState::MultiTextureOffMode &&
  1421. gEnableDetailTexture==1
  1422. )
  1423. {
  1424. GOSCopyData(
  1425. &gos_vertices2uv[numGOSVertices],
  1426. transformedCoords->GetData(),
  1427. #ifdef I_SAY_YES_TO_COLOR
  1428. actualColors->GetData(),
  1429. #endif // I_SAY_YES_TO_COLOR
  1430. #ifdef I_SAY_YES_TO_TERRAIN
  1431. clipTexCoords->GetData(),
  1432. #else // I_SAY_YES_TO_TERRAIN
  1433. texCoords.GetData(),
  1434. #endif // I_SAY_YES_TO_TERRAIN
  1435. #ifdef I_SAY_YES_TO_TERRAIN
  1436. clipTexCoords->GetData(),
  1437. #else // I_SAY_YES_TO_TERRAIN
  1438. texCoords.GetData(),
  1439. #endif // I_SAY_YES_TO_TERRAIN
  1440. j
  1441. #if FOG_HACK
  1442. , state.GetFogMode()
  1443. #endif // FOG_HACK
  1444. );
  1445. }
  1446. else
  1447. #endif // I_SAY_YES_TO_DETAIL_TEXTURES
  1448. {
  1449. GOSCopyData(
  1450. &gos_vertices[numGOSVertices],
  1451. transformedCoords->GetData(),
  1452. #ifdef I_SAY_YES_TO_COLOR
  1453. actualColors->GetData(),
  1454. #endif // I_SAY_YES_TO_COLOR
  1455. #ifdef I_SAY_YES_TO_TERRAIN
  1456. clipTexCoords->GetData(),
  1457. #else // I_SAY_YES_TO_TERRAIN
  1458. texCoords.GetData(),
  1459. #endif // I_SAY_YES_TO_TERRAIN
  1460. j
  1461. #if FOG_HACK
  1462. , state.GetFogMode()
  1463. #endif // FOG_HACK
  1464. );
  1465. }
  1466. #endif // I_SAY_YES_TO_DUAL_TEXTURES
  1467. if(textureAnimation)
  1468. {
  1469. gos_vertices[numGOSVertices].u += deltaU;
  1470. gos_vertices[numGOSVertices].v += deltaV;
  1471. }
  1472. #ifdef LAB_ONLY
  1473. if(gShowClippedPolys)
  1474. {
  1475. gos_vertices[numGOSVertices].argb = 0xff0000ff;
  1476. gos_vertices[numGOSVertices].u = 0.0f;
  1477. gos_vertices[numGOSVertices].v = 0.0f;
  1478. }
  1479. #endif // LAB_ONLY
  1480. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  1481. for(m=1;m<currentNrOfPasses;m++)
  1482. {
  1483. extraMultiTexCoords[m][tex2count[m]++] = multiTexCoordsPointers[m][j];
  1484. }
  1485. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  1486. numGOSVertices++;
  1487. }
  1488. }
  1489. for(i=0,j=0;i<numOfTriangles;j+=3,++i)
  1490. {
  1491. if(testList[i] == 0)
  1492. {
  1493. continue;
  1494. }
  1495. if(db==false)
  1496. {
  1497. Verify((vt->GetLastIndex() + 3 + numGOSIndices) < vt->GetLength());
  1498. }
  1499. else
  1500. {
  1501. Verify(3 + numGOSIndices < 2*Limits::Max_Number_Vertices_Per_Mesh);
  1502. }
  1503. Verify(indexOffset[index[j]] < numGOSVertices);
  1504. gos_indices[numGOSIndices] = indexOffset[index[j]];
  1505. Verify(indexOffset[index[j+2]] < numGOSVertices);
  1506. gos_indices[numGOSIndices+1] = indexOffset[index[j+2]];
  1507. Verify(indexOffset[index[j+1]] < numGOSVertices);
  1508. gos_indices[numGOSIndices+2] = indexOffset[index[j+1]];
  1509. numGOSIndices += 3;
  1510. }
  1511. unsigned short stride;
  1512. if(myNumberUsedClipLength > 0)
  1513. {
  1514. for(i=0,j=0;i<myNumberUsedClipLength;i++)
  1515. {
  1516. #ifdef _ARMOR
  1517. stride = static_cast<unsigned short>((*clipExtraLength)[i] & 0x7fff);
  1518. #else // _ARMOR
  1519. stride = (*clipExtraLength)[i];
  1520. #endif // _ARMOR
  1521. for(k=1;k<stride-1;k++)
  1522. {
  1523. if(db==false)
  1524. {
  1525. Verify((vt->GetLast() + 3 + numGOSVertices) < vt->GetLength());
  1526. Verify((vt->GetLastIndex() + 3 + numGOSIndices) < vt->GetLength());
  1527. }
  1528. else
  1529. {
  1530. Verify(3 + numGOSVertices < 2*Limits::Max_Number_Vertices_Per_Mesh);
  1531. Verify(3 + numGOSIndices < 2*Limits::Max_Number_Vertices_Per_Mesh);
  1532. }
  1533. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  1534. if(MLRState::GetMultitextureLightMap() == true && state.GetMultiTextureMode()!=MLRState::MultiTextureOffMode)
  1535. {
  1536. GOSCopyTriangleData(
  1537. &gos_vertices2uv[numGOSVertices],
  1538. clipExtraCoords->GetData(),
  1539. #ifdef I_SAY_YES_TO_COLOR
  1540. clipExtraColors->GetData(),
  1541. #endif // I_SAY_YES_TO_COLOR
  1542. clipExtraTexCoords->GetData(),
  1543. clipExtraTexCoords2->GetData(),
  1544. j, j+k+1, j+k
  1545. #if FOG_HACK
  1546. , state.GetFogMode()
  1547. #endif // FOG_HACK
  1548. );
  1549. // CheckVertices1( gos_vertices2uv+numGOSVertices, 3 );
  1550. }
  1551. else
  1552. {
  1553. GOSCopyTriangleData(
  1554. &gos_vertices[numGOSVertices],
  1555. clipExtraCoords->GetData(),
  1556. #ifdef I_SAY_YES_TO_COLOR
  1557. clipExtraColors->GetData(),
  1558. #endif // I_SAY_YES_TO_COLOR
  1559. clipExtraTexCoords->GetData(),
  1560. j, j+k+1, j+k
  1561. #if FOG_HACK
  1562. , state.GetFogMode()
  1563. #endif // FOG_HACK
  1564. );
  1565. (*texCoords2)[tex2count++] = (*clipExtraTexCoords2)[j];
  1566. (*texCoords2)[tex2count++] = (*clipExtraTexCoords2)[j+k+1];
  1567. (*texCoords2)[tex2count++] = (*clipExtraTexCoords2)[j+k];
  1568. }
  1569. #else // I_SAY_YES_TO_DUAL_TEXTURES
  1570. #ifdef I_SAY_YES_TO_DETAIL_TEXTURES
  1571. if( MLRState::GetMultitextureLightMap() == true &&
  1572. state.GetMultiTextureMode()!=MLRState::MultiTextureOffMode &&
  1573. gEnableDetailTexture==1
  1574. )
  1575. {
  1576. GOSCopyTriangleData(
  1577. &gos_vertices2uv[numGOSVertices],
  1578. clipExtraCoords->GetData(),
  1579. #ifdef I_SAY_YES_TO_COLOR
  1580. clipExtraColors->GetData(),
  1581. #endif // I_SAY_YES_TO_COLOR
  1582. clipExtraTexCoords->GetData(),
  1583. clipExtraTexCoords->GetData(),
  1584. j, j+k+1, j+k
  1585. #if FOG_HACK
  1586. , state.GetFogMode()
  1587. #endif // FOG_HACK
  1588. );
  1589. }
  1590. else
  1591. #endif // I_SAY_YES_TO_DETAIL_TEXTURES
  1592. {
  1593. GOSCopyTriangleData(
  1594. &gos_vertices[numGOSVertices],
  1595. clipExtraCoords->GetData(),
  1596. #ifdef I_SAY_YES_TO_COLOR
  1597. clipExtraColors->GetData(),
  1598. #endif // I_SAY_YES_TO_COLOR
  1599. clipExtraTexCoords->GetData(),
  1600. j, j+k+1, j+k
  1601. #if FOG_HACK
  1602. , state.GetFogMode()
  1603. #endif // FOG_HACK
  1604. );
  1605. }
  1606. #endif // I_SAY_YES_TO_DUAL_TEXTURES
  1607. if(textureAnimation)
  1608. {
  1609. gos_vertices[numGOSVertices].u += deltaU;
  1610. gos_vertices[numGOSVertices].v += deltaV;
  1611. gos_vertices[numGOSVertices+1].u += deltaU;
  1612. gos_vertices[numGOSVertices+1].v += deltaV;
  1613. gos_vertices[numGOSVertices+2].u += deltaU;
  1614. gos_vertices[numGOSVertices+2].v += deltaV;
  1615. }
  1616. #ifdef LAB_ONLY
  1617. if(gShowClippedPolys)
  1618. {
  1619. if((*clipExtraLength)[i] & 0x8000)
  1620. {
  1621. gos_vertices[numGOSVertices].argb = 0xffff0000;
  1622. gos_vertices[numGOSVertices].u = 0.0f;
  1623. gos_vertices[numGOSVertices].v = 0.0f;
  1624. gos_vertices[numGOSVertices+1].argb = 0xffff0000;
  1625. gos_vertices[numGOSVertices+1].u = 0.0f;
  1626. gos_vertices[numGOSVertices+1].v = 0.0f;
  1627. gos_vertices[numGOSVertices+2].argb = 0xffff0000;
  1628. gos_vertices[numGOSVertices+2].u = 0.0f;
  1629. gos_vertices[numGOSVertices+2].v = 0.0f;
  1630. }
  1631. else
  1632. {
  1633. gos_vertices[numGOSVertices].argb = 0xffff9999;
  1634. gos_vertices[numGOSVertices].u = 0.0f;
  1635. gos_vertices[numGOSVertices].v = 0.0f;
  1636. gos_vertices[numGOSVertices+1].argb = 0xffff9999;
  1637. gos_vertices[numGOSVertices+1].u = 0.0f;
  1638. gos_vertices[numGOSVertices+1].v = 0.0f;
  1639. gos_vertices[numGOSVertices+2].argb = 0xffff9999;
  1640. gos_vertices[numGOSVertices+2].u = 0.0f;
  1641. gos_vertices[numGOSVertices+2].v = 0.0f;
  1642. }
  1643. }
  1644. #endif // LAB_ONLY
  1645. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  1646. for(m=1;m<currentNrOfPasses;m++)
  1647. {
  1648. extraMultiTexCoords[m][tex2count[m]++] = clipExtraMultiTexCoords[m][j];
  1649. extraMultiTexCoords[m][tex2count[m]++] = clipExtraMultiTexCoords[m][j+k+1];
  1650. extraMultiTexCoords[m][tex2count[m]++] = clipExtraMultiTexCoords[m][j+k];
  1651. }
  1652. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  1653. if(db==false)
  1654. {
  1655. Verify((vt->GetLastIndex() + 3 + numGOSIndices) < vt->GetLength());
  1656. }
  1657. else
  1658. {
  1659. Verify(3 + numGOSIndices < 2*Limits::Max_Number_Vertices_Per_Mesh);
  1660. }
  1661. Verify(numGOSIndices%3 == 0);
  1662. gos_indices[numGOSIndices] = numGOSVertices;
  1663. gos_indices[numGOSIndices+1] = (unsigned short)(numGOSVertices + 1);
  1664. gos_indices[numGOSIndices+2] = (unsigned short)(numGOSVertices + 2);
  1665. numGOSVertices += 3;
  1666. numGOSIndices += 3;
  1667. }
  1668. j += stride;
  1669. }
  1670. #if HUNT_CLIP_ERROR
  1671. DEBUG_STREAM << "***" << endl << endl;
  1672. #endif // HUNT_CLIP_ERROR
  1673. }
  1674. #ifdef I_SAY_YES_TO_DUAL_TEXTURES
  1675. if(MLRState::GetMultitextureLightMap() == false || state.GetMultiTextureMode()==MLRState::MultiTextureOffMode)
  1676. {
  1677. Verify (tex2count == numGOSVertices);
  1678. if(db==false)
  1679. {
  1680. Verify (vt->GetLast() + 2*numGOSVertices < vt->GetLength());
  1681. }
  1682. else
  1683. {
  1684. Verify (2*numGOSVertices < 2*Limits::Max_Number_Vertices_Per_Mesh);
  1685. }
  1686. memcpy(gos_vertices + numGOSVertices, gos_vertices, numGOSVertices * sizeof(GOSVertex));
  1687. if(textureAnimation2)
  1688. {
  1689. for(i=0,j=numGOSVertices;i<numGOSVertices;i++,j++)
  1690. {
  1691. gos_vertices[j].u = (*texCoords2)[i][0] + deltaU2;
  1692. gos_vertices[j].v = (*texCoords2)[i][1] + deltaV2;
  1693. }
  1694. }
  1695. else
  1696. {
  1697. for(i=0,j=numGOSVertices;i<numGOSVertices;i++,j++)
  1698. {
  1699. gos_vertices[j].u = (*texCoords2)[i][0];
  1700. gos_vertices[j].v = (*texCoords2)[i][1];
  1701. }
  1702. }
  1703. if(db==false)
  1704. {
  1705. vt->Increase(2*numGOSVertices);
  1706. }
  1707. }
  1708. else
  1709. {
  1710. if(db==false)
  1711. {
  1712. vt->Increase2UV(numGOSVertices);
  1713. }
  1714. }
  1715. #else // I_SAY_YES_TO_DUAL_TEXTURES
  1716. #ifdef I_SAY_YES_TO_DETAIL_TEXTURES
  1717. if(gEnableDetailTexture==1)
  1718. {
  1719. if( MLRState::GetMultitextureLightMap() == true &&
  1720. state.GetMultiTextureMode()!=MLRState::MultiTextureOffMode
  1721. )
  1722. {
  1723. for(i=0;i<numGOSVertices;i++)
  1724. {
  1725. #ifdef I_SAY_YES_TO_TERRAIN2
  1726. gos_vertices2uv[i].u2 = gos_vertices2uv[i].u1*xScale;
  1727. gos_vertices2uv[i].v2 = gos_vertices2uv[i].v1*yScale;
  1728. #else // I_SAY_YES_TO_TERRAIN2
  1729. gos_vertices2uv[i].u2 = gos_vertices2uv[i].u1*xScale + xOffset + deltaU2;
  1730. gos_vertices2uv[i].v2 = gos_vertices2uv[i].v1*yScale + yOffset + deltaV2;
  1731. #endif // I_SAY_YES_TO_TERRAIN2
  1732. Verify( MLRState::GetHasMaxUVs() ? (gos_vertices2uv[i].u2>=-MLRState::GetMaxUV() && gos_vertices2uv[i].u2<=MLRState::GetMaxUV()) : 1);
  1733. Verify( MLRState::GetHasMaxUVs() ? (gos_vertices2uv[i].v2>=-MLRState::GetMaxUV() && gos_vertices2uv[i].v2<=MLRState::GetMaxUV()) : 1);
  1734. if(gos_vertices2uv[i].rhw < fadeDetailEnd)
  1735. {
  1736. gos_vertices2uv[i].argb &= 0x00ffffff;
  1737. }
  1738. else
  1739. {
  1740. if(gos_vertices2uv[i].rhw<fadeDetailStart)
  1741. {
  1742. gos_vertices2uv[i].argb &= ((Stuff::Truncate_Float_To_Word((gos_vertices2uv[i].rhw - fadeDetailEnd)*fadeMultiplicator)) << 24) | 0x00ffffff;
  1743. }
  1744. }
  1745. }
  1746. if(db==false)
  1747. {
  1748. vt->Increase2UV(numGOSVertices);
  1749. }
  1750. }
  1751. else
  1752. {
  1753. if(db==false)
  1754. {
  1755. Verify (vt->GetLast() + 2*numGOSVertices < vt->GetLength());
  1756. }
  1757. else
  1758. {
  1759. Verify (2*numGOSVertices < 2*Limits::Max_Number_Vertices_Per_Mesh);
  1760. }
  1761. memcpy(gos_vertices + numGOSVertices, gos_vertices, numGOSVertices * sizeof(GOSVertex));
  1762. stride = 0;
  1763. for(i=0,j=numGOSVertices;i<numGOSVertices;i++,j++)
  1764. {
  1765. #ifdef I_SAY_YES_TO_TERRAIN2
  1766. gos_vertices[j].u = gos_vertices[i].u*xScale;
  1767. gos_vertices[j].v = gos_vertices[i].v*yScale;
  1768. #else // I_SAY_YES_TO_TERRAIN2
  1769. gos_vertices[j].u = gos_vertices[i].u*xScale + xOffset + deltaU2;
  1770. gos_vertices[j].v = gos_vertices[i].v*yScale + yOffset + deltaV2;
  1771. #endif // I_SAY_YES_TO_TERRAIN2
  1772. Verify( MLRState::GetHasMaxUVs() ? (gos_vertices[j].u>=-MLRState::GetMaxUV() && gos_vertices[j].u<=MLRState::GetMaxUV()) : 1);
  1773. Verify( MLRState::GetHasMaxUVs() ? (gos_vertices[j].v>=-MLRState::GetMaxUV() && gos_vertices[j].v<=MLRState::GetMaxUV()) : 1);
  1774. if(gos_vertices[j].rhw < fadeDetailEnd)
  1775. {
  1776. gos_vertices[j].argb &= 0x00ffffff;
  1777. }
  1778. else
  1779. {
  1780. stride++;
  1781. if(gos_vertices[j].rhw<fadeDetailStart)
  1782. {
  1783. gos_vertices[j].argb &= ((Stuff::Truncate_Float_To_Word((gos_vertices[j].rhw - fadeDetailEnd)*fadeMultiplicator)) << 24) | 0x00ffffff;
  1784. }
  1785. }
  1786. }
  1787. if(stride==0)
  1788. {
  1789. detTextureVisible=false;
  1790. }
  1791. else
  1792. {
  1793. detTextureVisible=true;
  1794. }
  1795. if(db==false)
  1796. {
  1797. vt->Increase(2*numGOSVertices);
  1798. }
  1799. }
  1800. }
  1801. else
  1802. {
  1803. if(db==false)
  1804. {
  1805. vt->Increase(numGOSVertices);
  1806. }
  1807. }
  1808. #else // I_SAY_YES_TO_DETAIL_TEXTURES
  1809. if(db==false)
  1810. {
  1811. vt->Increase(numGOSVertices);
  1812. }
  1813. #endif // I_SAY_YES_TO_DETAIL_TEXTURES
  1814. #endif // // I_SAY_YES_TO_DUAL_TEXTURES
  1815. #ifdef I_SAY_YES_TO_MULTI_TEXTURES
  1816. for(m=1;m<currentNrOfPasses;m++)
  1817. {
  1818. Verify (tex2count[m] == numGOSVertices);
  1819. if(db==false)
  1820. {
  1821. Verify (vt->GetLast() + numGOSVertices < vt->GetLength());
  1822. }
  1823. else
  1824. {
  1825. Verify (numGOSVertices < 2*Limits::Max_Number_Vertices_Per_Mesh);
  1826. }
  1827. memcpy(gos_vertices + m*numGOSVertices, gos_vertices, numGOSVertices * sizeof(GOSVertex));
  1828. for(i=0,j=numGOSVertices;i<numGOSVertices;i++,j++)
  1829. {
  1830. gos_vertices[j].u = extraMultiTexCoords[m][i][0];
  1831. gos_vertices[j].v = extraMultiTexCoords[m][i][1];
  1832. }
  1833. if(db==false)
  1834. {
  1835. vt->Increase(numGOSVertices);
  1836. }
  1837. }
  1838. #endif // I_SAY_YES_TO_MULTI_TEXTURES
  1839. if(db==false)
  1840. {
  1841. vt->IncreaseIndex(numGOSIndices);
  1842. }
  1843. visible = numGOSVertices ? (unsigned char)1 : (unsigned char)0;
  1844. if(visible)
  1845. {
  1846. }
  1847. else
  1848. {
  1849. }
  1850. #if 0 //def I_SAY_YES_TO_DUAL_TEXTURES
  1851. if(MLRState::GetMultitextureLightMap() == true && state.GetMultiTextureMode()!=MLRState::MultiTextureOffMode)
  1852. {
  1853. CheckVertices1( gos_vertices2uv, numGOSVertices );
  1854. }
  1855. else
  1856. {
  1857. CheckVertices( gos_vertices, numGOSVertices, false );
  1858. }
  1859. #endif // // I_SAY_YES_TO_DUAL_TEXTURES
  1860. Stop_Timer(Clipping_Time);
  1861. return ret;
  1862. }