tr_trisurf.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "tr_local.h"
  23. /*
  24. ==============================================================================
  25. TRIANGLE MESH PROCESSING
  26. The functions in this file have no vertex / index count limits.
  27. Truly identical vertexes that match in position, normal, and texcoord can
  28. be merged away.
  29. Vertexes that match in position and texcoord, but have distinct normals will
  30. remain distinct for all purposes. This is usually a poor choice for models,
  31. as adding a bevel face will not add any more vertexes, and will tend to
  32. look better.
  33. Match in position and normal, but differ in texcoords are referenced together
  34. for calculating tangent vectors for bump mapping.
  35. Artists should take care to have identical texels in all maps (bump/diffuse/specular)
  36. in this case
  37. Vertexes that only match in position are merged for shadow edge finding.
  38. Degenerate triangles.
  39. Overlapped triangles, even if normals or texcoords differ, must be removed.
  40. for the silhoette based stencil shadow algorithm to function properly.
  41. Is this true???
  42. Is the overlapped triangle problem just an example of the trippled edge problem?
  43. Interpenetrating triangles are not currently clipped to surfaces.
  44. Do they effect the shadows?
  45. if vertexes are intended to deform apart, make sure that no vertexes
  46. are on top of each other in the base frame, or the sil edges may be
  47. calculated incorrectly.
  48. We might be able to identify this from topology.
  49. Dangling edges are acceptable, but three way edges are not.
  50. Are any combinations of two way edges unacceptable, like one facing
  51. the backside of the other?
  52. Topology is determined by a collection of triangle indexes.
  53. The edge list can be built up from this, and stays valid even under
  54. deformations.
  55. Somewhat non-intuitively, concave edges cannot be optimized away, or the
  56. stencil shadow algorithm miscounts.
  57. Face normals are needed for generating shadow volumes and for calculating
  58. the silhouette, but they will change with any deformation.
  59. Vertex normals and vertex tangents will change with each deformation,
  60. but they may be able to be transformed instead of recalculated.
  61. bounding volume, both box and sphere will change with deformation.
  62. silhouette indexes
  63. shade indexes
  64. texture indexes
  65. shade indexes will only be > silhouette indexes if there is facet shading present
  66. lookups from texture to sil and texture to shade?
  67. The normal and tangent vector smoothing is simple averaging, no attempt is
  68. made to better handle the cases where the distribution around the shared vertex
  69. is highly uneven.
  70. we may get degenerate triangles even with the uniquing and removal
  71. if the vertexes have different texcoords.
  72. ==============================================================================
  73. */
  74. // this shouldn't change anything, but previously renderbumped models seem to need it
  75. #define USE_INVA
  76. // instead of using the texture T vector, cross the normal and S vector for an orthogonal axis
  77. #define DERIVE_UNSMOOTHED_BITANGENT
  78. const int MAX_SIL_EDGES = 0x10000;
  79. const int SILEDGE_HASH_SIZE = 1024;
  80. static int numSilEdges;
  81. static silEdge_t * silEdges;
  82. static idHashIndex silEdgeHash( SILEDGE_HASH_SIZE, MAX_SIL_EDGES );
  83. static int numPlanes;
  84. static idBlockAlloc<srfTriangles_t, 1<<8> srfTrianglesAllocator;
  85. #ifdef USE_TRI_DATA_ALLOCATOR
  86. static idDynamicBlockAlloc<idDrawVert, 1<<20, 1<<10> triVertexAllocator;
  87. static idDynamicBlockAlloc<glIndex_t, 1<<18, 1<<10> triIndexAllocator;
  88. static idDynamicBlockAlloc<shadowCache_t, 1<<18, 1<<10> triShadowVertexAllocator;
  89. static idDynamicBlockAlloc<idPlane, 1<<17, 1<<10> triPlaneAllocator;
  90. static idDynamicBlockAlloc<glIndex_t, 1<<17, 1<<10> triSilIndexAllocator;
  91. static idDynamicBlockAlloc<silEdge_t, 1<<17, 1<<10> triSilEdgeAllocator;
  92. static idDynamicBlockAlloc<dominantTri_t, 1<<16, 1<<10> triDominantTrisAllocator;
  93. static idDynamicBlockAlloc<int, 1<<16, 1<<10> triMirroredVertAllocator;
  94. static idDynamicBlockAlloc<int, 1<<16, 1<<10> triDupVertAllocator;
  95. #else
  96. static idDynamicAlloc<idDrawVert, 1<<20, 1<<10> triVertexAllocator;
  97. static idDynamicAlloc<glIndex_t, 1<<18, 1<<10> triIndexAllocator;
  98. static idDynamicAlloc<shadowCache_t, 1<<18, 1<<10> triShadowVertexAllocator;
  99. static idDynamicAlloc<idPlane, 1<<17, 1<<10> triPlaneAllocator;
  100. static idDynamicAlloc<glIndex_t, 1<<17, 1<<10> triSilIndexAllocator;
  101. static idDynamicAlloc<silEdge_t, 1<<17, 1<<10> triSilEdgeAllocator;
  102. static idDynamicAlloc<dominantTri_t, 1<<16, 1<<10> triDominantTrisAllocator;
  103. static idDynamicAlloc<int, 1<<16, 1<<10> triMirroredVertAllocator;
  104. static idDynamicAlloc<int, 1<<16, 1<<10> triDupVertAllocator;
  105. #endif
  106. /*
  107. ===============
  108. R_InitTriSurfData
  109. ===============
  110. */
  111. void R_InitTriSurfData( void ) {
  112. silEdges = (silEdge_t *)R_StaticAlloc( MAX_SIL_EDGES * sizeof( silEdges[0] ) );
  113. // initialize allocators for triangle surfaces
  114. triVertexAllocator.Init();
  115. triIndexAllocator.Init();
  116. triShadowVertexAllocator.Init();
  117. triPlaneAllocator.Init();
  118. triSilIndexAllocator.Init();
  119. triSilEdgeAllocator.Init();
  120. triDominantTrisAllocator.Init();
  121. triMirroredVertAllocator.Init();
  122. triDupVertAllocator.Init();
  123. // never swap out triangle surfaces
  124. triVertexAllocator.SetLockMemory( true );
  125. triIndexAllocator.SetLockMemory( true );
  126. triShadowVertexAllocator.SetLockMemory( true );
  127. triPlaneAllocator.SetLockMemory( true );
  128. triSilIndexAllocator.SetLockMemory( true );
  129. triSilEdgeAllocator.SetLockMemory( true );
  130. triDominantTrisAllocator.SetLockMemory( true );
  131. triMirroredVertAllocator.SetLockMemory( true );
  132. triDupVertAllocator.SetLockMemory( true );
  133. }
  134. /*
  135. ===============
  136. R_ShutdownTriSurfData
  137. ===============
  138. */
  139. void R_ShutdownTriSurfData( void ) {
  140. R_StaticFree( silEdges );
  141. silEdgeHash.Free();
  142. srfTrianglesAllocator.Shutdown();
  143. triVertexAllocator.Shutdown();
  144. triIndexAllocator.Shutdown();
  145. triShadowVertexAllocator.Shutdown();
  146. triPlaneAllocator.Shutdown();
  147. triSilIndexAllocator.Shutdown();
  148. triSilEdgeAllocator.Shutdown();
  149. triDominantTrisAllocator.Shutdown();
  150. triMirroredVertAllocator.Shutdown();
  151. triDupVertAllocator.Shutdown();
  152. }
  153. /*
  154. ===============
  155. R_PurgeTriSurfData
  156. ===============
  157. */
  158. void R_PurgeTriSurfData( frameData_t *frame ) {
  159. // free deferred triangle surfaces
  160. R_FreeDeferredTriSurfs( frame );
  161. // free empty base blocks
  162. triVertexAllocator.FreeEmptyBaseBlocks();
  163. triIndexAllocator.FreeEmptyBaseBlocks();
  164. triShadowVertexAllocator.FreeEmptyBaseBlocks();
  165. triPlaneAllocator.FreeEmptyBaseBlocks();
  166. triSilIndexAllocator.FreeEmptyBaseBlocks();
  167. triSilEdgeAllocator.FreeEmptyBaseBlocks();
  168. triDominantTrisAllocator.FreeEmptyBaseBlocks();
  169. triMirroredVertAllocator.FreeEmptyBaseBlocks();
  170. triDupVertAllocator.FreeEmptyBaseBlocks();
  171. }
  172. /*
  173. ===============
  174. R_ShowTriMemory_f
  175. ===============
  176. */
  177. void R_ShowTriSurfMemory_f( const idCmdArgs &args ) {
  178. common->Printf( "%6d kB in %d triangle surfaces\n",
  179. ( srfTrianglesAllocator.GetAllocCount() * sizeof( srfTriangles_t ) ) >> 10,
  180. srfTrianglesAllocator.GetAllocCount() );
  181. common->Printf( "%6d kB vertex memory (%d kB free in %d blocks, %d empty base blocks)\n",
  182. triVertexAllocator.GetBaseBlockMemory() >> 10, triVertexAllocator.GetFreeBlockMemory() >> 10,
  183. triVertexAllocator.GetNumFreeBlocks(), triVertexAllocator.GetNumEmptyBaseBlocks() );
  184. common->Printf( "%6d kB index memory (%d kB free in %d blocks, %d empty base blocks)\n",
  185. triIndexAllocator.GetBaseBlockMemory() >> 10, triIndexAllocator.GetFreeBlockMemory() >> 10,
  186. triIndexAllocator.GetNumFreeBlocks(), triIndexAllocator.GetNumEmptyBaseBlocks() );
  187. common->Printf( "%6d kB shadow vert memory (%d kB free in %d blocks, %d empty base blocks)\n",
  188. triShadowVertexAllocator.GetBaseBlockMemory() >> 10, triShadowVertexAllocator.GetFreeBlockMemory() >> 10,
  189. triShadowVertexAllocator.GetNumFreeBlocks(), triShadowVertexAllocator.GetNumEmptyBaseBlocks() );
  190. common->Printf( "%6d kB tri plane memory (%d kB free in %d blocks, %d empty base blocks)\n",
  191. triPlaneAllocator.GetBaseBlockMemory() >> 10, triPlaneAllocator.GetFreeBlockMemory() >> 10,
  192. triPlaneAllocator.GetNumFreeBlocks(), triPlaneAllocator.GetNumEmptyBaseBlocks() );
  193. common->Printf( "%6d kB sil index memory (%d kB free in %d blocks, %d empty base blocks)\n",
  194. triSilIndexAllocator.GetBaseBlockMemory() >> 10, triSilIndexAllocator.GetFreeBlockMemory() >> 10,
  195. triSilIndexAllocator.GetNumFreeBlocks(), triSilIndexAllocator.GetNumEmptyBaseBlocks() );
  196. common->Printf( "%6d kB sil edge memory (%d kB free in %d blocks, %d empty base blocks)\n",
  197. triSilEdgeAllocator.GetBaseBlockMemory() >> 10, triSilEdgeAllocator.GetFreeBlockMemory() >> 10,
  198. triSilEdgeAllocator.GetNumFreeBlocks(), triSilEdgeAllocator.GetNumEmptyBaseBlocks() );
  199. common->Printf( "%6d kB dominant tri memory (%d kB free in %d blocks, %d empty base blocks)\n",
  200. triDominantTrisAllocator.GetBaseBlockMemory() >> 10, triDominantTrisAllocator.GetFreeBlockMemory() >> 10,
  201. triDominantTrisAllocator.GetNumFreeBlocks(), triDominantTrisAllocator.GetNumEmptyBaseBlocks() );
  202. common->Printf( "%6d kB mirror vert memory (%d kB free in %d blocks, %d empty base blocks)\n",
  203. triMirroredVertAllocator.GetBaseBlockMemory() >> 10, triMirroredVertAllocator.GetFreeBlockMemory() >> 10,
  204. triMirroredVertAllocator.GetNumFreeBlocks(), triMirroredVertAllocator.GetNumEmptyBaseBlocks() );
  205. common->Printf( "%6d kB dup vert memory (%d kB free in %d blocks, %d empty base blocks)\n",
  206. triDupVertAllocator.GetBaseBlockMemory() >> 10, triDupVertAllocator.GetFreeBlockMemory() >> 10,
  207. triDupVertAllocator.GetNumFreeBlocks(), triDupVertAllocator.GetNumEmptyBaseBlocks() );
  208. common->Printf( "%6d kB total triangle memory\n",
  209. ( srfTrianglesAllocator.GetAllocCount() * sizeof( srfTriangles_t ) +
  210. triVertexAllocator.GetBaseBlockMemory() +
  211. triIndexAllocator.GetBaseBlockMemory() +
  212. triShadowVertexAllocator.GetBaseBlockMemory() +
  213. triPlaneAllocator.GetBaseBlockMemory() +
  214. triSilIndexAllocator.GetBaseBlockMemory() +
  215. triSilEdgeAllocator.GetBaseBlockMemory() +
  216. triDominantTrisAllocator.GetBaseBlockMemory() +
  217. triMirroredVertAllocator.GetBaseBlockMemory() +
  218. triDupVertAllocator.GetBaseBlockMemory() ) >> 10 );
  219. }
  220. /*
  221. =================
  222. R_TriSurfMemory
  223. For memory profiling
  224. =================
  225. */
  226. int R_TriSurfMemory( const srfTriangles_t *tri ) {
  227. int total = 0;
  228. if ( !tri ) {
  229. return total;
  230. }
  231. // used as a flag in interations
  232. if ( tri == LIGHT_TRIS_DEFERRED ) {
  233. return total;
  234. }
  235. if ( tri->shadowVertexes != NULL ) {
  236. total += tri->numVerts * sizeof( tri->shadowVertexes[0] );
  237. } else if ( tri->verts != NULL ) {
  238. if ( tri->ambientSurface == NULL || tri->verts != tri->ambientSurface->verts ) {
  239. total += tri->numVerts * sizeof( tri->verts[0] );
  240. }
  241. }
  242. if ( tri->facePlanes != NULL ) {
  243. total += tri->numIndexes / 3 * sizeof( tri->facePlanes[0] );
  244. }
  245. if ( tri->indexes != NULL ) {
  246. if ( tri->ambientSurface == NULL || tri->indexes != tri->ambientSurface->indexes ) {
  247. total += tri->numIndexes * sizeof( tri->indexes[0] );
  248. }
  249. }
  250. if ( tri->silIndexes != NULL ) {
  251. total += tri->numIndexes * sizeof( tri->silIndexes[0] );
  252. }
  253. if ( tri->silEdges != NULL ) {
  254. total += tri->numSilEdges * sizeof( tri->silEdges[0] );
  255. }
  256. if ( tri->dominantTris != NULL ) {
  257. total += tri->numVerts * sizeof( tri->dominantTris[0] );
  258. }
  259. if ( tri->mirroredVerts != NULL ) {
  260. total += tri->numMirroredVerts * sizeof( tri->mirroredVerts[0] );
  261. }
  262. if ( tri->dupVerts != NULL ) {
  263. total += tri->numDupVerts * sizeof( tri->dupVerts[0] );
  264. }
  265. total += sizeof( *tri );
  266. return total;
  267. }
  268. /*
  269. ==============
  270. R_FreeStaticTriSurfVertexCaches
  271. ==============
  272. */
  273. void R_FreeStaticTriSurfVertexCaches( srfTriangles_t *tri ) {
  274. if ( tri->ambientSurface == NULL ) {
  275. // this is a real model surface
  276. vertexCache.Free( tri->ambientCache );
  277. tri->ambientCache = NULL;
  278. } else {
  279. // this is a light interaction surface that references
  280. // a different ambient model surface
  281. vertexCache.Free( tri->lightingCache );
  282. tri->lightingCache = NULL;
  283. }
  284. if ( tri->indexCache ) {
  285. vertexCache.Free( tri->indexCache );
  286. tri->indexCache = NULL;
  287. }
  288. if ( tri->shadowCache && ( tri->shadowVertexes != NULL || tri->verts != NULL ) ) {
  289. // if we don't have tri->shadowVertexes, these are a reference to a
  290. // shadowCache on the original surface, which a vertex program
  291. // will take care of making unique for each light
  292. vertexCache.Free( tri->shadowCache );
  293. tri->shadowCache = NULL;
  294. }
  295. }
  296. /*
  297. ==============
  298. R_ReallyFreeStaticTriSurf
  299. This does the actual free
  300. ==============
  301. */
  302. void R_ReallyFreeStaticTriSurf( srfTriangles_t *tri ) {
  303. if ( !tri ) {
  304. return;
  305. }
  306. R_FreeStaticTriSurfVertexCaches( tri );
  307. if ( tri->verts != NULL ) {
  308. // R_CreateLightTris points tri->verts at the verts of the ambient surface
  309. if ( tri->ambientSurface == NULL || tri->verts != tri->ambientSurface->verts ) {
  310. triVertexAllocator.Free( tri->verts );
  311. }
  312. }
  313. if ( !tri->deformedSurface ) {
  314. if ( tri->indexes != NULL ) {
  315. // if a surface is completely inside a light volume R_CreateLightTris points tri->indexes at the indexes of the ambient surface
  316. if ( tri->ambientSurface == NULL || tri->indexes != tri->ambientSurface->indexes ) {
  317. triIndexAllocator.Free( tri->indexes );
  318. }
  319. }
  320. if ( tri->silIndexes != NULL ) {
  321. triSilIndexAllocator.Free( tri->silIndexes );
  322. }
  323. if ( tri->silEdges != NULL ) {
  324. triSilEdgeAllocator.Free( tri->silEdges );
  325. }
  326. if ( tri->dominantTris != NULL ) {
  327. triDominantTrisAllocator.Free( tri->dominantTris );
  328. }
  329. if ( tri->mirroredVerts != NULL ) {
  330. triMirroredVertAllocator.Free( tri->mirroredVerts );
  331. }
  332. if ( tri->dupVerts != NULL ) {
  333. triDupVertAllocator.Free( tri->dupVerts );
  334. }
  335. }
  336. if ( tri->facePlanes != NULL ) {
  337. triPlaneAllocator.Free( tri->facePlanes );
  338. }
  339. if ( tri->shadowVertexes != NULL ) {
  340. triShadowVertexAllocator.Free( tri->shadowVertexes );
  341. }
  342. #ifdef _DEBUG
  343. memset( tri, 0, sizeof( srfTriangles_t ) );
  344. #endif
  345. srfTrianglesAllocator.Free( tri );
  346. }
  347. /*
  348. ==============
  349. R_CheckStaticTriSurfMemory
  350. ==============
  351. */
  352. void R_CheckStaticTriSurfMemory( const srfTriangles_t *tri ) {
  353. if ( !tri ) {
  354. return;
  355. }
  356. if ( tri->verts != NULL ) {
  357. // R_CreateLightTris points tri->verts at the verts of the ambient surface
  358. if ( tri->ambientSurface == NULL || tri->verts != tri->ambientSurface->verts ) {
  359. const char *error = triVertexAllocator.CheckMemory( tri->verts );
  360. assert( error == NULL );
  361. }
  362. }
  363. if ( !tri->deformedSurface ) {
  364. if ( tri->indexes != NULL ) {
  365. // if a surface is completely inside a light volume R_CreateLightTris points tri->indexes at the indexes of the ambient surface
  366. if ( tri->ambientSurface == NULL || tri->indexes != tri->ambientSurface->indexes ) {
  367. const char *error = triIndexAllocator.CheckMemory( tri->indexes );
  368. assert( error == NULL );
  369. }
  370. }
  371. }
  372. if ( tri->shadowVertexes != NULL ) {
  373. const char *error = triShadowVertexAllocator.CheckMemory( tri->shadowVertexes );
  374. assert( error == NULL );
  375. }
  376. }
  377. /*
  378. ==================
  379. R_FreeDeferredTriSurfs
  380. ==================
  381. */
  382. void R_FreeDeferredTriSurfs( frameData_t *frame ) {
  383. srfTriangles_t *tri, *next;
  384. if ( !frame ) {
  385. return;
  386. }
  387. for ( tri = frame->firstDeferredFreeTriSurf; tri; tri = next ) {
  388. next = tri->nextDeferredFree;
  389. R_ReallyFreeStaticTriSurf( tri );
  390. }
  391. frame->firstDeferredFreeTriSurf = NULL;
  392. frame->lastDeferredFreeTriSurf = NULL;
  393. }
  394. /*
  395. ==============
  396. R_FreeStaticTriSurf
  397. This will defer the free until the current frame has run through the back end.
  398. ==============
  399. */
  400. void R_FreeStaticTriSurf( srfTriangles_t *tri ) {
  401. frameData_t *frame;
  402. if ( !tri ) {
  403. return;
  404. }
  405. if ( tri->nextDeferredFree ) {
  406. common->Error( "R_FreeStaticTriSurf: freed a freed triangle" );
  407. }
  408. frame = frameData;
  409. if ( !frame ) {
  410. // command line utility, or rendering in editor preview mode ( force )
  411. R_ReallyFreeStaticTriSurf( tri );
  412. } else {
  413. #ifdef ID_DEBUG_MEMORY
  414. R_CheckStaticTriSurfMemory( tri );
  415. #endif
  416. tri->nextDeferredFree = NULL;
  417. if ( frame->lastDeferredFreeTriSurf ) {
  418. frame->lastDeferredFreeTriSurf->nextDeferredFree = tri;
  419. } else {
  420. frame->firstDeferredFreeTriSurf = tri;
  421. }
  422. frame->lastDeferredFreeTriSurf = tri;
  423. }
  424. }
  425. /*
  426. ==============
  427. R_AllocStaticTriSurf
  428. ==============
  429. */
  430. srfTriangles_t *R_AllocStaticTriSurf( void ) {
  431. srfTriangles_t *tris = srfTrianglesAllocator.Alloc();
  432. memset( tris, 0, sizeof( srfTriangles_t ) );
  433. return tris;
  434. }
  435. /*
  436. =================
  437. R_CopyStaticTriSurf
  438. This only duplicates the indexes and verts, not any of the derived data.
  439. =================
  440. */
  441. srfTriangles_t *R_CopyStaticTriSurf( const srfTriangles_t *tri ) {
  442. srfTriangles_t *newTri;
  443. newTri = R_AllocStaticTriSurf();
  444. R_AllocStaticTriSurfVerts( newTri, tri->numVerts );
  445. R_AllocStaticTriSurfIndexes( newTri, tri->numIndexes );
  446. newTri->numVerts = tri->numVerts;
  447. newTri->numIndexes = tri->numIndexes;
  448. memcpy( newTri->verts, tri->verts, tri->numVerts * sizeof( newTri->verts[0] ) );
  449. memcpy( newTri->indexes, tri->indexes, tri->numIndexes * sizeof( newTri->indexes[0] ) );
  450. return newTri;
  451. }
  452. /*
  453. =================
  454. R_AllocStaticTriSurfVerts
  455. =================
  456. */
  457. void R_AllocStaticTriSurfVerts( srfTriangles_t *tri, int numVerts ) {
  458. assert( tri->verts == NULL );
  459. tri->verts = triVertexAllocator.Alloc( numVerts );
  460. }
  461. /*
  462. =================
  463. R_AllocStaticTriSurfIndexes
  464. =================
  465. */
  466. void R_AllocStaticTriSurfIndexes( srfTriangles_t *tri, int numIndexes ) {
  467. assert( tri->indexes == NULL );
  468. tri->indexes = triIndexAllocator.Alloc( numIndexes );
  469. }
  470. /*
  471. =================
  472. R_AllocStaticTriSurfShadowVerts
  473. =================
  474. */
  475. void R_AllocStaticTriSurfShadowVerts( srfTriangles_t *tri, int numVerts ) {
  476. assert( tri->shadowVertexes == NULL );
  477. tri->shadowVertexes = triShadowVertexAllocator.Alloc( numVerts );
  478. }
  479. /*
  480. =================
  481. R_AllocStaticTriSurfPlanes
  482. =================
  483. */
  484. void R_AllocStaticTriSurfPlanes( srfTriangles_t *tri, int numIndexes ) {
  485. if ( tri->facePlanes ) {
  486. triPlaneAllocator.Free( tri->facePlanes );
  487. }
  488. tri->facePlanes = triPlaneAllocator.Alloc( numIndexes / 3 );
  489. }
  490. /*
  491. =================
  492. R_ResizeStaticTriSurfVerts
  493. =================
  494. */
  495. void R_ResizeStaticTriSurfVerts( srfTriangles_t *tri, int numVerts ) {
  496. #ifdef USE_TRI_DATA_ALLOCATOR
  497. tri->verts = triVertexAllocator.Resize( tri->verts, numVerts );
  498. #else
  499. assert( false );
  500. #endif
  501. }
  502. /*
  503. =================
  504. R_ResizeStaticTriSurfIndexes
  505. =================
  506. */
  507. void R_ResizeStaticTriSurfIndexes( srfTriangles_t *tri, int numIndexes ) {
  508. #ifdef USE_TRI_DATA_ALLOCATOR
  509. tri->indexes = triIndexAllocator.Resize( tri->indexes, numIndexes );
  510. #else
  511. assert( false );
  512. #endif
  513. }
  514. /*
  515. =================
  516. R_ResizeStaticTriSurfShadowVerts
  517. =================
  518. */
  519. void R_ResizeStaticTriSurfShadowVerts( srfTriangles_t *tri, int numVerts ) {
  520. #ifdef USE_TRI_DATA_ALLOCATOR
  521. tri->shadowVertexes = triShadowVertexAllocator.Resize( tri->shadowVertexes, numVerts );
  522. #else
  523. assert( false );
  524. #endif
  525. }
  526. /*
  527. =================
  528. R_ReferenceStaticTriSurfVerts
  529. =================
  530. */
  531. void R_ReferenceStaticTriSurfVerts( srfTriangles_t *tri, const srfTriangles_t *reference ) {
  532. tri->verts = reference->verts;
  533. }
  534. /*
  535. =================
  536. R_ReferenceStaticTriSurfIndexes
  537. =================
  538. */
  539. void R_ReferenceStaticTriSurfIndexes( srfTriangles_t *tri, const srfTriangles_t *reference ) {
  540. tri->indexes = reference->indexes;
  541. }
  542. /*
  543. =================
  544. R_FreeStaticTriSurfSilIndexes
  545. =================
  546. */
  547. void R_FreeStaticTriSurfSilIndexes( srfTriangles_t *tri ) {
  548. triSilIndexAllocator.Free( tri->silIndexes );
  549. tri->silIndexes = NULL;
  550. }
  551. /*
  552. ===============
  553. R_RangeCheckIndexes
  554. Check for syntactically incorrect indexes, like out of range values.
  555. Does not check for semantics, like degenerate triangles.
  556. No vertexes is acceptable if no indexes.
  557. No indexes is acceptable.
  558. More vertexes than are referenced by indexes are acceptable.
  559. ===============
  560. */
  561. void R_RangeCheckIndexes( const srfTriangles_t *tri ) {
  562. int i;
  563. if ( tri->numIndexes < 0 ) {
  564. common->Error( "R_RangeCheckIndexes: numIndexes < 0" );
  565. }
  566. if ( tri->numVerts < 0 ) {
  567. common->Error( "R_RangeCheckIndexes: numVerts < 0" );
  568. }
  569. // must specify an integral number of triangles
  570. if ( tri->numIndexes % 3 != 0 ) {
  571. common->Error( "R_RangeCheckIndexes: numIndexes %% 3" );
  572. }
  573. for ( i = 0 ; i < tri->numIndexes ; i++ ) {
  574. if ( tri->indexes[i] < 0 || tri->indexes[i] >= tri->numVerts ) {
  575. common->Error( "R_RangeCheckIndexes: index out of range" );
  576. }
  577. }
  578. // this should not be possible unless there are unused verts
  579. if ( tri->numVerts > tri->numIndexes ) {
  580. // FIXME: find the causes of these
  581. // common->Printf( "R_RangeCheckIndexes: tri->numVerts > tri->numIndexes\n" );
  582. }
  583. }
  584. /*
  585. =================
  586. R_BoundTriSurf
  587. =================
  588. */
  589. void R_BoundTriSurf( srfTriangles_t *tri ) {
  590. SIMDProcessor->MinMax( tri->bounds[0], tri->bounds[1], tri->verts, tri->numVerts );
  591. }
  592. /*
  593. =================
  594. R_CreateSilRemap
  595. =================
  596. */
  597. static int *R_CreateSilRemap( const srfTriangles_t *tri ) {
  598. int c_removed, c_unique;
  599. int *remap;
  600. int i, j, hashKey;
  601. const idDrawVert *v1, *v2;
  602. remap = (int *)R_ClearedStaticAlloc( tri->numVerts * sizeof( remap[0] ) );
  603. if ( !r_useSilRemap.GetBool() ) {
  604. for ( i = 0 ; i < tri->numVerts ; i++ ) {
  605. remap[i] = i;
  606. }
  607. return remap;
  608. }
  609. idHashIndex hash( 1024, tri->numVerts );
  610. c_removed = 0;
  611. c_unique = 0;
  612. for ( i = 0 ; i < tri->numVerts ; i++ ) {
  613. v1 = &tri->verts[i];
  614. // see if there is an earlier vert that it can map to
  615. hashKey = hash.GenerateKey( v1->xyz );
  616. for ( j = hash.First( hashKey ); j >= 0; j = hash.Next( j ) ) {
  617. v2 = &tri->verts[j];
  618. if ( v2->xyz[0] == v1->xyz[0]
  619. && v2->xyz[1] == v1->xyz[1]
  620. && v2->xyz[2] == v1->xyz[2] ) {
  621. c_removed++;
  622. remap[i] = j;
  623. break;
  624. }
  625. }
  626. if ( j < 0 ) {
  627. c_unique++;
  628. remap[i] = i;
  629. hash.Add( hashKey, i );
  630. }
  631. }
  632. return remap;
  633. }
  634. /*
  635. =================
  636. R_CreateSilIndexes
  637. Uniquing vertexes only on xyz before creating sil edges reduces
  638. the edge count by about 20% on Q3 models
  639. =================
  640. */
  641. void R_CreateSilIndexes( srfTriangles_t *tri ) {
  642. int i;
  643. int *remap;
  644. if ( tri->silIndexes ) {
  645. triSilIndexAllocator.Free( tri->silIndexes );
  646. tri->silIndexes = NULL;
  647. }
  648. remap = R_CreateSilRemap( tri );
  649. // remap indexes to the first one
  650. tri->silIndexes = triSilIndexAllocator.Alloc( tri->numIndexes );
  651. for ( i = 0; i < tri->numIndexes; i++ ) {
  652. tri->silIndexes[i] = remap[tri->indexes[i]];
  653. }
  654. R_StaticFree( remap );
  655. }
  656. /*
  657. =====================
  658. R_CreateDupVerts
  659. =====================
  660. */
  661. void R_CreateDupVerts( srfTriangles_t *tri ) {
  662. int i;
  663. int *remap = (int *) _alloca16( tri->numVerts * sizeof( remap[0] ) );
  664. // initialize vertex remap in case there are unused verts
  665. for ( i = 0; i < tri->numVerts; i++ ) {
  666. remap[i] = i;
  667. }
  668. // set the remap based on how the silhouette indexes are remapped
  669. for ( i = 0; i < tri->numIndexes; i++ ) {
  670. remap[tri->indexes[i]] = tri->silIndexes[i];
  671. }
  672. // create duplicate vertex index based on the vertex remap
  673. int * tempDupVerts = (int *) _alloca16( tri->numVerts * 2 * sizeof( tempDupVerts[0] ) );
  674. tri->numDupVerts = 0;
  675. for ( i = 0; i < tri->numVerts; i++ ) {
  676. if ( remap[i] != i ) {
  677. tempDupVerts[tri->numDupVerts*2+0] = i;
  678. tempDupVerts[tri->numDupVerts*2+1] = remap[i];
  679. tri->numDupVerts++;
  680. }
  681. }
  682. tri->dupVerts = triDupVertAllocator.Alloc( tri->numDupVerts * 2 );
  683. memcpy( tri->dupVerts, tempDupVerts, tri->numDupVerts * 2 * sizeof( tri->dupVerts[0] ) );
  684. }
  685. /*
  686. =====================
  687. R_DeriveFacePlanes
  688. Writes the facePlanes values, overwriting existing ones if present
  689. =====================
  690. */
  691. void R_DeriveFacePlanes( srfTriangles_t *tri ) {
  692. idPlane * planes;
  693. if ( !tri->facePlanes ) {
  694. R_AllocStaticTriSurfPlanes( tri, tri->numIndexes );
  695. }
  696. planes = tri->facePlanes;
  697. #if 1
  698. SIMDProcessor->DeriveTriPlanes( planes, tri->verts, tri->numVerts, tri->indexes, tri->numIndexes );
  699. #else
  700. for ( int i = 0; i < tri->numIndexes; i+= 3, planes++ ) {
  701. int i1, i2, i3;
  702. idVec3 d1, d2, normal;
  703. idVec3 *v1, *v2, *v3;
  704. i1 = tri->indexes[i + 0];
  705. i2 = tri->indexes[i + 1];
  706. i3 = tri->indexes[i + 2];
  707. v1 = &tri->verts[i1].xyz;
  708. v2 = &tri->verts[i2].xyz;
  709. v3 = &tri->verts[i3].xyz;
  710. d1[0] = v2->x - v1->x;
  711. d1[1] = v2->y - v1->y;
  712. d1[2] = v2->z - v1->z;
  713. d2[0] = v3->x - v1->x;
  714. d2[1] = v3->y - v1->y;
  715. d2[2] = v3->z - v1->z;
  716. normal[0] = d2.y * d1.z - d2.z * d1.y;
  717. normal[1] = d2.z * d1.x - d2.x * d1.z;
  718. normal[2] = d2.x * d1.y - d2.y * d1.x;
  719. float sqrLength, invLength;
  720. sqrLength = normal.x * normal.x + normal.y * normal.y + normal.z * normal.z;
  721. invLength = idMath::RSqrt( sqrLength );
  722. (*planes)[0] = normal[0] * invLength;
  723. (*planes)[1] = normal[1] * invLength;
  724. (*planes)[2] = normal[2] * invLength;
  725. planes->FitThroughPoint( *v1 );
  726. }
  727. #endif
  728. tri->facePlanesCalculated = true;
  729. }
  730. /*
  731. =====================
  732. R_CreateVertexNormals
  733. Averages together the contributions of all faces that are
  734. used by a vertex, creating drawVert->normal
  735. =====================
  736. */
  737. void R_CreateVertexNormals( srfTriangles_t *tri ) {
  738. int i, j;
  739. const idPlane *planes;
  740. for ( i = 0 ; i < tri->numVerts ; i++ ) {
  741. tri->verts[i].normal.Zero();
  742. }
  743. if ( !tri->facePlanes || !tri->facePlanesCalculated ) {
  744. R_DeriveFacePlanes( tri );
  745. }
  746. if ( !tri->silIndexes ) {
  747. R_CreateSilIndexes( tri );
  748. }
  749. planes = tri->facePlanes;
  750. for ( i = 0 ; i < tri->numIndexes ; i += 3, planes++ ) {
  751. for ( j = 0 ; j < 3 ; j++ ) {
  752. int index = tri->silIndexes[i+j];
  753. tri->verts[index].normal += planes->Normal();
  754. }
  755. }
  756. // normalize and replicate from silIndexes to all indexes
  757. for ( i = 0 ; i < tri->numIndexes ; i++ ) {
  758. tri->verts[tri->indexes[i]].normal = tri->verts[tri->silIndexes[i]].normal;
  759. tri->verts[tri->indexes[i]].normal.Normalize();
  760. }
  761. }
  762. /*
  763. ===============
  764. R_DefineEdge
  765. ===============
  766. */
  767. static int c_duplicatedEdges, c_tripledEdges;
  768. static void R_DefineEdge( int v1, int v2, int planeNum ) {
  769. int i, hashKey;
  770. // check for degenerate edge
  771. if ( v1 == v2 ) {
  772. return;
  773. }
  774. hashKey = silEdgeHash.GenerateKey( v1, v2 );
  775. // search for a matching other side
  776. for ( i = silEdgeHash.First( hashKey ); i >= 0 && i < MAX_SIL_EDGES; i = silEdgeHash.Next( i ) ) {
  777. if ( silEdges[i].v1 == v1 && silEdges[i].v2 == v2 ) {
  778. c_duplicatedEdges++;
  779. // allow it to still create a new edge
  780. continue;
  781. }
  782. if ( silEdges[i].v2 == v1 && silEdges[i].v1 == v2 ) {
  783. if ( silEdges[i].p2 != numPlanes ) {
  784. c_tripledEdges++;
  785. // allow it to still create a new edge
  786. continue;
  787. }
  788. // this is a matching back side
  789. silEdges[i].p2 = planeNum;
  790. return;
  791. }
  792. }
  793. // define the new edge
  794. if ( numSilEdges == MAX_SIL_EDGES ) {
  795. common->DWarning( "MAX_SIL_EDGES" );
  796. return;
  797. }
  798. silEdgeHash.Add( hashKey, numSilEdges );
  799. silEdges[numSilEdges].p1 = planeNum;
  800. silEdges[numSilEdges].p2 = numPlanes;
  801. silEdges[numSilEdges].v1 = v1;
  802. silEdges[numSilEdges].v2 = v2;
  803. numSilEdges++;
  804. }
  805. /*
  806. =================
  807. SilEdgeSort
  808. =================
  809. */
  810. static int SilEdgeSort( const void *a, const void *b ) {
  811. if ( ((silEdge_t *)a)->p1 < ((silEdge_t *)b)->p1 ) {
  812. return -1;
  813. }
  814. if ( ((silEdge_t *)a)->p1 > ((silEdge_t *)b)->p1 ) {
  815. return 1;
  816. }
  817. if ( ((silEdge_t *)a)->p2 < ((silEdge_t *)b)->p2 ) {
  818. return -1;
  819. }
  820. if ( ((silEdge_t *)a)->p2 > ((silEdge_t *)b)->p2 ) {
  821. return 1;
  822. }
  823. return 0;
  824. }
  825. /*
  826. =================
  827. R_IdentifySilEdges
  828. If the surface will not deform, coplanar edges (polygon interiors)
  829. can never create silhouette plains, and can be omited
  830. =================
  831. */
  832. int c_coplanarSilEdges;
  833. int c_totalSilEdges;
  834. void R_IdentifySilEdges( srfTriangles_t *tri, bool omitCoplanarEdges ) {
  835. int i;
  836. int numTris;
  837. int shared, single;
  838. omitCoplanarEdges = false; // optimization doesn't work for some reason
  839. numTris = tri->numIndexes / 3;
  840. numSilEdges = 0;
  841. silEdgeHash.Clear();
  842. numPlanes = numTris;
  843. c_duplicatedEdges = 0;
  844. c_tripledEdges = 0;
  845. for ( i = 0 ; i < numTris ; i++ ) {
  846. int i1, i2, i3;
  847. i1 = tri->silIndexes[ i*3 + 0 ];
  848. i2 = tri->silIndexes[ i*3 + 1 ];
  849. i3 = tri->silIndexes[ i*3 + 2 ];
  850. // create the edges
  851. R_DefineEdge( i1, i2, i );
  852. R_DefineEdge( i2, i3, i );
  853. R_DefineEdge( i3, i1, i );
  854. }
  855. if ( c_duplicatedEdges || c_tripledEdges ) {
  856. common->DWarning( "%i duplicated edge directions, %i tripled edges", c_duplicatedEdges, c_tripledEdges );
  857. }
  858. // if we know that the vertexes aren't going
  859. // to deform, we can remove interior triangulation edges
  860. // on otherwise planar polygons.
  861. // I earlier believed that I could also remove concave
  862. // edges, because they are never silhouettes in the conventional sense,
  863. // but they are still needed to balance out all the true sil edges
  864. // for the shadow algorithm to function
  865. int c_coplanarCulled;
  866. c_coplanarCulled = 0;
  867. if ( omitCoplanarEdges ) {
  868. for ( i = 0 ; i < numSilEdges ; i++ ) {
  869. int i1, i2, i3;
  870. idPlane plane;
  871. int base;
  872. int j;
  873. float d;
  874. if ( silEdges[i].p2 == numPlanes ) { // the fake dangling edge
  875. continue;
  876. }
  877. base = silEdges[i].p1 * 3;
  878. i1 = tri->silIndexes[ base + 0 ];
  879. i2 = tri->silIndexes[ base + 1 ];
  880. i3 = tri->silIndexes[ base + 2 ];
  881. plane.FromPoints( tri->verts[i1].xyz, tri->verts[i2].xyz, tri->verts[i3].xyz );
  882. // check to see if points of second triangle are not coplanar
  883. base = silEdges[i].p2 * 3;
  884. for ( j = 0 ; j < 3 ; j++ ) {
  885. i1 = tri->silIndexes[ base + j ];
  886. d = plane.Distance( tri->verts[i1].xyz );
  887. if ( d != 0 ) { // even a small epsilon causes problems
  888. break;
  889. }
  890. }
  891. if ( j == 3 ) {
  892. // we can cull this sil edge
  893. memmove( &silEdges[i], &silEdges[i+1], (numSilEdges-i-1) * sizeof( silEdges[i] ) );
  894. c_coplanarCulled++;
  895. numSilEdges--;
  896. i--;
  897. }
  898. }
  899. if ( c_coplanarCulled ) {
  900. c_coplanarSilEdges += c_coplanarCulled;
  901. // common->Printf( "%i of %i sil edges coplanar culled\n", c_coplanarCulled,
  902. // c_coplanarCulled + numSilEdges );
  903. }
  904. }
  905. c_totalSilEdges += numSilEdges;
  906. // sort the sil edges based on plane number
  907. qsort( silEdges, numSilEdges, sizeof( silEdges[0] ), SilEdgeSort );
  908. // count up the distribution.
  909. // a perfectly built model should only have shared
  910. // edges, but most models will have some interpenetration
  911. // and dangling edges
  912. shared = 0;
  913. single = 0;
  914. for ( i = 0 ; i < numSilEdges ; i++ ) {
  915. if ( silEdges[i].p2 == numPlanes ) {
  916. single++;
  917. } else {
  918. shared++;
  919. }
  920. }
  921. if ( !single ) {
  922. tri->perfectHull = true;
  923. } else {
  924. tri->perfectHull = false;
  925. }
  926. tri->numSilEdges = numSilEdges;
  927. tri->silEdges = triSilEdgeAllocator.Alloc( numSilEdges );
  928. memcpy( tri->silEdges, silEdges, numSilEdges * sizeof( tri->silEdges[0] ) );
  929. }
  930. /*
  931. ===============
  932. R_FaceNegativePolarity
  933. Returns true if the texture polarity of the face is negative, false if it is positive or zero
  934. ===============
  935. */
  936. static bool R_FaceNegativePolarity( const srfTriangles_t *tri, int firstIndex ) {
  937. idDrawVert *a, *b, *c;
  938. float area;
  939. float d0[5], d1[5];
  940. a = tri->verts + tri->indexes[firstIndex + 0];
  941. b = tri->verts + tri->indexes[firstIndex + 1];
  942. c = tri->verts + tri->indexes[firstIndex + 2];
  943. d0[3] = b->st[0] - a->st[0];
  944. d0[4] = b->st[1] - a->st[1];
  945. d1[3] = c->st[0] - a->st[0];
  946. d1[4] = c->st[1] - a->st[1];
  947. area = d0[3] * d1[4] - d0[4] * d1[3];
  948. if ( area >= 0 ) {
  949. return false;
  950. }
  951. return true;
  952. }
  953. /*
  954. ==================
  955. R_DeriveFaceTangents
  956. ==================
  957. */
  958. typedef struct {
  959. idVec3 tangents[2];
  960. bool negativePolarity;
  961. bool degenerate;
  962. } faceTangents_t;
  963. static void R_DeriveFaceTangents( const srfTriangles_t *tri, faceTangents_t *faceTangents ) {
  964. int i;
  965. int c_textureDegenerateFaces;
  966. int c_positive, c_negative;
  967. faceTangents_t *ft;
  968. idDrawVert *a, *b, *c;
  969. //
  970. // calculate tangent vectors for each face in isolation
  971. //
  972. c_positive = 0;
  973. c_negative = 0;
  974. c_textureDegenerateFaces = 0;
  975. for ( i = 0 ; i < tri->numIndexes ; i+=3 ) {
  976. float area;
  977. idVec3 temp;
  978. float d0[5], d1[5];
  979. ft = &faceTangents[i/3];
  980. a = tri->verts + tri->indexes[i + 0];
  981. b = tri->verts + tri->indexes[i + 1];
  982. c = tri->verts + tri->indexes[i + 2];
  983. d0[0] = b->xyz[0] - a->xyz[0];
  984. d0[1] = b->xyz[1] - a->xyz[1];
  985. d0[2] = b->xyz[2] - a->xyz[2];
  986. d0[3] = b->st[0] - a->st[0];
  987. d0[4] = b->st[1] - a->st[1];
  988. d1[0] = c->xyz[0] - a->xyz[0];
  989. d1[1] = c->xyz[1] - a->xyz[1];
  990. d1[2] = c->xyz[2] - a->xyz[2];
  991. d1[3] = c->st[0] - a->st[0];
  992. d1[4] = c->st[1] - a->st[1];
  993. area = d0[3] * d1[4] - d0[4] * d1[3];
  994. if ( fabs( area ) < 1e-20f ) {
  995. ft->negativePolarity = false;
  996. ft->degenerate = true;
  997. ft->tangents[0].Zero();
  998. ft->tangents[1].Zero();
  999. c_textureDegenerateFaces++;
  1000. continue;
  1001. }
  1002. if ( area > 0.0f ) {
  1003. ft->negativePolarity = false;
  1004. c_positive++;
  1005. } else {
  1006. ft->negativePolarity = true;
  1007. c_negative++;
  1008. }
  1009. ft->degenerate = false;
  1010. #ifdef USE_INVA
  1011. float inva = area < 0.0f ? -1 : 1; // was = 1.0f / area;
  1012. temp[0] = (d0[0] * d1[4] - d0[4] * d1[0]) * inva;
  1013. temp[1] = (d0[1] * d1[4] - d0[4] * d1[1]) * inva;
  1014. temp[2] = (d0[2] * d1[4] - d0[4] * d1[2]) * inva;
  1015. temp.Normalize();
  1016. ft->tangents[0] = temp;
  1017. temp[0] = (d0[3] * d1[0] - d0[0] * d1[3]) * inva;
  1018. temp[1] = (d0[3] * d1[1] - d0[1] * d1[3]) * inva;
  1019. temp[2] = (d0[3] * d1[2] - d0[2] * d1[3]) * inva;
  1020. temp.Normalize();
  1021. ft->tangents[1] = temp;
  1022. #else
  1023. temp[0] = (d0[0] * d1[4] - d0[4] * d1[0]);
  1024. temp[1] = (d0[1] * d1[4] - d0[4] * d1[1]);
  1025. temp[2] = (d0[2] * d1[4] - d0[4] * d1[2]);
  1026. temp.Normalize();
  1027. ft->tangents[0] = temp;
  1028. temp[0] = (d0[3] * d1[0] - d0[0] * d1[3]);
  1029. temp[1] = (d0[3] * d1[1] - d0[1] * d1[3]);
  1030. temp[2] = (d0[3] * d1[2] - d0[2] * d1[3]);
  1031. temp.Normalize();
  1032. ft->tangents[1] = temp;
  1033. #endif
  1034. }
  1035. }
  1036. /*
  1037. ===================
  1038. R_DuplicateMirroredVertexes
  1039. Modifies the surface to bust apart any verts that are shared by both positive and
  1040. negative texture polarities, so tangent space smoothing at the vertex doesn't
  1041. degenerate.
  1042. This will create some identical vertexes (which will eventually get different tangent
  1043. vectors), so never optimize the resulting mesh, or it will get the mirrored edges back.
  1044. Reallocates tri->verts and changes tri->indexes in place
  1045. Silindexes are unchanged by this.
  1046. sets mirroredVerts and mirroredVerts[]
  1047. ===================
  1048. */
  1049. typedef struct {
  1050. bool polarityUsed[2];
  1051. int negativeRemap;
  1052. } tangentVert_t;
  1053. static void R_DuplicateMirroredVertexes( srfTriangles_t *tri ) {
  1054. tangentVert_t *tverts, *vert;
  1055. int i, j;
  1056. int totalVerts;
  1057. int numMirror;
  1058. tverts = (tangentVert_t *)_alloca16( tri->numVerts * sizeof( *tverts ) );
  1059. memset( tverts, 0, tri->numVerts * sizeof( *tverts ) );
  1060. // determine texture polarity of each surface
  1061. // mark each vert with the polarities it uses
  1062. for ( i = 0 ; i < tri->numIndexes ; i+=3 ) {
  1063. int polarity;
  1064. polarity = R_FaceNegativePolarity( tri, i );
  1065. for ( j = 0 ; j < 3 ; j++ ) {
  1066. tverts[tri->indexes[i+j]].polarityUsed[ polarity ] = true;
  1067. }
  1068. }
  1069. // now create new verts as needed
  1070. totalVerts = tri->numVerts;
  1071. for ( i = 0 ; i < tri->numVerts ; i++ ) {
  1072. vert = &tverts[i];
  1073. if ( vert->polarityUsed[0] && vert->polarityUsed[1] ) {
  1074. vert->negativeRemap = totalVerts;
  1075. totalVerts++;
  1076. }
  1077. }
  1078. tri->numMirroredVerts = totalVerts - tri->numVerts;
  1079. // now create the new list
  1080. if ( totalVerts == tri->numVerts ) {
  1081. tri->mirroredVerts = NULL;
  1082. return;
  1083. }
  1084. tri->mirroredVerts = triMirroredVertAllocator.Alloc( tri->numMirroredVerts );
  1085. #ifdef USE_TRI_DATA_ALLOCATOR
  1086. tri->verts = triVertexAllocator.Resize( tri->verts, totalVerts );
  1087. #else
  1088. idDrawVert *oldVerts = tri->verts;
  1089. R_AllocStaticTriSurfVerts( tri, totalVerts );
  1090. memcpy( tri->verts, oldVerts, tri->numVerts * sizeof( tri->verts[0] ) );
  1091. triVertexAllocator.Free( oldVerts );
  1092. #endif
  1093. // create the duplicates
  1094. numMirror = 0;
  1095. for ( i = 0 ; i < tri->numVerts ; i++ ) {
  1096. j = tverts[i].negativeRemap;
  1097. if ( j ) {
  1098. tri->verts[j] = tri->verts[i];
  1099. tri->mirroredVerts[numMirror] = i;
  1100. numMirror++;
  1101. }
  1102. }
  1103. tri->numVerts = totalVerts;
  1104. // change the indexes
  1105. for ( i = 0 ; i < tri->numIndexes ; i++ ) {
  1106. if ( tverts[tri->indexes[i]].negativeRemap &&
  1107. R_FaceNegativePolarity( tri, 3*(i/3) ) ) {
  1108. tri->indexes[i] = tverts[tri->indexes[i]].negativeRemap;
  1109. }
  1110. }
  1111. tri->numVerts = totalVerts;
  1112. }
  1113. /*
  1114. =================
  1115. R_DeriveTangentsWithoutNormals
  1116. Build texture space tangents for bump mapping
  1117. If a surface is deformed, this must be recalculated
  1118. This assumes that any mirrored vertexes have already been duplicated, so
  1119. any shared vertexes will have the tangent spaces smoothed across.
  1120. Texture wrapping slightly complicates this, but as long as the normals
  1121. are shared, and the tangent vectors are projected onto the normals, the
  1122. separate vertexes should wind up with identical tangent spaces.
  1123. mirroring a normalmap WILL cause a slightly visible seam unless the normals
  1124. are completely flat around the edge's full bilerp support.
  1125. Vertexes which are smooth shaded must have their tangent vectors
  1126. in the same plane, which will allow a seamless
  1127. rendering as long as the normal map is even on both sides of the
  1128. seam.
  1129. A smooth shaded surface may have multiple tangent vectors at a vertex
  1130. due to texture seams or mirroring, but it should only have a single
  1131. normal vector.
  1132. Each triangle has a pair of tangent vectors in it's plane
  1133. Should we consider having vertexes point at shared tangent spaces
  1134. to save space or speed transforms?
  1135. this version only handles bilateral symetry
  1136. =================
  1137. */
  1138. void R_DeriveTangentsWithoutNormals( srfTriangles_t *tri ) {
  1139. int i, j;
  1140. faceTangents_t *faceTangents;
  1141. faceTangents_t *ft;
  1142. idDrawVert *vert;
  1143. faceTangents = (faceTangents_t *)_alloca16( sizeof(faceTangents[0]) * tri->numIndexes/3 );
  1144. R_DeriveFaceTangents( tri, faceTangents );
  1145. // clear the tangents
  1146. for ( i = 0 ; i < tri->numVerts ; i++ ) {
  1147. tri->verts[i].tangents[0].Zero();
  1148. tri->verts[i].tangents[1].Zero();
  1149. }
  1150. // sum up the neighbors
  1151. for ( i = 0 ; i < tri->numIndexes ; i+=3 ) {
  1152. ft = &faceTangents[i/3];
  1153. // for each vertex on this face
  1154. for ( j = 0 ; j < 3 ; j++ ) {
  1155. vert = &tri->verts[tri->indexes[i+j]];
  1156. vert->tangents[0] += ft->tangents[0];
  1157. vert->tangents[1] += ft->tangents[1];
  1158. }
  1159. }
  1160. #if 0
  1161. // sum up both sides of the mirrored verts
  1162. // so the S vectors exactly mirror, and the T vectors are equal
  1163. for ( i = 0 ; i < tri->numMirroredVerts ; i++ ) {
  1164. idDrawVert *v1, *v2;
  1165. v1 = &tri->verts[ tri->numVerts - tri->numMirroredVerts + i ];
  1166. v2 = &tri->verts[ tri->mirroredVerts[i] ];
  1167. v1->tangents[0] -= v2->tangents[0];
  1168. v1->tangents[1] += v2->tangents[1];
  1169. v2->tangents[0] = vec3_origin - v1->tangents[0];
  1170. v2->tangents[1] = v1->tangents[1];
  1171. }
  1172. #endif
  1173. // project the summed vectors onto the normal plane
  1174. // and normalize. The tangent vectors will not necessarily
  1175. // be orthogonal to each other, but they will be orthogonal
  1176. // to the surface normal.
  1177. for ( i = 0 ; i < tri->numVerts ; i++ ) {
  1178. vert = &tri->verts[i];
  1179. for ( j = 0 ; j < 2 ; j++ ) {
  1180. float d;
  1181. d = vert->tangents[j] * vert->normal;
  1182. vert->tangents[j] = vert->tangents[j] - d * vert->normal;
  1183. vert->tangents[j].Normalize();
  1184. }
  1185. }
  1186. tri->tangentsCalculated = true;
  1187. }
  1188. static ID_INLINE void VectorNormalizeFast2( const idVec3 &v, idVec3 &out) {
  1189. float ilength;
  1190. ilength = idMath::RSqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] );
  1191. out[0] = v[0] * ilength;
  1192. out[1] = v[1] * ilength;
  1193. out[2] = v[2] * ilength;
  1194. }
  1195. /*
  1196. ===================
  1197. R_BuildDominantTris
  1198. Find the largest triangle that uses each vertex
  1199. ===================
  1200. */
  1201. typedef struct {
  1202. int vertexNum;
  1203. int faceNum;
  1204. } indexSort_t;
  1205. static int IndexSort( const void *a, const void *b ) {
  1206. if ( ((indexSort_t *)a)->vertexNum < ((indexSort_t *)b)->vertexNum ) {
  1207. return -1;
  1208. }
  1209. if ( ((indexSort_t *)a)->vertexNum > ((indexSort_t *)b)->vertexNum ) {
  1210. return 1;
  1211. }
  1212. return 0;
  1213. }
  1214. void R_BuildDominantTris( srfTriangles_t *tri ) {
  1215. int i, j;
  1216. dominantTri_t *dt;
  1217. indexSort_t *ind = (indexSort_t *)R_StaticAlloc( tri->numIndexes * sizeof( *ind ) );
  1218. for ( i = 0; i < tri->numIndexes; i++ ) {
  1219. ind[i].vertexNum = tri->indexes[i];
  1220. ind[i].faceNum = i / 3;
  1221. }
  1222. qsort( ind, tri->numIndexes, sizeof( *ind ), IndexSort );
  1223. tri->dominantTris = dt = triDominantTrisAllocator.Alloc( tri->numVerts );
  1224. memset( dt, 0, tri->numVerts * sizeof( dt[0] ) );
  1225. for ( i = 0; i < tri->numIndexes; i += j ) {
  1226. float maxArea = 0;
  1227. int vertNum = ind[i].vertexNum;
  1228. for ( j = 0; i + j < tri->numIndexes && ind[i+j].vertexNum == vertNum; j++ ) {
  1229. float d0[5], d1[5];
  1230. idDrawVert *a, *b, *c;
  1231. idVec3 normal, tangent, bitangent;
  1232. int i1 = tri->indexes[ind[i+j].faceNum * 3 + 0];
  1233. int i2 = tri->indexes[ind[i+j].faceNum * 3 + 1];
  1234. int i3 = tri->indexes[ind[i+j].faceNum * 3 + 2];
  1235. a = tri->verts + i1;
  1236. b = tri->verts + i2;
  1237. c = tri->verts + i3;
  1238. d0[0] = b->xyz[0] - a->xyz[0];
  1239. d0[1] = b->xyz[1] - a->xyz[1];
  1240. d0[2] = b->xyz[2] - a->xyz[2];
  1241. d0[3] = b->st[0] - a->st[0];
  1242. d0[4] = b->st[1] - a->st[1];
  1243. d1[0] = c->xyz[0] - a->xyz[0];
  1244. d1[1] = c->xyz[1] - a->xyz[1];
  1245. d1[2] = c->xyz[2] - a->xyz[2];
  1246. d1[3] = c->st[0] - a->st[0];
  1247. d1[4] = c->st[1] - a->st[1];
  1248. normal[0] = ( d1[1] * d0[2] - d1[2] * d0[1] );
  1249. normal[1] = ( d1[2] * d0[0] - d1[0] * d0[2] );
  1250. normal[2] = ( d1[0] * d0[1] - d1[1] * d0[0] );
  1251. float area = normal.Length();
  1252. // if this is smaller than what we already have, skip it
  1253. if ( area < maxArea ) {
  1254. continue;
  1255. }
  1256. maxArea = area;
  1257. if ( i1 == vertNum ) {
  1258. dt[vertNum].v2 = i2;
  1259. dt[vertNum].v3 = i3;
  1260. } else if ( i2 == vertNum ) {
  1261. dt[vertNum].v2 = i3;
  1262. dt[vertNum].v3 = i1;
  1263. } else {
  1264. dt[vertNum].v2 = i1;
  1265. dt[vertNum].v3 = i2;
  1266. }
  1267. float len = area;
  1268. if ( len < 0.001f ) {
  1269. len = 0.001f;
  1270. }
  1271. dt[vertNum].normalizationScale[2] = 1.0f / len; // normal
  1272. // texture area
  1273. area = d0[3] * d1[4] - d0[4] * d1[3];
  1274. tangent[0] = ( d0[0] * d1[4] - d0[4] * d1[0] );
  1275. tangent[1] = ( d0[1] * d1[4] - d0[4] * d1[1] );
  1276. tangent[2] = ( d0[2] * d1[4] - d0[4] * d1[2] );
  1277. len = tangent.Length();
  1278. if ( len < 0.001f ) {
  1279. len = 0.001f;
  1280. }
  1281. dt[vertNum].normalizationScale[0] = ( area > 0 ? 1 : -1 ) / len; // tangents[0]
  1282. bitangent[0] = ( d0[3] * d1[0] - d0[0] * d1[3] );
  1283. bitangent[1] = ( d0[3] * d1[1] - d0[1] * d1[3] );
  1284. bitangent[2] = ( d0[3] * d1[2] - d0[2] * d1[3] );
  1285. len = bitangent.Length();
  1286. if ( len < 0.001f ) {
  1287. len = 0.001f;
  1288. }
  1289. #ifdef DERIVE_UNSMOOTHED_BITANGENT
  1290. dt[vertNum].normalizationScale[1] = ( area > 0 ? 1 : -1 );
  1291. #else
  1292. dt[vertNum].normalizationScale[1] = ( area > 0 ? 1 : -1 ) / len; // tangents[1]
  1293. #endif
  1294. }
  1295. }
  1296. R_StaticFree( ind );
  1297. }
  1298. /*
  1299. ====================
  1300. R_DeriveUnsmoothedTangents
  1301. Uses the single largest area triangle for each vertex, instead of smoothing over all
  1302. ====================
  1303. */
  1304. void R_DeriveUnsmoothedTangents( srfTriangles_t *tri ) {
  1305. if ( tri->tangentsCalculated ) {
  1306. return;
  1307. }
  1308. #if 1
  1309. SIMDProcessor->DeriveUnsmoothedTangents( tri->verts, tri->dominantTris, tri->numVerts );
  1310. #else
  1311. for ( int i = 0 ; i < tri->numVerts ; i++ ) {
  1312. idVec3 temp;
  1313. float d0[5], d1[5];
  1314. idDrawVert *a, *b, *c;
  1315. dominantTri_t *dt = &tri->dominantTris[i];
  1316. a = tri->verts + i;
  1317. b = tri->verts + dt->v2;
  1318. c = tri->verts + dt->v3;
  1319. d0[0] = b->xyz[0] - a->xyz[0];
  1320. d0[1] = b->xyz[1] - a->xyz[1];
  1321. d0[2] = b->xyz[2] - a->xyz[2];
  1322. d0[3] = b->st[0] - a->st[0];
  1323. d0[4] = b->st[1] - a->st[1];
  1324. d1[0] = c->xyz[0] - a->xyz[0];
  1325. d1[1] = c->xyz[1] - a->xyz[1];
  1326. d1[2] = c->xyz[2] - a->xyz[2];
  1327. d1[3] = c->st[0] - a->st[0];
  1328. d1[4] = c->st[1] - a->st[1];
  1329. a->normal[0] = dt->normalizationScale[2] * ( d1[1] * d0[2] - d1[2] * d0[1] );
  1330. a->normal[1] = dt->normalizationScale[2] * ( d1[2] * d0[0] - d1[0] * d0[2] );
  1331. a->normal[2] = dt->normalizationScale[2] * ( d1[0] * d0[1] - d1[1] * d0[0] );
  1332. a->tangents[0][0] = dt->normalizationScale[0] * ( d0[0] * d1[4] - d0[4] * d1[0] );
  1333. a->tangents[0][1] = dt->normalizationScale[0] * ( d0[1] * d1[4] - d0[4] * d1[1] );
  1334. a->tangents[0][2] = dt->normalizationScale[0] * ( d0[2] * d1[4] - d0[4] * d1[2] );
  1335. #ifdef DERIVE_UNSMOOTHED_BITANGENT
  1336. // derive the bitangent for a completely orthogonal axis,
  1337. // instead of using the texture T vector
  1338. a->tangents[1][0] = dt->normalizationScale[1] * ( a->normal[2] * a->tangents[0][1] - a->normal[1] * a->tangents[0][2] );
  1339. a->tangents[1][1] = dt->normalizationScale[1] * ( a->normal[0] * a->tangents[0][2] - a->normal[2] * a->tangents[0][0] );
  1340. a->tangents[1][2] = dt->normalizationScale[1] * ( a->normal[1] * a->tangents[0][0] - a->normal[0] * a->tangents[0][1] );
  1341. #else
  1342. // calculate the bitangent from the texture T vector
  1343. a->tangents[1][0] = dt->normalizationScale[1] * ( d0[3] * d1[0] - d0[0] * d1[3] );
  1344. a->tangents[1][1] = dt->normalizationScale[1] * ( d0[3] * d1[1] - d0[1] * d1[3] );
  1345. a->tangents[1][2] = dt->normalizationScale[1] * ( d0[3] * d1[2] - d0[2] * d1[3] );
  1346. #endif
  1347. }
  1348. #endif
  1349. tri->tangentsCalculated = true;
  1350. }
  1351. /*
  1352. ==================
  1353. R_DeriveTangents
  1354. This is called once for static surfaces, and every frame for deforming surfaces
  1355. Builds tangents, normals, and face planes
  1356. ==================
  1357. */
  1358. void R_DeriveTangents( srfTriangles_t *tri, bool allocFacePlanes ) {
  1359. int i;
  1360. idPlane *planes;
  1361. if ( tri->dominantTris != NULL ) {
  1362. R_DeriveUnsmoothedTangents( tri );
  1363. return;
  1364. }
  1365. if ( tri->tangentsCalculated ) {
  1366. return;
  1367. }
  1368. tr.pc.c_tangentIndexes += tri->numIndexes;
  1369. if ( !tri->facePlanes && allocFacePlanes ) {
  1370. R_AllocStaticTriSurfPlanes( tri, tri->numIndexes );
  1371. }
  1372. planes = tri->facePlanes;
  1373. #if 1
  1374. if ( !planes ) {
  1375. planes = (idPlane *)_alloca16( ( tri->numIndexes / 3 ) * sizeof( planes[0] ) );
  1376. }
  1377. SIMDProcessor->DeriveTangents( planes, tri->verts, tri->numVerts, tri->indexes, tri->numIndexes );
  1378. #else
  1379. for ( i = 0; i < tri->numVerts; i++ ) {
  1380. tri->verts[i].normal.Zero();
  1381. tri->verts[i].tangents[0].Zero();
  1382. tri->verts[i].tangents[1].Zero();
  1383. }
  1384. for ( i = 0; i < tri->numIndexes; i += 3 ) {
  1385. // make face tangents
  1386. float d0[5], d1[5];
  1387. idDrawVert *a, *b, *c;
  1388. idVec3 temp, normal, tangents[2];
  1389. a = tri->verts + tri->indexes[i + 0];
  1390. b = tri->verts + tri->indexes[i + 1];
  1391. c = tri->verts + tri->indexes[i + 2];
  1392. d0[0] = b->xyz[0] - a->xyz[0];
  1393. d0[1] = b->xyz[1] - a->xyz[1];
  1394. d0[2] = b->xyz[2] - a->xyz[2];
  1395. d0[3] = b->st[0] - a->st[0];
  1396. d0[4] = b->st[1] - a->st[1];
  1397. d1[0] = c->xyz[0] - a->xyz[0];
  1398. d1[1] = c->xyz[1] - a->xyz[1];
  1399. d1[2] = c->xyz[2] - a->xyz[2];
  1400. d1[3] = c->st[0] - a->st[0];
  1401. d1[4] = c->st[1] - a->st[1];
  1402. // normal
  1403. temp[0] = d1[1] * d0[2] - d1[2] * d0[1];
  1404. temp[1] = d1[2] * d0[0] - d1[0] * d0[2];
  1405. temp[2] = d1[0] * d0[1] - d1[1] * d0[0];
  1406. VectorNormalizeFast2( temp, normal );
  1407. #ifdef USE_INVA
  1408. float area = d0[3] * d1[4] - d0[4] * d1[3];
  1409. float inva = area < 0.0f ? -1 : 1; // was = 1.0f / area;
  1410. temp[0] = (d0[0] * d1[4] - d0[4] * d1[0]) * inva;
  1411. temp[1] = (d0[1] * d1[4] - d0[4] * d1[1]) * inva;
  1412. temp[2] = (d0[2] * d1[4] - d0[4] * d1[2]) * inva;
  1413. VectorNormalizeFast2( temp, tangents[0] );
  1414. temp[0] = (d0[3] * d1[0] - d0[0] * d1[3]) * inva;
  1415. temp[1] = (d0[3] * d1[1] - d0[1] * d1[3]) * inva;
  1416. temp[2] = (d0[3] * d1[2] - d0[2] * d1[3]) * inva;
  1417. VectorNormalizeFast2( temp, tangents[1] );
  1418. #else
  1419. temp[0] = (d0[0] * d1[4] - d0[4] * d1[0]);
  1420. temp[1] = (d0[1] * d1[4] - d0[4] * d1[1]);
  1421. temp[2] = (d0[2] * d1[4] - d0[4] * d1[2]);
  1422. VectorNormalizeFast2( temp, tangents[0] );
  1423. temp[0] = (d0[3] * d1[0] - d0[0] * d1[3]);
  1424. temp[1] = (d0[3] * d1[1] - d0[1] * d1[3]);
  1425. temp[2] = (d0[3] * d1[2] - d0[2] * d1[3]);
  1426. VectorNormalizeFast2( temp, tangents[1] );
  1427. #endif
  1428. // sum up the tangents and normals for each vertex on this face
  1429. for ( int j = 0 ; j < 3 ; j++ ) {
  1430. vert = &tri->verts[tri->indexes[i+j]];
  1431. vert->normal += normal;
  1432. vert->tangents[0] += tangents[0];
  1433. vert->tangents[1] += tangents[1];
  1434. }
  1435. if ( planes ) {
  1436. planes->Normal() = normal;
  1437. planes->FitThroughPoint( a->xyz );
  1438. planes++;
  1439. }
  1440. }
  1441. #endif
  1442. #if 0
  1443. if ( tri->silIndexes != NULL ) {
  1444. for ( i = 0; i < tri->numVerts; i++ ) {
  1445. tri->verts[i].normal.Zero();
  1446. }
  1447. for ( i = 0; i < tri->numIndexes; i++ ) {
  1448. tri->verts[tri->silIndexes[i]].normal += planes[i/3].Normal();
  1449. }
  1450. for ( i = 0 ; i < tri->numIndexes ; i++ ) {
  1451. tri->verts[tri->indexes[i]].normal = tri->verts[tri->silIndexes[i]].normal;
  1452. }
  1453. }
  1454. #else
  1455. int *dupVerts = tri->dupVerts;
  1456. idDrawVert *verts = tri->verts;
  1457. // add the normal of a duplicated vertex to the normal of the first vertex with the same XYZ
  1458. for ( i = 0; i < tri->numDupVerts; i++ ) {
  1459. verts[dupVerts[i*2+0]].normal += verts[dupVerts[i*2+1]].normal;
  1460. }
  1461. // copy vertex normals to duplicated vertices
  1462. for ( i = 0; i < tri->numDupVerts; i++ ) {
  1463. verts[dupVerts[i*2+1]].normal = verts[dupVerts[i*2+0]].normal;
  1464. }
  1465. #endif
  1466. #if 0
  1467. // sum up both sides of the mirrored verts
  1468. // so the S vectors exactly mirror, and the T vectors are equal
  1469. for ( i = 0 ; i < tri->numMirroredVerts ; i++ ) {
  1470. idDrawVert *v1, *v2;
  1471. v1 = &tri->verts[ tri->numVerts - tri->numMirroredVerts + i ];
  1472. v2 = &tri->verts[ tri->mirroredVerts[i] ];
  1473. v1->tangents[0] -= v2->tangents[0];
  1474. v1->tangents[1] += v2->tangents[1];
  1475. v2->tangents[0] = vec3_origin - v1->tangents[0];
  1476. v2->tangents[1] = v1->tangents[1];
  1477. }
  1478. #endif
  1479. // project the summed vectors onto the normal plane
  1480. // and normalize. The tangent vectors will not necessarily
  1481. // be orthogonal to each other, but they will be orthogonal
  1482. // to the surface normal.
  1483. #if 1
  1484. SIMDProcessor->NormalizeTangents( tri->verts, tri->numVerts );
  1485. #else
  1486. for ( i = 0 ; i < tri->numVerts ; i++ ) {
  1487. idDrawVert *vert = &tri->verts[i];
  1488. VectorNormalizeFast2( vert->normal, vert->normal );
  1489. // project the tangent vectors
  1490. for ( int j = 0 ; j < 2 ; j++ ) {
  1491. float d;
  1492. d = vert->tangents[j] * vert->normal;
  1493. vert->tangents[j] = vert->tangents[j] - d * vert->normal;
  1494. VectorNormalizeFast2( vert->tangents[j], vert->tangents[j] );
  1495. }
  1496. }
  1497. #endif
  1498. tri->tangentsCalculated = true;
  1499. tri->facePlanesCalculated = true;
  1500. }
  1501. /*
  1502. =================
  1503. R_RemoveDuplicatedTriangles
  1504. silIndexes must have already been calculated
  1505. silIndexes are used instead of indexes, because duplicated
  1506. triangles could have different texture coordinates.
  1507. =================
  1508. */
  1509. void R_RemoveDuplicatedTriangles( srfTriangles_t *tri ) {
  1510. int c_removed;
  1511. int i, j, r;
  1512. int a, b, c;
  1513. c_removed = 0;
  1514. // check for completely duplicated triangles
  1515. // any rotation of the triangle is still the same, but a mirroring
  1516. // is considered different
  1517. for ( i = 0 ; i < tri->numIndexes ; i+=3 ) {
  1518. for ( r = 0 ; r < 3 ; r++ ) {
  1519. a = tri->silIndexes[i+r];
  1520. b = tri->silIndexes[i+(r+1)%3];
  1521. c = tri->silIndexes[i+(r+2)%3];
  1522. for ( j = i + 3 ; j < tri->numIndexes ; j+=3 ) {
  1523. if ( tri->silIndexes[j] == a && tri->silIndexes[j+1] == b && tri->silIndexes[j+2] == c ) {
  1524. c_removed++;
  1525. memmove( tri->indexes + j, tri->indexes + j + 3, ( tri->numIndexes - j - 3 ) * sizeof( tri->indexes[0] ) );
  1526. memmove( tri->silIndexes + j, tri->silIndexes + j + 3, ( tri->numIndexes - j - 3 ) * sizeof( tri->silIndexes[0] ) );
  1527. tri->numIndexes -= 3;
  1528. j -= 3;
  1529. }
  1530. }
  1531. }
  1532. }
  1533. if ( c_removed ) {
  1534. common->Printf( "removed %i duplicated triangles\n", c_removed );
  1535. }
  1536. }
  1537. /*
  1538. =================
  1539. R_RemoveDegenerateTriangles
  1540. silIndexes must have already been calculated
  1541. =================
  1542. */
  1543. void R_RemoveDegenerateTriangles( srfTriangles_t *tri ) {
  1544. int c_removed;
  1545. int i;
  1546. int a, b, c;
  1547. // check for completely degenerate triangles
  1548. c_removed = 0;
  1549. for ( i = 0; i < tri->numIndexes; i += 3 ) {
  1550. a = tri->silIndexes[i];
  1551. b = tri->silIndexes[i+1];
  1552. c = tri->silIndexes[i+2];
  1553. if ( a == b || a == c || b == c ) {
  1554. c_removed++;
  1555. memmove( tri->indexes + i, tri->indexes + i + 3, ( tri->numIndexes - i - 3 ) * sizeof( tri->indexes[0] ) );
  1556. if ( tri->silIndexes ) {
  1557. memmove( tri->silIndexes + i, tri->silIndexes + i + 3, ( tri->numIndexes - i - 3 ) * sizeof( tri->silIndexes[0] ) );
  1558. }
  1559. tri->numIndexes -= 3;
  1560. i -= 3;
  1561. }
  1562. }
  1563. // this doesn't free the memory used by the unused verts
  1564. if ( c_removed ) {
  1565. common->Printf( "removed %i degenerate triangles\n", c_removed );
  1566. }
  1567. }
  1568. /*
  1569. =================
  1570. R_TestDegenerateTextureSpace
  1571. =================
  1572. */
  1573. void R_TestDegenerateTextureSpace( srfTriangles_t *tri ) {
  1574. int c_degenerate;
  1575. int i;
  1576. // check for triangles with a degenerate texture space
  1577. c_degenerate = 0;
  1578. for ( i = 0; i < tri->numIndexes; i += 3 ) {
  1579. const idDrawVert &a = tri->verts[tri->indexes[i+0]];
  1580. const idDrawVert &b = tri->verts[tri->indexes[i+1]];
  1581. const idDrawVert &c = tri->verts[tri->indexes[i+2]];
  1582. if ( a.st == b.st || b.st == c.st || c.st == a.st ) {
  1583. c_degenerate++;
  1584. }
  1585. }
  1586. if ( c_degenerate ) {
  1587. // common->Printf( "%d triangles with a degenerate texture space\n", c_degenerate );
  1588. }
  1589. }
  1590. /*
  1591. =================
  1592. R_RemoveUnusedVerts
  1593. =================
  1594. */
  1595. void R_RemoveUnusedVerts( srfTriangles_t *tri ) {
  1596. int i;
  1597. int *mark;
  1598. int index;
  1599. int used;
  1600. mark = (int *)R_ClearedStaticAlloc( tri->numVerts * sizeof( *mark ) );
  1601. for ( i = 0 ; i < tri->numIndexes ; i++ ) {
  1602. index = tri->indexes[i];
  1603. if ( index < 0 || index >= tri->numVerts ) {
  1604. common->Error( "R_RemoveUnusedVerts: bad index" );
  1605. }
  1606. mark[ index ] = 1;
  1607. if ( tri->silIndexes ) {
  1608. index = tri->silIndexes[i];
  1609. if ( index < 0 || index >= tri->numVerts ) {
  1610. common->Error( "R_RemoveUnusedVerts: bad index" );
  1611. }
  1612. mark[ index ] = 1;
  1613. }
  1614. }
  1615. used = 0;
  1616. for ( i = 0 ; i < tri->numVerts ; i++ ) {
  1617. if ( !mark[i] ) {
  1618. continue;
  1619. }
  1620. mark[i] = used + 1;
  1621. used++;
  1622. }
  1623. if ( used != tri->numVerts ) {
  1624. for ( i = 0 ; i < tri->numIndexes ; i++ ) {
  1625. tri->indexes[i] = mark[ tri->indexes[i] ] - 1;
  1626. if ( tri->silIndexes ) {
  1627. tri->silIndexes[i] = mark[ tri->silIndexes[i] ] - 1;
  1628. }
  1629. }
  1630. tri->numVerts = used;
  1631. for ( i = 0 ; i < tri->numVerts ; i++ ) {
  1632. index = mark[ i ];
  1633. if ( !index ) {
  1634. continue;
  1635. }
  1636. tri->verts[ index - 1 ] = tri->verts[i];
  1637. }
  1638. // this doesn't realloc the arrays to save the memory used by the unused verts
  1639. }
  1640. R_StaticFree( mark );
  1641. }
  1642. /*
  1643. =================
  1644. R_MergeSurfaceList
  1645. Only deals with vertexes and indexes, not silhouettes, planes, etc.
  1646. Does NOT perform a cleanup triangles, so there may be duplicated verts in the result.
  1647. =================
  1648. */
  1649. srfTriangles_t *R_MergeSurfaceList( const srfTriangles_t **surfaces, int numSurfaces ) {
  1650. srfTriangles_t *newTri;
  1651. const srfTriangles_t *tri;
  1652. int i, j;
  1653. int totalVerts;
  1654. int totalIndexes;
  1655. totalVerts = 0;
  1656. totalIndexes = 0;
  1657. for ( i = 0 ; i < numSurfaces ; i++ ) {
  1658. totalVerts += surfaces[i]->numVerts;
  1659. totalIndexes += surfaces[i]->numIndexes;
  1660. }
  1661. newTri = R_AllocStaticTriSurf();
  1662. newTri->numVerts = totalVerts;
  1663. newTri->numIndexes = totalIndexes;
  1664. R_AllocStaticTriSurfVerts( newTri, newTri->numVerts );
  1665. R_AllocStaticTriSurfIndexes( newTri, newTri->numIndexes );
  1666. totalVerts = 0;
  1667. totalIndexes = 0;
  1668. for ( i = 0 ; i < numSurfaces ; i++ ) {
  1669. tri = surfaces[i];
  1670. memcpy( newTri->verts + totalVerts, tri->verts, tri->numVerts * sizeof( *tri->verts ) );
  1671. for ( j = 0 ; j < tri->numIndexes ; j++ ) {
  1672. newTri->indexes[ totalIndexes + j ] = totalVerts + tri->indexes[j];
  1673. }
  1674. totalVerts += tri->numVerts;
  1675. totalIndexes += tri->numIndexes;
  1676. }
  1677. return newTri;
  1678. }
  1679. /*
  1680. =================
  1681. R_MergeTriangles
  1682. Only deals with vertexes and indexes, not silhouettes, planes, etc.
  1683. Does NOT perform a cleanup triangles, so there may be duplicated verts in the result.
  1684. =================
  1685. */
  1686. srfTriangles_t *R_MergeTriangles( const srfTriangles_t *tri1, const srfTriangles_t *tri2 ) {
  1687. const srfTriangles_t *tris[2];
  1688. tris[0] = tri1;
  1689. tris[1] = tri2;
  1690. return R_MergeSurfaceList( tris, 2 );
  1691. }
  1692. /*
  1693. =================
  1694. R_ReverseTriangles
  1695. Lit two sided surfaces need to have the triangles actually duplicated,
  1696. they can't just turn on two sided lighting, because the normal and tangents
  1697. are wrong on the other sides.
  1698. This should be called before R_CleanupTriangles
  1699. =================
  1700. */
  1701. void R_ReverseTriangles( srfTriangles_t *tri ) {
  1702. int i;
  1703. // flip the normal on each vertex
  1704. // If the surface is going to have generated normals, this won't matter,
  1705. // but if it has explicit normals, this will keep it on the correct side
  1706. for ( i = 0 ; i < tri->numVerts ; i++ ) {
  1707. tri->verts[i].normal = vec3_origin - tri->verts[i].normal;
  1708. }
  1709. // flip the index order to make them back sided
  1710. for ( i = 0 ; i < tri->numIndexes ; i+= 3 ) {
  1711. glIndex_t temp;
  1712. temp = tri->indexes[ i + 0 ];
  1713. tri->indexes[ i + 0 ] = tri->indexes[ i + 1 ];
  1714. tri->indexes[ i + 1 ] = temp;
  1715. }
  1716. }
  1717. /*
  1718. =================
  1719. R_CleanupTriangles
  1720. FIXME: allow createFlat and createSmooth normals, as well as explicit
  1721. =================
  1722. */
  1723. void R_CleanupTriangles( srfTriangles_t *tri, bool createNormals, bool identifySilEdges, bool useUnsmoothedTangents ) {
  1724. R_RangeCheckIndexes( tri );
  1725. R_CreateSilIndexes( tri );
  1726. // R_RemoveDuplicatedTriangles( tri ); // this may remove valid overlapped transparent triangles
  1727. R_RemoveDegenerateTriangles( tri );
  1728. R_TestDegenerateTextureSpace( tri );
  1729. // R_RemoveUnusedVerts( tri );
  1730. if ( identifySilEdges ) {
  1731. R_IdentifySilEdges( tri, true ); // assume it is non-deformable, and omit coplanar edges
  1732. }
  1733. // bust vertexes that share a mirrored edge into separate vertexes
  1734. R_DuplicateMirroredVertexes( tri );
  1735. // optimize the index order (not working?)
  1736. // R_OrderIndexes( tri->numIndexes, tri->indexes );
  1737. R_CreateDupVerts( tri );
  1738. R_BoundTriSurf( tri );
  1739. if ( useUnsmoothedTangents ) {
  1740. R_BuildDominantTris( tri );
  1741. R_DeriveUnsmoothedTangents( tri );
  1742. } else if ( !createNormals ) {
  1743. R_DeriveFacePlanes( tri );
  1744. R_DeriveTangentsWithoutNormals( tri );
  1745. } else {
  1746. R_DeriveTangents( tri );
  1747. }
  1748. }
  1749. /*
  1750. ===================================================================================
  1751. DEFORMED SURFACES
  1752. ===================================================================================
  1753. */
  1754. /*
  1755. ===================
  1756. R_BuildDeformInfo
  1757. ===================
  1758. */
  1759. deformInfo_t *R_BuildDeformInfo( int numVerts, const idDrawVert *verts, int numIndexes, const int *indexes, bool useUnsmoothedTangents ) {
  1760. deformInfo_t *deform;
  1761. srfTriangles_t tri;
  1762. int i;
  1763. memset( &tri, 0, sizeof( tri ) );
  1764. tri.numVerts = numVerts;
  1765. R_AllocStaticTriSurfVerts( &tri, tri.numVerts );
  1766. SIMDProcessor->Memcpy( tri.verts, verts, tri.numVerts * sizeof( tri.verts[0] ) );
  1767. tri.numIndexes = numIndexes;
  1768. R_AllocStaticTriSurfIndexes( &tri, tri.numIndexes );
  1769. // don't memcpy, so we can change the index type from int to short without changing the interface
  1770. for ( i = 0 ; i < tri.numIndexes ; i++ ) {
  1771. tri.indexes[i] = indexes[i];
  1772. }
  1773. R_RangeCheckIndexes( &tri );
  1774. R_CreateSilIndexes( &tri );
  1775. // should we order the indexes here?
  1776. // R_RemoveDuplicatedTriangles( &tri );
  1777. // R_RemoveDegenerateTriangles( &tri );
  1778. // R_RemoveUnusedVerts( &tri );
  1779. R_IdentifySilEdges( &tri, false ); // we cannot remove coplanar edges, because
  1780. // they can deform to silhouettes
  1781. R_DuplicateMirroredVertexes( &tri ); // split mirror points into multiple points
  1782. R_CreateDupVerts( &tri );
  1783. if ( useUnsmoothedTangents ) {
  1784. R_BuildDominantTris( &tri );
  1785. }
  1786. deform = (deformInfo_t *)R_ClearedStaticAlloc( sizeof( *deform ) );
  1787. deform->numSourceVerts = numVerts;
  1788. deform->numOutputVerts = tri.numVerts;
  1789. deform->numIndexes = numIndexes;
  1790. deform->indexes = tri.indexes;
  1791. deform->silIndexes = tri.silIndexes;
  1792. deform->numSilEdges = tri.numSilEdges;
  1793. deform->silEdges = tri.silEdges;
  1794. deform->dominantTris = tri.dominantTris;
  1795. deform->numMirroredVerts = tri.numMirroredVerts;
  1796. deform->mirroredVerts = tri.mirroredVerts;
  1797. deform->numDupVerts = tri.numDupVerts;
  1798. deform->dupVerts = tri.dupVerts;
  1799. if ( tri.verts ) {
  1800. triVertexAllocator.Free( tri.verts );
  1801. }
  1802. if ( tri.facePlanes ) {
  1803. triPlaneAllocator.Free( tri.facePlanes );
  1804. }
  1805. return deform;
  1806. }
  1807. /*
  1808. ===================
  1809. R_FreeDeformInfo
  1810. ===================
  1811. */
  1812. void R_FreeDeformInfo( deformInfo_t *deformInfo ) {
  1813. if ( deformInfo->indexes != NULL ) {
  1814. triIndexAllocator.Free( deformInfo->indexes );
  1815. }
  1816. if ( deformInfo->silIndexes != NULL ) {
  1817. triSilIndexAllocator.Free( deformInfo->silIndexes );
  1818. }
  1819. if ( deformInfo->silEdges != NULL ) {
  1820. triSilEdgeAllocator.Free( deformInfo->silEdges );
  1821. }
  1822. if ( deformInfo->dominantTris != NULL ) {
  1823. triDominantTrisAllocator.Free( deformInfo->dominantTris );
  1824. }
  1825. if ( deformInfo->mirroredVerts != NULL ) {
  1826. triMirroredVertAllocator.Free( deformInfo->mirroredVerts );
  1827. }
  1828. if ( deformInfo->dupVerts != NULL ) {
  1829. triDupVertAllocator.Free( deformInfo->dupVerts );
  1830. }
  1831. R_StaticFree( deformInfo );
  1832. }
  1833. /*
  1834. ===================
  1835. R_DeformInfoMemoryUsed
  1836. ===================
  1837. */
  1838. int R_DeformInfoMemoryUsed( deformInfo_t *deformInfo ) {
  1839. int total = 0;
  1840. if ( deformInfo->indexes != NULL ) {
  1841. total += deformInfo->numIndexes * sizeof( deformInfo->indexes[0] );
  1842. }
  1843. if ( deformInfo->silIndexes != NULL ) {
  1844. total += deformInfo->numIndexes * sizeof( deformInfo->silIndexes[0] );
  1845. }
  1846. if ( deformInfo->silEdges != NULL ) {
  1847. total += deformInfo->numSilEdges * sizeof( deformInfo->silEdges[0] );
  1848. }
  1849. if ( deformInfo->dominantTris != NULL ) {
  1850. total += deformInfo->numSourceVerts * sizeof( deformInfo->dominantTris[0] );
  1851. }
  1852. if ( deformInfo->mirroredVerts != NULL ) {
  1853. total += deformInfo->numMirroredVerts * sizeof( deformInfo->mirroredVerts[0] );
  1854. }
  1855. if ( deformInfo->dupVerts != NULL ) {
  1856. total += deformInfo->numDupVerts * sizeof( deformInfo->dupVerts[0] );
  1857. }
  1858. total += sizeof( *deformInfo );
  1859. return total;
  1860. }