GLSL 中的矩阵转换被无限拉伸(stretch)

标签 matrix opengl-es glsl webgl coordinate-transformation

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

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

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);
}

最佳答案

当您设置平移矩阵时,您交换了行和列

将其更改为:

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 ]

进一步查看:

关于GLSL 中的矩阵转换被无限拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46763234/

相关文章:

ios - 将 iPad 坐标转换为 OpenGL ES 2.0 场景坐标? iOS

iphone - glVertexPointer - 绘制不需要的线条

OpenGL 默认管道 alpha 混合对于 alpha 分量没有任何意义

opengl-es - 如何转换为 HDR 渲染器?

c++ - 基于物理的渲染结果看起来不正确

c# - 矩阵 NxN 旋转 90 度 - 是否有可能比 O(n^2) 做得更好?

c++ - OpenGL 中的旋转每次都有不同的效果

javascript - 给定 rotateX、rotateY 和 rotateZ,如何计算 matrix3d?

python - 在 pdist 压缩距离矩阵中找到最小值的索引

android - 何时调用 glDeleteBuffers()