r_edge.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // r_edge.c
  16. #include "r_local.h"
  17. #ifndef id386
  18. void R_SurfacePatch (void)
  19. {
  20. }
  21. void R_EdgeCodeStart (void)
  22. {
  23. }
  24. void R_EdgeCodeEnd (void)
  25. {
  26. }
  27. #endif
  28. #if 0
  29. the complex cases add new polys on most lines, so dont optimize for keeping them the same
  30. have multiple free span lists to try to get better coherence?
  31. low depth complexity -- 1 to 3 or so
  32. have a sentinal at both ends?
  33. #endif
  34. edge_t *auxedges;
  35. edge_t *r_edges, *edge_p, *edge_max;
  36. surf_t *surfaces, *surface_p, *surf_max;
  37. // surfaces are generated in back to front order by the bsp, so if a surf
  38. // pointer is greater than another one, it should be drawn in front
  39. // surfaces[1] is the background, and is used as the active surface stack
  40. edge_t *newedges[MAXHEIGHT];
  41. edge_t *removeedges[MAXHEIGHT];
  42. espan_t *span_p, *max_span_p;
  43. int r_currentkey;
  44. int current_iv;
  45. int edge_head_u_shift20, edge_tail_u_shift20;
  46. static void (*pdrawfunc)(void);
  47. edge_t edge_head;
  48. edge_t edge_tail;
  49. edge_t edge_aftertail;
  50. edge_t edge_sentinel;
  51. float fv;
  52. static int miplevel;
  53. float scale_for_mip;
  54. int ubasestep, errorterm, erroradjustup, erroradjustdown;
  55. // FIXME: should go away
  56. extern void R_RotateBmodel (void);
  57. extern void R_TransformFrustum (void);
  58. void R_GenerateSpans (void);
  59. void R_GenerateSpansBackward (void);
  60. void R_LeadingEdge (edge_t *edge);
  61. void R_LeadingEdgeBackwards (edge_t *edge);
  62. void R_TrailingEdge (surf_t *surf, edge_t *edge);
  63. /*
  64. ===============================================================================
  65. EDGE SCANNING
  66. ===============================================================================
  67. */
  68. /*
  69. ==============
  70. R_BeginEdgeFrame
  71. ==============
  72. */
  73. void R_BeginEdgeFrame (void)
  74. {
  75. int v;
  76. edge_p = r_edges;
  77. edge_max = &r_edges[r_numallocatededges];
  78. surface_p = &surfaces[2]; // background is surface 1,
  79. // surface 0 is a dummy
  80. surfaces[1].spans = NULL; // no background spans yet
  81. surfaces[1].flags = SURF_DRAWBACKGROUND;
  82. // put the background behind everything in the world
  83. if (sw_draworder->value)
  84. {
  85. pdrawfunc = R_GenerateSpansBackward;
  86. surfaces[1].key = 0;
  87. r_currentkey = 1;
  88. }
  89. else
  90. {
  91. pdrawfunc = R_GenerateSpans;
  92. surfaces[1].key = 0x7FFfFFFF;
  93. r_currentkey = 0;
  94. }
  95. // FIXME: set with memset
  96. for (v=r_refdef.vrect.y ; v<r_refdef.vrectbottom ; v++)
  97. {
  98. newedges[v] = removeedges[v] = NULL;
  99. }
  100. }
  101. #if !id386
  102. /*
  103. ==============
  104. R_InsertNewEdges
  105. Adds the edges in the linked list edgestoadd, adding them to the edges in the
  106. linked list edgelist. edgestoadd is assumed to be sorted on u, and non-empty (this is actually newedges[v]). edgelist is assumed to be sorted on u, with a
  107. sentinel at the end (actually, this is the active edge table starting at
  108. edge_head.next).
  109. ==============
  110. */
  111. void R_InsertNewEdges (edge_t *edgestoadd, edge_t *edgelist)
  112. {
  113. edge_t *next_edge;
  114. do
  115. {
  116. next_edge = edgestoadd->next;
  117. edgesearch:
  118. if (edgelist->u >= edgestoadd->u)
  119. goto addedge;
  120. edgelist=edgelist->next;
  121. if (edgelist->u >= edgestoadd->u)
  122. goto addedge;
  123. edgelist=edgelist->next;
  124. if (edgelist->u >= edgestoadd->u)
  125. goto addedge;
  126. edgelist=edgelist->next;
  127. if (edgelist->u >= edgestoadd->u)
  128. goto addedge;
  129. edgelist=edgelist->next;
  130. goto edgesearch;
  131. // insert edgestoadd before edgelist
  132. addedge:
  133. edgestoadd->next = edgelist;
  134. edgestoadd->prev = edgelist->prev;
  135. edgelist->prev->next = edgestoadd;
  136. edgelist->prev = edgestoadd;
  137. } while ((edgestoadd = next_edge) != NULL);
  138. }
  139. #endif // !id386
  140. #if !id386
  141. /*
  142. ==============
  143. R_RemoveEdges
  144. ==============
  145. */
  146. void R_RemoveEdges (edge_t *pedge)
  147. {
  148. do
  149. {
  150. pedge->next->prev = pedge->prev;
  151. pedge->prev->next = pedge->next;
  152. } while ((pedge = pedge->nextremove) != NULL);
  153. }
  154. #endif // !id386
  155. #if !id386
  156. /*
  157. ==============
  158. R_StepActiveU
  159. ==============
  160. */
  161. void R_StepActiveU (edge_t *pedge)
  162. {
  163. edge_t *pnext_edge, *pwedge;
  164. while (1)
  165. {
  166. nextedge:
  167. pedge->u += pedge->u_step;
  168. if (pedge->u < pedge->prev->u)
  169. goto pushback;
  170. pedge = pedge->next;
  171. pedge->u += pedge->u_step;
  172. if (pedge->u < pedge->prev->u)
  173. goto pushback;
  174. pedge = pedge->next;
  175. pedge->u += pedge->u_step;
  176. if (pedge->u < pedge->prev->u)
  177. goto pushback;
  178. pedge = pedge->next;
  179. pedge->u += pedge->u_step;
  180. if (pedge->u < pedge->prev->u)
  181. goto pushback;
  182. pedge = pedge->next;
  183. goto nextedge;
  184. pushback:
  185. if (pedge == &edge_aftertail)
  186. return;
  187. // push it back to keep it sorted
  188. pnext_edge = pedge->next;
  189. // pull the edge out of the edge list
  190. pedge->next->prev = pedge->prev;
  191. pedge->prev->next = pedge->next;
  192. // find out where the edge goes in the edge list
  193. pwedge = pedge->prev->prev;
  194. while (pwedge->u > pedge->u)
  195. {
  196. pwedge = pwedge->prev;
  197. }
  198. // put the edge back into the edge list
  199. pedge->next = pwedge->next;
  200. pedge->prev = pwedge;
  201. pedge->next->prev = pedge;
  202. pwedge->next = pedge;
  203. pedge = pnext_edge;
  204. if (pedge == &edge_tail)
  205. return;
  206. }
  207. }
  208. #endif // !id386
  209. /*
  210. ==============
  211. R_CleanupSpan
  212. ==============
  213. */
  214. void R_CleanupSpan (void)
  215. {
  216. surf_t *surf;
  217. int iu;
  218. espan_t *span;
  219. // now that we've reached the right edge of the screen, we're done with any
  220. // unfinished surfaces, so emit a span for whatever's on top
  221. surf = surfaces[1].next;
  222. iu = edge_tail_u_shift20;
  223. if (iu > surf->last_u)
  224. {
  225. span = span_p++;
  226. span->u = surf->last_u;
  227. span->count = iu - span->u;
  228. span->v = current_iv;
  229. span->pnext = surf->spans;
  230. surf->spans = span;
  231. }
  232. // reset spanstate for all surfaces in the surface stack
  233. do
  234. {
  235. surf->spanstate = 0;
  236. surf = surf->next;
  237. } while (surf != &surfaces[1]);
  238. }
  239. /*
  240. ==============
  241. R_LeadingEdgeBackwards
  242. ==============
  243. */
  244. void R_LeadingEdgeBackwards (edge_t *edge)
  245. {
  246. espan_t *span;
  247. surf_t *surf, *surf2;
  248. int iu;
  249. // it's adding a new surface in, so find the correct place
  250. surf = &surfaces[edge->surfs[1]];
  251. // don't start a span if this is an inverted span, with the end
  252. // edge preceding the start edge (that is, we've already seen the
  253. // end edge)
  254. if (++surf->spanstate == 1)
  255. {
  256. surf2 = surfaces[1].next;
  257. if (surf->key > surf2->key)
  258. goto newtop;
  259. // if it's two surfaces on the same plane, the one that's already
  260. // active is in front, so keep going unless it's a bmodel
  261. if (surf->insubmodel && (surf->key == surf2->key))
  262. {
  263. // must be two bmodels in the same leaf; don't care, because they'll
  264. // never be farthest anyway
  265. goto newtop;
  266. }
  267. continue_search:
  268. do
  269. {
  270. surf2 = surf2->next;
  271. } while (surf->key < surf2->key);
  272. if (surf->key == surf2->key)
  273. {
  274. // if it's two surfaces on the same plane, the one that's already
  275. // active is in front, so keep going unless it's a bmodel
  276. if (!surf->insubmodel)
  277. goto continue_search;
  278. // must be two bmodels in the same leaf; don't care which is really
  279. // in front, because they'll never be farthest anyway
  280. }
  281. goto gotposition;
  282. newtop:
  283. // emit a span (obscures current top)
  284. iu = edge->u >> 20;
  285. if (iu > surf2->last_u)
  286. {
  287. span = span_p++;
  288. span->u = surf2->last_u;
  289. span->count = iu - span->u;
  290. span->v = current_iv;
  291. span->pnext = surf2->spans;
  292. surf2->spans = span;
  293. }
  294. // set last_u on the new span
  295. surf->last_u = iu;
  296. gotposition:
  297. // insert before surf2
  298. surf->next = surf2;
  299. surf->prev = surf2->prev;
  300. surf2->prev->next = surf;
  301. surf2->prev = surf;
  302. }
  303. }
  304. /*
  305. ==============
  306. R_TrailingEdge
  307. ==============
  308. */
  309. void R_TrailingEdge (surf_t *surf, edge_t *edge)
  310. {
  311. espan_t *span;
  312. int iu;
  313. // don't generate a span if this is an inverted span, with the end
  314. // edge preceding the start edge (that is, we haven't seen the
  315. // start edge yet)
  316. if (--surf->spanstate == 0)
  317. {
  318. if (surf == surfaces[1].next)
  319. {
  320. // emit a span (current top going away)
  321. iu = edge->u >> 20;
  322. if (iu > surf->last_u)
  323. {
  324. span = span_p++;
  325. span->u = surf->last_u;
  326. span->count = iu - span->u;
  327. span->v = current_iv;
  328. span->pnext = surf->spans;
  329. surf->spans = span;
  330. }
  331. // set last_u on the surface below
  332. surf->next->last_u = iu;
  333. }
  334. surf->prev->next = surf->next;
  335. surf->next->prev = surf->prev;
  336. }
  337. }
  338. #if !id386
  339. /*
  340. ==============
  341. R_LeadingEdge
  342. ==============
  343. */
  344. void R_LeadingEdge (edge_t *edge)
  345. {
  346. espan_t *span;
  347. surf_t *surf, *surf2;
  348. int iu;
  349. float fu, newzi, testzi, newzitop, newzibottom;
  350. if (edge->surfs[1])
  351. {
  352. // it's adding a new surface in, so find the correct place
  353. surf = &surfaces[edge->surfs[1]];
  354. // don't start a span if this is an inverted span, with the end
  355. // edge preceding the start edge (that is, we've already seen the
  356. // end edge)
  357. if (++surf->spanstate == 1)
  358. {
  359. surf2 = surfaces[1].next;
  360. if (surf->key < surf2->key)
  361. goto newtop;
  362. // if it's two surfaces on the same plane, the one that's already
  363. // active is in front, so keep going unless it's a bmodel
  364. if (surf->insubmodel && (surf->key == surf2->key))
  365. {
  366. // must be two bmodels in the same leaf; sort on 1/z
  367. fu = (float)(edge->u - 0xFFFFF) * (1.0 / 0x100000);
  368. newzi = surf->d_ziorigin + fv*surf->d_zistepv +
  369. fu*surf->d_zistepu;
  370. newzibottom = newzi * 0.99;
  371. testzi = surf2->d_ziorigin + fv*surf2->d_zistepv +
  372. fu*surf2->d_zistepu;
  373. if (newzibottom >= testzi)
  374. {
  375. goto newtop;
  376. }
  377. newzitop = newzi * 1.01;
  378. if (newzitop >= testzi)
  379. {
  380. if (surf->d_zistepu >= surf2->d_zistepu)
  381. {
  382. goto newtop;
  383. }
  384. }
  385. }
  386. continue_search:
  387. do
  388. {
  389. surf2 = surf2->next;
  390. } while (surf->key > surf2->key);
  391. if (surf->key == surf2->key)
  392. {
  393. // if it's two surfaces on the same plane, the one that's already
  394. // active is in front, so keep going unless it's a bmodel
  395. if (!surf->insubmodel)
  396. goto continue_search;
  397. // must be two bmodels in the same leaf; sort on 1/z
  398. fu = (float)(edge->u - 0xFFFFF) * (1.0 / 0x100000);
  399. newzi = surf->d_ziorigin + fv*surf->d_zistepv +
  400. fu*surf->d_zistepu;
  401. newzibottom = newzi * 0.99;
  402. testzi = surf2->d_ziorigin + fv*surf2->d_zistepv +
  403. fu*surf2->d_zistepu;
  404. if (newzibottom >= testzi)
  405. {
  406. goto gotposition;
  407. }
  408. newzitop = newzi * 1.01;
  409. if (newzitop >= testzi)
  410. {
  411. if (surf->d_zistepu >= surf2->d_zistepu)
  412. {
  413. goto gotposition;
  414. }
  415. }
  416. goto continue_search;
  417. }
  418. goto gotposition;
  419. newtop:
  420. // emit a span (obscures current top)
  421. iu = edge->u >> 20;
  422. if (iu > surf2->last_u)
  423. {
  424. span = span_p++;
  425. span->u = surf2->last_u;
  426. span->count = iu - span->u;
  427. span->v = current_iv;
  428. span->pnext = surf2->spans;
  429. surf2->spans = span;
  430. }
  431. // set last_u on the new span
  432. surf->last_u = iu;
  433. gotposition:
  434. // insert before surf2
  435. surf->next = surf2;
  436. surf->prev = surf2->prev;
  437. surf2->prev->next = surf;
  438. surf2->prev = surf;
  439. }
  440. }
  441. }
  442. /*
  443. ==============
  444. R_GenerateSpans
  445. ==============
  446. */
  447. void R_GenerateSpans (void)
  448. {
  449. edge_t *edge;
  450. surf_t *surf;
  451. // clear active surfaces to just the background surface
  452. surfaces[1].next = surfaces[1].prev = &surfaces[1];
  453. surfaces[1].last_u = edge_head_u_shift20;
  454. // generate spans
  455. for (edge=edge_head.next ; edge != &edge_tail; edge=edge->next)
  456. {
  457. if (edge->surfs[0])
  458. {
  459. // it has a left surface, so a surface is going away for this span
  460. surf = &surfaces[edge->surfs[0]];
  461. R_TrailingEdge (surf, edge);
  462. if (!edge->surfs[1])
  463. continue;
  464. }
  465. R_LeadingEdge (edge);
  466. }
  467. R_CleanupSpan ();
  468. }
  469. #endif // !id386
  470. /*
  471. ==============
  472. R_GenerateSpansBackward
  473. ==============
  474. */
  475. void R_GenerateSpansBackward (void)
  476. {
  477. edge_t *edge;
  478. // clear active surfaces to just the background surface
  479. surfaces[1].next = surfaces[1].prev = &surfaces[1];
  480. surfaces[1].last_u = edge_head_u_shift20;
  481. // generate spans
  482. for (edge=edge_head.next ; edge != &edge_tail; edge=edge->next)
  483. {
  484. if (edge->surfs[0])
  485. R_TrailingEdge (&surfaces[edge->surfs[0]], edge);
  486. if (edge->surfs[1])
  487. R_LeadingEdgeBackwards (edge);
  488. }
  489. R_CleanupSpan ();
  490. }
  491. /*
  492. ==============
  493. R_ScanEdges
  494. Input:
  495. newedges[] array
  496. this has links to edges, which have links to surfaces
  497. Output:
  498. Each surface has a linked list of its visible spans
  499. ==============
  500. */
  501. void R_ScanEdges (void)
  502. {
  503. int iv, bottom;
  504. byte basespans[MAXSPANS*sizeof(espan_t)+CACHE_SIZE];
  505. espan_t *basespan_p;
  506. surf_t *s;
  507. basespan_p = (espan_t *)
  508. ((long)(basespans + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1));
  509. max_span_p = &basespan_p[MAXSPANS - r_refdef.vrect.width];
  510. span_p = basespan_p;
  511. // clear active edges to just the background edges around the whole screen
  512. // FIXME: most of this only needs to be set up once
  513. edge_head.u = r_refdef.vrect.x << 20;
  514. edge_head_u_shift20 = edge_head.u >> 20;
  515. edge_head.u_step = 0;
  516. edge_head.prev = NULL;
  517. edge_head.next = &edge_tail;
  518. edge_head.surfs[0] = 0;
  519. edge_head.surfs[1] = 1;
  520. edge_tail.u = (r_refdef.vrectright << 20) + 0xFFFFF;
  521. edge_tail_u_shift20 = edge_tail.u >> 20;
  522. edge_tail.u_step = 0;
  523. edge_tail.prev = &edge_head;
  524. edge_tail.next = &edge_aftertail;
  525. edge_tail.surfs[0] = 1;
  526. edge_tail.surfs[1] = 0;
  527. edge_aftertail.u = -1; // force a move
  528. edge_aftertail.u_step = 0;
  529. edge_aftertail.next = &edge_sentinel;
  530. edge_aftertail.prev = &edge_tail;
  531. // FIXME: do we need this now that we clamp x in r_draw.c?
  532. edge_sentinel.u = 2000 << 24; // make sure nothing sorts past this
  533. edge_sentinel.prev = &edge_aftertail;
  534. //
  535. // process all scan lines
  536. //
  537. bottom = r_refdef.vrectbottom - 1;
  538. for (iv=r_refdef.vrect.y ; iv<bottom ; iv++)
  539. {
  540. current_iv = iv;
  541. fv = (float)iv;
  542. // mark that the head (background start) span is pre-included
  543. surfaces[1].spanstate = 1;
  544. if (newedges[iv])
  545. {
  546. R_InsertNewEdges (newedges[iv], edge_head.next);
  547. }
  548. (*pdrawfunc) ();
  549. // flush the span list if we can't be sure we have enough spans left for
  550. // the next scan
  551. if (span_p > max_span_p)
  552. {
  553. D_DrawSurfaces ();
  554. // clear the surface span pointers
  555. for (s = &surfaces[1] ; s<surface_p ; s++)
  556. s->spans = NULL;
  557. span_p = basespan_p;
  558. }
  559. if (removeedges[iv])
  560. R_RemoveEdges (removeedges[iv]);
  561. if (edge_head.next != &edge_tail)
  562. R_StepActiveU (edge_head.next);
  563. }
  564. // do the last scan (no need to step or sort or remove on the last scan)
  565. current_iv = iv;
  566. fv = (float)iv;
  567. // mark that the head (background start) span is pre-included
  568. surfaces[1].spanstate = 1;
  569. if (newedges[iv])
  570. R_InsertNewEdges (newedges[iv], edge_head.next);
  571. (*pdrawfunc) ();
  572. // draw whatever's left in the span list
  573. D_DrawSurfaces ();
  574. }
  575. /*
  576. =========================================================================
  577. SURFACE FILLING
  578. =========================================================================
  579. */
  580. msurface_t *pface;
  581. surfcache_t *pcurrentcache;
  582. vec3_t transformed_modelorg;
  583. vec3_t world_transformed_modelorg;
  584. vec3_t local_modelorg;
  585. /*
  586. =============
  587. D_MipLevelForScale
  588. =============
  589. */
  590. int D_MipLevelForScale (float scale)
  591. {
  592. int lmiplevel;
  593. if (scale >= d_scalemip[0] )
  594. lmiplevel = 0;
  595. else if (scale >= d_scalemip[1] )
  596. lmiplevel = 1;
  597. else if (scale >= d_scalemip[2] )
  598. lmiplevel = 2;
  599. else
  600. lmiplevel = 3;
  601. if (lmiplevel < d_minmip)
  602. lmiplevel = d_minmip;
  603. return lmiplevel;
  604. }
  605. /*
  606. ==============
  607. D_FlatFillSurface
  608. Simple single color fill with no texture mapping
  609. ==============
  610. */
  611. void D_FlatFillSurface (surf_t *surf, int color)
  612. {
  613. espan_t *span;
  614. byte *pdest;
  615. int u, u2;
  616. for (span=surf->spans ; span ; span=span->pnext)
  617. {
  618. pdest = (byte *)d_viewbuffer + r_screenwidth*span->v;
  619. u = span->u;
  620. u2 = span->u + span->count - 1;
  621. for ( ; u <= u2 ; u++)
  622. pdest[u] = color;
  623. }
  624. }
  625. /*
  626. ==============
  627. D_CalcGradients
  628. ==============
  629. */
  630. void D_CalcGradients (msurface_t *pface)
  631. {
  632. mplane_t *pplane;
  633. float mipscale;
  634. vec3_t p_temp1;
  635. vec3_t p_saxis, p_taxis;
  636. float t;
  637. pplane = pface->plane;
  638. mipscale = 1.0 / (float)(1 << miplevel);
  639. TransformVector (pface->texinfo->vecs[0], p_saxis);
  640. TransformVector (pface->texinfo->vecs[1], p_taxis);
  641. t = xscaleinv * mipscale;
  642. d_sdivzstepu = p_saxis[0] * t;
  643. d_tdivzstepu = p_taxis[0] * t;
  644. t = yscaleinv * mipscale;
  645. d_sdivzstepv = -p_saxis[1] * t;
  646. d_tdivzstepv = -p_taxis[1] * t;
  647. d_sdivzorigin = p_saxis[2] * mipscale - xcenter * d_sdivzstepu -
  648. ycenter * d_sdivzstepv;
  649. d_tdivzorigin = p_taxis[2] * mipscale - xcenter * d_tdivzstepu -
  650. ycenter * d_tdivzstepv;
  651. VectorScale (transformed_modelorg, mipscale, p_temp1);
  652. t = 0x10000*mipscale;
  653. sadjust = ((fixed16_t)(DotProduct (p_temp1, p_saxis) * 0x10000 + 0.5)) -
  654. ((pface->texturemins[0] << 16) >> miplevel)
  655. + pface->texinfo->vecs[0][3]*t;
  656. tadjust = ((fixed16_t)(DotProduct (p_temp1, p_taxis) * 0x10000 + 0.5)) -
  657. ((pface->texturemins[1] << 16) >> miplevel)
  658. + pface->texinfo->vecs[1][3]*t;
  659. // PGM - changing flow speed for non-warping textures.
  660. if (pface->texinfo->flags & SURF_FLOWING)
  661. {
  662. if(pface->texinfo->flags & SURF_WARP)
  663. sadjust += 0x10000 * (-128 * ( (r_newrefdef.time * 0.25) - (int)(r_newrefdef.time * 0.25) ));
  664. else
  665. sadjust += 0x10000 * (-128 * ( (r_newrefdef.time * 0.77) - (int)(r_newrefdef.time * 0.77) ));
  666. }
  667. // PGM
  668. //
  669. // -1 (-epsilon) so we never wander off the edge of the texture
  670. //
  671. bbextents = ((pface->extents[0] << 16) >> miplevel) - 1;
  672. bbextentt = ((pface->extents[1] << 16) >> miplevel) - 1;
  673. }
  674. /*
  675. ==============
  676. D_BackgroundSurf
  677. The grey background filler seen when there is a hole in the map
  678. ==============
  679. */
  680. void D_BackgroundSurf (surf_t *s)
  681. {
  682. // set up a gradient for the background surface that places it
  683. // effectively at infinity distance from the viewpoint
  684. d_zistepu = 0;
  685. d_zistepv = 0;
  686. d_ziorigin = -0.9;
  687. D_FlatFillSurface (s, (int)sw_clearcolor->value & 0xFF);
  688. D_DrawZSpans (s->spans);
  689. }
  690. /*
  691. =================
  692. D_TurbulentSurf
  693. =================
  694. */
  695. void D_TurbulentSurf (surf_t *s)
  696. {
  697. d_zistepu = s->d_zistepu;
  698. d_zistepv = s->d_zistepv;
  699. d_ziorigin = s->d_ziorigin;
  700. pface = s->msurf;
  701. miplevel = 0;
  702. cacheblock = pface->texinfo->image->pixels[0];
  703. cachewidth = 64;
  704. if (s->insubmodel)
  705. {
  706. // FIXME: we don't want to do all this for every polygon!
  707. // TODO: store once at start of frame
  708. currententity = s->entity; //FIXME: make this passed in to
  709. // R_RotateBmodel ()
  710. VectorSubtract (r_origin, currententity->origin,
  711. local_modelorg);
  712. TransformVector (local_modelorg, transformed_modelorg);
  713. R_RotateBmodel (); // FIXME: don't mess with the frustum,
  714. // make entity passed in
  715. }
  716. D_CalcGradients (pface);
  717. //============
  718. //PGM
  719. // textures that aren't warping are just flowing. Use NonTurbulent8 instead
  720. if(!(pface->texinfo->flags & SURF_WARP))
  721. NonTurbulent8 (s->spans);
  722. else
  723. Turbulent8 (s->spans);
  724. //PGM
  725. //============
  726. D_DrawZSpans (s->spans);
  727. if (s->insubmodel)
  728. {
  729. //
  730. // restore the old drawing state
  731. // FIXME: we don't want to do this every time!
  732. // TODO: speed up
  733. //
  734. currententity = NULL; // &r_worldentity;
  735. VectorCopy (world_transformed_modelorg,
  736. transformed_modelorg);
  737. VectorCopy (base_vpn, vpn);
  738. VectorCopy (base_vup, vup);
  739. VectorCopy (base_vright, vright);
  740. R_TransformFrustum ();
  741. }
  742. }
  743. /*
  744. ==============
  745. D_SkySurf
  746. ==============
  747. */
  748. void D_SkySurf (surf_t *s)
  749. {
  750. pface = s->msurf;
  751. miplevel = 0;
  752. if (!pface->texinfo->image)
  753. return;
  754. cacheblock = pface->texinfo->image->pixels[0];
  755. cachewidth = 256;
  756. d_zistepu = s->d_zistepu;
  757. d_zistepv = s->d_zistepv;
  758. d_ziorigin = s->d_ziorigin;
  759. D_CalcGradients (pface);
  760. D_DrawSpans16 (s->spans);
  761. // set up a gradient for the background surface that places it
  762. // effectively at infinity distance from the viewpoint
  763. d_zistepu = 0;
  764. d_zistepv = 0;
  765. d_ziorigin = -0.9;
  766. D_DrawZSpans (s->spans);
  767. }
  768. /*
  769. ==============
  770. D_SolidSurf
  771. Normal surface cached, texture mapped surface
  772. ==============
  773. */
  774. void D_SolidSurf (surf_t *s)
  775. {
  776. d_zistepu = s->d_zistepu;
  777. d_zistepv = s->d_zistepv;
  778. d_ziorigin = s->d_ziorigin;
  779. if (s->insubmodel)
  780. {
  781. // FIXME: we don't want to do all this for every polygon!
  782. // TODO: store once at start of frame
  783. currententity = s->entity; //FIXME: make this passed in to
  784. // R_RotateBmodel ()
  785. VectorSubtract (r_origin, currententity->origin, local_modelorg);
  786. TransformVector (local_modelorg, transformed_modelorg);
  787. R_RotateBmodel (); // FIXME: don't mess with the frustum,
  788. // make entity passed in
  789. }
  790. else
  791. currententity = &r_worldentity;
  792. pface = s->msurf;
  793. #if 1
  794. miplevel = D_MipLevelForScale(s->nearzi * scale_for_mip * pface->texinfo->mipadjust);
  795. #else
  796. {
  797. float dot;
  798. float normal[3];
  799. if ( s->insubmodel )
  800. {
  801. VectorCopy( pface->plane->normal, normal );
  802. // TransformVector( pface->plane->normal, normal);
  803. dot = DotProduct( normal, vpn );
  804. }
  805. else
  806. {
  807. VectorCopy( pface->plane->normal, normal );
  808. dot = DotProduct( normal, vpn );
  809. }
  810. if ( pface->flags & SURF_PLANEBACK )
  811. dot = -dot;
  812. if ( dot > 0 )
  813. printf( "blah" );
  814. miplevel = D_MipLevelForScale(s->nearzi * scale_for_mip * pface->texinfo->mipadjust);
  815. }
  816. #endif
  817. // FIXME: make this passed in to D_CacheSurface
  818. pcurrentcache = D_CacheSurface (pface, miplevel);
  819. cacheblock = (pixel_t *)pcurrentcache->data;
  820. cachewidth = pcurrentcache->width;
  821. D_CalcGradients (pface);
  822. D_DrawSpans16 (s->spans);
  823. D_DrawZSpans (s->spans);
  824. if (s->insubmodel)
  825. {
  826. //
  827. // restore the old drawing state
  828. // FIXME: we don't want to do this every time!
  829. // TODO: speed up
  830. //
  831. VectorCopy (world_transformed_modelorg,
  832. transformed_modelorg);
  833. VectorCopy (base_vpn, vpn);
  834. VectorCopy (base_vup, vup);
  835. VectorCopy (base_vright, vright);
  836. R_TransformFrustum ();
  837. currententity = NULL; //&r_worldentity;
  838. }
  839. }
  840. /*
  841. =============
  842. D_DrawflatSurfaces
  843. To allow developers to see the polygon carving of the world
  844. =============
  845. */
  846. void D_DrawflatSurfaces (void)
  847. {
  848. surf_t *s;
  849. for (s = &surfaces[1] ; s<surface_p ; s++)
  850. {
  851. if (!s->spans)
  852. continue;
  853. d_zistepu = s->d_zistepu;
  854. d_zistepv = s->d_zistepv;
  855. d_ziorigin = s->d_ziorigin;
  856. // make a stable color for each surface by taking the low
  857. // bits of the msurface pointer
  858. D_FlatFillSurface (s, (int)s->msurf & 0xFF);
  859. D_DrawZSpans (s->spans);
  860. }
  861. }
  862. /*
  863. ==============
  864. D_DrawSurfaces
  865. Rasterize all the span lists. Guaranteed zero overdraw.
  866. May be called more than once a frame if the surf list overflows (higher res)
  867. ==============
  868. */
  869. void D_DrawSurfaces (void)
  870. {
  871. surf_t *s;
  872. // currententity = NULL; //&r_worldentity;
  873. VectorSubtract (r_origin, vec3_origin, modelorg);
  874. TransformVector (modelorg, transformed_modelorg);
  875. VectorCopy (transformed_modelorg, world_transformed_modelorg);
  876. if (!sw_drawflat->value)
  877. {
  878. for (s = &surfaces[1] ; s<surface_p ; s++)
  879. {
  880. if (!s->spans)
  881. continue;
  882. r_drawnpolycount++;
  883. if (! (s->flags & (SURF_DRAWSKYBOX|SURF_DRAWBACKGROUND|SURF_DRAWTURB) ) )
  884. D_SolidSurf (s);
  885. else if (s->flags & SURF_DRAWSKYBOX)
  886. D_SkySurf (s);
  887. else if (s->flags & SURF_DRAWBACKGROUND)
  888. D_BackgroundSurf (s);
  889. else if (s->flags & SURF_DRAWTURB)
  890. D_TurbulentSurf (s);
  891. }
  892. }
  893. else
  894. D_DrawflatSurfaces ();
  895. currententity = NULL; //&r_worldentity;
  896. VectorSubtract (r_origin, vec3_origin, modelorg);
  897. R_TransformFrustum ();
  898. }