如何在着色器中更改邻位矩阵大小

How to change ortho matrix size in shader

我尝试缩放正交投影矩阵,但它似乎无法缩放 dimensions.I 我正在使用正交投影进行定向照明。但是如果主摄像头在地面以上我想让我的正交矩阵范围更大。

例如,如果相机为零,orho 矩阵为 glm::ortho(-10.0f , 10.0f , -10.0f , 10.0f , 0.01f, 4000.0f)

但是如果相机在 y 方向上移动 400,我希望这个矩阵像 glm::ortho(-410.0f , 410.0f , -410.0f , 410.0f , 0.01f, 4000.0f)

但我想在着色器中使用矩阵乘法或加法来执行此操作

Orthographic Projection 矩阵可以通过以下方式计算:

r = right, l = left, b = bottom, t = top, n = near, f = far

X:    2/(r-l)         0               0               0
y:    0               2/(t-b)         0               0
z:    0               0               -2/(f-n)        0
t:    -(r+l)/(r-l)    -(t+b)/(t-b)    -(f+n)/(f-n)    1

在 glsl 中,例如:

float l = -410.0f;
float r =  410.0f;
float b = -410.0f;
float t =  410.0f;
float n =  0.01f;
float f =  4000.0f;

mat4 projection = mat4(
    vec4(2.0/(r-l),     0.0,          0.0,         0.0),
    vec4(0.0,           2.0/(t-b),    0.0,         0.0),
    vec4(0.0,           0.0,         -2.0/(f-n),   0.0),
    vec4(-(r+l)/(r-l), -(t+b)/(t-b), -(f+n)/(f-n), 1.0)
);

此外,您还可以缩放正交投影矩阵:

c++

glm::mat4 ortho = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, 0.01f, 4000.0f);

float scale = 10.0f / 410.0f;
glm::mat4 scale_mat = glm::scale(glm::mat4(1.0f), glm::vec3(scale, scale, 1.0f));
glm::mat4 ortho_new = ortho * scale_mat;

glsl

float scale = 10.0 / 410.0;
mat4 scale_mat = mat4(
    vec4(scale, 0.0,   0.0, 0.0),
    vec4(0.0,   scale, 0.0, 0.0),
    vec4(0.0,   0.0,   1.0, 0.0),
    vec4(0.0,   0.0,   0.0, 1.0)
);

mat4 ortho_new = ortho * scale_mat;