Jump to content

View more

Image of the Day

The night is still, but the invasion brings chaos. #screenshotsaturday #hanako #indiegame #gameart #ue4 #samurai https://t.co/cgILXuokoS
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Generating screen-aligned texture coordinates

4: Adsense

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   

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/

#2 Hodgman   Moderators   

50631
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   

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.