r_main.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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_main.c
  16. #include "quakedef.h"
  17. #include "r_local.h"
  18. //define PASSAGES
  19. void *colormap;
  20. vec3_t viewlightvec;
  21. alight_t r_viewlighting = {128, 192, viewlightvec};
  22. float r_time1;
  23. int r_numallocatededges;
  24. qboolean r_drawpolys;
  25. qboolean r_drawculledpolys;
  26. qboolean r_worldpolysbacktofront;
  27. qboolean r_recursiveaffinetriangles = true;
  28. int r_pixbytes = 1;
  29. float r_aliasuvscale = 1.0;
  30. int r_outofsurfaces;
  31. int r_outofedges;
  32. qboolean r_dowarp, r_dowarpold, r_viewchanged;
  33. int numbtofpolys;
  34. btofpoly_t *pbtofpolys;
  35. mvertex_t *r_pcurrentvertbase;
  36. int c_surf;
  37. int r_maxsurfsseen, r_maxedgesseen, r_cnumsurfs;
  38. qboolean r_surfsonstack;
  39. int r_clipflags;
  40. byte *r_warpbuffer;
  41. byte *r_stack_start;
  42. qboolean r_fov_greater_than_90;
  43. entity_t r_worldentity;
  44. //
  45. // view origin
  46. //
  47. vec3_t vup, base_vup;
  48. vec3_t vpn, base_vpn;
  49. vec3_t vright, base_vright;
  50. vec3_t r_origin;
  51. //
  52. // screen size info
  53. //
  54. refdef_t r_refdef;
  55. float xcenter, ycenter;
  56. float xscale, yscale;
  57. float xscaleinv, yscaleinv;
  58. float xscaleshrink, yscaleshrink;
  59. float aliasxscale, aliasyscale, aliasxcenter, aliasycenter;
  60. int screenwidth;
  61. float pixelAspect;
  62. float screenAspect;
  63. float verticalFieldOfView;
  64. float xOrigin, yOrigin;
  65. mplane_t screenedge[4];
  66. //
  67. // refresh flags
  68. //
  69. int r_framecount = 1; // so frame counts initialized to 0 don't match
  70. int r_visframecount;
  71. int d_spanpixcount;
  72. int r_polycount;
  73. int r_drawnpolycount;
  74. int r_wholepolycount;
  75. int *pfrustum_indexes[4];
  76. int r_frustum_indexes[4*6];
  77. int reinit_surfcache = 1; // if 1, surface cache is currently empty and
  78. // must be reinitialized for current cache size
  79. mleaf_t *r_viewleaf, *r_oldviewleaf;
  80. texture_t *r_notexture_mip;
  81. float r_aliastransition, r_resfudge;
  82. int d_lightstylevalue[256]; // 8.8 fraction of base light value
  83. float dp_time1, dp_time2, db_time1, db_time2, rw_time1, rw_time2;
  84. float se_time1, se_time2, de_time1, de_time2, dv_time1, dv_time2;
  85. void R_MarkLeaves (void);
  86. cvar_t r_draworder = {"r_draworder","0"};
  87. cvar_t r_speeds = {"r_speeds","0"};
  88. cvar_t r_timegraph = {"r_timegraph","0"};
  89. cvar_t r_netgraph = {"r_netgraph","0"};
  90. cvar_t r_zgraph = {"r_zgraph","0"};
  91. cvar_t r_graphheight = {"r_graphheight","15"};
  92. cvar_t r_clearcolor = {"r_clearcolor","2"};
  93. cvar_t r_waterwarp = {"r_waterwarp","1"};
  94. cvar_t r_fullbright = {"r_fullbright","0"};
  95. cvar_t r_drawentities = {"r_drawentities","1"};
  96. cvar_t r_drawviewmodel = {"r_drawviewmodel","1"};
  97. cvar_t r_aliasstats = {"r_polymodelstats","0"};
  98. cvar_t r_dspeeds = {"r_dspeeds","0"};
  99. cvar_t r_drawflat = {"r_drawflat", "0"};
  100. cvar_t r_ambient = {"r_ambient", "0"};
  101. cvar_t r_reportsurfout = {"r_reportsurfout", "0"};
  102. cvar_t r_maxsurfs = {"r_maxsurfs", "0"};
  103. cvar_t r_numsurfs = {"r_numsurfs", "0"};
  104. cvar_t r_reportedgeout = {"r_reportedgeout", "0"};
  105. cvar_t r_maxedges = {"r_maxedges", "0"};
  106. cvar_t r_numedges = {"r_numedges", "0"};
  107. cvar_t r_aliastransbase = {"r_aliastransbase", "200"};
  108. cvar_t r_aliastransadj = {"r_aliastransadj", "100"};
  109. extern cvar_t scr_fov;
  110. void CreatePassages (void);
  111. void SetVisibilityByPassages (void);
  112. void R_NetGraph (void);
  113. void R_ZGraph (void);
  114. /*
  115. ==================
  116. R_InitTextures
  117. ==================
  118. */
  119. void R_InitTextures (void)
  120. {
  121. int x,y, m;
  122. byte *dest;
  123. // create a simple checkerboard texture for the default
  124. r_notexture_mip = Hunk_AllocName (sizeof(texture_t) + 16*16+8*8+4*4+2*2, "notexture");
  125. r_notexture_mip->width = r_notexture_mip->height = 16;
  126. r_notexture_mip->offsets[0] = sizeof(texture_t);
  127. r_notexture_mip->offsets[1] = r_notexture_mip->offsets[0] + 16*16;
  128. r_notexture_mip->offsets[2] = r_notexture_mip->offsets[1] + 8*8;
  129. r_notexture_mip->offsets[3] = r_notexture_mip->offsets[2] + 4*4;
  130. for (m=0 ; m<4 ; m++)
  131. {
  132. dest = (byte *)r_notexture_mip + r_notexture_mip->offsets[m];
  133. for (y=0 ; y< (16>>m) ; y++)
  134. for (x=0 ; x< (16>>m) ; x++)
  135. {
  136. if ( (y< (8>>m) ) ^ (x< (8>>m) ) )
  137. *dest++ = 0;
  138. else
  139. *dest++ = 0xff;
  140. }
  141. }
  142. }
  143. /*
  144. ===============
  145. R_Init
  146. ===============
  147. */
  148. void R_Init (void)
  149. {
  150. int dummy;
  151. // get stack position so we can guess if we are going to overflow
  152. r_stack_start = (byte *)&dummy;
  153. R_InitTurb ();
  154. Cmd_AddCommand ("timerefresh", R_TimeRefresh_f);
  155. Cmd_AddCommand ("pointfile", R_ReadPointFile_f);
  156. Cvar_RegisterVariable (&r_draworder);
  157. Cvar_RegisterVariable (&r_speeds);
  158. Cvar_RegisterVariable (&r_timegraph);
  159. Cvar_RegisterVariable (&r_netgraph);
  160. Cvar_RegisterVariable (&r_zgraph);
  161. Cvar_RegisterVariable (&r_graphheight);
  162. Cvar_RegisterVariable (&r_drawflat);
  163. Cvar_RegisterVariable (&r_ambient);
  164. Cvar_RegisterVariable (&r_clearcolor);
  165. Cvar_RegisterVariable (&r_waterwarp);
  166. Cvar_RegisterVariable (&r_fullbright);
  167. Cvar_RegisterVariable (&r_drawentities);
  168. Cvar_RegisterVariable (&r_drawviewmodel);
  169. Cvar_RegisterVariable (&r_aliasstats);
  170. Cvar_RegisterVariable (&r_dspeeds);
  171. Cvar_RegisterVariable (&r_reportsurfout);
  172. Cvar_RegisterVariable (&r_maxsurfs);
  173. Cvar_RegisterVariable (&r_numsurfs);
  174. Cvar_RegisterVariable (&r_reportedgeout);
  175. Cvar_RegisterVariable (&r_maxedges);
  176. Cvar_RegisterVariable (&r_numedges);
  177. Cvar_RegisterVariable (&r_aliastransbase);
  178. Cvar_RegisterVariable (&r_aliastransadj);
  179. Cvar_SetValue ("r_maxedges", (float)NUMSTACKEDGES);
  180. Cvar_SetValue ("r_maxsurfs", (float)NUMSTACKSURFACES);
  181. view_clipplanes[0].leftedge = true;
  182. view_clipplanes[1].rightedge = true;
  183. view_clipplanes[1].leftedge = view_clipplanes[2].leftedge =
  184. view_clipplanes[3].leftedge = false;
  185. view_clipplanes[0].rightedge = view_clipplanes[2].rightedge =
  186. view_clipplanes[3].rightedge = false;
  187. r_refdef.xOrigin = XCENTERING;
  188. r_refdef.yOrigin = YCENTERING;
  189. R_InitParticles ();
  190. // TODO: collect 386-specific code in one place
  191. #if id386
  192. Sys_MakeCodeWriteable ((long)R_EdgeCodeStart,
  193. (long)R_EdgeCodeEnd - (long)R_EdgeCodeStart);
  194. #endif // id386
  195. D_Init ();
  196. }
  197. /*
  198. ===============
  199. R_NewMap
  200. ===============
  201. */
  202. void R_NewMap (void)
  203. {
  204. int i;
  205. memset (&r_worldentity, 0, sizeof(r_worldentity));
  206. r_worldentity.model = cl.worldmodel;
  207. // clear out efrags in case the level hasn't been reloaded
  208. // FIXME: is this one short?
  209. for (i=0 ; i<cl.worldmodel->numleafs ; i++)
  210. cl.worldmodel->leafs[i].efrags = NULL;
  211. r_viewleaf = NULL;
  212. R_ClearParticles ();
  213. r_cnumsurfs = r_maxsurfs.value;
  214. if (r_cnumsurfs <= MINSURFACES)
  215. r_cnumsurfs = MINSURFACES;
  216. if (r_cnumsurfs > NUMSTACKSURFACES)
  217. {
  218. surfaces = Hunk_AllocName (r_cnumsurfs * sizeof(surf_t), "surfaces");
  219. surface_p = surfaces;
  220. surf_max = &surfaces[r_cnumsurfs];
  221. r_surfsonstack = false;
  222. // surface 0 doesn't really exist; it's just a dummy because index 0
  223. // is used to indicate no edge attached to surface
  224. surfaces--;
  225. R_SurfacePatch ();
  226. }
  227. else
  228. {
  229. r_surfsonstack = true;
  230. }
  231. r_maxedgesseen = 0;
  232. r_maxsurfsseen = 0;
  233. r_numallocatededges = r_maxedges.value;
  234. if (r_numallocatededges < MINEDGES)
  235. r_numallocatededges = MINEDGES;
  236. if (r_numallocatededges <= NUMSTACKEDGES)
  237. {
  238. auxedges = NULL;
  239. }
  240. else
  241. {
  242. auxedges = Hunk_AllocName (r_numallocatededges * sizeof(edge_t),
  243. "edges");
  244. }
  245. r_dowarpold = false;
  246. r_viewchanged = false;
  247. }
  248. /*
  249. ===============
  250. R_SetVrect
  251. ===============
  252. */
  253. void R_SetVrect (vrect_t *pvrectin, vrect_t *pvrect, int lineadj)
  254. {
  255. int h;
  256. float size;
  257. qboolean full = false;
  258. if (scr_viewsize.value >= 100.0) {
  259. size = 100.0;
  260. full = true;
  261. } else
  262. size = scr_viewsize.value;
  263. if (cl.intermission)
  264. {
  265. full = true;
  266. size = 100.0;
  267. lineadj = 0;
  268. }
  269. size /= 100.0;
  270. if (!cl_sbar.value && full)
  271. h = pvrectin->height;
  272. else
  273. h = pvrectin->height - lineadj;
  274. // h = (!cl_sbar.value && size==1.0) ? pvrectin->height : (pvrectin->height - lineadj);
  275. // h = pvrectin->height - lineadj;
  276. if (full)
  277. pvrect->width = pvrectin->width;
  278. else
  279. pvrect->width = pvrectin->width * size;
  280. if (pvrect->width < 96)
  281. {
  282. size = 96.0 / pvrectin->width;
  283. pvrect->width = 96; // min for icons
  284. }
  285. pvrect->width &= ~7;
  286. pvrect->height = pvrectin->height * size;
  287. if (cl_sbar.value || !full) {
  288. if (pvrect->height > pvrectin->height - lineadj)
  289. pvrect->height = pvrectin->height - lineadj;
  290. } else
  291. if (pvrect->height > pvrectin->height)
  292. pvrect->height = pvrectin->height;
  293. pvrect->height &= ~1;
  294. pvrect->x = (pvrectin->width - pvrect->width)/2;
  295. if (full)
  296. pvrect->y = 0;
  297. else
  298. pvrect->y = (h - pvrect->height)/2;
  299. }
  300. /*
  301. ===============
  302. R_ViewChanged
  303. Called every time the vid structure or r_refdef changes.
  304. Guaranteed to be called before the first refresh
  305. ===============
  306. */
  307. void R_ViewChanged (vrect_t *pvrect, int lineadj, float aspect)
  308. {
  309. int i;
  310. float res_scale;
  311. r_viewchanged = true;
  312. R_SetVrect (pvrect, &r_refdef.vrect, lineadj);
  313. r_refdef.horizontalFieldOfView = 2.0 * tan (r_refdef.fov_x/360*M_PI);
  314. r_refdef.fvrectx = (float)r_refdef.vrect.x;
  315. r_refdef.fvrectx_adj = (float)r_refdef.vrect.x - 0.5;
  316. r_refdef.vrect_x_adj_shift20 = (r_refdef.vrect.x<<20) + (1<<19) - 1;
  317. r_refdef.fvrecty = (float)r_refdef.vrect.y;
  318. r_refdef.fvrecty_adj = (float)r_refdef.vrect.y - 0.5;
  319. r_refdef.vrectright = r_refdef.vrect.x + r_refdef.vrect.width;
  320. r_refdef.vrectright_adj_shift20 = (r_refdef.vrectright<<20) + (1<<19) - 1;
  321. r_refdef.fvrectright = (float)r_refdef.vrectright;
  322. r_refdef.fvrectright_adj = (float)r_refdef.vrectright - 0.5;
  323. r_refdef.vrectrightedge = (float)r_refdef.vrectright - 0.99;
  324. r_refdef.vrectbottom = r_refdef.vrect.y + r_refdef.vrect.height;
  325. r_refdef.fvrectbottom = (float)r_refdef.vrectbottom;
  326. r_refdef.fvrectbottom_adj = (float)r_refdef.vrectbottom - 0.5;
  327. r_refdef.aliasvrect.x = (int)(r_refdef.vrect.x * r_aliasuvscale);
  328. r_refdef.aliasvrect.y = (int)(r_refdef.vrect.y * r_aliasuvscale);
  329. r_refdef.aliasvrect.width = (int)(r_refdef.vrect.width * r_aliasuvscale);
  330. r_refdef.aliasvrect.height = (int)(r_refdef.vrect.height * r_aliasuvscale);
  331. r_refdef.aliasvrectright = r_refdef.aliasvrect.x +
  332. r_refdef.aliasvrect.width;
  333. r_refdef.aliasvrectbottom = r_refdef.aliasvrect.y +
  334. r_refdef.aliasvrect.height;
  335. pixelAspect = aspect;
  336. xOrigin = r_refdef.xOrigin;
  337. yOrigin = r_refdef.yOrigin;
  338. screenAspect = r_refdef.vrect.width*pixelAspect /
  339. r_refdef.vrect.height;
  340. // 320*200 1.0 pixelAspect = 1.6 screenAspect
  341. // 320*240 1.0 pixelAspect = 1.3333 screenAspect
  342. // proper 320*200 pixelAspect = 0.8333333
  343. verticalFieldOfView = r_refdef.horizontalFieldOfView / screenAspect;
  344. // values for perspective projection
  345. // if math were exact, the values would range from 0.5 to to range+0.5
  346. // hopefully they wll be in the 0.000001 to range+.999999 and truncate
  347. // the polygon rasterization will never render in the first row or column
  348. // but will definately render in the [range] row and column, so adjust the
  349. // buffer origin to get an exact edge to edge fill
  350. xcenter = ((float)r_refdef.vrect.width * XCENTERING) +
  351. r_refdef.vrect.x - 0.5;
  352. aliasxcenter = xcenter * r_aliasuvscale;
  353. ycenter = ((float)r_refdef.vrect.height * YCENTERING) +
  354. r_refdef.vrect.y - 0.5;
  355. aliasycenter = ycenter * r_aliasuvscale;
  356. xscale = r_refdef.vrect.width / r_refdef.horizontalFieldOfView;
  357. aliasxscale = xscale * r_aliasuvscale;
  358. xscaleinv = 1.0 / xscale;
  359. yscale = xscale * pixelAspect;
  360. aliasyscale = yscale * r_aliasuvscale;
  361. yscaleinv = 1.0 / yscale;
  362. xscaleshrink = (r_refdef.vrect.width-6)/r_refdef.horizontalFieldOfView;
  363. yscaleshrink = xscaleshrink*pixelAspect;
  364. // left side clip
  365. screenedge[0].normal[0] = -1.0 / (xOrigin*r_refdef.horizontalFieldOfView);
  366. screenedge[0].normal[1] = 0;
  367. screenedge[0].normal[2] = 1;
  368. screenedge[0].type = PLANE_ANYZ;
  369. // right side clip
  370. screenedge[1].normal[0] =
  371. 1.0 / ((1.0-xOrigin)*r_refdef.horizontalFieldOfView);
  372. screenedge[1].normal[1] = 0;
  373. screenedge[1].normal[2] = 1;
  374. screenedge[1].type = PLANE_ANYZ;
  375. // top side clip
  376. screenedge[2].normal[0] = 0;
  377. screenedge[2].normal[1] = -1.0 / (yOrigin*verticalFieldOfView);
  378. screenedge[2].normal[2] = 1;
  379. screenedge[2].type = PLANE_ANYZ;
  380. // bottom side clip
  381. screenedge[3].normal[0] = 0;
  382. screenedge[3].normal[1] = 1.0 / ((1.0-yOrigin)*verticalFieldOfView);
  383. screenedge[3].normal[2] = 1;
  384. screenedge[3].type = PLANE_ANYZ;
  385. for (i=0 ; i<4 ; i++)
  386. VectorNormalize (screenedge[i].normal);
  387. res_scale = sqrt ((double)(r_refdef.vrect.width * r_refdef.vrect.height) /
  388. (320.0 * 152.0)) *
  389. (2.0 / r_refdef.horizontalFieldOfView);
  390. r_aliastransition = r_aliastransbase.value * res_scale;
  391. r_resfudge = r_aliastransadj.value * res_scale;
  392. if (scr_fov.value <= 90.0)
  393. r_fov_greater_than_90 = false;
  394. else
  395. r_fov_greater_than_90 = true;
  396. // TODO: collect 386-specific code in one place
  397. #if id386
  398. if (r_pixbytes == 1)
  399. {
  400. Sys_MakeCodeWriteable ((long)R_Surf8Start,
  401. (long)R_Surf8End - (long)R_Surf8Start);
  402. colormap = vid.colormap;
  403. R_Surf8Patch ();
  404. }
  405. else
  406. {
  407. Sys_MakeCodeWriteable ((long)R_Surf16Start,
  408. (long)R_Surf16End - (long)R_Surf16Start);
  409. colormap = vid.colormap16;
  410. R_Surf16Patch ();
  411. }
  412. #endif // id386
  413. D_ViewChanged ();
  414. }
  415. /*
  416. ===============
  417. R_MarkLeaves
  418. ===============
  419. */
  420. void R_MarkLeaves (void)
  421. {
  422. byte *vis;
  423. mnode_t *node;
  424. int i;
  425. if (r_oldviewleaf == r_viewleaf)
  426. return;
  427. r_visframecount++;
  428. r_oldviewleaf = r_viewleaf;
  429. vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
  430. for (i=0 ; i<cl.worldmodel->numleafs ; i++)
  431. {
  432. if (vis[i>>3] & (1<<(i&7)))
  433. {
  434. node = (mnode_t *)&cl.worldmodel->leafs[i+1];
  435. do
  436. {
  437. if (node->visframe == r_visframecount)
  438. break;
  439. node->visframe = r_visframecount;
  440. node = node->parent;
  441. } while (node);
  442. }
  443. }
  444. }
  445. /*
  446. =============
  447. R_DrawEntitiesOnList
  448. =============
  449. */
  450. void R_DrawEntitiesOnList (void)
  451. {
  452. int i, j;
  453. int lnum;
  454. alight_t lighting;
  455. // FIXME: remove and do real lighting
  456. float lightvec[3] = {-1, 0, 0};
  457. vec3_t dist;
  458. float add;
  459. if (!r_drawentities.value)
  460. return;
  461. for (i=0 ; i<cl_numvisedicts ; i++)
  462. {
  463. currententity = &cl_visedicts[i];
  464. switch (currententity->model->type)
  465. {
  466. case mod_sprite:
  467. VectorCopy (currententity->origin, r_entorigin);
  468. VectorSubtract (r_origin, r_entorigin, modelorg);
  469. R_DrawSprite ();
  470. break;
  471. case mod_alias:
  472. VectorCopy (currententity->origin, r_entorigin);
  473. VectorSubtract (r_origin, r_entorigin, modelorg);
  474. // see if the bounding box lets us trivially reject, also sets
  475. // trivial accept status
  476. if (R_AliasCheckBBox ())
  477. {
  478. j = R_LightPoint (currententity->origin);
  479. lighting.ambientlight = j;
  480. lighting.shadelight = j;
  481. lighting.plightvec = lightvec;
  482. for (lnum=0 ; lnum<MAX_DLIGHTS ; lnum++)
  483. {
  484. if (cl_dlights[lnum].die >= cl.time)
  485. {
  486. VectorSubtract (currententity->origin,
  487. cl_dlights[lnum].origin,
  488. dist);
  489. add = cl_dlights[lnum].radius - Length(dist);
  490. if (add > 0)
  491. lighting.ambientlight += add;
  492. }
  493. }
  494. // clamp lighting so it doesn't overbright as much
  495. if (lighting.ambientlight > 128)
  496. lighting.ambientlight = 128;
  497. if (lighting.ambientlight + lighting.shadelight > 192)
  498. lighting.shadelight = 192 - lighting.ambientlight;
  499. R_AliasDrawModel (&lighting);
  500. }
  501. break;
  502. default:
  503. break;
  504. }
  505. }
  506. }
  507. /*
  508. =============
  509. R_DrawViewModel
  510. =============
  511. */
  512. void R_DrawViewModel (void)
  513. {
  514. // FIXME: remove and do real lighting
  515. float lightvec[3] = {-1, 0, 0};
  516. int j;
  517. int lnum;
  518. vec3_t dist;
  519. float add;
  520. dlight_t *dl;
  521. if (!r_drawviewmodel.value || r_fov_greater_than_90 || !Cam_DrawViewModel())
  522. return;
  523. if (cl.stats[STAT_ITEMS] & IT_INVISIBILITY)
  524. return;
  525. if (cl.stats[STAT_HEALTH] <= 0)
  526. return;
  527. currententity = &cl.viewent;
  528. if (!currententity->model)
  529. return;
  530. VectorCopy (currententity->origin, r_entorigin);
  531. VectorSubtract (r_origin, r_entorigin, modelorg);
  532. VectorCopy (vup, viewlightvec);
  533. VectorInverse (viewlightvec);
  534. j = R_LightPoint (currententity->origin);
  535. if (j < 24)
  536. j = 24; // allways give some light on gun
  537. r_viewlighting.ambientlight = j;
  538. r_viewlighting.shadelight = j;
  539. // add dynamic lights
  540. for (lnum=0 ; lnum<MAX_DLIGHTS ; lnum++)
  541. {
  542. dl = &cl_dlights[lnum];
  543. if (!dl->radius)
  544. continue;
  545. if (!dl->radius)
  546. continue;
  547. if (dl->die < cl.time)
  548. continue;
  549. VectorSubtract (currententity->origin, dl->origin, dist);
  550. add = dl->radius - Length(dist);
  551. if (add > 0)
  552. r_viewlighting.ambientlight += add;
  553. }
  554. // clamp lighting so it doesn't overbright as much
  555. if (r_viewlighting.ambientlight > 128)
  556. r_viewlighting.ambientlight = 128;
  557. if (r_viewlighting.ambientlight + r_viewlighting.shadelight > 192)
  558. r_viewlighting.shadelight = 192 - r_viewlighting.ambientlight;
  559. r_viewlighting.plightvec = lightvec;
  560. R_AliasDrawModel (&r_viewlighting);
  561. }
  562. /*
  563. =============
  564. R_BmodelCheckBBox
  565. =============
  566. */
  567. int R_BmodelCheckBBox (model_t *clmodel, float *minmaxs)
  568. {
  569. int i, *pindex, clipflags;
  570. vec3_t acceptpt, rejectpt;
  571. double d;
  572. clipflags = 0;
  573. if (currententity->angles[0] || currententity->angles[1]
  574. || currententity->angles[2])
  575. {
  576. for (i=0 ; i<4 ; i++)
  577. {
  578. d = DotProduct (currententity->origin, view_clipplanes[i].normal);
  579. d -= view_clipplanes[i].dist;
  580. if (d <= -clmodel->radius)
  581. return BMODEL_FULLY_CLIPPED;
  582. if (d <= clmodel->radius)
  583. clipflags |= (1<<i);
  584. }
  585. }
  586. else
  587. {
  588. for (i=0 ; i<4 ; i++)
  589. {
  590. // generate accept and reject points
  591. // FIXME: do with fast look-ups or integer tests based on the sign bit
  592. // of the floating point values
  593. pindex = pfrustum_indexes[i];
  594. rejectpt[0] = minmaxs[pindex[0]];
  595. rejectpt[1] = minmaxs[pindex[1]];
  596. rejectpt[2] = minmaxs[pindex[2]];
  597. d = DotProduct (rejectpt, view_clipplanes[i].normal);
  598. d -= view_clipplanes[i].dist;
  599. if (d <= 0)
  600. return BMODEL_FULLY_CLIPPED;
  601. acceptpt[0] = minmaxs[pindex[3+0]];
  602. acceptpt[1] = minmaxs[pindex[3+1]];
  603. acceptpt[2] = minmaxs[pindex[3+2]];
  604. d = DotProduct (acceptpt, view_clipplanes[i].normal);
  605. d -= view_clipplanes[i].dist;
  606. if (d <= 0)
  607. clipflags |= (1<<i);
  608. }
  609. }
  610. return clipflags;
  611. }
  612. /*
  613. =============
  614. R_DrawBEntitiesOnList
  615. =============
  616. */
  617. void R_DrawBEntitiesOnList (void)
  618. {
  619. int i, j, k, clipflags;
  620. vec3_t oldorigin;
  621. model_t *clmodel;
  622. float minmaxs[6];
  623. if (!r_drawentities.value)
  624. return;
  625. VectorCopy (modelorg, oldorigin);
  626. insubmodel = true;
  627. r_dlightframecount = r_framecount;
  628. for (i=0 ; i<cl_numvisedicts ; i++)
  629. {
  630. currententity = &cl_visedicts[i];
  631. switch (currententity->model->type)
  632. {
  633. case mod_brush:
  634. clmodel = currententity->model;
  635. // see if the bounding box lets us trivially reject, also sets
  636. // trivial accept status
  637. for (j=0 ; j<3 ; j++)
  638. {
  639. minmaxs[j] = currententity->origin[j] +
  640. clmodel->mins[j];
  641. minmaxs[3+j] = currententity->origin[j] +
  642. clmodel->maxs[j];
  643. }
  644. clipflags = R_BmodelCheckBBox (clmodel, minmaxs);
  645. if (clipflags != BMODEL_FULLY_CLIPPED)
  646. {
  647. VectorCopy (currententity->origin, r_entorigin);
  648. VectorSubtract (r_origin, r_entorigin, modelorg);
  649. // FIXME: is this needed?
  650. VectorCopy (modelorg, r_worldmodelorg);
  651. r_pcurrentvertbase = clmodel->vertexes;
  652. // FIXME: stop transforming twice
  653. R_RotateBmodel ();
  654. // calculate dynamic lighting for bmodel if it's not an
  655. // instanced model
  656. if (clmodel->firstmodelsurface != 0)
  657. {
  658. for (k=0 ; k<MAX_DLIGHTS ; k++)
  659. {
  660. if ((cl_dlights[k].die < cl.time) ||
  661. (!cl_dlights[k].radius))
  662. {
  663. continue;
  664. }
  665. R_MarkLights (&cl_dlights[k], 1<<k,
  666. clmodel->nodes + clmodel->hulls[0].firstclipnode);
  667. }
  668. }
  669. // if the driver wants polygons, deliver those. Z-buffering is on
  670. // at this point, so no clipping to the world tree is needed, just
  671. // frustum clipping
  672. if (r_drawpolys | r_drawculledpolys)
  673. {
  674. R_ZDrawSubmodelPolys (clmodel);
  675. }
  676. else
  677. {
  678. r_pefragtopnode = NULL;
  679. for (j=0 ; j<3 ; j++)
  680. {
  681. r_emins[j] = minmaxs[j];
  682. r_emaxs[j] = minmaxs[3+j];
  683. }
  684. R_SplitEntityOnNode2 (cl.worldmodel->nodes);
  685. if (r_pefragtopnode)
  686. {
  687. currententity->topnode = r_pefragtopnode;
  688. if (r_pefragtopnode->contents >= 0)
  689. {
  690. // not a leaf; has to be clipped to the world BSP
  691. r_clipflags = clipflags;
  692. R_DrawSolidClippedSubmodelPolygons (clmodel);
  693. }
  694. else
  695. {
  696. // falls entirely in one leaf, so we just put all the
  697. // edges in the edge list and let 1/z sorting handle
  698. // drawing order
  699. R_DrawSubmodelPolygons (clmodel, clipflags);
  700. }
  701. currententity->topnode = NULL;
  702. }
  703. }
  704. // put back world rotation and frustum clipping
  705. // FIXME: R_RotateBmodel should just work off base_vxx
  706. VectorCopy (base_vpn, vpn);
  707. VectorCopy (base_vup, vup);
  708. VectorCopy (base_vright, vright);
  709. VectorCopy (base_modelorg, modelorg);
  710. VectorCopy (oldorigin, modelorg);
  711. R_TransformFrustum ();
  712. }
  713. break;
  714. default:
  715. break;
  716. }
  717. }
  718. insubmodel = false;
  719. }
  720. /*
  721. ================
  722. R_EdgeDrawing
  723. ================
  724. */
  725. void R_EdgeDrawing (void)
  726. {
  727. edge_t ledges[NUMSTACKEDGES +
  728. ((CACHE_SIZE - 1) / sizeof(edge_t)) + 1];
  729. surf_t lsurfs[NUMSTACKSURFACES +
  730. ((CACHE_SIZE - 1) / sizeof(surf_t)) + 1];
  731. if (auxedges)
  732. {
  733. r_edges = auxedges;
  734. }
  735. else
  736. {
  737. r_edges = (edge_t *)
  738. (((long)&ledges[0] + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1));
  739. }
  740. if (r_surfsonstack)
  741. {
  742. surfaces = (surf_t *)
  743. (((long)&lsurfs[0] + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1));
  744. surf_max = &surfaces[r_cnumsurfs];
  745. // surface 0 doesn't really exist; it's just a dummy because index 0
  746. // is used to indicate no edge attached to surface
  747. surfaces--;
  748. R_SurfacePatch ();
  749. }
  750. R_BeginEdgeFrame ();
  751. if (r_dspeeds.value)
  752. {
  753. rw_time1 = Sys_DoubleTime ();
  754. }
  755. R_RenderWorld ();
  756. if (r_drawculledpolys)
  757. R_ScanEdges ();
  758. // only the world can be drawn back to front with no z reads or compares, just
  759. // z writes, so have the driver turn z compares on now
  760. D_TurnZOn ();
  761. if (r_dspeeds.value)
  762. {
  763. rw_time2 = Sys_DoubleTime ();
  764. db_time1 = rw_time2;
  765. }
  766. R_DrawBEntitiesOnList ();
  767. if (r_dspeeds.value)
  768. {
  769. db_time2 = Sys_DoubleTime ();
  770. se_time1 = db_time2;
  771. }
  772. if (!r_dspeeds.value)
  773. {
  774. VID_UnlockBuffer ();
  775. S_ExtraUpdate (); // don't let sound get messed up if going slow
  776. VID_LockBuffer ();
  777. }
  778. if (!(r_drawpolys | r_drawculledpolys))
  779. R_ScanEdges ();
  780. }
  781. /*
  782. ================
  783. R_RenderView
  784. r_refdef must be set before the first call
  785. ================
  786. */
  787. void R_RenderView_ (void)
  788. {
  789. byte warpbuffer[WARP_WIDTH * WARP_HEIGHT];
  790. r_warpbuffer = warpbuffer;
  791. if (r_timegraph.value || r_speeds.value || r_dspeeds.value)
  792. r_time1 = Sys_DoubleTime ();
  793. R_SetupFrame ();
  794. #ifdef PASSAGES
  795. SetVisibilityByPassages ();
  796. #else
  797. R_MarkLeaves (); // done here so we know if we're in water
  798. #endif
  799. // make FDIV fast. This reduces timing precision after we've been running for a
  800. // while, so we don't do it globally. This also sets chop mode, and we do it
  801. // here so that setup stuff like the refresh area calculations match what's
  802. // done in screen.c
  803. Sys_LowFPPrecision ();
  804. if (!r_worldentity.model || !cl.worldmodel)
  805. Sys_Error ("R_RenderView: NULL worldmodel");
  806. if (!r_dspeeds.value)
  807. {
  808. VID_UnlockBuffer ();
  809. S_ExtraUpdate (); // don't let sound get messed up if going slow
  810. VID_LockBuffer ();
  811. }
  812. R_EdgeDrawing ();
  813. if (!r_dspeeds.value)
  814. {
  815. VID_UnlockBuffer ();
  816. S_ExtraUpdate (); // don't let sound get messed up if going slow
  817. VID_LockBuffer ();
  818. }
  819. if (r_dspeeds.value)
  820. {
  821. se_time2 = Sys_DoubleTime ();
  822. de_time1 = se_time2;
  823. }
  824. R_DrawEntitiesOnList ();
  825. if (r_dspeeds.value)
  826. {
  827. de_time2 = Sys_DoubleTime ();
  828. dv_time1 = de_time2;
  829. }
  830. R_DrawViewModel ();
  831. if (r_dspeeds.value)
  832. {
  833. dv_time2 = Sys_DoubleTime ();
  834. dp_time1 = Sys_DoubleTime ();
  835. }
  836. R_DrawParticles ();
  837. if (r_dspeeds.value)
  838. dp_time2 = Sys_DoubleTime ();
  839. if (r_dowarp)
  840. D_WarpScreen ();
  841. V_SetContentsColor (r_viewleaf->contents);
  842. if (r_timegraph.value)
  843. R_TimeGraph ();
  844. if (r_netgraph.value)
  845. R_NetGraph ();
  846. if (r_zgraph.value)
  847. R_ZGraph ();
  848. if (r_aliasstats.value)
  849. R_PrintAliasStats ();
  850. if (r_speeds.value)
  851. R_PrintTimes ();
  852. if (r_dspeeds.value)
  853. R_PrintDSpeeds ();
  854. if (r_reportsurfout.value && r_outofsurfaces)
  855. Con_Printf ("Short %d surfaces\n", r_outofsurfaces);
  856. if (r_reportedgeout.value && r_outofedges)
  857. Con_Printf ("Short roughly %d edges\n", r_outofedges * 2 / 3);
  858. // back to high floating-point precision
  859. Sys_HighFPPrecision ();
  860. }
  861. void R_RenderView (void)
  862. {
  863. int dummy;
  864. int delta;
  865. delta = (byte *)&dummy - r_stack_start;
  866. if (delta < -10000 || delta > 10000)
  867. Sys_Error ("R_RenderView: called without enough stack");
  868. if ( Hunk_LowMark() & 3 )
  869. Sys_Error ("Hunk is missaligned");
  870. if ( (long)(&dummy) & 3 )
  871. Sys_Error ("Stack is missaligned");
  872. if ( (long)(&r_warpbuffer) & 3 )
  873. Sys_Error ("Globals are missaligned");
  874. R_RenderView_ ();
  875. }
  876. /*
  877. ================
  878. R_InitTurb
  879. ================
  880. */
  881. void R_InitTurb (void)
  882. {
  883. int i;
  884. for (i=0 ; i<1280 ; i++)
  885. {
  886. sintable[i] = AMP + sin(i*3.14159*2/CYCLE)*AMP;
  887. intsintable[i] = AMP2 + sin(i*3.14159*2/CYCLE)*AMP2; // AMP2, not 20
  888. }
  889. }