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 matrix4x4_t r_identitymatrix;
31 int c_alias_polys, c_light_polys, c_faces, c_nodes, c_leafs, c_models, c_bmodels, c_sprites, c_particles, c_dlights;
33 // true during envmap command capture
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_shadows = {CVAR_SAVE, "r_shadows", "1"};
55 cvar_t r_staticworldlights = {0, "r_staticworldlights", "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"};
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"};
70 cvar_t r_textureunits = {0, "r_textureunits", "32"};
72 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
75 for (i = 0;i < verts;i++)
86 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
89 for (i = 0;i < verts;i++)
103 For program optimization
106 qboolean intimerefresh = 0;
107 static void R_TimeRefresh_f (void)
110 float start, stop, time;
113 start = Sys_DoubleTime ();
114 for (i = 0;i < 128;i++)
116 r_refdef.viewangles[0] = 0;
117 r_refdef.viewangles[1] = i/128.0*360.0;
118 r_refdef.viewangles[2] = 0;
122 stop = Sys_DoubleTime ();
125 Con_Printf ("%f seconds (%f fps)\n", time, 128/time);
130 float fog_density, fog_red, fog_green, fog_blue;
132 qboolean oldgl_fogenable;
133 void R_SetupFog(void)
135 if (gamemode == GAME_NEHAHRA)
137 if (gl_fogenable.integer)
139 oldgl_fogenable = true;
140 fog_density = gl_fogdensity.value;
141 fog_red = gl_fogred.value;
142 fog_green = gl_foggreen.value;
143 fog_blue = gl_fogblue.value;
145 else if (oldgl_fogenable)
147 oldgl_fogenable = false;
156 fogcolor[0] = fog_red = bound(0.0f, fog_red , 1.0f);
157 fogcolor[1] = fog_green = bound(0.0f, fog_green, 1.0f);
158 fogcolor[2] = fog_blue = bound(0.0f, fog_blue , 1.0f);
163 fogdensity = -4000.0f / (fog_density * fog_density);
164 // fog color was already set
170 // FIXME: move this to client?
173 if (gamemode == GAME_NEHAHRA)
175 Cvar_Set("gl_fogenable", "0");
176 Cvar_Set("gl_fogdensity", "0.2");
177 Cvar_Set("gl_fogred", "0.3");
178 Cvar_Set("gl_foggreen", "0.3");
179 Cvar_Set("gl_fogblue", "0.3");
181 fog_density = fog_red = fog_green = fog_blue = 0.0f;
184 // FIXME: move this to client?
185 void FOG_registercvars(void)
187 if (gamemode == GAME_NEHAHRA)
189 Cvar_RegisterVariable (&gl_fogenable);
190 Cvar_RegisterVariable (&gl_fogdensity);
191 Cvar_RegisterVariable (&gl_fogred);
192 Cvar_RegisterVariable (&gl_foggreen);
193 Cvar_RegisterVariable (&gl_fogblue);
194 Cvar_RegisterVariable (&gl_fogstart);
195 Cvar_RegisterVariable (&gl_fogend);
199 void gl_main_start(void)
203 void gl_main_shutdown(void)
207 extern void CL_ParseEntityLump(char *entitystring);
208 void gl_main_newmap(void)
210 if (cl.worldmodel && cl.worldmodel->entities)
211 CL_ParseEntityLump(cl.worldmodel->entities);
215 void GL_Main_Init(void)
217 Matrix4x4_CreateIdentity(&r_identitymatrix);
218 // FIXME: move this to client?
220 Cmd_AddCommand ("timerefresh", R_TimeRefresh_f);
221 Cvar_RegisterVariable (&r_drawentities);
222 Cvar_RegisterVariable (&r_drawviewmodel);
223 Cvar_RegisterVariable (&r_shadows);
224 Cvar_RegisterVariable (&r_staticworldlights);
225 Cvar_RegisterVariable (&r_speeds);
226 Cvar_RegisterVariable (&r_fullbrights);
227 Cvar_RegisterVariable (&r_wateralpha);
228 Cvar_RegisterVariable (&r_dynamic);
229 Cvar_RegisterVariable (&r_fullbright);
230 Cvar_RegisterVariable (&r_textureunits);
231 if (gamemode == GAME_NEHAHRA)
232 Cvar_SetValue("r_fullbrights", 0);
233 R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
236 vec3_t r_farclip_origin;
237 vec3_t r_farclip_direction;
238 vec_t r_farclip_directiondist;
239 vec_t r_farclip_meshfarclip;
240 int r_farclip_directionbit0;
241 int r_farclip_directionbit1;
242 int r_farclip_directionbit2;
244 // start a farclip measuring session
245 void R_FarClip_Start(vec3_t origin, vec3_t direction, vec_t startfarclip)
247 VectorCopy(origin, r_farclip_origin);
248 VectorCopy(direction, r_farclip_direction);
249 r_farclip_directiondist = DotProduct(r_farclip_origin, r_farclip_direction);
250 r_farclip_directionbit0 = r_farclip_direction[0] < 0;
251 r_farclip_directionbit1 = r_farclip_direction[1] < 0;
252 r_farclip_directionbit2 = r_farclip_direction[2] < 0;
253 r_farclip_meshfarclip = r_farclip_directiondist + startfarclip;
256 // enlarge farclip to accomodate box
257 void R_FarClip_Box(vec3_t mins, vec3_t maxs)
260 d = (r_farclip_directionbit0 ? mins[0] : maxs[0]) * r_farclip_direction[0]
261 + (r_farclip_directionbit1 ? mins[1] : maxs[1]) * r_farclip_direction[1]
262 + (r_farclip_directionbit2 ? mins[2] : maxs[2]) * r_farclip_direction[2];
263 if (r_farclip_meshfarclip < d)
264 r_farclip_meshfarclip = d;
267 // return farclip value
268 float R_FarClip_Finish(void)
270 return r_farclip_meshfarclip - r_farclip_directiondist;
283 extern void R_Textures_Init(void);
284 extern void Mod_RenderInit(void);
285 extern void GL_Draw_Init(void);
286 extern void GL_Main_Init(void);
287 extern void R_Shadow_Init(void);
288 extern void GL_Models_Init(void);
289 extern void R_Sky_Init(void);
290 extern void GL_Surf_Init(void);
291 extern void R_Crosshairs_Init(void);
292 extern void R_Light_Init(void);
293 extern void R_Particles_Init(void);
294 extern void R_Explosion_Init(void);
295 extern void ui_init(void);
296 extern void gl_backend_init(void);
297 extern void Sbar_Init(void);
299 void Render_Init(void)
324 extern char *ENGINE_EXTENSIONS;
327 VID_CheckExtensions();
329 // LordHavoc: report supported extensions
330 Con_Printf ("\nengine extensions: %s\n", ENGINE_EXTENSIONS);
333 int R_CullBox(const vec3_t mins, const vec3_t maxs)
337 for (i = 0;i < 4;i++)
344 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
348 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
352 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
356 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
360 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
364 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
368 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
372 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
380 int PVS_CullBox(const vec3_t mins, const vec3_t maxs)
383 mnode_t *node, *stack[4096];
385 stack[stackpos++] = cl.worldmodel->nodes;
388 node = stack[--stackpos];
389 if (node->contents < 0)
391 if (((mleaf_t *)node)->pvsframe == cl.worldmodel->pvsframecount)
396 sides = BoxOnPlaneSide(mins, maxs, node->plane);
397 if (sides & 2 && stackpos < 4096)
398 stack[stackpos++] = node->children[1];
399 if (sides & 1 && stackpos < 4096)
400 stack[stackpos++] = node->children[0];
406 int VIS_CullBox(const vec3_t mins, const vec3_t maxs)
409 mnode_t *node, *stack[4096];
410 if (R_CullBox(mins, maxs))
413 stack[stackpos++] = cl.worldmodel->nodes;
416 node = stack[--stackpos];
417 if (node->contents < 0)
419 if (((mleaf_t *)node)->visframe == r_framecount)
424 sides = BoxOnPlaneSide(mins, maxs, node->plane);
425 if (sides & 2 && stackpos < 4096)
426 stack[stackpos++] = node->children[1];
427 if (sides & 1 && stackpos < 4096)
428 stack[stackpos++] = node->children[0];
434 int R_CullSphere(const vec3_t origin, vec_t radius)
436 return (DotProduct(frustum[0].normal, origin) + radius < frustum[0].dist
437 || DotProduct(frustum[1].normal, origin) + radius < frustum[1].dist
438 || DotProduct(frustum[2].normal, origin) + radius < frustum[2].dist
439 || DotProduct(frustum[3].normal, origin) + radius < frustum[3].dist);
442 int PVS_CullSphere(const vec3_t origin, vec_t radius)
445 mnode_t *node, *stack[4096];
448 stack[stackpos++] = cl.worldmodel->nodes;
451 node = stack[--stackpos];
452 if (node->contents < 0)
454 if (((mleaf_t *)node)->pvsframe == cl.worldmodel->pvsframecount)
459 dist = PlaneDiff(origin, node->plane);
461 stack[stackpos++] = node->children[1];
463 stack[stackpos++] = node->children[0];
469 int VIS_CullSphere(const vec3_t origin, vec_t radius)
472 mnode_t *node, *stack[4096];
474 if (R_CullSphere(origin, radius))
477 stack[stackpos++] = cl.worldmodel->nodes;
480 node = stack[--stackpos];
481 if (node->contents < 0)
483 if (((mleaf_t *)node)->visframe == r_framecount)
488 dist = PlaneDiff(origin, node->plane);
490 stack[stackpos++] = node->children[1];
492 stack[stackpos++] = node->children[0];
499 //==================================================================================
501 static void R_MarkEntities (void)
505 entity_render_t *ent;
507 ent = &cl_entities[0].render;
508 Matrix4x4_CreateIdentity(&ent->matrix);
509 Matrix4x4_CreateIdentity(&ent->inversematrix);
512 R_FarClip_Box(cl.worldmodel->normalmins, cl.worldmodel->normalmaxs);
514 if (!r_drawentities.integer)
517 for (i = 0;i < r_refdef.numentities;i++)
519 ent = r_refdef.entities[i];
520 Mod_CheckLoaded(ent->model);
522 // move view-relative models to where they should be
523 if (ent->flags & RENDER_VIEWMODEL)
525 // remove flag so it will not be repeated incase RelinkEntities is not called again for a while
526 ent->flags -= RENDER_VIEWMODEL;
528 VectorCopy(ent->origin, v);
529 ent->origin[0] = v[0] * vpn[0] + v[1] * vright[0] + v[2] * vup[0] + r_origin[0];
530 ent->origin[1] = v[0] * vpn[1] + v[1] * vright[1] + v[2] * vup[1] + r_origin[1];
531 ent->origin[2] = v[0] * vpn[2] + v[1] * vright[2] + v[2] * vup[2] + r_origin[2];
533 VectorAdd(ent->angles, r_refdef.viewangles, ent->angles);
536 VectorCopy(ent->angles, v);
537 if (!ent->model || ent->model->type != mod_brush)
539 Matrix4x4_CreateFromQuakeEntity(&ent->matrix, ent->origin[0], ent->origin[1], ent->origin[2], v[0], v[1], v[2], ent->scale);
540 Matrix4x4_Invert_Simple(&ent->inversematrix, &ent->matrix);
541 R_LerpAnimation(ent);
542 R_UpdateEntLights(ent);
543 if (!VIS_CullSphere(ent->origin, ent->model->radius * ent->scale)
544 && !VIS_CullBox(ent->mins, ent->maxs))
546 ent->visframe = r_framecount;
547 R_FarClip_Box(ent->mins, ent->maxs);
552 // only used if skyrendermasked, and normally returns false
553 int R_DrawBrushModelsSky (void)
556 entity_render_t *ent;
558 if (!r_drawentities.integer)
562 for (i = 0;i < r_refdef.numentities;i++)
564 ent = r_refdef.entities[i];
565 if (ent->visframe == r_framecount && ent->model && ent->model->DrawSky)
567 ent->model->DrawSky(ent);
579 void R_DrawViewModel (void)
581 entity_render_t *ent;
583 // FIXME: move these checks to client
584 if (!r_drawviewmodel.integer || chase_active.integer || envmap || !r_drawentities.integer || cl.items & IT_INVISIBILITY || cl.stats[STAT_HEALTH] <= 0 || !cl.viewent.render.model)
587 ent = &cl.viewent.render;
588 Mod_CheckLoaded(ent->model);
589 R_LerpAnimation(ent);
590 Matrix4x4_CreateFromQuakeEntity(&ent->matrix, ent->origin[0], ent->origin[1], ent->origin[2], -ent->angles[0], ent->angles[1], ent->angles[2], ent->scale);
591 Matrix4x4_Invert_Simple(&ent->inversematrix, &ent->matrix);
592 R_UpdateEntLights(ent);
593 ent->model->Draw(ent);
596 void R_DrawNoModel(entity_render_t *ent);
600 entity_render_t *ent;
602 if (!r_drawentities.integer)
606 for (i = 0;i < r_refdef.numentities;i++)
608 ent = r_refdef.entities[i];
609 if (ent->visframe == r_framecount)
611 if (ent->model && ent->model->Draw != NULL)
612 ent->model->Draw(ent);
619 void R_DrawFakeShadows (void)
622 entity_render_t *ent;
624 ent = &cl_entities[0].render;
625 if (ent->model && ent->model->DrawFakeShadow)
626 ent->model->DrawFakeShadow(ent);
628 if (!r_drawentities.integer)
630 for (i = 0;i < r_refdef.numentities;i++)
632 ent = r_refdef.entities[i];
633 if (ent->model && ent->model->DrawFakeShadow)
634 ent->model->DrawFakeShadow(ent);
638 #include "r_shadow.h"
640 void R_TestAndDrawShadowVolume(entity_render_t *ent, vec3_t lightorigin, float cullradius, float lightradius)
643 vec3_t p, p2, temp, relativelightorigin, mins, maxs;
644 float dist, projectdistance;
646 if (ent->model && ent->model->DrawShadowVolume)
648 temp[0] = bound(ent->mins[0], lightorigin[0], ent->maxs[0]) - lightorigin[0];
649 temp[1] = bound(ent->mins[1], lightorigin[1], ent->maxs[1]) - lightorigin[1];
650 temp[2] = bound(ent->mins[2], lightorigin[2], ent->maxs[2]) - lightorigin[2];
651 dist = DotProduct(temp, temp);
652 if (dist < cullradius * cullradius)
654 projectdistance = cullradius - sqrt(dist);
655 // calculate projected bounding box and decide if it is on-screen
656 VectorCopy(ent->mins, mins);
657 VectorCopy(ent->maxs, maxs);
658 for (i = 0;i < 8;i++)
660 p[0] = i & 1 ? ent->maxs[0] : ent->mins[0];
661 p[1] = i & 2 ? ent->maxs[1] : ent->mins[1];
662 p[2] = i & 4 ? ent->maxs[2] : ent->mins[2];
663 VectorSubtract(p, lightorigin, temp);
664 dist = projectdistance / sqrt(DotProduct(temp, temp));
665 VectorMA(p, dist, temp, p2);
666 if (mins[0] > p2[0]) mins[0] = p2[0];if (maxs[0] < p2[0]) maxs[0] = p2[0];
667 if (mins[1] > p2[1]) mins[1] = p2[1];if (maxs[1] < p2[1]) maxs[1] = p2[1];
668 if (mins[2] > p2[2]) mins[2] = p2[2];if (maxs[2] < p2[2]) maxs[2] = p2[2];
670 if (!VIS_CullBox(mins, maxs))
672 Matrix4x4_Transform(&ent->inversematrix, lightorigin, relativelightorigin);
673 ent->model->DrawShadowVolume (ent, relativelightorigin, lightradius);
679 void R_DrawWorldLightShadowVolume(mlight_t *sl)
682 R_Mesh_Matrix(&cl_entities[0].render.matrix);
683 for (mesh = sl->shadowvolume;mesh;mesh = mesh->next)
685 R_Mesh_ResizeCheck(mesh->numverts);
686 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
687 R_Shadow_RenderVolume(mesh->numverts, mesh->numtriangles, mesh->elements);
691 #define SHADOWSPHERE_SEGMENTS 16
693 shadowmesh_t *shadowsphere;
694 void R_CreateShadowSphere(void)
697 vec3_t angles, angles2, angles3, angles4;
699 shadowsphere = Mod_ShadowMesh_Begin(zonemempool);
700 for (i = 0;i < SHADOWSPHERE_SEGMENTS / 2;i++)
702 for (j = 0;j < SHADOWSPHERE_SEGMENTS;j++)
704 angles[0] = (i * 360.0f / SHADOWSPHERE_SEGMENTS) + 90.0f;
705 angles[1] = j * 360.0f / SHADOWSPHERE_SEGMENTS;
707 VectorCopy(angles, angles2);
708 VectorCopy(angles, angles3);
709 VectorCopy(angles, angles4);
710 angles2[1] += 360.0f / SHADOWSPHERE_SEGMENTS;
711 angles3[0] += 360.0f / SHADOWSPHERE_SEGMENTS;
712 angles3[1] += 360.0f / SHADOWSPHERE_SEGMENTS;
713 angles4[0] += 360.0f / SHADOWSPHERE_SEGMENTS;
714 AngleVectorsFLU(angles, verts, NULL, NULL);
715 AngleVectorsFLU(angles2, verts + 9, NULL, NULL);
716 AngleVectorsFLU(angles3, verts + 6, NULL, NULL);
717 AngleVectorsFLU(angles4, verts + 3, NULL, NULL);
718 VectorScale(&verts[0], 1.0f, &verts[0]);
719 VectorScale(&verts[3], 1.0f, &verts[3]);
720 VectorScale(&verts[6], 1.0f, &verts[6]);
721 VectorScale(&verts[9], 1.0f, &verts[9]);
722 Mod_ShadowMesh_AddPolygon(zonemempool, shadowsphere, 4, verts);
725 shadowsphere = Mod_ShadowMesh_Finish(zonemempool, shadowsphere);
729 void R_DrawShadowSphere(vec3_t origin, float cullradius, float lightradius)
734 R_CreateShadowSphere();
735 Matrix4x4_CreateScale(&matrix, lightradius);
736 Matrix4x4_ConcatTranslate(&matrix, origin[0], origin[1], origin[2]);
737 R_Mesh_Matrix(&matrix);
738 for (mesh = shadowsphere;mesh;mesh = mesh->next)
740 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
741 R_Shadow_RenderVolume(mesh->numverts, mesh->numtriangles, mesh->elements);
743 Matrix4x4_CreateScale(&matrix, -cullradius);
744 Matrix4x4_ConcatTranslate(&matrix, origin[0], origin[1], origin[2]);
745 R_Mesh_Matrix(&matrix);
746 for (mesh = shadowsphere;mesh;mesh = mesh->next)
748 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
749 R_Shadow_RenderVolume(mesh->numverts, mesh->numtriangles, mesh->elements);
753 void R_ShadowVolumeLighting (int visiblevolumes)
756 entity_render_t *ent;
758 float f, lightradius, cullradius;
759 vec3_t relativelightorigin, relativeeyeorigin, lightcolor;
766 memset(&m, 0, sizeof(m));
767 m.blendfunc1 = GL_ONE;
768 m.blendfunc2 = GL_ONE;
770 GL_Color(0.0 * r_colorscale, 0.0125 * r_colorscale, 0.1 * r_colorscale, 1);
773 R_Shadow_Stage_Begin();
774 for (lnum = 0, sl = cl.worldmodel->lights;lnum < cl.worldmodel->numlights;lnum++, sl++)
776 if (d_lightstylevalue[sl->style] <= 0)
778 if (r_light_debuglight.integer >= 0 && lnum != r_light_debuglight.integer)
780 cullradius = sl->cullradius;
781 lightradius = sl->lightradius;
782 if (VIS_CullBox(sl->mins, sl->maxs) || VIS_CullSphere(sl->origin, cullradius))
785 f = d_lightstylevalue[sl->style] * (1.0f / 32768.0f);
786 VectorScale(sl->light, f, lightcolor);
789 R_Shadow_Stage_ShadowVolumes();
790 if (sl->shadowvolume && r_staticworldlights.integer)
791 R_DrawWorldLightShadowVolume(sl);
794 //R_DrawShadowSphere(sl->origin, cullradius, lightradius * 2);
795 R_TestAndDrawShadowVolume(&cl_entities[0].render, sl->origin, cullradius, lightradius);
797 if (r_drawentities.integer)
799 for (i = 0;i < r_refdef.numentities;i++)
801 ent = r_refdef.entities[i];
802 if (ent->maxs[0] >= sl->mins[0] && ent->mins[0] <= sl->maxs[0]
803 && ent->maxs[1] >= sl->mins[1] && ent->mins[1] <= sl->maxs[1]
804 && ent->maxs[2] >= sl->mins[2] && ent->mins[2] <= sl->maxs[2]
805 && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
806 R_TestAndDrawShadowVolume(r_refdef.entities[i], sl->origin, cullradius, lightradius);
812 R_Shadow_Stage_Light();
813 ent = &cl_entities[0].render;
814 if (ent->model && ent->model->DrawLight)
816 Matrix4x4_Transform(&ent->inversematrix, sl->origin, relativelightorigin);
817 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
818 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, sl->distbias, sl->subtract, lightcolor);
820 if (r_drawentities.integer)
822 for (i = 0;i < r_refdef.numentities;i++)
824 ent = r_refdef.entities[i];
825 if (ent->visframe == r_framecount && ent->model && ent->model->DrawLight
826 && ent->maxs[0] >= sl->mins[0] && ent->mins[0] <= sl->maxs[0]
827 && ent->maxs[1] >= sl->mins[1] && ent->mins[1] <= sl->maxs[1]
828 && ent->maxs[2] >= sl->mins[2] && ent->mins[2] <= sl->maxs[2]
829 && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
831 Matrix4x4_Transform(&ent->inversematrix, sl->origin, relativelightorigin);
832 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
833 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, sl->distbias, sl->subtract, lightcolor);
838 R_Shadow_Stage_EraseShadowVolumes();
839 if (sl->shadowvolume && r_staticworldlights.integer)
840 R_DrawWorldLightShadowVolume(sl);
843 //R_DrawShadowSphere(sl->origin, cullradius, lightradius * 2);
844 R_TestAndDrawShadowVolume(&cl_entities[0].render, sl->origin, cullradius, lightradius);
846 if (r_drawentities.integer)
848 for (i = 0;i < r_refdef.numentities;i++)
850 ent = r_refdef.entities[i];
851 if (ent->maxs[0] >= sl->mins[0] && ent->mins[0] <= sl->maxs[0]
852 && ent->maxs[1] >= sl->mins[1] && ent->mins[1] <= sl->maxs[1]
853 && ent->maxs[2] >= sl->mins[2] && ent->mins[2] <= sl->maxs[2]
854 && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
855 R_TestAndDrawShadowVolume(r_refdef.entities[i], sl->origin, cullradius, lightradius);
860 for (lnum = 0, rd = r_dlight;lnum < r_numdlights;lnum++, rd++)
862 cullradius = rd->cullradius;
863 lightradius = rd->cullradius;
864 if (VIS_CullSphere(rd->origin, cullradius))
867 VectorScale(rd->light, 16.0f, lightcolor);
870 R_Shadow_Stage_ShadowVolumes();
871 R_TestAndDrawShadowVolume(&cl_entities[0].render, rd->origin, cullradius, lightradius);
872 if (r_drawentities.integer)
874 for (i = 0;i < r_refdef.numentities;i++)
876 ent = r_refdef.entities[i];
877 if (ent != rd->ent && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
878 R_TestAndDrawShadowVolume(ent, rd->origin, cullradius, lightradius);
884 R_Shadow_Stage_Light();
885 ent = &cl_entities[0].render;
886 if (ent->model && ent->model->DrawLight)
888 Matrix4x4_Transform(&ent->inversematrix, rd->origin, relativelightorigin);
889 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
890 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, LIGHTOFFSET, rd->subtract, lightcolor);
892 if (r_drawentities.integer)
894 for (i = 0;i < r_refdef.numentities;i++)
896 ent = r_refdef.entities[i];
897 if (ent->visframe == r_framecount && ent->model && ent->model->DrawLight
898 && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
900 Matrix4x4_Transform(&ent->inversematrix, rd->origin, relativelightorigin);
901 Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
902 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, LIGHTOFFSET, rd->subtract, lightcolor);
907 R_Shadow_Stage_EraseShadowVolumes();
908 R_TestAndDrawShadowVolume(&cl_entities[0].render, rd->origin, cullradius, lightradius);
909 if (r_drawentities.integer)
911 for (i = 0;i < r_refdef.numentities;i++)
913 ent = r_refdef.entities[i];
914 if (ent != rd->ent && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
915 R_TestAndDrawShadowVolume(ent, rd->origin, cullradius, lightradius);
922 R_Shadow_Stage_End();
925 static void R_SetFrustum (void)
929 // LordHavoc: note to all quake engine coders, the special case for 90
930 // degrees assumed a square view (wrong), so I removed it, Quake2 has it
932 // rotate VPN right by FOV_X/2 degrees
933 RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
934 // rotate VPN left by FOV_X/2 degrees
935 RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
936 // rotate VPN up by FOV_X/2 degrees
937 RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
938 // rotate VPN down by FOV_X/2 degrees
939 RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
941 for (i = 0;i < 4;i++)
943 frustum[i].type = PLANE_ANYZ;
944 frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
945 PlaneClassify(&frustum[i]);
954 static void R_SetupFrame (void)
956 // don't allow cheats in multiplayer
957 if (cl.maxclients > 1)
959 if (r_fullbright.integer != 0)
960 Cvar_Set ("r_fullbright", "0");
961 if (r_ambient.value != 0)
962 Cvar_Set ("r_ambient", "0");
967 // build the transformation matrix for the given view angles
968 VectorCopy (r_refdef.vieworg, r_origin);
970 AngleVectors (r_refdef.viewangles, vpn, vright, vup);
976 static void R_BlendView(void)
981 if (r_refdef.viewblend[3] < 0.01f)
984 memset(&m, 0, sizeof(m));
985 m.blendfunc1 = GL_SRC_ALPHA;
986 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
987 m.depthdisable = true; // magic
988 R_Mesh_Matrix(&r_identitymatrix);
992 varray_vertex[0] = r_origin[0] + vpn[0] * 1.5 - vright[0] * r - vup[0] * r;
993 varray_vertex[1] = r_origin[1] + vpn[1] * 1.5 - vright[1] * r - vup[1] * r;
994 varray_vertex[2] = r_origin[2] + vpn[2] * 1.5 - vright[2] * r - vup[2] * r;
996 varray_vertex[4] = varray_vertex[0] + vup[0] * r;
997 varray_vertex[5] = varray_vertex[1] + vup[1] * r;
998 varray_vertex[6] = varray_vertex[2] + vup[2] * r;
999 varray_vertex[8] = varray_vertex[0] + vright[0] * r;
1000 varray_vertex[9] = varray_vertex[1] + vright[1] * r;
1001 varray_vertex[10] = varray_vertex[2] + vright[2] * r;
1002 GL_Color(r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
1003 R_Mesh_Draw(3, 1, polygonelements);
1010 r_refdef must be set before the first call
1013 void R_RenderView (void)
1015 entity_render_t *world;
1016 if (!r_refdef.entities/* || !cl.worldmodel*/)
1017 return; //Host_Error ("R_RenderView: NULL worldmodel");
1019 world = &cl_entities[0].render;
1021 // FIXME: move to client
1023 R_TimeReport("mexplosion");
1031 R_TimeReport("setup");
1033 R_WorldVisibility(world);
1034 R_TimeReport("worldvis");
1036 R_FarClip_Start(r_origin, vpn, 768.0f);
1038 r_farclip = R_FarClip_Finish() + 256.0f;
1039 R_TimeReport("markentity");
1041 GL_SetupView_ViewPort(r_refdef.x, r_refdef.y, r_refdef.width, r_refdef.height);
1042 if (r_shadows.integer == 3)
1043 GL_SetupView_Mode_PerspectiveInfiniteFarClip(r_refdef.fov_x, r_refdef.fov_y, 1.0f);
1045 GL_SetupView_Mode_Perspective(r_refdef.fov_x, r_refdef.fov_y, 1.0f, r_farclip);
1046 GL_SetupView_Orientation_FromEntity (r_refdef.vieworg, r_refdef.viewangles);
1047 qglDepthFunc(GL_LEQUAL);
1050 R_MeshQueue_BeginScene();
1052 if (r_shadows.integer == 3)
1054 if (!gl_texturecubemap)
1056 Con_Printf("Cubemap texture support not detected, turning off r_shadows 3\n");
1057 Cvar_SetValueQuick(&r_shadows, 0);
1059 else if (!gl_dot3arb)
1061 Con_Printf("Bumpmapping support not detected, turning off r_shadows 3\n");
1062 Cvar_SetValueQuick(&r_shadows, 0);
1064 else if (!gl_stencil)
1066 Con_Printf("Stencil not enabled, turning off r_shadows 3, please type vid_stencil 1;vid_restart and try again\n");
1067 Cvar_SetValueQuick(&r_shadows, 0);
1069 else if (!gl_combine.integer)
1071 Con_Printf("Combine disabled, please turn on gl_combine, turning off r_shadows 3\n");
1072 Cvar_SetValueQuick(&r_shadows, 0);
1076 if (R_DrawBrushModelsSky())
1077 R_TimeReport("bmodelsky");
1079 // must occur early because it can draw sky
1081 R_TimeReport("world");
1083 // don't let sound skip if going slow
1084 if (!intimerefresh && !r_speeds.integer)
1087 R_DrawModels(r_shadows.integer == 3);
1088 R_TimeReport("models");
1090 if (r_shadows.integer == 1)
1092 R_DrawFakeShadows();
1093 R_TimeReport("fakeshadow");
1096 if (r_shadows.integer == 3)
1098 R_ShadowVolumeLighting(false);
1099 R_TimeReport("dynlight");
1103 R_TimeReport("particles");
1106 R_TimeReport("explosions");
1108 R_MeshQueue_RenderTransparent();
1109 R_TimeReport("drawtrans");
1112 R_TimeReport("coronas");
1114 R_DrawWorldCrosshair();
1115 R_TimeReport("crosshair");
1118 R_TimeReport("blendview");
1120 R_MeshQueue_Render();
1121 R_MeshQueue_EndScene();
1122 if (r_shadows.integer == 2)
1124 R_ShadowVolumeLighting(true);
1125 R_TimeReport("shadowvolume");
1128 R_TimeReport("meshfinish");
1132 void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
1135 float *v, *c, f1, f2, diff[3];
1137 m.blendfunc1 = GL_SRC_ALPHA;
1138 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1139 R_Mesh_Matrix(&r_identitymatrix);
1142 varray_vertex[ 0] = mins[0];varray_vertex[ 1] = mins[1];varray_vertex[ 2] = mins[2];
1143 varray_vertex[ 4] = maxs[0];varray_vertex[ 5] = mins[1];varray_vertex[ 6] = mins[2];
1144 varray_vertex[ 8] = mins[0];varray_vertex[ 9] = maxs[1];varray_vertex[10] = mins[2];
1145 varray_vertex[12] = maxs[0];varray_vertex[13] = maxs[1];varray_vertex[14] = mins[2];
1146 varray_vertex[16] = mins[0];varray_vertex[17] = mins[1];varray_vertex[18] = maxs[2];
1147 varray_vertex[20] = maxs[0];varray_vertex[21] = mins[1];varray_vertex[22] = maxs[2];
1148 varray_vertex[24] = mins[0];varray_vertex[25] = maxs[1];varray_vertex[26] = maxs[2];
1149 varray_vertex[28] = maxs[0];varray_vertex[29] = maxs[1];varray_vertex[30] = maxs[2];
1150 R_FillColors(varray_color, 8, cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
1153 for (i = 0, v = varray_vertex, c = varray_color;i < 8;i++, v += 4, c += 4)
1155 VectorSubtract(v, r_origin, diff);
1156 f2 = exp(fogdensity/DotProduct(diff, diff));
1159 c[0] = c[0] * f1 + fogcolor[0] * f2;
1160 c[1] = c[1] * f1 + fogcolor[1] * f2;
1161 c[2] = c[2] * f1 + fogcolor[2] * f2;
1169 void R_DrawNoModelCallback(const void *calldata1, int calldata2)
1171 const entity_render_t *ent = calldata1;
1173 float f1, f2, *c, diff[3];
1175 memset(&m, 0, sizeof(m));
1176 if (ent->flags & EF_ADDITIVE)
1178 m.blendfunc1 = GL_SRC_ALPHA;
1179 m.blendfunc2 = GL_ONE;
1181 else if (ent->alpha < 1)
1183 m.blendfunc1 = GL_SRC_ALPHA;
1184 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1188 m.blendfunc1 = GL_ONE;
1189 m.blendfunc2 = GL_ZERO;
1191 R_Mesh_Matrix(&ent->matrix);
1194 element[ 0] = 5;element[ 1] = 2;element[ 2] = 0;
1195 element[ 3] = 5;element[ 4] = 1;element[ 5] = 2;
1196 element[ 6] = 5;element[ 7] = 0;element[ 8] = 3;
1197 element[ 9] = 5;element[10] = 3;element[11] = 1;
1198 element[12] = 0;element[13] = 2;element[14] = 4;
1199 element[15] = 2;element[16] = 1;element[17] = 4;
1200 element[18] = 3;element[19] = 0;element[20] = 4;
1201 element[21] = 1;element[22] = 3;element[23] = 4;
1202 varray_vertex[ 0] = -16;varray_vertex[ 1] = 0;varray_vertex[ 2] = 0;
1203 varray_vertex[ 4] = 16;varray_vertex[ 5] = 0;varray_vertex[ 6] = 0;
1204 varray_vertex[ 8] = 0;varray_vertex[ 9] = -16;varray_vertex[10] = 0;
1205 varray_vertex[12] = 0;varray_vertex[13] = 16;varray_vertex[14] = 0;
1206 varray_vertex[16] = 0;varray_vertex[17] = 0;varray_vertex[18] = -16;
1207 varray_vertex[20] = 0;varray_vertex[21] = 0;varray_vertex[22] = 16;
1208 varray_color[ 0] = 0.00f * r_colorscale;varray_color[ 1] = 0.00f * r_colorscale;varray_color[ 2] = 0.50f * r_colorscale;varray_color[ 3] = ent->alpha;
1209 varray_color[ 4] = 0.00f * r_colorscale;varray_color[ 5] = 0.00f * r_colorscale;varray_color[ 6] = 0.50f * r_colorscale;varray_color[ 7] = ent->alpha;
1210 varray_color[ 8] = 0.00f * r_colorscale;varray_color[ 9] = 0.50f * r_colorscale;varray_color[10] = 0.00f * r_colorscale;varray_color[11] = ent->alpha;
1211 varray_color[12] = 0.00f * r_colorscale;varray_color[13] = 0.50f * r_colorscale;varray_color[14] = 0.00f * r_colorscale;varray_color[15] = ent->alpha;
1212 varray_color[16] = 0.50f * r_colorscale;varray_color[17] = 0.00f * r_colorscale;varray_color[18] = 0.00f * r_colorscale;varray_color[19] = ent->alpha;
1213 varray_color[20] = 0.50f * r_colorscale;varray_color[21] = 0.00f * r_colorscale;varray_color[22] = 0.00f * r_colorscale;varray_color[23] = ent->alpha;
1216 VectorSubtract(ent->origin, r_origin, diff);
1217 f2 = exp(fogdensity/DotProduct(diff, diff));
1219 for (i = 0, c = varray_color;i < 6;i++, c += 4)
1221 c[0] = (c[0] * f1 + fogcolor[0] * f2) * r_colorscale;
1222 c[1] = (c[1] * f1 + fogcolor[1] * f2) * r_colorscale;
1223 c[2] = (c[2] * f1 + fogcolor[2] * f2) * r_colorscale;
1228 for (i = 0, c = varray_color;i < 6;i++, c += 4)
1230 c[0] *= r_colorscale;
1231 c[1] *= r_colorscale;
1232 c[2] *= r_colorscale;
1236 R_Mesh_Draw(6, 8, element);
1239 void R_DrawNoModel(entity_render_t *ent)
1241 //if ((ent->effects & EF_ADDITIVE) || (ent->alpha < 1))
1242 R_MeshQueue_AddTransparent(ent->origin, R_DrawNoModelCallback, ent, 0);
1244 // R_DrawNoModelCallback(ent, 0);
1247 void R_CalcBeamVerts (float *vert, vec3_t org1, vec3_t org2, float width)
1249 vec3_t right1, right2, diff, normal;
1251 VectorSubtract (org2, org1, normal);
1252 VectorNormalizeFast (normal);
1254 // calculate 'right' vector for start
1255 VectorSubtract (r_origin, org1, diff);
1256 VectorNormalizeFast (diff);
1257 CrossProduct (normal, diff, right1);
1259 // calculate 'right' vector for end
1260 VectorSubtract (r_origin, org2, diff);
1261 VectorNormalizeFast (diff);
1262 CrossProduct (normal, diff, right2);
1264 vert[ 0] = org1[0] + width * right1[0];
1265 vert[ 1] = org1[1] + width * right1[1];
1266 vert[ 2] = org1[2] + width * right1[2];
1267 vert[ 4] = org1[0] - width * right1[0];
1268 vert[ 5] = org1[1] - width * right1[1];
1269 vert[ 6] = org1[2] - width * right1[2];
1270 vert[ 8] = org2[0] - width * right2[0];
1271 vert[ 9] = org2[1] - width * right2[1];
1272 vert[10] = org2[2] - width * right2[2];
1273 vert[12] = org2[0] + width * right2[0];
1274 vert[13] = org2[1] + width * right2[1];
1275 vert[14] = org2[2] + width * right2[2];