added R_CalcBeamVerts function, calculates vertex array for beam polygon
[xonotic/darkplaces.git] / gl_rmain.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_main.c
21
22 #include "quakedef.h"
23
24 // used for dlight push checking and other things
25 int r_framecount;
26
27 mplane_t frustum[4];
28
29 matrix4x4_t r_identitymatrix;
30
31 int c_alias_polys, c_light_polys, c_faces, c_nodes, c_leafs, c_models, c_bmodels, c_sprites, c_particles, c_dlights;
32
33 // true during envmap command capture
34 qboolean envmap;
35
36 float r_farclip;
37
38 // view origin
39 vec3_t r_origin;
40 vec3_t vpn;
41 vec3_t vright;
42 vec3_t vup;
43
44 //
45 // screen size info
46 //
47 refdef_t r_refdef;
48
49 // 8.8 fraction of base light value
50 unsigned short d_lightstylevalue[256];
51
52 cvar_t r_drawentities = {0, "r_drawentities","1"};
53 cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1"};
54 cvar_t r_speeds = {0, "r_speeds","0"};
55 cvar_t r_fullbright = {0, "r_fullbright","0"};
56 cvar_t r_wateralpha = {CVAR_SAVE, "r_wateralpha","1"};
57 cvar_t r_dynamic = {CVAR_SAVE, "r_dynamic","1"};
58 cvar_t r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1"};
59
60 cvar_t gl_fogenable = {0, "gl_fogenable", "0"};
61 cvar_t gl_fogdensity = {0, "gl_fogdensity", "0.25"};
62 cvar_t gl_fogred = {0, "gl_fogred","0.3"};
63 cvar_t gl_foggreen = {0, "gl_foggreen","0.3"};
64 cvar_t gl_fogblue = {0, "gl_fogblue","0.3"};
65 cvar_t gl_fogstart = {0, "gl_fogstart", "0"};
66 cvar_t gl_fogend = {0, "gl_fogend","0"};
67
68 cvar_t r_textureunits = {0, "r_textureunits", "32"};
69
70 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
71 {
72         int i;
73         for (i = 0;i < verts;i++)
74         {
75                 out[0] = in[0] * r;
76                 out[1] = in[1] * g;
77                 out[2] = in[2] * b;
78                 out[3] = in[3];
79                 in += 4;
80                 out += 4;
81         }
82 }
83
84 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
85 {
86         int i;
87         for (i = 0;i < verts;i++)
88         {
89                 out[0] = r;
90                 out[1] = g;
91                 out[2] = b;
92                 out[3] = a;
93                 out += 4;
94         }
95 }
96
97 /*
98 ====================
99 R_TimeRefresh_f
100
101 For program optimization
102 ====================
103 */
104 qboolean intimerefresh = 0;
105 static void R_TimeRefresh_f (void)
106 {
107         int i;
108         float start, stop, time;
109
110         intimerefresh = 1;
111         start = Sys_DoubleTime ();
112         for (i = 0;i < 128;i++)
113         {
114                 r_refdef.viewangles[0] = 0;
115                 r_refdef.viewangles[1] = i/128.0*360.0;
116                 r_refdef.viewangles[2] = 0;
117                 CL_UpdateScreen();
118         }
119
120         stop = Sys_DoubleTime ();
121         intimerefresh = 0;
122         time = stop-start;
123         Con_Printf ("%f seconds (%f fps)\n", time, 128/time);
124 }
125
126 vec3_t fogcolor;
127 vec_t fogdensity;
128 float fog_density, fog_red, fog_green, fog_blue;
129 qboolean fogenabled;
130 qboolean oldgl_fogenable;
131 void R_SetupFog(void)
132 {
133         if (gamemode == GAME_NEHAHRA)
134         {
135                 if (gl_fogenable.integer)
136                 {
137                         oldgl_fogenable = true;
138                         fog_density = gl_fogdensity.value;
139                         fog_red = gl_fogred.value;
140                         fog_green = gl_foggreen.value;
141                         fog_blue = gl_fogblue.value;
142                 }
143                 else if (oldgl_fogenable)
144                 {
145                         oldgl_fogenable = false;
146                         fog_density = 0;
147                         fog_red = 0;
148                         fog_green = 0;
149                         fog_blue = 0;
150                 }
151         }
152         if (fog_density)
153         {
154                 fogcolor[0] = fog_red   = bound(0.0f, fog_red  , 1.0f);
155                 fogcolor[1] = fog_green = bound(0.0f, fog_green, 1.0f);
156                 fogcolor[2] = fog_blue  = bound(0.0f, fog_blue , 1.0f);
157         }
158         if (fog_density)
159         {
160                 fogenabled = true;
161                 fogdensity = -4000.0f / (fog_density * fog_density);
162                 // fog color was already set
163         }
164         else
165                 fogenabled = false;
166 }
167
168 // FIXME: move this to client?
169 void FOG_clear(void)
170 {
171         if (gamemode == GAME_NEHAHRA)
172         {
173                 Cvar_Set("gl_fogenable", "0");
174                 Cvar_Set("gl_fogdensity", "0.2");
175                 Cvar_Set("gl_fogred", "0.3");
176                 Cvar_Set("gl_foggreen", "0.3");
177                 Cvar_Set("gl_fogblue", "0.3");
178         }
179         fog_density = fog_red = fog_green = fog_blue = 0.0f;
180 }
181
182 // FIXME: move this to client?
183 void FOG_registercvars(void)
184 {
185         if (gamemode == GAME_NEHAHRA)
186         {
187                 Cvar_RegisterVariable (&gl_fogenable);
188                 Cvar_RegisterVariable (&gl_fogdensity);
189                 Cvar_RegisterVariable (&gl_fogred);
190                 Cvar_RegisterVariable (&gl_foggreen);
191                 Cvar_RegisterVariable (&gl_fogblue);
192                 Cvar_RegisterVariable (&gl_fogstart);
193                 Cvar_RegisterVariable (&gl_fogend);
194         }
195 }
196
197 void gl_main_start(void)
198 {
199 }
200
201 void gl_main_shutdown(void)
202 {
203 }
204
205 extern void CL_ParseEntityLump(char *entitystring);
206 void gl_main_newmap(void)
207 {
208         if (cl.worldmodel && cl.worldmodel->entities)
209                 CL_ParseEntityLump(cl.worldmodel->entities);
210         r_framecount = 1;
211 }
212
213 void GL_Main_Init(void)
214 {
215         Matrix4x4_CreateIdentity(&r_identitymatrix);
216 // FIXME: move this to client?
217         FOG_registercvars();
218         Cmd_AddCommand ("timerefresh", R_TimeRefresh_f);
219         Cvar_RegisterVariable (&r_drawentities);
220         Cvar_RegisterVariable (&r_drawviewmodel);
221         Cvar_RegisterVariable (&r_speeds);
222         Cvar_RegisterVariable (&r_fullbrights);
223         Cvar_RegisterVariable (&r_wateralpha);
224         Cvar_RegisterVariable (&r_dynamic);
225         Cvar_RegisterVariable (&r_fullbright);
226         Cvar_RegisterVariable (&r_textureunits);
227         if (gamemode == GAME_NEHAHRA)
228                 Cvar_SetValue("r_fullbrights", 0);
229         R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
230 }
231
232 vec3_t r_farclip_origin;
233 vec3_t r_farclip_direction;
234 vec_t r_farclip_directiondist;
235 vec_t r_farclip_meshfarclip;
236 int r_farclip_directionbit0;
237 int r_farclip_directionbit1;
238 int r_farclip_directionbit2;
239
240 // start a farclip measuring session
241 void R_FarClip_Start(vec3_t origin, vec3_t direction, vec_t startfarclip)
242 {
243         VectorCopy(origin, r_farclip_origin);
244         VectorCopy(direction, r_farclip_direction);
245         r_farclip_directiondist = DotProduct(r_farclip_origin, r_farclip_direction);
246         r_farclip_directionbit0 = r_farclip_direction[0] < 0;
247         r_farclip_directionbit1 = r_farclip_direction[1] < 0;
248         r_farclip_directionbit2 = r_farclip_direction[2] < 0;
249         r_farclip_meshfarclip = r_farclip_directiondist + startfarclip;
250 }
251
252 // enlarge farclip to accomodate box
253 void R_FarClip_Box(vec3_t mins, vec3_t maxs)
254 {
255         float d;
256         d = (r_farclip_directionbit0 ? mins[0] : maxs[0]) * r_farclip_direction[0]
257           + (r_farclip_directionbit1 ? mins[1] : maxs[1]) * r_farclip_direction[1]
258           + (r_farclip_directionbit2 ? mins[2] : maxs[2]) * r_farclip_direction[2];
259         if (r_farclip_meshfarclip < d)
260                 r_farclip_meshfarclip = d;
261 }
262
263 // return farclip value
264 float R_FarClip_Finish(void)
265 {
266         return r_farclip_meshfarclip - r_farclip_directiondist;
267 }
268
269 /*
270 ===============
271 R_NewMap
272 ===============
273 */
274 void R_NewMap (void)
275 {
276         R_Modules_NewMap();
277 }
278
279 extern void R_Textures_Init(void);
280 extern void Mod_RenderInit(void);
281 extern void GL_Draw_Init(void);
282 extern void GL_Main_Init(void);
283 extern void GL_Models_Init(void);
284 extern void R_Sky_Init(void);
285 extern void GL_Surf_Init(void);
286 extern void R_Crosshairs_Init(void);
287 extern void R_Light_Init(void);
288 extern void R_Particles_Init(void);
289 extern void R_Explosion_Init(void);
290 extern void ui_init(void);
291 extern void gl_backend_init(void);
292
293 void Render_Init(void)
294 {
295         R_Modules_Shutdown();
296         R_Textures_Init();
297         Mod_RenderInit();
298         gl_backend_init();
299         R_MeshQueue_Init();
300         GL_Draw_Init();
301         GL_Main_Init();
302         GL_Models_Init();
303         R_Sky_Init();
304         GL_Surf_Init();
305         R_Crosshairs_Init();
306         R_Light_Init();
307         R_Particles_Init();
308         R_Explosion_Init();
309         ui_init();
310         R_Modules_Start();
311 }
312
313 /*
314 ===============
315 GL_Init
316 ===============
317 */
318 extern char *ENGINE_EXTENSIONS;
319 void GL_Init (void)
320 {
321         VID_CheckExtensions();
322
323         // LordHavoc: report supported extensions
324         Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
325
326         qglCullFace(GL_FRONT);
327         qglEnable(GL_TEXTURE_2D);
328 }
329
330 int R_CullBox(const vec3_t emins, const vec3_t emaxs)
331 {
332         int i;
333         mplane_t *p;
334         for (i = 0;i < 4;i++)
335         {
336                 p = frustum + i;
337                 switch(p->signbits)
338                 {
339                 default:
340                 case 0:
341                         if (p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2] < p->dist)
342                                 return true;
343                         break;
344                 case 1:
345                         if (p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2] < p->dist)
346                                 return true;
347                         break;
348                 case 2:
349                         if (p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2] < p->dist)
350                                 return true;
351                         break;
352                 case 3:
353                         if (p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2] < p->dist)
354                                 return true;
355                         break;
356                 case 4:
357                         if (p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2] < p->dist)
358                                 return true;
359                         break;
360                 case 5:
361                         if (p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2] < p->dist)
362                                 return true;
363                         break;
364                 case 6:
365                         if (p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2] < p->dist)
366                                 return true;
367                         break;
368                 case 7:
369                         if (p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2] < p->dist)
370                                 return true;
371                         break;
372                 }
373         }
374         return false;
375 }
376
377 int R_NotCulledBox(const vec3_t emins, const vec3_t emaxs)
378 {
379         int i;
380         mplane_t *p;
381         for (i = 0;i < 4;i++)
382         {
383                 p = frustum + i;
384                 switch(p->signbits)
385                 {
386                 default:
387                 case 0:
388                         if (p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2] < p->dist)
389                                 return false;
390                         break;
391                 case 1:
392                         if (p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2] < p->dist)
393                                 return false;
394                         break;
395                 case 2:
396                         if (p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2] < p->dist)
397                                 return false;
398                         break;
399                 case 3:
400                         if (p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2] < p->dist)
401                                 return false;
402                         break;
403                 case 4:
404                         if (p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2] < p->dist)
405                                 return false;
406                         break;
407                 case 5:
408                         if (p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2] < p->dist)
409                                 return false;
410                         break;
411                 case 6:
412                         if (p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2] < p->dist)
413                                 return false;
414                         break;
415                 case 7:
416                         if (p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2] < p->dist)
417                                 return false;
418                         break;
419                 }
420         }
421         return true;
422 }
423
424
425 //==================================================================================
426
427 static void R_MarkEntities (void)
428 {
429         int i;
430         vec3_t v;
431         entity_render_t *ent;
432
433         ent = &cl_entities[0].render;
434         Matrix4x4_CreateIdentity(&ent->matrix);
435         Matrix4x4_CreateIdentity(&ent->inversematrix);
436
437         if (cl.worldmodel)
438                 R_FarClip_Box(cl.worldmodel->normalmins, cl.worldmodel->normalmaxs);
439
440         if (!r_drawentities.integer)
441                 return;
442
443         for (i = 0;i < r_refdef.numentities;i++)
444         {
445                 ent = r_refdef.entities[i];
446                 Mod_CheckLoaded(ent->model);
447
448                 // move view-relative models to where they should be
449                 if (ent->flags & RENDER_VIEWMODEL)
450                 {
451                         // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
452                         ent->flags -= RENDER_VIEWMODEL;
453                         // transform origin
454                         VectorCopy(ent->origin, v);
455                         ent->origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
456                         ent->origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
457                         ent->origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
458                         // adjust angles
459                         VectorAdd(ent->angles, r_refdef.viewangles, ent->angles);
460                 }
461
462                 if (R_CullBox(ent->mins, ent->maxs))
463                         continue;
464
465                 ent->visframe = r_framecount;
466                 VectorCopy(ent->angles, v);
467                 if (!ent->model || ent->model->type != mod_brush)
468                         v[0] = -v[0];
469                 Matrix4x4_CreateFromQuakeEntity(&ent->matrix, ent->origin[0], ent->origin[1], ent->origin[2], v[0], v[1], v[2], ent->scale);
470                 Matrix4x4_Invert_Simple(&ent->inversematrix, &ent->matrix);
471                 R_LerpAnimation(ent);
472                 R_UpdateEntLights(ent);
473                 R_FarClip_Box(ent->mins, ent->maxs);
474         }
475 }
476
477 // only used if skyrendermasked, and normally returns false
478 int R_DrawBrushModelsSky (void)
479 {
480         int i, sky;
481         entity_render_t *ent;
482
483         if (!r_drawentities.integer)
484                 return false;
485
486         sky = false;
487         for (i = 0;i < r_refdef.numentities;i++)
488         {
489                 ent = r_refdef.entities[i];
490                 if (ent->visframe == r_framecount && ent->model && ent->model->DrawSky)
491                 {
492                         ent->model->DrawSky(ent);
493                         sky = true;
494                 }
495         }
496         return sky;
497 }
498
499 /*
500 =============
501 R_DrawViewModel
502 =============
503 */
504 void R_DrawViewModel (void)
505 {
506         entity_render_t *ent;
507
508         // FIXME: move these checks to client
509         if (!r_drawviewmodel.integer || chase_active.integer || envmap || !r_drawentities.integer || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
510                 return;
511
512         ent = &cl.viewent.render;
513         Mod_CheckLoaded(ent->model);
514         R_LerpAnimation(ent);
515         Matrix4x4_CreateFromQuakeEntity(&ent->matrix, ent->origin[0], ent->origin[1], ent->origin[2], -ent->angles[0], ent->angles[1], ent->angles[2], ent->scale);
516         Matrix4x4_Invert_Simple(&ent->inversematrix, &ent->matrix);
517         R_UpdateEntLights(ent);
518         ent->model->Draw(ent);
519 }
520
521 void R_DrawNoModel(entity_render_t *ent);
522 void R_DrawModels (void)
523 {
524         int i;
525         entity_render_t *ent;
526
527         if (!r_drawentities.integer)
528                 return;
529
530         R_DrawViewModel();
531         for (i = 0;i < r_refdef.numentities;i++)
532         {
533                 ent = r_refdef.entities[i];
534                 if (ent->visframe == r_framecount)
535                 {
536                         if (ent->model)
537                         {
538                                 if (ent->model->Draw)
539                                         ent->model->Draw(ent);
540                         }
541                         else
542                                 R_DrawNoModel(ent);
543                 }
544         }
545 }
546
547 static void R_SetFrustum (void)
548 {
549         int i;
550
551         // LordHavoc: note to all quake engine coders, the special case for 90
552         // degrees assumed a square view (wrong), so I removed it, Quake2 has it
553         // disabled as well.
554         // rotate VPN right by FOV_X/2 degrees
555         RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
556         // rotate VPN left by FOV_X/2 degrees
557         RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
558         // rotate VPN up by FOV_X/2 degrees
559         RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
560         // rotate VPN down by FOV_X/2 degrees
561         RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
562
563         for (i = 0;i < 4;i++)
564         {
565                 frustum[i].type = PLANE_ANYZ;
566                 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
567                 PlaneClassify(&frustum[i]);
568         }
569 }
570
571 /*
572 ===============
573 R_SetupFrame
574 ===============
575 */
576 static void R_SetupFrame (void)
577 {
578 // don't allow cheats in multiplayer
579         if (cl.maxclients > 1)
580         {
581                 if (r_fullbright.integer != 0)
582                         Cvar_Set ("r_fullbright", "0");
583                 if (r_ambient.value != 0)
584                         Cvar_Set ("r_ambient", "0");
585         }
586
587         r_framecount++;
588
589 // build the transformation matrix for the given view angles
590         VectorCopy (r_refdef.vieworg, r_origin);
591
592         AngleVectors (r_refdef.viewangles, vpn, vright, vup);
593
594         R_AnimateLight ();
595 }
596
597
598 static void R_BlendView(void)
599 {
600         rmeshstate_t m;
601         float r;
602
603         if (r_refdef.viewblend[3] < 0.01f)
604                 return;
605
606         memset(&m, 0, sizeof(m));
607         m.blendfunc1 = GL_SRC_ALPHA;
608         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
609         m.depthdisable = true; // magic
610         R_Mesh_Matrix(&r_identitymatrix);
611         R_Mesh_State(&m);
612
613         varray_color[0] = varray_color[4] = varray_color[8] = r_refdef.viewblend[0];
614         varray_color[1] = varray_color[5] = varray_color[9] = r_refdef.viewblend[1];
615         varray_color[2] = varray_color[6] = varray_color[10] = r_refdef.viewblend[2];
616         varray_color[3] = varray_color[7] = varray_color[11] = r_refdef.viewblend[3];
617         r = 64000;
618         varray_vertex[0] = r_origin[0] + vpn[0] * 1.5 - vright[0] * r - vup[0] * r;
619         varray_vertex[1] = r_origin[1] + vpn[1] * 1.5 - vright[1] * r - vup[1] * r;
620         varray_vertex[2] = r_origin[2] + vpn[2] * 1.5 - vright[2] * r - vup[2] * r;
621         r *= 3;
622         varray_vertex[4] = varray_vertex[0] + vup[0] * r;
623         varray_vertex[5] = varray_vertex[1] + vup[1] * r;
624         varray_vertex[6] = varray_vertex[2] + vup[2] * r;
625         varray_vertex[8] = varray_vertex[0] + vright[0] * r;
626         varray_vertex[9] = varray_vertex[1] + vright[1] * r;
627         varray_vertex[10] = varray_vertex[2] + vright[2] * r;
628         R_Mesh_Draw(3, 1, polygonelements);
629 }
630
631 /*
632 ================
633 R_RenderView
634
635 r_refdef must be set before the first call
636 ================
637 */
638 void R_RenderView (void)
639 {
640         entity_render_t *world;
641         if (!r_refdef.entities/* || !cl.worldmodel*/)
642                 return; //Host_Error ("R_RenderView: NULL worldmodel");
643
644         world = &cl_entities[0].render;
645
646         // FIXME: move to client
647         R_MoveExplosions();
648         R_TimeReport("mexplosion");
649
650         R_Textures_Frame();
651         R_SetupFrame();
652         R_SetFrustum();
653         R_SetupFog();
654         R_SkyStartFrame();
655         R_BuildLightList();
656         R_TimeReport("setup");
657
658         R_FarClip_Start(r_origin, vpn, 768.0f);
659         R_MarkEntities();
660         r_farclip = R_FarClip_Finish() + 256.0f;
661         R_TimeReport("markentity");
662
663         GL_SetupView_ViewPort(r_refdef.x, r_refdef.y, r_refdef.width, r_refdef.height);
664         GL_SetupView_Mode_Perspective((double) r_refdef.height / r_refdef.width, r_refdef.fov_x, r_refdef.fov_y, 1.0f, r_farclip);
665         GL_SetupView_Orientation_FromEntity (r_refdef.vieworg, r_refdef.viewangles);
666         GL_DepthFunc(GL_LEQUAL);
667         
668         R_Mesh_Start();
669         R_MeshQueue_BeginScene();
670
671         if (R_DrawBrushModelsSky())
672                 R_TimeReport("bmodelsky");
673
674         // must occur early because it can draw sky
675         R_DrawWorld(world);
676         R_TimeReport("world");
677
678         // don't let sound skip if going slow
679         if (!intimerefresh && !r_speeds.integer)
680                 S_ExtraUpdate ();
681
682         R_DrawModels();
683         R_TimeReport("models");
684
685         R_DrawParticles();
686         R_TimeReport("particles");
687
688         R_DrawExplosions();
689         R_TimeReport("explosions");
690
691         R_MeshQueue_RenderTransparent();
692         R_TimeReport("drawtrans");
693
694         R_DrawCoronas();
695         R_TimeReport("coronas");
696
697         R_DrawCrosshair();
698         R_TimeReport("crosshair");
699
700         R_BlendView();
701         R_TimeReport("blendview");
702
703         R_MeshQueue_Render();
704         R_MeshQueue_EndScene();
705         R_Mesh_Finish();
706         R_TimeReport("meshfinish");
707 }
708
709 /*
710 void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
711 {
712         int i;
713         float *v, *c, f1, f2, diff[3];
714         rmeshstate_t m;
715         m.blendfunc1 = GL_SRC_ALPHA;
716         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
717         R_Mesh_Matrix(&r_identitymatrix);
718         R_Mesh_State(&m);
719
720         varray_vertex[ 0] = mins[0];varray_vertex[ 1] = mins[1];varray_vertex[ 2] = mins[2];
721         varray_vertex[ 4] = maxs[0];varray_vertex[ 5] = mins[1];varray_vertex[ 6] = mins[2];
722         varray_vertex[ 8] = mins[0];varray_vertex[ 9] = maxs[1];varray_vertex[10] = mins[2];
723         varray_vertex[12] = maxs[0];varray_vertex[13] = maxs[1];varray_vertex[14] = mins[2];
724         varray_vertex[16] = mins[0];varray_vertex[17] = mins[1];varray_vertex[18] = maxs[2];
725         varray_vertex[20] = maxs[0];varray_vertex[21] = mins[1];varray_vertex[22] = maxs[2];
726         varray_vertex[24] = mins[0];varray_vertex[25] = maxs[1];varray_vertex[26] = maxs[2];
727         varray_vertex[28] = maxs[0];varray_vertex[29] = maxs[1];varray_vertex[30] = maxs[2];
728         varray_color[ 0] = varray_color[ 4] = varray_color[ 8] = varray_color[12] = varray_color[16] = varray_color[20] = varray_color[24] = varray_color[28] = cr * r_colorscale;
729         varray_color[ 1] = varray_color[ 5] = varray_color[ 9] = varray_color[13] = varray_color[17] = varray_color[21] = varray_color[25] = varray_color[29] = cg * r_colorscale;
730         varray_color[ 2] = varray_color[ 6] = varray_color[10] = varray_color[14] = varray_color[18] = varray_color[22] = varray_color[26] = varray_color[30] = cb * r_colorscale;
731         varray_color[ 3] = varray_color[ 7] = varray_color[11] = varray_color[15] = varray_color[19] = varray_color[23] = varray_color[27] = varray_color[31] = ca;
732         if (fogenabled)
733         {
734                 for (i = 0, v = varray_vertex, c = varray_color;i < 8;i++, v += 4, c += 4)
735                 {
736                         VectorSubtract(v, r_origin, diff);
737                         f2 = exp(fogdensity/DotProduct(diff, diff));
738                         f1 = 1 - f2;
739                         f2 *= r_colorscale;
740                         c[0] = c[0] * f1 + fogcolor[0] * f2;
741                         c[1] = c[1] * f1 + fogcolor[1] * f2;
742                         c[2] = c[2] * f1 + fogcolor[2] * f2;
743                 }
744         }
745         R_Mesh_Draw(8, 12);
746 }
747 */
748
749 void R_DrawNoModelCallback(const void *calldata1, int calldata2)
750 {
751         const entity_render_t *ent = calldata1;
752         int i, element[24];
753         float f1, f2, *c, diff[3];
754         rmeshstate_t m;
755         memset(&m, 0, sizeof(m));
756         if (ent->flags & EF_ADDITIVE)
757         {
758                 m.blendfunc1 = GL_SRC_ALPHA;
759                 m.blendfunc2 = GL_ONE;
760         }
761         else if (ent->alpha < 1)
762         {
763                 m.blendfunc1 = GL_SRC_ALPHA;
764                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
765         }
766         else
767         {
768                 m.blendfunc1 = GL_ONE;
769                 m.blendfunc2 = GL_ZERO;
770         }
771         R_Mesh_Matrix(&ent->matrix);
772         R_Mesh_State(&m);
773
774         element[ 0] = 5;element[ 1] = 2;element[ 2] = 0;
775         element[ 3] = 5;element[ 4] = 1;element[ 5] = 2;
776         element[ 6] = 5;element[ 7] = 0;element[ 8] = 3;
777         element[ 9] = 5;element[10] = 3;element[11] = 1;
778         element[12] = 0;element[13] = 2;element[14] = 4;
779         element[15] = 2;element[16] = 1;element[17] = 4;
780         element[18] = 3;element[19] = 0;element[20] = 4;
781         element[21] = 1;element[22] = 3;element[23] = 4;
782         varray_vertex[ 0] = -16;varray_vertex[ 1] =   0;varray_vertex[ 2] =   0;
783         varray_vertex[ 4] =  16;varray_vertex[ 5] =   0;varray_vertex[ 6] =   0;
784         varray_vertex[ 8] =   0;varray_vertex[ 9] = -16;varray_vertex[10] =   0;
785         varray_vertex[12] =   0;varray_vertex[13] =  16;varray_vertex[14] =   0;
786         varray_vertex[16] =   0;varray_vertex[17] =   0;varray_vertex[18] = -16;
787         varray_vertex[20] =   0;varray_vertex[21] =   0;varray_vertex[22] =  16;
788         varray_color[ 0] = 0.00f;varray_color[ 1] = 0.00f;varray_color[ 2] = 0.50f;varray_color[ 3] = ent->alpha;
789         varray_color[ 4] = 0.00f;varray_color[ 5] = 0.00f;varray_color[ 6] = 0.50f;varray_color[ 7] = ent->alpha;
790         varray_color[ 8] = 0.00f;varray_color[ 9] = 0.50f;varray_color[10] = 0.00f;varray_color[11] = ent->alpha;
791         varray_color[12] = 0.00f;varray_color[13] = 0.50f;varray_color[14] = 0.00f;varray_color[15] = ent->alpha;
792         varray_color[16] = 0.50f;varray_color[17] = 0.00f;varray_color[18] = 0.00f;varray_color[19] = ent->alpha;
793         varray_color[20] = 0.50f;varray_color[21] = 0.00f;varray_color[22] = 0.00f;varray_color[23] = ent->alpha;
794         if (fogenabled)
795         {
796                 VectorSubtract(ent->origin, r_origin, diff);
797                 f2 = exp(fogdensity/DotProduct(diff, diff));
798                 f1 = 1 - f2;
799                 for (i = 0, c = varray_color;i < 6;i++, c += 4)
800                 {
801                         c[0] = (c[0] * f1 + fogcolor[0] * f2) * r_colorscale;
802                         c[1] = (c[1] * f1 + fogcolor[1] * f2) * r_colorscale;
803                         c[2] = (c[2] * f1 + fogcolor[2] * f2) * r_colorscale;
804                 }
805         }
806         else
807         {
808                 for (i = 0, c = varray_color;i < 6;i++, c += 4)
809                 {
810                         c[0] *= r_colorscale;
811                         c[1] *= r_colorscale;
812                         c[2] *= r_colorscale;
813                 }
814         }
815         R_Mesh_Draw(6, 8, element);
816 }
817
818 void R_DrawNoModel(entity_render_t *ent)
819 {
820         //if ((ent->effects & EF_ADDITIVE) || (ent->alpha < 1))
821                 R_MeshQueue_AddTransparent(ent->origin, R_DrawNoModelCallback, ent, 0);
822         //else
823         //      R_DrawNoModelCallback(ent, 0);
824 }
825
826 void R_CalcBeamVerts (float *vert, vec3_t org1, vec3_t org2, float width)
827 {
828         vec3_t right1, right2, diff, normal;
829
830         VectorSubtract (org2, org1, normal);
831         VectorNormalizeFast (normal);
832
833         // calculate 'right' vector for start
834         VectorSubtract (r_origin, org1, diff);
835         VectorNormalizeFast (diff);
836         CrossProduct (normal, diff, right1);
837
838         // calculate 'right' vector for end
839         VectorSubtract (r_origin, org2, diff);
840         VectorNormalizeFast (diff);
841         CrossProduct (normal, diff, right2);
842
843         vert[ 0] = org1[0] + width * right1[0];
844         vert[ 1] = org1[1] + width * right1[1];
845         vert[ 2] = org1[2] + width * right1[2];
846         vert[ 4] = org1[0] - width * right1[0];
847         vert[ 5] = org1[1] - width * right1[1];
848         vert[ 6] = org1[2] - width * right1[2];
849         vert[ 8] = org2[0] - width * right2[0];
850         vert[ 9] = org2[1] - width * right2[1];
851         vert[10] = org2[2] - width * right2[2];
852         vert[12] = org2[0] + width * right2[0];
853         vert[13] = org2[1] + width * right2[1];
854         vert[14] = org2[2] + width * right2[2];
855 }