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