r_misc.c 9.4 KB

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