opengl - GLSL 编译在 mat4x3 vec4 乘法上失败

标签 opengl glsl

我有一个可以正常工作的 GLSL 着色器,直到我向其中添加以下行。

"vec4 coord = texture2D(Coordinates,gl_TexCoord[0].st);"
"mat4x3 theMatrix;"
"theMatrix[1][1] = 1.0;"
"vec3 CampProj = theMatrix * coord;"

当我检查错误日志时,我被告知:

"ERROR: 0:2: '=' :  cannot convert from '4-component vector of float' to '3-component vector of float'\n\n"

如果我将 CampProject 设为 vec4,则可以正常编译,但我很困惑 4 列、3 行矩阵乘以 4 分量向量将如何得到 4 分量向量。

这是一个错误,还是 4x3 矩阵实际上只是一个 4x4 矩阵,最后一行为 0,0,0,1?如果没有,有人可以向我解释为什么编译器坚持返回 vec4 吗?

我通过 VSExpress 2013、win7、移动式英特尔® 4 系列高速芯片组家族使用 C++

更新:

雷托的回答正是我所期望的。这是编译中的一个错误。既因为这是在 LA 上下文中唯一有意义的事情,又因为 LA 定义是 GLSL 文档中关于矩阵/矩阵和矩阵/向量乘法的引用;然而,即使在更新了我的视频芯片组驱动程序之后,编译也向我展示了相同的行为。其他人可以证实雷托所描述的行为吗?

@Reto,如果在 14 年 12 月 5 日之前没有人确认,我会接受您的正确答案,因为这似乎是唯一真正合乎逻辑的可能性。

最佳答案

这看起来像是 GLSL 编译器中的错误。这应该可以成功编译:

mat4x3 mat;
vec3 res = mat * vec4(1.0);

这应该会给出一个错误:

mat4x3 mat;
vec4 res = mat * vec4(1.0);

我在 3 种配置上进行了测试,所有配置都证实了这种行为:

  • 配备 Intel HD Graphics 4600 的 Windows 8.1。
  • 配备 NVIDIA GeForce GT 740M 的 Windows 8.1。
  • 配备 Intel Iris Pro 的 Mac OS Yosemite。

这也符合我对规范的理解。在 GLSL 3.30 规范文档中,mat4x3 描述为:

a floating-point matrix with 4 columns and 3 rows

乘法定义为(强调):

The operator is multiply (*), where both operands are matrices or one operand is a vector and the other a matrix. A right vector operand is treated as a column vector and a left vector operand as a row vector. In all these cases, it is required that the number of columns of the left operand is equal to the number of rows of the right operand. Then, the multiply (*) operation does a linear algebraic multiply, yielding an object that has the same number of rows as the left operand and the same number of columns as the right operand.

在这个例子中,“左操作数的列数”是4,这意味着向量需要有4个“行”,这是一个有4个元素的列向量。然后,由于左操作数有 3 行,因此得到的向量有 3 个“行”,这是一个具有 3 个元素的列向量。

这也是基于标准线性代数唯一有意义的事情。

关于opengl - GLSL 编译在 mat4x3 vec4 乘法上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27154648/

相关文章:

opengl - 在 GLSL 中选择立方体贴图的面

opengl - 概念 : what is the use of glDrawBuffer and glDrawBuffers?

c++ - 什么时候 glFlush 调用过于频繁?

c - 如何将与 OpenGL 一起使用的纹理嵌入到 C 数组中?

opengl - 如何获取片段着色器中当前多边形的顶点?

c++ - OpenGL C++ 不会显示纹理

c++ - 不支持 512 字节长度的顶点着色器

c++ - 如何使用 glReadPixels 读取 float 据

c++ - 执行一次类代码 C++

opengl - glTexParameter 每帧或初始化时