RenderWorld_portals.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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. All that is done in these functions is the creation of viewLights
  25. and viewEntitys for the lightDefs and entityDefs that are visible
  26. in the portal areas that can be seen from the current viewpoint.
  27. */
  28. // if we hit this many planes, we will just stop cropping the
  29. // view down, which is still correct, just conservative
  30. const int MAX_PORTAL_PLANES = 20;
  31. typedef struct portalStack_s {
  32. portal_t *p;
  33. const struct portalStack_s *next;
  34. idScreenRect rect;
  35. int numPortalPlanes;
  36. idPlane portalPlanes[MAX_PORTAL_PLANES+1];
  37. // positive side is outside the visible frustum
  38. } portalStack_t;
  39. //====================================================================
  40. /*
  41. ===================
  42. idRenderWorldLocal::ScreenRectForWinding
  43. ===================
  44. */
  45. idScreenRect idRenderWorldLocal::ScreenRectFromWinding( const idWinding *w, viewEntity_t *space ) {
  46. idScreenRect r;
  47. int i;
  48. idVec3 v;
  49. idVec3 ndc;
  50. float windowX, windowY;
  51. r.Clear();
  52. for ( i = 0 ; i < w->GetNumPoints() ; i++ ) {
  53. R_LocalPointToGlobal( space->modelMatrix, (*w)[i].ToVec3(), v );
  54. R_GlobalToNormalizedDeviceCoordinates( v, ndc );
  55. windowX = 0.5f * ( 1.0f + ndc[0] ) * ( tr.viewDef->viewport.x2 - tr.viewDef->viewport.x1 );
  56. windowY = 0.5f * ( 1.0f + ndc[1] ) * ( tr.viewDef->viewport.y2 - tr.viewDef->viewport.y1 );
  57. r.AddPoint( windowX, windowY );
  58. }
  59. r.Expand();
  60. return r;
  61. }
  62. /*
  63. ===================
  64. PortalIsFoggedOut
  65. ===================
  66. */
  67. bool idRenderWorldLocal::PortalIsFoggedOut( const portal_t *p ) {
  68. idRenderLightLocal *ldef;
  69. const idWinding *w;
  70. int i;
  71. idPlane forward;
  72. ldef = p->doublePortal->fogLight;
  73. if ( !ldef ) {
  74. return false;
  75. }
  76. // find the current density of the fog
  77. const idMaterial *lightShader = ldef->lightShader;
  78. int size = sizeof( float ) *lightShader->GetNumRegisters();
  79. float *regs =(float *)_alloca( size );
  80. lightShader->EvaluateRegisters( regs, ldef->parms.shaderParms, tr.viewDef, ldef->parms.referenceSound );
  81. const shaderStage_t *stage = lightShader->GetStage(0);
  82. float alpha = regs[ stage->color.registers[3] ];
  83. // if they left the default value on, set a fog distance of 500
  84. float a;
  85. if ( alpha <= 1.0f ) {
  86. a = -0.5f / DEFAULT_FOG_DISTANCE;
  87. } else {
  88. // otherwise, distance = alpha color
  89. a = -0.5f / alpha;
  90. }
  91. forward[0] = a * tr.viewDef->worldSpace.modelViewMatrix[2];
  92. forward[1] = a * tr.viewDef->worldSpace.modelViewMatrix[6];
  93. forward[2] = a * tr.viewDef->worldSpace.modelViewMatrix[10];
  94. forward[3] = a * tr.viewDef->worldSpace.modelViewMatrix[14];
  95. w = p->w;
  96. for ( i = 0 ; i < w->GetNumPoints() ; i++ ) {
  97. float d;
  98. d = forward.Distance( (*w)[i].ToVec3() );
  99. if ( d < 0.5f ) {
  100. return false; // a point not clipped off
  101. }
  102. }
  103. return true;
  104. }
  105. /*
  106. ===================
  107. FloodViewThroughArea_r
  108. ===================
  109. */
  110. void idRenderWorldLocal::FloodViewThroughArea_r( const idVec3 origin, int areaNum,
  111. const struct portalStack_s *ps ) {
  112. portal_t* p;
  113. float d;
  114. portalArea_t * area;
  115. const portalStack_t *check;
  116. portalStack_t newStack;
  117. int i, j;
  118. idVec3 v1, v2;
  119. int addPlanes;
  120. idFixedWinding w; // we won't overflow because MAX_PORTAL_PLANES = 20
  121. area = &portalAreas[ areaNum ];
  122. // cull models and lights to the current collection of planes
  123. AddAreaRefs( areaNum, ps );
  124. if ( areaScreenRect[areaNum].IsEmpty() ) {
  125. areaScreenRect[areaNum] = ps->rect;
  126. } else {
  127. areaScreenRect[areaNum].Union( ps->rect );
  128. }
  129. // go through all the portals
  130. for ( p = area->portals; p; p = p->next ) {
  131. // an enclosing door may have sealed the portal off
  132. if ( p->doublePortal->blockingBits & PS_BLOCK_VIEW ) {
  133. continue;
  134. }
  135. // make sure this portal is facing away from the view
  136. d = p->plane.Distance( origin );
  137. if ( d < -0.1f ) {
  138. continue;
  139. }
  140. // make sure the portal isn't in our stack trace,
  141. // which would cause an infinite loop
  142. for ( check = ps; check; check = check->next ) {
  143. if ( check->p == p ) {
  144. break; // don't recursively enter a stack
  145. }
  146. }
  147. if ( check ) {
  148. continue; // already in stack
  149. }
  150. // if we are very close to the portal surface, don't bother clipping
  151. // it, which tends to give epsilon problems that make the area vanish
  152. if ( d < 1.0f ) {
  153. // go through this portal
  154. newStack = *ps;
  155. newStack.p = p;
  156. newStack.next = ps;
  157. FloodViewThroughArea_r( origin, p->intoArea, &newStack );
  158. continue;
  159. }
  160. // clip the portal winding to all of the planes
  161. w = *p->w;
  162. for ( j = 0; j < ps->numPortalPlanes; j++ ) {
  163. if ( !w.ClipInPlace( -ps->portalPlanes[j], 0 ) ) {
  164. break;
  165. }
  166. }
  167. if ( !w.GetNumPoints() ) {
  168. continue; // portal not visible
  169. }
  170. // see if it is fogged out
  171. if ( PortalIsFoggedOut( p ) ) {
  172. continue;
  173. }
  174. // go through this portal
  175. newStack.p = p;
  176. newStack.next = ps;
  177. // find the screen pixel bounding box of the remaining portal
  178. // so we can scissor things outside it
  179. newStack.rect = ScreenRectFromWinding( &w, &tr.identitySpace );
  180. // slop might have spread it a pixel outside, so trim it back
  181. newStack.rect.Intersect( ps->rect );
  182. // generate a set of clipping planes that will further restrict
  183. // the visible view beyond just the scissor rect
  184. addPlanes = w.GetNumPoints();
  185. if ( addPlanes > MAX_PORTAL_PLANES ) {
  186. addPlanes = MAX_PORTAL_PLANES;
  187. }
  188. newStack.numPortalPlanes = 0;
  189. for ( i = 0; i < addPlanes; i++ ) {
  190. j = i+1;
  191. if ( j == w.GetNumPoints() ) {
  192. j = 0;
  193. }
  194. v1 = origin - w[i].ToVec3();
  195. v2 = origin - w[j].ToVec3();
  196. newStack.portalPlanes[newStack.numPortalPlanes].Normal().Cross( v2, v1 );
  197. // if it is degenerate, skip the plane
  198. if ( newStack.portalPlanes[newStack.numPortalPlanes].Normalize() < 0.01f ) {
  199. continue;
  200. }
  201. newStack.portalPlanes[newStack.numPortalPlanes].FitThroughPoint( origin );
  202. newStack.numPortalPlanes++;
  203. }
  204. // the last stack plane is the portal plane
  205. newStack.portalPlanes[newStack.numPortalPlanes] = p->plane;
  206. newStack.numPortalPlanes++;
  207. FloodViewThroughArea_r( origin, p->intoArea, &newStack );
  208. }
  209. }
  210. /*
  211. =======================
  212. FlowViewThroughPortals
  213. Finds viewLights and viewEntities by flowing from an origin through the visible portals.
  214. origin point can see into. The planes array defines a volume (positive
  215. sides facing in) that should contain the origin, such as a view frustum or a point light box.
  216. Zero planes assumes an unbounded volume.
  217. =======================
  218. */
  219. void idRenderWorldLocal::FlowViewThroughPortals( const idVec3 origin, int numPlanes, const idPlane *planes ) {
  220. portalStack_t ps;
  221. int i;
  222. ps.next = NULL;
  223. ps.p = NULL;
  224. for ( i = 0 ; i < numPlanes ; i++ ) {
  225. ps.portalPlanes[i] = planes[i];
  226. }
  227. ps.numPortalPlanes = numPlanes;
  228. ps.rect = tr.viewDef->scissor;
  229. if ( tr.viewDef->areaNum < 0 ){
  230. for ( i = 0; i < numPortalAreas; i++ ) {
  231. areaScreenRect[i] = tr.viewDef->scissor;
  232. }
  233. // if outside the world, mark everything
  234. for ( i = 0 ; i < numPortalAreas ; i++ ) {
  235. AddAreaRefs( i, &ps );
  236. }
  237. } else {
  238. for ( i = 0; i < numPortalAreas; i++ ) {
  239. areaScreenRect[i].Clear();
  240. }
  241. // flood out through portals, setting area viewCount
  242. FloodViewThroughArea_r( origin, tr.viewDef->areaNum, &ps );
  243. }
  244. }
  245. //==================================================================================================
  246. /*
  247. ===================
  248. FloodLightThroughArea_r
  249. ===================
  250. */
  251. void idRenderWorldLocal::FloodLightThroughArea_r( idRenderLightLocal *light, int areaNum,
  252. const struct portalStack_s *ps ) {
  253. portal_t* p;
  254. float d;
  255. portalArea_t * area;
  256. const portalStack_t *check, *firstPortalStack;
  257. portalStack_t newStack;
  258. int i, j;
  259. idVec3 v1, v2;
  260. int addPlanes;
  261. idFixedWinding w; // we won't overflow because MAX_PORTAL_PLANES = 20
  262. area = &portalAreas[ areaNum ];
  263. // add an areaRef
  264. AddLightRefToArea( light, area );
  265. // go through all the portals
  266. for ( p = area->portals; p; p = p->next ) {
  267. // make sure this portal is facing away from the view
  268. d = p->plane.Distance( light->globalLightOrigin );
  269. if ( d < -0.1f ) {
  270. continue;
  271. }
  272. // make sure the portal isn't in our stack trace,
  273. // which would cause an infinite loop
  274. for ( check = ps; check; check = check->next ) {
  275. firstPortalStack = check;
  276. if ( check->p == p ) {
  277. break; // don't recursively enter a stack
  278. }
  279. }
  280. if ( check ) {
  281. continue; // already in stack
  282. }
  283. // if we are very close to the portal surface, don't bother clipping
  284. // it, which tends to give epsilon problems that make the area vanish
  285. if ( d < 1.0f ) {
  286. // go through this portal
  287. newStack = *ps;
  288. newStack.p = p;
  289. newStack.next = ps;
  290. FloodLightThroughArea_r( light, p->intoArea, &newStack );
  291. continue;
  292. }
  293. // clip the portal winding to all of the planes
  294. w = *p->w;
  295. for ( j = 0; j < ps->numPortalPlanes; j++ ) {
  296. if ( !w.ClipInPlace( -ps->portalPlanes[j], 0 ) ) {
  297. break;
  298. }
  299. }
  300. if ( !w.GetNumPoints() ) {
  301. continue; // portal not visible
  302. }
  303. // also always clip to the original light planes, because they aren't
  304. // necessarily extending to infinitiy like a view frustum
  305. for ( j = 0; j < firstPortalStack->numPortalPlanes; j++ ) {
  306. if ( !w.ClipInPlace( -firstPortalStack->portalPlanes[j], 0 ) ) {
  307. break;
  308. }
  309. }
  310. if ( !w.GetNumPoints() ) {
  311. continue; // portal not visible
  312. }
  313. // go through this portal
  314. newStack.p = p;
  315. newStack.next = ps;
  316. // generate a set of clipping planes that will further restrict
  317. // the visible view beyond just the scissor rect
  318. addPlanes = w.GetNumPoints();
  319. if ( addPlanes > MAX_PORTAL_PLANES ) {
  320. addPlanes = MAX_PORTAL_PLANES;
  321. }
  322. newStack.numPortalPlanes = 0;
  323. for ( i = 0; i < addPlanes; i++ ) {
  324. j = i+1;
  325. if ( j == w.GetNumPoints() ) {
  326. j = 0;
  327. }
  328. v1 = light->globalLightOrigin - w[i].ToVec3();
  329. v2 = light->globalLightOrigin - w[j].ToVec3();
  330. newStack.portalPlanes[newStack.numPortalPlanes].Normal().Cross( v2, v1 );
  331. // if it is degenerate, skip the plane
  332. if ( newStack.portalPlanes[newStack.numPortalPlanes].Normalize() < 0.01f ) {
  333. continue;
  334. }
  335. newStack.portalPlanes[newStack.numPortalPlanes].FitThroughPoint( light->globalLightOrigin );
  336. newStack.numPortalPlanes++;
  337. }
  338. FloodLightThroughArea_r( light, p->intoArea, &newStack );
  339. }
  340. }
  341. /*
  342. =======================
  343. FlowLightThroughPortals
  344. Adds an arearef in each area that the light center flows into.
  345. This can only be used for shadow casting lights that have a generated
  346. prelight, because shadows are cast from back side which may not be in visible areas.
  347. =======================
  348. */
  349. void idRenderWorldLocal::FlowLightThroughPortals( idRenderLightLocal *light ) {
  350. portalStack_t ps;
  351. int i;
  352. const idVec3 origin = light->globalLightOrigin;
  353. // if the light origin areaNum is not in a valid area,
  354. // the light won't have any area refs
  355. if ( light->areaNum == -1 ) {
  356. return;
  357. }
  358. memset( &ps, 0, sizeof( ps ) );
  359. ps.numPortalPlanes = 6;
  360. for ( i = 0 ; i < 6 ; i++ ) {
  361. ps.portalPlanes[i] = light->frustum[i];
  362. }
  363. FloodLightThroughArea_r( light, light->areaNum, &ps );
  364. }
  365. //======================================================================================================
  366. /*
  367. ===================
  368. idRenderWorldLocal::FloodFrustumAreas_r
  369. ===================
  370. */
  371. areaNumRef_t *idRenderWorldLocal::FloodFrustumAreas_r( const idFrustum &frustum, const int areaNum, const idBounds &bounds, areaNumRef_t *areas ) {
  372. portal_t *p;
  373. portalArea_t *portalArea;
  374. idBounds newBounds;
  375. areaNumRef_t *a;
  376. portalArea = &portalAreas[ areaNum ];
  377. // go through all the portals
  378. for ( p = portalArea->portals; p; p = p->next ) {
  379. // check if we already visited the area the portal leads to
  380. for ( a = areas; a; a = a->next ) {
  381. if ( a->areaNum == p->intoArea ) {
  382. break;
  383. }
  384. }
  385. if ( a ) {
  386. continue;
  387. }
  388. // the frustum origin must be at the front of the portal plane
  389. if ( p->plane.Side( frustum.GetOrigin(), 0.1f ) == SIDE_BACK ) {
  390. continue;
  391. }
  392. // the frustum must cross the portal plane
  393. if ( frustum.PlaneSide( p->plane, 0.0f ) != PLANESIDE_CROSS ) {
  394. continue;
  395. }
  396. // get the bounds for the portal winding projected in the frustum
  397. frustum.ProjectionBounds( *p->w, newBounds );
  398. newBounds.IntersectSelf( bounds );
  399. if ( newBounds[0][0] > newBounds[1][0] || newBounds[0][1] > newBounds[1][1] || newBounds[0][2] > newBounds[1][2] ) {
  400. continue;
  401. }
  402. newBounds[1][0] = frustum.GetFarDistance();
  403. a = areaNumRefAllocator.Alloc();
  404. a->areaNum = p->intoArea;
  405. a->next = areas;
  406. areas = a;
  407. areas = FloodFrustumAreas_r( frustum, p->intoArea, newBounds, areas );
  408. }
  409. return areas;
  410. }
  411. /*
  412. ===================
  413. idRenderWorldLocal::FloodFrustumAreas
  414. Retrieves all the portal areas the frustum floods into where the frustum starts in the given areas.
  415. All portals are assumed to be open.
  416. ===================
  417. */
  418. areaNumRef_t *idRenderWorldLocal::FloodFrustumAreas( const idFrustum &frustum, areaNumRef_t *areas ) {
  419. idBounds bounds;
  420. areaNumRef_t *a;
  421. // bounds that cover the whole frustum
  422. bounds[0].Set( frustum.GetNearDistance(), -1.0f, -1.0f );
  423. bounds[1].Set( frustum.GetFarDistance(), 1.0f, 1.0f );
  424. for ( a = areas; a; a = a->next ) {
  425. areas = FloodFrustumAreas_r( frustum, a->areaNum, bounds, areas );
  426. }
  427. return areas;
  428. }
  429. /*
  430. =======================================================================
  431. R_FindViewLightsAndEntities
  432. =======================================================================
  433. */
  434. /*
  435. ================
  436. CullEntityByPortals
  437. Return true if the entity reference bounds do not intersect the current portal chain.
  438. ================
  439. */
  440. bool idRenderWorldLocal::CullEntityByPortals( const idRenderEntityLocal *entity, const portalStack_t *ps ) {
  441. if ( !r_useEntityCulling.GetBool() ) {
  442. return false;
  443. }
  444. // try to cull the entire thing using the reference bounds.
  445. // we do not yet do callbacks or dynamic model creation,
  446. // because we want to do all touching of the model after
  447. // we have determined all the lights that may effect it,
  448. // which optimizes cache usage
  449. if ( R_CullLocalBox( entity->referenceBounds, entity->modelMatrix,
  450. ps->numPortalPlanes, ps->portalPlanes ) ) {
  451. return true;
  452. }
  453. return false;
  454. }
  455. /*
  456. ===================
  457. AddAreaEntityRefs
  458. Any models that are visible through the current portalStack will
  459. have their scissor
  460. ===================
  461. */
  462. void idRenderWorldLocal::AddAreaEntityRefs( int areaNum, const portalStack_t *ps ) {
  463. areaReference_t *ref;
  464. idRenderEntityLocal *entity;
  465. portalArea_t *area;
  466. viewEntity_t *vEnt;
  467. idBounds b;
  468. area = &portalAreas[ areaNum ];
  469. for ( ref = area->entityRefs.areaNext ; ref != &area->entityRefs ; ref = ref->areaNext ) {
  470. entity = ref->entity;
  471. // debug tool to allow viewing of only one entity at a time
  472. if ( r_singleEntity.GetInteger() >= 0 && r_singleEntity.GetInteger() != entity->index ) {
  473. continue;
  474. }
  475. // remove decals that are completely faded away
  476. R_FreeEntityDefFadedDecals( entity, tr.viewDef->renderView.time );
  477. // check for completely suppressing the model
  478. if ( !r_skipSuppress.GetBool() ) {
  479. if ( entity->parms.suppressSurfaceInViewID
  480. && entity->parms.suppressSurfaceInViewID == tr.viewDef->renderView.viewID ) {
  481. continue;
  482. }
  483. if ( entity->parms.allowSurfaceInViewID
  484. && entity->parms.allowSurfaceInViewID != tr.viewDef->renderView.viewID ) {
  485. continue;
  486. }
  487. }
  488. // cull reference bounds
  489. if ( CullEntityByPortals( entity, ps ) ) {
  490. // we are culled out through this portal chain, but it might
  491. // still be visible through others
  492. continue;
  493. }
  494. vEnt = R_SetEntityDefViewEntity( entity );
  495. // possibly expand the scissor rect
  496. vEnt->scissorRect.Union( ps->rect );
  497. }
  498. }
  499. /*
  500. ================
  501. CullLightByPortals
  502. Return true if the light frustum does not intersect the current portal chain.
  503. The last stack plane is not used because lights are not near clipped.
  504. ================
  505. */
  506. bool idRenderWorldLocal::CullLightByPortals( const idRenderLightLocal *light, const portalStack_t *ps ) {
  507. int i, j;
  508. const srfTriangles_t *tri;
  509. float d;
  510. idFixedWinding w; // we won't overflow because MAX_PORTAL_PLANES = 20
  511. if ( r_useLightCulling.GetInteger() == 0 ) {
  512. return false;
  513. }
  514. if ( r_useLightCulling.GetInteger() >= 2 ) {
  515. // exact clip of light faces against all planes
  516. for ( i = 0; i < 6; i++ ) {
  517. // the light frustum planes face out from the light,
  518. // so the planes that have the view origin on the negative
  519. // side will be the "back" faces of the light, which must have
  520. // some fragment inside the portalStack to be visible
  521. if ( light->frustum[i].Distance( tr.viewDef->renderView.vieworg ) >= 0 ) {
  522. continue;
  523. }
  524. // get the exact winding for this side
  525. const idWinding *ow = light->frustumWindings[i];
  526. // projected lights may have one of the frustums degenerated
  527. if ( !ow ) {
  528. continue;
  529. }
  530. w = *ow;
  531. // now check the winding against each of the portalStack planes
  532. for ( j = 0; j < ps->numPortalPlanes - 1; j++ ) {
  533. if ( !w.ClipInPlace( -ps->portalPlanes[j] ) ) {
  534. break;
  535. }
  536. }
  537. if ( w.GetNumPoints() ) {
  538. // part of the winding is visible through the portalStack,
  539. // so the light is not culled
  540. return false;
  541. }
  542. }
  543. // none of the light surfaces were visible
  544. return true;
  545. } else {
  546. // simple point check against each plane
  547. tri = light->frustumTris;
  548. // check against frustum planes
  549. for ( i = 0; i < ps->numPortalPlanes - 1; i++ ) {
  550. for ( j = 0; j < tri->numVerts; j++ ) {
  551. d = ps->portalPlanes[i].Distance( tri->verts[j].xyz );
  552. if ( d < 0.0f ) {
  553. break; // point is inside this plane
  554. }
  555. }
  556. if ( j == tri->numVerts ) {
  557. // all points were outside one of the planes
  558. tr.pc.c_box_cull_out++;
  559. return true;
  560. }
  561. }
  562. }
  563. return false;
  564. }
  565. /*
  566. ===================
  567. AddAreaLightRefs
  568. This is the only point where lights get added to the viewLights list
  569. ===================
  570. */
  571. void idRenderWorldLocal::AddAreaLightRefs( int areaNum, const portalStack_t *ps ) {
  572. areaReference_t *lref;
  573. portalArea_t *area;
  574. idRenderLightLocal *light;
  575. viewLight_t *vLight;
  576. area = &portalAreas[ areaNum ];
  577. for ( lref = area->lightRefs.areaNext ; lref != &area->lightRefs ; lref = lref->areaNext ) {
  578. light = lref->light;
  579. // debug tool to allow viewing of only one light at a time
  580. if ( r_singleLight.GetInteger() >= 0 && r_singleLight.GetInteger() != light->index ) {
  581. continue;
  582. }
  583. // check for being closed off behind a door
  584. // a light that doesn't cast shadows will still light even if it is behind a door
  585. if ( r_useLightCulling.GetInteger() >= 3 &&
  586. !light->parms.noShadows && light->lightShader->LightCastsShadows()
  587. && light->areaNum != -1 && !tr.viewDef->connectedAreas[ light->areaNum ] ) {
  588. continue;
  589. }
  590. // cull frustum
  591. if ( CullLightByPortals( light, ps ) ) {
  592. // we are culled out through this portal chain, but it might
  593. // still be visible through others
  594. continue;
  595. }
  596. vLight = R_SetLightDefViewLight( light );
  597. // expand the scissor rect
  598. vLight->scissorRect.Union( ps->rect );
  599. }
  600. }
  601. /*
  602. ===================
  603. AddAreaRefs
  604. This may be entered multiple times with different planes
  605. if more than one portal sees into the area
  606. ===================
  607. */
  608. void idRenderWorldLocal::AddAreaRefs( int areaNum, const portalStack_t *ps ) {
  609. // mark the viewCount, so r_showPortals can display the
  610. // considered portals
  611. portalAreas[ areaNum ].viewCount = tr.viewCount;
  612. // add the models and lights, using more precise culling to the planes
  613. AddAreaEntityRefs( areaNum, ps );
  614. AddAreaLightRefs( areaNum, ps );
  615. }
  616. /*
  617. ===================
  618. BuildConnectedAreas_r
  619. ===================
  620. */
  621. void idRenderWorldLocal::BuildConnectedAreas_r( int areaNum ) {
  622. portalArea_t *area;
  623. portal_t *portal;
  624. if ( tr.viewDef->connectedAreas[areaNum] ) {
  625. return;
  626. }
  627. tr.viewDef->connectedAreas[areaNum] = true;
  628. // flood through all non-blocked portals
  629. area = &portalAreas[ areaNum ];
  630. for ( portal = area->portals ; portal ; portal = portal->next ) {
  631. if ( !(portal->doublePortal->blockingBits & PS_BLOCK_VIEW) ) {
  632. BuildConnectedAreas_r( portal->intoArea );
  633. }
  634. }
  635. }
  636. /*
  637. ===================
  638. BuildConnectedAreas
  639. This is only valid for a given view, not all views in a frame
  640. ===================
  641. */
  642. void idRenderWorldLocal::BuildConnectedAreas( void ) {
  643. int i;
  644. tr.viewDef->connectedAreas = (bool *)R_FrameAlloc( numPortalAreas
  645. * sizeof( tr.viewDef->connectedAreas[0] ) );
  646. // if we are outside the world, we can see all areas
  647. if ( tr.viewDef->areaNum == -1 ) {
  648. for ( i = 0 ; i < numPortalAreas ; i++ ) {
  649. tr.viewDef->connectedAreas[i] = true;
  650. }
  651. return;
  652. }
  653. // start with none visible, and flood fill from the current area
  654. memset( tr.viewDef->connectedAreas, 0, numPortalAreas * sizeof( tr.viewDef->connectedAreas[0] ) );
  655. BuildConnectedAreas_r( tr.viewDef->areaNum );
  656. }
  657. /*
  658. =============
  659. FindViewLightsAndEntites
  660. All the modelrefs and lightrefs that are in visible areas
  661. will have viewEntitys and viewLights created for them.
  662. The scissorRects on the viewEntitys and viewLights may be empty if
  663. they were considered, but not actually visible.
  664. =============
  665. */
  666. void idRenderWorldLocal::FindViewLightsAndEntities( void ) {
  667. // clear the visible lightDef and entityDef lists
  668. tr.viewDef->viewLights = NULL;
  669. tr.viewDef->viewEntitys = NULL;
  670. // find the area to start the portal flooding in
  671. if ( !r_usePortals.GetBool() ) {
  672. // debug tool to force no portal culling
  673. tr.viewDef->areaNum = -1;
  674. } else {
  675. tr.viewDef->areaNum = PointInArea( tr.viewDef->initialViewAreaOrigin );
  676. }
  677. // determine all possible connected areas for
  678. // light-behind-door culling
  679. BuildConnectedAreas();
  680. // bump the view count, invalidating all
  681. // visible areas
  682. tr.viewCount++;
  683. // flow through all the portals and add models / lights
  684. if ( r_singleArea.GetBool() ) {
  685. // if debugging, only mark this area
  686. // if we are outside the world, don't draw anything
  687. if ( tr.viewDef->areaNum >= 0 ) {
  688. portalStack_t ps;
  689. int i;
  690. static int lastPrintedAreaNum;
  691. if ( tr.viewDef->areaNum != lastPrintedAreaNum ) {
  692. lastPrintedAreaNum = tr.viewDef->areaNum;
  693. common->Printf( "entering portal area %i\n", tr.viewDef->areaNum );
  694. }
  695. for ( i = 0 ; i < 5 ; i++ ) {
  696. ps.portalPlanes[i] = tr.viewDef->frustum[i];
  697. }
  698. ps.numPortalPlanes = 5;
  699. ps.rect = tr.viewDef->scissor;
  700. AddAreaRefs( tr.viewDef->areaNum, &ps );
  701. }
  702. } else {
  703. // note that the center of projection for flowing through portals may
  704. // be a different point than initialViewAreaOrigin for subviews that
  705. // may have the viewOrigin in a solid/invalid area
  706. FlowViewThroughPortals( tr.viewDef->renderView.vieworg, 5, tr.viewDef->frustum );
  707. }
  708. }
  709. /*
  710. ==============
  711. NumPortals
  712. ==============
  713. */
  714. int idRenderWorldLocal::NumPortals( void ) const {
  715. return numInterAreaPortals;
  716. }
  717. /*
  718. ==============
  719. FindPortal
  720. Game code uses this to identify which portals are inside doors.
  721. Returns 0 if no portal contacts the bounds
  722. ==============
  723. */
  724. qhandle_t idRenderWorldLocal::FindPortal( const idBounds &b ) const {
  725. int i, j;
  726. idBounds wb;
  727. doublePortal_t *portal;
  728. idWinding *w;
  729. for ( i = 0 ; i < numInterAreaPortals ; i++ ) {
  730. portal = &doublePortals[i];
  731. w = portal->portals[0]->w;
  732. wb.Clear();
  733. for ( j = 0 ; j < w->GetNumPoints() ; j++ ) {
  734. wb.AddPoint( (*w)[j].ToVec3() );
  735. }
  736. if ( wb.IntersectsBounds( b ) ) {
  737. return i + 1;
  738. }
  739. }
  740. return 0;
  741. }
  742. /*
  743. =============
  744. FloodConnectedAreas
  745. =============
  746. */
  747. void idRenderWorldLocal::FloodConnectedAreas( portalArea_t *area, int portalAttributeIndex ) {
  748. if ( area->connectedAreaNum[portalAttributeIndex] == connectedAreaNum ) {
  749. return;
  750. }
  751. area->connectedAreaNum[portalAttributeIndex] = connectedAreaNum;
  752. for ( portal_t *p = area->portals ; p ; p = p->next ) {
  753. if ( !(p->doublePortal->blockingBits & (1<<portalAttributeIndex) ) ) {
  754. FloodConnectedAreas( &portalAreas[p->intoArea], portalAttributeIndex );
  755. }
  756. }
  757. }
  758. /*
  759. ==============
  760. AreasAreConnected
  761. ==============
  762. */
  763. bool idRenderWorldLocal::AreasAreConnected( int areaNum1, int areaNum2, portalConnection_t connection ) {
  764. if ( areaNum1 == -1 || areaNum2 == -1 ) {
  765. return false;
  766. }
  767. if ( areaNum1 > numPortalAreas || areaNum2 > numPortalAreas || areaNum1 < 0 || areaNum2 < 0 ) {
  768. common->Error( "idRenderWorldLocal::AreAreasConnected: bad parms: %i, %i", areaNum1, areaNum2 );
  769. }
  770. int attribute = 0;
  771. int intConnection = (int)connection;
  772. while ( intConnection > 1 ) {
  773. attribute++;
  774. intConnection >>= 1;
  775. }
  776. if ( attribute >= NUM_PORTAL_ATTRIBUTES || ( 1 << attribute ) != (int)connection ) {
  777. common->Error( "idRenderWorldLocal::AreasAreConnected: bad connection number: %i\n", (int)connection );
  778. }
  779. return portalAreas[areaNum1].connectedAreaNum[attribute] == portalAreas[areaNum2].connectedAreaNum[attribute];
  780. }
  781. /*
  782. ==============
  783. SetPortalState
  784. doors explicitly close off portals when shut
  785. ==============
  786. */
  787. void idRenderWorldLocal::SetPortalState( qhandle_t portal, int blockTypes ) {
  788. if ( portal == 0 ) {
  789. return;
  790. }
  791. if ( portal < 1 || portal > numInterAreaPortals ) {
  792. common->Error( "SetPortalState: bad portal number %i", portal );
  793. }
  794. int old = doublePortals[portal-1].blockingBits;
  795. if ( old == blockTypes ) {
  796. return;
  797. }
  798. doublePortals[portal-1].blockingBits = blockTypes;
  799. // leave the connectedAreaGroup the same on one side,
  800. // then flood fill from the other side with a new number for each changed attribute
  801. for ( int i = 0 ; i < NUM_PORTAL_ATTRIBUTES ; i++ ) {
  802. if ( ( old ^ blockTypes ) & ( 1 << i ) ) {
  803. connectedAreaNum++;
  804. FloodConnectedAreas( &portalAreas[doublePortals[portal-1].portals[1]->intoArea], i );
  805. }
  806. }
  807. if ( session->writeDemo ) {
  808. session->writeDemo->WriteInt( DS_RENDER );
  809. session->writeDemo->WriteInt( DC_SET_PORTAL_STATE );
  810. session->writeDemo->WriteInt( portal );
  811. session->writeDemo->WriteInt( blockTypes );
  812. }
  813. }
  814. /*
  815. ==============
  816. GetPortalState
  817. ==============
  818. */
  819. int idRenderWorldLocal::GetPortalState( qhandle_t portal ) {
  820. if ( portal == 0 ) {
  821. return 0;
  822. }
  823. if ( portal < 1 || portal > numInterAreaPortals ) {
  824. common->Error( "GetPortalState: bad portal number %i", portal );
  825. }
  826. return doublePortals[portal-1].blockingBits;
  827. }
  828. /*
  829. =====================
  830. idRenderWorldLocal::ShowPortals
  831. Debugging tool, won't work correctly with SMP or when mirrors are present
  832. =====================
  833. */
  834. void idRenderWorldLocal::ShowPortals() {
  835. int i, j;
  836. portalArea_t *area;
  837. portal_t *p;
  838. idWinding *w;
  839. // flood out through portals, setting area viewCount
  840. for ( i = 0 ; i < numPortalAreas ; i++ ) {
  841. area = &portalAreas[i];
  842. if ( area->viewCount != tr.viewCount ) {
  843. continue;
  844. }
  845. for ( p = area->portals ; p ; p = p->next ) {
  846. w = p->w;
  847. if ( !w ) {
  848. continue;
  849. }
  850. if ( portalAreas[ p->intoArea ].viewCount != tr.viewCount ) {
  851. // red = can't see
  852. qglColor3f( 1, 0, 0 );
  853. } else {
  854. // green = see through
  855. qglColor3f( 0, 1, 0 );
  856. }
  857. qglBegin( GL_LINE_LOOP );
  858. for ( j = 0 ; j < w->GetNumPoints() ; j++ ) {
  859. qglVertex3fv( (*w)[j].ToFloatPtr() );
  860. }
  861. qglEnd();
  862. }
  863. }
  864. }