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