1 /*
2 * r_misc.c --
3 * $Id: r_misc.c,v 1.10 2007-07-31 21:03:28 sezero Exp $
4 *
5 * Copyright (C) 1996-1997 Id Software, Inc.
6 * Copyright (C) 1997-1998 Raven Software Corp.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * See the GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "quakedef.h"
25 #include "r_local.h"
26
27
28 /*
29 ===============
30 R_CheckVariables
31 ===============
32 */
R_CheckVariables(void)33 static void R_CheckVariables (void)
34 {
35 static int oldbright;
36
37 if (r_fullbright.integer != oldbright)
38 {
39 oldbright = r_fullbright.integer;
40 D_FlushCaches (); // so all lighting changes
41 }
42 }
43
44
45 /*
46 ====================
47 R_TimeRefresh_f
48
49 For program optimization
50 ====================
51 */
R_TimeRefresh_f(void)52 void R_TimeRefresh_f (void)
53 {
54 int i;
55 float start, stop, time;
56 int startangle;
57 vrect_t vr;
58
59 if (cls.state != ca_active)
60 {
61 Con_Printf("Not connected to a server\n");
62 return;
63 }
64
65 startangle = r_refdef.viewangles[1];
66
67 start = Sys_DoubleTime ();
68 for (i = 0; i < 128; i++)
69 {
70 r_refdef.viewangles[1] = i/128.0*360.0;
71
72 VID_LockBuffer ();
73
74 R_RenderView ();
75
76 VID_UnlockBuffer ();
77
78 vr.x = r_refdef.vrect.x;
79 vr.y = r_refdef.vrect.y;
80 vr.width = r_refdef.vrect.width;
81 vr.height = r_refdef.vrect.height;
82 vr.pnext = NULL;
83 VID_Update (&vr);
84 }
85 stop = Sys_DoubleTime ();
86 time = stop-start;
87 Con_Printf ("%f seconds (%f fps)\n", time, 128/time);
88
89 r_refdef.viewangles[1] = startangle;
90 }
91
92 /*
93 ================
94 R_LineGraph
95
96 Only called by R_DisplayTime
97 ================
98 */
R_LineGraph(int x,int y,int h)99 static void R_LineGraph (int x, int y, int h)
100 {
101 int i;
102 byte *dest;
103 int s;
104 int color;
105
106 // FIXME: should be disabled on no-buffer adapters, or should be in the driver
107
108 // x += r_refdef.vrect.x;
109 // y += r_refdef.vrect.y;
110
111 dest = vid.buffer + vid.rowbytes*y + x;
112
113 s = r_graphheight.integer;
114
115 if (h == 10000)
116 color = 175; // yellow
117 else if (h == 9999)
118 color = 143; // red
119 else if (h == 9998)
120 color = 249; // blue
121 else
122 color = 242; // pink
123
124 if (h > s)
125 h = s;
126
127 for (i = 0; i < h; i++, dest -= vid.rowbytes*2)
128 {
129 dest[0] = color;
130 // *(dest-vid.rowbytes) = 0x30;
131 }
132 #if 0
133 for ( ; i < s; i++, dest -= vid.rowbytes*2)
134 {
135 dest[0] = 0x30;
136 *(dest-vid.rowbytes) = 0x30;
137 }
138 #endif
139 }
140
141 /*
142 ================
143 R_LineGraph
144 ================
145 */
R_LineGraph2(int x,int y,int h,int h2,int drawType,int marker)146 static void R_LineGraph2 (int x, int y, int h, int h2, int drawType, int marker)
147 {
148 int i;
149 byte *dest;
150 int s;
151
152 // FIXME: should be disabled on no-buffer adapters, or should be in the driver
153
154 // drawType
155 // 0-000: expanded / solid / background
156 // 1-001: condensed / background
157 // 2-010: expanded / holey / background
158 // 3-011: condensed / background
159 // 4-100: expanded / solid / no background
160 // 5-101: condensed / no background
161 // 6-110: expanded / holey / no background
162 // 7-111: condensed / no background
163
164 x += r_refdef.vrect.x;
165 y += r_refdef.vrect.y;
166 dest = vid.buffer + vid.rowbytes*y + x;
167 s = r_graphheight.integer;
168 if (s > r_refdef.vrect.height)
169 {
170 s = r_refdef.vrect.height;
171 }
172
173 // show actual received bytes here (h)
174 if (h > s)
175 {
176 h = s;
177 }
178 for (i = 0; i < h; i++, dest -= vid.rowbytes)
179 {
180 dest[0] = ((i+1) % marker == 0) ? 143 : 255;
181 if (!(drawType & 1))
182 { // Expanded
183 if (!(drawType & 2))
184 { // Solid
185 *(dest-vid.rowbytes) = 53;
186 }
187 dest -= vid.rowbytes;
188 }
189 }
190
191 // show uncompressed bytes here (h2)
192 if (h2 > s)
193 {
194 h2 = s;
195 }
196 for (i = h; i < h2; i++, dest -= vid.rowbytes)
197 {
198 dest[0] = ((i+1) % marker == 0) ? 130 : 244;
199 if (!(drawType & 1))
200 { // Expanded
201 if (!(drawType & 2))
202 { // Solid
203 *(dest-vid.rowbytes) = 53;
204 }
205 dest -= vid.rowbytes;
206 }
207 }
208
209 if (drawType & 4)
210 { // No background
211 return;
212 }
213 for ( ; i < s; i++, dest -= vid.rowbytes)
214 {
215 dest[0] = 53;
216 if (!(drawType & 1))
217 { // Expanded
218 if (!(drawType & 2))
219 { // Solid
220 *(dest-vid.rowbytes) = 53;
221 }
222 dest -= vid.rowbytes;
223 }
224 }
225 }
226
227 /*
228 ==============
229 R_TimeGraph
230
231 Performance monitoring tool
232 ==============
233 */
234 #define MAX_TIMINGS 100
235 #define GRAPH_TYPE_COUNT 2
236 extern int LastServerMessageSize; // uncompressed
237 extern int LastCompMessageSize; // compressed
R_TimeGraph(void)238 void R_TimeGraph (void)
239 {
240 int a, x;
241 int drawType, graphType;
242 static int timex;
243 static byte r_timings[MAX_TIMINGS]; //compressed
244 static byte r_timings2[MAX_TIMINGS];//uncompressed
245 static int graphMarkers[GRAPH_TYPE_COUNT] =
246 {
247 10000,
248 10
249 };
250
251 graphType = r_timegraph.integer;
252 if (graphType < 1 || graphType > GRAPH_TYPE_COUNT)
253 {
254 return;
255 }
256 drawType = (int)((r_timegraph.value-floor(r_timegraph.value)+1E-3)*10);
257
258 if (graphType == 1)
259 { // Frame times
260 a = (Sys_DoubleTime() - r_time1) / 0.01;
261 r_timings[timex] = a;
262 r_timings2[timex] = a;
263 }
264 else
265 { // Packet sizes
266 a = LastCompMessageSize/10;
267 LastCompMessageSize = 0;
268 r_timings[timex] = a;
269 a = LastServerMessageSize/10;
270 LastServerMessageSize = 0;
271 r_timings2[timex] = a;
272 }
273
274 a = timex;
275
276 if (r_refdef.vrect.width <= MAX_TIMINGS)
277 {
278 x = r_refdef.vrect.width-1;
279 }
280 else
281 {
282 x = r_refdef.vrect.width - (r_refdef.vrect.width-MAX_TIMINGS)/2;
283 }
284
285 do
286 {
287 R_LineGraph2(x, r_refdef.vrect.height-2, r_timings[a], r_timings2[a], drawType,
288 graphMarkers[graphType-1]);
289 if (x == 0)
290 {
291 break; // screen too small to hold entire thing
292 }
293 x--;
294 a--;
295 if (a == -1)
296 {
297 a = MAX_TIMINGS-1;
298 }
299 } while (a != timex);
300
301 timex = (timex+1) % MAX_TIMINGS;
302 }
303
304 /*
305 ==============
306 R_NetGraph
307 ==============
308 */
309 #define NET_TIMINGS 256
R_NetGraph(void)310 void R_NetGraph (void)
311 {
312 int a, x, y, y2, w, i;
313 frame_t *frame;
314 int lost;
315 char st[80];
316 static int packet_latency[256];
317
318 if (vid.width - 16 <= NET_TIMINGS)
319 w = vid.width - 16;
320 else
321 w = NET_TIMINGS;
322
323 for (i = cls.netchan.outgoing_sequence-UPDATE_BACKUP+1 ;
324 i <= cls.netchan.outgoing_sequence ;
325 i++)
326 {
327 frame = &cl.frames[i&UPDATE_MASK];
328 if (frame->receivedtime == -1)
329 packet_latency[i&255] = 9999; // dropped
330 else if (frame->receivedtime == -2)
331 packet_latency[i&255] = 10000; // choked
332 else if (frame->invalid)
333 packet_latency[i&255] = 9998; // invalid delta
334 else
335 packet_latency[i&255] = (frame->receivedtime - frame->senttime)*20;
336 }
337
338 x = -((vid.width - 320)>>1);
339 y = vid.height - sb_lines - 24 - r_graphheight.integer*2 - 2;
340
341 M_DrawTextBox (x, y, (w+7)/8, (r_graphheight.integer*2+7)/8 + 1);
342 y2 = y + 8;
343 y = vid.height - sb_lines - 8 - 2;
344
345 x = 8;
346 lost = 0;
347 for (a = 0 ; a < w ; a++)
348 {
349 i = (cls.netchan.outgoing_sequence-a) & 255;
350 if (packet_latency[i] == 9999)
351 lost++;
352 R_LineGraph (x+w-1-a, y, packet_latency[i]);
353 }
354 sprintf(st, "%3i%% packet loss", lost*100/NET_TIMINGS);
355 Draw_String(8, y2, st);
356 }
357
358 /*
359 ==============
360 R_ZGraph
361 ==============
362 */
R_ZGraph(void)363 void R_ZGraph (void)
364 {
365 int a, x, w, i;
366 static int height[256];
367
368 if (r_refdef.vrect.width <= 256)
369 w = r_refdef.vrect.width;
370 else
371 w = 256;
372
373 height[r_framecount&255] = ((int)r_origin[2]) & 31;
374
375 x = 0;
376 for (a = 0 ; a < w ; a++)
377 {
378 i = (r_framecount-a) & 255;
379 R_LineGraph (x+w-1-a, r_refdef.vrect.height-2, height[i]);
380 }
381 }
382
383 /*
384 =============
385 R_PrintTimes
386 =============
387 */
R_PrintTimes(void)388 void R_PrintTimes(void)
389 {
390 float r_time2;
391 float ms, fps;
392
393 r_lasttime1 = r_time2 = Sys_DoubleTime();
394
395 ms = 1000 * (r_time2 - r_time1);
396 fps = 1000 / ms;
397
398 Con_Printf("%3.1f fps %5.0f ms\n%3i/%3i/%3i poly %3i surf\n",
399 fps, ms, c_faceclip, r_polycount, r_drawnpolycount, c_surf);
400
401 c_surf = 0;
402 }
403
404 /*
405 =============
406 R_PrintDSpeeds
407 =============
408 */
R_PrintDSpeeds(void)409 void R_PrintDSpeeds (void)
410 {
411 float ms, dp_time, r_time2, rw_time, db_time, se_time, de_time, dv_time;
412
413 r_time2 = Sys_DoubleTime ();
414
415 dp_time = (dp_time2 - dp_time1) * 1000;
416 rw_time = (rw_time2 - rw_time1) * 1000;
417 db_time = (db_time2 - db_time1) * 1000;
418 se_time = (se_time2 - se_time1) * 1000;
419 de_time = (de_time2 - de_time1) * 1000;
420 dv_time = (dv_time2 - dv_time1) * 1000;
421 ms = (r_time2 - r_time1) * 1000;
422
423 Con_Printf ("%3i %4.1fp %3iw %4.1fb %3is %4.1fe %4.1fv\n",
424 (int)ms, dp_time, (int)rw_time, db_time, (int)se_time, de_time,
425 dv_time);
426 }
427
428
429 /*
430 =============
431 R_PrintAliasStats
432 =============
433 */
R_PrintAliasStats(void)434 void R_PrintAliasStats (void)
435 {
436 Con_Printf ("%3i polygon model drawn\n", r_amodels_drawn);
437 }
438
439
440 /*
441 ===================
442 R_TransformFrustum
443 ===================
444 */
R_TransformFrustum(void)445 void R_TransformFrustum (void)
446 {
447 int i;
448 vec3_t v, v2;
449
450 for (i = 0 ; i < 4 ; i++)
451 {
452 v[0] = screenedge[i].normal[2];
453 v[1] = -screenedge[i].normal[0];
454 v[2] = screenedge[i].normal[1];
455
456 v2[0] = v[1]*vright[0] + v[2]*vup[0] + v[0]*vpn[0];
457 v2[1] = v[1]*vright[1] + v[2]*vup[1] + v[0]*vpn[1];
458 v2[2] = v[1]*vright[2] + v[2]*vup[2] + v[0]*vpn[2];
459
460 VectorCopy (v2, view_clipplanes[i].normal);
461
462 view_clipplanes[i].dist = DotProduct (modelorg, v2);
463 }
464 }
465
466
467 #if !id386 && !id68k
468
469 /*
470 ================
471 TransformVector
472 ================
473 */
TransformVector(vec3_t in,vec3_t out)474 void TransformVector (vec3_t in, vec3_t out)
475 {
476 out[0] = DotProduct(in,vright);
477 out[1] = DotProduct(in,vup);
478 out[2] = DotProduct(in,vpn);
479 }
480
481 #endif
482
483
484 /*
485 ================
486 R_TransformPlane
487 ================
488 */
R_TransformPlane(mplane_t * p,float * normal,float * dist)489 void R_TransformPlane (mplane_t *p, float *normal, float *dist)
490 {
491 float d;
492
493 d = DotProduct (r_origin, p->normal);
494 *dist = p->dist - d;
495 // TODO: when we have rotating entities, this will need to use the view matrix
496 TransformVector (p->normal, normal);
497 }
498
499
500 /*
501 ===============
502 R_SetUpFrustumIndexes
503 ===============
504 */
R_SetUpFrustumIndexes(void)505 static void R_SetUpFrustumIndexes (void)
506 {
507 int i, j, *pindex;
508
509 pindex = r_frustum_indexes;
510
511 for (i = 0 ; i < 4 ; i++)
512 {
513 for (j = 0 ; j < 3 ; j++)
514 {
515 if (view_clipplanes[i].normal[j] < 0)
516 {
517 pindex[j] = j;
518 pindex[j+3] = j+3;
519 }
520 else
521 {
522 pindex[j] = j+3;
523 pindex[j+3] = j;
524 }
525 }
526
527 // FIXME: do just once at start
528 pfrustum_indexes[i] = pindex;
529 pindex += 6;
530 }
531 }
532
533
534 /*
535 ===============
536 R_SetupFrame
537 ===============
538 */
R_SetupFrame(void)539 void R_SetupFrame (void)
540 {
541 int edgecount;
542 vrect_t vrect;
543 float w, h;
544
545 // don't allow cheats in multiplayer
546 r_draworder.integer = 0;
547 r_fullbright.integer = 0;
548 r_ambient.integer = 0;
549 r_drawflat.integer = 0;
550
551 if (r_numsurfs.integer)
552 {
553 if ((surface_p - surfaces) > r_maxsurfsseen)
554 r_maxsurfsseen = surface_p - surfaces;
555
556 Con_Printf ("Used %ld of %ld surfs; %d max\n", (long)(surface_p - surfaces),
557 (long)(surf_max - surfaces), r_maxsurfsseen);
558 }
559
560 if (r_numedges.integer)
561 {
562 edgecount = edge_p - r_edges;
563
564 if (edgecount > r_maxedgesseen)
565 r_maxedgesseen = edgecount;
566
567 Con_Printf ("Used %d of %d edges; %d max\n", edgecount,
568 r_numallocatededges, r_maxedgesseen);
569 }
570
571 r_refdef.ambientlight = r_ambient.integer;
572
573 if (r_refdef.ambientlight < 0)
574 r_refdef.ambientlight = 0;
575
576 // if (!sv.active)
577 r_draworder.integer = 0; // don't let cheaters look behind walls
578
579 R_CheckVariables ();
580
581 R_AnimateLight ();
582
583 r_framecount++;
584
585 numbtofpolys = 0;
586
587 #if 0
588 // debugging
589 r_refdef.vieworg[0] = 80;
590 r_refdef.vieworg[1] = 64;
591 r_refdef.vieworg[2] = 40;
592 r_refdef.viewangles[0] = 0;
593 r_refdef.viewangles[1] = 46.763641357;
594 r_refdef.viewangles[2] = 0;
595 #endif
596
597 // build the transformation matrix for the given view angles
598 VectorCopy (r_refdef.vieworg, modelorg);
599 VectorCopy (r_refdef.vieworg, r_origin);
600
601 AngleVectors (r_refdef.viewangles, vpn, vright, vup);
602
603 // current viewleaf
604 r_oldviewleaf = r_viewleaf;
605 r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
606
607 r_dowarpold = r_dowarp;
608 r_dowarp = r_waterwarp.integer && (r_viewleaf->contents <= CONTENTS_WATER);
609
610 if ((r_dowarp != r_dowarpold) || r_viewchanged)
611 {
612 if (r_dowarp)
613 {
614 if ((vid.width <= vid.maxwarpwidth) &&
615 (vid.height <= vid.maxwarpheight))
616 {
617 vrect.x = 0;
618 vrect.y = 0;
619 vrect.width = vid.width;
620 vrect.height = vid.height;
621
622 R_SetVrect (&vrect, &r_refdef.vrect, sb_lines);
623 R_ViewChanged (vid.aspect);
624 }
625 else
626 {
627 w = vid.width;
628 h = vid.height;
629
630 if (w > vid.maxwarpwidth)
631 {
632 h *= (float)vid.maxwarpwidth / w;
633 w = vid.maxwarpwidth;
634 }
635
636 if (h > vid.maxwarpheight)
637 {
638 h = vid.maxwarpheight;
639 w *= (float)vid.maxwarpheight / h;
640 }
641
642 vrect.x = 0;
643 vrect.y = 0;
644 vrect.width = (int)w;
645 vrect.height = (int)h;
646
647 R_SetVrect (&vrect, &r_refdef.vrect, (int)((float)sb_lines * (h/(float)vid.height)));
648 R_ViewChanged (vid.aspect * (h / w) * ((float)vid.width / (float)vid.height));
649 }
650 }
651 else
652 {
653 // scr_vrect alredy holds the original data,
654 // therefore no need for extra R_SetVrect()
655 r_refdef.vrect = scr_vrect;
656 R_ViewChanged (vid.aspect);
657 }
658
659 r_viewchanged = false;
660 }
661
662 // start off with just the four screen edge clip planes
663 R_TransformFrustum ();
664
665 // save base values
666 VectorCopy (vpn, base_vpn);
667 VectorCopy (vright, base_vright);
668 VectorCopy (vup, base_vup);
669 VectorCopy (modelorg, base_modelorg);
670
671 R_SetSkyFrame ();
672
673 R_SetUpFrustumIndexes ();
674
675 r_cache_thrash = false;
676
677 // clear frame counts
678 c_faceclip = 0;
679 d_spanpixcount = 0;
680 r_polycount = 0;
681 r_drawnpolycount = 0;
682 r_wholepolycount = 0;
683 r_amodels_drawn = 0;
684 r_outofsurfaces = 0;
685 r_outofedges = 0;
686
687 D_SetupFrame ();
688 }
689
690