为什么这个正交顶点着色器不能产生正确的答案? [英] Why isn't this orthographic vertex shader producing the correct answer?
问题描述
我的问题是,我有一个(工作的)正交顶点和片段着色器对,这些对允许我通过传入的"translateX"和"translateY"制服指定精灵的中心X和Y.我乘以projectionMatrix进行了硬编码,效果很好.一切工作只要进行正交运算即可.我传入此着色器的几何体以0、0、0为中心.
My issue is that I have a (working) orthographic vertex and fragment shader pair that allow me to specify center X and Y of a sprite via 'translateX' and 'translateY' uniforms being passed in. I multiply by a projectionMatrix that is hardcoded and works great. Everything works as far as orthographic operation. My incoming geometry to this shader is based around 0, 0, 0 as its center point.
我现在想算出平移后该中心点(局部坐标空间中的0、0、0)变为什么.我需要在片段着色器中了解此信息.我假设我可以在0、0、0处创建一个向量,然后应用相同的转换.但是,我没有得到正确的答案.
I want to now figure out what that center point (0, 0, 0 in local coordinate space) becomes after the translations. I need to know this information in the fragment shader. I assumed that I can create a vector at 0, 0, 0 and then apply the same translations. However, I'm NOT getting the correct answer.
我的问题:我在做什么错,我该如何调试正在发生的事情?我知道所计算的值一定是错误的,但是我不知道它是什么. (我的平台是在OS X上为OpenGL ES 2.0 iOS开发的Xcode 4.2)
My question: what I am I doing wrong, and how can I even debug what's going on? I know that the value being computed must be wrong, but I have no insight in to what it is. (My platform is Xcode 4.2 on OS X developing for OpenGL ES 2.0 iOS)
这是我的顶点着色器:
// Vertex Shader for pixel-accurate rendering
attribute vec4 a_position;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;
uniform float translateX;
uniform float translateY;
// Set up orthographic projection
// this is for 640 x 960
mat4 projectionMatrix = mat4( 2.0/960.0, 0.0, 0.0, -1.0,
0.0, 2.0/640.0, 0.0, -1.0,
0.0, 0.0, -1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
void main()
{
// Set position
gl_Position = a_position;
// Translate by the uniforms for offsetting
gl_Position.x += translateX;
gl_Position.y += translateY;
// Translate
gl_Position *= projectionMatrix;
// Do all the same translations to a vector with origin at 0,0,0
vec4 toPass = vec4(0, 0, 0, 1); // initialize. doesn't matter if w is 1 or 0
toPass.x += translateX;
toPass.y += translateY;
toPass *= projectionMatrix;
// this SHOULD pass the computed value to my fragment shader.
// unfortunately, whatever value is sent, isn't right.
//v_translatedOrigin = toPass;
// instead, I use this as a workaround, since I do know the correct values for my
// situation. of course this is hardcoded and is terrible.
v_translatedOrigin = vec4(500.0, 200.0, 0.0, 0.0);
}
In response to my orthographic matrix being wrong, the following is what wikipedia has to say about ortho projections, and my -1's look right. because in my case for example the 4th element of my mat should be -((right+left)/(right-left)) which is right of 960 left of 0, so -1 * (960/960) which is -1.
I've possibly uncovered the root issue here - what do you think?
推荐答案
为什么正交矩阵的每一列底部都有-1?那些应该为零.当然,那应该不会有任何影响.
Why does your ortho matrix have -1's in the bottom of each column? Those should be zeros. Granted, that should not affect anything.
我对此更加关注:
gl_Position *= projectionMatrix;
那是什么意思?矩阵乘法不是可交换的; M * a
与a * M
不同.那么,您希望gl_Position
在哪一边相乘?
What does that mean? Matrix multiplication is not commutative; M * a
is not the same as a * M
. So which side do you expect gl_Position
to be multiplied on?
奇怪的是,GLSL规范没有说明(我对此提交了错误报告).因此,您应该使用保证可以正常工作的东西:
Oddly, the GLSL spec does not say (I filed a bug report on this). So you should go with what is guaranteed to work:
gl_Position = projectionMatrix * gl_Position;
此外,您应该使用正确的矢量化代码.您应该有一个translate
制服,这是一个vec2
.然后,您可以执行gl_Position.xy = a_position.xy + translate;
.您必须用常量(gl_Position.zw = vec2(0, 1);
)填充Z和W.
Also, you should use proper vectorized code. You should have one translate
uniform, which is a vec2
. Then you can just do gl_Position.xy = a_position.xy + translate;
. You'll have to fill in the Z and W with constants (gl_Position.zw = vec2(0, 1);
).
GLSL中的矩阵是大列.前四个值是矩阵的第一个列,而不是第一行.您正在与转置的正交矩阵相乘.
Matrices in GLSL are column major. The first four values are the first column of the matrix, not the first row. You are multiplying with a transposed ortho matrix.
这篇关于为什么这个正交顶点着色器不能产生正确的答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!