Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Member Since 19 Jun 2012
Offline Last Active Jun 20 2012 03:38 AM

Topics I've Started

Problem with tile based texturing using shader (CG)

19 June 2012 - 11:19 AM


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 structurestruct VP_Output{ float4 wvpos : POSITION; float2 texCoord : TEXCOORD0; float4 pageVertXZ : COLOR;};// Vertex programVP_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 structurestruct 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.