]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_rmain.c
engine mostly converted to use R_MeshQueue functions instead of true transparent...
[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 // 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         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                 ent = r_refdef.entities[i];
400                 Mod_CheckLoaded(ent->model);
401
402                 // move view-relative models to where they should be
403                 if (ent->flags & RENDER_VIEWMODEL)
404                 {
405                         // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
406                         ent->flags -= RENDER_VIEWMODEL;
407                         // transform origin
408                         VectorCopy(ent->origin, v);
409                         ent->origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
410                         ent->origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
411                         ent->origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
412                         // adjust angles
413                         VectorAdd(ent->angles, r_refdef.viewangles, ent->angles);
414                 }
415
416                 if (ent->angles[0] || ent->angles[2])
417                 {
418                         VectorMA(ent->origin, ent->scale, ent->model->rotatedmins, ent->mins);
419                         VectorMA(ent->origin, ent->scale, ent->model->rotatedmaxs, ent->maxs);
420                 }
421                 else if (ent->angles[1])
422                 {
423                         VectorMA(ent->origin, ent->scale, ent->model->yawmins, ent->mins);
424                         VectorMA(ent->origin, ent->scale, ent->model->yawmaxs, ent->maxs);
425                 }
426                 else
427                 {
428                         VectorMA(ent->origin, ent->scale, ent->model->normalmins, ent->mins);
429                         VectorMA(ent->origin, ent->scale, ent->model->normalmaxs, ent->maxs);
430                 }
431                 if (R_VisibleCullBox(ent->mins, ent->maxs))
432                         continue;
433
434                 R_LerpAnimation(ent);
435                 ent->visframe = r_framecount;
436
437                 R_FarClip_Box(ent->mins, ent->maxs);
438         }
439 }
440
441 // only used if skyrendermasked, and normally returns false
442 int R_DrawBModelSky (void)
443 {
444         int i, sky;
445         entity_render_t *ent;
446
447         if (!r_drawentities.integer)
448                 return false;
449
450         sky = false;
451         for (i = 0;i < r_refdef.numentities;i++)
452         {
453                 ent = r_refdef.entities[i];
454                 if (ent->visframe == r_framecount && ent->model->DrawSky)
455                 {
456                         ent->model->DrawSky(ent);
457                         sky = true;
458                 }
459         }
460         return sky;
461 }
462
463 void R_DrawModels (void)
464 {
465         int i;
466         entity_render_t *ent;
467
468         if (!r_drawentities.integer)
469                 return;
470
471         for (i = 0;i < r_refdef.numentities;i++)
472         {
473                 ent = r_refdef.entities[i];
474                 if (ent->visframe == r_framecount && ent->model->Draw)
475                         ent->model->Draw(ent);
476         }
477 }
478
479 /*
480 =============
481 R_DrawViewModel
482 =============
483 */
484 void R_DrawViewModel (void)
485 {
486         entity_render_t *ent;
487
488         // FIXME: move these checks to client
489         if (!r_drawviewmodel.integer || chase_active.integer || envmap || !r_drawentities.integer || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
490                 return;
491
492         ent = &cl.viewent.render;
493         Mod_CheckLoaded(ent->model);
494
495         R_LerpAnimation(ent);
496
497         ent->model->Draw(ent);
498 }
499
500 static void R_SetFrustum (void)
501 {
502         int i;
503
504         // LordHavoc: note to all quake engine coders, the special case for 90
505         // degrees assumed a square view (wrong), so I removed it, Quake2 has it
506         // disabled as well.
507         // rotate VPN right by FOV_X/2 degrees
508         RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
509         // rotate VPN left by FOV_X/2 degrees
510         RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
511         // rotate VPN up by FOV_X/2 degrees
512         RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
513         // rotate VPN down by FOV_X/2 degrees
514         RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
515
516         for (i = 0;i < 4;i++)
517         {
518                 frustum[i].type = PLANE_ANYZ;
519                 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
520                 PlaneClassify(&frustum[i]);
521         }
522 }
523
524 /*
525 ===============
526 R_SetupFrame
527 ===============
528 */
529 static void R_SetupFrame (void)
530 {
531 // don't allow cheats in multiplayer
532         if (cl.maxclients > 1)
533         {
534                 if (r_fullbright.integer != 0)
535                         Cvar_Set ("r_fullbright", "0");
536                 if (r_ambient.value != 0)
537                         Cvar_Set ("r_ambient", "0");
538         }
539
540         r_framecount++;
541
542 // build the transformation matrix for the given view angles
543         VectorCopy (r_refdef.vieworg, r_origin);
544
545         AngleVectors (r_refdef.viewangles, vpn, vright, vup);
546
547 // current viewleaf
548         r_oldviewleaf = r_viewleaf;
549         r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
550
551         R_AnimateLight ();
552 }
553
554
555 static void R_BlendView(void)
556 {
557         rmeshbufferinfo_t m;
558         float r;
559
560         if (r_refdef.viewblend[3] < 0.01f)
561                 return;
562
563         memset(&m, 0, sizeof(m));
564         m.transparent = false;
565         m.blendfunc1 = GL_SRC_ALPHA;
566         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
567         m.depthdisable = true; // magic
568         m.numtriangles = 1;
569         m.numverts = 3;
570         if (R_Mesh_Draw_GetBuffer(&m, false))
571         {
572                 m.index[0] = 0;
573                 m.index[1] = 1;
574                 m.index[2] = 2;
575                 m.color[0] = m.color[4] = m.color[8] = r_refdef.viewblend[0];
576                 m.color[1] = m.color[5] = m.color[9] = r_refdef.viewblend[1];
577                 m.color[2] = m.color[6] = m.color[10] = r_refdef.viewblend[2];
578                 m.color[3] = m.color[7] = m.color[11] = r_refdef.viewblend[3];
579                 r = 64000;
580                 m.vertex[0] = r_origin[0] + vpn[0] * 1.5 - vright[0] * r - vup[0] * r;
581                 m.vertex[1] = r_origin[1] + vpn[1] * 1.5 - vright[1] * r - vup[1] * r;
582                 m.vertex[2] = r_origin[2] + vpn[2] * 1.5 - vright[2] * r - vup[2] * r;
583                 r *= 3;
584                 m.vertex[4] = m.vertex[0] + vup[0] * r;
585                 m.vertex[5] = m.vertex[1] + vup[1] * r;
586                 m.vertex[6] = m.vertex[2] + vup[2] * r;
587                 m.vertex[8] = m.vertex[0] + vright[0] * r;
588                 m.vertex[9] = m.vertex[1] + vright[1] * r;
589                 m.vertex[10] = m.vertex[2] + vright[2] * r;
590                 R_Mesh_Render();
591         }
592 }
593
594 /*
595 ================
596 R_RenderView
597
598 r_refdef must be set before the first call
599 ================
600 */
601 void R_RenderView (void)
602 {
603         entity_render_t *world = &cl_entities[0].render;
604         if (!cl.worldmodel)
605                 return; //Host_Error ("R_RenderView: NULL worldmodel");
606
607         // FIXME: move to client
608         R_MoveExplosions();
609         R_TimeReport("mexplosion");
610
611         R_SetupFrame();
612         R_SetFrustum();
613         R_SetupFog();
614         R_SkyStartFrame();
615         R_BuildLightList();
616
617         R_MeshQueue_BeginScene();
618
619         R_FarClip_Start(r_origin, vpn, 768.0f);
620
621         R_TimeReport("setup");
622
623         R_DrawWorld(world);
624         R_TimeReport("worldnode");
625
626         R_MarkEntities();
627         R_TimeReport("markentity");
628
629         R_MarkWorldLights(world);
630         R_TimeReport("marklights");
631
632         r_farclip = R_FarClip_Finish() + 256.0f;
633
634         R_Mesh_Start(r_farclip);
635
636
637         if (skyrendermasked)
638         {
639                 if (R_DrawBModelSky())
640                         R_TimeReport("bmodelsky");
641         }
642         else
643         {
644                 R_DrawViewModel();
645                 R_TimeReport("viewmodel");
646         }
647
648         R_SetupForWorldRendering(world);
649         R_PrepareSurfaces(world);
650         R_TimeReport("surfprep");
651
652         R_DrawSurfaces(world, SHADERSTAGE_SKY);
653         R_DrawSurfaces(world, SHADERSTAGE_NORMAL);
654         R_TimeReport("surfdraw");
655
656         if (r_drawportals.integer)
657         {
658                 R_DrawPortals(world);
659                 R_TimeReport("portals");
660         }
661
662         // don't let sound skip if going slow
663         if (!intimerefresh && !r_speeds.integer)
664                 S_ExtraUpdate ();
665
666         if (skyrendermasked)
667         {
668                 R_DrawViewModel();
669                 R_TimeReport("viewmodel");
670         }
671
672         R_DrawModels();
673         R_TimeReport("models");
674
675         R_DrawParticles();
676         R_TimeReport("particles");
677
678         R_DrawExplosions();
679         R_TimeReport("explosions");
680
681         R_MeshQueue_EndScene();
682
683         // draw transparent meshs
684         R_Mesh_AddTransparent();
685         R_TimeReport("addtrans");
686
687         R_DrawCoronas();
688         R_TimeReport("coronas");
689
690         R_DrawCrosshair();
691         R_TimeReport("crosshair");
692
693         R_BlendView();
694         R_TimeReport("blendview");
695
696         R_Mesh_Finish();
697         R_TimeReport("meshfinish");
698 }
699