Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Problem with tile based texturing using shader (CG)


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
1 reply to this topic

#1 whisp   Members   -  Reputation: 102

Like
0Likes
Like

Posted 19 June 2012 - 11:19 AM

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 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.

Thanks
whisp

Sponsor:

#2 whisp   Members   -  Reputation: 102

Like
0Likes
Like

Posted 20 June 2012 - 01:44 AM

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.




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