Sign in to follow this  
Followers 0
whisp

Problem with tile based texturing using shader (CG)

1 post in this topic

Hi!

I'm desperately trying to get a simple shader running. What i want is to texture a terrain page of 65 * 65 vertices with tiles from a textur atlas, using a second texture, the tile-map, to lookup the respective tile-type to use.

Shaders are new to me, so it's likely that i didn't understand some of the logic i'm using. Instead of the tiles looked up in the tile-map it just repeats the whole tile-atlas a few times over the terrain page.

In the following my shader code commented with what i think the respective lines should do. It should output the top left tile from the texture atlas, but instead it outputs, as mentioned earlier, the whole tile-atlas a few times over the page:

[source lang="cpp"]// Output structure
struct VP_Output
{
float4 wvpos : POSITION;
float2 texCoord : TEXCOORD0;
float4 pageVertXZ : COLOR;
};

// Vertex program
VP_Output TerrainVP(
float4 position : POSITION,
uniform float4x4 worldViewProj)
{
VP_Output OUT;

// World position
OUT.wvpos = mul(worldViewProj, position);

// Position relative to model (top left vertex xz in page is 0, 0)
OUT.pageVertXZ.xy = position.xz;

// Texture Coordinates, 1/8 of the tileset (tile set contains 8 * 8 tiles, 256 * 256 pixels)
// In the fragment shader the texture coordinates will be set to 0,0 + the automatic interpolation
OUT.texCoord = OUT.pageVertXZ.xy * 0.125;

return OUT;
}

// Output structure
struct FP_Output
{
float4 color : COLOR;
};

FP_Output TerrainFP(float2 texCoord : TEXCOORD0,
float4 pageVertXZ : COLOR,
uniform sampler2D lookupTex : TEXUNIT1,
uniform sampler2D decal : TEXUNIT0)
{
FP_Output OUT;

// set texCoord relative to 0,0
float2 tc2;
tc2.xy = texCoord - pageVertXZ.xy * 0.125;

// This should output the respective pixel color of the first tile (top left) from the atlas
OUT.color = tex2D(decal, tc2);
return OUT;
}[/source]

Any help is appreciated.

Thanks
whisp
0

Share this post


Link to post
Share on other sites
I figured out a possible solution for the problem.

The data passed from a vertex program to a fragment program always gets interpolated. It's not possible to pass custom values that don't get interpolated, thus the interpolation needs to be removed in the fragment shader.

In this case i need the non-interpolated model-space-coordinates of the vertices in order to look up the tile-map texture.

In the vertex shader i calculated the texture coordinates:
// Texture Coordinates, 1/8 of the tileset
OUT.texCoord = OUT.pageVertXZ.xy * 0.125;

With this coordinates the vertex position can be calculated in the fragment shader:
// set texCoord relative to 0,0
float2 pageVertXZ = floor(indata.texCoord * 8);

The interpolation-value just gets cropped, what's left is the vertex coordinate in model space.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0