r_misc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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_misc.c
  16. #include "quakedef.h"
  17. #include "r_local.h"
  18. /*
  19. ===============
  20. R_CheckVariables
  21. ===============
  22. */
  23. void R_CheckVariables (void)
  24. {
  25. #if 0
  26. static float oldbright;
  27. if (r_fullbright.value != oldbright)
  28. {
  29. oldbright = r_fullbright.value;
  30. D_FlushCaches (); // so all lighting changes
  31. }
  32. #endif
  33. }
  34. /*
  35. ============
  36. Show
  37. Debugging use
  38. ============
  39. */
  40. void Show (void)
  41. {
  42. vrect_t vr;
  43. vr.x = vr.y = 0;
  44. vr.width = vid.width;
  45. vr.height = vid.height;
  46. vr.pnext = NULL;
  47. VID_Update (&vr);
  48. }
  49. /*
  50. ====================
  51. R_TimeRefresh_f
  52. For program optimization
  53. ====================
  54. */
  55. void R_TimeRefresh_f (void)
  56. {
  57. int i;
  58. float start, stop, time;
  59. int startangle;
  60. vrect_t vr;
  61. startangle = r_refdef.viewangles[1];
  62. start = Sys_DoubleTime ();
  63. for (i=0 ; i<128 ; i++)
  64. {
  65. r_refdef.viewangles[1] = i/128.0*360.0;
  66. VID_LockBuffer ();
  67. R_RenderView ();
  68. VID_UnlockBuffer ();
  69. vr.x = r_refdef.vrect.x;
  70. vr.y = r_refdef.vrect.y;
  71. vr.width = r_refdef.vrect.width;
  72. vr.height = r_refdef.vrect.height;
  73. vr.pnext = NULL;
  74. VID_Update (&vr);
  75. }
  76. stop = Sys_DoubleTime ();
  77. time = stop-start;
  78. Con_Printf ("%f seconds (%f fps)\n", time, 128/time);
  79. r_refdef.viewangles[1] = startangle;
  80. }
  81. /*
  82. ================
  83. R_LineGraph
  84. Only called by R_DisplayTime
  85. ================
  86. */
  87. void R_LineGraph (int x, int y, int h)
  88. {
  89. int i;
  90. byte *dest;
  91. int s;
  92. int color;
  93. // FIXME: should be disabled on no-buffer adapters, or should be in the driver
  94. // x += r_refdef.vrect.x;
  95. // y += r_refdef.vrect.y;
  96. dest = vid.buffer + vid.rowbytes*y + x;
  97. s = r_graphheight.value;
  98. if (h == 10000)
  99. color = 0x6f; // yellow
  100. else if (h == 9999)
  101. color = 0x4f; // red
  102. else if (h == 9998)
  103. color = 0xd0; // blue
  104. else
  105. color = 0xff; // pink
  106. if (h>s)
  107. h = s;
  108. for (i=0 ; i<h ; i++, dest -= vid.rowbytes*2)
  109. {
  110. dest[0] = color;
  111. // *(dest-vid.rowbytes) = 0x30;
  112. }
  113. #if 0
  114. for ( ; i<s ; i++, dest -= vid.rowbytes*2)
  115. {
  116. dest[0] = 0x30;
  117. *(dest-vid.rowbytes) = 0x30;
  118. }
  119. #endif
  120. }
  121. /*
  122. ==============
  123. R_TimeGraph
  124. Performance monitoring tool
  125. ==============
  126. */
  127. #define MAX_TIMINGS 100
  128. extern float mouse_x, mouse_y;
  129. int graphval;
  130. void R_TimeGraph (void)
  131. {
  132. static int timex;
  133. int a;
  134. float r_time2;
  135. static byte r_timings[MAX_TIMINGS];
  136. int x;
  137. r_time2 = Sys_DoubleTime ();
  138. a = (r_time2-r_time1)/0.01;
  139. //a = fabs(mouse_y * 0.05);
  140. //a = (int)((r_refdef.vieworg[2] + 1024)/1)%(int)r_graphheight.value;
  141. //a = (int)((pmove.velocity[2] + 500)/10);
  142. //a = fabs(velocity[0])/20;
  143. //a = ((int)fabs(origin[0])/8)%20;
  144. //a = (cl.idealpitch + 30)/5;
  145. //a = (int)(cl.simangles[YAW] * 64/360) & 63;
  146. a = graphval;
  147. r_timings[timex] = a;
  148. a = timex;
  149. if (r_refdef.vrect.width <= MAX_TIMINGS)
  150. x = r_refdef.vrect.width-1;
  151. else
  152. x = r_refdef.vrect.width -
  153. (r_refdef.vrect.width - MAX_TIMINGS)/2;
  154. do
  155. {
  156. R_LineGraph (x, r_refdef.vrect.height-2, r_timings[a]);
  157. if (x==0)
  158. break; // screen too small to hold entire thing
  159. x--;
  160. a--;
  161. if (a == -1)
  162. a = MAX_TIMINGS-1;
  163. } while (a != timex);
  164. timex = (timex+1)%MAX_TIMINGS;
  165. }
  166. /*
  167. ==============
  168. R_NetGraph
  169. ==============
  170. */
  171. void R_NetGraph (void)
  172. {
  173. int a, x, y, y2, w, i;
  174. frame_t *frame;
  175. int lost;
  176. char st[80];
  177. if (vid.width - 16 <= NET_TIMINGS)
  178. w = vid.width - 16;
  179. else
  180. w = NET_TIMINGS;
  181. x = -((vid.width - 320)>>1);
  182. y = vid.height - sb_lines - 24 - (int)r_graphheight.value*2 - 2;
  183. M_DrawTextBox (x, y, (w+7)/8, ((int)r_graphheight.value*2+7)/8 + 1);
  184. y2 = y + 8;
  185. y = vid.height - sb_lines - 8 - 2;
  186. x = 8;
  187. lost = CL_CalcNet();
  188. for (a=NET_TIMINGS-w ; a<w ; a++)
  189. {
  190. i = (cls.netchan.outgoing_sequence-a) & NET_TIMINGSMASK;
  191. R_LineGraph (x+w-1-a, y, packet_latency[i]);
  192. }
  193. sprintf(st, "%3i%% packet loss", lost);
  194. Draw_String(8, y2, st);
  195. }
  196. /*
  197. ==============
  198. R_ZGraph
  199. ==============
  200. */
  201. void R_ZGraph (void)
  202. {
  203. int a, x, w, i;
  204. static int height[256];
  205. if (r_refdef.vrect.width <= 256)
  206. w = r_refdef.vrect.width;
  207. else
  208. w = 256;
  209. height[r_framecount&255] = ((int)r_origin[2]) & 31;
  210. x = 0;
  211. for (a=0 ; a<w ; a++)
  212. {
  213. i = (r_framecount-a) & 255;
  214. R_LineGraph (x+w-1-a, r_refdef.vrect.height-2, height[i]);
  215. }
  216. }
  217. /*
  218. =============
  219. R_PrintTimes
  220. =============
  221. */
  222. void R_PrintTimes (void)
  223. {
  224. float r_time2;
  225. float ms;
  226. r_time2 = Sys_DoubleTime ();
  227. ms = 1000* (r_time2 - r_time1);
  228. Con_Printf ("%5.1f ms %3i/%3i/%3i poly %3i surf\n",
  229. ms, c_faceclip, r_polycount, r_drawnpolycount, c_surf);
  230. c_surf = 0;
  231. }
  232. /*
  233. =============
  234. R_PrintDSpeeds
  235. =============
  236. */
  237. void R_PrintDSpeeds (void)
  238. {
  239. float ms, dp_time, r_time2, rw_time, db_time, se_time, de_time, dv_time;
  240. r_time2 = Sys_DoubleTime ();
  241. dp_time = (dp_time2 - dp_time1) * 1000;
  242. rw_time = (rw_time2 - rw_time1) * 1000;
  243. db_time = (db_time2 - db_time1) * 1000;
  244. se_time = (se_time2 - se_time1) * 1000;
  245. de_time = (de_time2 - de_time1) * 1000;
  246. dv_time = (dv_time2 - dv_time1) * 1000;
  247. ms = (r_time2 - r_time1) * 1000;
  248. Con_Printf ("%3i %4.1fp %3iw %4.1fb %3is %4.1fe %4.1fv\n",
  249. (int)ms, dp_time, (int)rw_time, db_time, (int)se_time, de_time,
  250. dv_time);
  251. }
  252. /*
  253. =============
  254. R_PrintAliasStats
  255. =============
  256. */
  257. void R_PrintAliasStats (void)
  258. {
  259. Con_Printf ("%3i polygon model drawn\n", r_amodels_drawn);
  260. }
  261. void WarpPalette (void)
  262. {
  263. int i,j;
  264. byte newpalette[768];
  265. int basecolor[3];
  266. basecolor[0] = 130;
  267. basecolor[1] = 80;
  268. basecolor[2] = 50;
  269. // pull the colors halfway to bright brown
  270. for (i=0 ; i<256 ; i++)
  271. {
  272. for (j=0 ; j<3 ; j++)
  273. {
  274. newpalette[i*3+j] = (host_basepal[i*3+j] + basecolor[j])/2;
  275. }
  276. }
  277. VID_ShiftPalette (newpalette);
  278. }
  279. /*
  280. ===================
  281. R_TransformFrustum
  282. ===================
  283. */
  284. void R_TransformFrustum (void)
  285. {
  286. int i;
  287. vec3_t v, v2;
  288. for (i=0 ; i<4 ; i++)
  289. {
  290. v[0] = screenedge[i].normal[2];
  291. v[1] = -screenedge[i].normal[0];
  292. v[2] = screenedge[i].normal[1];
  293. v2[0] = v[1]*vright[0] + v[2]*vup[0] + v[0]*vpn[0];
  294. v2[1] = v[1]*vright[1] + v[2]*vup[1] + v[0]*vpn[1];
  295. v2[2] = v[1]*vright[2] + v[2]*vup[2] + v[0]*vpn[2];
  296. VectorCopy (v2, view_clipplanes[i].normal);
  297. view_clipplanes[i].dist = DotProduct (modelorg, v2);
  298. }
  299. }
  300. #if !id386
  301. /*
  302. ================
  303. TransformVector
  304. ================
  305. */
  306. void TransformVector (vec3_t in, vec3_t out)
  307. {
  308. out[0] = DotProduct(in,vright);
  309. out[1] = DotProduct(in,vup);
  310. out[2] = DotProduct(in,vpn);
  311. }
  312. #endif
  313. /*
  314. ================
  315. R_TransformPlane
  316. ================
  317. */
  318. void R_TransformPlane (mplane_t *p, float *normal, float *dist)
  319. {
  320. float d;
  321. d = DotProduct (r_origin, p->normal);
  322. *dist = p->dist - d;
  323. // TODO: when we have rotating entities, this will need to use the view matrix
  324. TransformVector (p->normal, normal);
  325. }
  326. /*
  327. ===============
  328. R_SetUpFrustumIndexes
  329. ===============
  330. */
  331. void R_SetUpFrustumIndexes (void)
  332. {
  333. int i, j, *pindex;
  334. pindex = r_frustum_indexes;
  335. for (i=0 ; i<4 ; i++)
  336. {
  337. for (j=0 ; j<3 ; j++)
  338. {
  339. if (view_clipplanes[i].normal[j] < 0)
  340. {
  341. pindex[j] = j;
  342. pindex[j+3] = j+3;
  343. }
  344. else
  345. {
  346. pindex[j] = j+3;
  347. pindex[j+3] = j;
  348. }
  349. }
  350. // FIXME: do just once at start
  351. pfrustum_indexes[i] = pindex;
  352. pindex += 6;
  353. }
  354. }
  355. /*
  356. ===============
  357. R_SetupFrame
  358. ===============
  359. */
  360. void R_SetupFrame (void)
  361. {
  362. int edgecount;
  363. vrect_t vrect;
  364. float w, h;
  365. // don't allow cheats in multiplayer
  366. r_draworder.value = 0;
  367. r_fullbright.value = 0;
  368. r_ambient.value = 0;
  369. r_drawflat.value = 0;
  370. if (r_numsurfs.value)
  371. {
  372. if ((surface_p - surfaces) > r_maxsurfsseen)
  373. r_maxsurfsseen = surface_p - surfaces;
  374. Con_Printf ("Used %d of %d surfs; %d max\n", surface_p - surfaces,
  375. surf_max - surfaces, r_maxsurfsseen);
  376. }
  377. if (r_numedges.value)
  378. {
  379. edgecount = edge_p - r_edges;
  380. if (edgecount > r_maxedgesseen)
  381. r_maxedgesseen = edgecount;
  382. Con_Printf ("Used %d of %d edges; %d max\n", edgecount,
  383. r_numallocatededges, r_maxedgesseen);
  384. }
  385. r_refdef.ambientlight = r_ambient.value;
  386. if (r_refdef.ambientlight < 0)
  387. r_refdef.ambientlight = 0;
  388. // if (!sv.active)
  389. r_draworder.value = 0; // don't let cheaters look behind walls
  390. R_CheckVariables ();
  391. R_AnimateLight ();
  392. r_framecount++;
  393. numbtofpolys = 0;
  394. // debugging
  395. #if 0
  396. r_refdef.vieworg[0]= 80;
  397. r_refdef.vieworg[1]= 64;
  398. r_refdef.vieworg[2]= 40;
  399. r_refdef.viewangles[0]= 0;
  400. r_refdef.viewangles[1]= 46.763641357;
  401. r_refdef.viewangles[2]= 0;
  402. #endif
  403. // build the transformation matrix for the given view angles
  404. VectorCopy (r_refdef.vieworg, modelorg);
  405. VectorCopy (r_refdef.vieworg, r_origin);
  406. AngleVectors (r_refdef.viewangles, vpn, vright, vup);
  407. // current viewleaf
  408. r_oldviewleaf = r_viewleaf;
  409. r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
  410. r_dowarpold = r_dowarp;
  411. r_dowarp = r_waterwarp.value && (r_viewleaf->contents <= CONTENTS_WATER);
  412. if ((r_dowarp != r_dowarpold) || r_viewchanged)
  413. {
  414. if (r_dowarp)
  415. {
  416. if ((vid.width <= vid.maxwarpwidth) &&
  417. (vid.height <= vid.maxwarpheight))
  418. {
  419. vrect.x = 0;
  420. vrect.y = 0;
  421. vrect.width = vid.width;
  422. vrect.height = vid.height;
  423. R_ViewChanged (&vrect, sb_lines, vid.aspect);
  424. }
  425. else
  426. {
  427. w = vid.width;
  428. h = vid.height;
  429. if (w > vid.maxwarpwidth)
  430. {
  431. h *= (float)vid.maxwarpwidth / w;
  432. w = vid.maxwarpwidth;
  433. }
  434. if (h > vid.maxwarpheight)
  435. {
  436. h = vid.maxwarpheight;
  437. w *= (float)vid.maxwarpheight / h;
  438. }
  439. vrect.x = 0;
  440. vrect.y = 0;
  441. vrect.width = (int)w;
  442. vrect.height = (int)h;
  443. R_ViewChanged (&vrect,
  444. (int)((float)sb_lines * (h/(float)vid.height)),
  445. vid.aspect * (h / w) *
  446. ((float)vid.width / (float)vid.height));
  447. }
  448. }
  449. else
  450. {
  451. vrect.x = 0;
  452. vrect.y = 0;
  453. vrect.width = vid.width;
  454. vrect.height = vid.height;
  455. R_ViewChanged (&vrect, sb_lines, vid.aspect);
  456. }
  457. r_viewchanged = false;
  458. }
  459. // start off with just the four screen edge clip planes
  460. R_TransformFrustum ();
  461. // save base values
  462. VectorCopy (vpn, base_vpn);
  463. VectorCopy (vright, base_vright);
  464. VectorCopy (vup, base_vup);
  465. VectorCopy (modelorg, base_modelorg);
  466. R_SetSkyFrame ();
  467. R_SetUpFrustumIndexes ();
  468. r_cache_thrash = false;
  469. // clear frame counts
  470. c_faceclip = 0;
  471. d_spanpixcount = 0;
  472. r_polycount = 0;
  473. r_drawnpolycount = 0;
  474. r_wholepolycount = 0;
  475. r_amodels_drawn = 0;
  476. r_outofsurfaces = 0;
  477. r_outofedges = 0;
  478. D_SetupFrame ();
  479. }