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