git-svn-id: http://bladebattles.com/kurok/SVN@11 20cd92bb-ff49-0410-b73e-96a06e42c3b9
[kurok.git] / r_misc.c
blob12a136c5ad944b45e467e0eaf104c67dfd69360e
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // r_misc.c
22 #include "quakedef.h"
23 #include "r_local.h"
27 ===============
28 R_CheckVariables
29 ===============
31 void R_CheckVariables (void)
33 static float oldbright;
35 if (r_fullbright.value != oldbright)
37 oldbright = r_fullbright.value;
38 D_FlushCaches (); // so all lighting changes
44 ============
45 Show
47 Debugging use
48 ============
50 void Show (void)
52 vrect_t vr;
54 vr.x = vr.y = 0;
55 vr.width = vid.width;
56 vr.height = vid.height;
57 vr.pnext = NULL;
58 VID_Update (&vr);
63 ====================
64 R_TimeRefresh_f
66 For program optimization
67 ====================
69 void R_TimeRefresh_f (void)
71 int i;
72 double start, stop, time;
73 int startangle;
74 vrect_t vr;
76 #ifdef PSP_SOFTWARE_VIDEO
77 startangle = r_refdef.viewangles[1];
79 start = Sys_FloatTime ();
80 for (i=0 ; i<128 ; i++)
82 r_refdef.viewangles[1] = i/128.0*360.0;
84 VID_LockBuffer ();
86 R_RenderView ();
88 VID_UnlockBuffer ();
90 vr.x = r_refdef.vrect.x;
91 vr.y = r_refdef.vrect.y;
92 vr.width = r_refdef.vrect.width;
93 vr.height = r_refdef.vrect.height;
94 vr.pnext = NULL;
95 VID_Update (&vr);
97 stop = Sys_FloatTime ();
98 time = stop-start;
99 Con_Printf ("%f seconds (%f fps)\n", time, 128/time);
101 r_refdef.viewangles[1] = startangle;
102 #else
103 Con_Printf ("Sorry: 'timerefresh' command disabled\n");
104 #endif
109 ================
110 R_LineGraph
112 Only called by R_DisplayTime
113 ================
115 void R_LineGraph (int x, int y, int h)
117 int i;
118 byte *dest;
119 int s;
121 // FIXME: should be disabled on no-buffer adapters, or should be in the driver
123 x += r_refdef.vrect.x;
124 y += r_refdef.vrect.y;
126 dest = vid.buffer + vid.rowbytes*y + x;
128 s = r_graphheight.value;
130 if (h>s)
131 h = s;
133 for (i=0 ; i<h ; i++, dest -= vid.rowbytes*2)
135 dest[0] = 0xff;
136 *(dest-vid.rowbytes) = 0x30;
138 for ( ; i<s ; i++, dest -= vid.rowbytes*2)
140 dest[0] = 0x30;
141 *(dest-vid.rowbytes) = 0x30;
146 ==============
147 R_TimeGraph
149 Performance monitoring tool
150 ==============
152 #define MAX_TIMINGS 100
153 extern float mouse_x, mouse_y;
154 void R_TimeGraph (void)
156 static int timex;
157 int a;
158 double r_time2;
159 static byte r_timings[MAX_TIMINGS];
160 int x;
162 r_time2 = Sys_FloatTime ();
164 a = (r_time2-r_time1)/0.01;
165 //a = fabsf(mouse_y * 0.05);
166 //a = (int)((r_refdef.vieworg[2] + 1024)/1)%(int)r_graphheight.value;
167 //a = fabsf(velocity[0])/20;
168 //a = ((int)fabsf(origin[0])/8)%20;
169 //a = (cl.idealpitch + 30)/5;
170 r_timings[timex] = a;
171 a = timex;
173 if (r_refdef.vrect.width <= MAX_TIMINGS)
174 x = r_refdef.vrect.width-1;
175 else
176 x = r_refdef.vrect.width -
177 (r_refdef.vrect.width - MAX_TIMINGS)/2;
180 R_LineGraph (x, r_refdef.vrect.height-2, r_timings[a]);
181 if (x==0)
182 break; // screen too small to hold entire thing
183 x--;
184 a--;
185 if (a == -1)
186 a = MAX_TIMINGS-1;
187 } while (a != timex);
189 timex = (timex+1)%MAX_TIMINGS;
194 =============
195 R_PrintTimes
196 =============
198 void R_PrintTimes (void)
200 double r_time2;
201 double ms;
203 r_time2 = Sys_FloatTime ();
205 ms = 1000* (r_time2 - r_time1);
207 Con_Printf ("%5.1f ms %3i/%3i/%3i poly %3i surf\n",
208 ms, c_faceclip, r_polycount, r_drawnpolycount, c_surf);
209 c_surf = 0;
214 =============
215 R_PrintDSpeeds
216 =============
218 void R_PrintDSpeeds (void)
220 float ms, dp_time, r_time2, rw_time, db_time, se_time, de_time, dv_time;
222 r_time2 = Sys_FloatTime ();
224 dp_time = (dp_time2 - dp_time1) * 1000;
225 rw_time = (rw_time2 - rw_time1) * 1000;
226 db_time = (db_time2 - db_time1) * 1000;
227 se_time = (se_time2 - se_time1) * 1000;
228 de_time = (de_time2 - de_time1) * 1000;
229 dv_time = (dv_time2 - dv_time1) * 1000;
230 ms = (r_time2 - r_time1) * 1000;
232 Con_Printf ("%3i %4.1fp %3iw %4.1fb %3is %4.1fe %4.1fv\n",
233 (int)ms, dp_time, (int)rw_time, db_time, (int)se_time, de_time,
234 dv_time);
239 =============
240 R_PrintAliasStats
241 =============
243 void R_PrintAliasStats (void)
245 Con_Printf ("%3i polygon model drawn\n", r_amodels_drawn);
249 void WarpPalette (void)
251 int i,j;
252 byte newpalette[768];
253 int basecolor[3];
255 basecolor[0] = 130;
256 basecolor[1] = 80;
257 basecolor[2] = 50;
259 // pull the colors halfway to bright brown
260 for (i=0 ; i<256 ; i++)
262 for (j=0 ; j<3 ; j++)
264 newpalette[i*3+j] = (host_basepal[i*3+j] + basecolor[j])/2;
268 VID_ShiftPalette (newpalette);
273 ===================
274 R_TransformFrustum
275 ===================
277 void R_TransformFrustum (void)
279 int i;
280 vec3_t v, v2;
282 for (i=0 ; i<4 ; i++)
284 v[0] = screenedge[i].normal[2];
285 v[1] = -screenedge[i].normal[0];
286 v[2] = screenedge[i].normal[1];
288 v2[0] = v[1]*vright[0] + v[2]*vup[0] + v[0]*vpn[0];
289 v2[1] = v[1]*vright[1] + v[2]*vup[1] + v[0]*vpn[1];
290 v2[2] = v[1]*vright[2] + v[2]*vup[2] + v[0]*vpn[2];
292 VectorCopy (v2, view_clipplanes[i].normal);
294 view_clipplanes[i].dist = DotProduct (modelorg, v2);
299 #if !id386
302 ================
303 TransformVector
304 ================
306 void TransformVector (vec3_t in, vec3_t out)
308 out[0] = DotProduct(in,vright);
309 out[1] = DotProduct(in,vup);
310 out[2] = DotProduct(in,vpn);
313 #endif
317 ================
318 R_TransformPlane
319 ================
321 void R_TransformPlane (mplane_t *p, float *normal, float *dist)
323 float d;
325 d = DotProduct (r_origin, p->normal);
326 *dist = p->dist - d;
327 // TODO: when we have rotating entities, this will need to use the view matrix
328 TransformVector (p->normal, normal);
333 ===============
334 R_SetUpFrustumIndexes
335 ===============
337 void R_SetUpFrustumIndexes (void)
339 int i, j, *pindex;
341 pindex = r_frustum_indexes;
343 for (i=0 ; i<4 ; i++)
345 for (j=0 ; j<3 ; j++)
347 if (view_clipplanes[i].normal[j] < 0)
349 pindex[j] = j;
350 pindex[j+3] = j+3;
352 else
354 pindex[j] = j+3;
355 pindex[j+3] = j;
359 // FIXME: do just once at start
360 pfrustum_indexes[i] = pindex;
361 pindex += 6;
367 ===============
368 R_SetupFrame
369 ===============
371 void R_SetupFrame (void)
373 int edgecount;
374 vrect_t vrect;
375 float w, h;
377 // don't allow cheats in multiplayer
378 if (cl.maxclients > 1)
380 Cvar_Set ("r_draworder", "0");
381 Cvar_Set ("r_fullbright", "0");
382 Cvar_Set ("r_ambient", "0");
383 Cvar_Set ("r_drawflat", "0");
386 if (r_numsurfs.value)
388 if ((surface_p - surfaces) > r_maxsurfsseen)
389 r_maxsurfsseen = surface_p - surfaces;
391 Con_Printf ("Used %d of %d surfs; %d max\n", surface_p - surfaces,
392 surf_max - surfaces, r_maxsurfsseen);
395 if (r_numedges.value)
397 edgecount = edge_p - r_edges;
399 if (edgecount > r_maxedgesseen)
400 r_maxedgesseen = edgecount;
402 Con_Printf ("Used %d of %d edges; %d max\n", edgecount,
403 r_numallocatededges, r_maxedgesseen);
406 r_refdef.ambientlight = r_ambient.value;
408 if (r_refdef.ambientlight < 0)
409 r_refdef.ambientlight = 0;
411 if (!sv.active)
412 r_draworder.value = 0; // don't let cheaters look behind walls
414 R_CheckVariables ();
416 R_AnimateLight ();
418 r_framecount++;
420 numbtofpolys = 0;
422 // debugging
423 #if 0
424 r_refdef.vieworg[0]= 80;
425 r_refdef.vieworg[1]= 64;
426 r_refdef.vieworg[2]= 40;
427 r_refdef.viewangles[0]= 0;
428 r_refdef.viewangles[1]= 46.763641357;
429 r_refdef.viewangles[2]= 0;
430 #endif
432 // build the transformation matrix for the given view angles
433 VectorCopy (r_refdef.vieworg, modelorg);
434 VectorCopy (r_refdef.vieworg, r_origin);
436 AngleVectors (r_refdef.viewangles, vpn, vright, vup);
438 // current viewleaf
439 r_oldviewleaf = r_viewleaf;
440 r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
442 r_dowarpold = r_dowarp;
443 r_dowarp = r_waterwarp.value && (r_viewleaf->contents <= CONTENTS_WATER);
445 if ((r_dowarp != r_dowarpold) || r_viewchanged || lcd_x.value)
447 if (r_dowarp)
449 if ((vid.width <= vid.maxwarpwidth) &&
450 (vid.height <= vid.maxwarpheight))
452 vrect.x = 0;
453 vrect.y = 0;
454 vrect.width = vid.width;
455 vrect.height = vid.height;
457 R_ViewChanged (&vrect, sb_lines, vid.aspect);
459 else
461 w = vid.width;
462 h = vid.height;
464 if (w > vid.maxwarpwidth)
466 h *= (float)vid.maxwarpwidth / w;
467 w = vid.maxwarpwidth;
470 if (h > vid.maxwarpheight)
472 h = vid.maxwarpheight;
473 w *= (float)vid.maxwarpheight / h;
476 vrect.x = 0;
477 vrect.y = 0;
478 vrect.width = (int)w;
479 vrect.height = (int)h;
481 R_ViewChanged (&vrect,
482 (int)((float)sb_lines * (h/(float)vid.height)),
483 vid.aspect * (h / w) *
484 ((float)vid.width / (float)vid.height));
487 else
489 vrect.x = 0;
490 vrect.y = 0;
491 vrect.width = vid.width;
492 vrect.height = vid.height;
494 R_ViewChanged (&vrect, sb_lines, vid.aspect);
497 r_viewchanged = false;
500 // start off with just the four screen edge clip planes
501 R_TransformFrustum ();
503 // save base values
504 VectorCopy (vpn, base_vpn);
505 VectorCopy (vright, base_vright);
506 VectorCopy (vup, base_vup);
507 VectorCopy (modelorg, base_modelorg);
509 R_SetSkyFrame ();
511 R_SetUpFrustumIndexes ();
513 r_cache_thrash = false;
515 // clear frame counts
516 c_faceclip = 0;
517 d_spanpixcount = 0;
518 r_polycount = 0;
519 r_drawnpolycount = 0;
520 r_wholepolycount = 0;
521 r_amodels_drawn = 0;
522 r_outofsurfaces = 0;
523 r_outofedges = 0;
525 D_SetupFrame ();