r_alias.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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_alias.c: routines for setting up to draw alias models
  16. #include "quakedef.h"
  17. #include "r_local.h"
  18. #include "d_local.h" // FIXME: shouldn't be needed (is needed for patch
  19. // right now, but that should move)
  20. #define LIGHT_MIN 5 // lowest light value we'll allow, to avoid the
  21. // need for inner-loop light clamping
  22. mtriangle_t *ptriangles;
  23. affinetridesc_t r_affinetridesc;
  24. void * acolormap; // FIXME: should go away
  25. trivertx_t *r_apverts;
  26. // TODO: these probably will go away with optimized rasterization
  27. mdl_t *pmdl;
  28. vec3_t r_plightvec;
  29. int r_ambientlight;
  30. float r_shadelight;
  31. aliashdr_t *paliashdr;
  32. finalvert_t *pfinalverts;
  33. auxvert_t *pauxverts;
  34. static float ziscale;
  35. static model_t *pmodel;
  36. static vec3_t alias_forward, alias_right, alias_up;
  37. static maliasskindesc_t *pskindesc;
  38. int r_amodels_drawn;
  39. int a_skinwidth;
  40. int r_anumverts;
  41. float aliastransform[3][4];
  42. typedef struct {
  43. int index0;
  44. int index1;
  45. } aedge_t;
  46. static aedge_t aedges[12] = {
  47. {0, 1}, {1, 2}, {2, 3}, {3, 0},
  48. {4, 5}, {5, 6}, {6, 7}, {7, 4},
  49. {0, 5}, {1, 4}, {2, 7}, {3, 6}
  50. };
  51. #define NUMVERTEXNORMALS 162
  52. float r_avertexnormals[NUMVERTEXNORMALS][3] = {
  53. #include "anorms.h"
  54. };
  55. void R_AliasTransformAndProjectFinalVerts (finalvert_t *fv,
  56. stvert_t *pstverts);
  57. void R_AliasSetUpTransform (int trivial_accept);
  58. void R_AliasTransformVector (vec3_t in, vec3_t out);
  59. void R_AliasTransformFinalVert (finalvert_t *fv, auxvert_t *av,
  60. trivertx_t *pverts, stvert_t *pstverts);
  61. void R_AliasProjectFinalVert (finalvert_t *fv, auxvert_t *av);
  62. /*
  63. ================
  64. R_AliasCheckBBox
  65. ================
  66. */
  67. qboolean R_AliasCheckBBox (void)
  68. {
  69. int i, flags, frame, numv;
  70. aliashdr_t *pahdr;
  71. float zi, basepts[8][3], v0, v1, frac;
  72. finalvert_t *pv0, *pv1, viewpts[16];
  73. auxvert_t *pa0, *pa1, viewaux[16];
  74. maliasframedesc_t *pframedesc;
  75. qboolean zclipped, zfullyclipped;
  76. unsigned anyclip, allclip;
  77. int minz;
  78. // expand, rotate, and translate points into worldspace
  79. currententity->trivial_accept = 0;
  80. pmodel = currententity->model;
  81. pahdr = Mod_Extradata (pmodel);
  82. pmdl = (mdl_t *)((byte *)pahdr + pahdr->model);
  83. R_AliasSetUpTransform (0);
  84. // construct the base bounding box for this frame
  85. frame = currententity->frame;
  86. // TODO: don't repeat this check when drawing?
  87. if ((frame >= pmdl->numframes) || (frame < 0))
  88. {
  89. Con_DPrintf ("No such frame %d %s\n", frame,
  90. pmodel->name);
  91. frame = 0;
  92. }
  93. pframedesc = &pahdr->frames[frame];
  94. // x worldspace coordinates
  95. basepts[0][0] = basepts[1][0] = basepts[2][0] = basepts[3][0] =
  96. (float)pframedesc->bboxmin.v[0];
  97. basepts[4][0] = basepts[5][0] = basepts[6][0] = basepts[7][0] =
  98. (float)pframedesc->bboxmax.v[0];
  99. // y worldspace coordinates
  100. basepts[0][1] = basepts[3][1] = basepts[5][1] = basepts[6][1] =
  101. (float)pframedesc->bboxmin.v[1];
  102. basepts[1][1] = basepts[2][1] = basepts[4][1] = basepts[7][1] =
  103. (float)pframedesc->bboxmax.v[1];
  104. // z worldspace coordinates
  105. basepts[0][2] = basepts[1][2] = basepts[4][2] = basepts[5][2] =
  106. (float)pframedesc->bboxmin.v[2];
  107. basepts[2][2] = basepts[3][2] = basepts[6][2] = basepts[7][2] =
  108. (float)pframedesc->bboxmax.v[2];
  109. zclipped = false;
  110. zfullyclipped = true;
  111. minz = 9999;
  112. for (i=0; i<8 ; i++)
  113. {
  114. R_AliasTransformVector (&basepts[i][0], &viewaux[i].fv[0]);
  115. if (viewaux[i].fv[2] < ALIAS_Z_CLIP_PLANE)
  116. {
  117. // we must clip points that are closer than the near clip plane
  118. viewpts[i].flags = ALIAS_Z_CLIP;
  119. zclipped = true;
  120. }
  121. else
  122. {
  123. if (viewaux[i].fv[2] < minz)
  124. minz = viewaux[i].fv[2];
  125. viewpts[i].flags = 0;
  126. zfullyclipped = false;
  127. }
  128. }
  129. if (zfullyclipped)
  130. {
  131. return false; // everything was near-z-clipped
  132. }
  133. numv = 8;
  134. if (zclipped)
  135. {
  136. // organize points by edges, use edges to get new points (possible trivial
  137. // reject)
  138. for (i=0 ; i<12 ; i++)
  139. {
  140. // edge endpoints
  141. pv0 = &viewpts[aedges[i].index0];
  142. pv1 = &viewpts[aedges[i].index1];
  143. pa0 = &viewaux[aedges[i].index0];
  144. pa1 = &viewaux[aedges[i].index1];
  145. // if one end is clipped and the other isn't, make a new point
  146. if (pv0->flags ^ pv1->flags)
  147. {
  148. frac = (ALIAS_Z_CLIP_PLANE - pa0->fv[2]) /
  149. (pa1->fv[2] - pa0->fv[2]);
  150. viewaux[numv].fv[0] = pa0->fv[0] +
  151. (pa1->fv[0] - pa0->fv[0]) * frac;
  152. viewaux[numv].fv[1] = pa0->fv[1] +
  153. (pa1->fv[1] - pa0->fv[1]) * frac;
  154. viewaux[numv].fv[2] = ALIAS_Z_CLIP_PLANE;
  155. viewpts[numv].flags = 0;
  156. numv++;
  157. }
  158. }
  159. }
  160. // project the vertices that remain after clipping
  161. anyclip = 0;
  162. allclip = ALIAS_XY_CLIP_MASK;
  163. // TODO: probably should do this loop in ASM, especially if we use floats
  164. for (i=0 ; i<numv ; i++)
  165. {
  166. // we don't need to bother with vertices that were z-clipped
  167. if (viewpts[i].flags & ALIAS_Z_CLIP)
  168. continue;
  169. zi = 1.0 / viewaux[i].fv[2];
  170. // FIXME: do with chop mode in ASM, or convert to float
  171. v0 = (viewaux[i].fv[0] * xscale * zi) + xcenter;
  172. v1 = (viewaux[i].fv[1] * yscale * zi) + ycenter;
  173. flags = 0;
  174. if (v0 < r_refdef.fvrectx)
  175. flags |= ALIAS_LEFT_CLIP;
  176. if (v1 < r_refdef.fvrecty)
  177. flags |= ALIAS_TOP_CLIP;
  178. if (v0 > r_refdef.fvrectright)
  179. flags |= ALIAS_RIGHT_CLIP;
  180. if (v1 > r_refdef.fvrectbottom)
  181. flags |= ALIAS_BOTTOM_CLIP;
  182. anyclip |= flags;
  183. allclip &= flags;
  184. }
  185. if (allclip)
  186. return false; // trivial reject off one side
  187. currententity->trivial_accept = !anyclip & !zclipped;
  188. if (currententity->trivial_accept)
  189. {
  190. if (minz > (r_aliastransition + (pmdl->size * r_resfudge)))
  191. {
  192. currententity->trivial_accept |= 2;
  193. }
  194. }
  195. return true;
  196. }
  197. /*
  198. ================
  199. R_AliasTransformVector
  200. ================
  201. */
  202. void R_AliasTransformVector (vec3_t in, vec3_t out)
  203. {
  204. out[0] = DotProduct(in, aliastransform[0]) + aliastransform[0][3];
  205. out[1] = DotProduct(in, aliastransform[1]) + aliastransform[1][3];
  206. out[2] = DotProduct(in, aliastransform[2]) + aliastransform[2][3];
  207. }
  208. /*
  209. ================
  210. R_AliasPreparePoints
  211. General clipped case
  212. ================
  213. */
  214. void R_AliasPreparePoints (void)
  215. {
  216. int i;
  217. stvert_t *pstverts;
  218. finalvert_t *fv;
  219. auxvert_t *av;
  220. mtriangle_t *ptri;
  221. finalvert_t *pfv[3];
  222. pstverts = (stvert_t *)((byte *)paliashdr + paliashdr->stverts);
  223. r_anumverts = pmdl->numverts;
  224. fv = pfinalverts;
  225. av = pauxverts;
  226. for (i=0 ; i<r_anumverts ; i++, fv++, av++, r_apverts++, pstverts++)
  227. {
  228. R_AliasTransformFinalVert (fv, av, r_apverts, pstverts);
  229. if (av->fv[2] < ALIAS_Z_CLIP_PLANE)
  230. fv->flags |= ALIAS_Z_CLIP;
  231. else
  232. {
  233. R_AliasProjectFinalVert (fv, av);
  234. if (fv->v[0] < r_refdef.aliasvrect.x)
  235. fv->flags |= ALIAS_LEFT_CLIP;
  236. if (fv->v[1] < r_refdef.aliasvrect.y)
  237. fv->flags |= ALIAS_TOP_CLIP;
  238. if (fv->v[0] > r_refdef.aliasvrectright)
  239. fv->flags |= ALIAS_RIGHT_CLIP;
  240. if (fv->v[1] > r_refdef.aliasvrectbottom)
  241. fv->flags |= ALIAS_BOTTOM_CLIP;
  242. }
  243. }
  244. //
  245. // clip and draw all triangles
  246. //
  247. r_affinetridesc.numtriangles = 1;
  248. ptri = (mtriangle_t *)((byte *)paliashdr + paliashdr->triangles);
  249. for (i=0 ; i<pmdl->numtris ; i++, ptri++)
  250. {
  251. pfv[0] = &pfinalverts[ptri->vertindex[0]];
  252. pfv[1] = &pfinalverts[ptri->vertindex[1]];
  253. pfv[2] = &pfinalverts[ptri->vertindex[2]];
  254. if ( pfv[0]->flags & pfv[1]->flags & pfv[2]->flags & (ALIAS_XY_CLIP_MASK | ALIAS_Z_CLIP) )
  255. continue; // completely clipped
  256. if ( ! ( (pfv[0]->flags | pfv[1]->flags | pfv[2]->flags) &
  257. (ALIAS_XY_CLIP_MASK | ALIAS_Z_CLIP) ) )
  258. { // totally unclipped
  259. r_affinetridesc.pfinalverts = pfinalverts;
  260. r_affinetridesc.ptriangles = ptri;
  261. D_PolysetDraw ();
  262. }
  263. else
  264. { // partially clipped
  265. R_AliasClipTriangle (ptri);
  266. }
  267. }
  268. }
  269. /*
  270. ================
  271. R_AliasSetUpTransform
  272. ================
  273. */
  274. void R_AliasSetUpTransform (int trivial_accept)
  275. {
  276. int i;
  277. float rotationmatrix[3][4], t2matrix[3][4];
  278. static float tmatrix[3][4];
  279. static float viewmatrix[3][4];
  280. vec3_t angles;
  281. // TODO: should really be stored with the entity instead of being reconstructed
  282. // TODO: should use a look-up table
  283. // TODO: could cache lazily, stored in the entity
  284. angles[ROLL] = currententity->angles[ROLL];
  285. angles[PITCH] = -currententity->angles[PITCH];
  286. angles[YAW] = currententity->angles[YAW];
  287. AngleVectors (angles, alias_forward, alias_right, alias_up);
  288. tmatrix[0][0] = pmdl->scale[0];
  289. tmatrix[1][1] = pmdl->scale[1];
  290. tmatrix[2][2] = pmdl->scale[2];
  291. tmatrix[0][3] = pmdl->scale_origin[0];
  292. tmatrix[1][3] = pmdl->scale_origin[1];
  293. tmatrix[2][3] = pmdl->scale_origin[2];
  294. // TODO: can do this with simple matrix rearrangement
  295. for (i=0 ; i<3 ; i++)
  296. {
  297. t2matrix[i][0] = alias_forward[i];
  298. t2matrix[i][1] = -alias_right[i];
  299. t2matrix[i][2] = alias_up[i];
  300. }
  301. t2matrix[0][3] = -modelorg[0];
  302. t2matrix[1][3] = -modelorg[1];
  303. t2matrix[2][3] = -modelorg[2];
  304. // FIXME: can do more efficiently than full concatenation
  305. R_ConcatTransforms (t2matrix, tmatrix, rotationmatrix);
  306. // TODO: should be global, set when vright, etc., set
  307. VectorCopy (vright, viewmatrix[0]);
  308. VectorCopy (vup, viewmatrix[1]);
  309. VectorInverse (viewmatrix[1]);
  310. VectorCopy (vpn, viewmatrix[2]);
  311. // viewmatrix[0][3] = 0;
  312. // viewmatrix[1][3] = 0;
  313. // viewmatrix[2][3] = 0;
  314. R_ConcatTransforms (viewmatrix, rotationmatrix, aliastransform);
  315. // do the scaling up of x and y to screen coordinates as part of the transform
  316. // for the unclipped case (it would mess up clipping in the clipped case).
  317. // Also scale down z, so 1/z is scaled 31 bits for free, and scale down x and y
  318. // correspondingly so the projected x and y come out right
  319. // FIXME: make this work for clipped case too?
  320. if (trivial_accept)
  321. {
  322. for (i=0 ; i<4 ; i++)
  323. {
  324. aliastransform[0][i] *= aliasxscale *
  325. (1.0 / ((float)0x8000 * 0x10000));
  326. aliastransform[1][i] *= aliasyscale *
  327. (1.0 / ((float)0x8000 * 0x10000));
  328. aliastransform[2][i] *= 1.0 / ((float)0x8000 * 0x10000);
  329. }
  330. }
  331. }
  332. /*
  333. ================
  334. R_AliasTransformFinalVert
  335. ================
  336. */
  337. void R_AliasTransformFinalVert (finalvert_t *fv, auxvert_t *av,
  338. trivertx_t *pverts, stvert_t *pstverts)
  339. {
  340. int temp;
  341. float lightcos, *plightnormal;
  342. av->fv[0] = DotProduct(pverts->v, aliastransform[0]) +
  343. aliastransform[0][3];
  344. av->fv[1] = DotProduct(pverts->v, aliastransform[1]) +
  345. aliastransform[1][3];
  346. av->fv[2] = DotProduct(pverts->v, aliastransform[2]) +
  347. aliastransform[2][3];
  348. fv->v[2] = pstverts->s;
  349. fv->v[3] = pstverts->t;
  350. fv->flags = pstverts->onseam;
  351. // lighting
  352. plightnormal = r_avertexnormals[pverts->lightnormalindex];
  353. lightcos = DotProduct (plightnormal, r_plightvec);
  354. temp = r_ambientlight;
  355. if (lightcos < 0)
  356. {
  357. temp += (int)(r_shadelight * lightcos);
  358. // clamp; because we limited the minimum ambient and shading light, we
  359. // don't have to clamp low light, just bright
  360. if (temp < 0)
  361. temp = 0;
  362. }
  363. fv->v[4] = temp;
  364. }
  365. #if !id386
  366. /*
  367. ================
  368. R_AliasTransformAndProjectFinalVerts
  369. ================
  370. */
  371. void R_AliasTransformAndProjectFinalVerts (finalvert_t *fv, stvert_t *pstverts)
  372. {
  373. int i, temp;
  374. float lightcos, *plightnormal, zi;
  375. trivertx_t *pverts;
  376. pverts = r_apverts;
  377. for (i=0 ; i<r_anumverts ; i++, fv++, pverts++, pstverts++)
  378. {
  379. // transform and project
  380. zi = 1.0 / (DotProduct(pverts->v, aliastransform[2]) +
  381. aliastransform[2][3]);
  382. // x, y, and z are scaled down by 1/2**31 in the transform, so 1/z is
  383. // scaled up by 1/2**31, and the scaling cancels out for x and y in the
  384. // projection
  385. fv->v[5] = zi;
  386. fv->v[0] = ((DotProduct(pverts->v, aliastransform[0]) +
  387. aliastransform[0][3]) * zi) + aliasxcenter;
  388. fv->v[1] = ((DotProduct(pverts->v, aliastransform[1]) +
  389. aliastransform[1][3]) * zi) + aliasycenter;
  390. fv->v[2] = pstverts->s;
  391. fv->v[3] = pstverts->t;
  392. fv->flags = pstverts->onseam;
  393. // lighting
  394. plightnormal = r_avertexnormals[pverts->lightnormalindex];
  395. lightcos = DotProduct (plightnormal, r_plightvec);
  396. temp = r_ambientlight;
  397. if (lightcos < 0)
  398. {
  399. temp += (int)(r_shadelight * lightcos);
  400. // clamp; because we limited the minimum ambient and shading light, we
  401. // don't have to clamp low light, just bright
  402. if (temp < 0)
  403. temp = 0;
  404. }
  405. fv->v[4] = temp;
  406. }
  407. }
  408. #endif
  409. /*
  410. ================
  411. R_AliasProjectFinalVert
  412. ================
  413. */
  414. void R_AliasProjectFinalVert (finalvert_t *fv, auxvert_t *av)
  415. {
  416. float zi;
  417. // project points
  418. zi = 1.0 / av->fv[2];
  419. fv->v[5] = zi * ziscale;
  420. fv->v[0] = (av->fv[0] * aliasxscale * zi) + aliasxcenter;
  421. fv->v[1] = (av->fv[1] * aliasyscale * zi) + aliasycenter;
  422. }
  423. /*
  424. ================
  425. R_AliasPrepareUnclippedPoints
  426. ================
  427. */
  428. void R_AliasPrepareUnclippedPoints (void)
  429. {
  430. stvert_t *pstverts;
  431. finalvert_t *fv;
  432. pstverts = (stvert_t *)((byte *)paliashdr + paliashdr->stverts);
  433. r_anumverts = pmdl->numverts;
  434. // FIXME: just use pfinalverts directly?
  435. fv = pfinalverts;
  436. R_AliasTransformAndProjectFinalVerts (fv, pstverts);
  437. if (r_affinetridesc.drawtype)
  438. D_PolysetDrawFinalVerts (fv, r_anumverts);
  439. r_affinetridesc.pfinalverts = pfinalverts;
  440. r_affinetridesc.ptriangles = (mtriangle_t *)
  441. ((byte *)paliashdr + paliashdr->triangles);
  442. r_affinetridesc.numtriangles = pmdl->numtris;
  443. D_PolysetDraw ();
  444. }
  445. /*
  446. ===============
  447. R_AliasSetupSkin
  448. ===============
  449. */
  450. void R_AliasSetupSkin (void)
  451. {
  452. int skinnum;
  453. int i, numskins;
  454. maliasskingroup_t *paliasskingroup;
  455. float *pskinintervals, fullskininterval;
  456. float skintargettime, skintime;
  457. skinnum = currententity->skinnum;
  458. if ((skinnum >= pmdl->numskins) || (skinnum < 0))
  459. {
  460. Con_DPrintf ("R_AliasSetupSkin: no such skin # %d\n", skinnum);
  461. skinnum = 0;
  462. }
  463. pskindesc = ((maliasskindesc_t *)
  464. ((byte *)paliashdr + paliashdr->skindesc)) + skinnum;
  465. a_skinwidth = pmdl->skinwidth;
  466. if (pskindesc->type == ALIAS_SKIN_GROUP)
  467. {
  468. paliasskingroup = (maliasskingroup_t *)((byte *)paliashdr +
  469. pskindesc->skin);
  470. pskinintervals = (float *)
  471. ((byte *)paliashdr + paliasskingroup->intervals);
  472. numskins = paliasskingroup->numskins;
  473. fullskininterval = pskinintervals[numskins-1];
  474. skintime = cl.time + currententity->syncbase;
  475. // when loading in Mod_LoadAliasSkinGroup, we guaranteed all interval
  476. // values are positive, so we don't have to worry about division by 0
  477. skintargettime = skintime -
  478. ((int)(skintime / fullskininterval)) * fullskininterval;
  479. for (i=0 ; i<(numskins-1) ; i++)
  480. {
  481. if (pskinintervals[i] > skintargettime)
  482. break;
  483. }
  484. pskindesc = &paliasskingroup->skindescs[i];
  485. }
  486. r_affinetridesc.pskindesc = pskindesc;
  487. r_affinetridesc.pskin = (void *)((byte *)paliashdr + pskindesc->skin);
  488. r_affinetridesc.skinwidth = a_skinwidth;
  489. r_affinetridesc.seamfixupX16 = (a_skinwidth >> 1) << 16;
  490. r_affinetridesc.skinheight = pmdl->skinheight;
  491. if (currententity->scoreboard)
  492. {
  493. byte *base;
  494. if (!currententity->scoreboard->skin)
  495. Skin_Find (currententity->scoreboard);
  496. base = Skin_Cache (currententity->scoreboard->skin);
  497. if (base)
  498. {
  499. r_affinetridesc.pskin = base;
  500. r_affinetridesc.skinwidth = 320;
  501. r_affinetridesc.skinheight = 200;
  502. }
  503. }
  504. }
  505. /*
  506. ================
  507. R_AliasSetupLighting
  508. ================
  509. */
  510. void R_AliasSetupLighting (alight_t *plighting)
  511. {
  512. // guarantee that no vertex will ever be lit below LIGHT_MIN, so we don't have
  513. // to clamp off the bottom
  514. r_ambientlight = plighting->ambientlight;
  515. if (r_ambientlight < LIGHT_MIN)
  516. r_ambientlight = LIGHT_MIN;
  517. r_ambientlight = (255 - r_ambientlight) << VID_CBITS;
  518. if (r_ambientlight < LIGHT_MIN)
  519. r_ambientlight = LIGHT_MIN;
  520. r_shadelight = plighting->shadelight;
  521. if (r_shadelight < 0)
  522. r_shadelight = 0;
  523. r_shadelight *= VID_GRADES;
  524. // rotate the lighting vector into the model's frame of reference
  525. r_plightvec[0] = DotProduct (plighting->plightvec, alias_forward);
  526. r_plightvec[1] = -DotProduct (plighting->plightvec, alias_right);
  527. r_plightvec[2] = DotProduct (plighting->plightvec, alias_up);
  528. }
  529. /*
  530. =================
  531. R_AliasSetupFrame
  532. set r_apverts
  533. =================
  534. */
  535. void R_AliasSetupFrame (void)
  536. {
  537. int frame;
  538. int i, numframes;
  539. maliasgroup_t *paliasgroup;
  540. float *pintervals, fullinterval, targettime, time;
  541. frame = currententity->frame;
  542. if ((frame >= pmdl->numframes) || (frame < 0))
  543. {
  544. Con_DPrintf ("R_AliasSetupFrame: no such frame %d\n", frame);
  545. frame = 0;
  546. }
  547. if (paliashdr->frames[frame].type == ALIAS_SINGLE)
  548. {
  549. r_apverts = (trivertx_t *)
  550. ((byte *)paliashdr + paliashdr->frames[frame].frame);
  551. return;
  552. }
  553. paliasgroup = (maliasgroup_t *)
  554. ((byte *)paliashdr + paliashdr->frames[frame].frame);
  555. pintervals = (float *)((byte *)paliashdr + paliasgroup->intervals);
  556. numframes = paliasgroup->numframes;
  557. fullinterval = pintervals[numframes-1];
  558. time = cl.time + currententity->syncbase;
  559. //
  560. // when loading in Mod_LoadAliasGroup, we guaranteed all interval values
  561. // are positive, so we don't have to worry about division by 0
  562. //
  563. targettime = time - ((int)(time / fullinterval)) * fullinterval;
  564. for (i=0 ; i<(numframes-1) ; i++)
  565. {
  566. if (pintervals[i] > targettime)
  567. break;
  568. }
  569. r_apverts = (trivertx_t *)
  570. ((byte *)paliashdr + paliasgroup->frames[i].frame);
  571. }
  572. /*
  573. ================
  574. R_AliasDrawModel
  575. ================
  576. */
  577. void R_AliasDrawModel (alight_t *plighting)
  578. {
  579. finalvert_t finalverts[MAXALIASVERTS +
  580. ((CACHE_SIZE - 1) / sizeof(finalvert_t)) + 1];
  581. auxvert_t auxverts[MAXALIASVERTS];
  582. r_amodels_drawn++;
  583. // cache align
  584. pfinalverts = (finalvert_t *)
  585. (((long)&finalverts[0] + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1));
  586. pauxverts = &auxverts[0];
  587. paliashdr = (aliashdr_t *)Mod_Extradata (currententity->model);
  588. pmdl = (mdl_t *)((byte *)paliashdr + paliashdr->model);
  589. R_AliasSetupSkin ();
  590. R_AliasSetUpTransform (currententity->trivial_accept);
  591. R_AliasSetupLighting (plighting);
  592. R_AliasSetupFrame ();
  593. if (!currententity->colormap)
  594. Sys_Error ("R_AliasDrawModel: !currententity->colormap");
  595. r_affinetridesc.drawtype = (currententity->trivial_accept == 3) &&
  596. r_recursiveaffinetriangles;
  597. if (r_affinetridesc.drawtype)
  598. {
  599. D_PolysetUpdateTables (); // FIXME: precalc...
  600. }
  601. else
  602. {
  603. #if id386
  604. D_Aff8Patch (currententity->colormap);
  605. #endif
  606. }
  607. acolormap = currententity->colormap;
  608. if (currententity != &cl.viewent)
  609. ziscale = (float)0x8000 * (float)0x10000;
  610. else
  611. ziscale = (float)0x8000 * (float)0x10000 * 3.0;
  612. if (currententity->trivial_accept)
  613. R_AliasPrepareUnclippedPoints ();
  614. else
  615. R_AliasPreparePoints ();
  616. }