r_main.c 25 KB

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