d_polyse.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. /*
  2. Copyright (C) 1996-1997 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. // d_polyset.c: routines for drawing sets of polygons sharing the same
  16. // texture (used for Alias models)
  17. #include "quakedef.h"
  18. #include "r_local.h"
  19. #include "d_local.h"
  20. // TODO: put in span spilling to shrink list size
  21. // !!! if this is changed, it must be changed in d_polysa.s too !!!
  22. #define DPS_MAXSPANS MAXHEIGHT+1
  23. // 1 extra for spanpackage that marks end
  24. // !!! if this is changed, it must be changed in asm_draw.h too !!!
  25. typedef struct {
  26. void *pdest;
  27. short *pz;
  28. int count;
  29. byte *ptex;
  30. int sfrac, tfrac, light, zi;
  31. } spanpackage_t;
  32. typedef struct {
  33. int isflattop;
  34. int numleftedges;
  35. int *pleftedgevert0;
  36. int *pleftedgevert1;
  37. int *pleftedgevert2;
  38. int numrightedges;
  39. int *prightedgevert0;
  40. int *prightedgevert1;
  41. int *prightedgevert2;
  42. } edgetable;
  43. int r_p0[6], r_p1[6], r_p2[6];
  44. byte *d_pcolormap;
  45. int d_aflatcolor;
  46. int d_xdenom;
  47. edgetable *pedgetable;
  48. edgetable edgetables[12] = {
  49. {0, 1, r_p0, r_p2, NULL, 2, r_p0, r_p1, r_p2 },
  50. {0, 2, r_p1, r_p0, r_p2, 1, r_p1, r_p2, NULL},
  51. {1, 1, r_p0, r_p2, NULL, 1, r_p1, r_p2, NULL},
  52. {0, 1, r_p1, r_p0, NULL, 2, r_p1, r_p2, r_p0 },
  53. {0, 2, r_p0, r_p2, r_p1, 1, r_p0, r_p1, NULL},
  54. {0, 1, r_p2, r_p1, NULL, 1, r_p2, r_p0, NULL},
  55. {0, 1, r_p2, r_p1, NULL, 2, r_p2, r_p0, r_p1 },
  56. {0, 2, r_p2, r_p1, r_p0, 1, r_p2, r_p0, NULL},
  57. {0, 1, r_p1, r_p0, NULL, 1, r_p1, r_p2, NULL},
  58. {1, 1, r_p2, r_p1, NULL, 1, r_p0, r_p1, NULL},
  59. {1, 1, r_p1, r_p0, NULL, 1, r_p2, r_p0, NULL},
  60. {0, 1, r_p0, r_p2, NULL, 1, r_p0, r_p1, NULL},
  61. };
  62. // FIXME: some of these can become statics
  63. int a_sstepxfrac, a_tstepxfrac, r_lstepx, a_ststepxwhole;
  64. int r_sstepx, r_tstepx, r_lstepy, r_sstepy, r_tstepy;
  65. int r_zistepx, r_zistepy;
  66. int d_aspancount, d_countextrastep;
  67. spanpackage_t *a_spans;
  68. spanpackage_t *d_pedgespanpackage;
  69. static int ystart;
  70. byte *d_pdest, *d_ptex;
  71. short *d_pz;
  72. int d_sfrac, d_tfrac, d_light, d_zi;
  73. int d_ptexextrastep, d_sfracextrastep;
  74. int d_tfracextrastep, d_lightextrastep, d_pdestextrastep;
  75. int d_lightbasestep, d_pdestbasestep, d_ptexbasestep;
  76. int d_sfracbasestep, d_tfracbasestep;
  77. int d_ziextrastep, d_zibasestep;
  78. int d_pzextrastep, d_pzbasestep;
  79. typedef struct {
  80. int quotient;
  81. int remainder;
  82. } adivtab_t;
  83. static adivtab_t adivtab[32*32] = {
  84. #include "adivtab.h"
  85. };
  86. byte *skintable[MAX_LBM_HEIGHT];
  87. int skinwidth;
  88. byte *skinstart;
  89. void D_PolysetDrawSpans8 (spanpackage_t *pspanpackage);
  90. void D_PolysetCalcGradients (int skinwidth);
  91. void D_DrawSubdiv (void);
  92. void D_DrawNonSubdiv (void);
  93. void D_PolysetRecursiveTriangle (int *p1, int *p2, int *p3);
  94. void D_PolysetSetEdgeTable (void);
  95. void D_RasterizeAliasPolySmooth (void);
  96. void D_PolysetScanLeftEdge (int height);
  97. #if !id386
  98. /*
  99. ================
  100. D_PolysetDraw
  101. ================
  102. */
  103. void D_PolysetDraw (void)
  104. {
  105. spanpackage_t spans[DPS_MAXSPANS + 1 +
  106. ((CACHE_SIZE - 1) / sizeof(spanpackage_t)) + 1];
  107. // one extra because of cache line pretouching
  108. a_spans = (spanpackage_t *)
  109. (((long)&spans[0] + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1));
  110. if (r_affinetridesc.drawtype)
  111. {
  112. D_DrawSubdiv ();
  113. }
  114. else
  115. {
  116. D_DrawNonSubdiv ();
  117. }
  118. }
  119. /*
  120. ================
  121. D_PolysetDrawFinalVerts
  122. ================
  123. */
  124. void D_PolysetDrawFinalVerts (finalvert_t *fv, int numverts)
  125. {
  126. int i, z;
  127. short *zbuf;
  128. for (i=0 ; i<numverts ; i++, fv++)
  129. {
  130. // valid triangle coordinates for filling can include the bottom and
  131. // right clip edges, due to the fill rule; these shouldn't be drawn
  132. if ((fv->v[0] < r_refdef.vrectright) &&
  133. (fv->v[1] < r_refdef.vrectbottom))
  134. {
  135. z = fv->v[5]>>16;
  136. zbuf = zspantable[fv->v[1]] + fv->v[0];
  137. if (z >= *zbuf)
  138. {
  139. int pix;
  140. *zbuf = z;
  141. pix = skintable[fv->v[3]>>16][fv->v[2]>>16];
  142. pix = ((byte *)acolormap)[pix + (fv->v[4] & 0xFF00) ];
  143. d_viewbuffer[d_scantable[fv->v[1]] + fv->v[0]] = pix;
  144. }
  145. }
  146. }
  147. }
  148. /*
  149. ================
  150. D_DrawSubdiv
  151. ================
  152. */
  153. void D_DrawSubdiv (void)
  154. {
  155. mtriangle_t *ptri;
  156. finalvert_t *pfv, *index0, *index1, *index2;
  157. int i;
  158. int lnumtriangles;
  159. pfv = r_affinetridesc.pfinalverts;
  160. ptri = r_affinetridesc.ptriangles;
  161. lnumtriangles = r_affinetridesc.numtriangles;
  162. for (i=0 ; i<lnumtriangles ; i++)
  163. {
  164. index0 = pfv + ptri[i].vertindex[0];
  165. index1 = pfv + ptri[i].vertindex[1];
  166. index2 = pfv + ptri[i].vertindex[2];
  167. if (((index0->v[1]-index1->v[1]) *
  168. (index0->v[0]-index2->v[0]) -
  169. (index0->v[0]-index1->v[0]) *
  170. (index0->v[1]-index2->v[1])) >= 0)
  171. {
  172. continue;
  173. }
  174. d_pcolormap = &((byte *)acolormap)[index0->v[4] & 0xFF00];
  175. if (ptri[i].facesfront)
  176. {
  177. D_PolysetRecursiveTriangle(index0->v, index1->v, index2->v);
  178. }
  179. else
  180. {
  181. int s0, s1, s2;
  182. s0 = index0->v[2];
  183. s1 = index1->v[2];
  184. s2 = index2->v[2];
  185. if (index0->flags & ALIAS_ONSEAM)
  186. index0->v[2] += r_affinetridesc.seamfixupX16;
  187. if (index1->flags & ALIAS_ONSEAM)
  188. index1->v[2] += r_affinetridesc.seamfixupX16;
  189. if (index2->flags & ALIAS_ONSEAM)
  190. index2->v[2] += r_affinetridesc.seamfixupX16;
  191. D_PolysetRecursiveTriangle(index0->v, index1->v, index2->v);
  192. index0->v[2] = s0;
  193. index1->v[2] = s1;
  194. index2->v[2] = s2;
  195. }
  196. }
  197. }
  198. /*
  199. ================
  200. D_DrawNonSubdiv
  201. ================
  202. */
  203. void D_DrawNonSubdiv (void)
  204. {
  205. mtriangle_t *ptri;
  206. finalvert_t *pfv, *index0, *index1, *index2;
  207. int i;
  208. int lnumtriangles;
  209. pfv = r_affinetridesc.pfinalverts;
  210. ptri = r_affinetridesc.ptriangles;
  211. lnumtriangles = r_affinetridesc.numtriangles;
  212. for (i=0 ; i<lnumtriangles ; i++, ptri++)
  213. {
  214. index0 = pfv + ptri->vertindex[0];
  215. index1 = pfv + ptri->vertindex[1];
  216. index2 = pfv + ptri->vertindex[2];
  217. d_xdenom = (index0->v[1]-index1->v[1]) *
  218. (index0->v[0]-index2->v[0]) -
  219. (index0->v[0]-index1->v[0])*(index0->v[1]-index2->v[1]);
  220. if (d_xdenom >= 0)
  221. {
  222. continue;
  223. }
  224. r_p0[0] = index0->v[0]; // u
  225. r_p0[1] = index0->v[1]; // v
  226. r_p0[2] = index0->v[2]; // s
  227. r_p0[3] = index0->v[3]; // t
  228. r_p0[4] = index0->v[4]; // light
  229. r_p0[5] = index0->v[5]; // iz
  230. r_p1[0] = index1->v[0];
  231. r_p1[1] = index1->v[1];
  232. r_p1[2] = index1->v[2];
  233. r_p1[3] = index1->v[3];
  234. r_p1[4] = index1->v[4];
  235. r_p1[5] = index1->v[5];
  236. r_p2[0] = index2->v[0];
  237. r_p2[1] = index2->v[1];
  238. r_p2[2] = index2->v[2];
  239. r_p2[3] = index2->v[3];
  240. r_p2[4] = index2->v[4];
  241. r_p2[5] = index2->v[5];
  242. if (!ptri->facesfront)
  243. {
  244. if (index0->flags & ALIAS_ONSEAM)
  245. r_p0[2] += r_affinetridesc.seamfixupX16;
  246. if (index1->flags & ALIAS_ONSEAM)
  247. r_p1[2] += r_affinetridesc.seamfixupX16;
  248. if (index2->flags & ALIAS_ONSEAM)
  249. r_p2[2] += r_affinetridesc.seamfixupX16;
  250. }
  251. D_PolysetSetEdgeTable ();
  252. D_RasterizeAliasPolySmooth ();
  253. }
  254. }
  255. /*
  256. ================
  257. D_PolysetRecursiveTriangle
  258. ================
  259. */
  260. void D_PolysetRecursiveTriangle (int *lp1, int *lp2, int *lp3)
  261. {
  262. int *temp;
  263. int d;
  264. int new[6];
  265. int z;
  266. short *zbuf;
  267. d = lp2[0] - lp1[0];
  268. if (d < -1 || d > 1)
  269. goto split;
  270. d = lp2[1] - lp1[1];
  271. if (d < -1 || d > 1)
  272. goto split;
  273. d = lp3[0] - lp2[0];
  274. if (d < -1 || d > 1)
  275. goto split2;
  276. d = lp3[1] - lp2[1];
  277. if (d < -1 || d > 1)
  278. goto split2;
  279. d = lp1[0] - lp3[0];
  280. if (d < -1 || d > 1)
  281. goto split3;
  282. d = lp1[1] - lp3[1];
  283. if (d < -1 || d > 1)
  284. {
  285. split3:
  286. temp = lp1;
  287. lp1 = lp3;
  288. lp3 = lp2;
  289. lp2 = temp;
  290. goto split;
  291. }
  292. return; // entire tri is filled
  293. split2:
  294. temp = lp1;
  295. lp1 = lp2;
  296. lp2 = lp3;
  297. lp3 = temp;
  298. split:
  299. // split this edge
  300. new[0] = (lp1[0] + lp2[0]) >> 1;
  301. new[1] = (lp1[1] + lp2[1]) >> 1;
  302. new[2] = (lp1[2] + lp2[2]) >> 1;
  303. new[3] = (lp1[3] + lp2[3]) >> 1;
  304. new[5] = (lp1[5] + lp2[5]) >> 1;
  305. // draw the point if splitting a leading edge
  306. if (lp2[1] > lp1[1])
  307. goto nodraw;
  308. if ((lp2[1] == lp1[1]) && (lp2[0] < lp1[0]))
  309. goto nodraw;
  310. z = new[5]>>16;
  311. zbuf = zspantable[new[1]] + new[0];
  312. if (z >= *zbuf)
  313. {
  314. int pix;
  315. *zbuf = z;
  316. pix = d_pcolormap[skintable[new[3]>>16][new[2]>>16]];
  317. d_viewbuffer[d_scantable[new[1]] + new[0]] = pix;
  318. }
  319. nodraw:
  320. // recursively continue
  321. D_PolysetRecursiveTriangle (lp3, lp1, new);
  322. D_PolysetRecursiveTriangle (lp3, new, lp2);
  323. }
  324. #endif // !id386
  325. /*
  326. ================
  327. D_PolysetUpdateTables
  328. ================
  329. */
  330. void D_PolysetUpdateTables (void)
  331. {
  332. int i;
  333. byte *s;
  334. if (r_affinetridesc.skinwidth != skinwidth ||
  335. r_affinetridesc.pskin != skinstart)
  336. {
  337. skinwidth = r_affinetridesc.skinwidth;
  338. skinstart = r_affinetridesc.pskin;
  339. s = skinstart;
  340. for (i=0 ; i<MAX_LBM_HEIGHT ; i++, s+=skinwidth)
  341. skintable[i] = s;
  342. }
  343. }
  344. #if !id386
  345. /*
  346. ===================
  347. D_PolysetScanLeftEdge
  348. ====================
  349. */
  350. void D_PolysetScanLeftEdge (int height)
  351. {
  352. do
  353. {
  354. d_pedgespanpackage->pdest = d_pdest;
  355. d_pedgespanpackage->pz = d_pz;
  356. d_pedgespanpackage->count = d_aspancount;
  357. d_pedgespanpackage->ptex = d_ptex;
  358. d_pedgespanpackage->sfrac = d_sfrac;
  359. d_pedgespanpackage->tfrac = d_tfrac;
  360. // FIXME: need to clamp l, s, t, at both ends?
  361. d_pedgespanpackage->light = d_light;
  362. d_pedgespanpackage->zi = d_zi;
  363. d_pedgespanpackage++;
  364. errorterm += erroradjustup;
  365. if (errorterm >= 0)
  366. {
  367. d_pdest += d_pdestextrastep;
  368. d_pz += d_pzextrastep;
  369. d_aspancount += d_countextrastep;
  370. d_ptex += d_ptexextrastep;
  371. d_sfrac += d_sfracextrastep;
  372. d_ptex += d_sfrac >> 16;
  373. d_sfrac &= 0xFFFF;
  374. d_tfrac += d_tfracextrastep;
  375. if (d_tfrac & 0x10000)
  376. {
  377. d_ptex += r_affinetridesc.skinwidth;
  378. d_tfrac &= 0xFFFF;
  379. }
  380. d_light += d_lightextrastep;
  381. d_zi += d_ziextrastep;
  382. errorterm -= erroradjustdown;
  383. }
  384. else
  385. {
  386. d_pdest += d_pdestbasestep;
  387. d_pz += d_pzbasestep;
  388. d_aspancount += ubasestep;
  389. d_ptex += d_ptexbasestep;
  390. d_sfrac += d_sfracbasestep;
  391. d_ptex += d_sfrac >> 16;
  392. d_sfrac &= 0xFFFF;
  393. d_tfrac += d_tfracbasestep;
  394. if (d_tfrac & 0x10000)
  395. {
  396. d_ptex += r_affinetridesc.skinwidth;
  397. d_tfrac &= 0xFFFF;
  398. }
  399. d_light += d_lightbasestep;
  400. d_zi += d_zibasestep;
  401. }
  402. } while (--height);
  403. }
  404. #endif // !id386
  405. /*
  406. ===================
  407. D_PolysetSetUpForLineScan
  408. ====================
  409. */
  410. void D_PolysetSetUpForLineScan(fixed8_t startvertu, fixed8_t startvertv,
  411. fixed8_t endvertu, fixed8_t endvertv)
  412. {
  413. double dm, dn;
  414. int tm, tn;
  415. adivtab_t *ptemp;
  416. // TODO: implement x86 version
  417. errorterm = -1;
  418. tm = endvertu - startvertu;
  419. tn = endvertv - startvertv;
  420. if (((tm <= 16) && (tm >= -15)) &&
  421. ((tn <= 16) && (tn >= -15)))
  422. {
  423. ptemp = &adivtab[((tm+15) << 5) + (tn+15)];
  424. ubasestep = ptemp->quotient;
  425. erroradjustup = ptemp->remainder;
  426. erroradjustdown = tn;
  427. }
  428. else
  429. {
  430. dm = (double)tm;
  431. dn = (double)tn;
  432. FloorDivMod (dm, dn, &ubasestep, &erroradjustup);
  433. erroradjustdown = dn;
  434. }
  435. }
  436. #if !id386
  437. /*
  438. ================
  439. D_PolysetCalcGradients
  440. ================
  441. */
  442. void D_PolysetCalcGradients (int skinwidth)
  443. {
  444. float xstepdenominv, ystepdenominv, t0, t1;
  445. float p01_minus_p21, p11_minus_p21, p00_minus_p20, p10_minus_p20;
  446. p00_minus_p20 = r_p0[0] - r_p2[0];
  447. p01_minus_p21 = r_p0[1] - r_p2[1];
  448. p10_minus_p20 = r_p1[0] - r_p2[0];
  449. p11_minus_p21 = r_p1[1] - r_p2[1];
  450. xstepdenominv = 1.0 / (float)d_xdenom;
  451. ystepdenominv = -xstepdenominv;
  452. // ceil () for light so positive steps are exaggerated, negative steps
  453. // diminished, pushing us away from underflow toward overflow. Underflow is
  454. // very visible, overflow is very unlikely, because of ambient lighting
  455. t0 = r_p0[4] - r_p2[4];
  456. t1 = r_p1[4] - r_p2[4];
  457. r_lstepx = (int)
  458. ceil((t1 * p01_minus_p21 - t0 * p11_minus_p21) * xstepdenominv);
  459. r_lstepy = (int)
  460. ceil((t1 * p00_minus_p20 - t0 * p10_minus_p20) * ystepdenominv);
  461. t0 = r_p0[2] - r_p2[2];
  462. t1 = r_p1[2] - r_p2[2];
  463. r_sstepx = (int)((t1 * p01_minus_p21 - t0 * p11_minus_p21) *
  464. xstepdenominv);
  465. r_sstepy = (int)((t1 * p00_minus_p20 - t0* p10_minus_p20) *
  466. ystepdenominv);
  467. t0 = r_p0[3] - r_p2[3];
  468. t1 = r_p1[3] - r_p2[3];
  469. r_tstepx = (int)((t1 * p01_minus_p21 - t0 * p11_minus_p21) *
  470. xstepdenominv);
  471. r_tstepy = (int)((t1 * p00_minus_p20 - t0 * p10_minus_p20) *
  472. ystepdenominv);
  473. t0 = r_p0[5] - r_p2[5];
  474. t1 = r_p1[5] - r_p2[5];
  475. r_zistepx = (int)((t1 * p01_minus_p21 - t0 * p11_minus_p21) *
  476. xstepdenominv);
  477. r_zistepy = (int)((t1 * p00_minus_p20 - t0 * p10_minus_p20) *
  478. ystepdenominv);
  479. #if id386
  480. a_sstepxfrac = r_sstepx << 16;
  481. a_tstepxfrac = r_tstepx << 16;
  482. #else
  483. a_sstepxfrac = r_sstepx & 0xFFFF;
  484. a_tstepxfrac = r_tstepx & 0xFFFF;
  485. #endif
  486. a_ststepxwhole = skinwidth * (r_tstepx >> 16) + (r_sstepx >> 16);
  487. }
  488. #endif // !id386
  489. #if 0
  490. byte gelmap[256];
  491. void InitGel (byte *palette)
  492. {
  493. int i;
  494. int r;
  495. for (i=0 ; i<256 ; i++)
  496. {
  497. // r = (palette[i*3]>>4);
  498. r = (palette[i*3] + palette[i*3+1] + palette[i*3+2])/(16*3);
  499. gelmap[i] = /* 64 */ 0 + r;
  500. }
  501. }
  502. #endif
  503. #if !id386
  504. /*
  505. ================
  506. D_PolysetDrawSpans8
  507. ================
  508. */
  509. void D_PolysetDrawSpans8 (spanpackage_t *pspanpackage)
  510. {
  511. int lcount;
  512. byte *lpdest;
  513. byte *lptex;
  514. int lsfrac, ltfrac;
  515. int llight;
  516. int lzi;
  517. short *lpz;
  518. do
  519. {
  520. lcount = d_aspancount - pspanpackage->count;
  521. errorterm += erroradjustup;
  522. if (errorterm >= 0)
  523. {
  524. d_aspancount += d_countextrastep;
  525. errorterm -= erroradjustdown;
  526. }
  527. else
  528. {
  529. d_aspancount += ubasestep;
  530. }
  531. if (lcount)
  532. {
  533. lpdest = pspanpackage->pdest;
  534. lptex = pspanpackage->ptex;
  535. lpz = pspanpackage->pz;
  536. lsfrac = pspanpackage->sfrac;
  537. ltfrac = pspanpackage->tfrac;
  538. llight = pspanpackage->light;
  539. lzi = pspanpackage->zi;
  540. do
  541. {
  542. if ((lzi >> 16) >= *lpz)
  543. {
  544. *lpdest = ((byte *)acolormap)[*lptex + (llight & 0xFF00)];
  545. // gel mapping *lpdest = gelmap[*lpdest];
  546. *lpz = lzi >> 16;
  547. }
  548. lpdest++;
  549. lzi += r_zistepx;
  550. lpz++;
  551. llight += r_lstepx;
  552. lptex += a_ststepxwhole;
  553. lsfrac += a_sstepxfrac;
  554. lptex += lsfrac >> 16;
  555. lsfrac &= 0xFFFF;
  556. ltfrac += a_tstepxfrac;
  557. if (ltfrac & 0x10000)
  558. {
  559. lptex += r_affinetridesc.skinwidth;
  560. ltfrac &= 0xFFFF;
  561. }
  562. } while (--lcount);
  563. }
  564. pspanpackage++;
  565. } while (pspanpackage->count != -999999);
  566. }
  567. #endif // !id386
  568. /*
  569. ================
  570. D_PolysetFillSpans8
  571. ================
  572. */
  573. void D_PolysetFillSpans8 (spanpackage_t *pspanpackage)
  574. {
  575. int color;
  576. // FIXME: do z buffering
  577. color = d_aflatcolor++;
  578. while (1)
  579. {
  580. int lcount;
  581. byte *lpdest;
  582. lcount = pspanpackage->count;
  583. if (lcount == -1)
  584. return;
  585. if (lcount)
  586. {
  587. lpdest = pspanpackage->pdest;
  588. do
  589. {
  590. *lpdest++ = color;
  591. } while (--lcount);
  592. }
  593. pspanpackage++;
  594. }
  595. }
  596. /*
  597. ================
  598. D_RasterizeAliasPolySmooth
  599. ================
  600. */
  601. void D_RasterizeAliasPolySmooth (void)
  602. {
  603. int initialleftheight, initialrightheight;
  604. int *plefttop, *prighttop, *pleftbottom, *prightbottom;
  605. int working_lstepx, originalcount;
  606. plefttop = pedgetable->pleftedgevert0;
  607. prighttop = pedgetable->prightedgevert0;
  608. pleftbottom = pedgetable->pleftedgevert1;
  609. prightbottom = pedgetable->prightedgevert1;
  610. initialleftheight = pleftbottom[1] - plefttop[1];
  611. initialrightheight = prightbottom[1] - prighttop[1];
  612. //
  613. // set the s, t, and light gradients, which are consistent across the triangle
  614. // because being a triangle, things are affine
  615. //
  616. D_PolysetCalcGradients (r_affinetridesc.skinwidth);
  617. //
  618. // rasterize the polygon
  619. //
  620. //
  621. // scan out the top (and possibly only) part of the left edge
  622. //
  623. d_pedgespanpackage = a_spans;
  624. ystart = plefttop[1];
  625. d_aspancount = plefttop[0] - prighttop[0];
  626. d_ptex = (byte *)r_affinetridesc.pskin + (plefttop[2] >> 16) +
  627. (plefttop[3] >> 16) * r_affinetridesc.skinwidth;
  628. #if id386
  629. d_sfrac = (plefttop[2] & 0xFFFF) << 16;
  630. d_tfrac = (plefttop[3] & 0xFFFF) << 16;
  631. #else
  632. d_sfrac = plefttop[2] & 0xFFFF;
  633. d_tfrac = plefttop[3] & 0xFFFF;
  634. #endif
  635. d_light = plefttop[4];
  636. d_zi = plefttop[5];
  637. d_pdest = (byte *)d_viewbuffer +
  638. ystart * screenwidth + plefttop[0];
  639. d_pz = d_pzbuffer + ystart * d_zwidth + plefttop[0];
  640. if (initialleftheight == 1)
  641. {
  642. d_pedgespanpackage->pdest = d_pdest;
  643. d_pedgespanpackage->pz = d_pz;
  644. d_pedgespanpackage->count = d_aspancount;
  645. d_pedgespanpackage->ptex = d_ptex;
  646. d_pedgespanpackage->sfrac = d_sfrac;
  647. d_pedgespanpackage->tfrac = d_tfrac;
  648. // FIXME: need to clamp l, s, t, at both ends?
  649. d_pedgespanpackage->light = d_light;
  650. d_pedgespanpackage->zi = d_zi;
  651. d_pedgespanpackage++;
  652. }
  653. else
  654. {
  655. D_PolysetSetUpForLineScan(plefttop[0], plefttop[1],
  656. pleftbottom[0], pleftbottom[1]);
  657. #if id386
  658. d_pzbasestep = (d_zwidth + ubasestep) << 1;
  659. d_pzextrastep = d_pzbasestep + 2;
  660. #else
  661. d_pzbasestep = d_zwidth + ubasestep;
  662. d_pzextrastep = d_pzbasestep + 1;
  663. #endif
  664. d_pdestbasestep = screenwidth + ubasestep;
  665. d_pdestextrastep = d_pdestbasestep + 1;
  666. // TODO: can reuse partial expressions here
  667. // for negative steps in x along left edge, bias toward overflow rather than
  668. // underflow (sort of turning the floor () we did in the gradient calcs into
  669. // ceil (), but plus a little bit)
  670. if (ubasestep < 0)
  671. working_lstepx = r_lstepx - 1;
  672. else
  673. working_lstepx = r_lstepx;
  674. d_countextrastep = ubasestep + 1;
  675. d_ptexbasestep = ((r_sstepy + r_sstepx * ubasestep) >> 16) +
  676. ((r_tstepy + r_tstepx * ubasestep) >> 16) *
  677. r_affinetridesc.skinwidth;
  678. #if id386
  679. d_sfracbasestep = (r_sstepy + r_sstepx * ubasestep) << 16;
  680. d_tfracbasestep = (r_tstepy + r_tstepx * ubasestep) << 16;
  681. #else
  682. d_sfracbasestep = (r_sstepy + r_sstepx * ubasestep) & 0xFFFF;
  683. d_tfracbasestep = (r_tstepy + r_tstepx * ubasestep) & 0xFFFF;
  684. #endif
  685. d_lightbasestep = r_lstepy + working_lstepx * ubasestep;
  686. d_zibasestep = r_zistepy + r_zistepx * ubasestep;
  687. d_ptexextrastep = ((r_sstepy + r_sstepx * d_countextrastep) >> 16) +
  688. ((r_tstepy + r_tstepx * d_countextrastep) >> 16) *
  689. r_affinetridesc.skinwidth;
  690. #if id386
  691. d_sfracextrastep = (r_sstepy + r_sstepx*d_countextrastep) << 16;
  692. d_tfracextrastep = (r_tstepy + r_tstepx*d_countextrastep) << 16;
  693. #else
  694. d_sfracextrastep = (r_sstepy + r_sstepx*d_countextrastep) & 0xFFFF;
  695. d_tfracextrastep = (r_tstepy + r_tstepx*d_countextrastep) & 0xFFFF;
  696. #endif
  697. d_lightextrastep = d_lightbasestep + working_lstepx;
  698. d_ziextrastep = d_zibasestep + r_zistepx;
  699. D_PolysetScanLeftEdge (initialleftheight);
  700. }
  701. //
  702. // scan out the bottom part of the left edge, if it exists
  703. //
  704. if (pedgetable->numleftedges == 2)
  705. {
  706. int height;
  707. plefttop = pleftbottom;
  708. pleftbottom = pedgetable->pleftedgevert2;
  709. height = pleftbottom[1] - plefttop[1];
  710. // TODO: make this a function; modularize this function in general
  711. ystart = plefttop[1];
  712. d_aspancount = plefttop[0] - prighttop[0];
  713. d_ptex = (byte *)r_affinetridesc.pskin + (plefttop[2] >> 16) +
  714. (plefttop[3] >> 16) * r_affinetridesc.skinwidth;
  715. d_sfrac = 0;
  716. d_tfrac = 0;
  717. d_light = plefttop[4];
  718. d_zi = plefttop[5];
  719. d_pdest = (byte *)d_viewbuffer + ystart * screenwidth + plefttop[0];
  720. d_pz = d_pzbuffer + ystart * d_zwidth + plefttop[0];
  721. if (height == 1)
  722. {
  723. d_pedgespanpackage->pdest = d_pdest;
  724. d_pedgespanpackage->pz = d_pz;
  725. d_pedgespanpackage->count = d_aspancount;
  726. d_pedgespanpackage->ptex = d_ptex;
  727. d_pedgespanpackage->sfrac = d_sfrac;
  728. d_pedgespanpackage->tfrac = d_tfrac;
  729. // FIXME: need to clamp l, s, t, at both ends?
  730. d_pedgespanpackage->light = d_light;
  731. d_pedgespanpackage->zi = d_zi;
  732. d_pedgespanpackage++;
  733. }
  734. else
  735. {
  736. D_PolysetSetUpForLineScan(plefttop[0], plefttop[1],
  737. pleftbottom[0], pleftbottom[1]);
  738. d_pdestbasestep = screenwidth + ubasestep;
  739. d_pdestextrastep = d_pdestbasestep + 1;
  740. #if id386
  741. d_pzbasestep = (d_zwidth + ubasestep) << 1;
  742. d_pzextrastep = d_pzbasestep + 2;
  743. #else
  744. d_pzbasestep = d_zwidth + ubasestep;
  745. d_pzextrastep = d_pzbasestep + 1;
  746. #endif
  747. if (ubasestep < 0)
  748. working_lstepx = r_lstepx - 1;
  749. else
  750. working_lstepx = r_lstepx;
  751. d_countextrastep = ubasestep + 1;
  752. d_ptexbasestep = ((r_sstepy + r_sstepx * ubasestep) >> 16) +
  753. ((r_tstepy + r_tstepx * ubasestep) >> 16) *
  754. r_affinetridesc.skinwidth;
  755. #if id386
  756. d_sfracbasestep = (r_sstepy + r_sstepx * ubasestep) << 16;
  757. d_tfracbasestep = (r_tstepy + r_tstepx * ubasestep) << 16;
  758. #else
  759. d_sfracbasestep = (r_sstepy + r_sstepx * ubasestep) & 0xFFFF;
  760. d_tfracbasestep = (r_tstepy + r_tstepx * ubasestep) & 0xFFFF;
  761. #endif
  762. d_lightbasestep = r_lstepy + working_lstepx * ubasestep;
  763. d_zibasestep = r_zistepy + r_zistepx * ubasestep;
  764. d_ptexextrastep = ((r_sstepy + r_sstepx * d_countextrastep) >> 16) +
  765. ((r_tstepy + r_tstepx * d_countextrastep) >> 16) *
  766. r_affinetridesc.skinwidth;
  767. #if id386
  768. d_sfracextrastep = ((r_sstepy+r_sstepx*d_countextrastep) & 0xFFFF)<<16;
  769. d_tfracextrastep = ((r_tstepy+r_tstepx*d_countextrastep) & 0xFFFF)<<16;
  770. #else
  771. d_sfracextrastep = (r_sstepy+r_sstepx*d_countextrastep) & 0xFFFF;
  772. d_tfracextrastep = (r_tstepy+r_tstepx*d_countextrastep) & 0xFFFF;
  773. #endif
  774. d_lightextrastep = d_lightbasestep + working_lstepx;
  775. d_ziextrastep = d_zibasestep + r_zistepx;
  776. D_PolysetScanLeftEdge (height);
  777. }
  778. }
  779. // scan out the top (and possibly only) part of the right edge, updating the
  780. // count field
  781. d_pedgespanpackage = a_spans;
  782. D_PolysetSetUpForLineScan(prighttop[0], prighttop[1],
  783. prightbottom[0], prightbottom[1]);
  784. d_aspancount = 0;
  785. d_countextrastep = ubasestep + 1;
  786. originalcount = a_spans[initialrightheight].count;
  787. a_spans[initialrightheight].count = -999999; // mark end of the spanpackages
  788. D_PolysetDrawSpans8 (a_spans);
  789. // scan out the bottom part of the right edge, if it exists
  790. if (pedgetable->numrightedges == 2)
  791. {
  792. int height;
  793. spanpackage_t *pstart;
  794. pstart = a_spans + initialrightheight;
  795. pstart->count = originalcount;
  796. d_aspancount = prightbottom[0] - prighttop[0];
  797. prighttop = prightbottom;
  798. prightbottom = pedgetable->prightedgevert2;
  799. height = prightbottom[1] - prighttop[1];
  800. D_PolysetSetUpForLineScan(prighttop[0], prighttop[1],
  801. prightbottom[0], prightbottom[1]);
  802. d_countextrastep = ubasestep + 1;
  803. a_spans[initialrightheight + height].count = -999999;
  804. // mark end of the spanpackages
  805. D_PolysetDrawSpans8 (pstart);
  806. }
  807. }
  808. /*
  809. ================
  810. D_PolysetSetEdgeTable
  811. ================
  812. */
  813. void D_PolysetSetEdgeTable (void)
  814. {
  815. int edgetableindex;
  816. edgetableindex = 0; // assume the vertices are already in
  817. // top to bottom order
  818. //
  819. // determine which edges are right & left, and the order in which
  820. // to rasterize them
  821. //
  822. if (r_p0[1] >= r_p1[1])
  823. {
  824. if (r_p0[1] == r_p1[1])
  825. {
  826. if (r_p0[1] < r_p2[1])
  827. pedgetable = &edgetables[2];
  828. else
  829. pedgetable = &edgetables[5];
  830. return;
  831. }
  832. else
  833. {
  834. edgetableindex = 1;
  835. }
  836. }
  837. if (r_p0[1] == r_p2[1])
  838. {
  839. if (edgetableindex)
  840. pedgetable = &edgetables[8];
  841. else
  842. pedgetable = &edgetables[9];
  843. return;
  844. }
  845. else if (r_p1[1] == r_p2[1])
  846. {
  847. if (edgetableindex)
  848. pedgetable = &edgetables[10];
  849. else
  850. pedgetable = &edgetables[11];
  851. return;
  852. }
  853. if (r_p0[1] > r_p2[1])
  854. edgetableindex += 2;
  855. if (r_p1[1] > r_p2[1])
  856. edgetableindex += 4;
  857. pedgetable = &edgetables[edgetableindex];
  858. }
  859. #if 0
  860. void D_PolysetRecursiveDrawLine (int *lp1, int *lp2)
  861. {
  862. int d;
  863. int new[6];
  864. int ofs;
  865. d = lp2[0] - lp1[0];
  866. if (d < -1 || d > 1)
  867. goto split;
  868. d = lp2[1] - lp1[1];
  869. if (d < -1 || d > 1)
  870. goto split;
  871. return; // line is completed
  872. split:
  873. // split this edge
  874. new[0] = (lp1[0] + lp2[0]) >> 1;
  875. new[1] = (lp1[1] + lp2[1]) >> 1;
  876. new[5] = (lp1[5] + lp2[5]) >> 1;
  877. new[2] = (lp1[2] + lp2[2]) >> 1;
  878. new[3] = (lp1[3] + lp2[3]) >> 1;
  879. new[4] = (lp1[4] + lp2[4]) >> 1;
  880. // draw the point
  881. ofs = d_scantable[new[1]] + new[0];
  882. if (new[5] > d_pzbuffer[ofs])
  883. {
  884. int pix;
  885. d_pzbuffer[ofs] = new[5];
  886. pix = skintable[new[3]>>16][new[2]>>16];
  887. // pix = ((byte *)acolormap)[pix + (new[4] & 0xFF00)];
  888. d_viewbuffer[ofs] = pix;
  889. }
  890. // recursively continue
  891. D_PolysetRecursiveDrawLine (lp1, new);
  892. D_PolysetRecursiveDrawLine (new, lp2);
  893. }
  894. void D_PolysetRecursiveTriangle2 (int *lp1, int *lp2, int *lp3)
  895. {
  896. int d;
  897. int new[4];
  898. d = lp2[0] - lp1[0];
  899. if (d < -1 || d > 1)
  900. goto split;
  901. d = lp2[1] - lp1[1];
  902. if (d < -1 || d > 1)
  903. goto split;
  904. return;
  905. split:
  906. // split this edge
  907. new[0] = (lp1[0] + lp2[0]) >> 1;
  908. new[1] = (lp1[1] + lp2[1]) >> 1;
  909. new[5] = (lp1[5] + lp2[5]) >> 1;
  910. new[2] = (lp1[2] + lp2[2]) >> 1;
  911. new[3] = (lp1[3] + lp2[3]) >> 1;
  912. new[4] = (lp1[4] + lp2[4]) >> 1;
  913. D_PolysetRecursiveDrawLine (new, lp3);
  914. // recursively continue
  915. D_PolysetRecursiveTriangle (lp1, new, lp3);
  916. D_PolysetRecursiveTriangle (new, lp2, lp3);
  917. }
  918. #endif