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