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 //static qboolean r_cache_thrash; // compatability
26 entity_render_t *currentrenderentity;
28 int r_framecount; // used for dlight push checking
32 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;
34 qboolean envmap; // true during envmap command capture
36 // LordHavoc: moved all code related to particles into r_part.c
37 //int particletexture; // little dot for particles
38 //int playertextures; // up to 16 color translated skins
48 //float r_world_matrix[16];
49 //float r_base_world_matrix[16];
56 mleaf_t *r_viewleaf, *r_oldviewleaf;
58 unsigned short d_lightstylevalue[256]; // 8.8 fraction of base light value
60 //cvar_t r_norefresh = {0, "r_norefresh","0"};
61 cvar_t r_drawentities = {0, "r_drawentities","1"};
62 cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1"};
63 cvar_t r_speeds = {0, "r_speeds","0"};
64 cvar_t r_fullbright = {0, "r_fullbright","0"};
65 //cvar_t r_lightmap = {0, "r_lightmap","0"};
66 cvar_t r_wateralpha = {CVAR_SAVE, "r_wateralpha","1"};
67 cvar_t r_dynamic = {CVAR_SAVE, "r_dynamic","1"};
68 cvar_t r_waterripple = {CVAR_SAVE, "r_waterripple","0"};
69 cvar_t r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1"};
71 cvar_t gl_lightmode = {CVAR_SAVE, "gl_lightmode", "1"}; // LordHavoc: overbright lighting
72 //cvar_t r_dynamicbothsides = {CVAR_SAVE, "r_dynamicbothsides", "1"}; // LordHavoc: can disable dynamic lighting of backfaces, but quake maps are weird so it doesn't always work right...
74 cvar_t gl_fogenable = {0, "gl_fogenable", "0"};
75 cvar_t gl_fogdensity = {0, "gl_fogdensity", "0.25"};
76 cvar_t gl_fogred = {0, "gl_fogred","0.3"};
77 cvar_t gl_foggreen = {0, "gl_foggreen","0.3"};
78 cvar_t gl_fogblue = {0, "gl_fogblue","0.3"};
79 cvar_t gl_fogstart = {0, "gl_fogstart", "0"};
80 cvar_t gl_fogend = {0, "gl_fogend","0"};
82 cvar_t r_ser = {CVAR_SAVE, "r_ser", "1"};
83 cvar_t gl_viewmodeldepthhack = {0, "gl_viewmodeldepthhack", "1"};
85 cvar_t r_multitexture = {0, "r_multitexture", "1"};
91 For program optimization
94 qboolean intimerefresh = 0;
95 static void R_TimeRefresh_f (void)
98 float start, stop, time;
101 start = Sys_DoubleTime ();
102 glDrawBuffer (GL_FRONT);
103 for (i = 0;i < 128;i++)
105 r_refdef.viewangles[0] = 0;
106 r_refdef.viewangles[1] = i/128.0*360.0;
107 r_refdef.viewangles[2] = 0;
110 glDrawBuffer (GL_BACK);
112 stop = Sys_DoubleTime ();
115 Con_Printf ("%f seconds (%f fps)\n", time, 128/time);
118 extern cvar_t r_drawportals;
120 int R_VisibleCullBox (vec3_t mins, vec3_t maxs)
123 mnode_t *nodestack[8192], *node;
126 if (R_CullBox(mins, maxs))
129 node = cl.worldmodel->nodes;
131 if (node->contents < 0)
133 if (((mleaf_t *)node)->visframe == r_framecount)
137 node = nodestack[--stack];
141 sides = BOX_ON_PLANE_SIDE(mins, maxs, node->plane);
143 // recurse down the contacted sides
148 // put second child on the stack for later examination
149 nodestack[stack++] = node->children[1];
150 node = node->children[0];
155 node = node->children[0];
160 node = node->children[1];
168 float fog_density, fog_red, fog_green, fog_blue;
170 qboolean oldgl_fogenable;
171 void R_SetupFog(void)
173 if (gamemode == GAME_NEHAHRA)
175 if (gl_fogenable.integer)
177 oldgl_fogenable = true;
178 fog_density = gl_fogdensity.value;
179 fog_red = gl_fogred.value;
180 fog_green = gl_foggreen.value;
181 fog_blue = gl_fogblue.value;
183 else if (oldgl_fogenable)
185 oldgl_fogenable = false;
194 fogcolor[0] = fog_red = bound(0.0f, fog_red , 1.0f);
195 fogcolor[1] = fog_green = bound(0.0f, fog_green, 1.0f);
196 fogcolor[2] = fog_blue = bound(0.0f, fog_blue , 1.0f);
201 fogdensity = -4000.0f / (fog_density * fog_density);
202 // fog color was already set
208 // FIXME: move this to client?
211 if (gamemode == GAME_NEHAHRA)
213 Cvar_Set("gl_fogenable", "0");
214 Cvar_Set("gl_fogdensity", "0.2");
215 Cvar_Set("gl_fogred", "0.3");
216 Cvar_Set("gl_foggreen", "0.3");
217 Cvar_Set("gl_fogblue", "0.3");
219 fog_density = fog_red = fog_green = fog_blue = 0.0f;
222 // FIXME: move this to client?
223 void FOG_registercvars(void)
225 if (gamemode == GAME_NEHAHRA)
227 Cvar_RegisterVariable (&gl_fogenable);
228 Cvar_RegisterVariable (&gl_fogdensity);
229 Cvar_RegisterVariable (&gl_fogred);
230 Cvar_RegisterVariable (&gl_foggreen);
231 Cvar_RegisterVariable (&gl_fogblue);
232 Cvar_RegisterVariable (&gl_fogstart);
233 Cvar_RegisterVariable (&gl_fogend);
237 void gl_main_start(void)
241 void gl_main_shutdown(void)
245 void gl_main_newmap(void)
250 void GL_Main_Init(void)
252 // FIXME: move this to client?
254 Cmd_AddCommand ("timerefresh", R_TimeRefresh_f);
255 Cvar_RegisterVariable (&r_drawentities);
256 Cvar_RegisterVariable (&r_drawviewmodel);
257 Cvar_RegisterVariable (&r_speeds);
258 Cvar_RegisterVariable (&gl_lightmode);
259 // Cvar_RegisterVariable (&r_dynamicwater);
260 // Cvar_RegisterVariable (&r_dynamicbothsides);
261 Cvar_RegisterVariable (&r_fullbrights);
262 Cvar_RegisterVariable (&r_wateralpha);
263 Cvar_RegisterVariable (&r_dynamic);
264 Cvar_RegisterVariable (&r_waterripple);
265 Cvar_RegisterVariable (&r_fullbright);
266 Cvar_RegisterVariable (&r_ser);
267 Cvar_RegisterVariable (&gl_viewmodeldepthhack);
268 Cvar_RegisterVariable (&r_multitexture);
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);
279 void CL_ParseEntityLump(char *entitystring);
284 for (i=0 ; i<256 ; i++)
285 d_lightstylevalue[i] = 264; // normal light value
288 if (cl.worldmodel->entities)
289 CL_ParseEntityLump(cl.worldmodel->entities);
293 extern void R_Textures_Init(void);
294 extern void Mod_RenderInit(void);
295 extern void GL_Draw_Init(void);
296 extern void GL_Main_Init(void);
297 extern void GL_Models_Init(void);
298 extern void R_Sky_Init(void);
299 extern void GL_Surf_Init(void);
300 extern void GL_Screen_Init(void);
301 extern void R_Crosshairs_Init(void);
302 extern void R_Light_Init(void);
303 extern void R_Particles_Init(void);
304 extern void R_Explosion_Init(void);
305 extern void R_Clip_Init(void);
306 extern void ui_init(void);
307 extern void gl_backend_init(void);
309 void Render_Init(void)
311 R_Modules_Shutdown();
335 extern char *ENGINE_EXTENSIONS;
338 gl_vendor = glGetString (GL_VENDOR);
339 Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
340 gl_renderer = glGetString (GL_RENDERER);
341 Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
343 gl_version = glGetString (GL_VERSION);
344 Con_Printf ("GL_VERSION: %s\n", gl_version);
345 gl_extensions = glGetString (GL_EXTENSIONS);
346 Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
348 // Con_Printf ("%s %s\n", gl_renderer, gl_version);
350 VID_CheckExtensions();
352 // LordHavoc: report supported extensions
353 Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
355 glCullFace(GL_FRONT);
356 glEnable(GL_TEXTURE_2D);
358 // glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
362 //==================================================================================
364 void R_Entity_Callback(void *data, void *junk)
366 ((entity_render_t *)data)->visframe = r_framecount;
369 static void R_MarkEntities (void)
374 if (!r_drawentities.integer)
377 for (i = 0;i < r_refdef.numentities;i++)
379 currentrenderentity = r_refdef.entities[i];
380 Mod_CheckLoaded(currentrenderentity->model);
382 // move view-relative models to where they should be
383 if (currentrenderentity->flags & RENDER_VIEWMODEL)
385 // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
386 currentrenderentity->flags -= RENDER_VIEWMODEL;
388 VectorCopy(currentrenderentity->origin, v);
389 currentrenderentity->origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
390 currentrenderentity->origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
391 currentrenderentity->origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
393 VectorAdd(currentrenderentity->angles, r_refdef.viewangles, currentrenderentity->angles);
396 if (currentrenderentity->angles[0] || currentrenderentity->angles[2])
398 VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->rotatedmins, currentrenderentity->mins);
399 VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->rotatedmaxs, currentrenderentity->maxs);
401 else if (currentrenderentity->angles[1])
403 VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->yawmins, currentrenderentity->mins);
404 VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->yawmaxs, currentrenderentity->maxs);
408 VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->normalmins, currentrenderentity->mins);
409 VectorMA(currentrenderentity->origin, currentrenderentity->scale, currentrenderentity->model->normalmaxs, currentrenderentity->maxs);
411 if (R_VisibleCullBox(currentrenderentity->mins, currentrenderentity->maxs))
414 R_LerpAnimation(currentrenderentity);
416 currentrenderentity->model->SERAddEntity();
418 currentrenderentity->visframe = r_framecount;
422 // only used if skyrendermasked, and normally returns false
423 int R_DrawBModelSky (void)
427 if (!r_drawentities.integer)
430 for (i = 0;i < r_refdef.numentities;i++)
432 currentrenderentity = r_refdef.entities[i];
433 if (currentrenderentity->visframe == r_framecount && currentrenderentity->model->DrawSky)
435 currentrenderentity->model->DrawSky();
442 void R_DrawModels (void)
446 if (!r_drawentities.integer)
449 for (i = 0;i < r_refdef.numentities;i++)
451 currentrenderentity = r_refdef.entities[i];
452 if (currentrenderentity->visframe == r_framecount && currentrenderentity->model->Draw)
453 currentrenderentity->model->Draw();
462 void R_DrawViewModel (void)
464 // FIXME: move these checks to client
465 if (!r_drawviewmodel.integer || chase_active.integer || envmap || !r_drawentities.integer || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
468 currentrenderentity = &cl.viewent.render;
469 Mod_CheckLoaded(currentrenderentity->model);
471 R_LerpAnimation(currentrenderentity);
473 // hack the depth range to prevent view model from poking into walls
474 if (gl_viewmodeldepthhack.integer)
477 glDepthRange (gldepthmin, gldepthmin + 0.3*(gldepthmax-gldepthmin));
479 currentrenderentity->model->Draw();
480 if (gl_viewmodeldepthhack.integer)
483 glDepthRange (gldepthmin, gldepthmax);
487 static void R_SetFrustum (void)
491 // LordHavoc: note to all quake engine coders, the special case for 90
492 // degrees assumed a square view (wrong), so I removed it, Quake2 has it
494 // rotate VPN right by FOV_X/2 degrees
495 RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
496 // rotate VPN left by FOV_X/2 degrees
497 RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
498 // rotate VPN up by FOV_X/2 degrees
499 RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
500 // rotate VPN down by FOV_X/2 degrees
501 RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
504 for (i=0 ; i<4 ; i++)
506 frustum[i].type = PLANE_ANYZ;
507 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
508 PlaneClassify(&frustum[i]);
517 static void R_SetupFrame (void)
519 // don't allow cheats in multiplayer
520 if (cl.maxclients > 1)
522 if (r_fullbright.integer != 0)
523 Cvar_Set ("r_fullbright", "0");
524 if (r_ambient.value != 0)
525 Cvar_Set ("r_ambient", "0");
527 if (r_multitexture.integer && gl_textureunits < 2)
528 Cvar_SetValue("r_multitexture", 0);
532 // build the transformation matrix for the given view angles
533 VectorCopy (r_refdef.vieworg, r_origin);
535 AngleVectors (r_refdef.viewangles, vpn, vright, vup);
538 r_oldviewleaf = r_viewleaf;
539 r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
541 // r_cache_thrash = false;
547 static void MYgluPerspective(GLdouble fovx, GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
551 xmax = zNear * tan( fovx * M_PI / 360.0 ) * aspect;
552 ymax = zNear * tan( fovy * M_PI / 360.0 );
554 if (r_viewleaf->contents != CONTENTS_EMPTY && r_viewleaf->contents != CONTENTS_SOLID)
556 xmax *= (sin(cl.time * 4.7) * 0.03 + 0.97);
557 ymax *= (sin(cl.time * 3.0) * 0.03 + 0.97);
560 glFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar );
569 static void R_SetupGL (void)
571 if (!r_render.integer)
574 // glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
577 glDepthFunc (GL_LEQUAL);
579 glDepthRange (gldepthmin, gldepthmax);
581 // update farclip based on previous frame
582 r_farclip = r_newfarclip;
585 glMatrixMode(GL_PROJECTION);
588 // y is weird beause OpenGL is bottom to top, we use top to bottom
589 glViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);
590 // yfov = 2*atan((float)r_refdef.height/r_refdef.width)*180/M_PI;
591 MYgluPerspective (r_refdef.fov_x, r_refdef.fov_y, r_refdef.width/r_refdef.height, 4, r_farclip);
593 glCullFace(GL_FRONT);
595 glMatrixMode(GL_MODELVIEW);
598 glRotatef (-90, 1, 0, 0); // put Z going up
599 glRotatef (90, 0, 0, 1); // put Z going up
600 glRotatef (-r_refdef.viewangles[2], 1, 0, 0);
601 glRotatef (-r_refdef.viewangles[0], 0, 1, 0);
602 glRotatef (-r_refdef.viewangles[1], 0, 0, 1);
603 glTranslatef (-r_refdef.vieworg[0], -r_refdef.vieworg[1], -r_refdef.vieworg[2]);
605 // glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
610 // if (gl_cull.integer)
611 glEnable(GL_CULL_FACE);
613 // glDisable(GL_CULL_FACE);
615 glEnable(GL_BLEND); // was Disable
616 glEnable(GL_DEPTH_TEST);
620 static void R_BlendView(void)
622 if (!r_render.integer)
625 if (r_refdef.viewblend[3] < 0.01f)
628 glMatrixMode(GL_PROJECTION);
630 glOrtho (0, 1, 1, 0, -99999, 99999);
631 glMatrixMode(GL_MODELVIEW);
633 glDisable (GL_DEPTH_TEST);
634 glDisable (GL_CULL_FACE);
635 glDisable(GL_TEXTURE_2D);
637 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
638 glBegin (GL_TRIANGLES);
640 glColor4f (r_refdef.viewblend[0] * 0.5f, r_refdef.viewblend[1] * 0.5f, r_refdef.viewblend[2] * 0.5f, r_refdef.viewblend[3]);
642 glColor4fv (r_refdef.viewblend);
648 glEnable (GL_CULL_FACE);
649 glEnable (GL_DEPTH_TEST);
651 glEnable(GL_TEXTURE_2D);
658 r_refdef must be set before the first call
661 void R_RenderView (void)
664 Host_Error ("R_RenderView: NULL worldmodel");
666 // FIXME: move to client
668 R_TimeReport("mexplosion");
680 R_TimeReport("setup");
683 R_TimeReport("worldnode");
686 R_TimeReport("markentity");
691 R_TimeReport("hiddensurf");
695 R_TimeReport("marklights");
699 if (R_DrawBModelSky())
700 R_TimeReport("bmodelsky");
705 R_TimeReport("viewmodel");
708 R_SetupForWorldRendering();
710 R_TimeReport("surfprep");
712 R_DrawSurfaces(SHADERSTAGE_SKY);
713 R_DrawSurfaces(SHADERSTAGE_NORMAL);
714 R_DrawSurfaces(SHADERSTAGE_FOG);
715 R_TimeReport("surfdraw");
717 if (r_drawportals.integer)
720 R_TimeReport("portals");
723 // don't let sound skip if going slow
724 if (!intimerefresh && !r_speeds.integer)
730 R_TimeReport("viewmodel");
734 R_TimeReport("models");
737 R_TimeReport("particles");
740 R_TimeReport("explosions");
742 // draw transparent meshs
743 R_Mesh_AddTransparent();
744 R_TimeReport("addtrans");
747 R_TimeReport("coronas");
749 // render any queued meshs
751 R_TimeReport("meshrender");
754 R_TimeReport("blendview");
756 //Mem_CheckSentinelsGlobal();
757 //R_TimeReport("memtest");