Changing Vertex Shader does not effects.

huangapple go评论48阅读模式
英文:

Changing Vertex Shader does not effects

问题

在下面的代码中,我预期通过将gl_Position乘以0.5来改变纹理的位置,但并没有发生这种情况。

顶点着色器代码:

uniform mat4 u_MVP;
attribute vec4 a_Position;
void main()
{
   gl_Position = u_MVP * 0.5 * a_Position;
}

我错在哪里?

如何修改顶点着色器?

英文:

I have expected in the bellow code when multiplying gl_Position by 0.5 to change the position of a texture.
But It does not happen.
Vertex shader code:

       "uniform mat4 u_MVP;            \n" \
       "attribute vec4 a_Position;     \n" \
       "void main()                    \n" \
       "{                              \n" \
       "   gl_Position = u_MVP   * 0.5  * a_Position;   \n" \
       "}                              \n"; 

what is my wrong?

How can I change vertex shaders?

答案1

得分: 3

以下是您要翻译的内容:

这将通过0.5缩放x、y、z和w,这意味着屏幕上的x和y位置保持不变,因为x被w除以,y也被w除以。例如,1/1等于1,如果两边都减半,那么0.5/0.5也等于1,所以没有区别。

相反,您可以使用例如 vec4(0.5, 0.5, 0.5, 1.0),这样w不受影响。

我也不确定按这个顺序进行乘法是否有意义。我会这样写:

gl_Position = (u_MVP * a_Position) * vec4(0.5, 0.5, 0.5, 1.0);

这将在矩阵之后将屏幕坐标减半,将整个图像缩小到屏幕的中间。

或者,您可以在矩阵之前将坐标减半,这通常会产生相同的效果,这取决于您使用的矩阵。在大多数情况下,这将使每个对象朝向其自身的原点(0,0,0点)缩小。

gl_Position = u_MVP * (a_Position * vec4(0.5, 0.5, 0.5, 1.0));

英文:

This scales the x, y, z and w all by 0.5, which means the x and y position on the screen stays the same, because x is divided by w and y is divided by w. 1/1 is 1, for example, and if you halve both sides, then 0.5/0.5 is also 1 so there's no difference.

Instead, you could use for example vec4(0.5, 0.5, 0.5, 1.0) so that w is not affected.

I'm also not sure whether doing the multiplication in this order makes sense. I would write it as:

gl_Position = (u_MVP * a_Position) * vec4(0.5,0.5,0.5,1.0);

This would halve the screen coordinates after the matrix, shrinking the whole picture towards the middle of the screen.

Or you could halve the coordinates before the matrix, which generally does not do the same thing, depending on which matrix you are using. In most cases this would shrink each object towards its own origin (0,0,0 point).

gl_Position = u_MVP * (a_Position * vec4(0.5,0.5,0.5,1.0));

huangapple
  • 本文由 发表于 2023年3月21日 01:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定