d_polyse.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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. byte gelmap[256];
  490. void InitGel (byte *palette)
  491. {
  492. int i;
  493. int r;
  494. for (i=0 ; i<256 ; i++)
  495. {
  496. // r = (palette[i*3]>>4);
  497. r = (palette[i*3] + palette[i*3+1] + palette[i*3+2])/(16*3);
  498. gelmap[i] = /* 64 */ 0 + r;
  499. }
  500. }
  501. #if !id386
  502. /*
  503. ================
  504. D_PolysetDrawSpans8
  505. ================
  506. */
  507. void D_PolysetDrawSpans8 (spanpackage_t *pspanpackage)
  508. {
  509. int lcount;
  510. byte *lpdest;
  511. byte *lptex;
  512. int lsfrac, ltfrac;
  513. int llight;
  514. int lzi;
  515. short *lpz;
  516. do
  517. {
  518. lcount = d_aspancount - pspanpackage->count;
  519. errorterm += erroradjustup;
  520. if (errorterm >= 0)
  521. {
  522. d_aspancount += d_countextrastep;
  523. errorterm -= erroradjustdown;
  524. }
  525. else
  526. {
  527. d_aspancount += ubasestep;
  528. }
  529. if (lcount)
  530. {
  531. lpdest = pspanpackage->pdest;
  532. lptex = pspanpackage->ptex;
  533. lpz = pspanpackage->pz;
  534. lsfrac = pspanpackage->sfrac;
  535. ltfrac = pspanpackage->tfrac;
  536. llight = pspanpackage->light;
  537. lzi = pspanpackage->zi;
  538. do
  539. {
  540. if ((lzi >> 16) >= *lpz)
  541. {
  542. *lpdest = ((byte *)acolormap)[*lptex + (llight & 0xFF00)];
  543. // gel mapping *lpdest = gelmap[*lpdest];
  544. *lpz = lzi >> 16;
  545. }
  546. lpdest++;
  547. lzi += r_zistepx;
  548. lpz++;
  549. llight += r_lstepx;
  550. lptex += a_ststepxwhole;
  551. lsfrac += a_sstepxfrac;
  552. lptex += lsfrac >> 16;
  553. lsfrac &= 0xFFFF;
  554. ltfrac += a_tstepxfrac;
  555. if (ltfrac & 0x10000)
  556. {
  557. lptex += r_affinetridesc.skinwidth;
  558. ltfrac &= 0xFFFF;
  559. }
  560. } while (--lcount);
  561. }
  562. pspanpackage++;
  563. } while (pspanpackage->count != -999999);
  564. }
  565. #endif // !id386
  566. /*
  567. ================
  568. D_PolysetFillSpans8
  569. ================
  570. */
  571. void D_PolysetFillSpans8 (spanpackage_t *pspanpackage)
  572. {
  573. int color;
  574. // FIXME: do z buffering
  575. color = d_aflatcolor++;
  576. while (1)
  577. {
  578. int lcount;
  579. byte *lpdest;
  580. lcount = pspanpackage->count;
  581. if (lcount == -1)
  582. return;
  583. if (lcount)
  584. {
  585. lpdest = pspanpackage->pdest;
  586. do
  587. {
  588. *lpdest++ = color;
  589. } while (--lcount);
  590. }
  591. pspanpackage++;
  592. }
  593. }
  594. /*
  595. ================
  596. D_RasterizeAliasPolySmooth
  597. ================
  598. */
  599. void D_RasterizeAliasPolySmooth (void)
  600. {
  601. int initialleftheight, initialrightheight;
  602. int *plefttop, *prighttop, *pleftbottom, *prightbottom;
  603. int working_lstepx, originalcount;
  604. plefttop = pedgetable->pleftedgevert0;
  605. prighttop = pedgetable->prightedgevert0;
  606. pleftbottom = pedgetable->pleftedgevert1;
  607. prightbottom = pedgetable->prightedgevert1;
  608. initialleftheight = pleftbottom[1] - plefttop[1];
  609. initialrightheight = prightbottom[1] - prighttop[1];
  610. //
  611. // set the s, t, and light gradients, which are consistent across the triangle
  612. // because being a triangle, things are affine
  613. //
  614. D_PolysetCalcGradients (r_affinetridesc.skinwidth);
  615. //
  616. // rasterize the polygon
  617. //
  618. //
  619. // scan out the top (and possibly only) part of the left edge
  620. //
  621. D_PolysetSetUpForLineScan(plefttop[0], plefttop[1],
  622. pleftbottom[0], pleftbottom[1]);
  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. d_pzbasestep = (d_zwidth + ubasestep) << 1;
  632. d_pzextrastep = d_pzbasestep + 2;
  633. #else
  634. d_sfrac = plefttop[2] & 0xFFFF;
  635. d_tfrac = plefttop[3] & 0xFFFF;
  636. d_pzbasestep = d_zwidth + ubasestep;
  637. d_pzextrastep = d_pzbasestep + 1;
  638. #endif
  639. d_light = plefttop[4];
  640. d_zi = plefttop[5];
  641. d_pdestbasestep = screenwidth + ubasestep;
  642. d_pdestextrastep = d_pdestbasestep + 1;
  643. d_pdest = (byte *)d_viewbuffer +
  644. ystart * screenwidth + plefttop[0];
  645. d_pz = d_pzbuffer + ystart * d_zwidth + plefttop[0];
  646. // TODO: can reuse partial expressions here
  647. // for negative steps in x along left edge, bias toward overflow rather than
  648. // underflow (sort of turning the floor () we did in the gradient calcs into
  649. // ceil (), but plus a little bit)
  650. if (ubasestep < 0)
  651. working_lstepx = r_lstepx - 1;
  652. else
  653. working_lstepx = r_lstepx;
  654. d_countextrastep = ubasestep + 1;
  655. d_ptexbasestep = ((r_sstepy + r_sstepx * ubasestep) >> 16) +
  656. ((r_tstepy + r_tstepx * ubasestep) >> 16) *
  657. r_affinetridesc.skinwidth;
  658. #if id386
  659. d_sfracbasestep = (r_sstepy + r_sstepx * ubasestep) << 16;
  660. d_tfracbasestep = (r_tstepy + r_tstepx * ubasestep) << 16;
  661. #else
  662. d_sfracbasestep = (r_sstepy + r_sstepx * ubasestep) & 0xFFFF;
  663. d_tfracbasestep = (r_tstepy + r_tstepx * ubasestep) & 0xFFFF;
  664. #endif
  665. d_lightbasestep = r_lstepy + working_lstepx * ubasestep;
  666. d_zibasestep = r_zistepy + r_zistepx * ubasestep;
  667. d_ptexextrastep = ((r_sstepy + r_sstepx * d_countextrastep) >> 16) +
  668. ((r_tstepy + r_tstepx * d_countextrastep) >> 16) *
  669. r_affinetridesc.skinwidth;
  670. #if id386
  671. d_sfracextrastep = (r_sstepy + r_sstepx*d_countextrastep) << 16;
  672. d_tfracextrastep = (r_tstepy + r_tstepx*d_countextrastep) << 16;
  673. #else
  674. d_sfracextrastep = (r_sstepy + r_sstepx*d_countextrastep) & 0xFFFF;
  675. d_tfracextrastep = (r_tstepy + r_tstepx*d_countextrastep) & 0xFFFF;
  676. #endif
  677. d_lightextrastep = d_lightbasestep + working_lstepx;
  678. d_ziextrastep = d_zibasestep + r_zistepx;
  679. D_PolysetScanLeftEdge (initialleftheight);
  680. //
  681. // scan out the bottom part of the left edge, if it exists
  682. //
  683. if (pedgetable->numleftedges == 2)
  684. {
  685. int height;
  686. plefttop = pleftbottom;
  687. pleftbottom = pedgetable->pleftedgevert2;
  688. D_PolysetSetUpForLineScan(plefttop[0], plefttop[1],
  689. pleftbottom[0], pleftbottom[1]);
  690. height = pleftbottom[1] - plefttop[1];
  691. // TODO: make this a function; modularize this function in general
  692. ystart = plefttop[1];
  693. d_aspancount = plefttop[0] - prighttop[0];
  694. d_ptex = (byte *)r_affinetridesc.pskin + (plefttop[2] >> 16) +
  695. (plefttop[3] >> 16) * r_affinetridesc.skinwidth;
  696. d_sfrac = 0;
  697. d_tfrac = 0;
  698. d_light = plefttop[4];
  699. d_zi = plefttop[5];
  700. d_pdestbasestep = screenwidth + ubasestep;
  701. d_pdestextrastep = d_pdestbasestep + 1;
  702. d_pdest = (byte *)d_viewbuffer + ystart * screenwidth + plefttop[0];
  703. #if id386
  704. d_pzbasestep = (d_zwidth + ubasestep) << 1;
  705. d_pzextrastep = d_pzbasestep + 2;
  706. #else
  707. d_pzbasestep = d_zwidth + ubasestep;
  708. d_pzextrastep = d_pzbasestep + 1;
  709. #endif
  710. d_pz = d_pzbuffer + ystart * d_zwidth + plefttop[0];
  711. if (ubasestep < 0)
  712. working_lstepx = r_lstepx - 1;
  713. else
  714. working_lstepx = r_lstepx;
  715. d_countextrastep = ubasestep + 1;
  716. d_ptexbasestep = ((r_sstepy + r_sstepx * ubasestep) >> 16) +
  717. ((r_tstepy + r_tstepx * ubasestep) >> 16) *
  718. r_affinetridesc.skinwidth;
  719. #if id386
  720. d_sfracbasestep = (r_sstepy + r_sstepx * ubasestep) << 16;
  721. d_tfracbasestep = (r_tstepy + r_tstepx * ubasestep) << 16;
  722. #else
  723. d_sfracbasestep = (r_sstepy + r_sstepx * ubasestep) & 0xFFFF;
  724. d_tfracbasestep = (r_tstepy + r_tstepx * ubasestep) & 0xFFFF;
  725. #endif
  726. d_lightbasestep = r_lstepy + working_lstepx * ubasestep;
  727. d_zibasestep = r_zistepy + r_zistepx * ubasestep;
  728. d_ptexextrastep = ((r_sstepy + r_sstepx * d_countextrastep) >> 16) +
  729. ((r_tstepy + r_tstepx * d_countextrastep) >> 16) *
  730. r_affinetridesc.skinwidth;
  731. #if id386
  732. d_sfracextrastep = ((r_sstepy+r_sstepx*d_countextrastep) & 0xFFFF)<<16;
  733. d_tfracextrastep = ((r_tstepy+r_tstepx*d_countextrastep) & 0xFFFF)<<16;
  734. #else
  735. d_sfracextrastep = (r_sstepy+r_sstepx*d_countextrastep) & 0xFFFF;
  736. d_tfracextrastep = (r_tstepy+r_tstepx*d_countextrastep) & 0xFFFF;
  737. #endif
  738. d_lightextrastep = d_lightbasestep + working_lstepx;
  739. d_ziextrastep = d_zibasestep + r_zistepx;
  740. D_PolysetScanLeftEdge (height);
  741. }
  742. // scan out the top (and possibly only) part of the right edge, updating the
  743. // count field
  744. d_pedgespanpackage = a_spans;
  745. D_PolysetSetUpForLineScan(prighttop[0], prighttop[1],
  746. prightbottom[0], prightbottom[1]);
  747. d_aspancount = 0;
  748. d_countextrastep = ubasestep + 1;
  749. originalcount = a_spans[initialrightheight].count;
  750. a_spans[initialrightheight].count = -999999; // mark end of the spanpackages
  751. D_PolysetDrawSpans8 (a_spans);
  752. // scan out the bottom part of the right edge, if it exists
  753. if (pedgetable->numrightedges == 2)
  754. {
  755. int height;
  756. spanpackage_t *pstart;
  757. pstart = a_spans + initialrightheight;
  758. pstart->count = originalcount;
  759. d_aspancount = prightbottom[0] - prighttop[0];
  760. prighttop = prightbottom;
  761. prightbottom = pedgetable->prightedgevert2;
  762. height = prightbottom[1] - prighttop[1];
  763. D_PolysetSetUpForLineScan(prighttop[0], prighttop[1],
  764. prightbottom[0], prightbottom[1]);
  765. d_countextrastep = ubasestep + 1;
  766. a_spans[initialrightheight + height].count = -999999;
  767. // mark end of the spanpackages
  768. D_PolysetDrawSpans8 (pstart);
  769. }
  770. }
  771. /*
  772. ================
  773. D_PolysetSetEdgeTable
  774. ================
  775. */
  776. void D_PolysetSetEdgeTable (void)
  777. {
  778. int edgetableindex;
  779. edgetableindex = 0; // assume the vertices are already in
  780. // top to bottom order
  781. //
  782. // determine which edges are right & left, and the order in which
  783. // to rasterize them
  784. //
  785. if (r_p0[1] >= r_p1[1])
  786. {
  787. if (r_p0[1] == r_p1[1])
  788. {
  789. if (r_p0[1] < r_p2[1])
  790. pedgetable = &edgetables[2];
  791. else
  792. pedgetable = &edgetables[5];
  793. return;
  794. }
  795. else
  796. {
  797. edgetableindex = 1;
  798. }
  799. }
  800. if (r_p0[1] == r_p2[1])
  801. {
  802. if (edgetableindex)
  803. pedgetable = &edgetables[8];
  804. else
  805. pedgetable = &edgetables[9];
  806. return;
  807. }
  808. else if (r_p1[1] == r_p2[1])
  809. {
  810. if (edgetableindex)
  811. pedgetable = &edgetables[10];
  812. else
  813. pedgetable = &edgetables[11];
  814. return;
  815. }
  816. if (r_p0[1] > r_p2[1])
  817. edgetableindex += 2;
  818. if (r_p1[1] > r_p2[1])
  819. edgetableindex += 4;
  820. pedgetable = &edgetables[edgetableindex];
  821. }
  822. #if 0
  823. void D_PolysetRecursiveDrawLine (int *lp1, int *lp2)
  824. {
  825. int d;
  826. int new[6];
  827. int ofs;
  828. d = lp2[0] - lp1[0];
  829. if (d < -1 || d > 1)
  830. goto split;
  831. d = lp2[1] - lp1[1];
  832. if (d < -1 || d > 1)
  833. goto split;
  834. return; // line is completed
  835. split:
  836. // split this edge
  837. new[0] = (lp1[0] + lp2[0]) >> 1;
  838. new[1] = (lp1[1] + lp2[1]) >> 1;
  839. new[5] = (lp1[5] + lp2[5]) >> 1;
  840. new[2] = (lp1[2] + lp2[2]) >> 1;
  841. new[3] = (lp1[3] + lp2[3]) >> 1;
  842. new[4] = (lp1[4] + lp2[4]) >> 1;
  843. // draw the point
  844. ofs = d_scantable[new[1]] + new[0];
  845. if (new[5] > d_pzbuffer[ofs])
  846. {
  847. int pix;
  848. d_pzbuffer[ofs] = new[5];
  849. pix = skintable[new[3]>>16][new[2]>>16];
  850. // pix = ((byte *)acolormap)[pix + (new[4] & 0xFF00)];
  851. d_viewbuffer[ofs] = pix;
  852. }
  853. // recursively continue
  854. D_PolysetRecursiveDrawLine (lp1, new);
  855. D_PolysetRecursiveDrawLine (new, lp2);
  856. }
  857. void D_PolysetRecursiveTriangle2 (int *lp1, int *lp2, int *lp3)
  858. {
  859. int d;
  860. int new[4];
  861. d = lp2[0] - lp1[0];
  862. if (d < -1 || d > 1)
  863. goto split;
  864. d = lp2[1] - lp1[1];
  865. if (d < -1 || d > 1)
  866. goto split;
  867. return;
  868. split:
  869. // split this edge
  870. new[0] = (lp1[0] + lp2[0]) >> 1;
  871. new[1] = (lp1[1] + lp2[1]) >> 1;
  872. new[5] = (lp1[5] + lp2[5]) >> 1;
  873. new[2] = (lp1[2] + lp2[2]) >> 1;
  874. new[3] = (lp1[3] + lp2[3]) >> 1;
  875. new[4] = (lp1[4] + lp2[4]) >> 1;
  876. D_PolysetRecursiveDrawLine (new, lp3);
  877. // recursively continue
  878. D_PolysetRecursiveTriangle (lp1, new, lp3);
  879. D_PolysetRecursiveTriangle (new, lp2, lp3);
  880. }
  881. #endif