free texture processing buffers each frame so they don't hang around wasting memory
[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 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;
30
31 // true during envmap command capture
32 qboolean envmap;
33
34 float r_farclip;
35
36 // view origin
37 vec3_t r_origin;
38 vec3_t vpn;
39 vec3_t vright;
40 vec3_t vup;
41
42 //
43 // screen size info
44 //
45 refdef_t r_refdef;
46
47 mleaf_t *r_viewleaf, *r_oldviewleaf;
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 extern cvar_t r_drawportals;
127
128 int R_VisibleCullBox (vec3_t mins, vec3_t maxs)
129 {
130         int sides;
131         mnode_t *nodestack[8192], *node;
132         int stack = 0;
133
134         if (R_CullBox(mins, maxs))
135                 return true;
136
137         node = cl.worldmodel->nodes;
138 loc0:
139         if (node->contents < 0)
140         {
141                 if (((mleaf_t *)node)->visframe == r_framecount)
142                         return false;
143                 if (!stack)
144                         return true;
145                 node = nodestack[--stack];
146                 goto loc0;
147         }
148
149         sides = BOX_ON_PLANE_SIDE(mins, maxs, node->plane);
150
151 // recurse down the contacted sides
152         if (sides & 1)
153         {
154                 if (sides & 2) // 3
155                 {
156                         // put second child on the stack for later examination
157                         nodestack[stack++] = node->children[1];
158                         node = node->children[0];
159                         goto loc0;
160                 }
161                 else // 1
162                 {
163                         node = node->children[0];
164                         goto loc0;
165                 }
166         }
167         // 2
168         node = node->children[1];
169         goto loc0;
170 }
171
172 vec3_t fogcolor;
173 vec_t fogdensity;
174 float fog_density, fog_red, fog_green, fog_blue;
175 qboolean fogenabled;
176 qboolean oldgl_fogenable;
177 void R_SetupFog(void)
178 {
179         if (gamemode == GAME_NEHAHRA)
180         {
181                 if (gl_fogenable.integer)
182                 {
183                         oldgl_fogenable = true;
184                         fog_density = gl_fogdensity.value;
185                         fog_red = gl_fogred.value;
186                         fog_green = gl_foggreen.value;
187                         fog_blue = gl_fogblue.value;
188                 }
189                 else if (oldgl_fogenable)
190                 {
191                         oldgl_fogenable = false;
192                         fog_density = 0;
193                         fog_red = 0;
194                         fog_green = 0;
195                         fog_blue = 0;
196                 }
197         }
198         if (fog_density)
199         {
200                 fogcolor[0] = fog_red   = bound(0.0f, fog_red  , 1.0f);
201                 fogcolor[1] = fog_green = bound(0.0f, fog_green, 1.0f);
202                 fogcolor[2] = fog_blue  = bound(0.0f, fog_blue , 1.0f);
203         }
204         if (fog_density)
205         {
206                 fogenabled = true;
207                 fogdensity = -4000.0f / (fog_density * fog_density);
208                 // fog color was already set
209         }
210         else
211                 fogenabled = false;
212 }
213
214 // FIXME: move this to client?
215 void FOG_clear(void)
216 {
217         if (gamemode == GAME_NEHAHRA)
218         {
219                 Cvar_Set("gl_fogenable", "0");
220                 Cvar_Set("gl_fogdensity", "0.2");
221                 Cvar_Set("gl_fogred", "0.3");
222                 Cvar_Set("gl_foggreen", "0.3");
223                 Cvar_Set("gl_fogblue", "0.3");
224         }
225         fog_density = fog_red = fog_green = fog_blue = 0.0f;
226 }
227
228 // FIXME: move this to client?
229 void FOG_registercvars(void)
230 {
231         if (gamemode == GAME_NEHAHRA)
232         {
233                 Cvar_RegisterVariable (&gl_fogenable);
234                 Cvar_RegisterVariable (&gl_fogdensity);
235                 Cvar_RegisterVariable (&gl_fogred);
236                 Cvar_RegisterVariable (&gl_foggreen);
237                 Cvar_RegisterVariable (&gl_fogblue);
238                 Cvar_RegisterVariable (&gl_fogstart);
239                 Cvar_RegisterVariable (&gl_fogend);
240         }
241 }
242
243 void gl_main_start(void)
244 {
245 }
246
247 void gl_main_shutdown(void)
248 {
249 }
250
251 void gl_main_newmap(void)
252 {
253         r_framecount = 1;
254 }
255
256 void GL_Main_Init(void)
257 {
258 // FIXME: move this to client?
259         FOG_registercvars();
260         Cmd_AddCommand ("timerefresh", R_TimeRefresh_f);
261         Cvar_RegisterVariable (&r_drawentities);
262         Cvar_RegisterVariable (&r_drawviewmodel);
263         Cvar_RegisterVariable (&r_speeds);
264         Cvar_RegisterVariable (&r_fullbrights);
265         Cvar_RegisterVariable (&r_wateralpha);
266         Cvar_RegisterVariable (&r_dynamic);
267         Cvar_RegisterVariable (&r_fullbright);
268         Cvar_RegisterVariable (&r_textureunits);
269         if (gamemode == GAME_NEHAHRA)
270                 Cvar_SetValue("r_fullbrights", 0);
271         R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
272 }
273
274 vec3_t r_farclip_origin;
275 vec3_t r_farclip_direction;
276 vec_t r_farclip_directiondist;
277 vec_t r_farclip_meshfarclip;
278 int r_farclip_directionbit0;
279 int r_farclip_directionbit1;
280 int r_farclip_directionbit2;
281
282 // start a farclip measuring session
283 void R_FarClip_Start(vec3_t origin, vec3_t direction, vec_t startfarclip)
284 {
285         VectorCopy(origin, r_farclip_origin);
286         VectorCopy(direction, r_farclip_direction);
287         r_farclip_directiondist = DotProduct(r_farclip_origin, r_farclip_direction);
288         r_farclip_directionbit0 = r_farclip_direction[0] < 0;
289         r_farclip_directionbit1 = r_farclip_direction[1] < 0;
290         r_farclip_directionbit2 = r_farclip_direction[2] < 0;
291         r_farclip_meshfarclip = r_farclip_directiondist + startfarclip;
292 }
293
294 // enlarge farclip to accomodate box
295 void R_FarClip_Box(vec3_t mins, vec3_t maxs)
296 {
297         float d;
298         d = (r_farclip_directionbit0 ? mins[0] : maxs[0]) * r_farclip_direction[0]
299           + (r_farclip_directionbit1 ? mins[1] : maxs[1]) * r_farclip_direction[1]
300           + (r_farclip_directionbit2 ? mins[2] : maxs[2]) * r_farclip_direction[2];
301         if (r_farclip_meshfarclip < d)
302                 r_farclip_meshfarclip = d;
303 }
304
305 // return farclip value
306 float R_FarClip_Finish(void)
307 {
308         return r_farclip_meshfarclip - r_farclip_directiondist;
309 }
310
311 /*
312 ===============
313 R_NewMap
314 ===============
315 */
316 void CL_ParseEntityLump(char *entitystring);
317 void R_NewMap (void)
318 {
319         int i;
320
321         for (i = 0;i < 256;i++)
322                 d_lightstylevalue[i] = 264;             // normal light value
323
324         r_viewleaf = NULL;
325         if (cl.worldmodel->entities)
326                 CL_ParseEntityLump(cl.worldmodel->entities);
327         R_Modules_NewMap();
328
329         r_farclip = 64.0f;
330 }
331
332 extern void R_Textures_Init(void);
333 extern void Mod_RenderInit(void);
334 extern void GL_Draw_Init(void);
335 extern void GL_Main_Init(void);
336 extern void GL_Models_Init(void);
337 extern void R_Sky_Init(void);
338 extern void GL_Surf_Init(void);
339 extern void R_Crosshairs_Init(void);
340 extern void R_Light_Init(void);
341 extern void R_Particles_Init(void);
342 extern void R_Explosion_Init(void);
343 extern void ui_init(void);
344 extern void gl_backend_init(void);
345
346 void Render_Init(void)
347 {
348         R_Modules_Shutdown();
349         R_Textures_Init();
350         Mod_RenderInit();
351         gl_backend_init();
352         R_MeshQueue_Init();
353         GL_Draw_Init();
354         GL_Main_Init();
355         GL_Models_Init();
356         R_Sky_Init();
357         GL_Surf_Init();
358         R_Crosshairs_Init();
359         R_Light_Init();
360         R_Particles_Init();
361         R_Explosion_Init();
362         ui_init();
363         R_Modules_Start();
364 }
365
366 /*
367 ===============
368 GL_Init
369 ===============
370 */
371 extern char *ENGINE_EXTENSIONS;
372 void GL_Init (void)
373 {
374         VID_CheckExtensions();
375
376         // LordHavoc: report supported extensions
377         Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
378
379         qglCullFace(GL_FRONT);
380         qglEnable(GL_TEXTURE_2D);
381 }
382
383
384 //==================================================================================
385
386 static void R_MarkEntities (void)
387 {
388         int i;
389         vec3_t v;
390         entity_render_t *ent;
391
392         ent = &cl_entities[0].render;
393         Matrix4x4_CreateIdentity(&ent->matrix);
394         Matrix4x4_CreateIdentity(&ent->inversematrix);
395
396         R_FarClip_Box(cl.worldmodel->normalmins, cl.worldmodel->normalmaxs);
397
398         if (!r_drawentities.integer)
399                 return;
400
401         for (i = 0;i < r_refdef.numentities;i++)
402         {
403                 ent = r_refdef.entities[i];
404                 Mod_CheckLoaded(ent->model);
405
406                 // move view-relative models to where they should be
407                 if (ent->flags & RENDER_VIEWMODEL)
408                 {
409                         // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
410                         ent->flags -= RENDER_VIEWMODEL;
411                         // transform origin
412                         VectorCopy(ent->origin, v);
413                         ent->origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
414                         ent->origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
415                         ent->origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
416                         // adjust angles
417                         VectorAdd(ent->angles, r_refdef.viewangles, ent->angles);
418                 }
419
420                 if (ent->angles[0] || ent->angles[2])
421                 {
422                         VectorMA(ent->origin, ent->scale, ent->model->rotatedmins, ent->mins);
423                         VectorMA(ent->origin, ent->scale, ent->model->rotatedmaxs, ent->maxs);
424                 }
425                 else if (ent->angles[1])
426                 {
427                         VectorMA(ent->origin, ent->scale, ent->model->yawmins, ent->mins);
428                         VectorMA(ent->origin, ent->scale, ent->model->yawmaxs, ent->maxs);
429                 }
430                 else
431                 {
432                         VectorMA(ent->origin, ent->scale, ent->model->normalmins, ent->mins);
433                         VectorMA(ent->origin, ent->scale, ent->model->normalmaxs, ent->maxs);
434                 }
435                 if (R_VisibleCullBox(ent->mins, ent->maxs))
436                         continue;
437
438                 VectorCopy(ent->angles, v);
439                 if (ent->model->type != mod_brush)
440                         v[0] = -v[0];
441                 Matrix4x4_CreateFromQuakeEntity(&ent->matrix, ent->origin[0], ent->origin[1], ent->origin[2], v[0], v[1], v[2], ent->scale);
442                 Matrix4x4_Invert_Simple(&ent->inversematrix, &ent->matrix);
443                 R_LerpAnimation(ent);
444                 ent->visframe = r_framecount;
445
446                 R_FarClip_Box(ent->mins, ent->maxs);
447
448                 R_UpdateEntLights(ent);
449         }
450 }
451
452 // only used if skyrendermasked, and normally returns false
453 int R_DrawBModelSky (void)
454 {
455         int i, sky;
456         entity_render_t *ent;
457
458         if (!r_drawentities.integer)
459                 return false;
460
461         sky = false;
462         for (i = 0;i < r_refdef.numentities;i++)
463         {
464                 ent = r_refdef.entities[i];
465                 if (ent->visframe == r_framecount && ent->model->DrawSky)
466                 {
467                         ent->model->DrawSky(ent);
468                         sky = true;
469                 }
470         }
471         return sky;
472 }
473
474 void R_DrawModels (void)
475 {
476         int i;
477         entity_render_t *ent;
478
479         if (!r_drawentities.integer)
480                 return;
481
482         for (i = 0;i < r_refdef.numentities;i++)
483         {
484                 ent = r_refdef.entities[i];
485                 if (ent->visframe == r_framecount && ent->model->Draw)
486                         ent->model->Draw(ent);
487         }
488 }
489
490 /*
491 =============
492 R_DrawViewModel
493 =============
494 */
495 void R_DrawViewModel (void)
496 {
497         entity_render_t *ent;
498
499         // FIXME: move these checks to client
500         if (!r_drawviewmodel.integer || chase_active.integer || envmap || !r_drawentities.integer || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
501                 return;
502
503         ent = &cl.viewent.render;
504         Mod_CheckLoaded(ent->model);
505         R_LerpAnimation(ent);
506         Matrix4x4_CreateFromQuakeEntity(&ent->matrix, ent->origin[0], ent->origin[1], ent->origin[2], -ent->angles[0], ent->angles[1], ent->angles[2], ent->scale);
507         Matrix4x4_Invert_Simple(&ent->inversematrix, &ent->matrix);
508         R_UpdateEntLights(ent);
509         ent->model->Draw(ent);
510 }
511
512 static void R_SetFrustum (void)
513 {
514         int i;
515
516         // LordHavoc: note to all quake engine coders, the special case for 90
517         // degrees assumed a square view (wrong), so I removed it, Quake2 has it
518         // disabled as well.
519         // rotate VPN right by FOV_X/2 degrees
520         RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
521         // rotate VPN left by FOV_X/2 degrees
522         RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
523         // rotate VPN up by FOV_X/2 degrees
524         RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
525         // rotate VPN down by FOV_X/2 degrees
526         RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
527
528         for (i = 0;i < 4;i++)
529         {
530                 frustum[i].type = PLANE_ANYZ;
531                 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
532                 PlaneClassify(&frustum[i]);
533         }
534 }
535
536 /*
537 ===============
538 R_SetupFrame
539 ===============
540 */
541 static void R_SetupFrame (void)
542 {
543 // don't allow cheats in multiplayer
544         if (cl.maxclients > 1)
545         {
546                 if (r_fullbright.integer != 0)
547                         Cvar_Set ("r_fullbright", "0");
548                 if (r_ambient.value != 0)
549                         Cvar_Set ("r_ambient", "0");
550         }
551
552         r_framecount++;
553
554 // build the transformation matrix for the given view angles
555         VectorCopy (r_refdef.vieworg, r_origin);
556
557         AngleVectors (r_refdef.viewangles, vpn, vright, vup);
558
559 // current viewleaf
560         r_oldviewleaf = r_viewleaf;
561         r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
562
563         R_AnimateLight ();
564 }
565
566
567 static void R_BlendView(void)
568 {
569         rmeshbufferinfo_t m;
570         float r;
571
572         if (r_refdef.viewblend[3] < 0.01f)
573                 return;
574
575         memset(&m, 0, sizeof(m));
576         m.blendfunc1 = GL_SRC_ALPHA;
577         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
578         m.depthdisable = true; // magic
579         m.numtriangles = 1;
580         m.numverts = 3;
581         Matrix4x4_CreateIdentity(&m.matrix);
582         if (R_Mesh_Draw_GetBuffer(&m, false))
583         {
584                 m.index[0] = 0;
585                 m.index[1] = 1;
586                 m.index[2] = 2;
587                 m.color[0] = m.color[4] = m.color[8] = r_refdef.viewblend[0];
588                 m.color[1] = m.color[5] = m.color[9] = r_refdef.viewblend[1];
589                 m.color[2] = m.color[6] = m.color[10] = r_refdef.viewblend[2];
590                 m.color[3] = m.color[7] = m.color[11] = r_refdef.viewblend[3];
591                 r = 64000;
592                 m.vertex[0] = r_origin[0] + vpn[0] * 1.5 - vright[0] * r - vup[0] * r;
593                 m.vertex[1] = r_origin[1] + vpn[1] * 1.5 - vright[1] * r - vup[1] * r;
594                 m.vertex[2] = r_origin[2] + vpn[2] * 1.5 - vright[2] * r - vup[2] * r;
595                 r *= 3;
596                 m.vertex[4] = m.vertex[0] + vup[0] * r;
597                 m.vertex[5] = m.vertex[1] + vup[1] * r;
598                 m.vertex[6] = m.vertex[2] + vup[2] * r;
599                 m.vertex[8] = m.vertex[0] + vright[0] * r;
600                 m.vertex[9] = m.vertex[1] + vright[1] * r;
601                 m.vertex[10] = m.vertex[2] + vright[2] * r;
602                 R_Mesh_Render();
603         }
604 }
605
606 /*
607 ================
608 R_RenderView
609
610 r_refdef must be set before the first call
611 ================
612 */
613 void R_RenderView (void)
614 {
615         entity_render_t *world = &cl_entities[0].render;
616         if (!cl.worldmodel)
617                 return; //Host_Error ("R_RenderView: NULL worldmodel");
618
619         // FIXME: move to client
620         R_MoveExplosions();
621         R_TimeReport("mexplosion");
622
623         R_Textures_Frame();
624
625         R_SetupFrame();
626         R_SetFrustum();
627         R_SetupFog();
628         R_SkyStartFrame();
629         R_BuildLightList();
630
631         R_FarClip_Start(r_origin, vpn, 768.0f);
632
633         R_TimeReport("setup");
634
635         R_DrawWorld(world);
636         R_TimeReport("worldnode");
637
638         R_MarkEntities();
639         R_TimeReport("markentity");
640
641         R_SurfMarkLights(world);
642         R_TimeReport("marklights");
643
644         r_farclip = R_FarClip_Finish() + 256.0f;
645
646         R_Mesh_Start(r_farclip);
647
648         R_MeshQueue_BeginScene();
649
650
651         if (skyrendermasked)
652         {
653                 if (R_DrawBModelSky())
654                         R_TimeReport("bmodelsky");
655         }
656         else
657         {
658                 R_DrawViewModel();
659                 R_TimeReport("viewmodel");
660         }
661
662         R_PrepareSurfaces(world);
663         R_TimeReport("surfprep");
664
665         R_DrawSurfaces(world, SHADERSTAGE_SKY);
666         R_DrawSurfaces(world, SHADERSTAGE_NORMAL);
667         R_TimeReport("surfdraw");
668
669         if (r_drawportals.integer)
670         {
671                 R_DrawPortals(world);
672                 R_TimeReport("portals");
673         }
674
675         // don't let sound skip if going slow
676         if (!intimerefresh && !r_speeds.integer)
677                 S_ExtraUpdate ();
678
679         if (skyrendermasked)
680         {
681                 R_DrawViewModel();
682                 R_TimeReport("viewmodel");
683         }
684
685         R_DrawModels();
686         R_TimeReport("models");
687
688         R_DrawParticles();
689         R_TimeReport("particles");
690
691         R_DrawExplosions();
692         R_TimeReport("explosions");
693
694         R_MeshQueue_RenderTransparent();
695         R_TimeReport("addtrans");
696
697         R_DrawCoronas();
698         R_TimeReport("coronas");
699
700         R_DrawCrosshair();
701         R_TimeReport("crosshair");
702
703         R_BlendView();
704         R_TimeReport("blendview");
705
706         R_MeshQueue_Render();
707
708         R_MeshQueue_EndScene();
709
710         R_Mesh_Finish();
711         R_TimeReport("meshfinish");
712 }
713