[vid,render] Clean up fov and aspect

vid.aspect is removed (for now) as it was not really the right idea (I
really didn't know what I was doing at the time). Nicely, this *almost*
fixes the fov bug on fresh installs: the view is now properly
upside-down rather than just flipped vertically (ie, it's now rotated
180 degrees).
This commit is contained in:
Bill Currie 2021-04-01 19:17:19 +09:00
parent 9ded490806
commit 37d35811e3
16 changed files with 45 additions and 125 deletions

View File

@ -67,7 +67,7 @@ void Vulkan_CreateRenderPass (struct vulkan_ctx_s *ctx);
void Vulkan_DestroyRenderPass (struct vulkan_ctx_s *ctx);
void Vulkan_CreateMatrices (struct vulkan_ctx_s *ctx);
void Vulkan_DestroyMatrices (struct vulkan_ctx_s *ctx);
void Vulkan_CalcProjectionMatrices (struct vulkan_ctx_s *ctx, float aspect);
void Vulkan_CalcProjectionMatrices (struct vulkan_ctx_s *ctx);
void Vulkan_CalcViewMatrix (struct vulkan_ctx_s *ctx);
void Vulkan_CreateSwapchain (struct vulkan_ctx_s *ctx);
void Vulkan_CreateDevice (struct vulkan_ctx_s *ctx);

View File

@ -161,7 +161,7 @@ typedef struct vid_render_funcs_s {
void (*R_RenderView) (void);
void (*R_DecayLights) (double frametime);
void (*R_ViewChanged) (float aspect);
void (*R_ViewChanged) (void);
void (*R_ClearParticles) (void);
void (*R_InitParticles) (void);
void (*SCR_ScreenShot_f) (void);

View File

@ -51,7 +51,6 @@ typedef struct {
int rowbytes; // may be > width if displayed in a window
unsigned width;
unsigned height;
float aspect; // width / height -- < 1 is taller than wide
int numpages;
qboolean recalc_refdef; // if true, recalc vid-based stuff
qboolean cshift_changed;

View File

@ -80,7 +80,7 @@ void R_ClearState (void);
void R_InitSky (struct texture_s *mt); // called at level load
void R_Textures_Init (void);
void R_RenderView (void); // must set r_refdef first
void R_ViewChanged (float aspect); // must set r_refdef first
void R_ViewChanged (void); // must set r_refdef first
// called whenever r_refdef or vid change
void R_AddEfrags (mod_brush_t *, entity_t *ent);

View File

@ -400,7 +400,7 @@ R_SetupGL_Viewport_and_Perspective (void)
}
// printf ("glViewport(%d, %d, %d, %d)\n", glx + x, gly + y2, w, h);
qfglViewport (x, y2, w, h);
screenaspect = r_refdef.vrect.width * vid.aspect / r_refdef.vrect.height;
screenaspect = r_refdef.vrect.width / r_refdef.vrect.height;
MYgluPerspective (r_refdef.fov_y, screenaspect, r_nearclip->value,
r_farclip->value);
}

View File

@ -238,7 +238,7 @@ gl_R_NewMap (model_t *worldmodel, struct model_s **models, int num_models)
}
void
gl_R_ViewChanged (float aspect)
gl_R_ViewChanged (void)
{
}

View File

@ -64,46 +64,21 @@ mat4f_t glsl_projection;
mat4f_t glsl_view;
void
glsl_R_ViewChanged (float aspect)
glsl_R_ViewChanged (void)
{
double xmin, xmax, ymin, ymax;
float fovx, fovy, neard, fard;
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;
vec4f_t *proj = glsl_projection;
fovx = r_refdef.fov_x;
fovy = r_refdef.fov_y;
neard = r_nearclip->value;
fard = r_farclip->value;
ymax = neard * tan (fovy * M_PI / 360); // fov_2 / 2
ymin = -ymax;
xmax = neard * tan (fovx * M_PI / 360); // fov_2 / 2
xmin = -xmax;
proj[0] = (vec4f_t) {
(2 * neard) / (xmax - xmin),
0,
0,
0
};
proj[1] = (vec4f_t) {
0,
(2 * neard) / (ymax - ymin),
0,
0
};
proj[2] = (vec4f_t) {
(xmax + xmin) / (xmax - xmin),
(ymax + ymin) / (ymax - ymin),
(fard + neard) / (neard - fard),
-1
};
proj[3] = (vec4f_t) {
0,
0,
(2 * fard * neard) / (neard - fard),
0
};
// 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 + neard) / (neard - fard), -1 };
proj[3] = (vec4f_t) { 0, 0, (2 * fard * neard) / (neard - fard), 0 };
}
void

View File

@ -167,7 +167,7 @@ SCR_CalcRefdef (void)
refdef->vrect = scr_vrect;
// notify the refresh of the change
r_funcs->R_ViewChanged (r_data->vid->aspect);
r_funcs->R_ViewChanged ();
}
/*

View File

@ -230,7 +230,7 @@ R_NewMap (model_t *worldmodel, struct model_s **models, int num_models)
Guaranteed to be called before the first refresh
*/
void
R_ViewChanged (float aspect)
R_ViewChanged (void)
{
int i;
float res_scale;
@ -263,7 +263,7 @@ R_ViewChanged (float aspect)
r_refdef.aliasvrectbottom = r_refdef.aliasvrect.y +
r_refdef.aliasvrect.height;
pixelAspect = vid.aspect;
pixelAspect = 1;//FIXME vid.aspect;
xOrigin = r_refdef.xOrigin;
yOrigin = r_refdef.yOrigin;

View File

@ -264,7 +264,7 @@ R_SetupFrame (void)
vrect.height = vid.height;
R_SetVrect (&vrect, &r_refdef.vrect, vr_data.lineadj);
R_ViewChanged (vid.aspect);
R_ViewChanged ();
} else {
w = vid.width;
h = vid.height;
@ -287,12 +287,11 @@ R_SetupFrame (void)
R_SetVrect (&vrect, &r_refdef.vrect,
(int) ((float) vr_data.lineadj *
(h / (float) vid.height)));
R_ViewChanged (vid.aspect * (h / w) * ((float) vid.width /
(float) vid.height));
R_ViewChanged ();
}
} else {
r_refdef.vrect = scr_vrect;
R_ViewChanged (vid.aspect);
R_ViewChanged ();
}
r_viewchanged = false;

View File

@ -245,7 +245,7 @@ sw32_R_NewMap (model_t *worldmodel, struct model_s **models, int num_models)
Guaranteed to be called before the first refresh
*/
void
sw32_R_ViewChanged (float aspect)
sw32_R_ViewChanged (void)
{
int i;
float res_scale;
@ -278,7 +278,7 @@ sw32_R_ViewChanged (float aspect)
r_refdef.aliasvrectbottom = r_refdef.aliasvrect.y +
r_refdef.aliasvrect.height;
sw32_pixelAspect = aspect;
sw32_pixelAspect = 1;//FIXME vid.aspect;
xOrigin = r_refdef.xOrigin;
yOrigin = r_refdef.yOrigin;

View File

@ -260,7 +260,7 @@ sw32_R_SetupFrame (void)
vrect.height = vid.height;
R_SetVrect (&vrect, &r_refdef.vrect, vr_data.lineadj);
sw32_R_ViewChanged (vid.aspect);
sw32_R_ViewChanged ();
} else {
w = vid.width;
h = vid.height;
@ -283,8 +283,7 @@ sw32_R_SetupFrame (void)
R_SetVrect (&vrect, &r_refdef.vrect,
(int) ((float) vr_data.lineadj *
(h / (float) vid.height)));
sw32_R_ViewChanged (vid.aspect * (h / w) * ((float) vid.width /
(float) vid.height));
sw32_R_ViewChanged ();
}
} else {
vrect.x = 0;
@ -293,7 +292,7 @@ sw32_R_SetupFrame (void)
vrect.height = vid.height;
r_refdef.vrect = scr_vrect;
sw32_R_ViewChanged (vid.aspect);
sw32_R_ViewChanged ();
}
sw32_r_viewchanged = false;

View File

@ -364,9 +364,9 @@ vulkan_Draw_SubPic (int x, int y, qpic_t *pic, int srcx, int srcy, int width, in
}
static void
vulkan_R_ViewChanged (float aspect)
vulkan_R_ViewChanged (void)
{
Vulkan_CalcProjectionMatrices (vulkan_ctx, aspect);
Vulkan_CalcProjectionMatrices (vulkan_ctx);
}
static void

View File

@ -65,8 +65,8 @@ ortho_mat (float *proj, float xmin, float xmax, float ymin, float ymax,
proj[2] = 0;
proj[6] = 0;
proj[10] = -2 / (zfar - znear);
proj[14] = -(zfar + znear) / (zfar - znear);
proj[10] = 1 / (znear - zfar);
proj[14] = znear / (znear - zfar);
proj[3] = 0;
proj[7] = 0;
@ -75,35 +75,28 @@ ortho_mat (float *proj, float xmin, float xmax, float ymin, float ymax,
}
static void
persp_mat (float *proj, float xmin, float xmax, float ymin, float ymax,
float aspect)
persp_mat (float *proj, float fov, float aspect)
{
float fovx, fovy, neard, fard;
float f = 1 / tan (fov * M_PI / 360);
float neard, fard;
fovx = r_refdef.fov_x;
fovy = r_refdef.fov_y;
neard = r_nearclip->value;
fard = r_farclip->value;
ymax = neard * tan (fovy * M_PI / 360); // fov_2 / 2
ymin = -ymax;
xmax = neard * tan (fovx * M_PI / 360); // fov_2 / 2
xmin = -xmax;
proj[0] = (2 * neard) / (xmax - xmin);
proj[0] = f / aspect;
proj[4] = 0;
proj[8] = (xmax + xmin) / (xmax - xmin);
proj[8] = 0;
proj[12] = 0;
proj[1] = 0;
proj[5] = -(2 * neard) / (ymax - ymin);
proj[9] = (ymax + ymin) / (ymax - ymin);
proj[5] = -f;
proj[9] = 0;
proj[13] = 0;
proj[2] = 0;
proj[6] = 0;
proj[10] = (fard) / (neard - fard);
proj[14] = (fard * neard) / (neard - fard);
proj[10] = fard / (neard - fard);
proj[14] = (neard * fard) / (neard - fard);
proj[3] = 0;
proj[7] = 0;
@ -163,7 +156,7 @@ Vulkan_CreateMatrices (vulkan_ctx_t *ctx)
}
void
Vulkan_CalcProjectionMatrices (vulkan_ctx_t *ctx, float aspect)
Vulkan_CalcProjectionMatrices (vulkan_ctx_t *ctx)
{
qfv_device_t *device = ctx->device;
qfv_devfuncs_t *dfunc = device->funcs;
@ -172,9 +165,10 @@ Vulkan_CalcProjectionMatrices (vulkan_ctx_t *ctx, float aspect)
int width = vid.conwidth;
int height = vid.conheight;
ortho_mat (mat->projection_2d, 0, width, 0, height, -99999, 99999);
persp_mat (mat->projection_3d, 0, width, 0, height, aspect);
float aspect = (float) r_refdef.vrect.width / r_refdef.vrect.height;
persp_mat (mat->projection_3d, r_refdef.fov_y, aspect);
#if 0
Sys_MaskPrintf (SYS_vulkan, "ortho:\n");
Sys_MaskPrintf (SYS_vulkan, " [[%g, %g, %g, %g],\n",

View File

@ -60,31 +60,9 @@ VISIBLE unsigned int d_8to24table[256];
/* Screen size */
cvar_t *vid_width;
cvar_t *vid_height;
cvar_t *vid_aspect;
cvar_t *vid_fullscreen;
static void
vid_aspect_f (cvar_t *var)
{
const char *p = strchr (var->string, ':');
float w, h;
if (p) {
w = atof (var->string);
h = atof (p + 1);
if (w > 0.0 && h > 0.0) {
var->vec[0] = w;
var->vec[1] = h;
return;
}
}
Sys_Printf ("badly formed aspect ratio: %s. Using default 4:3\n",
var->string);
var->vec[0] = 4.0;
var->vec[1] = 3.0;
}
void
VID_GetWindowSize (int def_w, int def_h)
{
@ -94,11 +72,6 @@ VID_GetWindowSize (int def_w, int def_h)
"screen width");
vid_height = Cvar_Get ("vid_height", va (0, "%d", def_h), CVAR_NONE, NULL,
"screen height");
vid_aspect = Cvar_Get ("vid_aspect", "4:3", CVAR_ROM, vid_aspect_f,
"Physical screen aspect ratio in \"width:height\" format. "
"Common values are 4:3, 5:3, 8:5, 16:9, but any width:height "
"measurement will do (eg, 475:296.875 the approximate dimentions "
"in mm of the display area of a certain monitor)");
if ((pnum = COM_CheckParm ("-width"))) {
if (pnum >= com_argc - 1)
@ -140,9 +113,6 @@ VID_GetWindowSize (int def_w, int def_h)
viddef.width = vid_width->int_val;
viddef.height = vid_height->int_val;
viddef.aspect = ((vid_aspect->vec[0] * viddef.height)
/ (vid_aspect->vec[1] * viddef.width));
con_width = Cvar_Get ("con_width", va (0, "%d", viddef.width), CVAR_NONE,
NULL, "console effective width (GL only)");
if ((pnum = COM_CheckParm ("-conwidth"))) {
@ -155,7 +125,7 @@ VID_GetWindowSize (int def_w, int def_h)
Cvar_SetFlags (con_width, con_width->flags | CVAR_ROM);
viddef.conwidth = con_width->int_val;
conheight = (viddef.conwidth * vid_aspect->vec[1]) / vid_aspect->vec[0];
conheight = (viddef.conwidth * viddef.height) / viddef.width;
con_height = Cvar_Get ("con_height", va (0, "%d", conheight), CVAR_NONE,
NULL, "console effective height (GL only)");
if ((pnum = COM_CheckParm ("-conheight"))) {

View File

@ -44,20 +44,12 @@ static byte backingbuf[48 * 24];
void
D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
{
int i, j, reps, repshift;
int i, j, reps = 1, repshift = 0;
vrect_t rect;
if (!viddef.initialized || !win_sw_context)
return;
if (viddef.aspect > 1.5) {
reps = 2;
repshift = 1;
} else {
reps = 1;
repshift = 0;
}
if (!viddef.direct)
return;
@ -86,20 +78,12 @@ D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
void
D_EndDirectRect (int x, int y, int width, int height)
{
int i, j, reps, repshift;
int i, j, reps = 1, repshift = 0;
vrect_t rect;
if (!viddef.initialized || !win_sw_context)
return;
if (viddef.aspect > 1.5) {
reps = 2;
repshift = 1;
} else {
reps = 1;
repshift = 0;
}
if (!viddef.direct)
return;