Why doesn't this spelling vertex shader give the right answer?

My problem is that I have a (working) spelling vertex and a couple of fragment shaders that allow me to specify the center X and Y of the sprite through the transmitted forms "translateX" and "translateY". I multiply by the projection matrix, which is hard-coded and works great. Everything works before the spelling operation. My input geometry for this shader is based on 0, 0, 0 as the center point.

Now I want to find out exactly which point of the center (0, 0, 0 in the local coordinate space) becomes after translations. I need to know this information in the fragment shader. I suggested that I can create a vector at 0, 0, 0, and then apply the same translations. However, I am NOT getting the correct answer.

My question is: what am I doing wrong, and how can I debug what is happening? I know that the calculated value must be wrong, but I don’t understand what it is. (My Xcode 4.2 platform for OS X for OpenGL ES 2.0 iOS)

Here is my vertex shader:

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

EDIT: In response to my spelling matrix incorrectly, the following: wikipedia should say about orthoprojections, and my -1 looks right. because in my case, for example, the 4th element of my mat should be - ((right + left) / (right-left)), which has a right of 960 to the left of 0, therefore -1 * (960/960), which is -1.

EDIT: I may have uncovered a root problem here - what do you think?

enter image description here

+3
shader opengl vertex-shader
source share
2 answers

Why does your ortho matrix have -1 at the bottom of each column? It must be zeros. Of course, this should not affect anything.

I am more worried about this:

 gl_Position *= projectionMatrix; 

What does it mean? Matrix multiplication is not commutative; M * a does not match a * M So which side do you expect to multiply by gl_Position ?

Oddly enough, the GLSL specification does not say (I wrote a bug report). So you have to go with what is guaranteed to work:

 gl_Position = projectionMatrix * gl_Position; 

In addition, you must use the correct vector code. You should have one form of translate , which is equal to vec2 . Then you can just do gl_Position.xy = a_position.xy + translate; . You will need to fill Z and W with constants ( gl_Position.zw = vec2(0, 1); ).


Matrices in GLSL are columns. The first four values ​​are the first column of the matrix, not the first row. You multiply by the transposed ortho matrix.

+4
source share

I need to respond to the mood of Nicole Bolas. Two mistakes that you have to work with are frustrating, but they do not make them less dangerous. The fact that things appear where you expect is probably because the translation part of your matrix is ​​0, 0, 0.

The equation you posted is correct, but the notation has a large row value, and OpenGL in the column: 4 columns of the OpenGL's matrix

I am starting this material in every new project that I am starting. This site is a really good resource that helped me keep it straight. They have another page on projection matrices .

If you are not sure of the correct spelling (this is not the case now), try connecting the same values ​​to glOrtho and reading the values ​​back from GL_PROJECTION.

+2
source share

All Articles