]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rmain.c
disable step interpolation unless entity is MOVETYPE_STEP
[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", "0"};
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         if (cl.worldmodel == NULL)
387                 return false;
388         stackpos = 0;
389         stack[stackpos++] = cl.worldmodel->nodes;
390         while (stackpos)
391         {
392                 node = stack[--stackpos];
393                 if (node->contents < 0)
394                 {
395                         if (((mleaf_t *)node)->pvsframe == cl.worldmodel->pvsframecount)
396                                 return false;
397                 }
398                 else
399                 {
400                         sides = BoxOnPlaneSide(mins, maxs, node->plane);
401                         if (sides & 2 && stackpos < 4096)
402                                 stack[stackpos++] = node->children[1];
403                         if (sides & 1 && stackpos < 4096)
404                                 stack[stackpos++] = node->children[0];
405                 }
406         }
407         return true;
408 }
409
410 int VIS_CullBox(const vec3_t mins, const vec3_t maxs)
411 {
412         int stackpos, sides;
413         mnode_t *node, *stack[4096];
414         if (R_CullBox(mins, maxs))
415                 return true;
416         if (cl.worldmodel == NULL)
417                 return false;
418         stackpos = 0;
419         stack[stackpos++] = cl.worldmodel->nodes;
420         while (stackpos)
421         {
422                 node = stack[--stackpos];
423                 if (node->contents < 0)
424                 {
425                         if (((mleaf_t *)node)->visframe == r_framecount)
426                                 return false;
427                 }
428                 else
429                 {
430                         sides = BoxOnPlaneSide(mins, maxs, node->plane);
431                         if (sides & 2 && stackpos < 4096)
432                                 stack[stackpos++] = node->children[1];
433                         if (sides & 1 && stackpos < 4096)
434                                 stack[stackpos++] = node->children[0];
435                 }
436         }
437         return true;
438 }
439
440 int R_CullSphere(const vec3_t origin, vec_t radius)
441 {
442         return (DotProduct(frustum[0].normal, origin) + radius < frustum[0].dist
443              || DotProduct(frustum[1].normal, origin) + radius < frustum[1].dist
444              || DotProduct(frustum[2].normal, origin) + radius < frustum[2].dist
445              || DotProduct(frustum[3].normal, origin) + radius < frustum[3].dist);
446 }
447
448 int PVS_CullSphere(const vec3_t origin, vec_t radius)
449 {
450         int stackpos;
451         mnode_t *node, *stack[4096];
452         float dist;
453         if (cl.worldmodel == NULL)
454                 return false;
455         stackpos = 0;
456         stack[stackpos++] = cl.worldmodel->nodes;
457         while (stackpos)
458         {
459                 node = stack[--stackpos];
460                 if (node->contents < 0)
461                 {
462                         if (((mleaf_t *)node)->pvsframe == cl.worldmodel->pvsframecount)
463                                 return false;
464                 }
465                 else
466                 {
467                         dist = PlaneDiff(origin, node->plane);
468                         if (dist <= radius)
469                                 stack[stackpos++] = node->children[1];
470                         if (dist >= -radius)
471                                 stack[stackpos++] = node->children[0];
472                 }
473         }
474         return true;
475 }
476
477 int VIS_CullSphere(const vec3_t origin, vec_t radius)
478 {
479         int stackpos;
480         mnode_t *node, *stack[4096];
481         float dist;
482         if (R_CullSphere(origin, radius))
483                 return true;
484         if (cl.worldmodel == NULL)
485                 return false;
486         stackpos = 0;
487         stack[stackpos++] = cl.worldmodel->nodes;
488         while (stackpos)
489         {
490                 node = stack[--stackpos];
491                 if (node->contents < 0)
492                 {
493                         if (((mleaf_t *)node)->visframe == r_framecount)
494                                 return false;
495                 }
496                 else
497                 {
498                         dist = PlaneDiff(origin, node->plane);
499                         if (dist <= radius)
500                                 stack[stackpos++] = node->children[1];
501                         if (dist >= -radius)
502                                 stack[stackpos++] = node->children[0];
503                 }
504         }
505         return true;
506 }
507
508
509 //==================================================================================
510
511 static void R_MarkEntities (void)
512 {
513         int i;
514         vec3_t v;
515         entity_render_t *ent;
516
517         ent = &cl_entities[0].render;
518         Matrix4x4_CreateIdentity(&ent->matrix);
519         Matrix4x4_CreateIdentity(&ent->inversematrix);
520
521         if (cl.worldmodel)
522                 R_FarClip_Box(cl.worldmodel->normalmins, cl.worldmodel->normalmaxs);
523
524         if (!r_drawentities.integer)
525                 return;
526
527         for (i = 0;i < r_refdef.numentities;i++)
528         {
529                 ent = r_refdef.entities[i];
530                 Mod_CheckLoaded(ent->model);
531
532                 // move view-relative models to where they should be
533                 if (ent->flags & RENDER_VIEWMODEL)
534                 {
535                         // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
536                         ent->flags -= RENDER_VIEWMODEL;
537                         // transform origin
538                         VectorCopy(ent->origin, v);
539                         ent->origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
540                         ent->origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
541                         ent->origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
542                         // adjust angles
543                         VectorAdd(ent->angles, r_refdef.viewangles, ent->angles);
544                 }
545
546                 VectorCopy(ent->angles, v);
547                 if (!ent->model || ent->model->type != mod_brush)
548                         v[0] = -v[0];
549                 Matrix4x4_CreateFromQuakeEntity(&ent->matrix, ent->origin[0], ent->origin[1], ent->origin[2], v[0], v[1], v[2], ent->scale);
550                 Matrix4x4_Invert_Simple(&ent->inversematrix, &ent->matrix);
551                 R_LerpAnimation(ent);
552                 R_UpdateEntLights(ent);
553                 if ((chase_active.integer || !(ent->flags & RENDER_EXTERIORMODEL))
554                  && !VIS_CullSphere(ent->origin, (ent->model != NULL ? ent->model->radius : 16) * ent->scale)
555                  && !VIS_CullBox(ent->mins, ent->maxs))
556                 {
557                         ent->visframe = r_framecount;
558                         R_FarClip_Box(ent->mins, ent->maxs);
559                 }
560         }
561 }
562
563 // only used if skyrendermasked, and normally returns false
564 int R_DrawBrushModelsSky (void)
565 {
566         int i, sky;
567         entity_render_t *ent;
568
569         if (!r_drawentities.integer)
570                 return false;
571
572         sky = false;
573         for (i = 0;i < r_refdef.numentities;i++)
574         {
575                 ent = r_refdef.entities[i];
576                 if (ent->visframe == r_framecount && ent->model && ent->model->DrawSky)
577                 {
578                         ent->model->DrawSky(ent);
579                         sky = true;
580                 }
581         }
582         return sky;
583 }
584
585 /*
586 =============
587 R_DrawViewModel
588 =============
589 */
590 /*
591 void R_DrawViewModel (void)
592 {
593         entity_render_t *ent;
594
595         // FIXME: move these checks to client
596         if (!r_drawviewmodel.integer || chase_active.integer || envmap || !r_drawentities.integer || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
597                 return;
598
599         ent = &cl.viewent.render;
600         Mod_CheckLoaded(ent->model);
601         R_LerpAnimation(ent);
602         Matrix4x4_CreateFromQuakeEntity(&ent->matrix, ent->origin[0], ent->origin[1], ent->origin[2], -ent->angles[0], ent->angles[1], ent->angles[2], ent->scale);
603         Matrix4x4_Invert_Simple(&ent->inversematrix, &ent->matrix);
604         R_UpdateEntLights(ent);
605         ent->model->Draw(ent);
606 }
607 */
608
609 void R_DrawNoModel(entity_render_t *ent);
610 void R_DrawModels ()
611 {
612         int i;
613         entity_render_t *ent;
614
615         if (!r_drawentities.integer)
616                 return;
617
618         for (i = 0;i < r_refdef.numentities;i++)
619         {
620                 ent = r_refdef.entities[i];
621                 if (ent->visframe == r_framecount)
622                 {
623                         if (ent->model && ent->model->Draw != NULL)
624                                 ent->model->Draw(ent);
625                         else
626                                 R_DrawNoModel(ent);
627                 }
628         }
629 }
630
631 void R_DrawFakeShadows (void)
632 {
633         int i;
634         entity_render_t *ent;
635
636         ent = &cl_entities[0].render;
637         if (ent->model && ent->model->DrawFakeShadow)
638                 ent->model->DrawFakeShadow(ent);
639
640         if (!r_drawentities.integer)
641                 return;
642         for (i = 0;i < r_refdef.numentities;i++)
643         {
644                 ent = r_refdef.entities[i];
645                 if ((ent->flags & RENDER_SHADOW) && ent->model && ent->model->DrawFakeShadow)
646                         ent->model->DrawFakeShadow(ent);
647         }
648 }
649
650 #include "r_shadow.h"
651
652 int shadowframecount = 0;
653
654 int Light_CullBox(const vec3_t mins, const vec3_t maxs)
655 {
656         int stackpos, sides;
657         mnode_t *node, *stack[4096];
658         if (cl.worldmodel == NULL)
659                 return false;
660         stackpos = 0;
661         stack[stackpos++] = cl.worldmodel->nodes;
662         while (stackpos)
663         {
664                 node = stack[--stackpos];
665                 if (node->contents < 0)
666                 {
667                         if (((mleaf_t *)node)->worldnodeframe == shadowframecount)
668                                 return false;
669                 }
670                 else
671                 {
672                         sides = BoxOnPlaneSide(mins, maxs, node->plane);
673                         if (sides & 2 && stackpos < 4096)
674                                 stack[stackpos++] = node->children[1];
675                         if (sides & 1 && stackpos < 4096)
676                                 stack[stackpos++] = node->children[0];
677                 }
678         }
679         return true;
680 }
681
682 int LightAndVis_CullBox(const vec3_t mins, const vec3_t maxs)
683 {
684         int stackpos, sides;
685         mnode_t *node, *stack[4096];
686         if (R_CullBox(mins, maxs))
687                 return true;
688         if (cl.worldmodel == NULL)
689                 return false;
690         stackpos = 0;
691         stack[stackpos++] = cl.worldmodel->nodes;
692         while (stackpos)
693         {
694                 node = stack[--stackpos];
695                 if (node->contents < 0)
696                 {
697                         if (((mleaf_t *)node)->visframe == r_framecount && ((mleaf_t *)node)->worldnodeframe == shadowframecount)
698                                 return false;
699                 }
700                 else
701                 {
702                         sides = BoxOnPlaneSide(mins, maxs, node->plane);
703                         if (sides & 2 && stackpos < 4096)
704                                 stack[stackpos++] = node->children[1];
705                         if (sides & 1 && stackpos < 4096)
706                                 stack[stackpos++] = node->children[0];
707                 }
708         }
709         return true;
710 }
711
712 int LightAndVis_CullPointCloud(int numpoints, const float *points)
713 {
714         int i;
715         const float *p;
716         int stackpos, sides;
717         mnode_t *node, *stack[4096];
718         //if (R_CullBox(mins, maxs))
719         //      return true;
720         if (cl.worldmodel == NULL)
721                 return false;
722         stackpos = 0;
723         stack[stackpos++] = cl.worldmodel->nodes;
724         while (stackpos)
725         {
726                 node = stack[--stackpos];
727                 if (node->contents < 0)
728                 {
729                         if (((mleaf_t *)node)->visframe == r_framecount && ((mleaf_t *)node)->worldnodeframe == shadowframecount)
730                                 return false;
731                 }
732                 else
733                 {
734                         sides = 0;
735                         for (i = 0, p = points;i < numpoints && sides != 3;i++, p += 3)
736                         {
737                                 if (DotProduct(p, node->plane->normal) < node->plane->dist)
738                                         sides |= 1;
739                                 else
740                                         sides |= 2;
741                         }
742                         if (sides & 2 && stackpos < 4096)
743                                 stack[stackpos++] = node->children[1];
744                         if (sides & 1 && stackpos < 4096)
745                                 stack[stackpos++] = node->children[0];
746                 }
747         }
748         return true;
749 }
750
751
752 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)
753 {
754         vec3_t relativelightorigin;
755         #if 0
756         int i;
757         vec3_t p, p2, temp, relativelightorigin/*, mins, maxs*/;
758         float dist, projectdistance;
759         float points[16][3];
760         #endif
761         // rough checks
762         if (!(ent->flags & RENDER_SHADOW) || ent->model == NULL || ent->model->DrawShadowVolume == NULL)
763                 return;
764         if (r_shadow_cull.integer)
765         {
766                 if (ent->maxs[0] < lightmins[0] || ent->mins[0] > lightmaxs[0]
767                  || ent->maxs[1] < lightmins[1] || ent->mins[1] > lightmaxs[1]
768                  || ent->maxs[2] < lightmins[2] || ent->mins[2] > lightmaxs[2]
769                  || Light_CullBox(ent->mins, ent->maxs))
770                         return;
771         }
772         #if 0
773         if (r_shadow_cull.integer)
774         {
775                 projectdistance = cullradius;
776                 // calculate projected bounding box and decide if it is on-screen
777                 for (i = 0;i < 8;i++)
778                 {
779                         temp[0] = i & 1 ? ent->model->normalmaxs[0] : ent->model->normalmins[0];
780                         temp[1] = i & 2 ? ent->model->normalmaxs[1] : ent->model->normalmins[1];
781                         temp[2] = i & 4 ? ent->model->normalmaxs[2] : ent->model->normalmins[2];
782                         Matrix4x4_Transform(&ent->matrix, temp, points[i]);
783                         VectorSubtract(points[i], lightorigin, temp);
784                         dist = projectdistance / sqrt(DotProduct(temp, temp));
785                         VectorMA(points[i], dist, temp, points[i+8]);
786                 }
787                 if (LightAndVis_CullPointCloud(16, points[0]))
788                         return;
789                 /*
790                 for (i = 0;i < 8;i++)
791                 {
792                         p2[0] = i & 1 ? ent->model->normalmaxs[0] : ent->model->normalmins[0];
793                         p2[1] = i & 2 ? ent->model->normalmaxs[1] : ent->model->normalmins[1];
794                         p2[2] = i & 4 ? ent->model->normalmaxs[2] : ent->model->normalmins[2];
795                         Matrix4x4_Transform(&ent->matrix, p2, p);
796                         VectorSubtract(p, lightorigin, temp);
797                         dist = projectdistance / sqrt(DotProduct(temp, temp));
798                         VectorMA(p, dist, temp, p2);
799                         if (i)
800                         {
801                                 if (mins[0] > p[0]) mins[0] = p[0];if (maxs[0] < p[0]) maxs[0] = p[0];
802                                 if (mins[1] > p[1]) mins[1] = p[1];if (maxs[1] < p[1]) maxs[1] = p[1];
803                                 if (mins[2] > p[2]) mins[2] = p[2];if (maxs[2] < p[2]) maxs[2] = p[2];
804                         }
805                         else
806                         {
807                                 VectorCopy(p, mins);
808                                 VectorCopy(p, maxs);
809                         }
810                         if (mins[0] > p2[0]) mins[0] = p2[0];if (maxs[0] < p2[0]) maxs[0] = p2[0];
811                         if (mins[1] > p2[1]) mins[1] = p2[1];if (maxs[1] < p2[1]) maxs[1] = p2[1];
812                         if (mins[2] > p2[2]) mins[2] = p2[2];if (maxs[2] < p2[2]) maxs[2] = p2[2];
813                 }
814                 if (mins[0] >= clipmaxs[0] || maxs[0] <= clipmins[0]
815                  || mins[1] >= clipmaxs[1] || maxs[1] <= clipmins[1]
816                  || mins[2] >= clipmaxs[2] || maxs[2] <= clipmins[2]
817                  || LightAndVis_CullBox(mins, maxs))
818                         return;
819                 */
820         }
821         #endif
822         Matrix4x4_Transform(&ent->inversematrix, lightorigin, relativelightorigin);
823         ent->model->DrawShadowVolume (ent, relativelightorigin, lightradius);
824 }
825
826 void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, worldlight_t *light);
827
828 #define SHADOWSPHERE_SEGMENTS 16
829
830 shadowmesh_t *shadowsphere;
831 void R_CreateShadowSphere(void)
832 {
833         int i, j;
834         vec3_t angles, angles2, angles3, angles4;
835         float verts[12];
836         shadowsphere = Mod_ShadowMesh_Begin(zonemempool, SHADOWSPHERE_SEGMENTS * SHADOWSPHERE_SEGMENTS / 2);
837         for (i = 0;i < SHADOWSPHERE_SEGMENTS / 2;i++)
838         {
839                 for (j = 0;j < SHADOWSPHERE_SEGMENTS;j++)
840                 {
841                         angles[0] = (i * 360.0f / SHADOWSPHERE_SEGMENTS) + 90.0f;
842                         angles[1] = j * 360.0f / SHADOWSPHERE_SEGMENTS;
843                         angles[2] = 0;
844                         VectorCopy(angles, angles2);
845                         VectorCopy(angles, angles3);
846                         VectorCopy(angles, angles4);
847                         angles2[1] += 360.0f / SHADOWSPHERE_SEGMENTS;
848                         angles3[0] += 360.0f / SHADOWSPHERE_SEGMENTS;
849                         angles3[1] += 360.0f / SHADOWSPHERE_SEGMENTS;
850                         angles4[0] += 360.0f / SHADOWSPHERE_SEGMENTS;
851                         AngleVectorsFLU(angles, verts, NULL, NULL);
852                         AngleVectorsFLU(angles2, verts + 9, NULL, NULL);
853                         AngleVectorsFLU(angles3, verts + 6, NULL, NULL);
854                         AngleVectorsFLU(angles4, verts + 3, NULL, NULL);
855                         VectorScale(&verts[0], 1.0f, &verts[0]);
856                         VectorScale(&verts[3], 1.0f, &verts[3]);
857                         VectorScale(&verts[6], 1.0f, &verts[6]);
858                         VectorScale(&verts[9], 1.0f, &verts[9]);
859                         Mod_ShadowMesh_AddPolygon(zonemempool, shadowsphere, 4, verts);
860                 }
861         }
862         shadowsphere = Mod_ShadowMesh_Finish(zonemempool, shadowsphere);
863 }
864
865
866 void R_DrawShadowSphere(vec3_t origin, float cullradius, float lightradius)
867 {
868         shadowmesh_t *mesh;
869         matrix4x4_t matrix;
870         if (!shadowsphere)
871                 R_CreateShadowSphere();
872         Matrix4x4_CreateScale(&matrix, lightradius);
873         Matrix4x4_ConcatTranslate(&matrix, origin[0], origin[1], origin[2]);
874         R_Mesh_Matrix(&matrix);
875         for (mesh = shadowsphere;mesh;mesh = mesh->next)
876         {
877                 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
878                 R_Shadow_RenderVolume(mesh->numverts, mesh->numtriangles, mesh->elements);
879         }
880         Matrix4x4_CreateScale(&matrix, -cullradius);
881         Matrix4x4_ConcatTranslate(&matrix, origin[0], origin[1], origin[2]);
882         R_Mesh_Matrix(&matrix);
883         for (mesh = shadowsphere;mesh;mesh = mesh->next)
884         {
885                 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
886                 R_Shadow_RenderVolume(mesh->numverts, mesh->numtriangles, mesh->elements);
887         }
888 }
889
890 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);
891 void R_ShadowVolumeLighting (int visiblevolumes)
892 {
893         int i;
894         entity_render_t *ent;
895         int lnum;
896         float f, lightradius, cullradius;
897         vec3_t relativelightorigin, relativeeyeorigin, lightcolor, clipmins, clipmaxs;
898         worldlight_t *wl;
899         //mlight_t *sl;
900         rdlight_t *rd;
901         rmeshstate_t m;
902         mleaf_t *leaf;
903
904         if (visiblevolumes)
905         {
906                 memset(&m, 0, sizeof(m));
907                 m.blendfunc1 = GL_ONE;
908                 m.blendfunc2 = GL_ONE;
909                 if (r_shadow_realtime.integer >= 3)
910                         m.depthdisable = true;
911                 R_Mesh_State(&m);
912                 qglDisable(GL_CULL_FACE);
913                 GL_Color(0.0 * r_colorscale, 0.0125 * r_colorscale, 0.1 * r_colorscale, 1);
914         }
915         else
916                 R_Shadow_Stage_Begin();
917         shadowframecount++;
918         for (lnum = 0, wl = r_shadow_worldlightchain;wl;wl = wl->next, lnum++)
919         {
920                 if (d_lightstylevalue[wl->style] <= 0)
921                         continue;
922                 cullradius = wl->cullradius;
923                 lightradius = wl->lightradius;
924                 if (R_CullSphere(wl->origin, lightradius))
925                         continue;
926                 //if (R_CullBox(wl->mins, wl->maxs) || R_CullSphere(wl->origin, lightradius))
927                 //      continue;
928                 //if (VIS_CullBox(wl->mins, wl->maxs) || VIS_CullSphere(wl->origin, lightradius))
929                 //      continue;
930                 if (r_shadow_debuglight.integer >= 0 && lnum != r_shadow_debuglight.integer)
931                         continue;
932
933                 if (cl.worldmodel != NULL)
934                 {
935                         for (i = 0;i < wl->numleafs;i++)
936                                 if (wl->leafs[i]->visframe == r_framecount)
937                                         break;
938                         if (i == wl->numleafs)
939                                 continue;
940                         leaf = wl->leafs[i++];
941                         VectorCopy(leaf->mins, clipmins);
942                         VectorCopy(leaf->maxs, clipmaxs);
943                         for (i++;i < wl->numleafs;i++)
944                         {
945                                 leaf = wl->leafs[i];
946                                 if (leaf->visframe == r_framecount)
947                                 {
948                                         if (clipmins[0] > leaf->mins[0]) clipmins[0] = leaf->mins[0];
949                                         if (clipmaxs[0] < leaf->maxs[0]) clipmaxs[0] = leaf->maxs[0];
950                                         if (clipmins[1] > leaf->mins[1]) clipmins[1] = leaf->mins[1];
951                                         if (clipmaxs[1] < leaf->maxs[1]) clipmaxs[1] = leaf->maxs[1];
952                                         if (clipmins[2] > leaf->mins[2]) clipmins[2] = leaf->mins[2];
953                                         if (clipmaxs[2] < leaf->maxs[2]) clipmaxs[2] = leaf->maxs[2];
954                                 }
955                         }
956                         if (clipmins[0] < wl->mins[0]) clipmins[0] = wl->mins[0];
957                         if (clipmins[1] < wl->mins[1]) clipmins[1] = wl->mins[1];
958                         if (clipmins[2] < wl->mins[2]) clipmins[2] = wl->mins[2];
959                         if (clipmaxs[0] > wl->maxs[0]) clipmaxs[0] = wl->maxs[0];
960                         if (clipmaxs[1] > wl->maxs[1]) clipmaxs[1] = wl->maxs[1];
961                         if (clipmaxs[2] > wl->maxs[2]) clipmaxs[2] = wl->maxs[2];
962                 }
963                 else
964                 {
965                         VectorCopy(wl->mins, clipmins);
966                         VectorCopy(wl->maxs, clipmaxs);
967                 }
968
969                 if (R_Shadow_ScissorForBBoxAndSphere(clipmins, clipmaxs, wl->origin, wl->cullradius))
970                         continue;
971
972                 // mark the leafs we care about so only things in those leafs will matter
973                 if (cl.worldmodel != NULL)
974                         for (i = 0;i < wl->numleafs;i++)
975                                 wl->leafs[i]->worldnodeframe = shadowframecount;
976
977                 f = d_lightstylevalue[wl->style] * (1.0f / 256.0f);
978                 VectorScale(wl->light, f, lightcolor);
979                 if (wl->selected)
980                 {
981                         f = 2 + sin(realtime * M_PI * 4.0);
982                         VectorScale(lightcolor, f, lightcolor);
983                 }
984
985                 if (wl->castshadows)
986                 {
987                         if (!visiblevolumes)
988                                 R_Shadow_Stage_ShadowVolumes();
989                         ent = &cl_entities[0].render;
990                         if (wl->shadowvolume && r_shadow_staticworldlights.integer)
991                                 R_Shadow_DrawWorldLightShadowVolume(&ent->matrix, wl);
992                         else
993                                 R_TestAndDrawShadowVolume(ent, wl->origin, cullradius, lightradius, wl->mins, wl->maxs, clipmins, clipmaxs);
994                         if (r_drawentities.integer)
995                                 for (i = 0;i < r_refdef.numentities;i++)
996                                         R_TestAndDrawShadowVolume(r_refdef.entities[i], wl->origin, cullradius, lightradius, wl->mins, wl->maxs, clipmins, clipmaxs);
997                 }
998
999                 if (!visiblevolumes)
1000                 {
1001                         if (wl->castshadows)
1002                                 R_Shadow_Stage_LightWithShadows();
1003                         else
1004                                 R_Shadow_Stage_LightWithoutShadows();
1005                         ent = &cl_entities[0].render;
1006                         if (ent->model && ent->model->DrawLight)
1007                         {
1008                                 Matrix4x4_Transform(&ent->inversematrix, wl->origin, relativelightorigin);
1009                                 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
1010                                 if (wl->numsurfaces)
1011                                         R_Model_Brush_DrawLightForSurfaceList(ent, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, wl->surfaces, wl->numsurfaces);
1012                                 else
1013                                         ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius / ent->scale, lightcolor);
1014                         }
1015                         if (r_drawentities.integer)
1016                         {
1017                                 for (i = 0;i < r_refdef.numentities;i++)
1018                                 {
1019                                         ent = r_refdef.entities[i];
1020                                         if (ent->visframe == r_framecount && ent->model && ent->model->DrawLight
1021                                          && ent->maxs[0] >= wl->mins[0] && ent->mins[0] <= wl->maxs[0]
1022                                          && ent->maxs[1] >= wl->mins[1] && ent->mins[1] <= wl->maxs[1]
1023                                          && ent->maxs[2] >= wl->mins[2] && ent->mins[2] <= wl->maxs[2]
1024                                          && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
1025                                         {
1026                                                 Matrix4x4_Transform(&ent->inversematrix, wl->origin, relativelightorigin);
1027                                                 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
1028                                                 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius / ent->scale, lightcolor);
1029                                         }
1030                                 }
1031                         }
1032                 }
1033         }
1034         /*
1035         for (lnum = 0, sl = cl.worldmodel->lights;lnum < cl.worldmodel->numlights;lnum++, sl++)
1036         {
1037                 if (d_lightstylevalue[sl->style] <= 0)
1038                         continue;
1039                 if (r_shadow_debuglight.integer >= 0 && lnum != r_shadow_debuglight.integer)
1040                         continue;
1041                 cullradius = sl->cullradius;
1042                 lightradius = sl->lightradius;
1043                 if (VIS_CullBox(sl->mins, sl->maxs) || VIS_CullSphere(sl->origin, lightradius))
1044                         continue;
1045
1046                 f = d_lightstylevalue[sl->style] * (1.0f / 32768.0f);
1047                 VectorScale(sl->light, f, lightcolor);
1048
1049                 if (!visiblevolumes)
1050                         R_Shadow_Stage_ShadowVolumes();
1051                 if (sl->shadowvolume && r_shadow_staticworldlights.integer)
1052                         R_DrawWorldLightShadowVolume(&cl_entities[0].render.matrix, sl->shadowvolume);
1053                 else
1054                         R_TestAndDrawShadowVolume(&cl_entities[0].render, sl->origin, cullradius, lightradius);
1055                 if (r_drawentities.integer)
1056                 {
1057                         for (i = 0;i < r_refdef.numentities;i++)
1058                         {
1059                                 ent = r_refdef.entities[i];
1060                                 if (ent->maxs[0] >= sl->mins[0] && ent->mins[0] <= sl->maxs[0]
1061                                  && ent->maxs[1] >= sl->mins[1] && ent->mins[1] <= sl->maxs[1]
1062                                  && ent->maxs[2] >= sl->mins[2] && ent->mins[2] <= sl->maxs[2]
1063                                  && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
1064                                         R_TestAndDrawShadowVolume(r_refdef.entities[i], sl->origin, cullradius, lightradius);
1065                         }
1066                 }
1067
1068                 if (!visiblevolumes)
1069                 {
1070                         R_Shadow_Stage_Light();
1071                         ent = &cl_entities[0].render;
1072                         if (ent->model && ent->model->DrawLight)
1073                         {
1074                                 Matrix4x4_Transform(&ent->inversematrix, sl->origin, relativelightorigin);
1075                                 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
1076                                 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, sl->distbias, sl->subtract, lightcolor);
1077                         }
1078                         if (r_drawentities.integer)
1079                         {
1080                                 for (i = 0;i < r_refdef.numentities;i++)
1081                                 {
1082                                         ent = r_refdef.entities[i];
1083                                         if (ent->visframe == r_framecount && ent->model && ent->model->DrawLight
1084                                          && ent->maxs[0] >= sl->mins[0] && ent->mins[0] <= sl->maxs[0]
1085                                          && ent->maxs[1] >= sl->mins[1] && ent->mins[1] <= sl->maxs[1]
1086                                          && ent->maxs[2] >= sl->mins[2] && ent->mins[2] <= sl->maxs[2]
1087                                          && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
1088                                         {
1089                                                 Matrix4x4_Transform(&ent->inversematrix, sl->origin, relativelightorigin);
1090                                                 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
1091                                                 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, sl->distbias, sl->subtract, lightcolor);
1092                                         }
1093                                 }
1094                         }
1095                 }
1096         }
1097         */
1098         for (lnum = 0, rd = r_dlight;lnum < r_numdlights;lnum++, rd++)
1099         {
1100                 cullradius = rd->cullradius;
1101                 lightradius = rd->cullradius;
1102                 if (VIS_CullSphere(rd->origin, lightradius))
1103                         continue;
1104
1105                 VectorScale(rd->light, (1.0f / 8192.0f), lightcolor);
1106                 clipmins[0] = rd->origin[0] - cullradius;
1107                 clipmins[1] = rd->origin[1] - cullradius;
1108                 clipmins[2] = rd->origin[2] - cullradius;
1109                 clipmaxs[0] = rd->origin[0] + cullradius;
1110                 clipmaxs[1] = rd->origin[1] + cullradius;
1111                 clipmaxs[2] = rd->origin[2] + cullradius;
1112
1113                 if (R_Shadow_ScissorForBBoxAndSphere(clipmins, clipmaxs, rd->origin, rd->cullradius))
1114                         continue;
1115
1116                 if (!visiblevolumes)
1117                         R_Shadow_Stage_ShadowVolumes();
1118                 ent = &cl_entities[0].render;
1119                 R_TestAndDrawShadowVolume(ent, rd->origin, cullradius, lightradius, clipmins, clipmaxs, clipmins, clipmaxs);
1120                 if (r_drawentities.integer)
1121                 {
1122                         for (i = 0;i < r_refdef.numentities;i++)
1123                         {
1124                                 ent = r_refdef.entities[i];
1125                                 if (ent != rd->ent)
1126                                         R_TestAndDrawShadowVolume(ent, rd->origin, cullradius, lightradius, clipmins, clipmaxs, clipmins, clipmaxs);
1127                         }
1128                 }
1129
1130                 if (!visiblevolumes)
1131                 {
1132                         R_Shadow_Stage_LightWithShadows();
1133                         ent = &cl_entities[0].render;
1134                         if (ent->model && ent->model->DrawLight)
1135                         {
1136                                 Matrix4x4_Transform(&ent->inversematrix, rd->origin, relativelightorigin);
1137                                 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
1138                                 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius / ent->scale, lightcolor);
1139                         }
1140                         if (r_drawentities.integer)
1141                         {
1142                                 for (i = 0;i < r_refdef.numentities;i++)
1143                                 {
1144                                         ent = r_refdef.entities[i];
1145                                         if (ent->visframe == r_framecount && ent->model && ent->model->DrawLight
1146                                          && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
1147                                         {
1148                                                 Matrix4x4_Transform(&ent->inversematrix, rd->origin, relativelightorigin);
1149                                                 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
1150                                                 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius / ent->scale, lightcolor);
1151                                         }
1152                                 }
1153                         }
1154                 }
1155         }
1156
1157         if (!visiblevolumes)
1158                 R_Shadow_Stage_End();
1159         qglEnable(GL_CULL_FACE);
1160         qglDisable(GL_SCISSOR_TEST);
1161 }
1162
1163 static void R_SetFrustum (void)
1164 {
1165         // LordHavoc: note to all quake engine coders, the special case for 90
1166         // degrees assumed a square view (wrong), so I removed it, Quake2 has it
1167         // disabled as well.
1168
1169         // rotate VPN right by FOV_X/2 degrees
1170         RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
1171         frustum[0].dist = DotProduct (r_origin, frustum[0].normal);
1172         PlaneClassify(&frustum[0]);
1173
1174         // rotate VPN left by FOV_X/2 degrees
1175         RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
1176         frustum[1].dist = DotProduct (r_origin, frustum[1].normal);
1177         PlaneClassify(&frustum[1]);
1178
1179         // rotate VPN up by FOV_X/2 degrees
1180         RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
1181         frustum[2].dist = DotProduct (r_origin, frustum[2].normal);
1182         PlaneClassify(&frustum[2]);
1183
1184         // rotate VPN down by FOV_X/2 degrees
1185         RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
1186         frustum[3].dist = DotProduct (r_origin, frustum[3].normal);
1187         PlaneClassify(&frustum[3]);
1188 }
1189
1190 /*
1191 ===============
1192 R_SetupFrame
1193 ===============
1194 */
1195 static void R_SetupFrame (void)
1196 {
1197 // don't allow cheats in multiplayer
1198         if (cl.maxclients > 1)
1199         {
1200                 if (r_fullbright.integer != 0)
1201                         Cvar_Set ("r_fullbright", "0");
1202                 if (r_ambient.value != 0)
1203                         Cvar_Set ("r_ambient", "0");
1204         }
1205
1206         r_framecount++;
1207
1208 // build the transformation matrix for the given view angles
1209         VectorCopy (r_refdef.vieworg, r_origin);
1210
1211         AngleVectors (r_refdef.viewangles, vpn, vright, vup);
1212
1213         R_AnimateLight ();
1214 }
1215
1216
1217 static void R_BlendView(void)
1218 {
1219         rmeshstate_t m;
1220         float r;
1221
1222         if (r_refdef.viewblend[3] < 0.01f)
1223                 return;
1224
1225         memset(&m, 0, sizeof(m));
1226         m.blendfunc1 = GL_SRC_ALPHA;
1227         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1228         m.depthdisable = true; // magic
1229         R_Mesh_Matrix(&r_identitymatrix);
1230         R_Mesh_State(&m);
1231
1232         r = 64000;
1233         varray_vertex[0] = r_origin[0] + vpn[0] * 1.5 - vright[0] * r - vup[0] * r;
1234         varray_vertex[1] = r_origin[1] + vpn[1] * 1.5 - vright[1] * r - vup[1] * r;
1235         varray_vertex[2] = r_origin[2] + vpn[2] * 1.5 - vright[2] * r - vup[2] * r;
1236         r *= 3;
1237         varray_vertex[4] = varray_vertex[0] + vup[0] * r;
1238         varray_vertex[5] = varray_vertex[1] + vup[1] * r;
1239         varray_vertex[6] = varray_vertex[2] + vup[2] * r;
1240         varray_vertex[8] = varray_vertex[0] + vright[0] * r;
1241         varray_vertex[9] = varray_vertex[1] + vright[1] * r;
1242         varray_vertex[10] = varray_vertex[2] + vright[2] * r;
1243         GL_Color(r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
1244         R_Mesh_Draw(3, 1, polygonelements);
1245 }
1246
1247 /*
1248 ================
1249 R_RenderView
1250
1251 r_refdef must be set before the first call
1252 ================
1253 */
1254 extern void R_DrawLightningBeams (void);
1255 void R_RenderView (void)
1256 {
1257         entity_render_t *world;
1258         if (!r_refdef.entities/* || !cl.worldmodel*/)
1259                 return; //Host_Error ("R_RenderView: NULL worldmodel");
1260
1261         if (r_shadow_realtime.integer == 1)
1262         {
1263                 if (!gl_texturecubemap)
1264                 {
1265                         Con_Printf("Cubemap texture support not detected, turning off r_shadow_realtime\n");
1266                         Cvar_SetValueQuick(&r_shadow_realtime, 0);
1267                 }
1268                 else if (!gl_dot3arb)
1269                 {
1270                         Con_Printf("Bumpmapping support not detected, turning off r_shadow_realtime\n");
1271                         Cvar_SetValueQuick(&r_shadow_realtime, 0);
1272                 }
1273                 else if (!gl_stencil)
1274                 {
1275                         Con_Printf("Stencil not enabled, turning off r_shadow_realtime, please type vid_stencil 1;vid_bitsperpixel 32;vid_restart and try again\n");
1276                         Cvar_SetValueQuick(&r_shadow_realtime, 0);
1277                 }
1278                 else if (!gl_combine.integer)
1279                 {
1280                         Con_Printf("Combine disabled, please turn on gl_combine, turning off r_shadow_realtime\n");
1281                         Cvar_SetValueQuick(&r_shadow_realtime, 0);
1282                 }
1283         }
1284
1285         R_Shadow_UpdateLightingMode();
1286
1287         world = &cl_entities[0].render;
1288
1289         // FIXME: move to client
1290         R_MoveExplosions();
1291         R_TimeReport("mexplosion");
1292
1293         R_Textures_Frame();
1294         R_SetupFrame();
1295         R_SetFrustum();
1296         R_SetupFog();
1297         R_SkyStartFrame();
1298         R_BuildLightList();
1299         R_TimeReport("setup");
1300
1301         R_WorldVisibility(world);
1302         R_TimeReport("worldvis");
1303
1304         R_FarClip_Start(r_origin, vpn, 768.0f);
1305         R_MarkEntities();
1306         r_farclip = R_FarClip_Finish() + 256.0f;
1307         R_TimeReport("markentity");
1308
1309         GL_SetupView_ViewPort(r_refdef.x, r_refdef.y, r_refdef.width, r_refdef.height);
1310         if (r_shadow_lightingmode > 0)
1311                 GL_SetupView_Mode_PerspectiveInfiniteFarClip(r_refdef.fov_x, r_refdef.fov_y, 1.0f);
1312         else
1313                 GL_SetupView_Mode_Perspective(r_refdef.fov_x, r_refdef.fov_y, 1.0f, r_farclip);
1314         GL_SetupView_Orientation_FromEntity (r_refdef.vieworg, r_refdef.viewangles);
1315         qglDepthFunc(GL_LEQUAL);
1316
1317         R_Mesh_Start();
1318         R_MeshQueue_BeginScene();
1319
1320         R_Shadow_UpdateWorldLightSelection();
1321
1322         if (R_DrawBrushModelsSky())
1323                 R_TimeReport("bmodelsky");
1324
1325         // must occur early because it can draw sky
1326         R_DrawWorld(world);
1327         R_TimeReport("world");
1328
1329         // don't let sound skip if going slow
1330         if (!intimerefresh && !r_speeds.integer)
1331                 S_ExtraUpdate ();
1332
1333         R_DrawModels(r_shadow_lightingmode > 0);
1334         R_TimeReport("models");
1335
1336         if (r_shadows.integer == 1 && r_shadow_lightingmode <= 0)
1337         {
1338                 R_DrawFakeShadows();
1339                 R_TimeReport("fakeshadow");
1340         }
1341
1342         if (r_shadow_lightingmode > 0)
1343         {
1344                 R_ShadowVolumeLighting(false);
1345                 R_TimeReport("dynlight");
1346         }
1347
1348         R_DrawLightningBeams();
1349         R_TimeReport("lightning");
1350
1351         R_DrawParticles();
1352         R_TimeReport("particles");
1353
1354         R_DrawExplosions();
1355         R_TimeReport("explosions");
1356
1357         R_MeshQueue_RenderTransparent();
1358         R_TimeReport("drawtrans");
1359
1360         R_DrawCoronas();
1361         R_TimeReport("coronas");
1362
1363         R_DrawWorldCrosshair();
1364         R_TimeReport("crosshair");
1365
1366         R_BlendView();
1367         R_TimeReport("blendview");
1368
1369         R_MeshQueue_Render();
1370         R_MeshQueue_EndScene();
1371         if (r_shadow_realtime.integer >= 2)
1372         {
1373                 R_ShadowVolumeLighting(true);
1374                 R_TimeReport("shadowvolume");
1375         }
1376         R_Mesh_Finish();
1377         R_TimeReport("meshfinish");
1378 }
1379
1380 /*
1381 void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
1382 {
1383         int i;
1384         float *v, *c, f1, f2, diff[3];
1385         rmeshstate_t m;
1386         m.blendfunc1 = GL_SRC_ALPHA;
1387         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1388         R_Mesh_Matrix(&r_identitymatrix);
1389         R_Mesh_State(&m);
1390
1391         varray_vertex[ 0] = mins[0];varray_vertex[ 1] = mins[1];varray_vertex[ 2] = mins[2];
1392         varray_vertex[ 4] = maxs[0];varray_vertex[ 5] = mins[1];varray_vertex[ 6] = mins[2];
1393         varray_vertex[ 8] = mins[0];varray_vertex[ 9] = maxs[1];varray_vertex[10] = mins[2];
1394         varray_vertex[12] = maxs[0];varray_vertex[13] = maxs[1];varray_vertex[14] = mins[2];
1395         varray_vertex[16] = mins[0];varray_vertex[17] = mins[1];varray_vertex[18] = maxs[2];
1396         varray_vertex[20] = maxs[0];varray_vertex[21] = mins[1];varray_vertex[22] = maxs[2];
1397         varray_vertex[24] = mins[0];varray_vertex[25] = maxs[1];varray_vertex[26] = maxs[2];
1398         varray_vertex[28] = maxs[0];varray_vertex[29] = maxs[1];varray_vertex[30] = maxs[2];
1399         R_FillColors(varray_color, 8, cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
1400         if (fogenabled)
1401         {
1402                 for (i = 0, v = varray_vertex, c = varray_color;i < 8;i++, v += 4, c += 4)
1403                 {
1404                         VectorSubtract(v, r_origin, diff);
1405                         f2 = exp(fogdensity/DotProduct(diff, diff));
1406                         f1 = 1 - f2;
1407                         f2 *= r_colorscale;
1408                         c[0] = c[0] * f1 + fogcolor[0] * f2;
1409                         c[1] = c[1] * f1 + fogcolor[1] * f2;
1410                         c[2] = c[2] * f1 + fogcolor[2] * f2;
1411                 }
1412         }
1413         GL_UseColorArray();
1414         R_Mesh_Draw(8, 12);
1415 }
1416 */
1417
1418 void R_DrawNoModelCallback(const void *calldata1, int calldata2)
1419 {
1420         const entity_render_t *ent = calldata1;
1421         int i, element[24];
1422         float f1, f2, *c, diff[3];
1423         rmeshstate_t m;
1424         memset(&m, 0, sizeof(m));
1425         if (ent->flags & EF_ADDITIVE)
1426         {
1427                 m.blendfunc1 = GL_SRC_ALPHA;
1428                 m.blendfunc2 = GL_ONE;
1429         }
1430         else if (ent->alpha < 1)
1431         {
1432                 m.blendfunc1 = GL_SRC_ALPHA;
1433                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1434         }
1435         else
1436         {
1437                 m.blendfunc1 = GL_ONE;
1438                 m.blendfunc2 = GL_ZERO;
1439         }
1440         R_Mesh_Matrix(&ent->matrix);
1441         R_Mesh_State(&m);
1442
1443         element[ 0] = 5;element[ 1] = 2;element[ 2] = 0;
1444         element[ 3] = 5;element[ 4] = 1;element[ 5] = 2;
1445         element[ 6] = 5;element[ 7] = 0;element[ 8] = 3;
1446         element[ 9] = 5;element[10] = 3;element[11] = 1;
1447         element[12] = 0;element[13] = 2;element[14] = 4;
1448         element[15] = 2;element[16] = 1;element[17] = 4;
1449         element[18] = 3;element[19] = 0;element[20] = 4;
1450         element[21] = 1;element[22] = 3;element[23] = 4;
1451         varray_vertex[ 0] = -16;varray_vertex[ 1] =   0;varray_vertex[ 2] =   0;
1452         varray_vertex[ 4] =  16;varray_vertex[ 5] =   0;varray_vertex[ 6] =   0;
1453         varray_vertex[ 8] =   0;varray_vertex[ 9] = -16;varray_vertex[10] =   0;
1454         varray_vertex[12] =   0;varray_vertex[13] =  16;varray_vertex[14] =   0;
1455         varray_vertex[16] =   0;varray_vertex[17] =   0;varray_vertex[18] = -16;
1456         varray_vertex[20] =   0;varray_vertex[21] =   0;varray_vertex[22] =  16;
1457         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;
1458         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;
1459         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;
1460         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;
1461         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;
1462         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;
1463         if (fogenabled)
1464         {
1465                 VectorSubtract(ent->origin, r_origin, diff);
1466                 f2 = exp(fogdensity/DotProduct(diff, diff));
1467                 f1 = 1 - f2;
1468                 for (i = 0, c = varray_color;i < 6;i++, c += 4)
1469                 {
1470                         c[0] = (c[0] * f1 + fogcolor[0] * f2) * r_colorscale;
1471                         c[1] = (c[1] * f1 + fogcolor[1] * f2) * r_colorscale;
1472                         c[2] = (c[2] * f1 + fogcolor[2] * f2) * r_colorscale;
1473                 }
1474         }
1475         else
1476         {
1477                 for (i = 0, c = varray_color;i < 6;i++, c += 4)
1478                 {
1479                         c[0] *= r_colorscale;
1480                         c[1] *= r_colorscale;
1481                         c[2] *= r_colorscale;
1482                 }
1483         }
1484         GL_UseColorArray();
1485         R_Mesh_Draw(6, 8, element);
1486 }
1487
1488 void R_DrawNoModel(entity_render_t *ent)
1489 {
1490         //if ((ent->effects & EF_ADDITIVE) || (ent->alpha < 1))
1491                 R_MeshQueue_AddTransparent(ent->origin, R_DrawNoModelCallback, ent, 0);
1492         //else
1493         //      R_DrawNoModelCallback(ent, 0);
1494 }
1495
1496 void R_CalcBeamVerts (float *vert, const vec3_t org1, const vec3_t org2, float width)
1497 {
1498         vec3_t right1, right2, diff, normal;
1499
1500         VectorSubtract (org2, org1, normal);
1501         VectorNormalizeFast (normal);
1502
1503         // calculate 'right' vector for start
1504         VectorSubtract (r_origin, org1, diff);
1505         VectorNormalizeFast (diff);
1506         CrossProduct (normal, diff, right1);
1507
1508         // calculate 'right' vector for end
1509         VectorSubtract (r_origin, org2, diff);
1510         VectorNormalizeFast (diff);
1511         CrossProduct (normal, diff, right2);
1512
1513         vert[ 0] = org1[0] + width * right1[0];
1514         vert[ 1] = org1[1] + width * right1[1];
1515         vert[ 2] = org1[2] + width * right1[2];
1516         vert[ 4] = org1[0] - width * right1[0];
1517         vert[ 5] = org1[1] - width * right1[1];
1518         vert[ 6] = org1[2] - width * right1[2];
1519         vert[ 8] = org2[0] - width * right2[0];
1520         vert[ 9] = org2[1] - width * right2[1];
1521         vert[10] = org2[2] - width * right2[2];
1522         vert[12] = org2[0] + width * right2[0];
1523         vert[13] = org2[1] + width * right2[1];
1524         vert[14] = org2[2] + width * right2[2];
1525 }