2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
24 entity_render_t *currentrenderentity;
26 int r_framecount; // used for dlight push checking
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;
32 qboolean envmap; // true during envmap command capture
47 mleaf_t *r_viewleaf, *r_oldviewleaf;
49 unsigned short d_lightstylevalue[256]; // 8.8 fraction of base light value
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"};
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"};
68 cvar_t r_ser = {CVAR_SAVE, "r_ser", "0"};
70 cvar_t r_multitexture = {0, "r_multitexture", "1"};
76 For program optimization
79 qboolean intimerefresh = 0;
80 static void R_TimeRefresh_f (void)
83 float start, stop, time;
86 start = Sys_DoubleTime ();
87 for (i = 0;i < 128;i++)
89 r_refdef.viewangles[0] = 0;
90 r_refdef.viewangles[1] = i/128.0*360.0;
91 r_refdef.viewangles[2] = 0;
95 stop = Sys_DoubleTime ();
98 Con_Printf ("%f seconds (%f fps)\n", time, 128/time);
101 extern cvar_t r_drawportals;
103 int R_VisibleCullBox (vec3_t mins, vec3_t maxs)
106 mnode_t *nodestack[8192], *node;
109 if (R_CullBox(mins, maxs))
112 node = cl.worldmodel->nodes;
114 if (node->contents < 0)
116 if (((mleaf_t *)node)->visframe == r_framecount)
120 node = nodestack[--stack];
124 sides = BOX_ON_PLANE_SIDE(mins, maxs, node->plane);
126 // recurse down the contacted sides
131 // put second child on the stack for later examination
132 nodestack[stack++] = node->children[1];
133 node = node->children[0];
138 node = node->children[0];
143 node = node->children[1];
149 float fog_density, fog_red, fog_green, fog_blue;
151 qboolean oldgl_fogenable;
152 void R_SetupFog(void)
154 if (gamemode == GAME_NEHAHRA)
156 if (gl_fogenable.integer)
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;
164 else if (oldgl_fogenable)
166 oldgl_fogenable = false;
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);
182 fogdensity = -4000.0f / (fog_density * fog_density);
183 // fog color was already set
189 // FIXME: move this to client?
192 if (gamemode == GAME_NEHAHRA)
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");
200 fog_density = fog_red = fog_green = fog_blue = 0.0f;
203 // FIXME: move this to client?
204 void FOG_registercvars(void)
206 if (gamemode == GAME_NEHAHRA)
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);
218 void gl_main_start(void)
222 void gl_main_shutdown(void)
226 void gl_main_newmap(void)
231 void GL_Main_Init(void)
233 // FIXME: move this to client?
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);
256 void CL_ParseEntityLump(char *entitystring);
261 for (i=0 ; i<256 ; i++)
262 d_lightstylevalue[i] = 264; // normal light value
265 if (cl.worldmodel->entities)
266 CL_ParseEntityLump(cl.worldmodel->entities);
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);
285 void Render_Init(void)
287 R_Modules_Shutdown();
310 extern char *ENGINE_EXTENSIONS;
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);
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);
323 VID_CheckExtensions();
325 // LordHavoc: report supported extensions
326 Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
328 qglCullFace(GL_FRONT);
329 qglEnable(GL_TEXTURE_2D);
333 //==================================================================================
335 void R_Entity_Callback(void *data, void *junk)
337 ((entity_render_t *)data)->visframe = r_framecount;
340 static void R_MarkEntities (void)
345 if (!r_drawentities.integer)
348 for (i = 0;i < r_refdef.numentities;i++)
350 currentrenderentity = r_refdef.entities[i];
351 Mod_CheckLoaded(currentrenderentity->model);
353 // move view-relative models to where they should be
354 if (currentrenderentity->flags & RENDER_VIEWMODEL)
356 // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
357 currentrenderentity->flags -= RENDER_VIEWMODEL;
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];
364 VectorAdd(currentrenderentity->angles, r_refdef.viewangles, currentrenderentity->angles);
367 if (currentrenderentity->angles[0] || currentrenderentity->angles[2])
369 VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->rotatedmins, currentrenderentity->mins);
370 VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->rotatedmaxs, currentrenderentity->maxs);
372 else if (currentrenderentity->angles[1])
374 VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->yawmins, currentrenderentity->mins);
375 VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->yawmaxs, currentrenderentity->maxs);
379 VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->normalmins, currentrenderentity->mins);
380 VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->normalmaxs, currentrenderentity->maxs);
382 if (R_VisibleCullBox(currentrenderentity->mins, currentrenderentity->maxs))
385 R_LerpAnimation(currentrenderentity);
387 currentrenderentity->model->SERAddEntity();
389 currentrenderentity->visframe = r_framecount;
393 // only used if skyrendermasked, and normally returns false
394 int R_DrawBModelSky (void)
398 if (!r_drawentities.integer)
401 for (i = 0;i < r_refdef.numentities;i++)
403 currentrenderentity = r_refdef.entities[i];
404 if (currentrenderentity->visframe == r_framecount && currentrenderentity->model->DrawSky)
406 currentrenderentity->model->DrawSky();
413 void R_DrawModels (void)
417 if (!r_drawentities.integer)
420 for (i = 0;i < r_refdef.numentities;i++)
422 currentrenderentity = r_refdef.entities[i];
423 if (currentrenderentity->visframe == r_framecount && currentrenderentity->model->Draw)
424 currentrenderentity->model->Draw();
433 void R_DrawViewModel (void)
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)
439 currentrenderentity = &cl.viewent.render;
440 Mod_CheckLoaded(currentrenderentity->model);
442 R_LerpAnimation(currentrenderentity);
444 currentrenderentity->model->Draw();
447 static void R_SetFrustum (void)
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
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 ) );
464 for (i=0 ; i<4 ; i++)
466 frustum[i].type = PLANE_ANYZ;
467 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
468 PlaneClassify(&frustum[i]);
477 static void R_SetupFrame (void)
479 // don't allow cheats in multiplayer
480 if (cl.maxclients > 1)
482 if (r_fullbright.integer != 0)
483 Cvar_Set ("r_fullbright", "0");
484 if (r_ambient.value != 0)
485 Cvar_Set ("r_ambient", "0");
487 if (r_multitexture.integer && gl_textureunits < 2)
488 Cvar_SetValue("r_multitexture", 0);
492 // build the transformation matrix for the given view angles
493 VectorCopy (r_refdef.vieworg, r_origin);
495 AngleVectors (r_refdef.viewangles, vpn, vright, vup);
498 r_oldviewleaf = r_viewleaf;
499 r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
505 static int blendviewpolyindex[3] = {0, 1, 2};
507 static void R_BlendView(void)
510 float tvxyz[3][4], r;
512 if (r_refdef.viewblend[3] < 0.01f)
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
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];
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;
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;
547 r_refdef must be set before the first call
550 void R_RenderView (void)
553 return; //Host_Error ("R_RenderView: NULL worldmodel");
555 // FIXME: move to client
557 R_TimeReport("mexplosion");
569 R_TimeReport("setup");
572 R_TimeReport("worldnode");
575 R_TimeReport("markentity");
580 R_TimeReport("hiddensurf");
584 R_TimeReport("marklights");
588 if (R_DrawBModelSky())
589 R_TimeReport("bmodelsky");
594 R_TimeReport("viewmodel");
597 R_SetupForWorldRendering();
599 R_TimeReport("surfprep");
601 R_DrawSurfaces(SHADERSTAGE_SKY);
602 R_DrawSurfaces(SHADERSTAGE_NORMAL);
603 R_TimeReport("surfdraw");
605 if (r_drawportals.integer)
608 R_TimeReport("portals");
611 // don't let sound skip if going slow
612 if (!intimerefresh && !r_speeds.integer)
618 R_TimeReport("viewmodel");
622 R_TimeReport("models");
625 R_TimeReport("particles");
628 R_TimeReport("explosions");
630 // draw transparent meshs
631 R_Mesh_AddTransparent();
632 R_TimeReport("addtrans");
635 R_TimeReport("coronas");
638 R_TimeReport("blendview");
640 // render any queued meshs
642 R_TimeReport("meshfinish");