Although I am relatively new to OpenGL, I have no trouble with matrix algebra, homogeneous coordinates, rotations and projections. I have set up my mvp matrix to get a good view of a cube and it renders nicely.
Next, instead of rendering the cube, I am trying to read the color from a background image. As expected, the texture is rendered clipped to where the cube is, but for some reason there is quite some distortion. (As per suggestion in the comments I am now dividing by w
[thought OpenGL did this].)
As far as I understand, the xy
coordinates in NDC should map linearly onto the window. Where does the distortion come from?
Vertex shader:
#version 330
uniform mat4 Mvp;
in vec3 in_shape;
out vec2 v_ndc;
void main() {
gl_Position = Mvp * vec4(in_shape, 1.0);
v_ndc = vec2(
gl_Position.x / gl_Position.w,
-gl_Position.y / gl_Position.w
) / 2 + 0.5;
}
Fragment shader:
#version 330
uniform sampler2D Background;
in vec2 v_ndc;
out vec4 f_color;
void main() {
f_color = vec4(texture(Background, v_ndc).rgb, 1.0);
}
gl_Position.xy/gl_Position.w
.gl_Position.xy
is clipspace – Rabbid76