tr_shadowbounds.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. // Compute conservative shadow bounds as the intersection
  24. // of the object's bounds' shadow volume and the light's bounds.
  25. //
  26. // --cass
  27. template <class T, int N>
  28. struct MyArray
  29. {
  30. MyArray() : s(0) {}
  31. MyArray( const MyArray<T,N> & cpy ) : s(cpy.s)
  32. {
  33. for(int i=0; i < s; i++)
  34. v[i] = cpy.v[i];
  35. }
  36. void push_back(const T & i) {
  37. v[s] = i;
  38. s++;
  39. //if(s > max_size)
  40. // max_size = int(s);
  41. }
  42. T & operator[](int i) {
  43. return v[i];
  44. }
  45. const T & operator[](int i) const {
  46. return v[i];
  47. }
  48. unsigned int size() const {
  49. return s;
  50. }
  51. void empty() {
  52. s = 0;
  53. }
  54. T v[N];
  55. int s;
  56. // static int max_size;
  57. };
  58. typedef MyArray<int, 4> MyArrayInt;
  59. //int MyArrayInt::max_size = 0;
  60. typedef MyArray<idVec4, 16> MyArrayVec4;
  61. //int MyArrayVec4::max_size = 0;
  62. struct poly
  63. {
  64. MyArrayInt vi;
  65. MyArrayInt ni;
  66. idVec4 plane;
  67. };
  68. typedef MyArray<poly, 9> MyArrayPoly;
  69. //int MyArrayPoly::max_size = 0;
  70. struct edge
  71. {
  72. int vi[2];
  73. int pi[2];
  74. };
  75. typedef MyArray<edge, 15> MyArrayEdge;
  76. //int MyArrayEdge::max_size = 0;
  77. MyArrayInt four_ints(int a, int b, int c, int d)
  78. {
  79. MyArrayInt vi;
  80. vi.push_back(a);
  81. vi.push_back(b);
  82. vi.push_back(c);
  83. vi.push_back(d);
  84. return vi;
  85. }
  86. idVec3 homogeneous_difference(idVec4 a, idVec4 b)
  87. {
  88. idVec3 v;
  89. v.x = b.x * a.w - a.x * b.w;
  90. v.y = b.y * a.w - a.y * b.w;
  91. v.z = b.z * a.w - a.z * b.w;
  92. return v;
  93. }
  94. // handles positive w only
  95. idVec4 compute_homogeneous_plane(idVec4 a, idVec4 b, idVec4 c)
  96. {
  97. idVec4 v, t;
  98. if(a[3] == 0)
  99. { t = a; a = b; b = c; c = t; }
  100. if(a[3] == 0)
  101. { t = a; a = b; b = c; c = t; }
  102. // can't handle 3 infinite points
  103. if( a[3] == 0 )
  104. return v;
  105. idVec3 vb = homogeneous_difference(a, b);
  106. idVec3 vc = homogeneous_difference(a, c);
  107. idVec3 n = vb.Cross(vc);
  108. n.Normalize();
  109. v.x = n.x;
  110. v.y = n.y;
  111. v.z = n.z;
  112. v.w = - (n * idVec3(a.x, a.y, a.z)) / a.w ;
  113. return v;
  114. }
  115. struct polyhedron
  116. {
  117. MyArrayVec4 v;
  118. MyArrayPoly p;
  119. MyArrayEdge e;
  120. void add_quad( int va, int vb, int vc, int vd )
  121. {
  122. poly pg;
  123. pg.vi = four_ints(va, vb, vc, vd);
  124. pg.ni = four_ints(-1, -1, -1, -1);
  125. pg.plane = compute_homogeneous_plane(v[va], v[vb], v[vc]);
  126. p.push_back(pg);
  127. }
  128. void discard_neighbor_info()
  129. {
  130. for(unsigned int i = 0; i < p.size(); i++ )
  131. {
  132. MyArrayInt & ni = p[i].ni;
  133. for(unsigned int j = 0; j < ni.size(); j++)
  134. ni[j] = -1;
  135. }
  136. }
  137. void compute_neighbors()
  138. {
  139. e.empty();
  140. discard_neighbor_info();
  141. bool found;
  142. int P = p.size();
  143. // for each polygon
  144. for(int i = 0; i < P-1; i++ )
  145. {
  146. const MyArrayInt & vi = p[i].vi;
  147. MyArrayInt & ni = p[i].ni;
  148. int Si = vi.size();
  149. // for each edge of that polygon
  150. for(int ii=0; ii < Si; ii++)
  151. {
  152. int ii0 = ii;
  153. int ii1 = (ii+1) % Si;
  154. // continue if we've already found this neighbor
  155. if(ni[ii] != -1)
  156. continue;
  157. found = false;
  158. // check all remaining polygons
  159. for(int j = i+1; j < P; j++ )
  160. {
  161. const MyArrayInt & vj = p[j].vi;
  162. MyArrayInt & nj = p[j].ni;
  163. int Sj = vj.size();
  164. for( int jj = 0; jj < Sj; jj++ )
  165. {
  166. int jj0 = jj;
  167. int jj1 = (jj+1) % Sj;
  168. if(vi[ii0] == vj[jj1] && vi[ii1] == vj[jj0])
  169. {
  170. edge ed;
  171. ed.vi[0] = vi[ii0];
  172. ed.vi[1] = vi[ii1];
  173. ed.pi[0] = i;
  174. ed.pi[1] = j;
  175. e.push_back(ed);
  176. ni[ii] = j;
  177. nj[jj] = i;
  178. found = true;
  179. break;
  180. }
  181. else if ( vi[ii0] == vj[jj0] && vi[ii1] == vj[jj1] )
  182. {
  183. fprintf(stderr,"why am I here?\n");
  184. }
  185. }
  186. if( found )
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. void recompute_planes()
  193. {
  194. // for each polygon
  195. for(unsigned int i = 0; i < p.size(); i++ )
  196. {
  197. p[i].plane = compute_homogeneous_plane(v[p[i].vi[0]], v[p[i].vi[1]], v[p[i].vi[2]]);
  198. }
  199. }
  200. void transform(const idMat4 & m)
  201. {
  202. for(unsigned int i=0; i < v.size(); i++ )
  203. v[i] = m * v[i];
  204. recompute_planes();
  205. }
  206. };
  207. // make a unit cube
  208. polyhedron PolyhedronFromBounds( const idBounds & b )
  209. {
  210. // 3----------2
  211. // |\ /|
  212. // | \ / |
  213. // | 7--6 |
  214. // | | | |
  215. // | 4--5 |
  216. // | / \ |
  217. // | / \ |
  218. // 0----------1
  219. //
  220. static polyhedron p;
  221. if( p.e.size() == 0 ) {
  222. p.v.push_back(idVec4( -1, -1, 1, 1));
  223. p.v.push_back(idVec4( 1, -1, 1, 1));
  224. p.v.push_back(idVec4( 1, 1, 1, 1));
  225. p.v.push_back(idVec4( -1, 1, 1, 1));
  226. p.v.push_back(idVec4( -1, -1, -1, 1));
  227. p.v.push_back(idVec4( 1, -1, -1, 1));
  228. p.v.push_back(idVec4( 1, 1, -1, 1));
  229. p.v.push_back(idVec4( -1, 1, -1, 1));
  230. p.add_quad( 0, 1, 2, 3 );
  231. p.add_quad( 7, 6, 5, 4 );
  232. p.add_quad( 1, 0, 4, 5 );
  233. p.add_quad( 2, 1, 5, 6 );
  234. p.add_quad( 3, 2, 6, 7 );
  235. p.add_quad( 0, 3, 7, 4 );
  236. p.compute_neighbors();
  237. p.recompute_planes();
  238. p.v.empty(); // no need to copy this data since it'll be replaced
  239. }
  240. polyhedron p2(p);
  241. const idVec3 & min = b[0];
  242. const idVec3 & max = b[1];
  243. p2.v.empty();
  244. p2.v.push_back(idVec4( min.x, min.y, max.z, 1));
  245. p2.v.push_back(idVec4( max.x, min.y, max.z, 1));
  246. p2.v.push_back(idVec4( max.x, max.y, max.z, 1));
  247. p2.v.push_back(idVec4( min.x, max.y, max.z, 1));
  248. p2.v.push_back(idVec4( min.x, min.y, min.z, 1));
  249. p2.v.push_back(idVec4( max.x, min.y, min.z, 1));
  250. p2.v.push_back(idVec4( max.x, max.y, min.z, 1));
  251. p2.v.push_back(idVec4( min.x, max.y, min.z, 1));
  252. p2.recompute_planes();
  253. return p2;
  254. }
  255. polyhedron make_sv(const polyhedron & oc, idVec4 light)
  256. {
  257. static polyhedron lut[64];
  258. int index = 0;
  259. for(unsigned int i = 0; i < 6; i++) {
  260. if( ( oc.p[i].plane * light ) > 0 )
  261. index |= 1<<i;
  262. }
  263. if( lut[index].e.size() == 0 )
  264. {
  265. polyhedron & ph = lut[index];
  266. ph = oc;
  267. int V = ph.v.size();
  268. for( int j = 0; j < V; j++ )
  269. {
  270. idVec3 proj = homogeneous_difference( light, ph.v[j] );
  271. ph.v.push_back( idVec4(proj.x, proj.y, proj.z, 0) );
  272. }
  273. ph.p.empty();
  274. for(unsigned int i=0; i < oc.p.size(); i++)
  275. {
  276. if( (oc.p[i].plane * light) > 0)
  277. {
  278. ph.p.push_back(oc.p[i]);
  279. }
  280. }
  281. if(ph.p.size() == 0)
  282. return ph = polyhedron();
  283. ph.compute_neighbors();
  284. MyArrayPoly vpg;
  285. int I = ph.p.size();
  286. for(int i=0; i < I; i++)
  287. {
  288. MyArrayInt & vi = ph.p[i].vi;
  289. MyArrayInt & ni = ph.p[i].ni;
  290. int S = vi.size();
  291. for(int j = 0; j < S; j++)
  292. {
  293. if( ni[j] == -1 )
  294. {
  295. poly pg;
  296. int a = vi[(j+1)%S];
  297. int b = vi[j];
  298. pg.vi = four_ints( a, b, b+V, a+V);
  299. pg.ni = four_ints(-1, -1, -1, -1);
  300. vpg.push_back(pg);
  301. }
  302. }
  303. }
  304. for(unsigned int i = 0; i < vpg.size(); i++)
  305. ph.p.push_back(vpg[i]);
  306. ph.compute_neighbors();
  307. ph.v.empty(); // no need to copy this data since it'll be replaced
  308. }
  309. polyhedron ph2 = lut[index];
  310. // initalize vertices
  311. ph2.v = oc.v;
  312. int V = ph2.v.size();
  313. for( int j = 0; j < V; j++ )
  314. {
  315. idVec3 proj = homogeneous_difference( light, ph2.v[j] );
  316. ph2.v.push_back( idVec4(proj.x, proj.y, proj.z, 0) );
  317. }
  318. // need to compute planes for the shadow volume (sv)
  319. ph2.recompute_planes();
  320. return ph2;
  321. }
  322. typedef MyArray<idVec4, 36> MySegments;
  323. //int MySegments::max_size = 0;
  324. void polyhedron_edges(polyhedron & a, MySegments & e)
  325. {
  326. e.empty();
  327. if(a.e.size() == 0 && a.p.size() != 0)
  328. a.compute_neighbors();
  329. for(unsigned int i = 0; i < a.e.size(); i++)
  330. {
  331. e.push_back(a.v[a.e[i].vi[0]]);
  332. e.push_back(a.v[a.e[i].vi[1]]);
  333. }
  334. }
  335. // clip the segments of e by the planes of polyhedron a.
  336. void clip_segments(const polyhedron & ph, MySegments & is, MySegments & os)
  337. {
  338. const MyArrayPoly & p = ph.p;
  339. for(unsigned int i = 0; i < is.size(); i+=2 )
  340. {
  341. idVec4 a = is[i ];
  342. idVec4 b = is[i+1];
  343. idVec4 c;
  344. bool discard = false;
  345. for(unsigned int j = 0; j < p.size(); j++ )
  346. {
  347. float da = a * p[j].plane;
  348. float db = b * p[j].plane;
  349. float rdw = 1/(da - db);
  350. int code = 0;
  351. if( da > 0 )
  352. code = 2;
  353. if( db > 0 )
  354. code |= 1;
  355. switch ( code )
  356. {
  357. case 3:
  358. discard = true;
  359. break;
  360. case 2:
  361. c = -db * rdw * a + da * rdw * b;
  362. a = c;
  363. break;
  364. case 1:
  365. c = -db * rdw * a + da * rdw * b;
  366. b = c;
  367. break;
  368. case 0:
  369. break;
  370. default:
  371. common->Printf("bad clip code!\n");
  372. break;
  373. }
  374. if( discard )
  375. break;
  376. }
  377. if( ! discard )
  378. {
  379. os.push_back(a);
  380. os.push_back(b);
  381. }
  382. }
  383. }
  384. idMat4 make_idMat4(const float * m)
  385. {
  386. return idMat4( m[ 0], m[ 4], m[ 8], m[12],
  387. m[ 1], m[ 5], m[ 9], m[13],
  388. m[ 2], m[ 6], m[10], m[14],
  389. m[ 3], m[ 7], m[11], m[15] );
  390. }
  391. idVec3 v4to3(const idVec4 & v)
  392. {
  393. return idVec3(v.x/v.w, v.y/v.w, v.z/v.w);
  394. }
  395. void draw_polyhedron( const viewDef_t *viewDef, const polyhedron & p, idVec4 color )
  396. {
  397. for(unsigned int i = 0; i < p.e.size(); i++)
  398. {
  399. viewDef->renderWorld->DebugLine( color, v4to3(p.v[p.e[i].vi[0]]), v4to3(p.v[p.e[i].vi[1]]));
  400. }
  401. }
  402. void draw_segments( const viewDef_t *viewDef, const MySegments & s, idVec4 color )
  403. {
  404. for(unsigned int i = 0; i < s.size(); i+=2)
  405. {
  406. viewDef->renderWorld->DebugLine( color, v4to3(s[i]), v4to3(s[i+1]));
  407. }
  408. }
  409. void world_to_hclip( const viewDef_t *viewDef, const idVec4 &global, idVec4 &clip ) {
  410. int i;
  411. idVec4 view;
  412. for ( i = 0 ; i < 4 ; i ++ ) {
  413. view[i] =
  414. global[0] * viewDef->worldSpace.modelViewMatrix[ i + 0 * 4 ] +
  415. global[1] * viewDef->worldSpace.modelViewMatrix[ i + 1 * 4 ] +
  416. global[2] * viewDef->worldSpace.modelViewMatrix[ i + 2 * 4 ] +
  417. global[3] * viewDef->worldSpace.modelViewMatrix[ i + 3 * 4 ];
  418. }
  419. for ( i = 0 ; i < 4 ; i ++ ) {
  420. clip[i] =
  421. view[0] * viewDef->projectionMatrix[ i + 0 * 4 ] +
  422. view[1] * viewDef->projectionMatrix[ i + 1 * 4 ] +
  423. view[2] * viewDef->projectionMatrix[ i + 2 * 4 ] +
  424. view[3] * viewDef->projectionMatrix[ i + 3 * 4 ];
  425. }
  426. }
  427. idScreenRect R_CalcIntersectionScissor( const idRenderLightLocal * lightDef,
  428. const idRenderEntityLocal * entityDef,
  429. const viewDef_t * viewDef ) {
  430. idMat4 omodel = make_idMat4( entityDef->modelMatrix );
  431. idMat4 lmodel = make_idMat4( lightDef->modelMatrix );
  432. // compute light polyhedron
  433. polyhedron lvol = PolyhedronFromBounds( lightDef->frustumTris->bounds );
  434. // transform it into world space
  435. //lvol.transform( lmodel );
  436. // debug //
  437. if ( r_useInteractionScissors.GetInteger() == -2 ) {
  438. draw_polyhedron( viewDef, lvol, colorRed );
  439. }
  440. // compute object polyhedron
  441. polyhedron vol = PolyhedronFromBounds( entityDef->referenceBounds );
  442. //viewDef->renderWorld->DebugBounds( colorRed, lightDef->frustumTris->bounds );
  443. //viewDef->renderWorld->DebugBox( colorBlue, idBox( model->Bounds(), entityDef->parms.origin, entityDef->parms.axis ) );
  444. // transform it into world space
  445. vol.transform( omodel );
  446. // debug //
  447. if ( r_useInteractionScissors.GetInteger() == -2 ) {
  448. draw_polyhedron( viewDef, vol, colorBlue );
  449. }
  450. // transform light position into world space
  451. idVec4 lightpos = idVec4(lightDef->globalLightOrigin.x,
  452. lightDef->globalLightOrigin.y,
  453. lightDef->globalLightOrigin.z,
  454. 1.0f );
  455. // generate shadow volume "polyhedron"
  456. polyhedron sv = make_sv(vol, lightpos);
  457. MySegments in_segs, out_segs;
  458. // get shadow volume edges
  459. polyhedron_edges(sv, in_segs);
  460. // clip them against light bounds planes
  461. clip_segments(lvol, in_segs, out_segs);
  462. // get light bounds edges
  463. polyhedron_edges(lvol, in_segs);
  464. // clip them by the shadow volume
  465. clip_segments(sv, in_segs, out_segs);
  466. // debug //
  467. if ( r_useInteractionScissors.GetInteger() == -2 ) {
  468. draw_segments( viewDef, out_segs, colorGreen );
  469. }
  470. idBounds outbounds;
  471. outbounds.Clear();
  472. for( unsigned int i = 0; i < out_segs.size(); i++ ) {
  473. idVec4 v;
  474. world_to_hclip( viewDef, out_segs[i], v );
  475. if( v.w <= 0.0f ) {
  476. return lightDef->viewLight->scissorRect;
  477. }
  478. idVec3 rv(v.x, v.y, v.z);
  479. rv /= v.w;
  480. outbounds.AddPoint( rv );
  481. }
  482. // limit the bounds to avoid an inside out scissor rectangle due to floating point to short conversion
  483. if ( outbounds[0].x < -1.0f ) {
  484. outbounds[0].x = -1.0f;
  485. }
  486. if ( outbounds[1].x > 1.0f ) {
  487. outbounds[1].x = 1.0f;
  488. }
  489. if ( outbounds[0].y < -1.0f ) {
  490. outbounds[0].y = -1.0f;
  491. }
  492. if ( outbounds[1].y > 1.0f ) {
  493. outbounds[1].y = 1.0f;
  494. }
  495. float w2 = ( viewDef->viewport.x2 - viewDef->viewport.x1 + 1 ) / 2.0f;
  496. float x = viewDef->viewport.x1;
  497. float h2 = ( viewDef->viewport.y2 - viewDef->viewport.y1 + 1 ) / 2.0f;
  498. float y = viewDef->viewport.y1;
  499. idScreenRect rect;
  500. rect.x1 = outbounds[0].x * w2 + w2 + x;
  501. rect.x2 = outbounds[1].x * w2 + w2 + x;
  502. rect.y1 = outbounds[0].y * h2 + h2 + y;
  503. rect.y2 = outbounds[1].y * h2 + h2 + y;
  504. rect.Expand();
  505. rect.Intersect( lightDef->viewLight->scissorRect );
  506. // debug //
  507. if ( r_useInteractionScissors.GetInteger() == -2 && !rect.IsEmpty() ) {
  508. viewDef->renderWorld->DebugScreenRect( colorYellow, rect, viewDef );
  509. }
  510. return rect;
  511. }