no more warping meshs of any kind
[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_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_fullbright);
243         Cvar_RegisterVariable (&r_multitexture);
244         if (gamemode == GAME_NEHAHRA)
245                 Cvar_SetValue("r_fullbrights", 0);
246         R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
247 }
248
249 vec3_t r_farclip_origin;
250 vec3_t r_farclip_direction;
251 vec_t r_farclip_directiondist;
252 vec_t r_farclip_meshfarclip;
253 int r_farclip_directionbit0;
254 int r_farclip_directionbit1;
255 int r_farclip_directionbit2;
256
257 // start a farclip measuring session
258 void R_FarClip_Start(vec3_t origin, vec3_t direction, vec_t startfarclip)
259 {
260         VectorCopy(origin, r_farclip_origin);
261         VectorCopy(direction, r_farclip_direction);
262         r_farclip_directiondist = DotProduct(r_farclip_origin, r_farclip_direction);
263         r_farclip_directionbit0 = r_farclip_direction[0] < 0;
264         r_farclip_directionbit1 = r_farclip_direction[1] < 0;
265         r_farclip_directionbit2 = r_farclip_direction[2] < 0;
266         r_farclip_meshfarclip = r_farclip_directiondist + startfarclip;
267 }
268
269 // enlarge farclip to accomodate box
270 void R_FarClip_Box(vec3_t mins, vec3_t maxs)
271 {
272         float d;
273         d = (r_farclip_directionbit0 ? mins[0] : maxs[0]) * r_farclip_direction[0]
274           + (r_farclip_directionbit1 ? mins[1] : maxs[1]) * r_farclip_direction[1]
275           + (r_farclip_directionbit2 ? mins[2] : maxs[2]) * r_farclip_direction[2];
276         if (r_farclip_meshfarclip < d)
277                 r_farclip_meshfarclip = d;
278 }
279
280 // return farclip value
281 float R_FarClip_Finish(void)
282 {
283         return r_farclip_meshfarclip - r_farclip_directiondist;
284 }
285
286 /*
287 ===============
288 R_NewMap
289 ===============
290 */
291 void CL_ParseEntityLump(char *entitystring);
292 void R_NewMap (void)
293 {
294         int i;
295
296         for (i = 0;i < 256;i++)
297                 d_lightstylevalue[i] = 264;             // normal light value
298
299         r_viewleaf = NULL;
300         if (cl.worldmodel->entities)
301                 CL_ParseEntityLump(cl.worldmodel->entities);
302         R_Modules_NewMap();
303
304         r_farclip = 64.0f;
305 }
306
307 extern void R_Textures_Init(void);
308 extern void Mod_RenderInit(void);
309 extern void GL_Draw_Init(void);
310 extern void GL_Main_Init(void);
311 extern void GL_Models_Init(void);
312 extern void R_Sky_Init(void);
313 extern void GL_Surf_Init(void);
314 extern void R_Crosshairs_Init(void);
315 extern void R_Light_Init(void);
316 extern void R_Particles_Init(void);
317 extern void R_Explosion_Init(void);
318 extern void ui_init(void);
319 extern void gl_backend_init(void);
320
321 void Render_Init(void)
322 {
323         R_Modules_Shutdown();
324         R_Textures_Init();
325         Mod_RenderInit();
326         gl_backend_init();
327         GL_Draw_Init();
328         GL_Main_Init();
329         GL_Models_Init();
330         R_Sky_Init();
331         GL_Surf_Init();
332         R_Crosshairs_Init();
333         R_Light_Init();
334         R_Particles_Init();
335         R_Explosion_Init();
336         ui_init();
337         R_Modules_Start();
338 }
339
340 /*
341 ===============
342 GL_Init
343 ===============
344 */
345 extern char *ENGINE_EXTENSIONS;
346 void GL_Init (void)
347 {
348         VID_CheckExtensions();
349
350         // LordHavoc: report supported extensions
351         Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
352
353         qglCullFace(GL_FRONT);
354         qglEnable(GL_TEXTURE_2D);
355 }
356
357
358 //==================================================================================
359
360 static void R_MarkEntities (void)
361 {
362         int i;
363         vec3_t v;
364
365         R_FarClip_Box(cl.worldmodel->normalmins, cl.worldmodel->normalmaxs);
366
367         if (!r_drawentities.integer)
368                 return;
369
370         for (i = 0;i < r_refdef.numentities;i++)
371         {
372                 currentrenderentity = r_refdef.entities[i];
373                 Mod_CheckLoaded(currentrenderentity->model);
374
375                 // move view-relative models to where they should be
376                 if (currentrenderentity->flags & RENDER_VIEWMODEL)
377                 {
378                         // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
379                         currentrenderentity->flags -= RENDER_VIEWMODEL;
380                         // transform origin
381                         VectorCopy(currentrenderentity->origin, v);
382                         currentrenderentity->origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
383                         currentrenderentity->origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
384                         currentrenderentity->origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
385                         // adjust angles
386                         VectorAdd(currentrenderentity->angles, r_refdef.viewangles, currentrenderentity->angles);
387                 }
388
389                 if (currentrenderentity->angles[0] || currentrenderentity->angles[2])
390                 {
391                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->rotatedmins, currentrenderentity->mins);
392                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->rotatedmaxs, currentrenderentity->maxs);
393                 }
394                 else if (currentrenderentity->angles[1])
395                 {
396                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->yawmins, currentrenderentity->mins);
397                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->yawmaxs, currentrenderentity->maxs);
398                 }
399                 else
400                 {
401                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->normalmins, currentrenderentity->mins);
402                         VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->normalmaxs, currentrenderentity->maxs);
403                 }
404                 if (R_VisibleCullBox(currentrenderentity->mins, currentrenderentity->maxs))
405                         continue;
406
407                 R_LerpAnimation(currentrenderentity);
408                 currentrenderentity->visframe = r_framecount;
409
410                 R_FarClip_Box(currentrenderentity->mins, currentrenderentity->maxs);
411         }
412 }
413
414 // only used if skyrendermasked, and normally returns false
415 int R_DrawBModelSky (void)
416 {
417         int i, sky;
418
419         if (!r_drawentities.integer)
420                 return false;
421
422         sky = false;
423         for (i = 0;i < r_refdef.numentities;i++)
424         {
425                 currentrenderentity = r_refdef.entities[i];
426                 if (currentrenderentity->visframe == r_framecount && currentrenderentity->model->DrawSky)
427                 {
428                         currentrenderentity->model->DrawSky();
429                         sky = true;
430                 }
431         }
432         return sky;
433 }
434
435 void R_DrawModels (void)
436 {
437         int i;
438
439         if (!r_drawentities.integer)
440                 return;
441
442         for (i = 0;i < r_refdef.numentities;i++)
443         {
444                 currentrenderentity = r_refdef.entities[i];
445                 if (currentrenderentity->visframe == r_framecount && currentrenderentity->model->Draw)
446                         currentrenderentity->model->Draw();
447         }
448 }
449
450 /*
451 =============
452 R_DrawViewModel
453 =============
454 */
455 void R_DrawViewModel (void)
456 {
457         // FIXME: move these checks to client
458         if (!r_drawviewmodel.integer || chase_active.integer || envmap || !r_drawentities.integer || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
459                 return;
460
461         currentrenderentity = &cl.viewent.render;
462         Mod_CheckLoaded(currentrenderentity->model);
463
464         R_LerpAnimation(currentrenderentity);
465
466         currentrenderentity->model->Draw();
467 }
468
469 static void R_SetFrustum (void)
470 {
471         int i;
472
473         // LordHavoc: note to all quake engine coders, the special case for 90
474         // degrees assumed a square view (wrong), so I removed it, Quake2 has it
475         // disabled as well.
476         // rotate VPN right by FOV_X/2 degrees
477         RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
478         // rotate VPN left by FOV_X/2 degrees
479         RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
480         // rotate VPN up by FOV_X/2 degrees
481         RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
482         // rotate VPN down by FOV_X/2 degrees
483         RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
484
485         for (i = 0;i < 4;i++)
486         {
487                 frustum[i].type = PLANE_ANYZ;
488                 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
489                 PlaneClassify(&frustum[i]);
490         }
491 }
492
493 /*
494 ===============
495 R_SetupFrame
496 ===============
497 */
498 static void R_SetupFrame (void)
499 {
500 // don't allow cheats in multiplayer
501         if (cl.maxclients > 1)
502         {
503                 if (r_fullbright.integer != 0)
504                         Cvar_Set ("r_fullbright", "0");
505                 if (r_ambient.value != 0)
506                         Cvar_Set ("r_ambient", "0");
507         }
508         if (r_multitexture.integer && gl_textureunits < 2)
509                 Cvar_SetValue("r_multitexture", 0);
510
511         r_framecount++;
512
513 // build the transformation matrix for the given view angles
514         VectorCopy (r_refdef.vieworg, r_origin);
515
516         AngleVectors (r_refdef.viewangles, vpn, vright, vup);
517
518 // current viewleaf
519         r_oldviewleaf = r_viewleaf;
520         r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
521
522         R_AnimateLight ();
523 }
524
525
526 static void R_BlendView(void)
527 {
528         rmeshbufferinfo_t m;
529         float r;
530
531         if (r_refdef.viewblend[3] < 0.01f)
532                 return;
533
534         memset(&m, 0, sizeof(m));
535         m.transparent = false;
536         m.blendfunc1 = GL_SRC_ALPHA;
537         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
538         m.depthdisable = true; // magic
539         m.numtriangles = 1;
540         m.numverts = 3;
541         if (R_Mesh_Draw_GetBuffer(&m))
542         {
543                 m.index[0] = 0;
544                 m.index[1] = 1;
545                 m.index[2] = 2;
546                 m.color[0] = m.color[4] = m.color[8] = r_refdef.viewblend[0];
547                 m.color[1] = m.color[5] = m.color[9] = r_refdef.viewblend[1];
548                 m.color[2] = m.color[6] = m.color[10] = r_refdef.viewblend[2];
549                 m.color[3] = m.color[7] = m.color[11] = r_refdef.viewblend[3];
550                 r = 64000;
551                 m.vertex[0] = r_origin[0] + vpn[0] * 1.5 - vright[0] * r - vup[0] * r;
552                 m.vertex[1] = r_origin[1] + vpn[1] * 1.5 - vright[1] * r - vup[1] * r;
553                 m.vertex[2] = r_origin[2] + vpn[2] * 1.5 - vright[2] * r - vup[2] * r;
554                 r *= 3;
555                 m.vertex[4] = m.vertex[0] + vup[0] * r;
556                 m.vertex[5] = m.vertex[1] + vup[1] * r;
557                 m.vertex[6] = m.vertex[2] + vup[2] * r;
558                 m.vertex[8] = m.vertex[0] + vright[0] * r;
559                 m.vertex[9] = m.vertex[1] + vright[1] * r;
560                 m.vertex[10] = m.vertex[2] + vright[2] * r;
561         }
562 }
563
564 /*
565 ================
566 R_RenderView
567
568 r_refdef must be set before the first call
569 ================
570 */
571 void R_RenderView (void)
572 {
573         if (!cl.worldmodel)
574                 return; //Host_Error ("R_RenderView: NULL worldmodel");
575
576         // FIXME: move to client
577         R_MoveExplosions();
578         R_TimeReport("mexplosion");
579
580         R_SetupFrame();
581         R_SetFrustum();
582         R_SetupFog();
583         R_SkyStartFrame();
584         R_BuildLightList();
585
586         R_FarClip_Start(r_origin, vpn, 768.0f);
587
588         R_TimeReport("setup");
589
590         R_DrawWorld();
591         R_TimeReport("worldnode");
592
593         R_MarkEntities();
594         R_TimeReport("markentity");
595
596         R_MarkWorldLights();
597         R_TimeReport("marklights");
598
599         r_farclip = R_FarClip_Finish() + 256.0f;
600
601         R_Mesh_Start(r_farclip);
602
603         if (skyrendermasked)
604         {
605                 if (R_DrawBModelSky())
606                         R_TimeReport("bmodelsky");
607         }
608         else
609         {
610                 R_DrawViewModel();
611                 R_TimeReport("viewmodel");
612         }
613
614         R_SetupForWorldRendering();
615         R_PrepareSurfaces();
616         R_TimeReport("surfprep");
617
618         R_DrawSurfaces(SHADERSTAGE_SKY);
619         R_DrawSurfaces(SHADERSTAGE_NORMAL);
620         R_TimeReport("surfdraw");
621
622         if (r_drawportals.integer)
623         {
624                 R_DrawPortals();
625                 R_TimeReport("portals");
626         }
627
628         // don't let sound skip if going slow
629         if (!intimerefresh && !r_speeds.integer)
630                 S_ExtraUpdate ();
631
632         if (skyrendermasked)
633         {
634                 R_DrawViewModel();
635                 R_TimeReport("viewmodel");
636         }
637
638         R_DrawModels();
639         R_TimeReport("models");
640
641         R_DrawParticles();
642         R_TimeReport("particles");
643
644         R_DrawExplosions();
645         R_TimeReport("explosions");
646
647         // draw transparent meshs
648         R_Mesh_AddTransparent();
649         R_TimeReport("addtrans");
650
651         R_DrawCoronas();
652         R_TimeReport("coronas");
653
654         R_BlendView();
655         R_TimeReport("blendview");
656
657         R_DrawCrosshair();
658         R_TimeReport("crosshair");
659
660         // render any queued meshs
661         R_Mesh_Finish();
662         R_TimeReport("meshfinish");
663 }
664