tr_trisurf.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  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. /*
  79. =================
  80. R_TriSurfMemory
  81. For memory profiling
  82. =================
  83. */
  84. int R_TriSurfMemory( const srfTriangles_t *tri ) {
  85. int total = 0;
  86. if ( tri == NULL ) {
  87. return total;
  88. }
  89. if ( tri->preLightShadowVertexes != NULL ) {
  90. total += tri->numVerts * 2 * sizeof( tri->preLightShadowVertexes[0] );
  91. }
  92. if ( tri->staticShadowVertexes != NULL ) {
  93. total += tri->numVerts * 2 * sizeof( tri->staticShadowVertexes[0] );
  94. }
  95. if ( tri->verts != NULL ) {
  96. if ( tri->ambientSurface == NULL || tri->verts != tri->ambientSurface->verts ) {
  97. total += tri->numVerts * sizeof( tri->verts[0] );
  98. }
  99. }
  100. if ( tri->indexes != NULL ) {
  101. if ( tri->ambientSurface == NULL || tri->indexes != tri->ambientSurface->indexes ) {
  102. total += tri->numIndexes * sizeof( tri->indexes[0] );
  103. }
  104. }
  105. if ( tri->silIndexes != NULL ) {
  106. total += tri->numIndexes * sizeof( tri->silIndexes[0] );
  107. }
  108. if ( tri->silEdges != NULL ) {
  109. total += tri->numSilEdges * sizeof( tri->silEdges[0] );
  110. }
  111. if ( tri->dominantTris != NULL ) {
  112. total += tri->numVerts * sizeof( tri->dominantTris[0] );
  113. }
  114. if ( tri->mirroredVerts != NULL ) {
  115. total += tri->numMirroredVerts * sizeof( tri->mirroredVerts[0] );
  116. }
  117. if ( tri->dupVerts != NULL ) {
  118. total += tri->numDupVerts * sizeof( tri->dupVerts[0] );
  119. }
  120. total += sizeof( *tri );
  121. return total;
  122. }
  123. /*
  124. ==============
  125. R_FreeStaticTriSurfVertexCaches
  126. ==============
  127. */
  128. void R_FreeStaticTriSurfVertexCaches( srfTriangles_t * tri ) {
  129. // we don't support reclaiming static geometry memory
  130. // without a level change
  131. tri->ambientCache = 0;
  132. tri->indexCache = 0;
  133. tri->shadowCache = 0;
  134. }
  135. /*
  136. ==============
  137. R_FreeStaticTriSurf
  138. ==============
  139. */
  140. void R_FreeStaticTriSurf( srfTriangles_t *tri ) {
  141. if ( !tri ) {
  142. return;
  143. }
  144. R_FreeStaticTriSurfVertexCaches( tri );
  145. if ( !tri->referencedVerts ) {
  146. if ( tri->verts != NULL ) {
  147. // R_CreateLightTris points tri->verts at the verts of the ambient surface
  148. if ( tri->ambientSurface == NULL || tri->verts != tri->ambientSurface->verts ) {
  149. Mem_Free( tri->verts );
  150. }
  151. }
  152. }
  153. if ( !tri->referencedIndexes ) {
  154. if ( tri->indexes != NULL ) {
  155. // if a surface is completely inside a light volume R_CreateLightTris points tri->indexes at the indexes of the ambient surface
  156. if ( tri->ambientSurface == NULL || tri->indexes != tri->ambientSurface->indexes ) {
  157. Mem_Free( tri->indexes );
  158. }
  159. }
  160. if ( tri->silIndexes != NULL ) {
  161. Mem_Free( tri->silIndexes );
  162. }
  163. if ( tri->silEdges != NULL ) {
  164. Mem_Free( tri->silEdges );
  165. }
  166. if ( tri->dominantTris != NULL ) {
  167. Mem_Free( tri->dominantTris );
  168. }
  169. if ( tri->mirroredVerts != NULL ) {
  170. Mem_Free( tri->mirroredVerts );
  171. }
  172. if ( tri->dupVerts != NULL ) {
  173. Mem_Free( tri->dupVerts );
  174. }
  175. }
  176. if ( tri->preLightShadowVertexes != NULL ) {
  177. Mem_Free( tri->preLightShadowVertexes );
  178. }
  179. if ( tri->staticShadowVertexes != NULL ) {
  180. Mem_Free( tri->staticShadowVertexes );
  181. }
  182. // clear the tri out so we don't retain stale data
  183. memset( tri, 0, sizeof( srfTriangles_t ) );
  184. Mem_Free( tri );
  185. }
  186. /*
  187. ==============
  188. R_FreeStaticTriSurfVerts
  189. ==============
  190. */
  191. void R_FreeStaticTriSurfVerts( srfTriangles_t *tri ) {
  192. // we don't support reclaiming static geometry memory
  193. // without a level change
  194. tri->ambientCache = 0;
  195. if ( tri->verts != NULL ) {
  196. // R_CreateLightTris points tri->verts at the verts of the ambient surface
  197. if ( tri->ambientSurface == NULL || tri->verts != tri->ambientSurface->verts ) {
  198. Mem_Free( tri->verts );
  199. }
  200. }
  201. }
  202. /*
  203. ==============
  204. R_AllocStaticTriSurf
  205. ==============
  206. */
  207. srfTriangles_t *R_AllocStaticTriSurf() {
  208. srfTriangles_t *tris = (srfTriangles_t *)Mem_ClearedAlloc( sizeof( srfTriangles_t ), TAG_SRFTRIS );
  209. return tris;
  210. }
  211. /*
  212. =================
  213. R_CopyStaticTriSurf
  214. This only duplicates the indexes and verts, not any of the derived data.
  215. =================
  216. */
  217. srfTriangles_t *R_CopyStaticTriSurf( const srfTriangles_t *tri ) {
  218. srfTriangles_t *newTri;
  219. newTri = R_AllocStaticTriSurf();
  220. R_AllocStaticTriSurfVerts( newTri, tri->numVerts );
  221. R_AllocStaticTriSurfIndexes( newTri, tri->numIndexes );
  222. newTri->numVerts = tri->numVerts;
  223. newTri->numIndexes = tri->numIndexes;
  224. memcpy( newTri->verts, tri->verts, tri->numVerts * sizeof( newTri->verts[0] ) );
  225. memcpy( newTri->indexes, tri->indexes, tri->numIndexes * sizeof( newTri->indexes[0] ) );
  226. return newTri;
  227. }
  228. /*
  229. =================
  230. R_AllocStaticTriSurfVerts
  231. =================
  232. */
  233. void R_AllocStaticTriSurfVerts( srfTriangles_t *tri, int numVerts ) {
  234. assert( tri->verts == NULL );
  235. tri->verts = (idDrawVert *)Mem_Alloc16( numVerts * sizeof( idDrawVert ), TAG_TRI_VERTS );
  236. }
  237. /*
  238. =================
  239. R_AllocStaticTriSurfIndexes
  240. =================
  241. */
  242. void R_AllocStaticTriSurfIndexes( srfTriangles_t *tri, int numIndexes ) {
  243. assert( tri->indexes == NULL );
  244. tri->indexes = (triIndex_t *)Mem_Alloc16( numIndexes * sizeof( triIndex_t ), TAG_TRI_INDEXES );
  245. }
  246. /*
  247. =================
  248. R_AllocStaticTriSurfSilIndexes
  249. =================
  250. */
  251. void R_AllocStaticTriSurfSilIndexes( srfTriangles_t *tri, int numIndexes ) {
  252. assert( tri->silIndexes == NULL );
  253. tri->silIndexes = (triIndex_t *)Mem_Alloc16( numIndexes * sizeof( triIndex_t ), TAG_TRI_SIL_INDEXES );
  254. }
  255. /*
  256. =================
  257. R_AllocStaticTriSurfDominantTris
  258. =================
  259. */
  260. void R_AllocStaticTriSurfDominantTris( srfTriangles_t *tri, int numVerts ) {
  261. assert( tri->dominantTris == NULL );
  262. tri->dominantTris = (dominantTri_t *)Mem_Alloc16( numVerts * sizeof( dominantTri_t ), TAG_TRI_DOMINANT_TRIS );
  263. }
  264. /*
  265. =================
  266. R_AllocStaticTriSurfMirroredVerts
  267. =================
  268. */
  269. void R_AllocStaticTriSurfMirroredVerts( srfTriangles_t *tri, int numMirroredVerts ) {
  270. assert( tri->mirroredVerts == NULL );
  271. tri->mirroredVerts = (int *)Mem_Alloc16( numMirroredVerts * sizeof( *tri->mirroredVerts ), TAG_TRI_MIR_VERT );
  272. }
  273. /*
  274. =================
  275. R_AllocStaticTriSurfDupVerts
  276. =================
  277. */
  278. void R_AllocStaticTriSurfDupVerts( srfTriangles_t *tri, int numDupVerts ) {
  279. assert( tri->dupVerts == NULL );
  280. tri->dupVerts = (int *)Mem_Alloc16( numDupVerts * 2 * sizeof( *tri->dupVerts ), TAG_TRI_DUP_VERT );
  281. }
  282. /*
  283. =================
  284. R_AllocStaticTriSurfSilEdges
  285. =================
  286. */
  287. void R_AllocStaticTriSurfSilEdges( srfTriangles_t *tri, int numSilEdges ) {
  288. assert( tri->silEdges == NULL );
  289. tri->silEdges = (silEdge_t *)Mem_Alloc16( numSilEdges * sizeof( silEdge_t ), TAG_TRI_SIL_EDGE );
  290. }
  291. /*
  292. =================
  293. R_AllocStaticTriSurfPreLightShadowVerts
  294. =================
  295. */
  296. void R_AllocStaticTriSurfPreLightShadowVerts( srfTriangles_t *tri, int numVerts ) {
  297. assert( tri->preLightShadowVertexes == NULL );
  298. tri->preLightShadowVertexes = (idShadowVert *)Mem_Alloc16( numVerts * sizeof( idShadowVert ), TAG_TRI_SHADOW );
  299. }
  300. /*
  301. =================
  302. R_ResizeStaticTriSurfVerts
  303. =================
  304. */
  305. void R_ResizeStaticTriSurfVerts( srfTriangles_t *tri, int numVerts ) {
  306. idDrawVert * newVerts = (idDrawVert *)Mem_Alloc16( numVerts * sizeof( idDrawVert ), TAG_TRI_VERTS );
  307. const int copy = std::min( numVerts, tri->numVerts );
  308. memcpy( newVerts, tri->verts, copy * sizeof( idDrawVert ) );
  309. Mem_Free( tri->verts );
  310. tri->verts = newVerts;
  311. }
  312. /*
  313. =================
  314. R_ResizeStaticTriSurfIndexes
  315. =================
  316. */
  317. void R_ResizeStaticTriSurfIndexes( srfTriangles_t *tri, int numIndexes ) {
  318. triIndex_t * newIndexes = (triIndex_t *)Mem_Alloc16( numIndexes * sizeof( triIndex_t ), TAG_TRI_INDEXES );
  319. const int copy = std::min( numIndexes, tri->numIndexes );
  320. memcpy( newIndexes, tri->indexes, copy * sizeof( triIndex_t ) );
  321. Mem_Free( tri->indexes );
  322. tri->indexes = newIndexes;
  323. }
  324. /*
  325. =================
  326. R_ReferenceStaticTriSurfVerts
  327. =================
  328. */
  329. void R_ReferenceStaticTriSurfVerts( srfTriangles_t *tri, const srfTriangles_t *reference ) {
  330. tri->verts = reference->verts;
  331. }
  332. /*
  333. =================
  334. R_ReferenceStaticTriSurfIndexes
  335. =================
  336. */
  337. void R_ReferenceStaticTriSurfIndexes( srfTriangles_t *tri, const srfTriangles_t *reference ) {
  338. tri->indexes = reference->indexes;
  339. }
  340. /*
  341. =================
  342. R_FreeStaticTriSurfSilIndexes
  343. =================
  344. */
  345. void R_FreeStaticTriSurfSilIndexes( srfTriangles_t *tri ) {
  346. Mem_Free( tri->silIndexes );
  347. tri->silIndexes = NULL;
  348. }
  349. /*
  350. ===============
  351. R_RangeCheckIndexes
  352. Check for syntactically incorrect indexes, like out of range values.
  353. Does not check for semantics, like degenerate triangles.
  354. No vertexes is acceptable if no indexes.
  355. No indexes is acceptable.
  356. More vertexes than are referenced by indexes are acceptable.
  357. ===============
  358. */
  359. void R_RangeCheckIndexes( const srfTriangles_t *tri ) {
  360. int i;
  361. if ( tri->numIndexes < 0 ) {
  362. common->Error( "R_RangeCheckIndexes: numIndexes < 0" );
  363. }
  364. if ( tri->numVerts < 0 ) {
  365. common->Error( "R_RangeCheckIndexes: numVerts < 0" );
  366. }
  367. // must specify an integral number of triangles
  368. if ( tri->numIndexes % 3 != 0 ) {
  369. common->Error( "R_RangeCheckIndexes: numIndexes %% 3" );
  370. }
  371. for ( i = 0; i < tri->numIndexes; i++ ) {
  372. if ( tri->indexes[i] >= tri->numVerts ) {
  373. common->Error( "R_RangeCheckIndexes: index out of range" );
  374. }
  375. }
  376. // this should not be possible unless there are unused verts
  377. if ( tri->numVerts > tri->numIndexes ) {
  378. // FIXME: find the causes of these
  379. // common->Printf( "R_RangeCheckIndexes: tri->numVerts > tri->numIndexes\n" );
  380. }
  381. }
  382. /*
  383. =================
  384. R_BoundTriSurf
  385. =================
  386. */
  387. void R_BoundTriSurf( srfTriangles_t *tri ) {
  388. SIMDProcessor->MinMax( tri->bounds[0], tri->bounds[1], tri->verts, tri->numVerts );
  389. }
  390. /*
  391. =================
  392. R_CreateSilRemap
  393. =================
  394. */
  395. static int *R_CreateSilRemap( const srfTriangles_t *tri ) {
  396. int c_removed, c_unique;
  397. int *remap;
  398. int i, j, hashKey;
  399. const idDrawVert *v1, *v2;
  400. remap = (int *)R_ClearedStaticAlloc( tri->numVerts * sizeof( remap[0] ) );
  401. if ( !r_useSilRemap.GetBool() ) {
  402. for ( i = 0; i < tri->numVerts; i++ ) {
  403. remap[i] = i;
  404. }
  405. return remap;
  406. }
  407. idHashIndex hash( 1024, tri->numVerts );
  408. c_removed = 0;
  409. c_unique = 0;
  410. for ( i = 0; i < tri->numVerts; i++ ) {
  411. v1 = &tri->verts[i];
  412. // see if there is an earlier vert that it can map to
  413. hashKey = hash.GenerateKey( v1->xyz );
  414. for ( j = hash.First( hashKey ); j >= 0; j = hash.Next( j ) ) {
  415. v2 = &tri->verts[j];
  416. if ( v2->xyz[0] == v1->xyz[0]
  417. && v2->xyz[1] == v1->xyz[1]
  418. && v2->xyz[2] == v1->xyz[2] ) {
  419. c_removed++;
  420. remap[i] = j;
  421. break;
  422. }
  423. }
  424. if ( j < 0 ) {
  425. c_unique++;
  426. remap[i] = i;
  427. hash.Add( hashKey, i );
  428. }
  429. }
  430. return remap;
  431. }
  432. /*
  433. =================
  434. R_CreateSilIndexes
  435. Uniquing vertexes only on xyz before creating sil edges reduces
  436. the edge count by about 20% on Q3 models
  437. =================
  438. */
  439. void R_CreateSilIndexes( srfTriangles_t *tri ) {
  440. int i;
  441. int *remap;
  442. if ( tri->silIndexes ) {
  443. Mem_Free( tri->silIndexes );
  444. tri->silIndexes = NULL;
  445. }
  446. remap = R_CreateSilRemap( tri );
  447. // remap indexes to the first one
  448. R_AllocStaticTriSurfSilIndexes( tri, tri->numIndexes );
  449. assert( tri->silIndexes != NULL );
  450. for ( i = 0; i < tri->numIndexes; i++ ) {
  451. tri->silIndexes[i] = remap[tri->indexes[i]];
  452. }
  453. R_StaticFree( remap );
  454. }
  455. /*
  456. =====================
  457. R_CreateDupVerts
  458. =====================
  459. */
  460. void R_CreateDupVerts( srfTriangles_t *tri ) {
  461. int i;
  462. idTempArray<int> remap( tri->numVerts );
  463. // initialize vertex remap in case there are unused verts
  464. for ( i = 0; i < tri->numVerts; i++ ) {
  465. remap[i] = i;
  466. }
  467. // set the remap based on how the silhouette indexes are remapped
  468. for ( i = 0; i < tri->numIndexes; i++ ) {
  469. remap[tri->indexes[i]] = tri->silIndexes[i];
  470. }
  471. // create duplicate vertex index based on the vertex remap
  472. idTempArray<int> tempDupVerts( tri->numVerts * 2 );
  473. tri->numDupVerts = 0;
  474. for ( i = 0; i < tri->numVerts; i++ ) {
  475. if ( remap[i] != i ) {
  476. tempDupVerts[tri->numDupVerts*2+0] = i;
  477. tempDupVerts[tri->numDupVerts*2+1] = remap[i];
  478. tri->numDupVerts++;
  479. }
  480. }
  481. R_AllocStaticTriSurfDupVerts( tri, tri->numDupVerts );
  482. memcpy( tri->dupVerts, tempDupVerts.Ptr(), tri->numDupVerts * 2 * sizeof( tri->dupVerts[0] ) );
  483. }
  484. /*
  485. ===============
  486. R_DefineEdge
  487. ===============
  488. */
  489. static int c_duplicatedEdges, c_tripledEdges;
  490. static const int MAX_SIL_EDGES = 0x7ffff;
  491. static void R_DefineEdge( const int v1, const int v2, const int planeNum, const int numPlanes,
  492. idList<silEdge_t> & silEdges, idHashIndex & silEdgeHash ) {
  493. int i, hashKey;
  494. // check for degenerate edge
  495. if ( v1 == v2 ) {
  496. return;
  497. }
  498. hashKey = silEdgeHash.GenerateKey( v1, v2 );
  499. // search for a matching other side
  500. for ( i = silEdgeHash.First( hashKey ); i >= 0 && i < MAX_SIL_EDGES; i = silEdgeHash.Next( i ) ) {
  501. if ( silEdges[i].v1 == v1 && silEdges[i].v2 == v2 ) {
  502. c_duplicatedEdges++;
  503. // allow it to still create a new edge
  504. continue;
  505. }
  506. if ( silEdges[i].v2 == v1 && silEdges[i].v1 == v2 ) {
  507. if ( silEdges[i].p2 != numPlanes ) {
  508. c_tripledEdges++;
  509. // allow it to still create a new edge
  510. continue;
  511. }
  512. // this is a matching back side
  513. silEdges[i].p2 = planeNum;
  514. return;
  515. }
  516. }
  517. // define the new edge
  518. silEdgeHash.Add( hashKey, silEdges.Num() );
  519. silEdge_t silEdge;
  520. silEdge.p1 = planeNum;
  521. silEdge.p2 = numPlanes;
  522. silEdge.v1 = v1;
  523. silEdge.v2 = v2;
  524. silEdges.Append( silEdge );
  525. }
  526. /*
  527. =================
  528. SilEdgeSort
  529. =================
  530. */
  531. static int SilEdgeSort( const void *a, const void *b ) {
  532. if ( ((silEdge_t *)a)->p1 < ((silEdge_t *)b)->p1 ) {
  533. return -1;
  534. }
  535. if ( ((silEdge_t *)a)->p1 > ((silEdge_t *)b)->p1 ) {
  536. return 1;
  537. }
  538. if ( ((silEdge_t *)a)->p2 < ((silEdge_t *)b)->p2 ) {
  539. return -1;
  540. }
  541. if ( ((silEdge_t *)a)->p2 > ((silEdge_t *)b)->p2 ) {
  542. return 1;
  543. }
  544. return 0;
  545. }
  546. /*
  547. =================
  548. R_IdentifySilEdges
  549. If the surface will not deform, coplanar edges (polygon interiors)
  550. can never create silhouette plains, and can be omited
  551. =================
  552. */
  553. int c_coplanarSilEdges;
  554. int c_totalSilEdges;
  555. void R_IdentifySilEdges( srfTriangles_t *tri, bool omitCoplanarEdges ) {
  556. int i;
  557. int shared, single;
  558. omitCoplanarEdges = false; // optimization doesn't work for some reason
  559. static const int SILEDGE_HASH_SIZE = 1024;
  560. const int numTris = tri->numIndexes / 3;
  561. idList<silEdge_t> silEdges( MAX_SIL_EDGES );
  562. idHashIndex silEdgeHash( SILEDGE_HASH_SIZE, MAX_SIL_EDGES );
  563. int numPlanes = numTris;
  564. silEdgeHash.Clear();
  565. c_duplicatedEdges = 0;
  566. c_tripledEdges = 0;
  567. for ( i = 0; i < numTris; i++ ) {
  568. int i1, i2, i3;
  569. i1 = tri->silIndexes[ i*3 + 0 ];
  570. i2 = tri->silIndexes[ i*3 + 1 ];
  571. i3 = tri->silIndexes[ i*3 + 2 ];
  572. // create the edges
  573. R_DefineEdge( i1, i2, i, numPlanes, silEdges, silEdgeHash );
  574. R_DefineEdge( i2, i3, i, numPlanes, silEdges, silEdgeHash );
  575. R_DefineEdge( i3, i1, i, numPlanes, silEdges, silEdgeHash );
  576. }
  577. if ( c_duplicatedEdges || c_tripledEdges ) {
  578. common->DWarning( "%i duplicated edge directions, %i tripled edges", c_duplicatedEdges, c_tripledEdges );
  579. }
  580. // if we know that the vertexes aren't going
  581. // to deform, we can remove interior triangulation edges
  582. // on otherwise planar polygons.
  583. // I earlier believed that I could also remove concave
  584. // edges, because they are never silhouettes in the conventional sense,
  585. // but they are still needed to balance out all the true sil edges
  586. // for the shadow algorithm to function
  587. int c_coplanarCulled;
  588. c_coplanarCulled = 0;
  589. if ( omitCoplanarEdges ) {
  590. for ( i = 0; i < silEdges.Num(); i++ ) {
  591. int i1, i2, i3;
  592. idPlane plane;
  593. int base;
  594. int j;
  595. float d;
  596. if ( silEdges[i].p2 == numPlanes ) { // the fake dangling edge
  597. continue;
  598. }
  599. base = silEdges[i].p1 * 3;
  600. i1 = tri->silIndexes[ base + 0 ];
  601. i2 = tri->silIndexes[ base + 1 ];
  602. i3 = tri->silIndexes[ base + 2 ];
  603. plane.FromPoints( tri->verts[i1].xyz, tri->verts[i2].xyz, tri->verts[i3].xyz );
  604. // check to see if points of second triangle are not coplanar
  605. base = silEdges[i].p2 * 3;
  606. for ( j = 0; j < 3; j++ ) {
  607. i1 = tri->silIndexes[ base + j ];
  608. d = plane.Distance( tri->verts[i1].xyz );
  609. if ( d != 0 ) { // even a small epsilon causes problems
  610. break;
  611. }
  612. }
  613. if ( j == 3 ) {
  614. // we can cull this sil edge
  615. memmove( &silEdges[i], &silEdges[i+1], (silEdges.Num()-i-1) * sizeof( silEdges[i] ) );
  616. c_coplanarCulled++;
  617. silEdges.SetNum( silEdges.Num() - 1 );
  618. i--;
  619. }
  620. }
  621. if ( c_coplanarCulled ) {
  622. c_coplanarSilEdges += c_coplanarCulled;
  623. // common->Printf( "%i of %i sil edges coplanar culled\n", c_coplanarCulled,
  624. // c_coplanarCulled + numSilEdges );
  625. }
  626. }
  627. c_totalSilEdges += silEdges.Num();
  628. // sort the sil edges based on plane number
  629. qsort( silEdges.Ptr(), silEdges.Num(), sizeof( silEdges[0] ), SilEdgeSort );
  630. // count up the distribution.
  631. // a perfectly built model should only have shared
  632. // edges, but most models will have some interpenetration
  633. // and dangling edges
  634. shared = 0;
  635. single = 0;
  636. for ( i = 0; i < silEdges.Num(); i++ ) {
  637. if ( silEdges[i].p2 == numPlanes ) {
  638. single++;
  639. } else {
  640. shared++;
  641. }
  642. }
  643. if ( !single ) {
  644. tri->perfectHull = true;
  645. } else {
  646. tri->perfectHull = false;
  647. }
  648. tri->numSilEdges = silEdges.Num();
  649. R_AllocStaticTriSurfSilEdges( tri, silEdges.Num() );
  650. memcpy( tri->silEdges, silEdges.Ptr(), silEdges.Num() * sizeof( tri->silEdges[0] ) );
  651. }
  652. /*
  653. ===============
  654. R_FaceNegativePolarity
  655. Returns true if the texture polarity of the face is negative, false if it is positive or zero
  656. ===============
  657. */
  658. static bool R_FaceNegativePolarity( const srfTriangles_t *tri, int firstIndex ) {
  659. const idDrawVert * a = tri->verts + tri->indexes[firstIndex + 0];
  660. const idDrawVert * b = tri->verts + tri->indexes[firstIndex + 1];
  661. const idDrawVert * c = tri->verts + tri->indexes[firstIndex + 2];
  662. const idVec2 aST = a->GetTexCoord();
  663. const idVec2 bST = b->GetTexCoord();
  664. const idVec2 cST = c->GetTexCoord();
  665. float d0[5];
  666. d0[3] = bST[0] - aST[0];
  667. d0[4] = bST[1] - aST[1];
  668. float d1[5];
  669. d1[3] = cST[0] - aST[0];
  670. d1[4] = cST[1] - aST[1];
  671. const float area = d0[3] * d1[4] - d0[4] * d1[3];
  672. if ( area >= 0 ) {
  673. return false;
  674. }
  675. return true;
  676. }
  677. /*
  678. ===================
  679. R_DuplicateMirroredVertexes
  680. Modifies the surface to bust apart any verts that are shared by both positive and
  681. negative texture polarities, so tangent space smoothing at the vertex doesn't
  682. degenerate.
  683. This will create some identical vertexes (which will eventually get different tangent
  684. vectors), so never optimize the resulting mesh, or it will get the mirrored edges back.
  685. Reallocates tri->verts and changes tri->indexes in place
  686. Silindexes are unchanged by this.
  687. sets mirroredVerts and mirroredVerts[]
  688. ===================
  689. */
  690. struct tangentVert_t {
  691. bool polarityUsed[2];
  692. int negativeRemap;
  693. };
  694. static void R_DuplicateMirroredVertexes( srfTriangles_t *tri ) {
  695. tangentVert_t *vert;
  696. int i, j;
  697. int totalVerts;
  698. int numMirror;
  699. idTempArray<tangentVert_t> tverts( tri->numVerts );
  700. tverts.Zero();
  701. // determine texture polarity of each surface
  702. // mark each vert with the polarities it uses
  703. for ( i = 0; i < tri->numIndexes; i+=3 ) {
  704. int polarity = R_FaceNegativePolarity( tri, i );
  705. for ( j = 0; j < 3; j++ ) {
  706. tverts[tri->indexes[i+j]].polarityUsed[ polarity ] = true;
  707. }
  708. }
  709. // now create new vertex indices as needed
  710. totalVerts = tri->numVerts;
  711. for ( i = 0; i < tri->numVerts; i++ ) {
  712. vert = &tverts[i];
  713. if ( vert->polarityUsed[0] && vert->polarityUsed[1] ) {
  714. vert->negativeRemap = totalVerts;
  715. totalVerts++;
  716. }
  717. }
  718. tri->numMirroredVerts = totalVerts - tri->numVerts;
  719. if ( tri->numMirroredVerts == 0 ) {
  720. tri->mirroredVerts = NULL;
  721. return;
  722. }
  723. // now create the new list
  724. R_AllocStaticTriSurfMirroredVerts( tri, tri->numMirroredVerts );
  725. R_ResizeStaticTriSurfVerts( tri, totalVerts );
  726. // create the duplicates
  727. numMirror = 0;
  728. for ( i = 0; i < tri->numVerts; i++ ) {
  729. j = tverts[i].negativeRemap;
  730. if ( j ) {
  731. tri->verts[j] = tri->verts[i];
  732. tri->mirroredVerts[numMirror] = i;
  733. numMirror++;
  734. }
  735. }
  736. tri->numVerts = totalVerts;
  737. // change the indexes
  738. for ( i = 0; i < tri->numIndexes; i++ ) {
  739. if ( tverts[tri->indexes[i]].negativeRemap && R_FaceNegativePolarity( tri, 3 * ( i / 3 ) ) ) {
  740. tri->indexes[i] = tverts[tri->indexes[i]].negativeRemap;
  741. }
  742. }
  743. }
  744. /*
  745. ============
  746. R_DeriveNormalsAndTangents
  747. Derives the normal and orthogonal tangent vectors for the triangle vertices.
  748. For each vertex the normal and tangent vectors are derived from all triangles
  749. using the vertex which results in smooth tangents across the mesh.
  750. ============
  751. */
  752. void R_DeriveNormalsAndTangents( srfTriangles_t *tri ) {
  753. idTempArray< idVec3 > vertexNormals( tri->numVerts );
  754. idTempArray< idVec3 > vertexTangents( tri->numVerts );
  755. idTempArray< idVec3 > vertexBitangents( tri->numVerts );
  756. vertexNormals.Zero();
  757. vertexTangents.Zero();
  758. vertexBitangents.Zero();
  759. for ( int i = 0; i < tri->numIndexes; i += 3 ) {
  760. const int v0 = tri->indexes[i + 0];
  761. const int v1 = tri->indexes[i + 1];
  762. const int v2 = tri->indexes[i + 2];
  763. const idDrawVert * a = tri->verts + v0;
  764. const idDrawVert * b = tri->verts + v1;
  765. const idDrawVert * c = tri->verts + v2;
  766. const idVec2 aST = a->GetTexCoord();
  767. const idVec2 bST = b->GetTexCoord();
  768. const idVec2 cST = c->GetTexCoord();
  769. float d0[5];
  770. d0[0] = b->xyz[0] - a->xyz[0];
  771. d0[1] = b->xyz[1] - a->xyz[1];
  772. d0[2] = b->xyz[2] - a->xyz[2];
  773. d0[3] = bST[0] - aST[0];
  774. d0[4] = bST[1] - aST[1];
  775. float d1[5];
  776. d1[0] = c->xyz[0] - a->xyz[0];
  777. d1[1] = c->xyz[1] - a->xyz[1];
  778. d1[2] = c->xyz[2] - a->xyz[2];
  779. d1[3] = cST[0] - aST[0];
  780. d1[4] = cST[1] - aST[1];
  781. idVec3 normal;
  782. normal[0] = d1[1] * d0[2] - d1[2] * d0[1];
  783. normal[1] = d1[2] * d0[0] - d1[0] * d0[2];
  784. normal[2] = d1[0] * d0[1] - d1[1] * d0[0];
  785. const float f0 = idMath::InvSqrt( normal.x * normal.x + normal.y * normal.y + normal.z * normal.z );
  786. normal.x *= f0;
  787. normal.y *= f0;
  788. normal.z *= f0;
  789. // area sign bit
  790. const float area = d0[3] * d1[4] - d0[4] * d1[3];
  791. unsigned int signBit = ( *(unsigned int *)&area ) & ( 1 << 31 );
  792. idVec3 tangent;
  793. tangent[0] = d0[0] * d1[4] - d0[4] * d1[0];
  794. tangent[1] = d0[1] * d1[4] - d0[4] * d1[1];
  795. tangent[2] = d0[2] * d1[4] - d0[4] * d1[2];
  796. const float f1 = idMath::InvSqrt( tangent.x * tangent.x + tangent.y * tangent.y + tangent.z * tangent.z );
  797. *(unsigned int *)&f1 ^= signBit;
  798. tangent.x *= f1;
  799. tangent.y *= f1;
  800. tangent.z *= f1;
  801. idVec3 bitangent;
  802. bitangent[0] = d0[3] * d1[0] - d0[0] * d1[3];
  803. bitangent[1] = d0[3] * d1[1] - d0[1] * d1[3];
  804. bitangent[2] = d0[3] * d1[2] - d0[2] * d1[3];
  805. const float f2 = idMath::InvSqrt( bitangent.x * bitangent.x + bitangent.y * bitangent.y + bitangent.z * bitangent.z );
  806. *(unsigned int *)&f2 ^= signBit;
  807. bitangent.x *= f2;
  808. bitangent.y *= f2;
  809. bitangent.z *= f2;
  810. vertexNormals[v0] += normal;
  811. vertexTangents[v0] += tangent;
  812. vertexBitangents[v0] += bitangent;
  813. vertexNormals[v1] += normal;
  814. vertexTangents[v1] += tangent;
  815. vertexBitangents[v1] += bitangent;
  816. vertexNormals[v2] += normal;
  817. vertexTangents[v2] += tangent;
  818. vertexBitangents[v2] += bitangent;
  819. }
  820. // add the normal of a duplicated vertex to the normal of the first vertex with the same XYZ
  821. for ( int i = 0; i < tri->numDupVerts; i++ ) {
  822. vertexNormals[tri->dupVerts[i*2+0]] += vertexNormals[tri->dupVerts[i*2+1]];
  823. }
  824. // copy vertex normals to duplicated vertices
  825. for ( int i = 0; i < tri->numDupVerts; i++ ) {
  826. vertexNormals[tri->dupVerts[i*2+1]] = vertexNormals[tri->dupVerts[i*2+0]];
  827. }
  828. // Project the summed vectors onto the normal plane and normalize.
  829. // The tangent vectors will not necessarily be orthogonal to each
  830. // other, but they will be orthogonal to the surface normal.
  831. for ( int i = 0; i < tri->numVerts; i++ ) {
  832. const float normalScale = idMath::InvSqrt( vertexNormals[i].x * vertexNormals[i].x + vertexNormals[i].y * vertexNormals[i].y + vertexNormals[i].z * vertexNormals[i].z );
  833. vertexNormals[i].x *= normalScale;
  834. vertexNormals[i].y *= normalScale;
  835. vertexNormals[i].z *= normalScale;
  836. vertexTangents[i] -= ( vertexTangents[i] * vertexNormals[i] ) * vertexNormals[i];
  837. vertexBitangents[i] -= ( vertexBitangents[i] * vertexNormals[i] ) * vertexNormals[i];
  838. const float tangentScale = idMath::InvSqrt( vertexTangents[i].x * vertexTangents[i].x + vertexTangents[i].y * vertexTangents[i].y + vertexTangents[i].z * vertexTangents[i].z );
  839. vertexTangents[i].x *= tangentScale;
  840. vertexTangents[i].y *= tangentScale;
  841. vertexTangents[i].z *= tangentScale;
  842. const float bitangentScale = idMath::InvSqrt( vertexBitangents[i].x * vertexBitangents[i].x + vertexBitangents[i].y * vertexBitangents[i].y + vertexBitangents[i].z * vertexBitangents[i].z );
  843. vertexBitangents[i].x *= bitangentScale;
  844. vertexBitangents[i].y *= bitangentScale;
  845. vertexBitangents[i].z *= bitangentScale;
  846. }
  847. // compress the normals and tangents
  848. for ( int i = 0; i < tri->numVerts; i++ ) {
  849. tri->verts[i].SetNormal( vertexNormals[i] );
  850. tri->verts[i].SetTangent( vertexTangents[i] );
  851. tri->verts[i].SetBiTangent( vertexBitangents[i] );
  852. }
  853. }
  854. /*
  855. ============
  856. R_DeriveUnsmoothedNormalsAndTangents
  857. ============
  858. */
  859. void R_DeriveUnsmoothedNormalsAndTangents( srfTriangles_t * tri ) {
  860. for ( int i = 0; i < tri->numVerts; i++ ) {
  861. float d0, d1, d2, d3, d4;
  862. float d5, d6, d7, d8, d9;
  863. float s0, s1, s2;
  864. float n0, n1, n2;
  865. float t0, t1, t2;
  866. float t3, t4, t5;
  867. const dominantTri_t &dt = tri->dominantTris[i];
  868. idDrawVert *a = tri->verts + i;
  869. idDrawVert *b = tri->verts + dt.v2;
  870. idDrawVert *c = tri->verts + dt.v3;
  871. const idVec2 aST = a->GetTexCoord();
  872. const idVec2 bST = b->GetTexCoord();
  873. const idVec2 cST = c->GetTexCoord();
  874. d0 = b->xyz[0] - a->xyz[0];
  875. d1 = b->xyz[1] - a->xyz[1];
  876. d2 = b->xyz[2] - a->xyz[2];
  877. d3 = bST[0] - aST[0];
  878. d4 = bST[1] - aST[1];
  879. d5 = c->xyz[0] - a->xyz[0];
  880. d6 = c->xyz[1] - a->xyz[1];
  881. d7 = c->xyz[2] - a->xyz[2];
  882. d8 = cST[0] - aST[0];
  883. d9 = cST[1] - aST[1];
  884. s0 = dt.normalizationScale[0];
  885. s1 = dt.normalizationScale[1];
  886. s2 = dt.normalizationScale[2];
  887. n0 = s2 * ( d6 * d2 - d7 * d1 );
  888. n1 = s2 * ( d7 * d0 - d5 * d2 );
  889. n2 = s2 * ( d5 * d1 - d6 * d0 );
  890. t0 = s0 * ( d0 * d9 - d4 * d5 );
  891. t1 = s0 * ( d1 * d9 - d4 * d6 );
  892. t2 = s0 * ( d2 * d9 - d4 * d7 );
  893. #ifndef DERIVE_UNSMOOTHED_BITANGENT
  894. t3 = s1 * ( d3 * d5 - d0 * d8 );
  895. t4 = s1 * ( d3 * d6 - d1 * d8 );
  896. t5 = s1 * ( d3 * d7 - d2 * d8 );
  897. #else
  898. t3 = s1 * ( n2 * t1 - n1 * t2 );
  899. t4 = s1 * ( n0 * t2 - n2 * t0 );
  900. t5 = s1 * ( n1 * t0 - n0 * t1 );
  901. #endif
  902. a->SetNormal( n0, n1, n2 );
  903. a->SetTangent( t0, t1, t2 );
  904. a->SetBiTangent( t3, t4, t5 );
  905. }
  906. }
  907. /*
  908. =====================
  909. R_CreateVertexNormals
  910. Averages together the contributions of all faces that are
  911. used by a vertex, creating drawVert->normal
  912. =====================
  913. */
  914. void R_CreateVertexNormals( srfTriangles_t *tri ) {
  915. if ( tri->silIndexes == NULL ) {
  916. R_CreateSilIndexes( tri );
  917. }
  918. idTempArray< idVec3 > vertexNormals( tri->numVerts );
  919. vertexNormals.Zero();
  920. assert( tri->silIndexes != NULL );
  921. for ( int i = 0; i < tri->numIndexes; i += 3 ) {
  922. const int i0 = tri->silIndexes[i + 0];
  923. const int i1 = tri->silIndexes[i + 1];
  924. const int i2 = tri->silIndexes[i + 2];
  925. const idDrawVert & v0 = tri->verts[i0];
  926. const idDrawVert & v1 = tri->verts[i1];
  927. const idDrawVert & v2 = tri->verts[i2];
  928. const idPlane plane( v0.xyz, v1.xyz, v2.xyz );
  929. vertexNormals[i0] += plane.Normal();
  930. vertexNormals[i1] += plane.Normal();
  931. vertexNormals[i2] += plane.Normal();
  932. }
  933. // replicate from silIndexes to all indexes
  934. for ( int i = 0; i < tri->numIndexes; i++ ) {
  935. vertexNormals[tri->indexes[i]] = vertexNormals[tri->silIndexes[i]];
  936. }
  937. // normalize
  938. for ( int i = 0; i < tri->numVerts; i++ ) {
  939. vertexNormals[i].Normalize();
  940. }
  941. // compress the normals
  942. for ( int i = 0; i < tri->numVerts; i++ ) {
  943. tri->verts[i].SetNormal( vertexNormals[i] );
  944. }
  945. }
  946. /*
  947. =================
  948. R_DeriveTangentsWithoutNormals
  949. Build texture space tangents for bump mapping
  950. If a surface is deformed, this must be recalculated
  951. This assumes that any mirrored vertexes have already been duplicated, so
  952. any shared vertexes will have the tangent spaces smoothed across.
  953. Texture wrapping slightly complicates this, but as long as the normals
  954. are shared, and the tangent vectors are projected onto the normals, the
  955. separate vertexes should wind up with identical tangent spaces.
  956. mirroring a normalmap WILL cause a slightly visible seam unless the normals
  957. are completely flat around the edge's full bilerp support.
  958. Vertexes which are smooth shaded must have their tangent vectors
  959. in the same plane, which will allow a seamless
  960. rendering as long as the normal map is even on both sides of the
  961. seam.
  962. A smooth shaded surface may have multiple tangent vectors at a vertex
  963. due to texture seams or mirroring, but it should only have a single
  964. normal vector.
  965. Each triangle has a pair of tangent vectors in it's plane
  966. Should we consider having vertexes point at shared tangent spaces
  967. to save space or speed transforms?
  968. this version only handles bilateral symetry
  969. =================
  970. */
  971. void R_DeriveTangentsWithoutNormals( srfTriangles_t *tri ) {
  972. idTempArray< idVec3 > triangleTangents( tri->numIndexes / 3 );
  973. idTempArray< idVec3 > triangleBitangents( tri->numIndexes / 3 );
  974. //
  975. // calculate tangent vectors for each face in isolation
  976. //
  977. int c_positive = 0;
  978. int c_negative = 0;
  979. int c_textureDegenerateFaces = 0;
  980. for ( int i = 0; i < tri->numIndexes; i += 3 ) {
  981. idVec3 temp;
  982. idDrawVert * a = tri->verts + tri->indexes[i + 0];
  983. idDrawVert * b = tri->verts + tri->indexes[i + 1];
  984. idDrawVert * c = tri->verts + tri->indexes[i + 2];
  985. const idVec2 aST = a->GetTexCoord();
  986. const idVec2 bST = b->GetTexCoord();
  987. const idVec2 cST = c->GetTexCoord();
  988. float d0[5];
  989. d0[0] = b->xyz[0] - a->xyz[0];
  990. d0[1] = b->xyz[1] - a->xyz[1];
  991. d0[2] = b->xyz[2] - a->xyz[2];
  992. d0[3] = bST[0] - aST[0];
  993. d0[4] = bST[1] - aST[1];
  994. float d1[5];
  995. d1[0] = c->xyz[0] - a->xyz[0];
  996. d1[1] = c->xyz[1] - a->xyz[1];
  997. d1[2] = c->xyz[2] - a->xyz[2];
  998. d1[3] = cST[0] - aST[0];
  999. d1[4] = cST[1] - aST[1];
  1000. const float area = d0[3] * d1[4] - d0[4] * d1[3];
  1001. if ( fabs( area ) < 1e-20f ) {
  1002. triangleTangents[i / 3].Zero();
  1003. triangleBitangents[i / 3].Zero();
  1004. c_textureDegenerateFaces++;
  1005. continue;
  1006. }
  1007. if ( area > 0.0f ) {
  1008. c_positive++;
  1009. } else {
  1010. c_negative++;
  1011. }
  1012. #ifdef USE_INVA
  1013. float inva = ( area < 0.0f ) ? -1.0f : 1.0f; // was = 1.0f / area;
  1014. temp[0] = ( d0[0] * d1[4] - d0[4] * d1[0] ) * inva;
  1015. temp[1] = ( d0[1] * d1[4] - d0[4] * d1[1] ) * inva;
  1016. temp[2] = ( d0[2] * d1[4] - d0[4] * d1[2] ) * inva;
  1017. temp.Normalize();
  1018. triangleTangents[i / 3] = temp;
  1019. temp[0] = ( d0[3] * d1[0] - d0[0] * d1[3] ) * inva;
  1020. temp[1] = ( d0[3] * d1[1] - d0[1] * d1[3] ) * inva;
  1021. temp[2] = ( d0[3] * d1[2] - d0[2] * d1[3] ) * inva;
  1022. temp.Normalize();
  1023. triangleBitangents[i / 3] = temp;
  1024. #else
  1025. temp[0] = ( d0[0] * d1[4] - d0[4] * d1[0] );
  1026. temp[1] = ( d0[1] * d1[4] - d0[4] * d1[1] );
  1027. temp[2] = ( d0[2] * d1[4] - d0[4] * d1[2] );
  1028. temp.Normalize();
  1029. triangleTangents[i / 3] = temp;
  1030. temp[0] = ( d0[3] * d1[0] - d0[0] * d1[3] );
  1031. temp[1] = ( d0[3] * d1[1] - d0[1] * d1[3] );
  1032. temp[2] = ( d0[3] * d1[2] - d0[2] * d1[3] );
  1033. temp.Normalize();
  1034. triangleBitangents[i / 3] = temp;
  1035. #endif
  1036. }
  1037. idTempArray< idVec3 > vertexTangents( tri->numVerts );
  1038. idTempArray< idVec3 > vertexBitangents( tri->numVerts );
  1039. // clear the tangents
  1040. for ( int i = 0; i < tri->numVerts; ++i ) {
  1041. vertexTangents[i].Zero();
  1042. vertexBitangents[i].Zero();
  1043. }
  1044. // sum up the neighbors
  1045. for ( int i = 0; i < tri->numIndexes; i += 3 ) {
  1046. // for each vertex on this face
  1047. for ( int j = 0; j < 3; j++ ) {
  1048. vertexTangents[tri->indexes[i+j]] += triangleTangents[i / 3];
  1049. vertexBitangents[tri->indexes[i+j]] += triangleBitangents[i / 3];
  1050. }
  1051. }
  1052. // Project the summed vectors onto the normal plane and normalize.
  1053. // The tangent vectors will not necessarily be orthogonal to each
  1054. // other, but they will be orthogonal to the surface normal.
  1055. for ( int i = 0; i < tri->numVerts; i++ ) {
  1056. idVec3 normal = tri->verts[i].GetNormal();
  1057. normal.Normalize();
  1058. vertexTangents[i] -= ( vertexTangents[i] * normal ) * normal;
  1059. vertexTangents[i].Normalize();
  1060. vertexBitangents[i] -= ( vertexBitangents[i] * normal ) * normal;
  1061. vertexBitangents[i].Normalize();
  1062. }
  1063. for ( int i = 0; i < tri->numVerts; i++ ) {
  1064. tri->verts[i].SetTangent( vertexTangents[i] );
  1065. tri->verts[i].SetBiTangent( vertexBitangents[i] );
  1066. }
  1067. tri->tangentsCalculated = true;
  1068. }
  1069. /*
  1070. ===================
  1071. R_BuildDominantTris
  1072. Find the largest triangle that uses each vertex
  1073. ===================
  1074. */
  1075. typedef struct {
  1076. int vertexNum;
  1077. int faceNum;
  1078. } indexSort_t;
  1079. static int IndexSort( const void *a, const void *b ) {
  1080. if ( ((indexSort_t *)a)->vertexNum < ((indexSort_t *)b)->vertexNum ) {
  1081. return -1;
  1082. }
  1083. if ( ((indexSort_t *)a)->vertexNum > ((indexSort_t *)b)->vertexNum ) {
  1084. return 1;
  1085. }
  1086. return 0;
  1087. }
  1088. void R_BuildDominantTris( srfTriangles_t *tri ) {
  1089. int i, j;
  1090. dominantTri_t *dt;
  1091. const int numIndexes = tri->numIndexes;
  1092. indexSort_t *ind = (indexSort_t *)R_StaticAlloc( numIndexes * sizeof( indexSort_t ) );
  1093. if ( ind == NULL ) {
  1094. idLib::Error( "Couldn't allocate index sort array" );
  1095. return;
  1096. }
  1097. for ( i = 0; i < tri->numIndexes; i++ ) {
  1098. ind[i].vertexNum = tri->indexes[i];
  1099. ind[i].faceNum = i / 3;
  1100. }
  1101. qsort( ind, tri->numIndexes, sizeof( *ind ), IndexSort );
  1102. R_AllocStaticTriSurfDominantTris( tri, tri->numVerts );
  1103. dt = tri->dominantTris;
  1104. memset( dt, 0, tri->numVerts * sizeof( dt[0] ) );
  1105. for ( i = 0; i < numIndexes; i += j ) {
  1106. float maxArea = 0;
  1107. #pragma warning( disable: 6385 ) // This is simply to get pass a false defect for /analyze -- if you can figure out a better way, please let Shawn know...
  1108. int vertNum = ind[i].vertexNum;
  1109. #pragma warning( default: 6385 )
  1110. for ( j = 0; i + j < tri->numIndexes && ind[i+j].vertexNum == vertNum; j++ ) {
  1111. float d0[5], d1[5];
  1112. idDrawVert *a, *b, *c;
  1113. idVec3 normal, tangent, bitangent;
  1114. int i1 = tri->indexes[ind[i+j].faceNum * 3 + 0];
  1115. int i2 = tri->indexes[ind[i+j].faceNum * 3 + 1];
  1116. int i3 = tri->indexes[ind[i+j].faceNum * 3 + 2];
  1117. a = tri->verts + i1;
  1118. b = tri->verts + i2;
  1119. c = tri->verts + i3;
  1120. const idVec2 aST = a->GetTexCoord();
  1121. const idVec2 bST = b->GetTexCoord();
  1122. const idVec2 cST = c->GetTexCoord();
  1123. d0[0] = b->xyz[0] - a->xyz[0];
  1124. d0[1] = b->xyz[1] - a->xyz[1];
  1125. d0[2] = b->xyz[2] - a->xyz[2];
  1126. d0[3] = bST[0] - aST[0];
  1127. d0[4] = bST[1] - aST[1];
  1128. d1[0] = c->xyz[0] - a->xyz[0];
  1129. d1[1] = c->xyz[1] - a->xyz[1];
  1130. d1[2] = c->xyz[2] - a->xyz[2];
  1131. d1[3] = cST[0] - aST[0];
  1132. d1[4] = cST[1] - aST[1];
  1133. normal[0] = ( d1[1] * d0[2] - d1[2] * d0[1] );
  1134. normal[1] = ( d1[2] * d0[0] - d1[0] * d0[2] );
  1135. normal[2] = ( d1[0] * d0[1] - d1[1] * d0[0] );
  1136. float area = normal.Length();
  1137. // if this is smaller than what we already have, skip it
  1138. if ( area < maxArea ) {
  1139. continue;
  1140. }
  1141. maxArea = area;
  1142. if ( i1 == vertNum ) {
  1143. dt[vertNum].v2 = i2;
  1144. dt[vertNum].v3 = i3;
  1145. } else if ( i2 == vertNum ) {
  1146. dt[vertNum].v2 = i3;
  1147. dt[vertNum].v3 = i1;
  1148. } else {
  1149. dt[vertNum].v2 = i1;
  1150. dt[vertNum].v3 = i2;
  1151. }
  1152. float len = area;
  1153. if ( len < 0.001f ) {
  1154. len = 0.001f;
  1155. }
  1156. dt[vertNum].normalizationScale[2] = 1.0f / len; // normal
  1157. // texture area
  1158. area = d0[3] * d1[4] - d0[4] * d1[3];
  1159. tangent[0] = ( d0[0] * d1[4] - d0[4] * d1[0] );
  1160. tangent[1] = ( d0[1] * d1[4] - d0[4] * d1[1] );
  1161. tangent[2] = ( d0[2] * d1[4] - d0[4] * d1[2] );
  1162. len = tangent.Length();
  1163. if ( len < 0.001f ) {
  1164. len = 0.001f;
  1165. }
  1166. dt[vertNum].normalizationScale[0] = ( area > 0 ? 1 : -1 ) / len; // tangents[0]
  1167. bitangent[0] = ( d0[3] * d1[0] - d0[0] * d1[3] );
  1168. bitangent[1] = ( d0[3] * d1[1] - d0[1] * d1[3] );
  1169. bitangent[2] = ( d0[3] * d1[2] - d0[2] * d1[3] );
  1170. len = bitangent.Length();
  1171. if ( len < 0.001f ) {
  1172. len = 0.001f;
  1173. }
  1174. #ifdef DERIVE_UNSMOOTHED_BITANGENT
  1175. dt[vertNum].normalizationScale[1] = ( area > 0 ? 1 : -1 );
  1176. #else
  1177. dt[vertNum].normalizationScale[1] = ( area > 0 ? 1 : -1 ) / len; // tangents[1]
  1178. #endif
  1179. }
  1180. }
  1181. R_StaticFree( ind );
  1182. }
  1183. /*
  1184. ==================
  1185. R_DeriveTangents
  1186. This is called once for static surfaces, and every frame for deforming surfaces
  1187. Builds tangents, normals, and face planes
  1188. ==================
  1189. */
  1190. void R_DeriveTangents( srfTriangles_t *tri ) {
  1191. if ( tri->tangentsCalculated ) {
  1192. return;
  1193. }
  1194. tr.pc.c_tangentIndexes += tri->numIndexes;
  1195. if ( tri->dominantTris != NULL ) {
  1196. R_DeriveUnsmoothedNormalsAndTangents( tri );
  1197. } else {
  1198. R_DeriveNormalsAndTangents( tri );
  1199. }
  1200. tri->tangentsCalculated = true;
  1201. }
  1202. /*
  1203. =================
  1204. R_RemoveDuplicatedTriangles
  1205. silIndexes must have already been calculated
  1206. silIndexes are used instead of indexes, because duplicated
  1207. triangles could have different texture coordinates.
  1208. =================
  1209. */
  1210. void R_RemoveDuplicatedTriangles( srfTriangles_t *tri ) {
  1211. int c_removed;
  1212. int i, j, r;
  1213. int a, b, c;
  1214. c_removed = 0;
  1215. // check for completely duplicated triangles
  1216. // any rotation of the triangle is still the same, but a mirroring
  1217. // is considered different
  1218. for ( i = 0; i < tri->numIndexes; i+=3 ) {
  1219. for ( r = 0; r < 3; r++ ) {
  1220. a = tri->silIndexes[i+r];
  1221. b = tri->silIndexes[i+(r+1)%3];
  1222. c = tri->silIndexes[i+(r+2)%3];
  1223. for ( j = i + 3; j < tri->numIndexes; j+=3 ) {
  1224. if ( tri->silIndexes[j] == a && tri->silIndexes[j+1] == b && tri->silIndexes[j+2] == c ) {
  1225. c_removed++;
  1226. memmove( tri->indexes + j, tri->indexes + j + 3, ( tri->numIndexes - j - 3 ) * sizeof( tri->indexes[0] ) );
  1227. memmove( tri->silIndexes + j, tri->silIndexes + j + 3, ( tri->numIndexes - j - 3 ) * sizeof( tri->silIndexes[0] ) );
  1228. tri->numIndexes -= 3;
  1229. j -= 3;
  1230. }
  1231. }
  1232. }
  1233. }
  1234. if ( c_removed ) {
  1235. common->Printf( "removed %i duplicated triangles\n", c_removed );
  1236. }
  1237. }
  1238. /*
  1239. =================
  1240. R_RemoveDegenerateTriangles
  1241. silIndexes must have already been calculated
  1242. =================
  1243. */
  1244. void R_RemoveDegenerateTriangles( srfTriangles_t *tri ) {
  1245. int c_removed;
  1246. int i;
  1247. int a, b, c;
  1248. assert( tri->silIndexes != NULL );
  1249. // check for completely degenerate triangles
  1250. c_removed = 0;
  1251. for ( i = 0; i < tri->numIndexes; i += 3 ) {
  1252. a = tri->silIndexes[i];
  1253. b = tri->silIndexes[i+1];
  1254. c = tri->silIndexes[i+2];
  1255. if ( a == b || a == c || b == c ) {
  1256. c_removed++;
  1257. memmove( tri->indexes + i, tri->indexes + i + 3, ( tri->numIndexes - i - 3 ) * sizeof( tri->indexes[0] ) );
  1258. memmove( tri->silIndexes + i, tri->silIndexes + i + 3, ( tri->numIndexes - i - 3 ) * sizeof( tri->silIndexes[0] ) );
  1259. tri->numIndexes -= 3;
  1260. i -= 3;
  1261. }
  1262. }
  1263. // this doesn't free the memory used by the unused verts
  1264. if ( c_removed ) {
  1265. common->Printf( "removed %i degenerate triangles\n", c_removed );
  1266. }
  1267. }
  1268. /*
  1269. =================
  1270. R_TestDegenerateTextureSpace
  1271. =================
  1272. */
  1273. void R_TestDegenerateTextureSpace( srfTriangles_t *tri ) {
  1274. int c_degenerate;
  1275. int i;
  1276. // check for triangles with a degenerate texture space
  1277. c_degenerate = 0;
  1278. for ( i = 0; i < tri->numIndexes; i += 3 ) {
  1279. const idDrawVert &a = tri->verts[tri->indexes[i+0]];
  1280. const idDrawVert &b = tri->verts[tri->indexes[i+1]];
  1281. const idDrawVert &c = tri->verts[tri->indexes[i+2]];
  1282. if ( a.st == b.st || b.st == c.st || c.st == a.st ) {
  1283. c_degenerate++;
  1284. }
  1285. }
  1286. if ( c_degenerate ) {
  1287. // common->Printf( "%d triangles with a degenerate texture space\n", c_degenerate );
  1288. }
  1289. }
  1290. /*
  1291. =================
  1292. R_RemoveUnusedVerts
  1293. =================
  1294. */
  1295. void R_RemoveUnusedVerts( srfTriangles_t *tri ) {
  1296. int i;
  1297. int *mark;
  1298. int index;
  1299. int used;
  1300. mark = (int *)R_ClearedStaticAlloc( tri->numVerts * sizeof( *mark ) );
  1301. for ( i = 0; i < tri->numIndexes; i++ ) {
  1302. index = tri->indexes[i];
  1303. if ( index < 0 || index >= tri->numVerts ) {
  1304. common->Error( "R_RemoveUnusedVerts: bad index" );
  1305. }
  1306. mark[ index ] = 1;
  1307. if ( tri->silIndexes ) {
  1308. index = tri->silIndexes[i];
  1309. if ( index < 0 || index >= tri->numVerts ) {
  1310. common->Error( "R_RemoveUnusedVerts: bad index" );
  1311. }
  1312. mark[ index ] = 1;
  1313. }
  1314. }
  1315. used = 0;
  1316. for ( i = 0; i < tri->numVerts; i++ ) {
  1317. if ( !mark[i] ) {
  1318. continue;
  1319. }
  1320. mark[i] = used + 1;
  1321. used++;
  1322. }
  1323. if ( used != tri->numVerts ) {
  1324. for ( i = 0; i < tri->numIndexes; i++ ) {
  1325. tri->indexes[i] = mark[ tri->indexes[i] ] - 1;
  1326. if ( tri->silIndexes ) {
  1327. tri->silIndexes[i] = mark[ tri->silIndexes[i] ] - 1;
  1328. }
  1329. }
  1330. tri->numVerts = used;
  1331. for ( i = 0; i < tri->numVerts; i++ ) {
  1332. index = mark[ i ];
  1333. if ( !index ) {
  1334. continue;
  1335. }
  1336. tri->verts[ index - 1 ] = tri->verts[i];
  1337. }
  1338. // this doesn't realloc the arrays to save the memory used by the unused verts
  1339. }
  1340. R_StaticFree( mark );
  1341. }
  1342. /*
  1343. =================
  1344. R_MergeSurfaceList
  1345. Only deals with vertexes and indexes, not silhouettes, planes, etc.
  1346. Does NOT perform a cleanup triangles, so there may be duplicated verts in the result.
  1347. =================
  1348. */
  1349. srfTriangles_t * R_MergeSurfaceList( const srfTriangles_t **surfaces, int numSurfaces ) {
  1350. srfTriangles_t *newTri;
  1351. const srfTriangles_t *tri;
  1352. int i, j;
  1353. int totalVerts;
  1354. int totalIndexes;
  1355. totalVerts = 0;
  1356. totalIndexes = 0;
  1357. for ( i = 0; i < numSurfaces; i++ ) {
  1358. totalVerts += surfaces[i]->numVerts;
  1359. totalIndexes += surfaces[i]->numIndexes;
  1360. }
  1361. newTri = R_AllocStaticTriSurf();
  1362. newTri->numVerts = totalVerts;
  1363. newTri->numIndexes = totalIndexes;
  1364. R_AllocStaticTriSurfVerts( newTri, newTri->numVerts );
  1365. R_AllocStaticTriSurfIndexes( newTri, newTri->numIndexes );
  1366. totalVerts = 0;
  1367. totalIndexes = 0;
  1368. for ( i = 0; i < numSurfaces; i++ ) {
  1369. tri = surfaces[i];
  1370. memcpy( newTri->verts + totalVerts, tri->verts, tri->numVerts * sizeof( *tri->verts ) );
  1371. for ( j = 0; j < tri->numIndexes; j++ ) {
  1372. newTri->indexes[ totalIndexes + j ] = totalVerts + tri->indexes[j];
  1373. }
  1374. totalVerts += tri->numVerts;
  1375. totalIndexes += tri->numIndexes;
  1376. }
  1377. return newTri;
  1378. }
  1379. /*
  1380. =================
  1381. R_MergeTriangles
  1382. Only deals with vertexes and indexes, not silhouettes, planes, etc.
  1383. Does NOT perform a cleanup triangles, so there may be duplicated verts in the result.
  1384. =================
  1385. */
  1386. srfTriangles_t * R_MergeTriangles( const srfTriangles_t *tri1, const srfTriangles_t *tri2 ) {
  1387. const srfTriangles_t *tris[2];
  1388. tris[0] = tri1;
  1389. tris[1] = tri2;
  1390. return R_MergeSurfaceList( tris, 2 );
  1391. }
  1392. /*
  1393. =================
  1394. R_ReverseTriangles
  1395. Lit two sided surfaces need to have the triangles actually duplicated,
  1396. they can't just turn on two sided lighting, because the normal and tangents
  1397. are wrong on the other sides.
  1398. This should be called before R_CleanupTriangles
  1399. =================
  1400. */
  1401. void R_ReverseTriangles( srfTriangles_t *tri ) {
  1402. int i;
  1403. // flip the normal on each vertex
  1404. // If the surface is going to have generated normals, this won't matter,
  1405. // but if it has explicit normals, this will keep it on the correct side
  1406. for ( i = 0; i < tri->numVerts; i++ ) {
  1407. tri->verts[i].SetNormal( vec3_origin - tri->verts[i].GetNormal() );
  1408. }
  1409. // flip the index order to make them back sided
  1410. for ( i = 0; i < tri->numIndexes; i+= 3 ) {
  1411. triIndex_t temp;
  1412. temp = tri->indexes[ i + 0 ];
  1413. tri->indexes[ i + 0 ] = tri->indexes[ i + 1 ];
  1414. tri->indexes[ i + 1 ] = temp;
  1415. }
  1416. }
  1417. /*
  1418. =================
  1419. R_CleanupTriangles
  1420. FIXME: allow createFlat and createSmooth normals, as well as explicit
  1421. =================
  1422. */
  1423. void R_CleanupTriangles( srfTriangles_t *tri, bool createNormals, bool identifySilEdges, bool useUnsmoothedTangents ) {
  1424. R_RangeCheckIndexes( tri );
  1425. R_CreateSilIndexes( tri );
  1426. // R_RemoveDuplicatedTriangles( tri ); // this may remove valid overlapped transparent triangles
  1427. R_RemoveDegenerateTriangles( tri );
  1428. R_TestDegenerateTextureSpace( tri );
  1429. // R_RemoveUnusedVerts( tri );
  1430. if ( identifySilEdges ) {
  1431. R_IdentifySilEdges( tri, true ); // assume it is non-deformable, and omit coplanar edges
  1432. }
  1433. // bust vertexes that share a mirrored edge into separate vertexes
  1434. R_DuplicateMirroredVertexes( tri );
  1435. R_CreateDupVerts( tri );
  1436. R_BoundTriSurf( tri );
  1437. if ( useUnsmoothedTangents ) {
  1438. R_BuildDominantTris( tri );
  1439. R_DeriveTangents( tri );
  1440. } else if ( !createNormals ) {
  1441. R_DeriveTangentsWithoutNormals( tri );
  1442. } else {
  1443. R_DeriveTangents( tri );
  1444. }
  1445. }
  1446. /*
  1447. ===================================================================================
  1448. DEFORMED SURFACES
  1449. ===================================================================================
  1450. */
  1451. /*
  1452. ===================
  1453. R_BuildDeformInfo
  1454. ===================
  1455. */
  1456. deformInfo_t *R_BuildDeformInfo( int numVerts, const idDrawVert *verts, int numIndexes, const int *indexes,
  1457. bool useUnsmoothedTangents ) {
  1458. srfTriangles_t tri;
  1459. memset( &tri, 0, sizeof( srfTriangles_t ) );
  1460. tri.numVerts = numVerts;
  1461. R_AllocStaticTriSurfVerts( &tri, tri.numVerts );
  1462. SIMDProcessor->Memcpy( tri.verts, verts, tri.numVerts * sizeof( tri.verts[0] ) );
  1463. tri.numIndexes = numIndexes;
  1464. R_AllocStaticTriSurfIndexes( &tri, tri.numIndexes );
  1465. // don't memcpy, so we can change the index type from int to short without changing the interface
  1466. for ( int i = 0; i < tri.numIndexes; i++ ) {
  1467. tri.indexes[i] = indexes[i];
  1468. }
  1469. R_RangeCheckIndexes( &tri );
  1470. R_CreateSilIndexes( &tri );
  1471. R_IdentifySilEdges( &tri, false ); // we cannot remove coplanar edges, because they can deform to silhouettes
  1472. R_DuplicateMirroredVertexes( &tri ); // split mirror points into multiple points
  1473. R_CreateDupVerts( &tri );
  1474. if ( useUnsmoothedTangents ) {
  1475. R_BuildDominantTris( &tri );
  1476. }
  1477. R_DeriveTangents( &tri );
  1478. deformInfo_t * deform = (deformInfo_t *)R_ClearedStaticAlloc( sizeof( *deform ) );
  1479. deform->numSourceVerts = numVerts;
  1480. deform->numOutputVerts = tri.numVerts;
  1481. deform->verts = tri.verts;
  1482. deform->numIndexes = numIndexes;
  1483. deform->indexes = tri.indexes;
  1484. deform->silIndexes = tri.silIndexes;
  1485. deform->numSilEdges = tri.numSilEdges;
  1486. deform->silEdges = tri.silEdges;
  1487. deform->numMirroredVerts = tri.numMirroredVerts;
  1488. deform->mirroredVerts = tri.mirroredVerts;
  1489. deform->numDupVerts = tri.numDupVerts;
  1490. deform->dupVerts = tri.dupVerts;
  1491. if ( tri.dominantTris != NULL ) {
  1492. Mem_Free( tri.dominantTris );
  1493. tri.dominantTris = NULL;
  1494. }
  1495. idShadowVertSkinned * shadowVerts = (idShadowVertSkinned *) Mem_Alloc16( ALIGN( deform->numOutputVerts * 2 * sizeof( idShadowVertSkinned ), 16 ), TAG_MODEL );
  1496. idShadowVertSkinned::CreateShadowCache( shadowVerts, deform->verts, deform->numOutputVerts );
  1497. deform->staticAmbientCache = vertexCache.AllocStaticVertex( deform->verts, ALIGN( deform->numOutputVerts * sizeof( idDrawVert ), VERTEX_CACHE_ALIGN ) );
  1498. deform->staticIndexCache = vertexCache.AllocStaticIndex( deform->indexes, ALIGN( deform->numIndexes * sizeof( triIndex_t ), INDEX_CACHE_ALIGN ) );
  1499. deform->staticShadowCache = vertexCache.AllocStaticVertex( shadowVerts, ALIGN( deform->numOutputVerts * 2 * sizeof( idShadowVertSkinned ), VERTEX_CACHE_ALIGN ) );
  1500. Mem_Free( shadowVerts );
  1501. return deform;
  1502. }
  1503. /*
  1504. ===================
  1505. R_FreeDeformInfo
  1506. ===================
  1507. */
  1508. void R_FreeDeformInfo( deformInfo_t *deformInfo ) {
  1509. if ( deformInfo->verts != NULL ) {
  1510. Mem_Free( deformInfo->verts );
  1511. }
  1512. if ( deformInfo->indexes != NULL ) {
  1513. Mem_Free( deformInfo->indexes );
  1514. }
  1515. if ( deformInfo->silIndexes != NULL ) {
  1516. Mem_Free( deformInfo->silIndexes );
  1517. }
  1518. if ( deformInfo->silEdges != NULL ) {
  1519. Mem_Free( deformInfo->silEdges );
  1520. }
  1521. if ( deformInfo->mirroredVerts != NULL ) {
  1522. Mem_Free( deformInfo->mirroredVerts );
  1523. }
  1524. if ( deformInfo->dupVerts != NULL ) {
  1525. Mem_Free( deformInfo->dupVerts );
  1526. }
  1527. R_StaticFree( deformInfo );
  1528. }
  1529. /*
  1530. ===================
  1531. R_DeformInfoMemoryUsed
  1532. ===================
  1533. */
  1534. int R_DeformInfoMemoryUsed( deformInfo_t *deformInfo ) {
  1535. int total = 0;
  1536. if ( deformInfo->verts != NULL ) {
  1537. total += deformInfo->numOutputVerts * sizeof( deformInfo->verts[0] );
  1538. }
  1539. if ( deformInfo->indexes != NULL ) {
  1540. total += deformInfo->numIndexes * sizeof( deformInfo->indexes[0] );
  1541. }
  1542. if ( deformInfo->mirroredVerts != NULL ) {
  1543. total += deformInfo->numMirroredVerts * sizeof( deformInfo->mirroredVerts[0] );
  1544. }
  1545. if ( deformInfo->dupVerts != NULL ) {
  1546. total += deformInfo->numDupVerts * sizeof( deformInfo->dupVerts[0] );
  1547. }
  1548. if ( deformInfo->silIndexes != NULL ) {
  1549. total += deformInfo->numIndexes * sizeof( deformInfo->silIndexes[0] );
  1550. }
  1551. if ( deformInfo->silEdges != NULL ) {
  1552. total += deformInfo->numSilEdges * sizeof( deformInfo->silEdges[0] );
  1553. }
  1554. total += sizeof( *deformInfo );
  1555. return total;
  1556. }
  1557. /*
  1558. ===================================================================================
  1559. VERTEX / INDEX CACHING
  1560. ===================================================================================
  1561. */
  1562. /*
  1563. ===================
  1564. R_InitDrawSurfFromTri
  1565. ===================
  1566. */
  1567. void R_InitDrawSurfFromTri( drawSurf_t & ds, srfTriangles_t & tri ) {
  1568. if ( tri.numIndexes == 0 ) {
  1569. ds.numIndexes = 0;
  1570. return;
  1571. }
  1572. // copy verts and indexes to this frame's hardware memory if they aren't already there
  1573. //
  1574. // deformed surfaces will not have any vertices but the ambient cache will have already
  1575. // been created for them.
  1576. if ( ( tri.verts == NULL ) && !tri.referencedIndexes ) {
  1577. // pre-generated shadow models will not have any verts, just shadowVerts
  1578. tri.ambientCache = 0;
  1579. } else if ( !vertexCache.CacheIsCurrent( tri.ambientCache ) ) {
  1580. tri.ambientCache = vertexCache.AllocVertex( tri.verts, ALIGN( tri.numVerts * sizeof( tri.verts[0] ), VERTEX_CACHE_ALIGN ) );
  1581. }
  1582. if ( !vertexCache.CacheIsCurrent( tri.indexCache ) ) {
  1583. tri.indexCache = vertexCache.AllocIndex( tri.indexes, ALIGN( tri.numIndexes * sizeof( tri.indexes[0] ), INDEX_CACHE_ALIGN ) );
  1584. }
  1585. ds.numIndexes = tri.numIndexes;
  1586. ds.ambientCache = tri.ambientCache;
  1587. ds.indexCache = tri.indexCache;
  1588. ds.shadowCache = tri.shadowCache;
  1589. ds.jointCache = 0;
  1590. }
  1591. /*
  1592. ===================
  1593. R_CreateStaticBuffersForTri
  1594. For static surfaces, the indexes, ambient, and shadow buffers can be pre-created at load
  1595. time, rather than being re-created each frame in the frame temporary buffers.
  1596. ===================
  1597. */
  1598. void R_CreateStaticBuffersForTri( srfTriangles_t & tri ) {
  1599. tri.indexCache = 0;
  1600. tri.ambientCache = 0;
  1601. tri.shadowCache = 0;
  1602. // index cache
  1603. if ( tri.indexes != NULL ) {
  1604. tri.indexCache = vertexCache.AllocStaticIndex( tri.indexes, ALIGN( tri.numIndexes * sizeof( tri.indexes[0] ), INDEX_CACHE_ALIGN ) );
  1605. }
  1606. // vertex cache
  1607. if ( tri.verts != NULL ) {
  1608. tri.ambientCache = vertexCache.AllocStaticVertex( tri.verts, ALIGN( tri.numVerts * sizeof( tri.verts[0] ), VERTEX_CACHE_ALIGN ) );
  1609. }
  1610. // shadow cache
  1611. if ( tri.preLightShadowVertexes != NULL ) {
  1612. // this should only be true for the _prelight<NAME> pre-calculated shadow volumes
  1613. assert( tri.verts == NULL ); // pre-light shadow volume surfaces don't have ambient vertices
  1614. const int shadowSize = ALIGN( tri.numVerts * 2 * sizeof( idShadowVert ), VERTEX_CACHE_ALIGN );
  1615. tri.shadowCache = vertexCache.AllocStaticVertex( tri.preLightShadowVertexes, shadowSize );
  1616. } else if ( tri.verts != NULL ) {
  1617. // the shadowVerts for normal models include all the xyz values duplicated
  1618. // for a W of 1 (near cap) and a W of 0 (end cap, projected to infinity)
  1619. const int shadowSize = ALIGN( tri.numVerts * 2 * sizeof( idShadowVert ), VERTEX_CACHE_ALIGN );
  1620. if ( tri.staticShadowVertexes == NULL ) {
  1621. tri.staticShadowVertexes = (idShadowVert *) Mem_Alloc16( shadowSize, TAG_TEMP );
  1622. idShadowVert::CreateShadowCache( tri.staticShadowVertexes, tri.verts, tri.numVerts );
  1623. }
  1624. tri.shadowCache = vertexCache.AllocStaticVertex( tri.staticShadowVertexes, shadowSize );
  1625. #if !defined( KEEP_INTERACTION_CPU_DATA )
  1626. Mem_Free( tri.staticShadowVertexes );
  1627. tri.staticShadowVertexes = NULL;
  1628. #endif
  1629. }
  1630. }