我的问题是我有一个(工作的)正交顶点和片段着色器对,允许我通过传入的“translateX”和“translateY”制服指定精灵的中心 X 和 Y。我乘以一个硬编码的 projectionMatrix 和效果很好。就正交运算而言,一切都有效。我传入此着色器的几何图形以 0、0、0 为中心点。
我现在想弄清楚平移后的中心点(局部坐标空间中的 0、0、0)变成了什么。我需要在片段着色器中知道这些信息。我假设我可以在 0、0、0 处创建一个向量,然后应用相同的平移。但是,我没有得到正确的答案。
我的问题:我做错了什么,我怎么能调试发生了什么?我知道正在计算的值一定是错误的,但我不知道它是什么。(我的平台是为 OpenGL ES 2.0 iOS 开发的 OS X 上的 Xcode 4.2)
这是我的顶点着色器:
// 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);
}
编辑:作为对我的正交矩阵错误的回应,以下是维基百科关于正交投影的说法,而我的 -1 看起来是正确的。因为在我的情况下,例如我的垫子的第 4 个元素应该是 -((right+left)/(right-left)),它是 0 左边 960 的右边,所以 -1 * (960/960) 是 -1 .
编辑:我可能在这里发现了根本问题——你怎么看?