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