render.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. #import "qedefs.h"
  2. //define NOLIGHT
  3. vec3_t r_origin, r_matrix[3];
  4. int t_width, t_height;
  5. unsigned *t_data;
  6. int t_widthmask, t_heightmask, t_widthshift;
  7. float t_widthadd, t_heightadd;
  8. int r_width, r_height;
  9. float *r_zbuffer;
  10. unsigned *r_picbuffer;
  11. vec5_t rightside, leftside, rightstep,leftstep;
  12. face_t *r_face;
  13. BOOL r_drawflat;
  14. pixel32_t r_flatcolor;
  15. int sy[20];
  16. /*
  17. ====================
  18. REN_ClearBuffers
  19. ====================
  20. */
  21. void REN_ClearBuffers (void)
  22. {
  23. int size;
  24. size = r_width * r_height*4;
  25. memset (r_zbuffer, 0, size);
  26. memset (r_picbuffer, 0, size);
  27. }
  28. /*
  29. ====================
  30. REN_SetTexture
  31. ====================
  32. */
  33. void REN_SetTexture (face_t *face)
  34. {
  35. int i;
  36. int t_heightshift;
  37. qtexture_t *q;
  38. if (!face->qtexture)
  39. face->qtexture = TEX_ForName (face->texture.texture); // try to load
  40. q = face->qtexture;
  41. t_width = q->width;
  42. t_height = q->height;
  43. t_data = q->data;
  44. r_flatcolor = q->flatcolor;
  45. r_flatcolor.chan[0] *= r_face->light;
  46. r_flatcolor.chan[1] *= r_face->light;
  47. r_flatcolor.chan[2] *= r_face->light;
  48. t_widthadd = t_width*1024;
  49. t_heightadd = t_height*1024;
  50. t_widthmask = t_width-1;
  51. t_heightmask = t_height-1;
  52. t_widthshift = 0;
  53. i = t_width;
  54. while (i >= 2)
  55. {
  56. t_widthshift++;
  57. i>>=1;
  58. }
  59. t_heightshift = 0;
  60. i = t_width;
  61. while (i >= 2)
  62. {
  63. t_heightshift++;
  64. i>>=1;
  65. }
  66. if ( (1<<t_widthshift) != t_width || (1<<t_heightshift) != t_height)
  67. t_widthshift = t_heightshift = 0; // non power of two
  68. }
  69. /*
  70. ==================
  71. REN_DrawSpan
  72. ==================
  73. */
  74. void REN_DrawSpan (int y)
  75. {
  76. int x, count;
  77. int ofs;
  78. int tx, ty;
  79. int x1, x2;
  80. float ufrac, vfrac, zfrac, lightfrac, ustep, vstep, zstep;
  81. pixel32_t *in, *out;
  82. float scale;
  83. if (y<0 || y >= r_height)
  84. return;
  85. x1 = (leftside[0]);
  86. x2 = (rightside[0]);
  87. count = x2 - x1;
  88. if (count < 0)
  89. return;
  90. zfrac = leftside[2];
  91. ufrac = leftside[3];
  92. vfrac = leftside[4];
  93. lightfrac = r_face->light;
  94. if (!count)
  95. scale = 1;
  96. else
  97. scale = 1.0/count;
  98. zstep = (rightside[2] - zfrac)*scale;
  99. ustep = (rightside[3] - ufrac)*scale;
  100. vstep = (rightside[4] - vfrac)*scale;
  101. if (x1 < 0)
  102. {
  103. ufrac -= x1*ustep;
  104. vfrac -= x1*vstep;
  105. zfrac -= x1*zstep;
  106. x1 = 0;
  107. }
  108. if (x2 > r_width)
  109. x2 = r_width;
  110. ofs = y*r_width+x1;
  111. // this should be specialized for 1.0 / 0.5 / 0.75 light levels
  112. for (x=x1 ; x < x2 ; x++)
  113. {
  114. if (r_zbuffer[ofs] <= zfrac)
  115. {
  116. scale = 1/zfrac;
  117. r_zbuffer[ofs] = zfrac;
  118. if (t_widthshift)
  119. {
  120. tx = (int)((ufrac*scale)) & t_widthmask;
  121. ty = (int)((vfrac*scale)) & t_heightmask;
  122. in = (pixel32_t *)&t_data [(ty<<t_widthshift)+tx];
  123. }
  124. else
  125. {
  126. tx = (int)((ufrac*scale)+t_widthadd) % t_width;
  127. ty = (int)((vfrac*scale)+t_heightadd) % t_height;
  128. in = (pixel32_t *)&t_data [ty*t_width+tx];
  129. }
  130. out = (pixel32_t *)&r_picbuffer[ofs];
  131. #ifdef NOLIGHT
  132. *out = *in;
  133. #else
  134. out->chan[0] = in->chan[0]*lightfrac;
  135. out->chan[1] = in->chan[1]*lightfrac;
  136. out->chan[2] = in->chan[2]*lightfrac;
  137. out->chan[3] = 0xff;
  138. #endif
  139. }
  140. ufrac += ustep;
  141. vfrac += vstep;
  142. zfrac += zstep;
  143. ofs++;
  144. }
  145. }
  146. /*
  147. ==================
  148. REN_DrawFlatSpan
  149. ==================
  150. */
  151. void REN_DrawFlatSpan (int y)
  152. {
  153. int x, count;
  154. int ofs;
  155. int x1, x2;
  156. float zfrac, zstep;
  157. pixel32_t *out;
  158. if (y<0 || y >= r_height)
  159. return;
  160. x1 = (leftside[0]);
  161. x2 = (rightside[0]);
  162. count = x2 - x1;
  163. if (count < 0)
  164. return;
  165. zfrac = leftside[2];
  166. zstep = (rightside[2] - zfrac)/count;
  167. if (x1 < 0)
  168. {
  169. zfrac -= x1*zstep;
  170. x1 = 0;
  171. }
  172. if (x2 > r_width)
  173. x2 = r_width;
  174. ofs = y*r_width+x1;
  175. // this should be specialized for 1.0 / 0.5 / 0.75 light levels
  176. for (x=x1 ; x < x2 ; x++)
  177. {
  178. if (r_zbuffer[ofs] <= zfrac)
  179. {
  180. r_zbuffer[ofs] = zfrac;
  181. out = (pixel32_t *)&r_picbuffer[ofs];
  182. *out = r_flatcolor.p;
  183. }
  184. zfrac += zstep;
  185. ofs++;
  186. }
  187. }
  188. /*
  189. =====================
  190. REN_RasterizeFace
  191. =====================
  192. */
  193. void REN_RasterizeFace (winding_t *w)
  194. {
  195. int y;
  196. int i;
  197. int top, bot;
  198. int leftv, rightv;
  199. int count;
  200. int numvertex;
  201. //
  202. // find top vertex
  203. //
  204. numvertex = w->numpoints;
  205. top = 0x7fffffff;
  206. bot = 0x80000000;
  207. leftv = 0;
  208. for (i=0 ; i<numvertex ; i++)
  209. {
  210. w->points[i][3] *= w->points[i][2];
  211. w->points[i][4] *= w->points[i][2];
  212. sy[i] = (int)w->points[i][1];
  213. if (sy[i] < top)
  214. {
  215. top = sy[i];
  216. leftv = i;
  217. }
  218. if (sy[i] > bot)
  219. bot = sy[i];
  220. }
  221. rightv = leftv;
  222. if (top < 0 || bot > r_height || top > bot)
  223. return; // shouldn't have to have this...
  224. //
  225. // render a trapezoid
  226. //
  227. y = top;
  228. while (y < bot)
  229. {
  230. if (y >= sy[leftv])
  231. {
  232. do
  233. {
  234. for (i=0 ; i<5 ; i++)
  235. leftside[i] = w->points[leftv][i];
  236. leftv--;
  237. if (leftv == -1)
  238. leftv = numvertex-1;
  239. } while (sy[leftv] <= y);
  240. count = sy[leftv]-y;
  241. for (i=0 ; i<5 ; i++)
  242. leftstep[i] = (w->points[leftv][i] - leftside[i])/count;
  243. }
  244. if (y >= sy[rightv])
  245. {
  246. do
  247. {
  248. for (i=0 ; i<5 ; i++)
  249. rightside[i] = w->points[rightv][i];
  250. rightv++;
  251. if (rightv == numvertex)
  252. rightv = 0;
  253. } while (sy[rightv] <= y);
  254. count = sy[rightv]-y;
  255. for (i=0 ; i<5 ; i++)
  256. rightstep[i] = (w->points[rightv][i] - rightside[i])/count;
  257. }
  258. if (r_drawflat)
  259. REN_DrawFlatSpan (y);
  260. else
  261. REN_DrawSpan (y);
  262. for (i=0 ; i<5 ; i++)
  263. {
  264. leftside[i] += leftstep[i];
  265. rightside[i] += rightstep[i];
  266. }
  267. y++;
  268. }
  269. }
  270. //=============================================================================
  271. /*
  272. ==================
  273. REN_DrawSpanLinear
  274. ==================
  275. */
  276. void REN_DrawSpanLinear (int y)
  277. {
  278. int x, count;
  279. int ofs;
  280. int tx, ty;
  281. int x1, x2;
  282. float ufrac, vfrac, zfrac, ustep, vstep, zstep;
  283. pixel32_t *in, *out;
  284. float scale;
  285. if (y<0 || y >= r_height)
  286. return;
  287. x1 = (leftside[0]);
  288. x2 = (rightside[0]);
  289. count = x2 - x1;
  290. if (count < 0)
  291. return;
  292. zfrac = leftside[2];
  293. ufrac = leftside[3];
  294. vfrac = leftside[4];
  295. if (!count)
  296. scale = 1;
  297. else
  298. scale = 1.0/count;
  299. zstep = (rightside[2] - zfrac)*scale;
  300. ustep = (rightside[3] - ufrac)*scale;
  301. vstep = (rightside[4] - vfrac)*scale;
  302. if (x1 < 0)
  303. {
  304. ufrac -= x1*ustep;
  305. vfrac -= x1*vstep;
  306. zfrac -= x1*zstep;
  307. x1 = 0;
  308. }
  309. if (x2 > r_width)
  310. x2 = r_width;
  311. ofs = y*r_width+x1;
  312. for (x=x1 ; x < x2 ; x++)
  313. {
  314. if (r_zbuffer[ofs] <= zfrac)
  315. {
  316. r_zbuffer[ofs] = zfrac;
  317. if (t_widthshift)
  318. {
  319. tx = (int)ufrac & t_widthmask;
  320. ty = (int)vfrac & t_heightmask;
  321. in = (pixel32_t *)&t_data [(ty<<t_widthshift)+tx];
  322. }
  323. else
  324. {
  325. tx = (int)(ufrac+t_widthadd) % t_width;
  326. ty = (int)(vfrac+t_heightadd) % t_height;
  327. in = (pixel32_t *)&t_data [ty*t_width+tx];
  328. }
  329. out = (pixel32_t *)&r_picbuffer[ofs];
  330. *out = *in;
  331. }
  332. ufrac += ustep;
  333. vfrac += vstep;
  334. zfrac += zstep;
  335. ofs++;
  336. }
  337. }
  338. /*
  339. =====================
  340. REN_RasterizeFaceLinear
  341. =====================
  342. */
  343. void REN_RasterizeFaceLinear (winding_t *w)
  344. {
  345. int y;
  346. int i;
  347. int top, bot;
  348. int leftv, rightv;
  349. int count;
  350. int numvertex;
  351. //
  352. // find top vertex
  353. //
  354. numvertex = w->numpoints;
  355. top = 0x7fffffff;
  356. bot = 0x80000000;
  357. leftv = 0;
  358. for (i=0 ; i<numvertex ; i++)
  359. {
  360. sy[i] = (int)w->points[i][1];
  361. if (sy[i] < top)
  362. {
  363. top = sy[i];
  364. leftv = i;
  365. }
  366. if (sy[i] > bot)
  367. bot = sy[i];
  368. }
  369. rightv = leftv;
  370. if (top < 0 || bot > r_height || top > bot)
  371. return; // shouldn't have to have this...
  372. //
  373. // render a trapezoid
  374. //
  375. y = top;
  376. while (y < bot)
  377. {
  378. if (y >= sy[leftv])
  379. {
  380. do
  381. {
  382. for (i=0 ; i<5 ; i++)
  383. leftside[i] = w->points[leftv][i];
  384. leftv--;
  385. if (leftv == -1)
  386. leftv = numvertex-1;
  387. } while (sy[leftv] <= y);
  388. count = sy[leftv]-y;
  389. for (i=0 ; i<5 ; i++)
  390. leftstep[i] = (w->points[leftv][i] - leftside[i])/count;
  391. }
  392. if (y >= sy[rightv])
  393. {
  394. do
  395. {
  396. for (i=0 ; i<5 ; i++)
  397. rightside[i] = w->points[rightv][i];
  398. rightv++;
  399. if (rightv == numvertex)
  400. rightv = 0;
  401. } while (sy[rightv] <= y);
  402. count = sy[rightv]-y;
  403. for (i=0 ; i<5 ; i++)
  404. rightstep[i] = (w->points[rightv][i] - rightside[i])/count;
  405. }
  406. REN_DrawSpanLinear (y);
  407. for (i=0 ; i<5 ; i++)
  408. {
  409. leftside[i] += leftstep[i];
  410. rightside[i] += rightstep[i];
  411. }
  412. y++;
  413. }
  414. }
  415. //============================================================================
  416. /*
  417. ==================
  418. REN_BeginCamera
  419. ===================
  420. */
  421. float r_width_2, r_height_3;
  422. plane_t frustum[5];
  423. void REN_BeginCamera (void)
  424. {
  425. r_width_2 = (float)r_width / 2;
  426. r_height_3 = (float)r_height / 3;
  427. // clip to right side
  428. frustum[0].normal[0] = -1;
  429. frustum[0].normal[1] = 0;
  430. frustum[0].normal[2] = 1;
  431. frustum[0].dist = 0;
  432. // clip to left side
  433. frustum[1].normal[0] = 1;
  434. frustum[1].normal[1] = 0;
  435. frustum[1].normal[2] = 1;
  436. frustum[1].dist = 0;
  437. // clip to top side
  438. frustum[2].normal[0] = 0;
  439. frustum[2].normal[1] = -1;
  440. frustum[2].normal[2] = r_height_3 / r_width_2;
  441. frustum[2].dist = 0;
  442. // clip to bottom side
  443. frustum[3].normal[0] = 0;
  444. frustum[3].normal[1] = 1;
  445. frustum[3].normal[2] = 2*r_height_3 / r_width_2;
  446. frustum[3].dist = 0;
  447. // near Z
  448. frustum[4].normal[0] = 0;
  449. frustum[4].normal[1] = 0;
  450. frustum[4].normal[2] = 1;
  451. frustum[4].dist = 1;
  452. }
  453. void REN_BeginXY (void)
  454. {
  455. frustum[0].normal[0] = 1;
  456. frustum[0].normal[1] = 0;
  457. frustum[0].normal[2] = 0;
  458. frustum[0].dist = 0;
  459. frustum[1].normal[0] = -1;
  460. frustum[1].normal[1] = 0;
  461. frustum[1].normal[2] = 0;
  462. frustum[1].dist = -r_width;
  463. frustum[2].normal[0] = 0;
  464. frustum[2].normal[1] = 1;
  465. frustum[2].normal[2] = 0;
  466. frustum[2].dist = 0;
  467. frustum[3].normal[0] = 0;
  468. frustum[3].normal[1] = -1;
  469. frustum[3].normal[2] = 0;
  470. frustum[3].dist = -r_height;
  471. }
  472. /*
  473. =====================
  474. REN_DrawCameraFace
  475. =====================
  476. */
  477. void REN_DrawCameraFace (face_t *idpol)
  478. {
  479. int i;
  480. float scale;
  481. int numvertex;
  482. winding_t *w, *in;
  483. vec3_t temp;
  484. if (!idpol->w)
  485. return; // overconstrained plane
  486. r_face = idpol;
  487. //
  488. // back face cull
  489. //
  490. if (DotProduct (r_origin, idpol->plane.normal) <= idpol->plane.dist)
  491. return;
  492. //
  493. // transform in 3D (FIXME: clip first, then transform)
  494. //
  495. in = idpol->w;
  496. numvertex = in->numpoints;
  497. w = NewWinding (numvertex);
  498. w->numpoints = numvertex;
  499. for (i=0 ; i<numvertex ; i++)
  500. {
  501. VectorSubtract (in->points[i], r_origin, temp);
  502. w->points[i][0] = DotProduct(temp,r_matrix[0]);
  503. w->points[i][1] = DotProduct(temp,r_matrix[1]);
  504. w->points[i][2] = DotProduct(temp,r_matrix[2]);
  505. w->points[i][3] = in->points[i][3];
  506. w->points[i][4] = in->points[i][4];
  507. }
  508. //
  509. // 3D clip
  510. //
  511. for (i=0 ; i<4 ; i++)
  512. {
  513. w = ClipWinding (w, &frustum[i]);
  514. if (!w)
  515. return;
  516. }
  517. //
  518. // project to 2D
  519. //
  520. for (i=0 ; i<w->numpoints ; i++)
  521. {
  522. scale = r_width_2 / w->points[i][2];
  523. w->points[i][0] = r_width_2 + scale*w->points[i][0];
  524. w->points[i][1] = r_height_3 - scale*w->points[i][1];
  525. w->points[i][2] = scale;
  526. }
  527. //
  528. // draw it
  529. //
  530. REN_SetTexture (idpol);
  531. REN_RasterizeFace (w);
  532. free (w);
  533. }
  534. /*
  535. =====================
  536. REN_DrawXYFace
  537. =====================
  538. */
  539. void REN_DrawXYFace (face_t *idpol)
  540. {
  541. int i, j, numvertex;
  542. winding_t *w, *in;
  543. float *dest, *source;
  544. float temp;
  545. if (!idpol->w)
  546. return; // overconstrained plane
  547. w = idpol->w;
  548. r_face = idpol;
  549. //
  550. // back (and side) face cull
  551. //
  552. if (DotProduct (idpol->plane.normal, xy_viewnormal) > -VECTOR_EPSILON)
  553. return;
  554. //
  555. // transform
  556. //
  557. in = idpol->w;
  558. numvertex = in->numpoints;
  559. w = NewWinding (numvertex);
  560. w->numpoints = numvertex;
  561. for (i=0 ; i<numvertex ; i++)
  562. {
  563. // using Z as a scale for the 2D projection
  564. w->points[i][0] = (in->points[i][0] - r_origin[0])*r_origin[2];
  565. w->points[i][1] = r_height - (in->points[i][1] - r_origin[1])*r_origin[2];
  566. w->points[i][2] = in->points[i][2] + 3000;
  567. w->points[i][3] = in->points[i][3];
  568. w->points[i][4] = in->points[i][4];
  569. }
  570. //
  571. // clip
  572. //
  573. for (i=0 ; i<4 ; i++)
  574. {
  575. w = ClipWinding (w, &frustum[i]);
  576. if (!w)
  577. return;
  578. }
  579. //
  580. // project to 2D
  581. //
  582. for (i=0 ; i<w->numpoints ; i++)
  583. {
  584. dest = w->points[i];
  585. if (dest[0] < 0)
  586. dest[0] = 0;
  587. if (dest[0] > r_width)
  588. dest[0] = r_width;
  589. if (dest[1] < 0)
  590. dest[1] = 0;
  591. if (dest[1] > r_height)
  592. dest[1] = r_height;
  593. if (xy_viewnormal[2] > 0)
  594. dest[2] = 4096-dest[2];
  595. }
  596. if (xy_viewnormal[2] > 0)
  597. { // flip order when upside down
  598. for (i=0 ; i<w->numpoints/2 ; i++)
  599. {
  600. dest = w->points[i];
  601. source = w->points[w->numpoints-1-i];
  602. for (j=0 ; j<5 ; j++)
  603. {
  604. temp = dest[j];
  605. dest[j] = source[j];
  606. source[j] = temp;
  607. }
  608. }
  609. }
  610. REN_SetTexture (idpol);
  611. //
  612. // draw it
  613. //
  614. REN_RasterizeFaceLinear (w);
  615. free (w);
  616. }