1
0

PF_SOLID.C 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /******************************************************************************
  2. Plush Version 1.2
  3. pf_solid.c
  4. Solid Rasterizers
  5. Copyright (c) 1996-2000, Justin Frankel
  6. ******************************************************************************/
  7. #include "plush.h"
  8. #include "putface.h"
  9. void plPF_SolidF(pl_Cam *cam, pl_Face *TriFace) {
  10. pl_uChar i0, i1, i2;
  11. pl_uChar *gmem = cam->frameBuffer;
  12. pl_ZBuffer *zbuf = cam->zBuffer;
  13. pl_sInt32 X1, X2, dX1=0, dX2=0, XL1, XL2;
  14. pl_ZBuffer dZL=0, dZ1=0, dZ2=0, Z1, ZL, Z2, Z3;
  15. pl_sInt32 Y1, Y2, Y0, dY;
  16. pl_uChar stat;
  17. pl_Bool zb = (zbuf&&TriFace->Material->zBufferable) ? 1 : 0;
  18. pl_uChar bc;
  19. pl_sInt32 shade;
  20. PUTFACE_SORT();
  21. shade=(pl_sInt32) (TriFace->fShade*(TriFace->Material->_ColorsUsed-1));
  22. if (shade < 0) shade=0;
  23. if (shade > (pl_sInt32) TriFace->Material->_ColorsUsed-1) shade=TriFace->Material->_ColorsUsed-1;
  24. bc=TriFace->Material->_ReMapTable[shade];
  25. X2 = X1 = TriFace->Scrx[i0];
  26. Z1 = TriFace->Scrz[i0];
  27. Z2 = TriFace->Scrz[i1];
  28. Z3 = TriFace->Scrz[i2];
  29. Y0 = (TriFace->Scry[i0]+(1<<19)) >> 20;
  30. Y1 = (TriFace->Scry[i1]+(1<<19)) >> 20;
  31. Y2 = (TriFace->Scry[i2]+(1<<19)) >> 20;
  32. dY = Y2-Y0;
  33. if (dY) {
  34. dX2 = (TriFace->Scrx[i2] - X1) / dY;
  35. dZ2 = (Z3 - Z1) / dY;
  36. }
  37. dY = Y1-Y0;
  38. if (dY) {
  39. dX1 = (TriFace->Scrx[i1] - X1) / dY;
  40. dZ1 = (Z2 - Z1) / dY;
  41. if (dX2 < dX1) {
  42. dX2 ^= dX1; dX1 ^= dX2; dX2 ^= dX1;
  43. dZL = dZ1; dZ1 = dZ2; dZ2 = dZL;
  44. stat = 2;
  45. } else stat = 1;
  46. Z2 = Z1;
  47. } else {
  48. if (TriFace->Scrx[i1] > X1) {
  49. X2 = TriFace->Scrx[i1];
  50. stat = 2|4;
  51. } else {
  52. X1 = TriFace->Scrx[i1];
  53. ZL = Z1; Z1 = Z2; Z2 = ZL;
  54. stat = 1|8;
  55. }
  56. }
  57. if (zb) {
  58. XL1 = ((dX1-dX2)*dY+(1<<19))>>20;
  59. if (XL1) dZL = ((dZ1-dZ2)*dY)/XL1;
  60. else {
  61. XL1 = (X2-X1+(1<<19))>>20;
  62. if (zb && XL1) dZL = (Z2-Z1)/XL1;
  63. else dZL = 0.0;
  64. }
  65. }
  66. gmem += (Y0 * cam->ScreenWidth);
  67. zbuf += (Y0 * cam->ScreenWidth);
  68. while (Y0 < Y2) {
  69. if (Y0 == Y1) {
  70. dY = Y2 - ((TriFace->Scry[i1]+(1<<19))>>20);
  71. if (dY) {
  72. if (stat & 1) {
  73. X1 = TriFace->Scrx[i1];
  74. dX1 = (TriFace->Scrx[i2]-TriFace->Scrx[i1])/dY;
  75. }
  76. if (stat & 2) {
  77. X2 = TriFace->Scrx[i1];
  78. dX2 = (TriFace->Scrx[i2]-TriFace->Scrx[i1])/dY;
  79. }
  80. if (stat & 4) {
  81. X1 = TriFace->Scrx[i0];
  82. dX1 = (TriFace->Scrx[i2]-TriFace->Scrx[i0])/dY;
  83. }
  84. if (stat & 8) {
  85. X2 = TriFace->Scrx[i0];
  86. dX2 = (TriFace->Scrx[i2]-TriFace->Scrx[i0])/dY;
  87. }
  88. dZ1 = (Z3-Z1)/dY;
  89. }
  90. }
  91. XL1 = (X1+(1<<19))>>20;
  92. XL2 = (X2+(1<<19))>>20;
  93. ZL = Z1;
  94. XL2 -= XL1;
  95. if (XL2 > 0) {
  96. zbuf += XL1;
  97. gmem += XL1;
  98. XL1 += XL2;
  99. if (zb) do {
  100. if (*zbuf < ZL) {
  101. *zbuf = ZL;
  102. *gmem = bc;
  103. }
  104. gmem++;
  105. zbuf++;
  106. ZL += dZL;
  107. } while (--XL2);
  108. else do *gmem++ = bc; while (--XL2);
  109. gmem -= XL1;
  110. zbuf -= XL1;
  111. }
  112. gmem += cam->ScreenWidth;
  113. zbuf += cam->ScreenWidth;
  114. Z1 += dZ1;
  115. X1 += dX1;
  116. X2 += dX2;
  117. Y0++;
  118. }
  119. }
  120. void plPF_SolidG(pl_Cam *cam, pl_Face *TriFace) {
  121. pl_uChar i0, i1, i2;
  122. pl_uChar *gmem = cam->frameBuffer;
  123. pl_uChar *remap = TriFace->Material->_ReMapTable;
  124. pl_ZBuffer *zbuf = cam->zBuffer;
  125. pl_ZBuffer dZL=0, dZ1=0, dZ2=0, Z1, Z2, ZL, Z3;
  126. pl_sInt32 dX1=0, dX2=0, X1, X2, XL1, XL2;
  127. pl_sInt32 C1, C2, dC1=0, dC2=0, dCL=0, CL, C3;
  128. pl_sInt32 Y1, Y2, Y0, dY;
  129. pl_uChar stat;
  130. pl_Bool zb = (zbuf&&TriFace->Material->zBufferable) ? 1 : 0;
  131. pl_Float nc = (TriFace->Material->_ColorsUsed-1)*65536.0f;
  132. pl_sInt32 maxColor=((TriFace->Material->_ColorsUsed-1)<<16);
  133. pl_sInt32 maxColorNonShift=TriFace->Material->_ColorsUsed-1;
  134. PUTFACE_SORT();
  135. C1 = (pl_sInt32) (TriFace->Shades[i0]*nc);
  136. C2 = (pl_sInt32) (TriFace->Shades[i1]*nc);
  137. C3 = (pl_sInt32) (TriFace->Shades[i2]*nc);
  138. X2 = X1 = TriFace->Scrx[i0];
  139. Z1 = TriFace->Scrz[i0];
  140. Z2 = TriFace->Scrz[i1];
  141. Z3 = TriFace->Scrz[i2];
  142. Y0 = (TriFace->Scry[i0]+(1<<19))>>20;
  143. Y1 = (TriFace->Scry[i1]+(1<<19))>>20;
  144. Y2 = (TriFace->Scry[i2]+(1<<19))>>20;
  145. dY = Y2 - Y0;
  146. if (dY) {
  147. dX2 = (TriFace->Scrx[i2] - X1) / dY;
  148. dC2 = (C3 - C1) / dY;
  149. dZ2 = (Z3 - Z1) / dY;
  150. }
  151. dY = Y1 - Y0;
  152. if (dY) {
  153. dX1 = (TriFace->Scrx[i1] - X1) / dY;
  154. dC1 = (C2 - C1) / dY;
  155. dZ1 = (Z2 - Z1) / dY;
  156. if (dX2 < dX1) {
  157. dX2 ^= dX1; dX1 ^= dX2; dX2 ^= dX1;
  158. dC2 ^= dC1; dC1 ^= dC2; dC2 ^= dC1;
  159. dZL = dZ1; dZ1 = dZ2; dZ2 = dZL;
  160. stat = 2;
  161. } else stat = 1;
  162. Z2 = Z1;
  163. C2 = C1;
  164. } else {
  165. if (TriFace->Scrx[i1] > X1) {
  166. X2 = TriFace->Scrx[i1];
  167. stat = 2|4;
  168. } else {
  169. X1 = C1; C1 = C2; C2 = X1;
  170. ZL = Z1; Z1 = Z2; Z2 = ZL;
  171. X1 = TriFace->Scrx[i1];
  172. stat = 1|8;
  173. }
  174. }
  175. gmem += (Y0 * cam->ScreenWidth);
  176. zbuf += (Y0 * cam->ScreenWidth);
  177. XL1 = (((dX1-dX2)*dY+(1<<19))>>20);
  178. if (XL1) {
  179. dCL = ((dC1-dC2)*dY)/XL1;
  180. dZL = ((dZ1-dZ2)*dY)/XL1;
  181. } else {
  182. XL1 = ((X2-X1+(1<<19))>>20);
  183. if (XL1) {
  184. dCL = (C2-C1)/XL1;
  185. dZL = (Z2-Z1)/XL1;
  186. }
  187. }
  188. while (Y0 < Y2) {
  189. if (Y0 == Y1) {
  190. dY = Y2 - ((TriFace->Scry[i1]+(1<<19))>>20);
  191. if (dY) {
  192. dZ1 = (Z3-Z1)/dY;
  193. dC1 = (C3-C1) / dY;
  194. if (stat & 1) {
  195. X1 = TriFace->Scrx[i1];
  196. dX1 = (TriFace->Scrx[i2]-TriFace->Scrx[i1])/dY;
  197. }
  198. if (stat & 2) {
  199. X2 = TriFace->Scrx[i1];
  200. dX2 = (TriFace->Scrx[i2]-TriFace->Scrx[i1])/dY;
  201. }
  202. if (stat & 4) {
  203. X1 = TriFace->Scrx[i0];
  204. dX1 = (TriFace->Scrx[i2]-TriFace->Scrx[i0])/dY;
  205. }
  206. if (stat & 8) {
  207. X2 = TriFace->Scrx[i0];
  208. dX2 = (TriFace->Scrx[i2]-TriFace->Scrx[i0])/dY;
  209. }
  210. }
  211. }
  212. CL = C1;
  213. XL1 = (X1+(1<<19))>>20;
  214. XL2 = (X2+(1<<19))>>20;
  215. ZL = Z1;
  216. XL2 -= XL1;
  217. if (XL2 > 0) {
  218. gmem += XL1;
  219. zbuf += XL1;
  220. XL1 += XL2;
  221. if (zb) do {
  222. if (*zbuf < ZL) {
  223. *zbuf = ZL;
  224. if (CL >= maxColor) *gmem=remap[maxColorNonShift];
  225. else if (CL > 0) *gmem = remap[CL>>16];
  226. else *gmem = remap[0];
  227. }
  228. gmem++;
  229. zbuf++;
  230. ZL += dZL;
  231. CL += dCL;
  232. } while (--XL2);
  233. else do {
  234. if (CL >= maxColor) *gmem++=remap[maxColorNonShift];
  235. else if (CL > 0) *gmem++ = remap[CL>>16];
  236. else *gmem++ = remap[0];
  237. CL += dCL;
  238. } while (--XL2);
  239. gmem -= XL1;
  240. zbuf -= XL1;
  241. }
  242. gmem += cam->ScreenWidth;
  243. zbuf += cam->ScreenWidth;
  244. X1 += dX1;
  245. X2 += dX2;
  246. C1 += dC1;
  247. Z1 += dZ1;
  248. Y0++;
  249. }
  250. }