2
$\begingroup$

I'm rendering a full screen quad using gl_VertexID as GL_TRIANGLE_STRIP

const vec2 kVertices[4] = vec2[]
(
 vec2(-1.0,  1.0),
 vec2(-1.0, -1.0),
 vec2( 1.0,  1.0),
 vec2( 1.0, -1.0)
 );

void main() {
    gl_Position = vec4(kVertices[gl_VertexID], 1.0, 1.0);
    vs_out.texCoords = gl_Position.xy / 2.0 + 0.5;
    vs_out.instanceID = gl_InstanceID;
}

Geometry shader just passes coordinates through into the output called vTexCoords.

Then, in fragment shader, I output red and green into 2x2 texture using following code:

if (vTexCoords.x >= 0.95) {
    oFragData0 = vec4(0.0, 1.0, 0.0, 1.0);
} else {
    oFragData0 = vec4(1.0, 0.0, 0.0, 1.0);
}

I always assumed texture coordinates (0; 0), (0; 1), (1; 0), (1; 1) would map to the vertices, but the output is completely red, which means X value never reaches 1.0. Then I'm trying to use 0.75 (vTexCoords.x >= 0.75), which is a texel center, and half of the texture becomes green, as expected.

What am I misunderstanding here?

$\endgroup$
1
$\begingroup$

I may have misunderstood, but it sounds like the frame buffer you're rendering to is only 2×2 pixels? In that case, the fragment shader will be evaluated at each pixel center, which will be at 0.25 and 0.75 in UV space. So with a threshold of 0.95 you will indeed not get any pixels green.

If you use a larger framebuffer, say 100×100, then the last 5 pixels of each row should come out green.

$\endgroup$
0
$\begingroup$

An alternative solution would be to avoid the vertex/geometry shader completely and draw your fullscreen quad with

glRecti(-1, -1, 1, 1);

then just compile a fragment shader and generate your UVs with:

vec2 uv = gl_FragCoord.xy / framebufferSize.xy;

I don't know what you're doing but this could be potentially a fast forward solution for what you require. Maybe you're doing something else and you just broke down your problem to the bare minimum, i think in this case my answer should be still helpful for people seeking help in the problem as summarized in the headline.

$\endgroup$
  • $\begingroup$ That would pretty much result in the exact same outcome and isn't really related to his issue. $\endgroup$ – Christian Rau Apr 11 '18 at 15:12
  • $\begingroup$ this guy knows more about quad-mapping than everybody else ;) but nathan, you could explain the connection better. did you find a solution for that real 3d quad-mapping shader problem on your blog? $\endgroup$ – thewhiteambit Sep 3 '18 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.