quakeforge/libs/video/renderer/gl/gl_rmain.c
Bill Currie aafd5c3d81 [gl] Make perspective matrix setup consistent
Now GL perspective matrix setup matches that of GLSL and Vulkan, and
GL's z_up matrix matches GLSL's (as it should, since they're really
going through the same API). GL also needs the depth adjustmet matrix
now. Other than having to google the docs for glFrustum, there's nothing
wrong with the function itself, but it's nice to have direct control
over the matrices.

In the process, I discovered how horribly confused I've been at times
with respect to the handedness of GL and Quake: GL is right-handed
(y-up, z-out, x-right), as is Quake itself (but z-up, y-left, x-in), but
as the perspective matrix used in the three renderers expects z-in,
having x-right and y-up makes the matrix effectively left-handed (not
for Vulkan though, because there it's y-down, x-right, z-up, so
right-handed again).
2022-03-27 13:23:44 +09:00

323 lines
7.7 KiB
C

/*
gl_rmain.c
(no description)
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "QF/cvar.h"
#include "QF/draw.h"
#include "QF/mathlib.h"
#include "QF/qargs.h"
#include "QF/render.h"
#include "QF/skin.h"
#include "QF/sound.h"
#include "QF/sys.h"
#include "QF/vid.h"
#include "QF/scene/entity.h"
#include "QF/GL/defines.h"
#include "QF/GL/funcs.h"
#include "QF/GL/qf_alias.h"
#include "QF/GL/qf_draw.h"
#include "QF/GL/qf_iqm.h"
#include "QF/GL/qf_particles.h"
#include "QF/GL/qf_rlight.h"
#include "QF/GL/qf_rmain.h"
#include "QF/GL/qf_rsurf.h"
#include "QF/GL/qf_sprite.h"
#include "QF/GL/qf_vid.h"
#include "compat.h"
#include "r_internal.h"
#include "varrays.h"
#include "vid_gl.h"
float gl_r_world_matrix[16];
//FIXME static float r_base_world_matrix[16];
//vec3_t gl_shadecolor; // Ender (Extend) Colormod
float gl_modelalpha; // Ender (Extend) Alpha
void
glrmain_init (void)
{
gldepthmin = 0;
gldepthmax = 1;
qfglDepthFunc (GL_LEQUAL);
qfglDepthRange (gldepthmin, gldepthmax);
if (gl_multitexture)
gl_multitexture_f (gl_multitexture);
if (gl_overbright)
gl_overbright_f (gl_overbright);
}
void
gl_R_RotateForEntity (entity_t *e)
{
mat4f_t mat;
Transform_GetWorldMatrix (e->transform, mat);
qfglMultMatrixf (&mat[0][0]);
}
void
gl_R_RenderEntities (entqueue_t *queue)
{
if (!r_drawentities->int_val)
return;
// LordHavoc: split into 3 loops to simplify state changes
if (gl_mtex_active_tmus >= 2) {
qglActiveTexture (gl_mtex_enum + 1);
qfglEnable (GL_TEXTURE_2D);
qfglTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
qfglDisable (GL_TEXTURE_2D);
qglActiveTexture (gl_mtex_enum + 0);
}
if (gl_affinemodels->int_val)
qfglHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
if (gl_tess)
qfglEnable (GL_PN_TRIANGLES_ATI);
qfglEnable (GL_CULL_FACE);
if (gl_vector_light->int_val) {
qfglEnable (GL_LIGHTING);
qfglEnable (GL_NORMALIZE);
} else if (gl_tess) {
qfglEnable (GL_NORMALIZE);
}
for (size_t i = 0; i < queue->ent_queues[mod_alias].size; i++) { \
entity_t *ent = queue->ent_queues[mod_alias].a[i]; \
gl_R_DrawAliasModel (ent);
}
qfglColor3ubv (color_white);
qfglDisable (GL_NORMALIZE);
qfglDisable (GL_LIGHTING);
if (gl_tess)
qfglDisable (GL_PN_TRIANGLES_ATI);
if (gl_affinemodels->int_val)
qfglHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
if (gl_mtex_active_tmus >= 2) { // FIXME: Ugly, but faster than cleaning
// up in every R_DrawAliasModel()!
qglActiveTexture (gl_mtex_enum + 1);
qfglEnable (GL_TEXTURE_2D);
if (gl_combine_capable && gl_overbright->int_val) {
qfglTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
qfglTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
qfglTexEnvf (GL_TEXTURE_ENV, GL_RGB_SCALE, gl_rgb_scale);
} else {
qfglTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
qfglDisable (GL_TEXTURE_2D);
qglActiveTexture (gl_mtex_enum + 0);
}
for (size_t i = 0; i < queue->ent_queues[mod_iqm].size; i++) { \
entity_t *ent = queue->ent_queues[mod_iqm].a[i]; \
gl_R_DrawIQMModel (ent);
}
qfglColor3ubv (color_white);
qfglDisable (GL_CULL_FACE);
qfglEnable (GL_ALPHA_TEST);
if (gl_va_capable)
qfglInterleavedArrays (GL_T2F_C4UB_V3F, 0, gl_spriteVertexArray);
for (size_t i = 0; i < queue->ent_queues[mod_sprite].size; i++) { \
entity_t *ent = queue->ent_queues[mod_sprite].a[i]; \
gl_R_DrawSpriteModel (ent);
}
qfglDisable (GL_ALPHA_TEST);
}
static void
R_DrawViewModel (void)
{
entity_t *ent = vr_data.view_model;
if (vr_data.inhibit_viewmodel
|| !r_drawviewmodel->int_val
|| !r_drawentities->int_val
|| !ent->renderer.model)
return;
// hack the depth range to prevent view model from poking into walls
qfglDepthRange (gldepthmin, gldepthmin + 0.3 * (gldepthmax - gldepthmin));
qfglEnable (GL_CULL_FACE);
if (gl_vector_light->int_val) {
qfglEnable (GL_LIGHTING);
qfglEnable (GL_NORMALIZE);
} else if (gl_tess) {
qfglEnable (GL_NORMALIZE);
}
if (gl_affinemodels->int_val)
qfglHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
if (gl_mtex_active_tmus >= 2) {
qglActiveTexture (gl_mtex_enum + 1);
qfglEnable (GL_TEXTURE_2D);
qfglTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
qfglDisable (GL_TEXTURE_2D);
qglActiveTexture (gl_mtex_enum + 0);
}
gl_R_DrawAliasModel (ent);
qfglColor3ubv (color_white);
if (gl_mtex_active_tmus >= 2) { // FIXME: Ugly, but faster than cleaning
// up in every R_DrawAliasModel()!
qglActiveTexture (gl_mtex_enum + 1);
qfglEnable (GL_TEXTURE_2D);
if (gl_combine_capable && gl_overbright->int_val) {
qfglTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
qfglTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
qfglTexEnvf (GL_TEXTURE_ENV, GL_RGB_SCALE, gl_rgb_scale);
} else {
qfglTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
qfglDisable (GL_TEXTURE_2D);
qglActiveTexture (gl_mtex_enum + 0);
}
if (gl_affinemodels->int_val)
qfglHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
qfglDisable (GL_NORMALIZE);
qfglDisable (GL_LIGHTING);
qfglDisable (GL_CULL_FACE);
qfglDepthRange (gldepthmin, gldepthmax);
}
static void
R_Perspective (void)
{
float aspect = (float) r_refdef.vrect.width / r_refdef.vrect.height;
float f = 1 / tan (r_refdef.fov_y * M_PI / 360);
float neard, fard;
mat4f_t proj;
neard = r_nearclip->value;
fard = r_farclip->value;
// NOTE columns!
proj[0] = (vec4f_t) { f / aspect, 0, 0, 0 };
proj[1] = (vec4f_t) { 0, f, 0, 0 };
proj[2] = (vec4f_t) { 0, 0, (fard) / (fard - neard), 1 };
proj[3] = (vec4f_t) { 0, 0, (fard * neard) / (neard - fard), 0 };
// convert 0..1 depth buffer range to -1..1
static mat4f_t depth_range = {
{ 1, 0, 0, 0},
{ 0, 1, 0, 0},
{ 0, 0, 2, 0},
{ 0, 0,-1, 1},
};
mmulf (proj, depth_range, proj);
qfglMatrixMode (GL_PROJECTION);
qfglLoadMatrixf (&proj[0][0]);
}
static void
R_SetupGL (void)
{
R_Perspective ();
qfglFrontFace (GL_CW);
qfglMatrixMode (GL_MODELVIEW);
qfglLoadIdentity ();
static mat4f_t z_up = {
{ 0, 0, 1, 0},
{-1, 0, 0, 0},
{ 0, 1, 0, 0},
{ 0, 0, 0, 1},
};
mat4f_t view;
mmulf (view, z_up, r_refdef.camera_inverse);
qfglLoadMatrixf (&view[0][0]);
qfglGetFloatv (GL_MODELVIEW_MATRIX, gl_r_world_matrix);
// set drawing parms
// qfglEnable (GL_CULL_FACE);
qfglDisable (GL_ALPHA_TEST);
qfglAlphaFunc (GL_GREATER, 0.5);
qfglEnable (GL_DEPTH_TEST);
if (gl_dlight_smooth->int_val)
qfglShadeModel (GL_SMOOTH);
else
qfglShadeModel (GL_FLAT);
}
void
gl_R_RenderView (void)
{
if (r_norefresh->int_val) {
return;
}
if (!r_refdef.worldmodel) {
return;
}
R_SetupGL ();
gl_Fog_EnableGFog ();
gl_R_DrawWorld ();
S_ExtraUpdate (); // don't let sound get messed up if going slow
gl_R_RenderDlights ();
R_DrawViewModel ();
gl_Fog_DisableGFog ();
}
void
gl_R_ClearState (void)
{
r_refdef.worldmodel = 0;
R_ClearEfrags ();
R_ClearDlights ();
R_ClearParticles ();
}