removed support for r_ser (also known as R_Clip or "Hidden Surface Removal") as it...
[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 entity_render_t *currentrenderentity;
25
26 int                     r_framecount;           // used for dlight push checking
27
28 mplane_t        frustum[4];
29
30 int                     c_brush_polys, c_alias_polys, c_light_polys, c_faces, c_nodes, c_leafs, c_models, c_bmodels, c_sprites, c_particles, c_dlights;
31
32 qboolean        envmap;                         // true during envmap command capture
33
34 //
35 // view origin
36 //
37 vec3_t  vup;
38 vec3_t  vpn;
39 vec3_t  vright;
40 vec3_t  r_origin;
41
42 //
43 // screen size info
44 //
45 refdef_t        r_refdef;
46
47 mleaf_t         *r_viewleaf, *r_oldviewleaf;
48
49 unsigned short  d_lightstylevalue[256]; // 8.8 fraction of base light value
50
51 cvar_t  r_drawentities = {0, "r_drawentities","1"};
52 cvar_t  r_drawviewmodel = {0, "r_drawviewmodel","1"};
53 cvar_t  r_speeds = {0, "r_speeds","0"};
54 cvar_t  r_fullbright = {0, "r_fullbright","0"};
55 cvar_t  r_wateralpha = {CVAR_SAVE, "r_wateralpha","1"};
56 cvar_t  r_dynamic = {CVAR_SAVE, "r_dynamic","1"};
57 cvar_t  r_waterripple = {CVAR_SAVE, "r_waterripple","0"};
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_multitexture = {0, "r_multitexture", "1"};
69
70 /*
71 ====================
72 R_TimeRefresh_f
73
74 For program optimization
75 ====================
76 */
77 qboolean intimerefresh = 0;
78 static void R_TimeRefresh_f (void)
79 {
80         int                     i;
81         float           start, stop, time;
82
83         intimerefresh = 1;
84         start = Sys_DoubleTime ();
85         for (i = 0;i < 128;i++)
86         {
87                 r_refdef.viewangles[0] = 0;
88                 r_refdef.viewangles[1] = i/128.0*360.0;
89                 r_refdef.viewangles[2] = 0;
90                 CL_UpdateScreen();
91         }
92
93         stop = Sys_DoubleTime ();
94         intimerefresh = 0;
95         time = stop-start;
96         Con_Printf ("%f seconds (%f fps)\n", time, 128/time);
97 }
98
99 extern cvar_t r_drawportals;
100
101 int R_VisibleCullBox (vec3_t mins, vec3_t maxs)
102 {
103         int sides;
104         mnode_t *nodestack[8192], *node;
105         int stack = 0;
106
107         if (R_CullBox(mins, maxs))
108                 return true;
109
110         node = cl.worldmodel->nodes;
111 loc0:
112         if (node->contents < 0)
113         {
114                 if (((mleaf_t *)node)->visframe == r_framecount)
115                         return false;
116                 if (!stack)
117                         return true;
118                 node = nodestack[--stack];
119                 goto loc0;
120         }
121
122         sides = BOX_ON_PLANE_SIDE(mins, maxs, node->plane);
123
124 // recurse down the contacted sides
125         if (sides & 1)
126         {
127                 if (sides & 2) // 3
128                 {
129                         // put second child on the stack for later examination
130                         nodestack[stack++] = node->children[1];
131                         node = node->children[0];
132                         goto loc0;
133                 }
134                 else // 1
135                 {
136                         node = node->children[0];
137                         goto loc0;
138                 }
139         }
140         // 2
141         node = node->children[1];
142         goto loc0;
143 }
144
145 vec3_t fogcolor;
146 vec_t fogdensity;
147 float fog_density, fog_red, fog_green, fog_blue;
148 qboolean fogenabled;
149 qboolean oldgl_fogenable;
150 void R_SetupFog(void)
151 {
152         if (gamemode == GAME_NEHAHRA)
153         {
154                 if (gl_fogenable.integer)
155                 {
156                         oldgl_fogenable = true;
157                         fog_density = gl_fogdensity.value;
158                         fog_red = gl_fogred.value;
159                         fog_green = gl_foggreen.value;
160                         fog_blue = gl_fogblue.value;
161                 }
162                 else if (oldgl_fogenable)
163                 {
164                         oldgl_fogenable = false;
165                         fog_density = 0;
166                         fog_red = 0;
167                         fog_green = 0;
168                         fog_blue = 0;
169                 }
170         }
171         if (fog_density)
172         {
173                 fogcolor[0] = fog_red   = bound(0.0f, fog_red  , 1.0f);
174                 fogcolor[1] = fog_green = bound(0.0f, fog_green, 1.0f);
175                 fogcolor[2] = fog_blue  = bound(0.0f, fog_blue , 1.0f);
176         }
177         if (fog_density)
178         {
179                 fogenabled = true;
180                 fogdensity = -4000.0f / (fog_density * fog_density);
181                 // fog color was already set
182         }
183         else
184                 fogenabled = false;
185 }
186
187 // FIXME: move this to client?
188 void FOG_clear(void)
189 {
190         if (gamemode == GAME_NEHAHRA)
191         {
192                 Cvar_Set("gl_fogenable", "0");
193                 Cvar_Set("gl_fogdensity", "0.2");
194                 Cvar_Set("gl_fogred", "0.3");
195                 Cvar_Set("gl_foggreen", "0.3");
196                 Cvar_Set("gl_fogblue", "0.3");
197         }
198         fog_density = fog_red = fog_green = fog_blue = 0.0f;
199 }
200
201 // FIXME: move this to client?
202 void FOG_registercvars(void)
203 {
204         if (gamemode == GAME_NEHAHRA)
205         {
206                 Cvar_RegisterVariable (&gl_fogenable);
207                 Cvar_RegisterVariable (&gl_fogdensity);
208                 Cvar_RegisterVariable (&gl_fogred);
209                 Cvar_RegisterVariable (&gl_foggreen);
210                 Cvar_RegisterVariable (&gl_fogblue);
211                 Cvar_RegisterVariable (&gl_fogstart);
212                 Cvar_RegisterVariable (&gl_fogend);
213         }
214 }
215
216 void gl_main_start(void)
217 {
218 }
219
220 void gl_main_shutdown(void)
221 {
222 }
223
224 void gl_main_newmap(void)
225 {
226         r_framecount = 1;
227 }
228
229 void GL_Main_Init(void)
230 {
231 // FIXME: move this to client?
232         FOG_registercvars();
233         Cmd_AddCommand ("timerefresh", R_TimeRefresh_f);
234         Cvar_RegisterVariable (&r_drawentities);
235         Cvar_RegisterVariable (&r_drawviewmodel);
236         Cvar_RegisterVariable (&r_speeds);
237         Cvar_RegisterVariable (&r_fullbrights);
238         Cvar_RegisterVariable (&r_wateralpha);
239         Cvar_RegisterVariable (&r_dynamic);
240         Cvar_RegisterVariable (&r_waterripple);
241         Cvar_RegisterVariable (&r_fullbright);
242         Cvar_RegisterVariable (&r_multitexture);
243         if (gamemode == GAME_NEHAHRA)
244                 Cvar_SetValue("r_fullbrights", 0);
245         R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
246 }
247
248 /*
249 ===============
250 R_NewMap
251 ===============
252 */
253 void CL_ParseEntityLump(char *entitystring);
254 void R_NewMap (void)
255 {
256         int             i;
257
258         for (i=0 ; i<256 ; i++)
259                 d_lightstylevalue[i] = 264;             // normal light value
260
261         r_viewleaf = NULL;
262         if (cl.worldmodel->entities)
263                 CL_ParseEntityLump(cl.worldmodel->entities);
264         R_Modules_NewMap();
265 }
266
267 extern void R_Textures_Init(void);
268 extern void Mod_RenderInit(void);
269 extern void GL_Draw_Init(void);
270 extern void GL_Main_Init(void);
271 extern void GL_Models_Init(void);
272 extern void R_Sky_Init(void);
273 extern void GL_Surf_Init(void);
274 extern void R_Crosshairs_Init(void);
275 extern void R_Light_Init(void);
276 extern void R_Particles_Init(void);
277 extern void R_Explosion_Init(void);
278 extern void ui_init(void);
279 extern void gl_backend_init(void);
280
281 void Render_Init(void)
282 {
283         R_Modules_Shutdown();
284         R_Textures_Init();
285         Mod_RenderInit();
286         gl_backend_init();
287         GL_Draw_Init();
288         GL_Main_Init();
289         GL_Models_Init();
290         R_Sky_Init();
291         GL_Surf_Init();
292         R_Crosshairs_Init();
293         R_Light_Init();
294         R_Particles_Init();
295         R_Explosion_Init();
296         ui_init();
297         R_Modules_Start();
298 }
299
300 /*
301 ===============
302 GL_Init
303 ===============
304 */
305 extern char *ENGINE_EXTENSIONS;
306 void GL_Init (void)
307 {
308         VID_CheckExtensions();
309
310         // LordHavoc: report supported extensions
311         Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
312
313         qglCullFace(GL_FRONT);
314         qglEnable(GL_TEXTURE_2D);
315 }
316
317
318 //==================================================================================
319
320 void R_Entity_Callback(void *data, void *junk)
321 {
322         ((entity_render_t *)data)->visframe = r_framecount;
323 }
324
325 static void R_MarkEntities (void)
326 {
327         int             i;
328         vec3_t  v;
329
330         if (!r_drawentities.integer)
331                 return;
332
333         for (i = 0;i < r_refdef.numentities;i++)
334         {
335                 currentrenderentity = r_refdef.entities[i];
336                 Mod_CheckLoaded(currentrenderentity->model);
337
338                 // move view-relative models to where they should be
339                 if (currentrenderentity->flags & RENDER_VIEWMODEL)
340                 {
341                         // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
342                         currentrenderentity->flags -= RENDER_VIEWMODEL;
343                         // transform origin
344                         VectorCopy(currentrenderentity->origin, v);
345                         currentrenderentity->origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
346                         currentrenderentity->origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
347                         currentrenderentity->origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
348                         // adjust angles
349                         VectorAdd(currentrenderentity->angles, r_refdef.viewangles, currentrenderentity->angles);
350                 }
351
352                 if (currentrenderentity->angles[0] || currentrenderentity->angles[2])
353                 {
354                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->rotatedmins, currentrenderentity->mins);
355                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->rotatedmaxs, currentrenderentity->maxs);
356                 }
357                 else if (currentrenderentity->angles[1])
358                 {
359                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->yawmins, currentrenderentity->mins);
360                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->yawmaxs, currentrenderentity->maxs);
361                 }
362                 else
363                 {
364                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->normalmins, currentrenderentity->mins);
365                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->normalmaxs, currentrenderentity->maxs);
366                 }
367                 if (R_VisibleCullBox(currentrenderentity->mins, currentrenderentity->maxs))
368                         continue;
369
370                 R_LerpAnimation(currentrenderentity);
371                 currentrenderentity->visframe = r_framecount;
372         }
373 }
374
375 // only used if skyrendermasked, and normally returns false
376 int R_DrawBModelSky (void)
377 {
378         int             i, sky = false;
379
380         if (!r_drawentities.integer)
381                 return false;
382
383         for (i = 0;i < r_refdef.numentities;i++)
384         {
385                 currentrenderentity = r_refdef.entities[i];
386                 if (currentrenderentity->visframe == r_framecount && currentrenderentity->model->DrawSky)
387                 {
388                         currentrenderentity->model->DrawSky();
389                         sky = true;
390                 }
391         }
392         return sky;
393 }
394
395 void R_DrawModels (void)
396 {
397         int             i;
398
399         if (!r_drawentities.integer)
400                 return;
401
402         for (i = 0;i < r_refdef.numentities;i++)
403         {
404                 currentrenderentity = r_refdef.entities[i];
405                 if (currentrenderentity->visframe == r_framecount && currentrenderentity->model->Draw)
406                         currentrenderentity->model->Draw();
407         }
408 }
409
410 /*
411 =============
412 R_DrawViewModel
413 =============
414 */
415 void R_DrawViewModel (void)
416 {
417         // FIXME: move these checks to client
418         if (!r_drawviewmodel.integer || chase_active.integer || envmap || !r_drawentities.integer || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
419                 return;
420
421         currentrenderentity = &cl.viewent.render;
422         Mod_CheckLoaded(currentrenderentity->model);
423
424         R_LerpAnimation(currentrenderentity);
425
426         currentrenderentity->model->Draw();
427 }
428
429 static void R_SetFrustum (void)
430 {
431         int             i;
432
433         // LordHavoc: note to all quake engine coders, the special case for 90
434         // degrees assumed a square view (wrong), so I removed it, Quake2 has it
435         // disabled as well.
436         // rotate VPN right by FOV_X/2 degrees
437         RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
438         // rotate VPN left by FOV_X/2 degrees
439         RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
440         // rotate VPN up by FOV_X/2 degrees
441         RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
442         // rotate VPN down by FOV_X/2 degrees
443         RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
444
445
446         for (i=0 ; i<4 ; i++)
447         {
448                 frustum[i].type = PLANE_ANYZ;
449                 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
450                 PlaneClassify(&frustum[i]);
451         }
452 }
453
454 /*
455 ===============
456 R_SetupFrame
457 ===============
458 */
459 static void R_SetupFrame (void)
460 {
461 // don't allow cheats in multiplayer
462         if (cl.maxclients > 1)
463         {
464                 if (r_fullbright.integer != 0)
465                         Cvar_Set ("r_fullbright", "0");
466                 if (r_ambient.value != 0)
467                         Cvar_Set ("r_ambient", "0");
468         }
469         if (r_multitexture.integer && gl_textureunits < 2)
470                 Cvar_SetValue("r_multitexture", 0);
471
472         r_framecount++;
473
474 // build the transformation matrix for the given view angles
475         VectorCopy (r_refdef.vieworg, r_origin);
476
477         AngleVectors (r_refdef.viewangles, vpn, vright, vup);
478
479 // current viewleaf
480         r_oldviewleaf = r_viewleaf;
481         r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
482
483         R_AnimateLight ();
484 }
485
486
487 static int blendviewpolyindex[3] = {0, 1, 2};
488
489 static void R_BlendView(void)
490 {
491         rmeshinfo_t m;
492         float tvxyz[3][4], r;
493
494         if (r_refdef.viewblend[3] < 0.01f)
495                 return;
496
497         memset(&m, 0, sizeof(m));
498         m.transparent = false;
499         m.blendfunc1 = GL_SRC_ALPHA;
500         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
501         m.depthdisable = true; // magic
502         m.numtriangles = 1;
503         m.numverts = 3;
504         m.index = blendviewpolyindex;
505         m.vertex = &tvxyz[0][0];
506         m.vertexstep = sizeof(float[4]);
507         m.cr = r_refdef.viewblend[0];
508         m.cg = r_refdef.viewblend[1];
509         m.cb = r_refdef.viewblend[2];
510         m.ca = r_refdef.viewblend[3];
511         r = 64000;
512         tvxyz[0][0] = r_origin[0] + vpn[0] * 1.5 - vright[0] * r - vup[0] * r;
513         tvxyz[0][1] = r_origin[1] + vpn[1] * 1.5 - vright[1] * r - vup[1] * r;
514         tvxyz[0][2] = r_origin[2] + vpn[2] * 1.5 - vright[2] * r - vup[2] * r;
515         r *= 3;
516         tvxyz[1][0] = tvxyz[0][0] + vup[0] * r;
517         tvxyz[1][1] = tvxyz[0][1] + vup[1] * r;
518         tvxyz[1][2] = tvxyz[0][2] + vup[2] * r;
519         tvxyz[2][0] = tvxyz[0][0] + vright[0] * r;
520         tvxyz[2][1] = tvxyz[0][1] + vright[1] * r;
521         tvxyz[2][2] = tvxyz[0][2] + vright[2] * r;
522         R_Mesh_Draw(&m);
523 }
524
525 /*
526 ================
527 R_RenderView
528
529 r_refdef must be set before the first call
530 ================
531 */
532 void R_RenderView (void)
533 {
534         if (!cl.worldmodel)
535                 return; //Host_Error ("R_RenderView: NULL worldmodel");
536
537         // FIXME: move to client
538         R_MoveExplosions();
539         R_TimeReport("mexplosion");
540
541         R_SetupFrame();
542         R_SetFrustum();
543         R_SetupFog();
544         R_SkyStartFrame();
545         R_BuildLightList();
546
547         R_Mesh_Start();
548
549         R_TimeReport("setup");
550
551         R_DrawWorld();
552         R_TimeReport("worldnode");
553
554         R_MarkEntities();
555         R_TimeReport("markentity");
556
557         R_MarkWorldLights();
558         R_TimeReport("marklights");
559
560         if (skyrendermasked)
561         {
562                 if (R_DrawBModelSky())
563                         R_TimeReport("bmodelsky");
564         }
565         else
566         {
567                 R_DrawViewModel();
568                 R_TimeReport("viewmodel");
569         }
570
571         R_SetupForWorldRendering();
572         R_PrepareSurfaces();
573         R_TimeReport("surfprep");
574
575         R_DrawSurfaces(SHADERSTAGE_SKY);
576         R_DrawSurfaces(SHADERSTAGE_NORMAL);
577         R_TimeReport("surfdraw");
578
579         if (r_drawportals.integer)
580         {
581                 R_DrawPortals();
582                 R_TimeReport("portals");
583         }
584
585         // don't let sound skip if going slow
586         if (!intimerefresh && !r_speeds.integer)
587                 S_ExtraUpdate ();
588
589         if (skyrendermasked)
590         {
591                 R_DrawViewModel();
592                 R_TimeReport("viewmodel");
593         }
594
595         R_DrawModels();
596         R_TimeReport("models");
597
598         R_DrawParticles();
599         R_TimeReport("particles");
600
601         R_DrawExplosions();
602         R_TimeReport("explosions");
603
604         // draw transparent meshs
605         R_Mesh_AddTransparent();
606         R_TimeReport("addtrans");
607
608         R_DrawCoronas();
609         R_TimeReport("coronas");
610
611         R_BlendView();
612         R_TimeReport("blendview");
613
614         R_DrawCrosshair();
615         R_TimeReport("crosshair");
616
617         // render any queued meshs
618         R_Mesh_Finish();
619         R_TimeReport("meshfinish");
620 }
621