-1
\$\begingroup\$

In the old days you would just do glOrtho in Projection, reset Modelview-Matrix and draw in [-1,1],[-1,1],0.

But now-a-days we have shaders and I wanted to avoid the use of matrices. My code so far:

#version 130

uniform float sizeX;
uniform float sizeY;
in vec2 position;
in vec2 coord;

out vec2 texcoord;

void main() 
{
  gl_Position = vec4(position.x * sizeX, position.y * sizeY, 0, 1);
  texcoord = coord;
}

Why does this not work?

If I use this instead:

gl_Position = ftransform();

The HUD is visible.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ What inputs are you using? The values in gl_Position must still be in NDC range: [-1,+1]. If your input position is supposed to be pixels, you need to properly adjust such that position 0,0 maps to -1,-1 and position WIDTH,HEIGHT maps to +1,+1 which is what the matrix from glOrtho does. \$\endgroup\$ Jun 4, 2013 at 22:17
  • \$\begingroup\$ @Sean: mister, you are my hero! i didn't test the essentials when i started and jumped straight to doing Vertex-array-nonsense. i still doesn't do what i think it should but at least something shows. still some culling issues and and the texture repeats.. but hey, it works! can i 'vote you up' or something like that? \$\endgroup\$
    – yogo1212
    Jun 4, 2013 at 22:35
  • \$\begingroup\$ I guess I can make an actual answer to upvote, though I'm unsure how valuable it would be in general without rephrasing the question to be a bit less specific to your code and more about 2D and NDC coordinates in general. \$\endgroup\$ Jun 4, 2013 at 22:40
  • 3
    \$\begingroup\$ "I wanted to avoid the use of matrices." Um, why? Is there something wrong with matrices? \$\endgroup\$ Jun 4, 2013 at 23:56

1 Answer 1

2
\$\begingroup\$

The values in gl_Position must still be in NDC range: [-1,+1]. If your input position is supposed to be pixels, you need to properly adjust such that position 0,0 maps to -1,-1 and position WIDTH,HEIGHT maps to +1,+1 which is what the matrix from glOrtho does.

Without a matrix, take any x,y and scale down by 2/width,2/height then subtract 1,1 to transform to NDC.

gl_Position = position * vec2(2.0/sizeX,2.0/sizeY) - vec2(1,1);

Instead of passing those sizes each time, you could use a vec2 uniform variable that already has 1/dim calculated in it.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .