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 // used for dlight push checking and other things
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;
31 // true during envmap command capture
47 mleaf_t *r_viewleaf, *r_oldviewleaf;
49 // 8.8 fraction of base light value
50 unsigned short d_lightstylevalue[256];
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"};
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_textureunits = {0, "r_textureunits", "32"};
70 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
73 for (i = 0;i < verts;i++)
84 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
87 for (i = 0;i < verts;i++)
101 For program optimization
104 qboolean intimerefresh = 0;
105 static void R_TimeRefresh_f (void)
108 float start, stop, time;
111 start = Sys_DoubleTime ();
112 for (i = 0;i < 128;i++)
114 r_refdef.viewangles[0] = 0;
115 r_refdef.viewangles[1] = i/128.0*360.0;
116 r_refdef.viewangles[2] = 0;
120 stop = Sys_DoubleTime ();
123 Con_Printf ("%f seconds (%f fps)\n", time, 128/time);
126 extern cvar_t r_drawportals;
128 int R_VisibleCullBox (vec3_t mins, vec3_t maxs)
131 mnode_t *nodestack[8192], *node;
134 if (R_CullBox(mins, maxs))
137 node = cl.worldmodel->nodes;
139 if (node->contents < 0)
141 if (((mleaf_t *)node)->visframe == r_framecount)
145 node = nodestack[--stack];
149 sides = BOX_ON_PLANE_SIDE(mins, maxs, node->plane);
151 // recurse down the contacted sides
156 // put second child on the stack for later examination
157 nodestack[stack++] = node->children[1];
158 node = node->children[0];
163 node = node->children[0];
168 node = node->children[1];
174 float fog_density, fog_red, fog_green, fog_blue;
176 qboolean oldgl_fogenable;
177 void R_SetupFog(void)
179 if (gamemode == GAME_NEHAHRA)
181 if (gl_fogenable.integer)
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;
189 else if (oldgl_fogenable)
191 oldgl_fogenable = false;
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);
207 fogdensity = -4000.0f / (fog_density * fog_density);
208 // fog color was already set
214 // FIXME: move this to client?
217 if (gamemode == GAME_NEHAHRA)
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");
225 fog_density = fog_red = fog_green = fog_blue = 0.0f;
228 // FIXME: move this to client?
229 void FOG_registercvars(void)
231 if (gamemode == GAME_NEHAHRA)
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);
243 void gl_main_start(void)
247 void gl_main_shutdown(void)
251 void gl_main_newmap(void)
256 void GL_Main_Init(void)
258 // FIXME: move this to client?
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);
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;
282 // start a farclip measuring session
283 void R_FarClip_Start(vec3_t origin, vec3_t direction, vec_t startfarclip)
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;
294 // enlarge farclip to accomodate box
295 void R_FarClip_Box(vec3_t mins, vec3_t maxs)
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;
305 // return farclip value
306 float R_FarClip_Finish(void)
308 return r_farclip_meshfarclip - r_farclip_directiondist;
316 void CL_ParseEntityLump(char *entitystring);
321 for (i = 0;i < 256;i++)
322 d_lightstylevalue[i] = 264; // normal light value
325 if (cl.worldmodel->entities)
326 CL_ParseEntityLump(cl.worldmodel->entities);
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);
346 void Render_Init(void)
348 R_Modules_Shutdown();
371 extern char *ENGINE_EXTENSIONS;
374 VID_CheckExtensions();
376 // LordHavoc: report supported extensions
377 Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
379 qglCullFace(GL_FRONT);
380 qglEnable(GL_TEXTURE_2D);
384 //==================================================================================
386 static void R_MarkEntities (void)
390 entity_render_t *ent;
392 ent = &cl_entities[0].render;
393 Matrix4x4_CreateIdentity(&ent->matrix);
394 Matrix4x4_CreateIdentity(&ent->inversematrix);
396 R_FarClip_Box(cl.worldmodel->normalmins, cl.worldmodel->normalmaxs);
398 if (!r_drawentities.integer)
401 for (i = 0;i < r_refdef.numentities;i++)
403 ent = r_refdef.entities[i];
404 Mod_CheckLoaded(ent->model);
406 // move view-relative models to where they should be
407 if (ent->flags & RENDER_VIEWMODEL)
409 // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
410 ent->flags -= RENDER_VIEWMODEL;
412 VectorCopy(ent->origin, v);
413 ent->origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
414 ent->origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
415 ent->origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
417 VectorAdd(ent->angles, r_refdef.viewangles, ent->angles);
420 if (ent->angles[0] || ent->angles[2])
422 VectorMA(ent->origin, ent->scale, ent->model->rotatedmins, ent->mins);
423 VectorMA(ent->origin, ent->scale, ent->model->rotatedmaxs, ent->maxs);
425 else if (ent->angles[1])
427 VectorMA(ent->origin, ent->scale, ent->model->yawmins, ent->mins);
428 VectorMA(ent->origin, ent->scale, ent->model->yawmaxs, ent->maxs);
432 VectorMA(ent->origin, ent->scale, ent->model->normalmins, ent->mins);
433 VectorMA(ent->origin, ent->scale, ent->model->normalmaxs, ent->maxs);
435 if (R_VisibleCullBox(ent->mins, ent->maxs))
438 VectorCopy(ent->angles, v);
439 if (ent->model->type != mod_brush)
441 Matrix4x4_CreateFromQuakeEntity(&ent->matrix, ent->origin[0], ent->origin[1], ent->origin[2], v[0], v[1], v[2], ent->scale);
442 Matrix4x4_Invert_Simple(&ent->inversematrix, &ent->matrix);
443 R_LerpAnimation(ent);
444 ent->visframe = r_framecount;
446 R_FarClip_Box(ent->mins, ent->maxs);
448 R_UpdateEntLights(ent);
452 // only used if skyrendermasked, and normally returns false
453 int R_DrawBModelSky (void)
456 entity_render_t *ent;
458 if (!r_drawentities.integer)
462 for (i = 0;i < r_refdef.numentities;i++)
464 ent = r_refdef.entities[i];
465 if (ent->visframe == r_framecount && ent->model->DrawSky)
467 ent->model->DrawSky(ent);
474 void R_DrawModels (void)
477 entity_render_t *ent;
479 if (!r_drawentities.integer)
482 for (i = 0;i < r_refdef.numentities;i++)
484 ent = r_refdef.entities[i];
485 if (ent->visframe == r_framecount && ent->model->Draw)
486 ent->model->Draw(ent);
495 void R_DrawViewModel (void)
497 entity_render_t *ent;
499 // FIXME: move these checks to client
500 if (!r_drawviewmodel.integer || chase_active.integer || envmap || !r_drawentities.integer || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
503 ent = &cl.viewent.render;
504 Mod_CheckLoaded(ent->model);
506 R_LerpAnimation(ent);
508 ent->model->Draw(ent);
511 static void R_SetFrustum (void)
515 // LordHavoc: note to all quake engine coders, the special case for 90
516 // degrees assumed a square view (wrong), so I removed it, Quake2 has it
518 // rotate VPN right by FOV_X/2 degrees
519 RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
520 // rotate VPN left by FOV_X/2 degrees
521 RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
522 // rotate VPN up by FOV_X/2 degrees
523 RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
524 // rotate VPN down by FOV_X/2 degrees
525 RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
527 for (i = 0;i < 4;i++)
529 frustum[i].type = PLANE_ANYZ;
530 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
531 PlaneClassify(&frustum[i]);
540 static void R_SetupFrame (void)
542 // don't allow cheats in multiplayer
543 if (cl.maxclients > 1)
545 if (r_fullbright.integer != 0)
546 Cvar_Set ("r_fullbright", "0");
547 if (r_ambient.value != 0)
548 Cvar_Set ("r_ambient", "0");
553 // build the transformation matrix for the given view angles
554 VectorCopy (r_refdef.vieworg, r_origin);
556 AngleVectors (r_refdef.viewangles, vpn, vright, vup);
559 r_oldviewleaf = r_viewleaf;
560 r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
566 static void R_BlendView(void)
571 if (r_refdef.viewblend[3] < 0.01f)
574 memset(&m, 0, sizeof(m));
575 m.blendfunc1 = GL_SRC_ALPHA;
576 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
577 m.depthdisable = true; // magic
580 Matrix4x4_CreateIdentity(&m.matrix);
581 if (R_Mesh_Draw_GetBuffer(&m, false))
586 m.color[0] = m.color[4] = m.color[8] = r_refdef.viewblend[0];
587 m.color[1] = m.color[5] = m.color[9] = r_refdef.viewblend[1];
588 m.color[2] = m.color[6] = m.color[10] = r_refdef.viewblend[2];
589 m.color[3] = m.color[7] = m.color[11] = r_refdef.viewblend[3];
591 m.vertex[0] = r_origin[0] + vpn[0] * 1.5 - vright[0] * r - vup[0] * r;
592 m.vertex[1] = r_origin[1] + vpn[1] * 1.5 - vright[1] * r - vup[1] * r;
593 m.vertex[2] = r_origin[2] + vpn[2] * 1.5 - vright[2] * r - vup[2] * r;
595 m.vertex[4] = m.vertex[0] + vup[0] * r;
596 m.vertex[5] = m.vertex[1] + vup[1] * r;
597 m.vertex[6] = m.vertex[2] + vup[2] * r;
598 m.vertex[8] = m.vertex[0] + vright[0] * r;
599 m.vertex[9] = m.vertex[1] + vright[1] * r;
600 m.vertex[10] = m.vertex[2] + vright[2] * r;
609 r_refdef must be set before the first call
612 void R_RenderView (void)
614 entity_render_t *world = &cl_entities[0].render;
616 return; //Host_Error ("R_RenderView: NULL worldmodel");
618 // FIXME: move to client
620 R_TimeReport("mexplosion");
628 R_FarClip_Start(r_origin, vpn, 768.0f);
630 R_TimeReport("setup");
633 R_TimeReport("worldnode");
636 R_TimeReport("markentity");
638 R_MarkWorldLights(world);
639 R_TimeReport("marklights");
641 r_farclip = R_FarClip_Finish() + 256.0f;
643 R_Mesh_Start(r_farclip);
645 R_MeshQueue_BeginScene();
650 if (R_DrawBModelSky())
651 R_TimeReport("bmodelsky");
656 R_TimeReport("viewmodel");
659 R_SetupForWorldRendering(world);
660 R_PrepareSurfaces(world);
661 R_TimeReport("surfprep");
663 R_DrawSurfaces(world, SHADERSTAGE_SKY);
664 R_DrawSurfaces(world, SHADERSTAGE_NORMAL);
665 R_TimeReport("surfdraw");
667 if (r_drawportals.integer)
669 R_DrawPortals(world);
670 R_TimeReport("portals");
673 // don't let sound skip if going slow
674 if (!intimerefresh && !r_speeds.integer)
680 R_TimeReport("viewmodel");
684 R_TimeReport("models");
687 R_TimeReport("particles");
690 R_TimeReport("explosions");
692 R_MeshQueue_RenderTransparent();
693 R_TimeReport("addtrans");
696 R_TimeReport("coronas");
699 R_TimeReport("crosshair");
702 R_TimeReport("blendview");
704 R_MeshQueue_Render();
706 R_MeshQueue_EndScene();
709 R_TimeReport("meshfinish");