Jump to content

  • Log In with Google      Sign In   
  • Create Account

Generating screen-aligned texture coordinates


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Starfox   Members   -  Reputation: 504

Like
0Likes
Like

Posted 27 April 2011 - 01:28 AM

I'm a bit puzzled by this: I have a ground plane polygon that I draw as such:


const float Extent = 128;
    glBegin(GL_QUADS);
    glVertexAttrib4f(0, -Extent, 0, Extent, 1);
    glVertexAttrib4f(0, Extent, 0, Extent, 1);
    glVertexAttrib4f(0, Extent, 0, -Extent, 1);
    glVertexAttrib4f(0, -Extent, 0, -Extent, 1);
    glEnd();

With the following vertex shader aiming to generate screen aligned texture coordinates in the range [0, 1]:


float2 pack(float2 In)
{
    return (In + 1.0) / 2.0;
}

float4 main(in float4 Position : ATTR0, 
            uniform float4x4 WorldMatrix, 
            uniform float4x4 WorldViewProjectionMatrix, 
            out float2 oScreenSpaceTexCoord : TEXCOORD3) : HPOS
{
    float4 ProjectedCoord = mul(WorldViewProjectionMatrix, Position);
    oScreenSpaceTexCoord = pack(ProjectedCoord.xy / ProjectedCoord.ww);
	return ProjectedCoord;
}


and using the following visualization fragment shader:

float4 main(in float2 ScreenSpaceTexCoord : TEXCOORD3) : COLOR
{
    return float4(ScreenSpaceTexCoord, 0, 1);
}


The results tend to be in the range [0, 1] but they're messed up and they change depending on the camera - ie pixel at coords (x,y) can have a different texcoord based on the camera position, which is wrong - a pixel's texcoord should only depend on its Normalized Device Coordinates and by connection, screen coordinates. Any ideas?
Holy crap I started a blog - http://unobvious.typepad.com/

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 30388

Like
0Likes
Like

Posted 27 April 2011 - 01:38 AM

IIRC - You've got to perform the perspective divide after interpolation (i.e. in the fragment shader), rather than doing it immediately in the vertex shader.

#3 Starfox   Members   -  Reputation: 504

Like
1Likes
Like

Posted 27 April 2011 - 10:07 AM

Aha, that was it, thanks a million.
Holy crap I started a blog - http://unobvious.typepad.com/




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS