r_aclip.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. // r_aclip.c: clip routines for drawing Alias models directly to the screen
  16. #include "quakedef.h"
  17. #include "r_local.h"
  18. #include "d_local.h"
  19. static finalvert_t fv[2][8];
  20. static auxvert_t av[8];
  21. void R_AliasProjectFinalVert (finalvert_t *fv, auxvert_t *av);
  22. void R_Alias_clip_top (finalvert_t *pfv0, finalvert_t *pfv1,
  23. finalvert_t *out);
  24. void R_Alias_clip_bottom (finalvert_t *pfv0, finalvert_t *pfv1,
  25. finalvert_t *out);
  26. void R_Alias_clip_left (finalvert_t *pfv0, finalvert_t *pfv1,
  27. finalvert_t *out);
  28. void R_Alias_clip_right (finalvert_t *pfv0, finalvert_t *pfv1,
  29. finalvert_t *out);
  30. /*
  31. ================
  32. R_Alias_clip_z
  33. pfv0 is the unclipped vertex, pfv1 is the z-clipped vertex
  34. ================
  35. */
  36. void R_Alias_clip_z (finalvert_t *pfv0, finalvert_t *pfv1, finalvert_t *out)
  37. {
  38. float scale;
  39. auxvert_t *pav0, *pav1, avout;
  40. pav0 = &av[pfv0 - &fv[0][0]];
  41. pav1 = &av[pfv1 - &fv[0][0]];
  42. if (pfv0->v[1] >= pfv1->v[1])
  43. {
  44. scale = (ALIAS_Z_CLIP_PLANE - pav0->fv[2]) /
  45. (pav1->fv[2] - pav0->fv[2]);
  46. avout.fv[0] = pav0->fv[0] + (pav1->fv[0] - pav0->fv[0]) * scale;
  47. avout.fv[1] = pav0->fv[1] + (pav1->fv[1] - pav0->fv[1]) * scale;
  48. avout.fv[2] = ALIAS_Z_CLIP_PLANE;
  49. out->v[2] = pfv0->v[2] + (pfv1->v[2] - pfv0->v[2]) * scale;
  50. out->v[3] = pfv0->v[3] + (pfv1->v[3] - pfv0->v[3]) * scale;
  51. out->v[4] = pfv0->v[4] + (pfv1->v[4] - pfv0->v[4]) * scale;
  52. }
  53. else
  54. {
  55. scale = (ALIAS_Z_CLIP_PLANE - pav1->fv[2]) /
  56. (pav0->fv[2] - pav1->fv[2]);
  57. avout.fv[0] = pav1->fv[0] + (pav0->fv[0] - pav1->fv[0]) * scale;
  58. avout.fv[1] = pav1->fv[1] + (pav0->fv[1] - pav1->fv[1]) * scale;
  59. avout.fv[2] = ALIAS_Z_CLIP_PLANE;
  60. out->v[2] = pfv1->v[2] + (pfv0->v[2] - pfv1->v[2]) * scale;
  61. out->v[3] = pfv1->v[3] + (pfv0->v[3] - pfv1->v[3]) * scale;
  62. out->v[4] = pfv1->v[4] + (pfv0->v[4] - pfv1->v[4]) * scale;
  63. }
  64. R_AliasProjectFinalVert (out, &avout);
  65. if (out->v[0] < r_refdef.aliasvrect.x)
  66. out->flags |= ALIAS_LEFT_CLIP;
  67. if (out->v[1] < r_refdef.aliasvrect.y)
  68. out->flags |= ALIAS_TOP_CLIP;
  69. if (out->v[0] > r_refdef.aliasvrectright)
  70. out->flags |= ALIAS_RIGHT_CLIP;
  71. if (out->v[1] > r_refdef.aliasvrectbottom)
  72. out->flags |= ALIAS_BOTTOM_CLIP;
  73. }
  74. #if !id386
  75. void R_Alias_clip_left (finalvert_t *pfv0, finalvert_t *pfv1, finalvert_t *out)
  76. {
  77. float scale;
  78. int i;
  79. if (pfv0->v[1] >= pfv1->v[1])
  80. {
  81. scale = (float)(r_refdef.aliasvrect.x - pfv0->v[0]) /
  82. (pfv1->v[0] - pfv0->v[0]);
  83. for (i=0 ; i<6 ; i++)
  84. out->v[i] = pfv0->v[i] + (pfv1->v[i] - pfv0->v[i])*scale + 0.5;
  85. }
  86. else
  87. {
  88. scale = (float)(r_refdef.aliasvrect.x - pfv1->v[0]) /
  89. (pfv0->v[0] - pfv1->v[0]);
  90. for (i=0 ; i<6 ; i++)
  91. out->v[i] = pfv1->v[i] + (pfv0->v[i] - pfv1->v[i])*scale + 0.5;
  92. }
  93. }
  94. void R_Alias_clip_right (finalvert_t *pfv0, finalvert_t *pfv1,
  95. finalvert_t *out)
  96. {
  97. float scale;
  98. int i;
  99. if (pfv0->v[1] >= pfv1->v[1])
  100. {
  101. scale = (float)(r_refdef.aliasvrectright - pfv0->v[0]) /
  102. (pfv1->v[0] - pfv0->v[0]);
  103. for (i=0 ; i<6 ; i++)
  104. out->v[i] = pfv0->v[i] + (pfv1->v[i] - pfv0->v[i])*scale + 0.5;
  105. }
  106. else
  107. {
  108. scale = (float)(r_refdef.aliasvrectright - pfv1->v[0]) /
  109. (pfv0->v[0] - pfv1->v[0]);
  110. for (i=0 ; i<6 ; i++)
  111. out->v[i] = pfv1->v[i] + (pfv0->v[i] - pfv1->v[i])*scale + 0.5;
  112. }
  113. }
  114. void R_Alias_clip_top (finalvert_t *pfv0, finalvert_t *pfv1, finalvert_t *out)
  115. {
  116. float scale;
  117. int i;
  118. if (pfv0->v[1] >= pfv1->v[1])
  119. {
  120. scale = (float)(r_refdef.aliasvrect.y - pfv0->v[1]) /
  121. (pfv1->v[1] - pfv0->v[1]);
  122. for (i=0 ; i<6 ; i++)
  123. out->v[i] = pfv0->v[i] + (pfv1->v[i] - pfv0->v[i])*scale + 0.5;
  124. }
  125. else
  126. {
  127. scale = (float)(r_refdef.aliasvrect.y - pfv1->v[1]) /
  128. (pfv0->v[1] - pfv1->v[1]);
  129. for (i=0 ; i<6 ; i++)
  130. out->v[i] = pfv1->v[i] + (pfv0->v[i] - pfv1->v[i])*scale + 0.5;
  131. }
  132. }
  133. void R_Alias_clip_bottom (finalvert_t *pfv0, finalvert_t *pfv1,
  134. finalvert_t *out)
  135. {
  136. float scale;
  137. int i;
  138. if (pfv0->v[1] >= pfv1->v[1])
  139. {
  140. scale = (float)(r_refdef.aliasvrectbottom - pfv0->v[1]) /
  141. (pfv1->v[1] - pfv0->v[1]);
  142. for (i=0 ; i<6 ; i++)
  143. out->v[i] = pfv0->v[i] + (pfv1->v[i] - pfv0->v[i])*scale + 0.5;
  144. }
  145. else
  146. {
  147. scale = (float)(r_refdef.aliasvrectbottom - pfv1->v[1]) /
  148. (pfv0->v[1] - pfv1->v[1]);
  149. for (i=0 ; i<6 ; i++)
  150. out->v[i] = pfv1->v[i] + (pfv0->v[i] - pfv1->v[i])*scale + 0.5;
  151. }
  152. }
  153. #endif
  154. int R_AliasClip (finalvert_t *in, finalvert_t *out, int flag, int count,
  155. void(*clip)(finalvert_t *pfv0, finalvert_t *pfv1, finalvert_t *out) )
  156. {
  157. int i,j,k;
  158. int flags, oldflags;
  159. j = count-1;
  160. k = 0;
  161. for (i=0 ; i<count ; j = i, i++)
  162. {
  163. oldflags = in[j].flags & flag;
  164. flags = in[i].flags & flag;
  165. if (flags && oldflags)
  166. continue;
  167. if (oldflags ^ flags)
  168. {
  169. clip (&in[j], &in[i], &out[k]);
  170. out[k].flags = 0;
  171. if (out[k].v[0] < r_refdef.aliasvrect.x)
  172. out[k].flags |= ALIAS_LEFT_CLIP;
  173. if (out[k].v[1] < r_refdef.aliasvrect.y)
  174. out[k].flags |= ALIAS_TOP_CLIP;
  175. if (out[k].v[0] > r_refdef.aliasvrectright)
  176. out[k].flags |= ALIAS_RIGHT_CLIP;
  177. if (out[k].v[1] > r_refdef.aliasvrectbottom)
  178. out[k].flags |= ALIAS_BOTTOM_CLIP;
  179. k++;
  180. }
  181. if (!flags)
  182. {
  183. out[k] = in[i];
  184. k++;
  185. }
  186. }
  187. return k;
  188. }
  189. /*
  190. ================
  191. R_AliasClipTriangle
  192. ================
  193. */
  194. void R_AliasClipTriangle (mtriangle_t *ptri)
  195. {
  196. int i, k, pingpong;
  197. mtriangle_t mtri;
  198. unsigned clipflags;
  199. // copy vertexes and fix seam texture coordinates
  200. if (ptri->facesfront)
  201. {
  202. fv[0][0] = pfinalverts[ptri->vertindex[0]];
  203. fv[0][1] = pfinalverts[ptri->vertindex[1]];
  204. fv[0][2] = pfinalverts[ptri->vertindex[2]];
  205. }
  206. else
  207. {
  208. for (i=0 ; i<3 ; i++)
  209. {
  210. fv[0][i] = pfinalverts[ptri->vertindex[i]];
  211. if (!ptri->facesfront && (fv[0][i].flags & ALIAS_ONSEAM) )
  212. fv[0][i].v[2] += r_affinetridesc.seamfixupX16;
  213. }
  214. }
  215. // clip
  216. clipflags = fv[0][0].flags | fv[0][1].flags | fv[0][2].flags;
  217. if (clipflags & ALIAS_Z_CLIP)
  218. {
  219. for (i=0 ; i<3 ; i++)
  220. av[i] = pauxverts[ptri->vertindex[i]];
  221. k = R_AliasClip (fv[0], fv[1], ALIAS_Z_CLIP, 3, R_Alias_clip_z);
  222. if (k == 0)
  223. return;
  224. pingpong = 1;
  225. clipflags = fv[1][0].flags | fv[1][1].flags | fv[1][2].flags;
  226. }
  227. else
  228. {
  229. pingpong = 0;
  230. k = 3;
  231. }
  232. if (clipflags & ALIAS_LEFT_CLIP)
  233. {
  234. k = R_AliasClip (fv[pingpong], fv[pingpong ^ 1],
  235. ALIAS_LEFT_CLIP, k, R_Alias_clip_left);
  236. if (k == 0)
  237. return;
  238. pingpong ^= 1;
  239. }
  240. if (clipflags & ALIAS_RIGHT_CLIP)
  241. {
  242. k = R_AliasClip (fv[pingpong], fv[pingpong ^ 1],
  243. ALIAS_RIGHT_CLIP, k, R_Alias_clip_right);
  244. if (k == 0)
  245. return;
  246. pingpong ^= 1;
  247. }
  248. if (clipflags & ALIAS_BOTTOM_CLIP)
  249. {
  250. k = R_AliasClip (fv[pingpong], fv[pingpong ^ 1],
  251. ALIAS_BOTTOM_CLIP, k, R_Alias_clip_bottom);
  252. if (k == 0)
  253. return;
  254. pingpong ^= 1;
  255. }
  256. if (clipflags & ALIAS_TOP_CLIP)
  257. {
  258. k = R_AliasClip (fv[pingpong], fv[pingpong ^ 1],
  259. ALIAS_TOP_CLIP, k, R_Alias_clip_top);
  260. if (k == 0)
  261. return;
  262. pingpong ^= 1;
  263. }
  264. for (i=0 ; i<k ; i++)
  265. {
  266. if (fv[pingpong][i].v[0] < r_refdef.aliasvrect.x)
  267. fv[pingpong][i].v[0] = r_refdef.aliasvrect.x;
  268. else if (fv[pingpong][i].v[0] > r_refdef.aliasvrectright)
  269. fv[pingpong][i].v[0] = r_refdef.aliasvrectright;
  270. if (fv[pingpong][i].v[1] < r_refdef.aliasvrect.y)
  271. fv[pingpong][i].v[1] = r_refdef.aliasvrect.y;
  272. else if (fv[pingpong][i].v[1] > r_refdef.aliasvrectbottom)
  273. fv[pingpong][i].v[1] = r_refdef.aliasvrectbottom;
  274. fv[pingpong][i].flags = 0;
  275. }
  276. // draw triangles
  277. mtri.facesfront = ptri->facesfront;
  278. r_affinetridesc.ptriangles = &mtri;
  279. r_affinetridesc.pfinalverts = fv[pingpong];
  280. // FIXME: do all at once as trifan?
  281. mtri.vertindex[0] = 0;
  282. for (i=1 ; i<k-1 ; i++)
  283. {
  284. mtri.vertindex[1] = i;
  285. mtri.vertindex[2] = i+1;
  286. D_PolysetDraw ();
  287. }
  288. }