]> git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_rmain.c
much nicer rocket and grenade trails
[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         gl_vendor = glGetString (GL_VENDOR);
314         Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
315         gl_renderer = glGetString (GL_RENDERER);
316         Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
317
318         gl_version = glGetString (GL_VERSION);
319         Con_Printf ("GL_VERSION: %s\n", gl_version);
320         gl_extensions = glGetString (GL_EXTENSIONS);
321         Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
322
323         VID_CheckExtensions();
324
325         // LordHavoc: report supported extensions
326         Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
327
328         qglCullFace(GL_FRONT);
329         qglEnable(GL_TEXTURE_2D);
330 }
331
332
333 //==================================================================================
334
335 void R_Entity_Callback(void *data, void *junk)
336 {
337         ((entity_render_t *)data)->visframe = r_framecount;
338 }
339
340 static void R_MarkEntities (void)
341 {
342         int             i;
343         vec3_t  v;
344
345         if (!r_drawentities.integer)
346                 return;
347
348         for (i = 0;i < r_refdef.numentities;i++)
349         {
350                 currentrenderentity = r_refdef.entities[i];
351                 Mod_CheckLoaded(currentrenderentity->model);
352
353                 // move view-relative models to where they should be
354                 if (currentrenderentity->flags & RENDER_VIEWMODEL)
355                 {
356                         // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
357                         currentrenderentity->flags -= RENDER_VIEWMODEL;
358                         // transform origin
359                         VectorCopy(currentrenderentity->origin, v);
360                         currentrenderentity->origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
361                         currentrenderentity->origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
362                         currentrenderentity->origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
363                         // adjust angles
364                         VectorAdd(currentrenderentity->angles, r_refdef.viewangles, currentrenderentity->angles);
365                 }
366
367                 if (currentrenderentity->angles[0] || currentrenderentity->angles[2])
368                 {
369                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->rotatedmins, currentrenderentity->mins);
370                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->rotatedmaxs, currentrenderentity->maxs);
371                 }
372                 else if (currentrenderentity->angles[1])
373                 {
374                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->yawmins, currentrenderentity->mins);
375                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->yawmaxs, currentrenderentity->maxs);
376                 }
377                 else
378                 {
379                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->normalmins, currentrenderentity->mins);
380                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->normalmaxs, currentrenderentity->maxs);
381                 }
382                 if (R_VisibleCullBox(currentrenderentity->mins, currentrenderentity->maxs))
383                         continue;
384
385                 R_LerpAnimation(currentrenderentity);
386                 if (r_ser.integer)
387                         currentrenderentity->model->SERAddEntity();
388                 else
389                         currentrenderentity->visframe = r_framecount;
390         }
391 }
392
393 // only used if skyrendermasked, and normally returns false
394 int R_DrawBModelSky (void)
395 {
396         int             i, sky = false;
397
398         if (!r_drawentities.integer)
399                 return false;
400
401         for (i = 0;i < r_refdef.numentities;i++)
402         {
403                 currentrenderentity = r_refdef.entities[i];
404                 if (currentrenderentity->visframe == r_framecount && currentrenderentity->model->DrawSky)
405                 {
406                         currentrenderentity->model->DrawSky();
407                         sky = true;
408                 }
409         }
410         return sky;
411 }
412
413 void R_DrawModels (void)
414 {
415         int             i;
416
417         if (!r_drawentities.integer)
418                 return;
419
420         for (i = 0;i < r_refdef.numentities;i++)
421         {
422                 currentrenderentity = r_refdef.entities[i];
423                 if (currentrenderentity->visframe == r_framecount && currentrenderentity->model->Draw)
424                         currentrenderentity->model->Draw();
425         }
426 }
427
428 /*
429 =============
430 R_DrawViewModel
431 =============
432 */
433 void R_DrawViewModel (void)
434 {
435         // FIXME: move these checks to client
436         if (!r_drawviewmodel.integer || chase_active.integer || envmap || !r_drawentities.integer || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
437                 return;
438
439         currentrenderentity = &cl.viewent.render;
440         Mod_CheckLoaded(currentrenderentity->model);
441
442         R_LerpAnimation(currentrenderentity);
443
444         currentrenderentity->model->Draw();
445 }
446
447 static void R_SetFrustum (void)
448 {
449         int             i;
450
451         // LordHavoc: note to all quake engine coders, the special case for 90
452         // degrees assumed a square view (wrong), so I removed it, Quake2 has it
453         // disabled as well.
454         // rotate VPN right by FOV_X/2 degrees
455         RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
456         // rotate VPN left by FOV_X/2 degrees
457         RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
458         // rotate VPN up by FOV_X/2 degrees
459         RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
460         // rotate VPN down by FOV_X/2 degrees
461         RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
462
463
464         for (i=0 ; i<4 ; i++)
465         {
466                 frustum[i].type = PLANE_ANYZ;
467                 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
468                 PlaneClassify(&frustum[i]);
469         }
470 }
471
472 /*
473 ===============
474 R_SetupFrame
475 ===============
476 */
477 static void R_SetupFrame (void)
478 {
479 // don't allow cheats in multiplayer
480         if (cl.maxclients > 1)
481         {
482                 if (r_fullbright.integer != 0)
483                         Cvar_Set ("r_fullbright", "0");
484                 if (r_ambient.value != 0)
485                         Cvar_Set ("r_ambient", "0");
486         }
487         if (r_multitexture.integer && gl_textureunits < 2)
488                 Cvar_SetValue("r_multitexture", 0);
489
490         r_framecount++;
491
492 // build the transformation matrix for the given view angles
493         VectorCopy (r_refdef.vieworg, r_origin);
494
495         AngleVectors (r_refdef.viewangles, vpn, vright, vup);
496
497 // current viewleaf
498         r_oldviewleaf = r_viewleaf;
499         r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
500
501         R_AnimateLight ();
502 }
503
504
505 static int blendviewpolyindex[3] = {0, 1, 2};
506
507 static void R_BlendView(void)
508 {
509         rmeshinfo_t m;
510         float tvxyz[3][4], r;
511
512         if (r_refdef.viewblend[3] < 0.01f)
513                 return;
514
515         memset(&m, 0, sizeof(m));
516         m.transparent = false;
517         m.blendfunc1 = GL_SRC_ALPHA;
518         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
519         m.depthdisable = true; // magic
520         m.numtriangles = 1;
521         m.numverts = 3;
522         m.index = blendviewpolyindex;
523         m.vertex = &tvxyz[0][0];
524         m.vertexstep = sizeof(float[4]);
525         m.cr = r_refdef.viewblend[0];
526         m.cg = r_refdef.viewblend[1];
527         m.cb = r_refdef.viewblend[2];
528         m.ca = r_refdef.viewblend[3];
529         r = 64000;
530         tvxyz[0][0] = r_origin[0] + vpn[0] * 1.5 - vright[0] * r - vup[0] * r;
531         tvxyz[0][1] = r_origin[1] + vpn[1] * 1.5 - vright[1] * r - vup[1] * r;
532         tvxyz[0][2] = r_origin[2] + vpn[2] * 1.5 - vright[2] * r - vup[2] * r;
533         r *= 3;
534         tvxyz[1][0] = tvxyz[0][0] + vup[0] * r;
535         tvxyz[1][1] = tvxyz[0][1] + vup[1] * r;
536         tvxyz[1][2] = tvxyz[0][2] + vup[2] * r;
537         tvxyz[2][0] = tvxyz[0][0] + vright[0] * r;
538         tvxyz[2][1] = tvxyz[0][1] + vright[1] * r;
539         tvxyz[2][2] = tvxyz[0][2] + vright[2] * r;
540         R_Mesh_Draw(&m);
541 }
542
543 /*
544 ================
545 R_RenderView
546
547 r_refdef must be set before the first call
548 ================
549 */
550 void R_RenderView (void)
551 {
552         if (!cl.worldmodel)
553                 return; //Host_Error ("R_RenderView: NULL worldmodel");
554
555         // FIXME: move to client
556         R_MoveExplosions();
557         R_TimeReport("mexplosion");
558
559         R_SetupFrame();
560         R_SetFrustum();
561         R_SetupFog();
562         R_SkyStartFrame();
563         if (r_ser.integer)
564                 R_Clip_StartFrame();
565         R_BuildLightList();
566
567         R_Mesh_Start();
568
569         R_TimeReport("setup");
570
571         R_DrawWorld();
572         R_TimeReport("worldnode");
573
574         R_MarkEntities();
575         R_TimeReport("markentity");
576
577         if (r_ser.integer)
578         {
579                 R_Clip_EndFrame();
580                 R_TimeReport("hiddensurf");
581         }
582
583         R_MarkWorldLights();
584         R_TimeReport("marklights");
585
586         if (skyrendermasked)
587         {
588                 if (R_DrawBModelSky())
589                         R_TimeReport("bmodelsky");
590         }
591         else
592         {
593                 R_DrawViewModel();
594                 R_TimeReport("viewmodel");
595         }
596
597         R_SetupForWorldRendering();
598         R_PrepareSurfaces();
599         R_TimeReport("surfprep");
600
601         R_DrawSurfaces(SHADERSTAGE_SKY);
602         R_DrawSurfaces(SHADERSTAGE_NORMAL);
603         R_TimeReport("surfdraw");
604
605         if (r_drawportals.integer)
606         {
607                 R_DrawPortals();
608                 R_TimeReport("portals");
609         }
610
611         // don't let sound skip if going slow
612         if (!intimerefresh && !r_speeds.integer)
613                 S_ExtraUpdate ();
614
615         if (skyrendermasked)
616         {
617                 R_DrawViewModel();
618                 R_TimeReport("viewmodel");
619         }
620
621         R_DrawModels();
622         R_TimeReport("models");
623
624         R_DrawParticles();
625         R_TimeReport("particles");
626
627         R_DrawExplosions();
628         R_TimeReport("explosions");
629
630         // draw transparent meshs
631         R_Mesh_AddTransparent();
632         R_TimeReport("addtrans");
633
634         R_DrawCoronas();
635         R_TimeReport("coronas");
636
637         R_BlendView();
638         R_TimeReport("blendview");
639
640         // render any queued meshs
641         R_Mesh_Finish();
642         R_TimeReport("meshfinish");
643 }
644