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