flow.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. #pragma warning(disable:4018) //amckern - 64bit - '<' Singed/Unsigned Mismatch
  2. #include "vis.h"
  3. // =====================================================================================
  4. // CheckStack
  5. // =====================================================================================
  6. #ifdef USE_CHECK_STACK
  7. static void CheckStack(const leaf_t* const leaf, const threaddata_t* const thread)
  8. {
  9. pstack_t* p;
  10. for (p = thread->pstack_head.next; p; p = p->next)
  11. {
  12. if (p->leaf == leaf)
  13. Error("CheckStack: leaf recursion");
  14. }
  15. }
  16. #endif
  17. // =====================================================================================
  18. // AllocStackWinding
  19. // =====================================================================================
  20. inline static winding_t* AllocStackWinding(pstack_t* const stack)
  21. {
  22. int i;
  23. for (i = 0; i < 3; i++)
  24. {
  25. if (stack->freewindings[i])
  26. {
  27. stack->freewindings[i] = 0;
  28. return &stack->windings[i];
  29. }
  30. }
  31. Error("AllocStackWinding: failed");
  32. return NULL;
  33. }
  34. // =====================================================================================
  35. // FreeStackWinding
  36. // =====================================================================================
  37. inline static void FreeStackWinding(const winding_t* const w, pstack_t* const stack)
  38. {
  39. int i;
  40. i = w - stack->windings;
  41. if (i < 0 || i > 2)
  42. return; // not from local
  43. if (stack->freewindings[i])
  44. Error("FreeStackWinding: allready free");
  45. stack->freewindings[i] = 1;
  46. }
  47. // =====================================================================================
  48. // ChopWinding
  49. // =====================================================================================
  50. inline winding_t* ChopWinding(winding_t* const in, pstack_t* const stack, const plane_t* const split)
  51. {
  52. vec_t dists[128];
  53. int sides[128];
  54. int counts[3];
  55. vec_t dot;
  56. int i;
  57. vec3_t mid;
  58. winding_t* neww;
  59. counts[0] = counts[1] = counts[2] = 0;
  60. if (in->numpoints > (sizeof(sides) / sizeof(*sides)))
  61. {
  62. Error("Winding with too many sides!");
  63. }
  64. // determine sides for each point
  65. for (i = 0; i < in->numpoints; i++)
  66. {
  67. dot = DotProduct(in->points[i], split->normal);
  68. dot -= split->dist;
  69. dists[i] = dot;
  70. if (dot > ON_EPSILON)
  71. {
  72. sides[i] = SIDE_FRONT;
  73. }
  74. else if (dot < -ON_EPSILON)
  75. {
  76. sides[i] = SIDE_BACK;
  77. }
  78. else
  79. {
  80. sides[i] = SIDE_ON;
  81. }
  82. counts[sides[i]]++;
  83. }
  84. if (!counts[1])
  85. {
  86. return in; // completely on front side
  87. }
  88. if (!counts[0])
  89. {
  90. FreeStackWinding(in, stack);
  91. return NULL;
  92. }
  93. sides[i] = sides[0];
  94. dists[i] = dists[0];
  95. neww = AllocStackWinding(stack);
  96. neww->numpoints = 0;
  97. for (i = 0; i < in->numpoints; i++)
  98. {
  99. vec_t* p1 = in->points[i];
  100. if (neww->numpoints == MAX_POINTS_ON_FIXED_WINDING)
  101. {
  102. Warning("ChopWinding : rejected(1) due to too many points\n");
  103. FreeStackWinding(neww, stack);
  104. return in; // can't chop -- fall back to original
  105. }
  106. if (sides[i] == SIDE_ON)
  107. {
  108. VectorCopy(p1, neww->points[neww->numpoints]);
  109. neww->numpoints++;
  110. continue;
  111. }
  112. else if (sides[i] == SIDE_FRONT)
  113. {
  114. VectorCopy(p1, neww->points[neww->numpoints]);
  115. neww->numpoints++;
  116. }
  117. if ((sides[i + 1] == SIDE_ON) | (sides[i + 1] == sides[i])) // | instead of || for branch optimization
  118. {
  119. continue;
  120. }
  121. if (neww->numpoints == MAX_POINTS_ON_FIXED_WINDING)
  122. {
  123. Warning("ChopWinding : rejected(2) due to too many points\n");
  124. FreeStackWinding(neww, stack);
  125. return in; // can't chop -- fall back to original
  126. }
  127. // generate a split point
  128. {
  129. unsigned tmp = i + 1;
  130. if (tmp >= in->numpoints)
  131. {
  132. tmp = 0;
  133. }
  134. const vec_t* p2 = in->points[tmp];
  135. dot = dists[i] / (dists[i] - dists[i + 1]);
  136. const vec_t* normal = split->normal;
  137. const vec_t dist = split->dist;
  138. unsigned int j;
  139. for (j = 0; j < 3; j++)
  140. { // avoid round off error when possible
  141. if (normal[j] < (1.0 - NORMAL_EPSILON))
  142. {
  143. if (normal[j] > (-1.0 + NORMAL_EPSILON))
  144. {
  145. mid[j] = p1[j] + dot * (p2[j] - p1[j]);
  146. }
  147. else
  148. {
  149. mid[j] = -dist;
  150. }
  151. }
  152. else
  153. {
  154. mid[j] = dist;
  155. }
  156. }
  157. }
  158. VectorCopy(mid, neww->points[neww->numpoints]);
  159. neww->numpoints++;
  160. }
  161. // free the original winding
  162. FreeStackWinding(in, stack);
  163. return neww;
  164. }
  165. // =====================================================================================
  166. // AddPlane
  167. // =====================================================================================
  168. #ifdef RVIS_LEVEL_2
  169. inline static void AddPlane(pstack_t* const stack, const plane_t* const split)
  170. {
  171. int j;
  172. if (stack->clipPlaneCount)
  173. {
  174. for (j = 0; j < stack->clipPlaneCount; j++)
  175. {
  176. if (fabs((stack->clipPlane[j]).dist - split->dist) <= EQUAL_EPSILON &&
  177. VectorCompare((stack->clipPlane[j]).normal, split->normal))
  178. {
  179. return;
  180. }
  181. }
  182. }
  183. stack->clipPlane[stack->clipPlaneCount] = *split;
  184. stack->clipPlaneCount++;
  185. }
  186. #endif
  187. // =====================================================================================
  188. // ClipToSeperators
  189. // Source, pass, and target are an ordering of portals.
  190. // Generates seperating planes canidates by taking two points from source and one
  191. // point from pass, and clips target by them.
  192. // If the target argument is NULL, then a list of clipping planes is built in
  193. // stack instead.
  194. // If target is totally clipped away, that portal can not be seen through.
  195. // Normal clip keeps target on the same side as pass, which is correct if the
  196. // order goes source, pass, target. If the order goes pass, source, target then
  197. // flipclip should be set.
  198. // =====================================================================================
  199. inline static winding_t* ClipToSeperators(
  200. const winding_t* const source,
  201. const winding_t* const pass,
  202. winding_t* const a_target,
  203. const bool flipclip,
  204. pstack_t* const stack)
  205. {
  206. int i, j, k, l;
  207. plane_t plane;
  208. vec3_t v1, v2;
  209. float d;
  210. int counts[3];
  211. bool fliptest;
  212. winding_t* target = a_target;
  213. const unsigned int numpoints = source->numpoints;
  214. // check all combinations
  215. for (i=0, l=1; i < numpoints; i++, l++)
  216. {
  217. if (l == numpoints)
  218. {
  219. l = 0;
  220. }
  221. VectorSubtract(source->points[l], source->points[i], v1);
  222. // fing a vertex of pass that makes a plane that puts all of the
  223. // vertexes of pass on the front side and all of the vertexes of
  224. // source on the back side
  225. for (j = 0; j < pass->numpoints; j++)
  226. {
  227. VectorSubtract(pass->points[j], source->points[i], v2);
  228. CrossProduct(v1, v2, plane.normal);
  229. if (VectorNormalize(plane.normal) < ON_EPSILON)
  230. {
  231. continue;
  232. }
  233. plane.dist = DotProduct(pass->points[j], plane.normal);
  234. // find out which side of the generated seperating plane has the
  235. // source portal
  236. fliptest = false;
  237. for (k = 0; k < numpoints; k++)
  238. {
  239. if ((k == i) | (k == l)) // | instead of || for branch optimization
  240. {
  241. continue;
  242. }
  243. d = DotProduct(source->points[k], plane.normal) - plane.dist;
  244. if (d < -ON_EPSILON)
  245. { // source is on the negative side, so we want all
  246. // pass and target on the positive side
  247. fliptest = false;
  248. break;
  249. }
  250. else if (d > ON_EPSILON)
  251. { // source is on the positive side, so we want all
  252. // pass and target on the negative side
  253. fliptest = true;
  254. break;
  255. }
  256. }
  257. if (k == numpoints)
  258. {
  259. continue; // planar with source portal
  260. }
  261. // flip the normal if the source portal is backwards
  262. if (fliptest)
  263. {
  264. VectorSubtract(vec3_origin, plane.normal, plane.normal);
  265. plane.dist = -plane.dist;
  266. }
  267. // if all of the pass portal points are now on the positive side,
  268. // this is the seperating plane
  269. counts[0] = counts[1] = counts[2] = 0;
  270. for (k = 0; k < pass->numpoints; k++)
  271. {
  272. if (k == j)
  273. {
  274. continue;
  275. }
  276. d = DotProduct(pass->points[k], plane.normal) - plane.dist;
  277. if (d < -ON_EPSILON)
  278. {
  279. break;
  280. }
  281. else if (d > ON_EPSILON)
  282. {
  283. counts[0]++;
  284. }
  285. else
  286. {
  287. counts[2]++;
  288. }
  289. }
  290. if (k != pass->numpoints)
  291. {
  292. continue; // points on negative side, not a seperating plane
  293. }
  294. if (!counts[0])
  295. {
  296. continue; // planar with seperating plane
  297. }
  298. // flip the normal if we want the back side
  299. if (flipclip)
  300. {
  301. VectorSubtract(vec3_origin, plane.normal, plane.normal);
  302. plane.dist = -plane.dist;
  303. }
  304. if (target != NULL)
  305. {
  306. // clip target by the seperating plane
  307. target = ChopWinding(target, stack, &plane);
  308. if (!target)
  309. {
  310. return NULL; // target is not visible
  311. }
  312. }
  313. else
  314. {
  315. AddPlane(stack, &plane);
  316. }
  317. #ifdef RVIS_LEVEL_1
  318. break; /* Antony was here */
  319. #endif
  320. }
  321. }
  322. return target;
  323. }
  324. // =====================================================================================
  325. // RecursiveLeafFlow
  326. // Flood fill through the leafs
  327. // If src_portal is NULL, this is the originating leaf
  328. // =====================================================================================
  329. inline static void RecursiveLeafFlow(const int leafnum, const threaddata_t* const thread, const pstack_t* const prevstack)
  330. {
  331. pstack_t stack;
  332. leaf_t* leaf;
  333. leaf = &g_leafs[leafnum];
  334. #ifdef USE_CHECK_STACK
  335. CheckStack(leaf, thread);
  336. #endif
  337. {
  338. const unsigned offset = leafnum >> 3;
  339. const unsigned bit = (1 << (leafnum & 7));
  340. // mark the leaf as visible
  341. if (!(thread->leafvis[offset] & bit))
  342. {
  343. thread->leafvis[offset] |= bit;
  344. thread->base->numcansee++;
  345. }
  346. }
  347. #ifdef USE_CHECK_STACK
  348. prevstack->next = &stack;
  349. stack.next = NULL;
  350. #endif
  351. stack.head = prevstack->head;
  352. stack.leaf = leaf;
  353. stack.portal = NULL;
  354. #ifdef RVIS_LEVEL_2
  355. stack.clipPlaneCount = -1;
  356. stack.clipPlane = NULL;
  357. #endif
  358. // check all portals for flowing into other leafs
  359. unsigned i;
  360. portal_t** plist = leaf->portals;
  361. for (i = 0; i < leaf->numportals; i++, plist++)
  362. {
  363. portal_t* p = *plist;
  364. #if ZHLT_ZONES
  365. portal_t * head_p = stack.head->portal;
  366. if (g_Zones->check(head_p->zone, p->zone))
  367. {
  368. continue;
  369. }
  370. #endif
  371. {
  372. const unsigned offset = p->leaf >> 3;
  373. const unsigned bit = 1 << (p->leaf & 7);
  374. if (!(stack.head->mightsee[offset] & bit))
  375. {
  376. continue; // can't possibly see it
  377. }
  378. if (!(prevstack->mightsee[offset] & bit))
  379. {
  380. continue; // can't possibly see it
  381. }
  382. }
  383. // if the portal can't see anything we haven't allready seen, skip it
  384. {
  385. long* test;
  386. if (p->status == stat_done)
  387. {
  388. test = (long*)p->visbits;
  389. }
  390. else
  391. {
  392. test = (long*)p->mightsee;
  393. }
  394. {
  395. const int bitlongs = g_bitlongs;
  396. {
  397. long* prevmight = (long*)prevstack->mightsee;
  398. long* might = (long*)stack.mightsee;
  399. unsigned j;
  400. for (j = 0; j < bitlongs; j++, test++, might++, prevmight++)
  401. {
  402. (*might) = (*prevmight) & (*test);
  403. }
  404. }
  405. {
  406. long* might = (long*)stack.mightsee;
  407. long* vis = (long*)thread->leafvis;
  408. unsigned j;
  409. for (j = 0; j < bitlongs; j++, might++, vis++)
  410. {
  411. if ((*might) & ~(*vis))
  412. {
  413. break;
  414. }
  415. }
  416. if (j == g_bitlongs)
  417. { // can't see anything new
  418. continue;
  419. }
  420. }
  421. }
  422. }
  423. // get plane of portal, point normal into the neighbor leaf
  424. stack.portalplane = &p->plane;
  425. plane_t backplane;
  426. VectorSubtract(vec3_origin, p->plane.normal, backplane.normal);
  427. backplane.dist = -p->plane.dist;
  428. if (VectorCompare(prevstack->portalplane->normal, backplane.normal))
  429. {
  430. continue; // can't go out a coplanar face
  431. }
  432. stack.portal = p;
  433. #ifdef USE_CHECK_STACK
  434. stack.next = NULL;
  435. #endif
  436. stack.freewindings[0] = 1;
  437. stack.freewindings[1] = 1;
  438. stack.freewindings[2] = 1;
  439. stack.pass = ChopWinding(p->winding, &stack, thread->pstack_head.portalplane);
  440. if (!stack.pass)
  441. {
  442. continue;
  443. }
  444. stack.source = ChopWinding(prevstack->source, &stack, &backplane);
  445. if (!stack.source)
  446. {
  447. continue;
  448. }
  449. if (!prevstack->pass)
  450. { // the second leaf can only be blocked if coplanar
  451. RecursiveLeafFlow(p->leaf, thread, &stack);
  452. continue;
  453. }
  454. stack.pass = ChopWinding(stack.pass, &stack, prevstack->portalplane);
  455. if (!stack.pass)
  456. {
  457. continue;
  458. }
  459. #ifdef RVIS_LEVEL_2
  460. if (stack.clipPlaneCount == -1)
  461. {
  462. stack.clipPlaneCount = 0;
  463. stack.clipPlane = (plane_t*)alloca(sizeof(plane_t) * prevstack->source->numpoints * prevstack->pass->numpoints);
  464. ClipToSeperators(prevstack->source, prevstack->pass, NULL, false, &stack);
  465. ClipToSeperators(prevstack->pass, prevstack->source, NULL, true, &stack);
  466. }
  467. if (stack.clipPlaneCount > 0)
  468. {
  469. unsigned j;
  470. for (j = 0; j < stack.clipPlaneCount && stack.pass != NULL; j++)
  471. {
  472. stack.pass = ChopWinding(stack.pass, &stack, &(stack.clipPlane[j]));
  473. }
  474. if (stack.pass == NULL)
  475. continue;
  476. }
  477. #else
  478. stack.pass = ClipToSeperators(stack.source, prevstack->pass, stack.pass, false, &stack);
  479. if (!stack.pass)
  480. {
  481. continue;
  482. }
  483. stack.pass = ClipToSeperators(prevstack->pass, stack.source, stack.pass, true, &stack);
  484. if (!stack.pass)
  485. {
  486. continue;
  487. }
  488. #endif
  489. if (g_fullvis)
  490. {
  491. stack.source = ClipToSeperators(stack.pass, prevstack->pass, stack.source, false, &stack);
  492. if (!stack.source)
  493. {
  494. continue;
  495. }
  496. stack.source = ClipToSeperators(prevstack->pass, stack.pass, stack.source, true, &stack);
  497. if (!stack.source)
  498. {
  499. continue;
  500. }
  501. }
  502. // flow through it for real
  503. RecursiveLeafFlow(p->leaf, thread, &stack);
  504. }
  505. #ifdef RVIS_LEVEL_2
  506. #if 0
  507. if (stack.clipPlane != NULL)
  508. {
  509. free(stack.clipPlane);
  510. }
  511. #endif
  512. #endif
  513. }
  514. // =====================================================================================
  515. // PortalFlow
  516. // =====================================================================================
  517. void PortalFlow(portal_t* p)
  518. {
  519. threaddata_t data;
  520. unsigned i;
  521. if (p->status != stat_working)
  522. Error("PortalFlow: reflowed");
  523. p->visbits = (byte*)calloc(1, g_bitbytes);
  524. memset(&data, 0, sizeof(data));
  525. data.leafvis = p->visbits;
  526. data.base = p;
  527. data.pstack_head.head = &data.pstack_head;
  528. data.pstack_head.portal = p;
  529. data.pstack_head.source = p->winding;
  530. data.pstack_head.portalplane = &p->plane;
  531. for (i = 0; i < g_bitlongs; i++)
  532. {
  533. ((long*)data.pstack_head.mightsee)[i] = ((long*)p->mightsee)[i];
  534. }
  535. RecursiveLeafFlow(p->leaf, &data, &data.pstack_head);
  536. #ifdef ZHLT_NETVIS
  537. p->fromclient = g_clientid;
  538. #endif
  539. p->status = stat_done;
  540. #ifdef ZHLT_NETVIS
  541. Flag_VIS_DONE_PORTAL(g_visportalindex);
  542. #endif
  543. }
  544. // =====================================================================================
  545. // SimpleFlood
  546. // This is a rough first-order aproximation that is used to trivially reject some
  547. // of the final calculations.
  548. // =====================================================================================
  549. static void SimpleFlood(byte* const srcmightsee, const int leafnum, byte* const portalsee, unsigned int* const c_leafsee)
  550. {
  551. unsigned i;
  552. leaf_t* leaf;
  553. portal_t* p;
  554. {
  555. const unsigned offset = leafnum >> 3;
  556. const unsigned bit = (1 << (leafnum & 7));
  557. if (srcmightsee[offset] & bit)
  558. {
  559. return;
  560. }
  561. else
  562. {
  563. srcmightsee[offset] |= bit;
  564. }
  565. }
  566. (*c_leafsee)++;
  567. leaf = &g_leafs[leafnum];
  568. for (i = 0; i < leaf->numportals; i++)
  569. {
  570. p = leaf->portals[i];
  571. if (!portalsee[p - g_portals])
  572. {
  573. continue;
  574. }
  575. SimpleFlood(srcmightsee, p->leaf, portalsee, c_leafsee);
  576. }
  577. }
  578. #define PORTALSEE_SIZE (MAX_PORTALS*2)
  579. #ifdef SYSTEM_WIN32
  580. #pragma warning(push)
  581. #pragma warning(disable:4100) // unreferenced formal parameter
  582. #endif
  583. #ifdef HLVIS_MAXDIST
  584. // AJM: MVD
  585. // =====================================================================================
  586. // BlockVis
  587. // =====================================================================================
  588. void BlockVis(int unused)
  589. {
  590. int i, j, k, l, m;
  591. portal_t *p;
  592. visblocker_t *v;
  593. visblocker_t *v2;
  594. leaf_t *leaf;
  595. while(1)
  596. {
  597. i = GetThreadWork();
  598. if(i == -1)
  599. break;
  600. v = &g_visblockers[i];
  601. // See which visblockers we need
  602. for(j = 0; j < v->numnames; j++)
  603. {
  604. // Find visblocker
  605. if(!(v2 = GetVisBlock(v->blocknames[j])))
  606. continue;
  607. // For each leaf in v2, eliminate visibility from v1
  608. for(k = 0; k < v->numleafs; k++)
  609. {
  610. leaf = &g_leafs[v->blockleafs[k]];
  611. for(l = 0; l < leaf->numportals; l++)
  612. {
  613. p = leaf->portals[l];
  614. for(m = 0; m < v2->numleafs; m++)
  615. {
  616. const unsigned offset = v2->blockleafs[m] >> 3;
  617. const unsigned bit = (1 << (v2->blockleafs[m] & 7));
  618. p->mightsee[offset] &= ~bit;
  619. }
  620. }
  621. }
  622. }
  623. }
  624. }
  625. // AJM: MVD
  626. // =====================================================================================
  627. // GetSplitPortal
  628. // This function returns a portal on leaf1 that sucessfully seperates leaf1
  629. // and leaf2
  630. // =====================================================================================
  631. static portal_t *GetSplitPortal(leaf_t *leaf1, leaf_t *leaf2)
  632. {
  633. int i, k, l;
  634. portal_t *p1;
  635. portal_t *t;
  636. float check_dist;
  637. for(i = 0, p1 = leaf1->portals[0]; i < leaf1->numportals; i++, p1++)
  638. {
  639. hlassert(p1->winding->numpoints >= 3);
  640. // Check to make sure all the points on the other leaf are in front of the portal plane
  641. for(k = 0, t = leaf2->portals[0]; k < leaf2->numportals; k++, t++)
  642. {
  643. for(l = 0; l < t->winding->numpoints; l++)
  644. {
  645. check_dist = DotProduct(t->winding->points[l], p1->plane.normal) - p1->plane.dist;
  646. // We make the assumption that all portals face away from their parent leaf
  647. if(check_dist < -ON_EPSILON)
  648. goto PostLoop;
  649. }
  650. }
  651. PostLoop:
  652. // If we didn't check all the leaf2 portals, then this leaf1 portal doesn't work
  653. if(k < leaf2->numportals)
  654. continue;
  655. // If we reach this point, we found a good portal
  656. return p1;
  657. }
  658. // Didn't find any
  659. return NULL;
  660. }
  661. // AJM: MVD
  662. // =====================================================================================
  663. // MakeSplitPortalList
  664. // This function returns a portal on leaf1 that sucessfully seperates leaf1
  665. // and leaf2
  666. // =====================================================================================
  667. static void MakeSplitPortalList(leaf_t *leaf1, leaf_t *leaf2, portal_t **portals, int *num_portals)
  668. {
  669. int i, k, l;
  670. portal_t *p1;
  671. portal_t *t;
  672. *num_portals = 0;
  673. float check_dist;
  674. portal_t p_list[MAX_PORTALS_ON_LEAF];
  675. int c_portal = 0;
  676. if(*portals)
  677. delete [] *portals;
  678. for(i = 0, p1 = leaf1->portals[0]; i < leaf1->numportals; i++, p1++)
  679. {
  680. hlassert(p1->winding->numpoints >= 3);
  681. // Check to make sure all the points on the other leaf are in front of the portal plane
  682. for(k = 0, t = leaf2->portals[0]; k < leaf2->numportals; k++, t++)
  683. {
  684. for(l = 0; l < t->winding->numpoints; l++)
  685. {
  686. check_dist = DotProduct(t->winding->points[l], p1->plane.normal) - p1->plane.dist;
  687. // We make the assumption that all portals face away from their parent leaf
  688. if(check_dist < -ON_EPSILON)
  689. goto PostLoop;
  690. }
  691. }
  692. PostLoop:
  693. // If we didn't check all the leaf2 portals, then this leaf1 portal doesn't work
  694. if(k < leaf2->numportals)
  695. continue;
  696. // If we reach this point, we found a good portal
  697. memcpy(&p_list[c_portal++], p1, sizeof(portal_t));
  698. if(c_portal >= MAX_PORTALS_ON_LEAF)
  699. Error("c_portal > MAX_PORTALS_ON_LEAF");
  700. }
  701. if(!c_portal)
  702. return;
  703. *num_portals = c_portal;
  704. *portals = new portal_t[c_portal];
  705. memcpy(*portals, p_list, c_portal * sizeof(portal_t));
  706. }
  707. // AJM: MVD
  708. // =====================================================================================
  709. // DisjointLeafVis
  710. // This function returns TRUE if neither leaf can see the other
  711. // Returns FALSE otherwise
  712. // =====================================================================================
  713. static bool DisjointLeafVis(int leaf1, int leaf2)
  714. {
  715. leaf_t *l = g_leafs + leaf1;
  716. leaf_t *tl = g_leafs + leaf2;
  717. const unsigned offset_l = leaf1 >> 3;
  718. const unsigned bit_l = (1 << (leaf1 & 7));
  719. const unsigned offset_tl = leaf2 >> 3;
  720. const unsigned bit_tl = (1 << (leaf2 & 7));
  721. for(int k = 0; k < l->numportals; k++)
  722. {
  723. for(int m = 0; m < tl->numportals; m++)
  724. {
  725. if(l->portals[k]->mightsee[offset_tl] & bit_tl)
  726. goto RetFalse;
  727. if(tl->portals[m]->mightsee[offset_l] & bit_l)
  728. goto RetFalse;
  729. if(l->portals[k]->status != stat_none)
  730. {
  731. if(l->portals[k]->visbits[offset_tl] & bit_tl)
  732. goto RetFalse;
  733. }
  734. if(tl->portals[m]->status != stat_none)
  735. {
  736. if(tl->portals[m]->visbits[offset_l] & bit_l)
  737. goto RetFalse;
  738. }
  739. }
  740. }
  741. return true;
  742. RetFalse:
  743. return false;
  744. }
  745. // AJM: MVD
  746. // =====================================================================================
  747. // GetPortalBounds
  748. // This function take a portal and finds its bounds
  749. // parallel to the normal of the portal. They will face inwards
  750. // =====================================================================================
  751. static void GetPortalBounds(portal_t *p, plane_t **bounds)
  752. {
  753. int i;
  754. vec3_t vec1, vec2;
  755. hlassert(p->winding->numpoints >= 3);
  756. if(*bounds)
  757. delete [] *bounds;
  758. *bounds = new plane_t[p->winding->numpoints];
  759. // Loop through each set of points and create a plane boundary for each
  760. for(i = 0; i < p->winding->numpoints; i++)
  761. {
  762. VectorSubtract(p->winding->points[(i + 1) % p->winding->numpoints],p->winding->points[i],vec1);
  763. // Create inward normal for this boundary
  764. CrossProduct(p->plane.normal, vec1, vec2);
  765. VectorNormalize(vec2);
  766. VectorCopy(vec2, (*bounds)[i].normal);
  767. (*bounds)[i].dist = DotProduct(p->winding->points[i], vec2);
  768. }
  769. }
  770. // AJM: MVD
  771. // =====================================================================================
  772. // ClipWindingsToBounds
  773. // clips all the windings with all the planes (including original face) and outputs
  774. // what's left int "out"
  775. // =====================================================================================
  776. static void ClipWindingsToBounds(winding_t *windings, int numwindings, plane_t *bounds, int numbounds, plane_t &original_plane, winding_t **out, int &num_out)
  777. {
  778. hlassert(windings);
  779. hlassert(bounds);
  780. winding_t out_windings[MAX_PORTALS_ON_LEAF];
  781. num_out = 0;
  782. int h, i;
  783. *out = NULL;
  784. Winding wind;
  785. for(h = 0; h < numwindings; h++)
  786. {
  787. // For each winding...
  788. // Create a winding with CWinding
  789. wind.initFromPoints(windings[h].points, windings[h].numpoints);
  790. // Clip winding to original plane
  791. wind.Chop(original_plane.normal, original_plane.dist);
  792. for(i = 0; i < numbounds, wind.Valid(); i++)
  793. {
  794. // For each bound...
  795. // Chop the winding to the bounds
  796. wind.Chop(bounds[i].normal, bounds[i].dist);
  797. }
  798. if(wind.Valid())
  799. {
  800. // We have a valid winding, copy to array
  801. wind.CopyPoints(&out_windings[num_out].points[0], out_windings[num_out].numpoints);
  802. num_out++;
  803. }
  804. }
  805. if(!num_out) // Everything was clipped away
  806. return;
  807. // Otherwise, create out
  808. *out = new winding_t[num_out];
  809. memcpy(*out, out_windings, num_out * sizeof(winding_t));
  810. }
  811. // AJM: MVD
  812. // =====================================================================================
  813. // GenerateWindingList
  814. // This function generates a list of windings for a leaf through its portals
  815. // =====================================================================================
  816. static void GenerateWindingList(leaf_t *leaf, winding_t **winds)
  817. {
  818. winding_t windings[MAX_PORTALS_ON_LEAF];
  819. int numwinds = 0;
  820. int i;
  821. for(i = 0; i < leaf->numportals; i++)
  822. {
  823. memcpy(&windings[numwinds++], leaf->portals[i]->winding, sizeof(winding_t));
  824. }
  825. if(!numwinds)
  826. return;
  827. *winds = new winding_t[numwinds];
  828. memcpy(*winds, &windings, sizeof(winding_t) * numwinds);
  829. }
  830. // AJM: MVD
  831. // =====================================================================================
  832. // CalcPortalBoundsAndClipPortals
  833. // =====================================================================================
  834. static void CalcPortalBoundsAndClipPortals(portal_t *portal, leaf_t *leaf, winding_t **out, int &numout)
  835. {
  836. plane_t *bounds = NULL;
  837. winding_t *windings = NULL;
  838. GetPortalBounds(portal, &bounds);
  839. GenerateWindingList(leaf, &windings);
  840. ClipWindingsToBounds(windings, leaf->numportals, bounds, portal->winding->numpoints, portal->plane, out, numout);
  841. delete bounds;
  842. delete windings;
  843. }
  844. // AJM: MVD
  845. // =====================================================================================
  846. // GetShortestDistance
  847. // Gets the shortest distance between both leaves
  848. // =====================================================================================
  849. static float GetShortestDistance(leaf_t *leaf1, leaf_t *leaf2)
  850. {
  851. winding_t *final = NULL;
  852. int num_finals = 0;
  853. int i, x, y;
  854. float check;
  855. for(i = 0; i < leaf1->numportals; i++)
  856. {
  857. CalcPortalBoundsAndClipPortals(leaf1->portals[i], leaf2, &final, num_finals);
  858. // Minimum point distance
  859. for(x = 0; x < num_finals; x++)
  860. {
  861. for(y = 0; y < final[x].numpoints; y++)
  862. {
  863. check = DotProduct(leaf1->portals[i]->plane.normal, final[x].points[y]) - leaf1->portals[i]->plane.dist;
  864. if(check <= g_maxdistance)
  865. return check;
  866. }
  867. }
  868. delete final;
  869. }
  870. // Switch leaf 1 and 2
  871. for(i = 0; i < leaf2->numportals; i++)
  872. {
  873. CalcPortalBoundsAndClipPortals(leaf2->portals[i], leaf1, &final, num_finals);
  874. // Minimum point distance
  875. for(x = 0; x < num_finals; x++)
  876. {
  877. for(y = 0; y < final[x].numpoints; y++)
  878. {
  879. check = DotProduct(leaf2->portals[i]->plane.normal, final[x].points[y]) - leaf2->portals[i]->plane.dist;
  880. if(check <= g_maxdistance)
  881. return check;
  882. }
  883. }
  884. delete final;
  885. }
  886. return 9E10;
  887. }
  888. // AJM: MVD
  889. // =====================================================================================
  890. // CalcSplitsAndDotProducts
  891. // This function finds the splits of the leaf, and generates windings (if applicable)
  892. // =====================================================================================
  893. static float CalcSplitsAndDotProducts(plane_t *org_split_plane, leaf_t *leaf1, leaf_t *leaf2, plane_t *bounds, int num_bounds)
  894. {
  895. int i, j, k, l;
  896. portal_t *splits = NULL;
  897. int num_splits;
  898. float dist;
  899. float min_dist = 999999999.999;
  900. vec3_t i_points[MAX_POINTS_ON_FIXED_WINDING * MAX_PORTALS_ON_LEAF * 2];
  901. vec3_t delta;
  902. int num_points = 0;
  903. // First get splits
  904. MakeSplitPortalList(leaf1, leaf2, &splits, &num_splits);
  905. if(!num_splits)
  906. return min_dist;
  907. // If the number of splits = 1, then clip the plane using the boundary windings
  908. if(num_splits == 1)
  909. {
  910. Winding wind(splits[0].plane.normal, splits[0].plane.dist);
  911. for(i = 0; i < num_bounds; i++)
  912. {
  913. wind.Chop(bounds[i].normal, bounds[i].dist);
  914. }
  915. // The wind is chopped - get closest dot product
  916. for(i = 0; i < wind.m_NumPoints; i++)
  917. {
  918. dist = DotProduct(wind.m_Points[i], org_split_plane->normal) - org_split_plane->dist;
  919. min_dist = min(min_dist, dist);
  920. }
  921. return min_dist;
  922. }
  923. // In this case, we have more than one split point, and we must calculate all intersections
  924. // Properties of convex objects allow us to assume that these intersections will be the closest
  925. // points to the other leaf, and our other checks before this eliminate exception cases
  926. // Loop through each split portal, and using an inside loop, loop through every OTHER split portal
  927. // Common portal points in more than one split portal are intersections!
  928. for(i = 0; i < num_splits; i++)
  929. {
  930. for(j = 0; j < num_splits; j++)
  931. {
  932. if(i == j)
  933. {
  934. continue;
  935. }
  936. // Loop through each point on both portals
  937. for(k = 0; k < splits[i].winding->numpoints; k++)
  938. {
  939. for(l = 0; l < splits[j].winding->numpoints; l++)
  940. {
  941. VectorSubtract(splits[i].winding->points[k], splits[j].winding->points[l], delta);
  942. if(VectorLength(delta) < EQUAL_EPSILON)
  943. {
  944. memcpy(i_points[num_points++], splits[i].winding->points[k], sizeof(vec3_t));
  945. }
  946. }
  947. }
  948. }
  949. }
  950. // Loop through each intersection point and check
  951. for(i = 0; i < num_points; i++)
  952. {
  953. dist = DotProduct(i_points[i], org_split_plane->normal) - org_split_plane->dist;
  954. min_dist = min(min_dist, dist);
  955. }
  956. if(splits)
  957. delete [] splits;
  958. return min_dist;
  959. }
  960. #endif // HLVIS_MAXDIST
  961. // =====================================================================================
  962. // BasePortalVis
  963. // =====================================================================================
  964. void BasePortalVis(int unused)
  965. {
  966. int i, j, k;
  967. portal_t* tp;
  968. portal_t* p;
  969. float d;
  970. winding_t* w;
  971. byte portalsee[PORTALSEE_SIZE];
  972. const int portalsize = (g_numportals * 2);
  973. #ifdef ZHLT_NETVIS
  974. {
  975. i = unused;
  976. #else
  977. while (1)
  978. {
  979. i = GetThreadWork();
  980. if (i == -1)
  981. break;
  982. #endif
  983. p = g_portals + i;
  984. p->mightsee = (byte*)calloc(1, g_bitbytes);
  985. memset(portalsee, 0, portalsize);
  986. #if ZHLT_ZONES
  987. UINT32 zone = p->zone;
  988. #endif
  989. for (j = 0, tp = g_portals; j < portalsize; j++, tp++)
  990. {
  991. if (j == i)
  992. {
  993. continue;
  994. }
  995. #if ZHLT_ZONES
  996. if (g_Zones->check(zone, tp->zone))
  997. {
  998. continue;
  999. }
  1000. #endif
  1001. w = tp->winding;
  1002. for (k = 0; k < w->numpoints; k++)
  1003. {
  1004. d = DotProduct(w->points[k], p->plane.normal) - p->plane.dist;
  1005. if (d > ON_EPSILON)
  1006. {
  1007. break;
  1008. }
  1009. }
  1010. if (k == w->numpoints)
  1011. {
  1012. continue; // no points on front
  1013. }
  1014. w = p->winding;
  1015. for (k = 0; k < w->numpoints; k++)
  1016. {
  1017. d = DotProduct(w->points[k], tp->plane.normal) - tp->plane.dist;
  1018. if (d < -ON_EPSILON)
  1019. {
  1020. break;
  1021. }
  1022. }
  1023. if (k == w->numpoints)
  1024. {
  1025. continue; // no points on front
  1026. }
  1027. portalsee[j] = 1;
  1028. }
  1029. SimpleFlood(p->mightsee, p->leaf, portalsee, &p->nummightsee);
  1030. Verbose("portal:%4i nummightsee:%4i \n", i, p->nummightsee);
  1031. }
  1032. }
  1033. #ifdef HLVIS_MAXDIST
  1034. // AJM: MVD
  1035. // =====================================================================================
  1036. // MaxDistVis
  1037. // =====================================================================================
  1038. void MaxDistVis(int unused)
  1039. {
  1040. int i, j, k, m;
  1041. int a, b, c, d;
  1042. leaf_t *l;
  1043. leaf_t *tl;
  1044. plane_t *boundary = NULL;
  1045. vec3_t delta;
  1046. float new_dist;
  1047. unsigned offset_l;
  1048. unsigned bit_l;
  1049. unsigned offset_tl;
  1050. unsigned bit_tl;
  1051. while(1)
  1052. {
  1053. i = GetThreadWork();
  1054. if (i == -1)
  1055. break;
  1056. l = &g_leafs[i];
  1057. for(j = i + 1, tl = g_leafs + j; j < g_portalleafs; j++, tl++)
  1058. {
  1059. if(j == i) // Ideally, should never be true
  1060. {
  1061. continue;
  1062. }
  1063. // If they already can't see each other, no use checking
  1064. if(DisjointLeafVis(i, j))
  1065. {
  1066. continue;
  1067. }
  1068. new_dist = GetShortestDistance(l, tl);
  1069. if(new_dist <= g_maxdistance)
  1070. continue;
  1071. // Try out our NEW, IMPROVED ALGORITHM!!!!
  1072. // Get a portal on Leaf 1 that completely seperates the two leafs
  1073. /*split = GetSplitPortal(l, tl);
  1074. if(!split)
  1075. continue;
  1076. // We have a split, so create the bounds
  1077. GetPortalBounds(split, &boundary);
  1078. // Now get the dot product for all points on the other leaf
  1079. max_dist = 999999999.999;
  1080. /// Do the first check if mode is >= 2
  1081. if(g_mdmode >= 2)
  1082. {
  1083. for(k = 0; k < tl->numportals; k++)
  1084. {
  1085. for(m = 0; m < tl->portals[k]->winding->numpoints; m++)
  1086. {
  1087. for(n = 0; n < split->winding->numpoints; n++) // numpoints of split portals = number of boundaries
  1088. {
  1089. dist = DotProduct(tl->portals[k]->winding->points[m], boundary[n].normal) - boundary[n].dist;
  1090. if(dist < -ON_EPSILON)
  1091. {
  1092. // Outside boundaries
  1093. //max_dot = MaxDotProduct(tl->portals[k]->winding->points[m], boundary, split->winding->numpoints);
  1094. //max_dist = min(max_dist, max_dot);
  1095. // Break so we don't do inside boundary check
  1096. break;
  1097. }
  1098. }
  1099. if(n < split->winding->numpoints)
  1100. continue;
  1101. // We found a point that's inside all the boundries!
  1102. new_dist = DotProduct(tl->portals[k]->winding->points[m], split->plane.normal) - split->plane.dist;
  1103. max_dist = min(max_dist, new_dist);
  1104. }
  1105. }
  1106. }
  1107. // This is now a special check. If Leaf 2 has a split plane, we generate a polygon by clipping the plane
  1108. // with the borders. We then get the minimum dot products. If more than one split plane, use intersection.
  1109. // Only do this is g_mdmode is 3
  1110. if(g_mdmode >= 3) // For future mode expansion
  1111. {
  1112. new_dist = CalcSplitsAndDotProducts(&split->plane, tl, l, boundary, split->winding->numpoints);
  1113. max_dist = min(max_dist, new_dist);
  1114. }*/
  1115. // Third and final check. If the whole of leaf2 is outside of leaf1 boundaries, this one will catch it
  1116. // Basic "every point to every point" type of deal :)
  1117. // This is done by default all the time
  1118. for(a = 0; a < l->numportals; a++)
  1119. {
  1120. for(b = 0; b < tl->numportals; b++)
  1121. {
  1122. for(c = 0; c < l->portals[a]->winding->numpoints; c++)
  1123. {
  1124. for(d = 0; d < tl->portals[b]->winding->numpoints; d++)
  1125. {
  1126. VectorSubtract(l->portals[a]->winding->points[c], tl->portals[b]->winding->points[d], delta);
  1127. if(VectorLength(delta) <= g_maxdistance)
  1128. goto NoWork;
  1129. }
  1130. }
  1131. }
  1132. }
  1133. offset_l = i >> 3;
  1134. bit_l = (1 << (i & 7));
  1135. offset_tl = j >> 3;
  1136. bit_tl = (1 << (j & 7));
  1137. for(k = 0; k < l->numportals; k++)
  1138. {
  1139. for(m = 0; m < tl->numportals; m++)
  1140. {
  1141. if(l->portals[k]->status != stat_none)
  1142. l->portals[k]->visbits[offset_tl] &= ~bit_tl;
  1143. else
  1144. l->portals[k]->mightsee[offset_tl] &= ~bit_tl;
  1145. if(tl->portals[m]->status != stat_none)
  1146. tl->portals[m]->visbits[offset_l] &= ~bit_l;
  1147. else
  1148. tl->portals[m]->mightsee[offset_l] &= ~bit_l;
  1149. }
  1150. }
  1151. NoWork:
  1152. continue; // Hack to keep label from causing compile error
  1153. }
  1154. }
  1155. // Release potential memory
  1156. if(boundary)
  1157. delete [] boundary;
  1158. }
  1159. #endif // HLVIS_MAXDIST
  1160. #ifdef SYSTEM_WIN32
  1161. #pragma warning(pop)
  1162. #endif