GLSL中的矩阵转换无限延伸

时间:2017-10-16 05:00:55

标签: matrix opengl-es glsl webgl coordinate-transformation

我使用webgl并修改着色器(vs.glsls和fs.glsl)以理解GLSL和图形编程。我有一个模型,我想缩放,旋转和翻译。缩放和旋转工作正常但是当我乘以平移矩阵时,结果很奇怪。我知道这是一个非常基本的问题,但我错过了一些东西,我需要找到它。 我的模型通过y轴无限延伸。

白色区域应该是模型的眼睛:

The white area is supposed to be the eye of the model

这是我的顶点着色器代码:

mat4 rX = mat4 (
 1.0, 0.0, 0.0, 0.0,
 0.0, 0.0, -1.0, 0.0,
 0.0, 1.0, 0.0, 0.0,
 0.0, 0.0, 0.0, 1.0
 );

mat4 rZ = mat4 (
 0.0, 1.0, 0.0, 0.0,
 -1.0, 0.0, 0.0, 0.0,
 0.0, 0.0, 1.0, 0.0,
 0.0, 0.0, 0.0, 1.0
 );

mat4 eyeScale = mat4 (
 .50,0.0,0.0,0.0,
 0.0,.50,0.0,0.0,
 0.0,0.0,.50,0.0,
 0.0,0.0,0.0,1.0
 );
mat4 eyeTrans = mat4(
1.0,0.0,0.0,0.0,
0.0,1.0,0.0,4.0,
0.0,0.0,1.0,0.0,
0.0,0.0,0.0,1.0
);
 mat4 iR = eyeTrans*rZ*rX*eyeScale;
 gl_Position = projectionMatrix * modelViewMatrix  *iR* vec4(position, 1.0);
}

1 个答案:

答案 0 :(得分:4)

您在设置转换矩阵时交换了行和列

将其更改为:

mat4 eyeTrans = mat4(
    1.0, 0.0, 0.0, 0.0,
    0.0, 1.0, 0.0, 0.0,
    0.0, 0.0, 1.0, 0.0,
    0.0, 4.0, 0.0, 1.0
);


4 * 4矩阵看起来像这样:

  c0  c1  c2  c3            c0  c1  c2  c3
[ Xx  Yx  Zx  Tx ]        [  0   4   8  12 ]     
[ Xy  Yy  Zy  Ty ]        [  1   5   9  13 ]     
[ Xz  Yz  Zz  Tz ]        [  2   6  10  14 ]     
[  0   0   0   1 ]        [  3   7  11  15 ] 

在GLSL中,列的处理方式如下:

vec4 c0 = eyeTrans[0].xyzw;
vec4 c1 = eyeTrans[1].xyzw;
vec4 c2 = eyeTrans[2].xyzw;
vec4 c3 = eyeTrans[3].xyzw;

4 * 4矩阵的记忆图像如下所示:

[ Xx, Xy, Xz, 0, Yx, Yy, Yz, 0, Zx, Zy, Zz, 0, Tx, Ty, Tz, 1 ]

进一步了解: