implemented scissor rect clipping of lights in realtime lighting mode
[divverent/darkplaces.git] / gl_rmain.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // r_main.c
21
22 #include "quakedef.h"
23
24 // used for dlight push checking and other things
25 int r_framecount;
26
27 mplane_t frustum[4];
28
29 matrix4x4_t r_identitymatrix;
30
31 int c_alias_polys, c_light_polys, c_faces, c_nodes, c_leafs, c_models, c_bmodels, c_sprites, c_particles, c_dlights;
32
33 // true during envmap command capture
34 qboolean envmap;
35
36 float r_farclip;
37
38 // view origin
39 vec3_t r_origin;
40 vec3_t vpn;
41 vec3_t vright;
42 vec3_t vup;
43
44 //
45 // screen size info
46 //
47 refdef_t r_refdef;
48
49 // 8.8 fraction of base light value
50 unsigned short d_lightstylevalue[256];
51
52 cvar_t r_drawentities = {0, "r_drawentities","1"};
53 cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1"};
54 cvar_t r_shadows = {CVAR_SAVE, "r_shadows", "1"};
55 cvar_t r_staticworldlights = {0, "r_staticworldlights", "1"};
56 cvar_t r_speeds = {0, "r_speeds","0"};
57 cvar_t r_fullbright = {0, "r_fullbright","0"};
58 cvar_t r_wateralpha = {CVAR_SAVE, "r_wateralpha","1"};
59 cvar_t r_dynamic = {CVAR_SAVE, "r_dynamic","1"};
60 cvar_t r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1"};
61
62 cvar_t gl_fogenable = {0, "gl_fogenable", "0"};
63 cvar_t gl_fogdensity = {0, "gl_fogdensity", "0.25"};
64 cvar_t gl_fogred = {0, "gl_fogred","0.3"};
65 cvar_t gl_foggreen = {0, "gl_foggreen","0.3"};
66 cvar_t gl_fogblue = {0, "gl_fogblue","0.3"};
67 cvar_t gl_fogstart = {0, "gl_fogstart", "0"};
68 cvar_t gl_fogend = {0, "gl_fogend","0"};
69
70 cvar_t r_textureunits = {0, "r_textureunits", "32"};
71
72 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
73 {
74         int i;
75         for (i = 0;i < verts;i++)
76         {
77                 out[0] = in[0] * r;
78                 out[1] = in[1] * g;
79                 out[2] = in[2] * b;
80                 out[3] = in[3];
81                 in += 4;
82                 out += 4;
83         }
84 }
85
86 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
87 {
88         int i;
89         for (i = 0;i < verts;i++)
90         {
91                 out[0] = r;
92                 out[1] = g;
93                 out[2] = b;
94                 out[3] = a;
95                 out += 4;
96         }
97 }
98
99 /*
100 ====================
101 R_TimeRefresh_f
102
103 For program optimization
104 ====================
105 */
106 qboolean intimerefresh = 0;
107 static void R_TimeRefresh_f (void)
108 {
109         int i;
110         float start, stop, time;
111
112         intimerefresh = 1;
113         start = Sys_DoubleTime ();
114         for (i = 0;i < 128;i++)
115         {
116                 r_refdef.viewangles[0] = 0;
117                 r_refdef.viewangles[1] = i/128.0*360.0;
118                 r_refdef.viewangles[2] = 0;
119                 CL_UpdateScreen();
120         }
121
122         stop = Sys_DoubleTime ();
123         intimerefresh = 0;
124         time = stop-start;
125         Con_Printf ("%f seconds (%f fps)\n", time, 128/time);
126 }
127
128 vec3_t fogcolor;
129 vec_t fogdensity;
130 float fog_density, fog_red, fog_green, fog_blue;
131 qboolean fogenabled;
132 qboolean oldgl_fogenable;
133 void R_SetupFog(void)
134 {
135         if (gamemode == GAME_NEHAHRA)
136         {
137                 if (gl_fogenable.integer)
138                 {
139                         oldgl_fogenable = true;
140                         fog_density = gl_fogdensity.value;
141                         fog_red = gl_fogred.value;
142                         fog_green = gl_foggreen.value;
143                         fog_blue = gl_fogblue.value;
144                 }
145                 else if (oldgl_fogenable)
146                 {
147                         oldgl_fogenable = false;
148                         fog_density = 0;
149                         fog_red = 0;
150                         fog_green = 0;
151                         fog_blue = 0;
152                 }
153         }
154         if (fog_density)
155         {
156                 fogcolor[0] = fog_red   = bound(0.0f, fog_red  , 1.0f);
157                 fogcolor[1] = fog_green = bound(0.0f, fog_green, 1.0f);
158                 fogcolor[2] = fog_blue  = bound(0.0f, fog_blue , 1.0f);
159         }
160         if (fog_density)
161         {
162                 fogenabled = true;
163                 fogdensity = -4000.0f / (fog_density * fog_density);
164                 // fog color was already set
165         }
166         else
167                 fogenabled = false;
168 }
169
170 // FIXME: move this to client?
171 void FOG_clear(void)
172 {
173         if (gamemode == GAME_NEHAHRA)
174         {
175                 Cvar_Set("gl_fogenable", "0");
176                 Cvar_Set("gl_fogdensity", "0.2");
177                 Cvar_Set("gl_fogred", "0.3");
178                 Cvar_Set("gl_foggreen", "0.3");
179                 Cvar_Set("gl_fogblue", "0.3");
180         }
181         fog_density = fog_red = fog_green = fog_blue = 0.0f;
182 }
183
184 // FIXME: move this to client?
185 void FOG_registercvars(void)
186 {
187         if (gamemode == GAME_NEHAHRA)
188         {
189                 Cvar_RegisterVariable (&gl_fogenable);
190                 Cvar_RegisterVariable (&gl_fogdensity);
191                 Cvar_RegisterVariable (&gl_fogred);
192                 Cvar_RegisterVariable (&gl_foggreen);
193                 Cvar_RegisterVariable (&gl_fogblue);
194                 Cvar_RegisterVariable (&gl_fogstart);
195                 Cvar_RegisterVariable (&gl_fogend);
196         }
197 }
198
199 void gl_main_start(void)
200 {
201 }
202
203 void gl_main_shutdown(void)
204 {
205 }
206
207 extern void CL_ParseEntityLump(char *entitystring);
208 void gl_main_newmap(void)
209 {
210         if (cl.worldmodel && cl.worldmodel->entities)
211                 CL_ParseEntityLump(cl.worldmodel->entities);
212         r_framecount = 1;
213 }
214
215 void GL_Main_Init(void)
216 {
217         Matrix4x4_CreateIdentity(&r_identitymatrix);
218 // FIXME: move this to client?
219         FOG_registercvars();
220         Cmd_AddCommand ("timerefresh", R_TimeRefresh_f);
221         Cvar_RegisterVariable (&r_drawentities);
222         Cvar_RegisterVariable (&r_drawviewmodel);
223         Cvar_RegisterVariable (&r_shadows);
224         Cvar_RegisterVariable (&r_staticworldlights);
225         Cvar_RegisterVariable (&r_speeds);
226         Cvar_RegisterVariable (&r_fullbrights);
227         Cvar_RegisterVariable (&r_wateralpha);
228         Cvar_RegisterVariable (&r_dynamic);
229         Cvar_RegisterVariable (&r_fullbright);
230         Cvar_RegisterVariable (&r_textureunits);
231         if (gamemode == GAME_NEHAHRA)
232                 Cvar_SetValue("r_fullbrights", 0);
233         R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
234 }
235
236 vec3_t r_farclip_origin;
237 vec3_t r_farclip_direction;
238 vec_t r_farclip_directiondist;
239 vec_t r_farclip_meshfarclip;
240 int r_farclip_directionbit0;
241 int r_farclip_directionbit1;
242 int r_farclip_directionbit2;
243
244 // start a farclip measuring session
245 void R_FarClip_Start(vec3_t origin, vec3_t direction, vec_t startfarclip)
246 {
247         VectorCopy(origin, r_farclip_origin);
248         VectorCopy(direction, r_farclip_direction);
249         r_farclip_directiondist = DotProduct(r_farclip_origin, r_farclip_direction);
250         r_farclip_directionbit0 = r_farclip_direction[0] < 0;
251         r_farclip_directionbit1 = r_farclip_direction[1] < 0;
252         r_farclip_directionbit2 = r_farclip_direction[2] < 0;
253         r_farclip_meshfarclip = r_farclip_directiondist + startfarclip;
254 }
255
256 // enlarge farclip to accomodate box
257 void R_FarClip_Box(vec3_t mins, vec3_t maxs)
258 {
259         float d;
260         d = (r_farclip_directionbit0 ? mins[0] : maxs[0]) * r_farclip_direction[0]
261           + (r_farclip_directionbit1 ? mins[1] : maxs[1]) * r_farclip_direction[1]
262           + (r_farclip_directionbit2 ? mins[2] : maxs[2]) * r_farclip_direction[2];
263         if (r_farclip_meshfarclip < d)
264                 r_farclip_meshfarclip = d;
265 }
266
267 // return farclip value
268 float R_FarClip_Finish(void)
269 {
270         return r_farclip_meshfarclip - r_farclip_directiondist;
271 }
272
273 /*
274 ===============
275 R_NewMap
276 ===============
277 */
278 void R_NewMap (void)
279 {
280         R_Modules_NewMap();
281 }
282
283 extern void R_Textures_Init(void);
284 extern void Mod_RenderInit(void);
285 extern void GL_Draw_Init(void);
286 extern void GL_Main_Init(void);
287 extern void R_Shadow_Init(void);
288 extern void GL_Models_Init(void);
289 extern void R_Sky_Init(void);
290 extern void GL_Surf_Init(void);
291 extern void R_Crosshairs_Init(void);
292 extern void R_Light_Init(void);
293 extern void R_Particles_Init(void);
294 extern void R_Explosion_Init(void);
295 extern void ui_init(void);
296 extern void gl_backend_init(void);
297 extern void Sbar_Init(void);
298
299 void Render_Init(void)
300 {
301         R_Textures_Init();
302         Mod_RenderInit();
303         gl_backend_init();
304         R_MeshQueue_Init();
305         GL_Draw_Init();
306         GL_Main_Init();
307         R_Shadow_Init();
308         GL_Models_Init();
309         R_Sky_Init();
310         GL_Surf_Init();
311         R_Crosshairs_Init();
312         R_Light_Init();
313         R_Particles_Init();
314         R_Explosion_Init();
315         ui_init();
316         Sbar_Init();
317 }
318
319 /*
320 ===============
321 GL_Init
322 ===============
323 */
324 extern char *ENGINE_EXTENSIONS;
325 void GL_Init (void)
326 {
327         VID_CheckExtensions();
328
329         // LordHavoc: report supported extensions
330         Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
331 }
332
333 int R_CullBox(const vec3_t mins, const vec3_t maxs)
334 {
335         int i;
336         mplane_t *p;
337         for (i = 0;i < 4;i++)
338         {
339                 p = frustum + i;
340                 switch(p->signbits)
341                 {
342                 default:
343                 case 0:
344                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
345                                 return true;
346                         break;
347                 case 1:
348                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
349                                 return true;
350                         break;
351                 case 2:
352                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
353                                 return true;
354                         break;
355                 case 3:
356                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
357                                 return true;
358                         break;
359                 case 4:
360                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
361                                 return true;
362                         break;
363                 case 5:
364                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
365                                 return true;
366                         break;
367                 case 6:
368                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
369                                 return true;
370                         break;
371                 case 7:
372                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
373                                 return true;
374                         break;
375                 }
376         }
377         return false;
378 }
379
380 int PVS_CullBox(const vec3_t mins, const vec3_t maxs)
381 {
382         int stackpos, sides;
383         mnode_t *node, *stack[4096];
384         stackpos = 0;
385         stack[stackpos++] = cl.worldmodel->nodes;
386         while (stackpos)
387         {
388                 node = stack[--stackpos];
389                 if (node->contents < 0)
390                 {
391                         if (((mleaf_t *)node)->pvsframe == cl.worldmodel->pvsframecount)
392                                 return false;
393                 }
394                 else
395                 {
396                         sides = BoxOnPlaneSide(mins, maxs, node->plane);
397                         if (sides & 2 && stackpos < 4096)
398                                 stack[stackpos++] = node->children[1];
399                         if (sides & 1 && stackpos < 4096)
400                                 stack[stackpos++] = node->children[0];
401                 }
402         }
403         return true;
404 }
405
406 int VIS_CullBox(const vec3_t mins, const vec3_t maxs)
407 {
408         int stackpos, sides;
409         mnode_t *node, *stack[4096];
410         if (R_CullBox(mins, maxs))
411                 return true;
412         stackpos = 0;
413         stack[stackpos++] = cl.worldmodel->nodes;
414         while (stackpos)
415         {
416                 node = stack[--stackpos];
417                 if (node->contents < 0)
418                 {
419                         if (((mleaf_t *)node)->visframe == r_framecount)
420                                 return false;
421                 }
422                 else
423                 {
424                         sides = BoxOnPlaneSide(mins, maxs, node->plane);
425                         if (sides & 2 && stackpos < 4096)
426                                 stack[stackpos++] = node->children[1];
427                         if (sides & 1 && stackpos < 4096)
428                                 stack[stackpos++] = node->children[0];
429                 }
430         }
431         return true;
432 }
433
434 int R_CullSphere(const vec3_t origin, vec_t radius)
435 {
436         return (DotProduct(frustum[0].normal, origin) + radius < frustum[0].dist
437              || DotProduct(frustum[1].normal, origin) + radius < frustum[1].dist
438              || DotProduct(frustum[2].normal, origin) + radius < frustum[2].dist
439              || DotProduct(frustum[3].normal, origin) + radius < frustum[3].dist);
440 }
441
442 int PVS_CullSphere(const vec3_t origin, vec_t radius)
443 {
444         int stackpos;
445         mnode_t *node, *stack[4096];
446         float dist;
447         stackpos = 0;
448         stack[stackpos++] = cl.worldmodel->nodes;
449         while (stackpos)
450         {
451                 node = stack[--stackpos];
452                 if (node->contents < 0)
453                 {
454                         if (((mleaf_t *)node)->pvsframe == cl.worldmodel->pvsframecount)
455                                 return false;
456                 }
457                 else
458                 {
459                         dist = PlaneDiff(origin, node->plane);
460                         if (dist <= radius)
461                                 stack[stackpos++] = node->children[1];
462                         if (dist >= -radius)
463                                 stack[stackpos++] = node->children[0];
464                 }
465         }
466         return true;
467 }
468
469 int VIS_CullSphere(const vec3_t origin, vec_t radius)
470 {
471         int stackpos;
472         mnode_t *node, *stack[4096];
473         float dist;
474         if (R_CullSphere(origin, radius))
475                 return true;
476         stackpos = 0;
477         stack[stackpos++] = cl.worldmodel->nodes;
478         while (stackpos)
479         {
480                 node = stack[--stackpos];
481                 if (node->contents < 0)
482                 {
483                         if (((mleaf_t *)node)->visframe == r_framecount)
484                                 return false;
485                 }
486                 else
487                 {
488                         dist = PlaneDiff(origin, node->plane);
489                         if (dist <= radius)
490                                 stack[stackpos++] = node->children[1];
491                         if (dist >= -radius)
492                                 stack[stackpos++] = node->children[0];
493                 }
494         }
495         return true;
496 }
497
498
499 //==================================================================================
500
501 static void R_MarkEntities (void)
502 {
503         int i;
504         vec3_t v;
505         entity_render_t *ent;
506
507         ent = &cl_entities[0].render;
508         Matrix4x4_CreateIdentity(&ent->matrix);
509         Matrix4x4_CreateIdentity(&ent->inversematrix);
510
511         if (cl.worldmodel)
512                 R_FarClip_Box(cl.worldmodel->normalmins, cl.worldmodel->normalmaxs);
513
514         if (!r_drawentities.integer)
515                 return;
516
517         for (i = 0;i < r_refdef.numentities;i++)
518         {
519                 ent = r_refdef.entities[i];
520                 Mod_CheckLoaded(ent->model);
521
522                 // move view-relative models to where they should be
523                 if (ent->flags & RENDER_VIEWMODEL)
524                 {
525                         // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
526                         ent->flags -= RENDER_VIEWMODEL;
527                         // transform origin
528                         VectorCopy(ent->origin, v);
529                         ent->origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
530                         ent->origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
531                         ent->origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
532                         // adjust angles
533                         VectorAdd(ent->angles, r_refdef.viewangles, ent->angles);
534                 }
535
536                 VectorCopy(ent->angles, v);
537                 if (!ent->model || ent->model->type != mod_brush)
538                         v[0] = -v[0];
539                 Matrix4x4_CreateFromQuakeEntity(&ent->matrix, ent->origin[0], ent->origin[1], ent->origin[2], v[0], v[1], v[2], ent->scale);
540                 Matrix4x4_Invert_Simple(&ent->inversematrix, &ent->matrix);
541                 R_LerpAnimation(ent);
542                 R_UpdateEntLights(ent);
543                 if ((chase_active.integer || !(ent->flags & RENDER_EXTERIORMODEL))
544                  && !VIS_CullSphere(ent->origin, ent->model->radius * ent->scale)
545                  && !VIS_CullBox(ent->mins, ent->maxs))
546                 {
547                         ent->visframe = r_framecount;
548                         R_FarClip_Box(ent->mins, ent->maxs);
549                 }
550         }
551 }
552
553 // only used if skyrendermasked, and normally returns false
554 int R_DrawBrushModelsSky (void)
555 {
556         int i, sky;
557         entity_render_t *ent;
558
559         if (!r_drawentities.integer)
560                 return false;
561
562         sky = false;
563         for (i = 0;i < r_refdef.numentities;i++)
564         {
565                 ent = r_refdef.entities[i];
566                 if (ent->visframe == r_framecount && ent->model && ent->model->DrawSky)
567                 {
568                         ent->model->DrawSky(ent);
569                         sky = true;
570                 }
571         }
572         return sky;
573 }
574
575 /*
576 =============
577 R_DrawViewModel
578 =============
579 */
580 void R_DrawViewModel (void)
581 {
582         entity_render_t *ent;
583
584         // FIXME: move these checks to client
585         if (!r_drawviewmodel.integer || chase_active.integer || envmap || !r_drawentities.integer || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
586                 return;
587
588         ent = &cl.viewent.render;
589         Mod_CheckLoaded(ent->model);
590         R_LerpAnimation(ent);
591         Matrix4x4_CreateFromQuakeEntity(&ent->matrix, ent->origin[0], ent->origin[1], ent->origin[2], -ent->angles[0], ent->angles[1], ent->angles[2], ent->scale);
592         Matrix4x4_Invert_Simple(&ent->inversematrix, &ent->matrix);
593         R_UpdateEntLights(ent);
594         ent->model->Draw(ent);
595 }
596
597 void R_DrawNoModel(entity_render_t *ent);
598 void R_DrawModels ()
599 {
600         int i;
601         entity_render_t *ent;
602
603         if (!r_drawentities.integer)
604                 return;
605
606         R_DrawViewModel();
607         for (i = 0;i < r_refdef.numentities;i++)
608         {
609                 ent = r_refdef.entities[i];
610                 if (ent->visframe == r_framecount)
611                 {
612                         if (ent->model && ent->model->Draw != NULL)
613                                 ent->model->Draw(ent);
614                         else
615                                 R_DrawNoModel(ent);
616                 }
617         }
618 }
619
620 void R_DrawFakeShadows (void)
621 {
622         int i;
623         entity_render_t *ent;
624
625         ent = &cl_entities[0].render;
626         if (ent->model && ent->model->DrawFakeShadow)
627                 ent->model->DrawFakeShadow(ent);
628
629         if (!r_drawentities.integer)
630                 return;
631         for (i = 0;i < r_refdef.numentities;i++)
632         {
633                 ent = r_refdef.entities[i];
634                 if (ent->model && ent->model->DrawFakeShadow)
635                         ent->model->DrawFakeShadow(ent);
636         }
637 }
638
639 #include "r_shadow.h"
640
641 int shadowframecount = 0;
642
643 int Light_CullBox(const vec3_t mins, const vec3_t maxs)
644 {
645         int stackpos, sides;
646         mnode_t *node, *stack[4096];
647         stackpos = 0;
648         stack[stackpos++] = cl.worldmodel->nodes;
649         while (stackpos)
650         {
651                 node = stack[--stackpos];
652                 if (node->contents < 0)
653                 {
654                         if (((mleaf_t *)node)->worldnodeframe == shadowframecount)
655                                 return false;
656                 }
657                 else
658                 {
659                         sides = BoxOnPlaneSide(mins, maxs, node->plane);
660                         if (sides & 2 && stackpos < 4096)
661                                 stack[stackpos++] = node->children[1];
662                         if (sides & 1 && stackpos < 4096)
663                                 stack[stackpos++] = node->children[0];
664                 }
665         }
666         return true;
667 }
668
669 int LightAndVis_CullBox(const vec3_t mins, const vec3_t maxs)
670 {
671         int stackpos, sides;
672         mnode_t *node, *stack[4096];
673         if (R_CullBox(mins, maxs))
674                 return true;
675         stackpos = 0;
676         stack[stackpos++] = cl.worldmodel->nodes;
677         while (stackpos)
678         {
679                 node = stack[--stackpos];
680                 if (node->contents < 0)
681                 {
682                         if (((mleaf_t *)node)->visframe == r_framecount && ((mleaf_t *)node)->worldnodeframe == shadowframecount)
683                                 return false;
684                 }
685                 else
686                 {
687                         sides = BoxOnPlaneSide(mins, maxs, node->plane);
688                         if (sides & 2 && stackpos < 4096)
689                                 stack[stackpos++] = node->children[1];
690                         if (sides & 1 && stackpos < 4096)
691                                 stack[stackpos++] = node->children[0];
692                 }
693         }
694         return true;
695 }
696
697
698 void R_TestAndDrawShadowVolume(entity_render_t *ent, vec3_t lightorigin, float cullradius, float lightradius, vec3_t clipmins, vec3_t clipmaxs)
699 {
700         int i;
701         vec3_t p, p2, temp, relativelightorigin, mins, maxs;
702         float dist, projectdistance;
703         // rough checks
704         if (ent->model && ent->model->DrawShadowVolume)
705         {
706                 temp[0] = bound(ent->mins[0], lightorigin[0], ent->maxs[0]) - lightorigin[0];
707                 temp[1] = bound(ent->mins[1], lightorigin[1], ent->maxs[1]) - lightorigin[1];
708                 temp[2] = bound(ent->mins[2], lightorigin[2], ent->maxs[2]) - lightorigin[2];
709                 dist = DotProduct(temp, temp);
710                 if (dist < cullradius * cullradius)
711                 {
712                         if (!Light_CullBox(ent->mins, ent->maxs))
713                         {
714                                 projectdistance = cullradius - sqrt(dist);
715                                 // calculate projected bounding box and decide if it is on-screen
716                                 VectorCopy(ent->mins, mins);
717                                 VectorCopy(ent->maxs, maxs);
718                                 for (i = 0;i < 8;i++)
719                                 {
720                                         p[0] = i & 1 ? ent->maxs[0] : ent->mins[0];
721                                         p[1] = i & 2 ? ent->maxs[1] : ent->mins[1];
722                                         p[2] = i & 4 ? ent->maxs[2] : ent->mins[2];
723                                         VectorSubtract(p, lightorigin, temp);
724                                         dist = projectdistance / sqrt(DotProduct(temp, temp));
725                                         VectorMA(p, dist, temp, p2);
726                                         if (mins[0] > p2[0]) mins[0] = p2[0];if (maxs[0] < p2[0]) maxs[0] = p2[0];
727                                         if (mins[1] > p2[1]) mins[1] = p2[1];if (maxs[1] < p2[1]) maxs[1] = p2[1];
728                                         if (mins[2] > p2[2]) mins[2] = p2[2];if (maxs[2] < p2[2]) maxs[2] = p2[2];
729                                 }
730                                 if (mins[0] < clipmaxs[0] && maxs[0] > clipmins[0]
731                                  && mins[1] < clipmaxs[1] && maxs[1] > clipmins[1]
732                                  && mins[2] < clipmaxs[2] && maxs[2] > clipmins[2]
733                                  && !LightAndVis_CullBox(mins, maxs))
734                                 {
735                                         Matrix4x4_Transform(&ent->inversematrix, lightorigin, relativelightorigin);
736                                         ent->model->DrawShadowVolume (ent, relativelightorigin, lightradius);
737                                 }
738                         }
739                 }
740         }
741 }
742
743 void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, worldlight_t *light);
744
745 #define SHADOWSPHERE_SEGMENTS 16
746
747 shadowmesh_t *shadowsphere;
748 void R_CreateShadowSphere(void)
749 {
750         int i, j;
751         vec3_t angles, angles2, angles3, angles4;
752         float verts[12];
753         shadowsphere = Mod_ShadowMesh_Begin(zonemempool, SHADOWSPHERE_SEGMENTS * SHADOWSPHERE_SEGMENTS / 2);
754         for (i = 0;i < SHADOWSPHERE_SEGMENTS / 2;i++)
755         {
756                 for (j = 0;j < SHADOWSPHERE_SEGMENTS;j++)
757                 {
758                         angles[0] = (i * 360.0f / SHADOWSPHERE_SEGMENTS) + 90.0f;
759                         angles[1] = j * 360.0f / SHADOWSPHERE_SEGMENTS;
760                         angles[2] = 0;
761                         VectorCopy(angles, angles2);
762                         VectorCopy(angles, angles3);
763                         VectorCopy(angles, angles4);
764                         angles2[1] += 360.0f / SHADOWSPHERE_SEGMENTS;
765                         angles3[0] += 360.0f / SHADOWSPHERE_SEGMENTS;
766                         angles3[1] += 360.0f / SHADOWSPHERE_SEGMENTS;
767                         angles4[0] += 360.0f / SHADOWSPHERE_SEGMENTS;
768                         AngleVectorsFLU(angles, verts, NULL, NULL);
769                         AngleVectorsFLU(angles2, verts + 9, NULL, NULL);
770                         AngleVectorsFLU(angles3, verts + 6, NULL, NULL);
771                         AngleVectorsFLU(angles4, verts + 3, NULL, NULL);
772                         VectorScale(&verts[0], 1.0f, &verts[0]);
773                         VectorScale(&verts[3], 1.0f, &verts[3]);
774                         VectorScale(&verts[6], 1.0f, &verts[6]);
775                         VectorScale(&verts[9], 1.0f, &verts[9]);
776                         Mod_ShadowMesh_AddPolygon(zonemempool, shadowsphere, 4, verts);
777                 }
778         }
779         shadowsphere = Mod_ShadowMesh_Finish(zonemempool, shadowsphere);
780 }
781
782
783 void R_DrawShadowSphere(vec3_t origin, float cullradius, float lightradius)
784 {
785         shadowmesh_t *mesh;
786         matrix4x4_t matrix;
787         if (!shadowsphere)
788                 R_CreateShadowSphere();
789         Matrix4x4_CreateScale(&matrix, lightradius);
790         Matrix4x4_ConcatTranslate(&matrix, origin[0], origin[1], origin[2]);
791         R_Mesh_Matrix(&matrix);
792         for (mesh = shadowsphere;mesh;mesh = mesh->next)
793         {
794                 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
795                 R_Shadow_RenderVolume(mesh->numverts, mesh->numtriangles, mesh->elements);
796         }
797         Matrix4x4_CreateScale(&matrix, -cullradius);
798         Matrix4x4_ConcatTranslate(&matrix, origin[0], origin[1], origin[2]);
799         R_Mesh_Matrix(&matrix);
800         for (mesh = shadowsphere;mesh;mesh = mesh->next)
801         {
802                 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
803                 R_Shadow_RenderVolume(mesh->numverts, mesh->numtriangles, mesh->elements);
804         }
805 }
806
807 extern void R_Model_Brush_DrawLightForSurfaceList(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, msurface_t **surflist, int numsurfaces);
808 void R_ShadowVolumeLighting (int visiblevolumes)
809 {
810         int i;
811         entity_render_t *ent;
812         int lnum;
813         float f, lightradius, cullradius;
814         vec3_t relativelightorigin, relativeeyeorigin, lightcolor, clipmins, clipmaxs;
815         worldlight_t *wl;
816         //mlight_t *sl;
817         rdlight_t *rd;
818         rmeshstate_t m;
819         mleaf_t *leaf;
820
821         if (visiblevolumes)
822         {
823                 memset(&m, 0, sizeof(m));
824                 m.blendfunc1 = GL_ONE;
825                 m.blendfunc2 = GL_ONE;
826                 R_Mesh_State(&m);
827                 GL_Color(0.0 * r_colorscale, 0.0125 * r_colorscale, 0.1 * r_colorscale, 1);
828         }
829         else
830                 R_Shadow_Stage_Begin();
831         shadowframecount++;
832         for (lnum = 0, wl = r_shadow_worldlightchain;wl;wl = wl->next, lnum++)
833         {
834                 if (d_lightstylevalue[wl->style] <= 0)
835                         continue;
836                 cullradius = wl->cullradius;
837                 lightradius = wl->lightradius;
838                 if (R_CullSphere(wl->origin, lightradius))
839                         continue;
840                 //if (R_CullBox(wl->mins, wl->maxs) || R_CullSphere(wl->origin, lightradius))
841                 //      continue;
842                 //if (VIS_CullBox(wl->mins, wl->maxs) || VIS_CullSphere(wl->origin, lightradius))
843                 //      continue;
844                 if (r_shadow_debuglight.integer >= 0 && lnum != r_shadow_debuglight.integer)
845                         continue;
846
847                 for (i = 0;i < wl->numleafs;i++)
848                         if (wl->leafs[i]->visframe == r_framecount)
849                                 break;
850                 if (i == wl->numleafs)
851                         continue;
852                 leaf = wl->leafs[i];
853                 VectorCopy(leaf->mins, clipmins);
854                 VectorCopy(leaf->maxs, clipmaxs);
855                 for (i = 0;i < wl->numleafs;i++)
856                 {
857                         leaf = wl->leafs[i];
858                         if (leaf->visframe == r_framecount)
859                         {
860                                 if (clipmins[0] > leaf->mins[0]) clipmins[0] = leaf->mins[0];
861                                 if (clipmaxs[0] < leaf->maxs[0]) clipmaxs[0] = leaf->maxs[0];
862                                 if (clipmins[1] > leaf->mins[1]) clipmins[1] = leaf->mins[1];
863                                 if (clipmaxs[1] < leaf->maxs[1]) clipmaxs[1] = leaf->maxs[1];
864                                 if (clipmins[2] > leaf->mins[2]) clipmins[2] = leaf->mins[2];
865                                 if (clipmaxs[2] < leaf->maxs[2]) clipmaxs[2] = leaf->maxs[2];
866                         }
867                 }
868                 if (clipmins[0] < wl->mins[0]) clipmins[0] = wl->mins[0];
869                 if (clipmins[1] < wl->mins[1]) clipmins[1] = wl->mins[1];
870                 if (clipmins[2] < wl->mins[2]) clipmins[2] = wl->mins[2];
871                 if (clipmaxs[0] > wl->maxs[0]) clipmaxs[0] = wl->maxs[0];
872                 if (clipmaxs[1] > wl->maxs[1]) clipmaxs[1] = wl->maxs[1];
873                 if (clipmaxs[2] > wl->maxs[2]) clipmaxs[2] = wl->maxs[2];
874
875                 if (R_Shadow_ScissorForBBoxAndSphere(clipmins, clipmaxs, wl->origin, wl->cullradius))
876                         continue;
877
878                 // mark the leafs we care about so only things in those leafs will matter
879                 for (i = 0;i < wl->numleafs;i++)
880                         wl->leafs[i]->worldnodeframe = shadowframecount;
881
882
883                 f = d_lightstylevalue[wl->style] * (1.0f / 256.0f);
884                 VectorScale(wl->light, f, lightcolor);
885                 if (wl->selected)
886                 {
887                         f = 2 + sin(realtime * M_PI * 4.0);
888                         VectorScale(lightcolor, f, lightcolor);
889                 }
890
891                 if (!visiblevolumes)
892                         R_Shadow_Stage_ShadowVolumes();
893                 if (wl->shadowvolume && r_staticworldlights.integer)
894                         R_Shadow_DrawWorldLightShadowVolume(&cl_entities[0].render.matrix, wl);
895                 else
896                         R_TestAndDrawShadowVolume(&cl_entities[0].render, wl->origin, cullradius, lightradius, clipmins, clipmaxs);
897                 if (r_drawentities.integer)
898                 {
899                         for (i = 0;i < r_refdef.numentities;i++)
900                         {
901                                 ent = r_refdef.entities[i];
902                                 if (ent->maxs[0] >= wl->mins[0] && ent->mins[0] <= wl->maxs[0]
903                                  && ent->maxs[1] >= wl->mins[1] && ent->mins[1] <= wl->maxs[1]
904                                  && ent->maxs[2] >= wl->mins[2] && ent->mins[2] <= wl->maxs[2]
905                                  && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
906                                         R_TestAndDrawShadowVolume(r_refdef.entities[i], wl->origin, cullradius, lightradius, clipmins, clipmaxs);
907                         }
908                 }
909
910                 if (!visiblevolumes)
911                 {
912                         R_Shadow_Stage_Light();
913                         ent = &cl_entities[0].render;
914                         if (ent->model && ent->model->DrawLight)
915                         {
916                                 Matrix4x4_Transform(&ent->inversematrix, wl->origin, relativelightorigin);
917                                 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
918                                 if (wl->numsurfaces)
919                                         R_Model_Brush_DrawLightForSurfaceList(ent, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, wl->surfaces, wl->numsurfaces);
920                                 else
921                                         ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, lightcolor);
922                         }
923                         if (r_drawentities.integer)
924                         {
925                                 for (i = 0;i < r_refdef.numentities;i++)
926                                 {
927                                         ent = r_refdef.entities[i];
928                                         if (ent->visframe == r_framecount && ent->model && ent->model->DrawLight
929                                          && ent->maxs[0] >= wl->mins[0] && ent->mins[0] <= wl->maxs[0]
930                                          && ent->maxs[1] >= wl->mins[1] && ent->mins[1] <= wl->maxs[1]
931                                          && ent->maxs[2] >= wl->mins[2] && ent->mins[2] <= wl->maxs[2]
932                                          && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
933                                         {
934                                                 Matrix4x4_Transform(&ent->inversematrix, wl->origin, relativelightorigin);
935                                                 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
936                                                 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, lightcolor);
937                                         }
938                                 }
939                         }
940
941                         if (R_Shadow_Stage_EraseShadowVolumes())
942                         {
943                                 if (wl->shadowvolume && r_staticworldlights.integer)
944                                         R_Shadow_DrawWorldLightShadowVolume(&cl_entities[0].render.matrix, wl);
945                                 else
946                                         R_TestAndDrawShadowVolume(&cl_entities[0].render, wl->origin, cullradius, lightradius, clipmins, clipmaxs);
947                                 if (r_drawentities.integer)
948                                 {
949                                         for (i = 0;i < r_refdef.numentities;i++)
950                                         {
951                                                 ent = r_refdef.entities[i];
952                                                 if (ent->maxs[0] >= wl->mins[0] && ent->mins[0] <= wl->maxs[0]
953                                                  && ent->maxs[1] >= wl->mins[1] && ent->mins[1] <= wl->maxs[1]
954                                                  && ent->maxs[2] >= wl->mins[2] && ent->mins[2] <= wl->maxs[2]
955                                                  && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
956                                                         R_TestAndDrawShadowVolume(r_refdef.entities[i], wl->origin, cullradius, lightradius, clipmins, clipmaxs);
957                                         }
958                                 }
959                         }
960                 }
961         }
962         /*
963         for (lnum = 0, sl = cl.worldmodel->lights;lnum < cl.worldmodel->numlights;lnum++, sl++)
964         {
965                 if (d_lightstylevalue[sl->style] <= 0)
966                         continue;
967                 if (r_shadow_debuglight.integer >= 0 && lnum != r_shadow_debuglight.integer)
968                         continue;
969                 cullradius = sl->cullradius;
970                 lightradius = sl->lightradius;
971                 if (VIS_CullBox(sl->mins, sl->maxs) || VIS_CullSphere(sl->origin, lightradius))
972                         continue;
973
974                 f = d_lightstylevalue[sl->style] * (1.0f / 32768.0f);
975                 VectorScale(sl->light, f, lightcolor);
976
977                 if (!visiblevolumes)
978                         R_Shadow_Stage_ShadowVolumes();
979                 if (sl->shadowvolume && r_staticworldlights.integer)
980                         R_DrawWorldLightShadowVolume(&cl_entities[0].render.matrix, sl->shadowvolume);
981                 else
982                         R_TestAndDrawShadowVolume(&cl_entities[0].render, sl->origin, cullradius, lightradius);
983                 if (r_drawentities.integer)
984                 {
985                         for (i = 0;i < r_refdef.numentities;i++)
986                         {
987                                 ent = r_refdef.entities[i];
988                                 if (ent->maxs[0] >= sl->mins[0] && ent->mins[0] <= sl->maxs[0]
989                                  && ent->maxs[1] >= sl->mins[1] && ent->mins[1] <= sl->maxs[1]
990                                  && ent->maxs[2] >= sl->mins[2] && ent->mins[2] <= sl->maxs[2]
991                                  && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
992                                         R_TestAndDrawShadowVolume(r_refdef.entities[i], sl->origin, cullradius, lightradius);
993                         }
994                 }
995
996                 if (!visiblevolumes)
997                 {
998                         R_Shadow_Stage_Light();
999                         ent = &cl_entities[0].render;
1000                         if (ent->model && ent->model->DrawLight)
1001                         {
1002                                 Matrix4x4_Transform(&ent->inversematrix, sl->origin, relativelightorigin);
1003                                 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
1004                                 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, sl->distbias, sl->subtract, lightcolor);
1005                         }
1006                         if (r_drawentities.integer)
1007                         {
1008                                 for (i = 0;i < r_refdef.numentities;i++)
1009                                 {
1010                                         ent = r_refdef.entities[i];
1011                                         if (ent->visframe == r_framecount && ent->model && ent->model->DrawLight
1012                                          && ent->maxs[0] >= sl->mins[0] && ent->mins[0] <= sl->maxs[0]
1013                                          && ent->maxs[1] >= sl->mins[1] && ent->mins[1] <= sl->maxs[1]
1014                                          && ent->maxs[2] >= sl->mins[2] && ent->mins[2] <= sl->maxs[2]
1015                                          && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
1016                                         {
1017                                                 Matrix4x4_Transform(&ent->inversematrix, sl->origin, relativelightorigin);
1018                                                 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
1019                                                 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, sl->distbias, sl->subtract, lightcolor);
1020                                         }
1021                                 }
1022                         }
1023
1024                         R_Shadow_Stage_EraseShadowVolumes();
1025                         if (sl->shadowvolume && r_staticworldlights.integer)
1026                                 R_DrawWorldLightShadowVolume(&cl_entities[0].render.matrix, sl->shadowvolume);
1027                         else
1028                                 R_TestAndDrawShadowVolume(&cl_entities[0].render, sl->origin, cullradius, lightradius);
1029                         if (r_drawentities.integer)
1030                         {
1031                                 for (i = 0;i < r_refdef.numentities;i++)
1032                                 {
1033                                         ent = r_refdef.entities[i];
1034                                         if (ent->maxs[0] >= sl->mins[0] && ent->mins[0] <= sl->maxs[0]
1035                                         && ent->maxs[1] >= sl->mins[1] && ent->mins[1] <= sl->maxs[1]
1036                                         && ent->maxs[2] >= sl->mins[2] && ent->mins[2] <= sl->maxs[2]
1037                                         && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
1038                                                 R_TestAndDrawShadowVolume(r_refdef.entities[i], sl->origin, cullradius, lightradius);
1039                                 }
1040                         }
1041                 }
1042         }
1043         */
1044         for (lnum = 0, rd = r_dlight;lnum < r_numdlights;lnum++, rd++)
1045         {
1046                 cullradius = rd->cullradius;
1047                 lightradius = rd->cullradius;
1048                 if (VIS_CullSphere(rd->origin, lightradius))
1049                         continue;
1050
1051                 VectorScale(rd->light, (1.0f / 8192.0f), lightcolor);
1052                 clipmins[0] = rd->origin[0] - cullradius;
1053                 clipmins[1] = rd->origin[1] - cullradius;
1054                 clipmins[2] = rd->origin[2] - cullradius;
1055                 clipmaxs[0] = rd->origin[0] + cullradius;
1056                 clipmaxs[1] = rd->origin[1] + cullradius;
1057                 clipmaxs[2] = rd->origin[2] + cullradius;
1058
1059                 if (R_Shadow_ScissorForBBoxAndSphere(clipmins, clipmaxs, rd->origin, rd->cullradius))
1060                         continue;
1061
1062                 if (!visiblevolumes)
1063                         R_Shadow_Stage_ShadowVolumes();
1064                 R_TestAndDrawShadowVolume(&cl_entities[0].render, rd->origin, cullradius, lightradius, clipmins, clipmaxs);
1065                 if (r_drawentities.integer)
1066                 {
1067                         for (i = 0;i < r_refdef.numentities;i++)
1068                         {
1069                                 ent = r_refdef.entities[i];
1070                                 if (ent != rd->ent && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
1071                                         R_TestAndDrawShadowVolume(ent, rd->origin, cullradius, lightradius, clipmins, clipmaxs);
1072                         }
1073                 }
1074
1075                 if (!visiblevolumes)
1076                 {
1077                         R_Shadow_Stage_Light();
1078                         ent = &cl_entities[0].render;
1079                         if (ent->model && ent->model->DrawLight)
1080                         {
1081                                 Matrix4x4_Transform(&ent->inversematrix, rd->origin, relativelightorigin);
1082                                 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
1083                                 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, lightcolor);
1084                         }
1085                         if (r_drawentities.integer)
1086                         {
1087                                 for (i = 0;i < r_refdef.numentities;i++)
1088                                 {
1089                                         ent = r_refdef.entities[i];
1090                                         if (ent->visframe == r_framecount && ent->model && ent->model->DrawLight
1091                                          && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
1092                                         {
1093                                                 Matrix4x4_Transform(&ent->inversematrix, rd->origin, relativelightorigin);
1094                                                 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
1095                                                 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, lightcolor);
1096                                         }
1097                                 }
1098                         }
1099
1100                         if (R_Shadow_Stage_EraseShadowVolumes())
1101                         {
1102                                 R_TestAndDrawShadowVolume(&cl_entities[0].render, rd->origin, cullradius, lightradius, clipmins, clipmaxs);
1103                                 if (r_drawentities.integer)
1104                                 {
1105                                         for (i = 0;i < r_refdef.numentities;i++)
1106                                         {
1107                                                 ent = r_refdef.entities[i];
1108                                                 if (ent != rd->ent && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
1109                                                         R_TestAndDrawShadowVolume(ent, rd->origin, cullradius, lightradius, clipmins, clipmaxs);
1110                                         }
1111                                 }
1112                         }
1113                 }
1114         }
1115
1116         if (!visiblevolumes)
1117                 R_Shadow_Stage_End();
1118         qglDisable(GL_SCISSOR_TEST);
1119 }
1120
1121 static void R_SetFrustum (void)
1122 {
1123         int i;
1124
1125         // LordHavoc: note to all quake engine coders, the special case for 90
1126         // degrees assumed a square view (wrong), so I removed it, Quake2 has it
1127         // disabled as well.
1128         // rotate VPN right by FOV_X/2 degrees
1129         RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
1130         // rotate VPN left by FOV_X/2 degrees
1131         RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
1132         // rotate VPN up by FOV_X/2 degrees
1133         RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
1134         // rotate VPN down by FOV_X/2 degrees
1135         RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
1136
1137         for (i = 0;i < 4;i++)
1138         {
1139                 frustum[i].type = PLANE_ANYZ;
1140                 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
1141                 PlaneClassify(&frustum[i]);
1142         }
1143 }
1144
1145 /*
1146 ===============
1147 R_SetupFrame
1148 ===============
1149 */
1150 static void R_SetupFrame (void)
1151 {
1152 // don't allow cheats in multiplayer
1153         if (cl.maxclients > 1)
1154         {
1155                 if (r_fullbright.integer != 0)
1156                         Cvar_Set ("r_fullbright", "0");
1157                 if (r_ambient.value != 0)
1158                         Cvar_Set ("r_ambient", "0");
1159         }
1160
1161         r_framecount++;
1162
1163 // build the transformation matrix for the given view angles
1164         VectorCopy (r_refdef.vieworg, r_origin);
1165
1166         AngleVectors (r_refdef.viewangles, vpn, vright, vup);
1167
1168         R_AnimateLight ();
1169 }
1170
1171
1172 static void R_BlendView(void)
1173 {
1174         rmeshstate_t m;
1175         float r;
1176
1177         if (r_refdef.viewblend[3] < 0.01f)
1178                 return;
1179
1180         memset(&m, 0, sizeof(m));
1181         m.blendfunc1 = GL_SRC_ALPHA;
1182         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1183         m.depthdisable = true; // magic
1184         R_Mesh_Matrix(&r_identitymatrix);
1185         R_Mesh_State(&m);
1186
1187         r = 64000;
1188         varray_vertex[0] = r_origin[0] + vpn[0] * 1.5 - vright[0] * r - vup[0] * r;
1189         varray_vertex[1] = r_origin[1] + vpn[1] * 1.5 - vright[1] * r - vup[1] * r;
1190         varray_vertex[2] = r_origin[2] + vpn[2] * 1.5 - vright[2] * r - vup[2] * r;
1191         r *= 3;
1192         varray_vertex[4] = varray_vertex[0] + vup[0] * r;
1193         varray_vertex[5] = varray_vertex[1] + vup[1] * r;
1194         varray_vertex[6] = varray_vertex[2] + vup[2] * r;
1195         varray_vertex[8] = varray_vertex[0] + vright[0] * r;
1196         varray_vertex[9] = varray_vertex[1] + vright[1] * r;
1197         varray_vertex[10] = varray_vertex[2] + vright[2] * r;
1198         GL_Color(r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
1199         R_Mesh_Draw(3, 1, polygonelements);
1200 }
1201
1202 /*
1203 ================
1204 R_RenderView
1205
1206 r_refdef must be set before the first call
1207 ================
1208 */
1209 void R_RenderView (void)
1210 {
1211         entity_render_t *world;
1212         if (!r_refdef.entities/* || !cl.worldmodel*/)
1213                 return; //Host_Error ("R_RenderView: NULL worldmodel");
1214
1215         if (r_shadow_realtime.integer == 1)
1216         {
1217                 if (!gl_texturecubemap)
1218                 {
1219                         Con_Printf("Cubemap texture support not detected, turning off r_shadow_realtime\n");
1220                         Cvar_SetValueQuick(&r_shadow_realtime, 0);
1221                 }
1222                 else if (!gl_dot3arb)
1223                 {
1224                         Con_Printf("Bumpmapping support not detected, turning off r_shadow_realtime\n");
1225                         Cvar_SetValueQuick(&r_shadow_realtime, 0);
1226                 }
1227                 else if (!gl_stencil)
1228                 {
1229                         Con_Printf("Stencil not enabled, turning off r_shadow_realtime, please type vid_stencil 1;vid_restart and try again\n");
1230                         Cvar_SetValueQuick(&r_shadow_realtime, 0);
1231                 }
1232                 else if (!gl_combine.integer)
1233                 {
1234                         Con_Printf("Combine disabled, please turn on gl_combine, turning off r_shadow_realtime\n");
1235                         Cvar_SetValueQuick(&r_shadow_realtime, 0);
1236                 }
1237         }
1238
1239         R_Shadow_UpdateLightingMode();
1240
1241         world = &cl_entities[0].render;
1242
1243         // FIXME: move to client
1244         R_MoveExplosions();
1245         R_TimeReport("mexplosion");
1246
1247         R_Textures_Frame();
1248         R_SetupFrame();
1249         R_SetFrustum();
1250         R_SetupFog();
1251         R_SkyStartFrame();
1252         R_BuildLightList();
1253         R_TimeReport("setup");
1254
1255         R_WorldVisibility(world);
1256         R_TimeReport("worldvis");
1257
1258         R_FarClip_Start(r_origin, vpn, 768.0f);
1259         R_MarkEntities();
1260         r_farclip = R_FarClip_Finish() + 256.0f;
1261         R_TimeReport("markentity");
1262
1263         GL_SetupView_ViewPort(r_refdef.x, r_refdef.y, r_refdef.width, r_refdef.height);
1264         if (r_shadow_lightingmode > 0)
1265                 GL_SetupView_Mode_PerspectiveInfiniteFarClip(r_refdef.fov_x, r_refdef.fov_y, 1.0f);
1266         else
1267                 GL_SetupView_Mode_Perspective(r_refdef.fov_x, r_refdef.fov_y, 1.0f, r_farclip);
1268         GL_SetupView_Orientation_FromEntity (r_refdef.vieworg, r_refdef.viewangles);
1269         qglDepthFunc(GL_LEQUAL);
1270
1271         R_Mesh_Start();
1272         R_MeshQueue_BeginScene();
1273
1274         if (r_shadow_lightingmode)
1275                 R_Shadow_UpdateWorldLightSelection();
1276
1277         if (R_DrawBrushModelsSky())
1278                 R_TimeReport("bmodelsky");
1279
1280         // must occur early because it can draw sky
1281         R_DrawWorld(world);
1282         R_TimeReport("world");
1283
1284         // don't let sound skip if going slow
1285         if (!intimerefresh && !r_speeds.integer)
1286                 S_ExtraUpdate ();
1287
1288         R_DrawModels(r_shadow_lightingmode > 0);
1289         R_TimeReport("models");
1290
1291         if (r_shadows.integer == 1)
1292         {
1293                 R_DrawFakeShadows();
1294                 R_TimeReport("fakeshadow");
1295         }
1296
1297         if (r_shadow_lightingmode > 0)
1298         {
1299                 R_ShadowVolumeLighting(false);
1300                 R_TimeReport("dynlight");
1301         }
1302
1303         R_DrawParticles();
1304         R_TimeReport("particles");
1305
1306         R_DrawExplosions();
1307         R_TimeReport("explosions");
1308
1309         R_MeshQueue_RenderTransparent();
1310         R_TimeReport("drawtrans");
1311
1312         R_DrawCoronas();
1313         R_TimeReport("coronas");
1314
1315         R_DrawWorldCrosshair();
1316         R_TimeReport("crosshair");
1317
1318         R_BlendView();
1319         R_TimeReport("blendview");
1320
1321         R_MeshQueue_Render();
1322         R_MeshQueue_EndScene();
1323         if (r_shadow_realtime.integer == 2)
1324         {
1325                 R_ShadowVolumeLighting(true);
1326                 R_TimeReport("shadowvolume");
1327         }
1328         R_Mesh_Finish();
1329         R_TimeReport("meshfinish");
1330 }
1331
1332 /*
1333 void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
1334 {
1335         int i;
1336         float *v, *c, f1, f2, diff[3];
1337         rmeshstate_t m;
1338         m.blendfunc1 = GL_SRC_ALPHA;
1339         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1340         R_Mesh_Matrix(&r_identitymatrix);
1341         R_Mesh_State(&m);
1342
1343         varray_vertex[ 0] = mins[0];varray_vertex[ 1] = mins[1];varray_vertex[ 2] = mins[2];
1344         varray_vertex[ 4] = maxs[0];varray_vertex[ 5] = mins[1];varray_vertex[ 6] = mins[2];
1345         varray_vertex[ 8] = mins[0];varray_vertex[ 9] = maxs[1];varray_vertex[10] = mins[2];
1346         varray_vertex[12] = maxs[0];varray_vertex[13] = maxs[1];varray_vertex[14] = mins[2];
1347         varray_vertex[16] = mins[0];varray_vertex[17] = mins[1];varray_vertex[18] = maxs[2];
1348         varray_vertex[20] = maxs[0];varray_vertex[21] = mins[1];varray_vertex[22] = maxs[2];
1349         varray_vertex[24] = mins[0];varray_vertex[25] = maxs[1];varray_vertex[26] = maxs[2];
1350         varray_vertex[28] = maxs[0];varray_vertex[29] = maxs[1];varray_vertex[30] = maxs[2];
1351         R_FillColors(varray_color, 8, cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
1352         if (fogenabled)
1353         {
1354                 for (i = 0, v = varray_vertex, c = varray_color;i < 8;i++, v += 4, c += 4)
1355                 {
1356                         VectorSubtract(v, r_origin, diff);
1357                         f2 = exp(fogdensity/DotProduct(diff, diff));
1358                         f1 = 1 - f2;
1359                         f2 *= r_colorscale;
1360                         c[0] = c[0] * f1 + fogcolor[0] * f2;
1361                         c[1] = c[1] * f1 + fogcolor[1] * f2;
1362                         c[2] = c[2] * f1 + fogcolor[2] * f2;
1363                 }
1364         }
1365         GL_UseColorArray();
1366         R_Mesh_Draw(8, 12);
1367 }
1368 */
1369
1370 void R_DrawNoModelCallback(const void *calldata1, int calldata2)
1371 {
1372         const entity_render_t *ent = calldata1;
1373         int i, element[24];
1374         float f1, f2, *c, diff[3];
1375         rmeshstate_t m;
1376         memset(&m, 0, sizeof(m));
1377         if (ent->flags & EF_ADDITIVE)
1378         {
1379                 m.blendfunc1 = GL_SRC_ALPHA;
1380                 m.blendfunc2 = GL_ONE;
1381         }
1382         else if (ent->alpha < 1)
1383         {
1384                 m.blendfunc1 = GL_SRC_ALPHA;
1385                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1386         }
1387         else
1388         {
1389                 m.blendfunc1 = GL_ONE;
1390                 m.blendfunc2 = GL_ZERO;
1391         }
1392         R_Mesh_Matrix(&ent->matrix);
1393         R_Mesh_State(&m);
1394
1395         element[ 0] = 5;element[ 1] = 2;element[ 2] = 0;
1396         element[ 3] = 5;element[ 4] = 1;element[ 5] = 2;
1397         element[ 6] = 5;element[ 7] = 0;element[ 8] = 3;
1398         element[ 9] = 5;element[10] = 3;element[11] = 1;
1399         element[12] = 0;element[13] = 2;element[14] = 4;
1400         element[15] = 2;element[16] = 1;element[17] = 4;
1401         element[18] = 3;element[19] = 0;element[20] = 4;
1402         element[21] = 1;element[22] = 3;element[23] = 4;
1403         varray_vertex[ 0] = -16;varray_vertex[ 1] =   0;varray_vertex[ 2] =   0;
1404         varray_vertex[ 4] =  16;varray_vertex[ 5] =   0;varray_vertex[ 6] =   0;
1405         varray_vertex[ 8] =   0;varray_vertex[ 9] = -16;varray_vertex[10] =   0;
1406         varray_vertex[12] =   0;varray_vertex[13] =  16;varray_vertex[14] =   0;
1407         varray_vertex[16] =   0;varray_vertex[17] =   0;varray_vertex[18] = -16;
1408         varray_vertex[20] =   0;varray_vertex[21] =   0;varray_vertex[22] =  16;
1409         varray_color[ 0] = 0.00f * r_colorscale;varray_color[ 1] = 0.00f * r_colorscale;varray_color[ 2] = 0.50f * r_colorscale;varray_color[ 3] = ent->alpha;
1410         varray_color[ 4] = 0.00f * r_colorscale;varray_color[ 5] = 0.00f * r_colorscale;varray_color[ 6] = 0.50f * r_colorscale;varray_color[ 7] = ent->alpha;
1411         varray_color[ 8] = 0.00f * r_colorscale;varray_color[ 9] = 0.50f * r_colorscale;varray_color[10] = 0.00f * r_colorscale;varray_color[11] = ent->alpha;
1412         varray_color[12] = 0.00f * r_colorscale;varray_color[13] = 0.50f * r_colorscale;varray_color[14] = 0.00f * r_colorscale;varray_color[15] = ent->alpha;
1413         varray_color[16] = 0.50f * r_colorscale;varray_color[17] = 0.00f * r_colorscale;varray_color[18] = 0.00f * r_colorscale;varray_color[19] = ent->alpha;
1414         varray_color[20] = 0.50f * r_colorscale;varray_color[21] = 0.00f * r_colorscale;varray_color[22] = 0.00f * r_colorscale;varray_color[23] = ent->alpha;
1415         if (fogenabled)
1416         {
1417                 VectorSubtract(ent->origin, r_origin, diff);
1418                 f2 = exp(fogdensity/DotProduct(diff, diff));
1419                 f1 = 1 - f2;
1420                 for (i = 0, c = varray_color;i < 6;i++, c += 4)
1421                 {
1422                         c[0] = (c[0] * f1 + fogcolor[0] * f2) * r_colorscale;
1423                         c[1] = (c[1] * f1 + fogcolor[1] * f2) * r_colorscale;
1424                         c[2] = (c[2] * f1 + fogcolor[2] * f2) * r_colorscale;
1425                 }
1426         }
1427         else
1428         {
1429                 for (i = 0, c = varray_color;i < 6;i++, c += 4)
1430                 {
1431                         c[0] *= r_colorscale;
1432                         c[1] *= r_colorscale;
1433                         c[2] *= r_colorscale;
1434                 }
1435         }
1436         GL_UseColorArray();
1437         R_Mesh_Draw(6, 8, element);
1438 }
1439
1440 void R_DrawNoModel(entity_render_t *ent)
1441 {
1442         //if ((ent->effects & EF_ADDITIVE) || (ent->alpha < 1))
1443                 R_MeshQueue_AddTransparent(ent->origin, R_DrawNoModelCallback, ent, 0);
1444         //else
1445         //      R_DrawNoModelCallback(ent, 0);
1446 }
1447
1448 void R_CalcBeamVerts (float *vert, vec3_t org1, vec3_t org2, float width)
1449 {
1450         vec3_t right1, right2, diff, normal;
1451
1452         VectorSubtract (org2, org1, normal);
1453         VectorNormalizeFast (normal);
1454
1455         // calculate 'right' vector for start
1456         VectorSubtract (r_origin, org1, diff);
1457         VectorNormalizeFast (diff);
1458         CrossProduct (normal, diff, right1);
1459
1460         // calculate 'right' vector for end
1461         VectorSubtract (r_origin, org2, diff);
1462         VectorNormalizeFast (diff);
1463         CrossProduct (normal, diff, right2);
1464
1465         vert[ 0] = org1[0] + width * right1[0];
1466         vert[ 1] = org1[1] + width * right1[1];
1467         vert[ 2] = org1[2] + width * right1[2];
1468         vert[ 4] = org1[0] - width * right1[0];
1469         vert[ 5] = org1[1] - width * right1[1];
1470         vert[ 6] = org1[2] - width * right1[2];
1471         vert[ 8] = org2[0] - width * right2[0];
1472         vert[ 9] = org2[1] - width * right2[1];
1473         vert[10] = org2[2] - width * right2[2];
1474         vert[12] = org2[0] + width * right2[0];
1475         vert[13] = org2[1] + width * right2[1];
1476         vert[14] = org2[2] + width * right2[2];
1477 }