quakeforge/libs/video/renderer/vulkan/projection.c
Bill Currie 495dd759f0 [renderer] Clean up FOV and viewport handling
Viewport and FOV updates are now separate so updating one doesn't cause
recalculations of the other. Also, perspective setup is now done
directly from the tangents of the half angles for fov_x and fov_y making
the renderers independent of fov/aspect mode. I imagine things are a bit
of a mess with view size changes, and especially screen size changes
(not supported yet anyway), and vulkan winds up updating its projection
matrices every frame, but everything that's expected to work does
(vulkan errors out for fisheye or warp due to frame buffer creation not
being supported yet).
2022-03-30 14:55:32 +09:00

91 lines
1.9 KiB
C

/*
proejct.c
Vulkan projection matrices
Copyright (C) 2021 Bill Currie <bill@taniwha.org>
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_MATH_H
# include <math.h>
#endif
#include "QF/cvar.h"
#include "QF/Vulkan/projection.h"
#include "r_internal.h"
void
QFV_Orthographic (mat4f_t proj, float xmin, float xmax, float ymin, float ymax,
float znear, float zfar)
{
proj[0] = (vec4f_t) {
2 / (xmax - xmin),
0,
0,
0
};
proj[1] = (vec4f_t) {
0,
2 / (ymax - ymin),
0,
0
};
proj[2] = (vec4f_t) {
0,
0,
1 / (znear - zfar),
0
};
proj[3] = (vec4f_t) {
-(xmax + xmin) / (xmax - xmin),
-(ymax + ymin) / (ymax - ymin),
znear / (znear - zfar),
1,
};
}
void
QFV_PerspectiveTan (mat4f_t proj, float fov_x, float fov_y)
{
float neard, fard;
neard = r_nearclip->value;
fard = r_farclip->value;
proj[0] = (vec4f_t) { 1 / fov_x, 0, 0, 0 };
proj[1] = (vec4f_t) { 0, 1 / fov_y, 0, 0 };
proj[2] = (vec4f_t) { 0, 0, fard / (fard - neard), 1 };
proj[3] = (vec4f_t) { 0, 0, (neard * fard) / (neard - fard), 0 };
}
void
QFV_PerspectiveCos (mat4f_t proj, float fov)
{
// square first for auto-abs (no support for > 180 degree fov)
fov = fov * fov;
float t = sqrt ((1 - fov) / fov);
QFV_PerspectiveTan (proj, t, t);
}